Skip to main content

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

  • Universal Rewiring: MHCRewire wraps any existing nn.Module (Linear, Conv2d) to apply mHC dynamics.
  • 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-info output in bug reports.

License

MIT

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_mlx-0.6.2.tar.gz (60.7 kB view details)

Uploaded Source

Built Distribution

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

mhc_mlx-0.6.2-py3-none-any.whl (79.5 kB view details)

Uploaded Python 3

File details

Details for the file mhc_mlx-0.6.2.tar.gz.

File metadata

  • Download URL: mhc_mlx-0.6.2.tar.gz
  • Upload date:
  • Size: 60.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mhc_mlx-0.6.2.tar.gz
Algorithm Hash digest
SHA256 7b17abf10fed2ef35b8a85dce3be63a660a512cece4630ef716639b2a7360329
MD5 6eafa33c7e7bee9f894edd3eb9f9c12f
BLAKE2b-256 963975dfed30a8101a85178ff1fdb66c4ed25d87e35a99e71076baa766994ee7

See more details on using hashes here.

Provenance

The following attestation bundles were made for mhc_mlx-0.6.2.tar.gz:

Publisher: publish.yml on svdrecbd/mhc-mlx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mhc_mlx-0.6.2-py3-none-any.whl.

File metadata

  • Download URL: mhc_mlx-0.6.2-py3-none-any.whl
  • Upload date:
  • Size: 79.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mhc_mlx-0.6.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0a520354f926d5a86a6a12a608cf6d3a3c7953eca0af8d242b77ab5413bf156e
MD5 26c6a38e49b97ddedaaccf546a37e564
BLAKE2b-256 321ad6a9973a50812cbf873ae33032e7eca963becdf443b54bfd5e71bdfeecc4

See more details on using hashes here.

Provenance

The following attestation bundles were made for mhc_mlx-0.6.2-py3-none-any.whl:

Publisher: publish.yml on svdrecbd/mhc-mlx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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