Research implementation of mHC (DeepSeek; https://arxiv.org/abs/2512.24880) as a drop-in variant of Hyper-Connections (https://arxiv.org/abs/2409.19606).
A runnable PyTorch implementation of the mHC layer update
x_{l+1} = H_l^{res} x_l + H_l^{post,T} F(H_l^{pre} x_l, W_l)
with the key constraints:
H_res: doubly stochastic (Birkhoff polytope; entries ≥ 0, rows sum to 1, cols sum to 1), via Sinkhorn-Knopp.H_pre,H_post: non-negative mixing maps.
Static per-layer matrices:
- learn
H_res_logits ∈ R^{s×s}and project toH_reswith Sinkhorn - learn
H_pre_logits,H_post_logitsand map to non-negative weights (e.g. softmax)
This is a research prototype aimed at correctness + clarity, not the paper's systems optimizations.
Run from examples/nanogpt/. Adjust --nproc_per_node to match your GPU count.
6-layer configs (~20M params):
python train.py config/train_fineweb10B.py
python train.py config/train_fineweb10B_hc.py
python train.py config/train_fineweb10B_mhc.py
python train.py config/train_fineweb10B_vres.py
python train.py config/train_fineweb10B_vres_mhc.py
python train.py config/train_fineweb10B_cvres_mhc.py48-layer configs (~20M params):
python train.py config/train_fineweb10B_48l.py
python train.py config/train_fineweb10B_hc_48l.py
python train.py config/train_fineweb10B_mhc_48l.py
python train.py config/train_fineweb10B_vres_48l.py
python train.py config/train_fineweb10B_vres_mhc_48l.py
python train.py config/train_fineweb10B_cvres_mhc_48l.pyMulti-GPU example:
torchrun --standalone --nproc_per_node=4 train.py config/train_fineweb10B_mhc_48l.pymHC supports an orthostochastic H_res projection via Newton-Schulz. Set mhc_h_res_proj = "orthostochastic" in your config.
By default, configs use fixed Newton-Schulz coefficients (ns_steps=5, ns_coeffs=(3.0, -3.2, 1.2)). For research, ns_coeffs can also be a per-step schedule (tuple of (a, b, c) triplets); set ns_steps = len(ns_coeffs).
For an ablation that keeps residual routing close to identity, enable:
mhc_residual_identity_mix = Truemhc_residual_alpha = 0.01
This applies H_res = (1-α) * I + α * S where S is the projected matrix (Sinkhorn or orthostochastic) and α is learned.
train_fineweb10B_vres*.pyenables value residual only.train_fineweb10B_vres_mhc*.pycombines vRes + mHC.train_fineweb10B_cvres_mhc*.pycombines vRes + mHC withv_residual_constrained=True(convex mixing via softmax).
- Value residual ablations with baseline/HC/mHC
- Combined vRes + mHC configs (unconstrained + constrained)
- H^res =
(1−α)*I + α*Sinstead of full doubly stochastic - Orthostochastic H_res projection (Newton-Schulz) as alternative to Sinkhorn-Knopp
- Opt-in Newton-Schulz coefficient schedule for orthostochastic projection
Built using code snippets from nanogpt, lucidrains/hyper-connections and my own mHC implementation.
Apache 2.0