mHC: Manifold-Constrained Hyper-Connections
mHC: Manifold-Constrained Hyper-Connections
This spec documents the Frankenstein integration of mHC (Manifold-Constrained Hyper-Connections), arXiv:2512.24880 (DeepSeek-AI).
Overview
mHC replaces the standard residual connection with an n-stream residual.
The residual stream width is expanded by a factor n, so the stream carried
across layers is x ∈ R^{n×C} while each layer’s internal function F
(attention and FFN) still operates at dimension C. Three learnable mappings
read, write and mix the stream each layer:
H[pre] ∈ R^{1×n}— aggregates then·C-dim stream into theC-dim layer input.H[post] ∈ R^{1×n}— maps the layer output back onto the stream.H[res] ∈ R^{n×n}— mixes features within the residual stream.
Unconstrained Hyper-Connections (HC) lose the identity-mapping property of the
residual connection, causing unstable gradients and restricted scalability. mHC
constrains ``H[res]`` to the Birkhoff polytope (the set of doubly stochastic
matrices) via the Sinkhorn-Knopp projection, which restores the identity-mapping
/conservation property: the composite product Π_l H_l[res] stays doubly
stochastic across all depth, spectral norm ‖H[res]‖₂ ≤ 1 (non-expansive), and
H[res] x becomes a convex combination of the stream features.
When n = 1 the doubly-stochastic condition degenerates to scalar 1, exactly
recovering the identity mapping.
Mathematical formulation
Per layer l, with stream x_l ∈ R^{n×C}:
x̃_l = vec(x_l) ∈ R^{1×nC}
H̃ = (1/r)·(α ⊙ (x̃_l φ_l)) + b_l # r = ‖x̃_l‖₂ / √(nC); α gating (init 0.01)
H_l[pre] = σ(H̃_pre) # non-negative
H_l[post] = 2σ(H̃_post) # non-negative, range [0, 2]
H_l[res] = SinkhornKnopp(exp(H̃_res)) # doubly stochastic
Fpre = H_l[pre] @ x_l # [1, C]
x_{l+1} = H_l[res] @ x_l + H_l[post]ᵀ ⊗ F(Fpre, W_l)
φ_l ∈ R^{nC × (n²+2n)}— learned linear projection (full precision).b_l ∈ R^{1 × (n²+2n)}— learned bias.α_pre, α_post, α_res— learnable scalar gates, initialised small (0.01).Sinkhorn-Knopp: starting from
exp(H̃_res), alternate row and column normalisations (defaultt_max = 20rounds) to converge to a doubly stochastic matrix. The backward pass recomputes the iteration on-chip and differentiates through it (exact Jacobian-vector product).
Implementation (Frankenstein)
New module src/model/mhc.py:
SinkhornKnoppFunction(torch.autograd.Function)— differentiable projection.ManifoldHyperConnections(nn.Module)— holdsφ_l(proj),b_l(bias) and the three gating scalars; exposesfpre,recombineandmappings.
Wiring in src/model/frankenstein_model.py:
HybridLayergainsmhc_attnandmhc_ffn(one module per layer function)._forward_dense_mhcruns attention then FFN as layer functions over the shared(B, S, n, C)stream.FrankensteinTransformerexpands theC-dim embedding to(B, S, n, C)viamhc_in_projand collapses back viamhc_out_projbefore the head.mhc_checkpointoptionally applies gradient checkpointing per layer to mitigate the ~``n``× activation-memory increase of the n-stream residual.
Config reference
The model.mhc sub-object (hierarchical schema) or flat keys:
YAML ( |
Flat key |
Type |
Default |
Meaning |
|---|---|---|---|---|
|
|
bool |
|
Enable the mHC n-stream residual. |
|
|
int ≥ 1 |
|
Stream expansion factor |
|
|
int ≥ 1 |
|
Sinkhorn-Knopp normalisation rounds. |
|
|
float > 0 |
|
Initial value of the gating scalars |
|
|
bool |
|
Gradient checkpointing on mHC layers. |
|
|
bool |
|
Keep |
Example config: configs/examples/es_arch_mhc_adamw.yaml.
Constraints
mHC is incompatible with ``use_mixture_of_depths`` (MoD token routing operates on a single
C-dim stream, conflicting with the n-stream residual). AValueErroris raised if both are enabled.The
φ_lprojection stays full-precision under BitNet by default (full_prec_under_bitnet: true) to avoid ternary-quantisation noise on the small mHC coefficients. Set it tofalseto useBitLinear.