Skip to main content

Wormhole Hyperconnections — Drop-in replacement for residual connections

Project description

Wormhole Hyperconnections (WHC)

PyPI version Python 3.8+ License: MIT PyTorch

A drop-in replacement for the standard residual connection x = x + layer(x).

WHC replaces the single residual path with n parallel lanes and learnable mixing matrices, delivering:

  • 6–15% faster training than standard residual
  • Lower final loss on benchmark tasks
  • Proven stability — spectral norm ≤ 1
  • 82 parameters for the mixing matrix (vs mHC's 32,768)
  • 2× faster than mHC in wall-clock time

Quick Start

from whc import WormholeHyperconnection

whc = WormholeHyperconnection(
    dim=512,
    expansion_rate=4,
    num_layers=12,
)

x = whc.expand(x)                      # (B, T, dim) -> (B, T, n, dim)
for i, layer in enumerate(transformer_blocks):
    x = whc(x, layer, layer_idx=i)      # replaces `x = x + layer(x)`
x = whc.reduce(x)                      # (B, T, n, dim) -> (B, T, dim)

Installation

From PyPI (Recommended)

pip install whc

From Source

git clone https://github.com/fardinsabid/wHC.git
cd wHC
pip install -e .

Requirements

  • Python >= 3.8
  • PyTorch >= 2.0.0

Key Features

Feature Description
Drop-in replacement Replace x = x + layer(x) with x = whc(x, layer=layer)
n parallel lanes Instead of 1 fixed path
Learnable mixing Network learns how lanes interact
Proven stability Spectral norm ≤ 1 guarantees no explosion
No iterations Closed-form kernel, no Sinkhorn-Knopp
Minimal overhead 82 parameters for the mixing matrix
GPU ready Runs on CUDA, CPU, MPS

Benchmarks

Signal Gain vs Depth

Stability Comparison

Method Mean Final Gain Max Final Gain
Unconstrained HC 9.54×10⁵ 4.77×10⁶
mHC (DeepSeek) 0.42 0.68
WHC 0.48 0.63

WHC and mHC stay bounded; unconstrained HC explodes.

Training Loss

Training Loss

Method Final Loss Time (s)
Standard Residual 0.3765 0.91
mHC-static 0.6966 11.01
WHC 0.3590 5.54

WHC achieves lower loss and is 2× faster than mHC.

Parameter Count

Component Parameters
WormholeKernel (n=8) 82
mHC H_res generator (dim=512, n=8) 32,768

WHC uses ~400× fewer parameters for the mixing matrix.


Usage

1. Basic Usage (Manual Integration)

from whc import WormholeHyperconnection

# Create WHC wrapper
whc = WormholeHyperconnection(dim=512, expansion_rate=4, num_layers=12)

# Forward pass
x = whc.expand(x)
for i, layer in enumerate(transformer_blocks):
    x = whc(x, layer, layer_idx=i)
x = whc.reduce(x)

2. Single Layer

whc = WormholeHyperconnection(dim=512, expansion_rate=4, num_layers=1)

x = whc.expand(x)
x = whc(x, layer, layer_idx=0)
x = whc.reduce(x)

3. With Pre-computed Layer Output

layer_out = layer(x)
x = whc(x, layer_out=layer_out)

4. Custom Configuration

whc = WormholeHyperconnection(
    dim=768,
    expansion_rate=8,           # Number of parallel lanes
    num_layers=24,              # Number of layers in your stack
    manifold_dim=3,             # Dimension of the wormhole manifold
    shared_kernel=False,        # Share kernel across layers?
)

Examples

Example Description Run
simple_mlp.py MLP with WHC replacing residuals python examples/simple_mlp.py
transformer_whc.py Transformer with WHC replacing residuals python examples/transformer_whc.py
resnet_whc.py ResNet with WHC replacing residuals python examples/resnet_whc.py

Tests

pytest tests/ -v
Test File What It Tests
test_kernel.py Kernel stability, spectral norm ≤ 1
test_whc.py Shape, forward pass, parameters
test_gradients.py Gradient flow, no NaNs, no explosion

All 21 tests passed ✅


Research Paper

The full mathematical derivation, stability proof, and experimental results are in:

📄 papers/whc.pdf

Key contributions:

  • Spectral normalization (closed-form, no iteration)
  • Wormhole kernel (physics-inspired, interpretable)
  • 82 parameters for H_res (vs mHC's 32,768)
  • 2× faster than mHC
  • Better loss than standard residual

Project Structure

whc/
├── whc.py                          # Core implementation
├── README.md                       # This file
├── LICENSE                         # MIT License
├── setup.py                        # Package installer
├── pyproject.toml                  # Build config
├── requirements.txt                # Dependencies
├── examples/
│   ├── simple_mlp.py               # MLP with WHC
│   ├── transformer_whc.py          # Transformer with WHC
│   └── resnet_whc.py               # ResNet with WHC
├── papers/
│   └── whc.pdf                     # Research paper
├── assets/
│   ├── whc_stability.png           # Stability comparison
│   └── whc_training.png            # Training loss curves
└── tests/
    ├── test_kernel.py              # Kernel unit tests
    ├── test_whc.py                 # WHC unit tests
    └── test_gradients.py           # Gradient stability tests

Citation

If you use WHC in your research, please cite:

@misc{sabid2026whc,
  author = {Fardin Sabid},
  title = {Wormhole Hyperconnections: A Physics-Inspired Framework for Stable Deep Residual Learning},
  year = {2026},
  publisher = {GitHub},
  howpublished = {\url{https://github.com/fardinsabid/wHC}}
}

License

MIT License — see LICENSE for details.


Author

Fardin Sabid

  • GitHub: @fardinsabid
  • Research: Deep Learning Optimization, Physics-Inspired Architectures

Acknowledgments

WHC builds on the foundation of:

  • He et al. (2016) — Residual connections
  • Zhu et al. (2025) — Hyper-Connections (HC)
  • Xie et al. (2025/2026) — Manifold-Constrained Hyper-Connections (mHC)
  • Kipf & Welling (2017) — Graph Convolutional Networks

Star Us

If you find WHC useful, please ⭐ star the repository!


The standard residual was 2016. WHC is 2026.

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

whc-1.0.0.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

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

whc-1.0.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file whc-1.0.0.tar.gz.

File metadata

  • Download URL: whc-1.0.0.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for whc-1.0.0.tar.gz
Algorithm Hash digest
SHA256 0ae36f523a183635d584dee3bf6bfdf4c1e1a4e8c82eedec3f7388c4f65cbba1
MD5 d6df87340f56aba30f966bea56ee07c4
BLAKE2b-256 b097cd007f7e96e1b96762f35d051db7e83db88364b80d9b68c9960309029e9e

See more details on using hashes here.

File details

Details for the file whc-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: whc-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for whc-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d30b2c2386a8e393ccf26ca5478d01427bc2059467872506a97596a7fc4f658b
MD5 2f145cfde90185625cedd8389ecda0e3
BLAKE2b-256 db96dee6abd070cc8984557886747841fe515bde10109e91efe17c8c2dd9a8c1

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