Skip to main content

Manifold-Constrained Hyper-Connections: Stable multi-history skip connections for deep neural networks.

Project description

mHC Logo

mhc: Manifold-Constrained Hyper-Connections

Honey Badger Stability for Deeper, More Stable Neural Networks.

Python 3.9+ PyTorch 2.0+ License: MIT CI

DocumentationExamplesPaperContributing


🎯 What is mHC?

mhc is a next-generation PyTorch library that reimagines residual connections. Instead of simple one-to-one skips, mHC learns to dynamically mix multiple historical network states through geometrically constrained manifolds, bringing Honey Badger toughness to your gradients.

🚀 High Performance 🧠 Smart Memory 🛠️ Drop-in Ease
Reach deeper than ever before with optimized gradient flow. Dynamically mix past states for richer feature representation. Transform any model to mHC with a single line of code.

mHC Architecture

🚀 Quick Start

Installation

# With uv (recommended)
uv pip install -e .

# Or standard pip
pip install -e .

30-Second Example

import torch
from mhc import MHCSequential

# Create a model with mHC skip connections
model = MHCSequential([
    nn.Linear(64, 64),
    nn.ReLU(),
    nn.Linear(64, 64),
    nn.ReLU(),
    nn.Linear(64, 32)
], max_history=4, mode="mhc", constraint="simplex")

# Use it like any PyTorch model
x = torch.randn(8, 64)
output = model(x)

Inject into Existing Models

Transform any model to use mHC with one line:

from mhc import inject_mhc
import torchvision.models as models

model = models.resnet50(pretrained=True)
inject_mhc(model, target_types=nn.Conv2d, max_history=4)

🤔 Why mHC?

The Gradient Bottleneck

Standard residual connections only look one step back: $x_{l+1} = x_l + f(x_l)$. While revolutionary, this narrow window limits the network's ability to leverage long-range dependencies and can lead to diminishing returns in extremely deep architectures.

The mHC Breakthrough

mHC implements a history-aware manifold that mixes a sliding window of $H$ past representations:

$$ x_{l+1} = f(x_l) + \sum_{k=l-H+1}^{l} \alpha_{l,k}, x_k $$

Where:

  • $\alpha_{l,k}$: Learned mixing weights optimized for feature relevance.
  • Constraints: Weights are projected onto stable manifolds (Simplex, Identity-preserving, or Doubly Stochastic) to ensure mathematical convergence.

Key Advantages

Benefit Description
Deep Stability Geometric constraints prevent gradient explosion even at 200+ layers.
Feature Fusion Multi-history mixing allows layers to recover lost spatial or semantic info.
Adaptive Flow The network learns which historical states are most important for the current layer.

📊 Performance Highlights

Experiments with 50-layer networks show:

  • 2x Faster Convergence compared to standard ResNet on deep MLPs.
  • Superior Gradient Stability through geometric manifold constraints.
  • Minimal Overhead (~10% additional compute for 4x history).

[!TIP] Run the benchmark yourself: uv run python experiments/benchmark_stability.py


📊 Visualizing Results

Mixing Weights Gradient Flow
Mixing Weights Gradient Flow
Learned coefficients over time Improved backpropagation signal

🌳 Repository Structure

MHC/
├── mhc/                     # Core package
│   ├── constraints/         # Mathematical projections
│   ├── layers/              # mHC Skip implementations
│   ├── tf/                  # TensorFlow compatibility
│   └── utils/               # Injection & logging tools
├── tests/                   # Robust PyTest suite
├── docs/                    # Documentation sources
└── examples/                # Quick-start notebooks

📜 Roadmap

  • v0.4: Managed Layers & Model Injection
  • v0.5: Deep Stability Benchmark Suite
  • [/] v0.6: PyPI Release & Documentation Site
  • v0.7: PyTorch Lightning & Hugging Face Integration
  • v0.8: Pre-trained Model Zoo

⭐ Star us on GitHub!

Report BugRequest FeatureDiscussions

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

mhc-0.5.0.tar.gz (32.0 kB view details)

Uploaded Source

Built Distribution

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

mhc-0.5.0-py3-none-any.whl (31.8 kB view details)

Uploaded Python 3

File details

Details for the file mhc-0.5.0.tar.gz.

File metadata

  • Download URL: mhc-0.5.0.tar.gz
  • Upload date:
  • Size: 32.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for mhc-0.5.0.tar.gz
Algorithm Hash digest
SHA256 8bf7e0af14fe256c764e5f4a4b58d0e04b9daf954e31499d791bdaf56cf95ed5
MD5 082e2728c5ebddf33b8e80267cb93e03
BLAKE2b-256 e95be2e33481f5a5362371723306d2b827e9938d7cc05ed646cfea43d40c8062

See more details on using hashes here.

File details

Details for the file mhc-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: mhc-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 31.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for mhc-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f8be65eff1cb847067a869c92a78cf935da7b046b32d594edcf627688a309cf3
MD5 a5c9efa4f4e248af2998c40fef600196
BLAKE2b-256 a84708c7a25c04bc7a1be191a809a54fba9912298daea0cb34c57b695d702cbe

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