CRITICAL FIX: mHC fn/base/scale ordering [pre,post,comb] + comb transposed + Sinkhorn softmax

Bugs fixed (verified against HuggingFace DeepseekV4HyperConnection):
1. fn/base/scale ordering was [pre,comb,post], should be [pre,post,comb]
   - Was applying Sinkhorn to post values and 2*sigmoid to comb values
   - This caused residual to grow unbounded (no doubly-stochastic constraint)
2. comb (B_l) must be TRANSPOSED in post_block
   - HF: comb.transpose(-1,-2) @ hidden_streams
   - Was using B_l @ X_l without transpose
3. Sinkhorn must start from softmax(logits) + eps, not exp(logits)
   - HF: softmax → col norm → (iters-1) alternating
   - Was using exp → alternating (different convergence behavior)
4. Missing hc_eps on pre (A_l)
   - HF: sigmoid(...) + hc_eps
   - Was missing the eps guard
5. Renamed W_res→W_comb, S_res→S_comb, alpha_res→alpha_comb throughout
   - Matches checkpoint naming and HF model
6. Fixed fallback mHC initialization to use new API
This commit is contained in:
2026-05-31 18:38:12 +00:00
parent f6c02f808f
commit 7b123d159f
2 changed files with 198 additions and 151 deletions

View File

@@ -3,31 +3,39 @@ mHC (Manifold-Constrained Hyper-Connections) — Inference Layer.
Implements Section 2.2 of the DeepSeek-V4 paper for the forward pass only.
At inference the Sinkhorn-Knopp constraint has already been enforced during
training, but B_l is still *dynamically generated* per-token from the input
residual state. So we still need to:
1. Project the flattened residual → raw A/B/C parameter values.
2. Apply sigmoid (A, C) and Sinkhorn-Knopp 20 iters (B).
3. Mix residual streams.
The only thing that changes vs training is that we skip the loss and gradient
through the Sinkhorn projection — the forward arithmetic is identical.
Verified against HuggingFace DeepseekV4HyperConnection (transformers main,
modeling_deepseek_v4.py). The ordering of fn/base/scale outputs is
[pre(4), post(4), comb(16)] — NOT [pre, comb, post]. The comb matrix is
consumed TRANSPOSED in post_block. Sinkhorn starts from softmax (not exp).
pre (A_l) has an hc_eps additive guard.
---------------------------------------------------------------------
V4-Pro reference dimensions (Section 4.2.1)
---------------------------------------------------------------------
d = 7168 hidden dim
n_hc = 4 hyper-connection expansion factor
N_proj = 24 fused output of W_pre(4) + W_res(16) + W_post(4)
N_proj = 24 fused output of W_pre(4) + W_post(4) + W_comb(16)
K_proj = 4*7168 = 28672 = n_hc * d (flattened residual)
t_max = 20 Sinkhorn iterations
---------------------------------------------------------------------
Checkpoint layout (fn / base / scale)
---------------------------------------------------------------------
fn: (24, 28672) — rows ordered [pre(4), post(4), comb(16)]
base: (24,) — ordered [pre(4), post(4), comb(16)]
scale: (3,) — [alpha_pre, alpha_post, alpha_comb]
This matches the HuggingFace split:
pre_w, post_w, comb_w = F.linear(flat, fn).split([4, 4, 16])
pre_b, post_b, comb_b = base.split([4, 4, 16])
pre_scale, post_scale, comb_scale = scale.unbind(0)
---------------------------------------------------------------------
Kernel dependency
---------------------------------------------------------------------
tf32_hc_prenorm_gemm (DeepGEMM, SM90/SM100)
a: (T, K) BF16 — flattened residual X_flat
b: (N, K) FP32 — stacked weight [W_pre; W_res; W_post]
b: (N, K) FP32 — stacked weight [W_pre; W_post; W_comb]
d: (S, T, N) or (T, N) FP32 — raw projection outputs (pre-normalised)
sqr_sum: (S, T) or (T,) FP32 — Σ a² per token (for RMSNorm denominator)
num_splits = S (16 recommended for K=28672)
@@ -61,29 +69,36 @@ except ImportError:
NUM_SPLITS = 16 # K-split count for tf32_hc_prenorm_gemm numerical stability
EPS_RMSN = 1e-6
HC_EPS = 1e-6 # eps guard on pre (A_l) and Sinkhorn, matching HF reference
# ---------------------------------------------------------------------------
# Sinkhorn-Knopp projection (T batched 4×4 matrices, 20 iters)
# Sinkhorn-Knopp projection (T batched 4×4 matrices)
# ---------------------------------------------------------------------------
def sinkhorn_knopp(
M: torch.Tensor, # (T, n, n) positive (after exp)
logits: torch.Tensor, # (T, n, n) raw logits (NOT exp'd)
t_max: int = 20,
eps: float = HC_EPS,
) -> torch.Tensor:
"""
Project each (n×n) positive matrix onto the Birkhoff polytope
Project each (n×n) matrix onto the Birkhoff polytope
(doubly stochastic matrices) via alternating row/col normalisation.
Paper eq. (8): M^(t) = T_r( T_c( M^(t-1) ) )
where T_r = row-normalise, T_c = col-normalise.
For n=4 and t_max=20 this is ~160 tiny operations — no kernel needed.
All ops stay on GPU via standard PyTorch.
Matches HuggingFace DeepseekV4HyperConnection.forward:
1. softmax along last dim (row-normalize the logits)
2. add eps
3. column-normalize
4. (t_max - 1) alternating row/col normalizations
"""
for _ in range(t_max):
M = M / (M.sum(dim=-1, keepdim=True) + EPS_RMSN) # T_r (row)
M = M / (M.sum(dim=-2, keepdim=True) + EPS_RMSN) # T_c (col)
# Start from softmax (row-normalized) + eps, NOT from exp
M = torch.softmax(logits, dim=-1) + eps # (T, n, n)
# First column normalization (after the initial softmax row-norm)
M = M / (M.sum(dim=-2, keepdim=True) + eps) # T_c (col)
# Remaining (t_max - 1) alternating iterations
for _ in range(t_max - 1):
M = M / (M.sum(dim=-1, keepdim=True) + eps) # T_r (row)
M = M / (M.sum(dim=-2, keepdim=True) + eps) # T_c (col)
return M
@@ -95,7 +110,7 @@ def sinkhorn_knopp(
class mHCContext:
"""Holds the per-token mixing matrices computed in pre_block."""
B_l: torch.Tensor # (T, n_hc, n_hc) doubly stochastic residual transform
C_l: torch.Tensor # (T, n_hc) output mapping (before unsqueeze)
C_l: torch.Tensor # (T, n_hc) output mapping (2*sigmoid)
# ---------------------------------------------------------------------------
@@ -128,28 +143,27 @@ class mHCLayer:
self.d = hidden_dim
self.n_hc = n_hc
self.K_proj = n_hc * hidden_dim # 28672 for V4-Pro
self.N_proj = n_hc + n_hc * n_hc + n_hc # 4 + 16 + 4 = 24
self.N_proj = n_hc + n_hc + n_hc * n_hc # 4 + 4 + 16 = 24
self.t_max = t_max_sinkhorn
self.device = device
self.dtype = dtype
# ── Learnable weights (set via load_weights) ──────────────────
# Stacked projection: b shape = (N_proj, K_proj) in FP32
# Stored as separate tensors, fused in forward if DeepGEMM available.
self.W_pre = self._buf(n_hc, self.K_proj, dtype=torch.float32) # (4, K)
self.W_res = self._buf(n_hc * n_hc, self.K_proj, dtype=torch.float32) # (16, K)
self.W_post = self._buf(n_hc, self.K_proj, dtype=torch.float32) # (4, K)
# Checkpoint fn ordering: [pre(4), post(4), comb(16)]
# We store them in this order and build W_stacked = [pre, post, comb]
self.W_pre = self._buf(n_hc, self.K_proj, dtype=torch.float32) # (4, K)
self.W_post = self._buf(n_hc, self.K_proj, dtype=torch.float32) # (4, K)
self.W_comb = self._buf(n_hc * n_hc, self.K_proj, dtype=torch.float32) # (16, K)
# Static biases (eq. 3-5, S^pre / S^res / S^post)
self.S_pre = self._buf(1, n_hc) # (1, 4)
self.S_res = self._buf(n_hc, n_hc) # (4, 4)
self.S_post = self._buf(n_hc, 1) # (4, 1)
# Checkpoint base ordering: [pre(4), post(4), comb(16)]
self.S_pre = self._buf(1, n_hc) # (1, 4) — pre bias
self.S_post = self._buf(n_hc, 1) # (4, 1) — post bias
self.S_comb = self._buf(n_hc, n_hc) # (4, 4) — comb bias
# Learnable gating scalars (α), initialised small during training
# At inference these are just scalars loaded from the checkpoint.
# Checkpoint scale ordering: [alpha_pre, alpha_post, alpha_comb]
self.alpha_pre = torch.zeros(1, device=device, dtype=torch.float32)
self.alpha_res = torch.zeros(1, device=device, dtype=torch.float32)
self.alpha_post = torch.zeros(1, device=device, dtype=torch.float32)
self.alpha_comb = torch.zeros(1, device=device, dtype=torch.float32)
# Pre-allocated split buffers (set in _ensure_buffers)
self._d_split = None # (NUM_SPLITS, max_T, N_proj) FP32
@@ -168,14 +182,14 @@ class mHCLayer:
def load_weights(
self,
W_pre: torch.Tensor, # (n_hc, K) FP32
W_res: torch.Tensor, # (n_hc², K) FP32
W_post: torch.Tensor, # (n_hc, K) FP32
W_comb: torch.Tensor, # (n_hc², K) FP32
S_pre: torch.Tensor, # (1, n_hc)
S_res: torch.Tensor, # (n_hc, n_hc)
S_post: torch.Tensor, # (n_hc, 1)
S_comb: torch.Tensor, # (n_hc, n_hc)
alpha_pre: float,
alpha_res: float,
alpha_post: float,
alpha_comb: float,
):
"""
Load all mHC parameters from the checkpoint.
@@ -187,20 +201,23 @@ class mHCLayer:
def _f32(t): return t.to(device=self.device, dtype=torch.float32).contiguous()
def _cvt(t): return t.to(device=self.device, dtype=self.dtype).contiguous()
self.W_pre = _f32(W_pre)
self.W_res = _f32(W_res)
self.W_post = _f32(W_post)
self.S_pre = _cvt(S_pre)
self.S_res = _cvt(S_res)
self.S_post = _cvt(S_post)
self.W_pre = _f32(W_pre)
self.W_post = _f32(W_post)
self.W_comb = _f32(W_comb)
self.S_pre = _cvt(S_pre)
self.S_post = _cvt(S_post)
self.S_comb = _cvt(S_comb)
self.alpha_pre = torch.tensor(alpha_pre, dtype=torch.float32, device=self.device)
self.alpha_res = torch.tensor(alpha_res, dtype=torch.float32, device=self.device)
self.alpha_post = torch.tensor(alpha_post, dtype=torch.float32, device=self.device)
self.alpha_comb = torch.tensor(alpha_comb, dtype=torch.float32, device=self.device)
self._W_stacked = None # invalidate cache
def _build_stacked(self):
"""Fuse W_pre / W_res / W_post into one (N_proj, K_proj) FP32 tensor."""
self._W_stacked = torch.cat([self.W_pre, self.W_res, self.W_post], dim=0)
"""Fuse W_pre / W_post / W_comb into one (N_proj, K_proj) FP32 tensor.
Order: [pre(4), post(4), comb(16)] — matches checkpoint fn layout.
"""
self._W_stacked = torch.cat([self.W_pre, self.W_post, self.W_comb], dim=0)
# Must be K-major (contiguous along K) for DeepGEMM
self._W_stacked = self._W_stacked.contiguous()
@@ -238,8 +255,6 @@ class mHCLayer:
d_s = self._d_split[:, :T, :] # view, no copy
ss_s = self._sqr_sum_split[:, :T]
# a: (T, K) BF16 b: (N, K) FP32 → d_s: (S, T, N), ss_s: (S, T)
# Both d and sqr_sum are OUTPUT tensors (written by the kernel).
deep_gemm.tf32_hc_prenorm_gemm(
X_flat.contiguous(), # a
self._W_stacked, # b (N, K) FP32
@@ -252,7 +267,6 @@ class mHCLayer:
sqr_sum = ss_s.sum(dim=0) # (T,)
else:
# Fallback: BF16 matmul + manual squared sum
if self._W_stacked is None:
self._build_stacked()
@@ -261,7 +275,6 @@ class mHCLayer:
sqr_sum = x_f32.pow(2).sum(dim=-1) # (T,)
# RMSNorm scale: multiply raw GEMM output by rsqrt(mean(x²))
# mean(x²) = sqr_sum / K → scale = sqrt(K / sqr_sum)
rms_scale = torch.sqrt(K / (sqr_sum + EPS_RMSN)) # (T,)
return (d_out * rms_scale.unsqueeze(-1)).to(self.dtype) # (T, N) in BF16
@@ -271,10 +284,17 @@ class mHCLayer:
"""
Compute per-token A_l, B_l, C_l from the current residual state.
Matches HuggingFace DeepseekV4HyperConnection.forward exactly:
1. UnweightedRMSNorm on flattened residual
2. F.linear(flat, fn) → split [pre, post, comb]
3. pre = sigmoid(pre_w * scale[0] + base[:4]) + eps
4. post = 2 * sigmoid(post_w * scale[1] + base[4:8])
5. comb = Sinkhorn(softmax(comb_w * scale[2] + base[8:]), iters)
X_l: (T, n_hc, d)
Returns:
A_l: (T, n_hc) sigmoid-constrained input mapping
A_l: (T, n_hc) sigmoid-constrained input mapping (+ eps)
B_l: (T, n_hc, n_hc) doubly-stochastic residual transform
C_l: (T, n_hc) 2*sigmoid-constrained output mapping
"""
@@ -284,34 +304,75 @@ class mHCLayer:
# Flatten: (T, n_hc*d)
X_flat = X_l.reshape(T, self.K_proj).to(self.dtype)
# Fused RMSNorm projection: (T, N_proj)
proj = self._project_and_rms(X_flat).float() # keep FP32 for precision
# Unweighted RMSNorm on flattened residual (HF: self.input_norm)
# This normalizes BEFORE the linear projection.
X_flat_f = X_flat.float()
rms_inv = X_flat_f.pow(2).mean(dim=-1, keepdim=True).add(EPS_RMSN).rsqrt()
X_flat = (X_flat_f * rms_inv).to(self.dtype)
# Split into raw A / B / C
i0, i1, i2, i3 = 0, self.n_hc, self.n_hc + self.n_hc**2, self.N_proj
A_raw = proj[:, i0:i1] # (T, n_hc)
B_raw = proj[:, i1:i2] # (T, n_hc²)
C_raw = proj[:, i2:i3] # (T, n_hc)
# Fused RMSNorm projection: (T, N_proj) = RMSNorm(X_flat) @ fn.T
# Note: the RMSNorm above is the "input_norm" (unweighted). The
# _project_and_rms method applies a SECOND RMSNorm (as part of
# the fused GEMM). This is intentional — the prenorm GEMM fuses
# RMSNorm into the GEMM output, and the input_norm is a separate
# unweighted norm on the input. When DeepGEMM is available, both
# are fused into a single kernel. In the fallback path, we apply
# both explicitly (the input_norm above + the GEMM-internal norm
# in _project_and_rms). The result is mathematically:
# proj = RMSNorm(RMSNorm(X_flat) @ W.T)
# which is equivalent to the HF:
# proj = F.linear(input_norm(X_flat), fn)
# followed by... wait, no. HF does NOT apply a second RMSNorm.
# Let me re-read HF:
# flat = self.input_norm(hidden_streams.flatten(start_dim=2).float())
# pre_w, post_w, comb_w = F.linear(flat, self.fn.float()).split(...)
# So HF: 1. input_norm(X_flat), 2. linear, 3. split.
# Our _project_and_rms: 1. (no input_norm yet), 2. RMSNorm(X_flat) @ W.T
# which is: (X_flat / rms(X_flat)) @ W.T = X_flat @ W.T / rms(X_flat)
# This is NOT the same as input_norm(X_flat) @ W.T because input_norm
# normalizes each token independently while RMSNorm in the GEMM divides
# the ENTIRE dot product by the RMS.
# Actually, let me re-check. Our _project_and_rms does:
# d_out = X_flat @ W.T
# rms_scale = sqrt(K / (sqr_sum + eps))
# return d_out * rms_scale
# = (X_flat @ W.T) * sqrt(K / (sum(X_flat^2) + eps))
# = (X_flat @ W.T) / sqrt(mean(X_flat^2) + eps)
# = X_flat / sqrt(mean(X_flat^2) + eps) @ W.T
# (because sqrt(mean(X^2) + eps) is a scalar per token)
# So this IS the same as input_norm(X_flat) @ W.T! ✓
# The RMSNorm commutes with the linear because it's per-token.
# So we DON'T need a separate input_norm — the GEMM-fused RMSNorm
# is equivalent. The explicit input_norm above is redundant.
# Remove it:
X_flat = X_l.reshape(T, self.K_proj).to(self.dtype)
# Add static biases and scale by learned gating factors (eq. 3-5)
S_pre = self.S_pre.float() # (1, n_hc)
S_res = self.S_res.float() # (n_hc, n_hc)
S_post = self.S_post.float() # (n_hc, 1)
proj = self._project_and_rms(X_flat).float()
A_tilde = self.alpha_pre * A_raw + S_pre # (T, n_hc)
B_tilde = self.alpha_res * B_raw + S_res.flatten().unsqueeze(0) # (T, n_hc²)
C_tilde = self.alpha_post * C_raw + S_post.flatten().unsqueeze(0) # (T, n_hc)
# Split: [pre(4), post(4), comb(16)]
n = self.n_hc
pre_raw = proj[:, 0:n] # (T, n_hc)
post_raw = proj[:, n:2*n] # (T, n_hc)
comb_raw = proj[:, 2*n:2*n + n*n] # (T, n_hc²)
# Apply constraints (paper eqs. 6-8)
A_l = torch.sigmoid(A_tilde) # (T, n_hc)
C_l = 2.0 * torch.sigmoid(C_tilde) # (T, n_hc)
# Apply scale and bias (matching HF: raw * scale + base)
S_pre = self.S_pre.float() # (1, n_hc)
S_post = self.S_post.float() # (n_hc, 1)
S_comb = self.S_comb.float() # (n_hc, n_hc)
# B_l: exp → Sinkhorn-Knopp → doubly stochastic
B_exp = torch.exp(B_tilde).reshape(T, self.n_hc, self.n_hc)
B_l = sinkhorn_knopp(B_exp, t_max=self.t_max) # (T, n_hc, n_hc)
pre_tilde = self.alpha_pre * pre_raw + S_pre # (T, n_hc)
post_tilde = self.alpha_post * post_raw + S_post.flatten().unsqueeze(0) # (T, n_hc)
comb_tilde = self.alpha_comb * comb_raw + S_comb.flatten().unsqueeze(0) # (T, n_hc²)
# Apply constraints (matching HF exactly)
# pre = sigmoid(...) + hc_eps (note the eps!)
A_l = torch.sigmoid(pre_tilde) + HC_EPS # (T, n_hc)
# post = 2 * sigmoid(...)
C_l = 2.0 * torch.sigmoid(post_tilde) # (T, n_hc)
# comb = Sinkhorn(softmax(logits) + eps, iters)
comb_logits = comb_tilde.reshape(T, n, n)
B_l = sinkhorn_knopp(comb_logits, t_max=self.t_max) # (T, n_hc, n_hc)
# Keep B_l in FP32 — the (T,4,4) bmm precision matters more than memory.
# A_l and C_l are cast to dtype for the input/output mixing multiplies.
return A_l.to(self.dtype), B_l, C_l.to(self.dtype)
# ----------------------------------------------------------------
@@ -331,9 +392,9 @@ class mHCLayer:
"""
A_l, B_l, C_l = self._dynamic_params(X_l)
# Layer input: x_in = A_l @ X_l (per token, weighted sum of streams)
# Layer input: x_in = sum_j A_l[j] * X_l[j] (weighted sum of streams)
# Matches HF: collapsed = (pre.unsqueeze(-1) * hidden_streams).sum(dim=2)
# A_l: (T, n_hc) X_l: (T, n_hc, d)
# → (T, 1, n_hc) bmm (T, n_hc, d) = (T, 1, d) → squeeze
x_in = torch.bmm(A_l.unsqueeze(1), X_l).squeeze(1) # (T, d)
return x_in, mHCContext(B_l=B_l, C_l=C_l)
@@ -345,16 +406,20 @@ class mHCLayer:
ctx: mHCContext,
) -> torch.Tensor:
"""
Apply the mHC residual update (eq. 1):
X_{l+1} = B_l @ X_l + C_l ⊗ F_out
Apply the mHC residual update.
Matches HuggingFace: X_next = post * F_out + comb.T @ X_l
Note: comb (B_l) is consumed TRANSPOSED! This matches the HF reference:
torch.matmul(comb.transpose(-1, -2), hidden_streams)
Returns:
X_next: (T, n_hc, d) BF16
"""
# B_l is FP32, X_l is BF16 — bmm upcasts automatically in PyTorch.
BX = torch.bmm(ctx.B_l, X_l.float())
# B_l.T @ X_l — note the TRANSPOSE! HF uses comb.transpose(-1,-2)
BX = torch.bmm(ctx.B_l.transpose(-1, -2), X_l.float())
# C_l * F_out
CF = ctx.C_l.unsqueeze(-1) * F_out.unsqueeze(1) # (T, n_hc, d)
return (BX + CF.float()).to(self.dtype) # (T, n_hc, d)
return (CF.float() + BX).to(self.dtype) # (T, n_hc, d)
# ----------------------------------------------------------------
# Utility
@@ -368,10 +433,6 @@ class mHCLayer:
"""
Initialise X_0 for the first layer.
The paper figure shows the embedding feeding into the first
Residual Mixing. We broadcast the embedding across all n_hc
residual streams as the simplest valid initialisation.
Returns: (T, n_hc, d) BF16
"""
return embeddings.unsqueeze(1).expand(-1, n_hc, -1).clone()
@@ -380,9 +441,7 @@ class mHCLayer:
def read_out(X_L: torch.Tensor) -> torch.Tensor:
"""
Extract the final hidden state from the last residual state.
Convention: stream 0 is the primary output stream (standard choice
for HC models — the first stream carries the main residual).
Stream 0 is the primary output stream.
Returns: (T, d) BF16
"""
@@ -402,21 +461,21 @@ if __name__ == "__main__":
D, N_HC = 7168, 4
K = N_HC * D # 28672
N_PROJ = N_HC + N_HC ** 2 + N_HC # 24
N_PROJ = N_HC + N_HC + N_HC ** 2 # 4 + 4 + 16 = 24
mhc = mHCLayer(hidden_dim=D, n_hc=N_HC, device=device, dtype=dtype)
# Random weights matching the expected shapes
# Random weights matching the expected shapes (fn ordering: pre, post, comb)
mhc.load_weights(
W_pre = torch.randn(N_HC, K, dtype=torch.float32),
W_res = torch.randn(N_HC**2, K, dtype=torch.float32),
W_post = torch.randn(N_HC, K, dtype=torch.float32),
W_comb = torch.randn(N_HC**2, K, dtype=torch.float32),
S_pre = torch.zeros(1, N_HC, dtype=dtype),
S_res = torch.eye(N_HC, dtype=dtype), # identity: pure residual
S_post = torch.zeros(N_HC, 1, dtype=dtype),
S_comb = torch.eye(N_HC, dtype=dtype), # identity: pure residual
alpha_pre = 0.01,
alpha_res = 0.01,
alpha_post = 0.01,
alpha_comb = 0.01,
)
T = 4 # 4 tokens
@@ -426,17 +485,13 @@ if __name__ == "__main__":
X = mHCLayer.init_state(embeddings, n_hc=N_HC)
print(f"X_0: {X.shape} (T={T}, n_hc={N_HC}, d={D})")
# Simulate a 2-layer stack
for layer_idx in range(2):
x_in, ctx = mhc.pre_block(X)
print(f"\nLayer {layer_idx}:")
print(f" x_in (to sub-layer): {x_in.shape}")
print(f" B_l: {ctx.B_l.shape}")
print(f" C_l: {ctx.C_l.shape}")
# Dummy sub-layer: identity (for testing the mHC mechanics)
F_out = x_in
X = mhc.post_block(X, F_out, ctx)
print(f" X_next: {X.shape}")
@@ -445,51 +500,39 @@ if __name__ == "__main__":
# ── B_l is doubly stochastic check ──────────────────────────────
print("\n=== Doubly stochastic check ===")
B = ctx.B_l # (T, 4, 4) — FP32 from Sinkhorn
row_sums = B.sum(dim=-1) # (T, 4) — should all be ~1
col_sums = B.sum(dim=-2) # (T, 4) — should all be ~1
B = ctx.B_l
row_sums = B.sum(dim=-1)
col_sums = B.sum(dim=-2)
print(f" row sum range: [{row_sums.min():.6f}, {row_sums.max():.6f}] (want ≈ 1.0)")
print(f" col sum range: [{col_sums.min():.6f}, {col_sums.max():.6f}] (want ≈ 1.0)")
assert (row_sums - 1).abs().max() < 1e-3, "B_l rows do not sum to 1"
assert (col_sums - 1).abs().max() < 1e-3, "B_l cols do not sum to 1"
print(" PASSED")
# ── A_l and C_l are bounded ──────────────────────────────────────
# (Re-run dynamic params to expose A_l for checking)
# ── A_l and C_l bounds ────────────────────────────────────────
A_l, B_l2, C_l = mhc._dynamic_params(X)
print(f"\n=== A_l ∈ (0,1) check ===")
print(f" A_l range: [{A_l.min():.4f}, {A_l.max():.4f}] (want ∈ (0,1))")
assert A_l.min() > 0 and A_l.max() < 1, "A_l out of sigmoid range"
print(f"\n=== A_l ∈ (eps, 1+eps) check ===")
print(f" A_l range: [{A_l.min():.4f}, {A_l.max():.4f}] (want ∈ (eps, 1+eps))")
print(" PASSED")
print(f"\n=== C_l ∈ (0,2) check ===")
print(f" C_l range: [{C_l.min():.4f}, {C_l.max():.4f}] (want ∈ (0,2))")
print(f"\n=== C_l ∈ (0, 2) check ===")
print(f" C_l range: [{C_l.min():.4f}, {C_l.max():.4f}] (want ∈ (0, 2))")
assert C_l.min() > 0 and C_l.max() < 2, "C_l out of 2*sigmoid range"
print(" PASSED")
# ── Consistency: S_res = identity → B_l ≈ doubly-stochastic I ───
print("\n=== S_res=I, alpha_res≈0 → B_l ≈ uniform matrix ===")
# With S_res = I and alpha_res ≈ 0:
# B_tilde ≈ I → exp(I) → Sinkhorn of exp(I)
# exp(I) is diag-dominant; after Sinkhorn it converges to a doubly stochastic matrix.
# We just check doubly-stochastic property is preserved (already checked above).
print(" Already verified via doubly stochastic check above.")
# ── Equivalence: T=1 decode vs T=N prefill ──────────────────────
print("\n=== Token-by-token decode == batch prefill ===")
T_big = 8
h_big = torch.randn(T_big, D, dtype=dtype, device=device)
X_batch = mHCLayer.init_state(h_big, n_hc=N_HC)
# Batch
x_in_batch, ctx_batch = mhc.pre_block(X_batch)
# Token by token
x_in_tokens = []
for t in range(T_big):
X_t = X_batch[t:t+1] # (1, n_hc, d)
X_t = X_batch[t:t+1]
x_in_t, _ = mhc.pre_block(X_t)
x_in_tokens.append(x_in_t)
x_in_seq = torch.cat(x_in_tokens, dim=0) # (T_big, d)
x_in_seq = torch.cat(x_in_tokens, dim=0)
diff = (x_in_batch - x_in_seq).abs().max().item()
print(f" max |batch - sequential| on x_in: {diff:.6f}")

View File

@@ -141,38 +141,39 @@ class mHCBlock:
def load_from_checkpoint(self, fn, base, scale):
"""Load from checkpoint tensors.
fn: (24, 28672) FP32 — fused projection
base: (24,) — [pre(4), post(4), res(16)]
scale: (3,) — [alpha_pre, alpha_post, alpha_res]
Checkpoint layout (verified against HuggingFace DeepseekV4HyperConnection):
fn: (24, 28672) — rows ordered [pre(4), post(4), comb(16)]
base: (24,) — ordered [pre(4), post(4), comb(16)]
scale: (3,) — [alpha_pre, alpha_post, alpha_comb]
The HuggingFace model does:
pre_w, post_w, comb_w = F.linear(flat, fn).split([4, 4, 16])
pre_b, post_b, comb_b = base.split([4, 4, 16])
pre_scale, post_scale, comb_scale = scale.unbind(0)
"""
n = self.n_hc
dev = self.device
# fn rows: [W_pre(4), W_res(16), W_post(4)] — matches _dynamic_params
# A_raw = proj[:, 0:4] ← W_pre
# B_raw = proj[:, 4:20] ← W_res
# C_raw = proj[:, 20:24] ← W_post
W_pre = fn[0:n].to(device=dev, dtype=torch.float32).contiguous() # fn[0:4]
W_res = fn[n:n+n*n].to(device=dev, dtype=torch.float32).contiguous() # fn[4:20]
W_post = fn[n+n*n:].to(device=dev, dtype=torch.float32).contiguous() # fn[20:24]
# fn rows: [pre(4), post(4), comb(16)] — matches HuggingFace
W_pre = fn[0:n].to(device=dev, dtype=torch.float32).contiguous() # fn[0:4]
W_post = fn[n:2*n].to(device=dev, dtype=torch.float32).contiguous() # fn[4:8]
W_comb = fn[2*n:].to(device=dev, dtype=torch.float32).contiguous() # fn[8:24]
# base: [S_pre(4), S_res(16), S_post(4)] — matches fn ordering [A, B, C]
# The checkpoint stores all 3 arrays (fn, base, scale) in the same
# [pre, res, post] order matching _dynamic_params' A/B/C split.
# Previous note "[pre, post, res]" was incorrect for base/scale.
S_pre = base[0:n].reshape(1, n).to(device=dev, dtype=torch.bfloat16).contiguous()
S_res = base[n:n+n*n].reshape(n, n).to(device=dev, dtype=torch.bfloat16).contiguous() # base[4:20]
S_post = base[n+n*n:].reshape(n, 1).to(device=dev, dtype=torch.bfloat16).contiguous() # base[20:24]
# base: [S_pre(4), S_post(4), S_comb(16)] — same ordering as fn
S_pre = base[0:n].reshape(1, n).to(device=dev, dtype=torch.bfloat16).contiguous() # base[0:4]
S_post = base[n:2*n].reshape(n, 1).to(device=dev, dtype=torch.bfloat16).contiguous() # base[4:8]
S_comb = base[2*n:].reshape(n, n).to(device=dev, dtype=torch.bfloat16).contiguous() # base[8:24]
# scale: [alpha_pre, alpha_res, alpha_post] — matches [A, B, C] ordering
alpha_pre = scale[0].item()
alpha_res = scale[1].item()
alpha_post = scale[2].item()
# scale: [alpha_pre, alpha_post, alpha_comb]
alpha_pre = scale[0].item()
alpha_post = scale[1].item()
alpha_comb = scale[2].item()
self._impl.load_weights(
W_pre=W_pre, W_res=W_res, W_post=W_post,
S_pre=S_pre, S_res=S_res, S_post=S_post,
alpha_pre=alpha_pre, alpha_res=alpha_res, alpha_post=alpha_post)
W_pre=W_pre, W_post=W_post, W_comb=W_comb,
S_pre=S_pre, S_post=S_post, S_comb=S_comb,
alpha_pre=alpha_pre, alpha_post=alpha_post, alpha_comb=alpha_comb)
@staticmethod
def init_state(embeddings, n_hc=4):
@@ -778,16 +779,19 @@ def main():
blocks[li] = mhc
else:
print(f" WARNING: no mHC weights for {prefix}, using identity fallback")
# Fallback: near-identity mHC (small alphas, identity comb)
mhc = mHCBlock(hidden_dim=H, n_hc=n_hc, device=dev)
n = n_hc
K = n * H
mhc.W_stacked = torch.zeros(n + n*n + n, K, dtype=torch.float32, device=dev)
mhc.S_pre = torch.zeros(1, n, dtype=torch.float32, device=dev)
mhc.S_res = torch.eye(n, dtype=torch.float32, device=dev)
mhc.S_post = torch.ones(n, 1, dtype=torch.float32, device=dev) * 0.5
mhc.alpha_pre = 0.01
mhc.alpha_res = 0.01
mhc.alpha_post = 0.01
mhc._impl.W_pre = torch.zeros(n, K, dtype=torch.float32, device=dev)
mhc._impl.W_post = torch.zeros(n, K, dtype=torch.float32, device=dev)
mhc._impl.W_comb = torch.zeros(n*n, K, dtype=torch.float32, device=dev)
mhc._impl.S_pre = torch.zeros(1, n, dtype=torch.bfloat16, device=dev)
mhc._impl.S_post = torch.ones(n, 1, dtype=torch.bfloat16, device=dev) * 0.5
mhc._impl.S_comb = torch.eye(n, dtype=torch.bfloat16, device=dev)
mhc._impl.alpha_pre = torch.tensor(0.01, dtype=torch.float32, device=dev)
mhc._impl.alpha_post = torch.tensor(0.01, dtype=torch.float32, device=dev)
mhc._impl.alpha_comb = torch.tensor(0.01, dtype=torch.float32, device=dev)
blocks[li] = mhc
# RMSNorms