torch-circuit
A PyTorch extension for building neural networks with skip connections and repeatable blocks.
Features
- Named Skip Connections: Easily implement ResNet-style skip connections with named references
- Repeatable Blocks: Define blocks once and repeat them multiple times without code duplication
- Visualization: Generate circuit diagrams of your network architecture
- PyTorch Compatible: Works seamlessly with existing PyTorch code and training loops
Installation
From Source
git clone https://github.com/your-username/torch-circuit.git
cd torch-circuit
pip install -e .
From PyPI (when published)
pip install torch-circuit
Quick Start
import torch
import torch.nn as nn
from torch_circuit import Circuit, SaveInput, GetInput, StartBlock, EndBlock
# Create a simple ResNet-style model with skip connections
model = Circuit(
nn.Conv2d(3, 64, 3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(),
# Repeatable ResNet block
StartBlock("resnet", num_repeats=3),
SaveInput("residual"),
nn.Conv2d(64, 64, 3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.Conv2d(64, 64, 3, padding=1),
nn.BatchNorm2d(64),
GetInput("residual", op=torch.add), # Add skip connection
nn.ReLU(),
EndBlock("resnet"),
nn.AdaptiveAvgPool2d((1, 1)),
nn.Flatten(),
nn.Linear(64, 10)
)
# Use like any PyTorch model
x = torch.randn(1, 3, 32, 32)
output = model(x)
# Visualize the architecture
model.visualize(save_path="resnet_example.pdf")
Key Components
Circuit
The main container class that supports skip connections and repeatable blocks.
SaveInput / GetInput
SaveInput(name): Save the input tensor with a given nameGetInput(name, op=torch.add): Retrieve saved tensor and combine it with the current tensor using an operation (e.g. addition, concatenation)
StartBlock / EndBlock
StartBlock(name, num_repeats=N): Mark the beginning of a repeatable blockEndBlock(name): Mark the end of a repeatable block
Examples
See the examples/ directory for a complete example demonstrating equivalence to standard PyTorch implementations:
examples/resnet_mnist.py: ResNet architecture on MNIST dataset
Architecture Visualization
torch-circuit can generate simple visual diagrams of your network architecture:
model.visualize(save_path="architecture.pdf")
Advanced Usage
Custom Operations
You can use custom operations for combining skip connections:
# Element-wise multiplication instead of addition
GetInput("residual", op=torch.mul)
# Custom lambda function
GetInput("residual", op=lambda x, y: x + 0.5 * y)
License
This project is licensed under the MIT License - see the LICENSE file for details.
Metadata
Release files for torch-circuit 0.9.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| torch_circuit-0.9.1.tar.gz | 15.4 kB | Without provenance | Details for torch_circuit-0.9.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| torch_circuit-0.9.1-py3-none-any.whl | Python 3 | none | any | Details for torch_circuit-0.9.1-py3-none-any.whl |
Total release size: 24.5 kB
Release files / torch_circuit-0.9.1.tar.gz
| Download URL | torch_circuit-0.9.1.tar.gz |
|---|---|
| Size | 15.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5b5c01fad9d1680e85cb60606a70ac287e2158d043075cd08abcf2413165149a
|
|
BLAKE2b-256 checksum How to use checksums |
374932350770d93d2960b7ad4b84aed4bc69c19919bac65e8b5f5052dbcb8b04
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.3
|
Release files / torch_circuit-0.9.1-py3-none-any.whl
| Download URL | torch_circuit-0.9.1-py3-none-any.whl |
|---|---|
| Size | 9.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bc20b7f4f44e3c9169bf40cd00999a8bc98733e020160d008b147e565de4096f
|
|
BLAKE2b-256 checksum How to use checksums |
1a20233dc5f8e9ba028929696179e0d73655b0ec01d35f5f7261b0cd9f2f379e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.3
|
Security for torch-circuit 0.9.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.