Skip to main content

A PyTorch extension for easy skip connections and repeatable blocks

Project description

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 name
  • GetInput(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 block
  • EndBlock(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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torch_circuit-0.9.1.tar.gz (15.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

torch_circuit-0.9.1-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file torch_circuit-0.9.1.tar.gz.

File metadata

  • Download URL: torch_circuit-0.9.1.tar.gz
  • Upload date:
  • Size: 15.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for torch_circuit-0.9.1.tar.gz
Algorithm Hash digest
SHA256 5b5c01fad9d1680e85cb60606a70ac287e2158d043075cd08abcf2413165149a
MD5 52b43d7ae0db4081fc85c3ba6af895a4
BLAKE2b-256 374932350770d93d2960b7ad4b84aed4bc69c19919bac65e8b5f5052dbcb8b04

See more details on using hashes here.

File details

Details for the file torch_circuit-0.9.1-py3-none-any.whl.

File metadata

  • Download URL: torch_circuit-0.9.1-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for torch_circuit-0.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bc20b7f4f44e3c9169bf40cd00999a8bc98733e020160d008b147e565de4096f
MD5 e1b7619a8be871c88c35fdd58a9e9148
BLAKE2b-256 1a20233dc5f8e9ba028929696179e0d73655b0ec01d35f5f7261b0cd9f2f379e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page