High-performance MLX implementation of Manifold-Constrained Hyper-Connections (mHC)
Project description
mhc-mlx
High-performance MLX implementation of Manifold-Constrained Hyper-Connections (mHC).
mHC improves training stability and performance in deep architectures by constraining residual connections to the Birkhoff polytope (doubly stochastic matrices). This library provides optimized Metal kernels for Apple Silicon and a fast compiled fallback for other platforms.
Original Paper: mHC: Manifold-Constrained Hyper-Connections (DeepSeek-AI)
Installation
pip install mhc-mlx
Compatibility
- Primary: macOS + Apple Silicon (M1, M2, M3, M4) for peak performance.
- Support: Linux (CPU/CUDA) and Intel Macs via automatic pure-MLX compiled fallback.
- Software: MLX >= 0.30.0.
Quick Start (30-second Demo)
import mlx.core as mx
import mlx.nn as nn
from mhc_mlx import MHCRewire
# 1. Take any standard MLX layer
layer = nn.Linear(2048, 2048)
# 2. Wrap it with mHC stability (automatically uses optimized Metal kernels)
model = MHCRewire(layer, dims=2048, n=32)
# 3. Run forward pass
x = mx.random.normal((1, 2048))
y = model(x)
mx.eval(y)
# 4. Run backward pass (fully vectorized)
loss_fn = lambda m, x: mx.sum(m(x))
grads = mx.grad(loss_fn)(model, x)
mx.eval(grads)
print(f"Output shape: {y.shape}") # (1, 2048)
Note: You can also use from mlx_mhc import MHCRewire for a community-friendly alias.
Performance
mhc-mlx utilizes fused Metal kernels to minimize memory bandwidth bottlenecks. We benchmarked on an Apple M4 Pro (macOS 15.6).
Comparative Benchmarks
Comparison with a standard MLX implementation of mHC ($C=512$):
| Metric | mhc-mlx | Baseline Impl | Speedup |
|---|---|---|---|
| Inference Latency ($B=1$) | 392 us | 1120 us | 2.86x |
| Training Throughput ($B=32$) | 105 us | 866 us | 8.25x |
Why It's Faster
| Approach | Architecture | Impact |
|---|---|---|
| Baseline | Multiple kernel launches | High memory overhead, low GPU occupancy |
| mhc-mlx | Fused Metal Kernels | Minimal memory round-trips, maximal bandwidth |
Reproduce Benchmarks
Run the standardized benchmark suite on your own hardware:
mhc-mlx-bench --mode latency --C 512,2048,4096
Key Optimizations
- "Zero-Cost" Weight Folding:
MHCRewirefolds scaling directly intonn.Linearweights where possible. - Quantized Layer Support: Seamlessly wraps
nn.QuantizedLinear(4-bit/8-bit). - Fully Fused Kernel: Single-pass kernel for Aggregate + RMS + Mix + Add.
- Adaptive Dispatch: Runtime heuristic selects the fastest kernel strategy for your workload.
Diagnostics
If you encounter issues, run the diagnostic utility:
mhc-mlx-info
Set MHC_MLX_DISABLE_METAL=1 in your environment to force the pure-MLX reference path (useful for debugging or non-Metal hardware).
Support Policy
- Tested: macOS (Apple Silicon) + Linux (CPU/CUDA) using MLX 0.30.0+.
- Best Effort: Intel Macs, older macOS versions, and older MLX versions.
- Reporting: Please include OS, MLX version, and
mhc-mlx-infooutput in bug reports.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mhc_mlx-0.6.1.tar.gz.
File metadata
- Download URL: mhc_mlx-0.6.1.tar.gz
- Upload date:
- Size: 60.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f66137fd8b0d77463ae753c27a6210b201ebb4496b56e02989d4bde0107d5ca4
|
|
| MD5 |
f45f60a4b02ef69d24d95c59f2b682a3
|
|
| BLAKE2b-256 |
7dd73edd00b5597bb4844816419f5faaa5aee56026f1d86916b2a57a1b81937a
|
Provenance
The following attestation bundles were made for mhc_mlx-0.6.1.tar.gz:
Publisher:
publish.yml on svdrecbd/mhc-mlx
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mhc_mlx-0.6.1.tar.gz -
Subject digest:
f66137fd8b0d77463ae753c27a6210b201ebb4496b56e02989d4bde0107d5ca4 - Sigstore transparency entry: 815062628
- Sigstore integration time:
-
Permalink:
svdrecbd/mhc-mlx@89b728b2fd8282d08682dae4064ffd1b96b36449 -
Branch / Tag:
refs/tags/v0.6.1 - Owner: https://github.com/svdrecbd
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@89b728b2fd8282d08682dae4064ffd1b96b36449 -
Trigger Event:
push
-
Statement type:
File details
Details for the file mhc_mlx-0.6.1-py3-none-any.whl.
File metadata
- Download URL: mhc_mlx-0.6.1-py3-none-any.whl
- Upload date:
- Size: 79.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
70f867a7c2381642848af9a8ddde18f138c1873e34907802d282613df9ba76a4
|
|
| MD5 |
162c1204202ec3bfb314b4a45c65c269
|
|
| BLAKE2b-256 |
ec0d074fc0d4764553ef3533bdbff84db32c35c79270ae97e89f592d253a32c2
|
Provenance
The following attestation bundles were made for mhc_mlx-0.6.1-py3-none-any.whl:
Publisher:
publish.yml on svdrecbd/mhc-mlx
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mhc_mlx-0.6.1-py3-none-any.whl -
Subject digest:
70f867a7c2381642848af9a8ddde18f138c1873e34907802d282613df9ba76a4 - Sigstore transparency entry: 815062634
- Sigstore integration time:
-
Permalink:
svdrecbd/mhc-mlx@89b728b2fd8282d08682dae4064ffd1b96b36449 -
Branch / Tag:
refs/tags/v0.6.1 - Owner: https://github.com/svdrecbd
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@89b728b2fd8282d08682dae4064ffd1b96b36449 -
Trigger Event:
push
-
Statement type: