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.1.tar.gz (12.6 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.1-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: whc-1.0.1.tar.gz
  • Upload date:
  • Size: 12.6 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.1.tar.gz
Algorithm Hash digest
SHA256 0b74f9ba24391b7704e190fe631eda6d84b38c9c85a7224e4860de10451de739
MD5 e293d55b758e8a429f002d36de41e75b
BLAKE2b-256 ded73d4041a2474274074c0a71105f51c3fb2f8a3b60d108865e0deef81b5d35

See more details on using hashes here.

File details

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

File metadata

  • Download URL: whc-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.0 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 446191506be9fb3f45052423688dd5d2e32ca31390a1eceeca3991ba4daa9d3a
MD5 aec1186afbb1b30ec80349f6c44895f8
BLAKE2b-256 0f90951416f27266f2c42536ee93ff5b881d20049c2e9af7022e95f1b466c3bb

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