Bare-metal C++20/CUDA ingestion of tencent/Hy4-flash (220B) on a single RTX 5090 — bit-exact SelfCheck, iHC sandwich-norm invariant & CODE/DET 3/3

#8
by alexmv2025 - opened

Hy4-flash (tencent/Hy4-flash) is a 220-billion-parameter mixture-of-experts
model — 155.5B text backbone, 63.5B of n-gram memory ("Engram"), a 0.43B vision
encoder and a 0.85B speculative drafter — with 11.3B text parameters active per
token (the card rounds to 12B with the vision tower) and a 1M-token context. Its 42-layer backbone interleaves sparse attention
(12 layers, 32 heads) with sliding-window attention (30 layers, 64 heads, a
512-token window) at a 1:3 ratio, runs routed experts in a 1536-wide latent
space
instead of the model's 3072-wide hidden space, and carries four residual
streams through "identity Hyper-Connections" rather than one.

Hardware: one RTX 5090 (32 GiB, SM 12.0), 768 GB host RAM, 160 hardware threads,
CUDA 13.5 toolkit on a 13.4 driver.

Artifact: 83 GB under <model>/logestix/ — 1232 pinned dense tensors (4.8 GB)
plus 20 992 routed experts (41 MoE layers × 512, 78 GB), 4-bit weights with norms,
biases, sinks, the router and the hyper-connection parameters held at source
precision. Engram, the vision tower and the speculative drafter are cut and
counted (deferred: engram=164 vision=441 dfly=87). Routed experts stay on host as
memory-mapped views behind a bounded LRU with a device slot cache, which is what
lets a 220B model answer inside 32 GiB of VRAM at all.

What the checkpoint forced into the engine

  1. Two attention regimes in one stack, with different head counts and different
    RoPE bases.
    Layers 0–1 and every layer with l % 4 == 1 run sparse attention
    (32 heads, θ = 1e7); every other layer runs sliding-window attention (64 heads,
    window 512, θ = 1e4). A single attention shape across the stack cannot even
    load both weight sets.
  2. Hyper-connections in their "identity" form. Four residual streams, an
    8-row mix matrix producing four pre gains and four post gains, magnitude
    2.0 — and no Sinkhorn iteration and no combine matrix, unlike the
    DeepSeek-V4 lineage this architecture otherwise follows. The residual update is
    streams_j = post_j · y + streams_j for every stream j, i.e. the same
    sublayer output scaled per stream, not a mixture.
  3. Sandwich norms on the sublayer output, two of them per layer
    (post_proj_norm after attention; post_fc2_norm on the single dense layer and
    combine_output_norm on all 41 MoE layers). Normalizing the residual instead is
    the natural misreading and changes every layer — and, as it turned out, so is
    multiplying the pre-norm sum (see the defect below).
  4. LatentMoE. The experts read a 1536-wide projection of the block input and
    write a 1536-wide latent sum, which a second projection lifts back to 3072; the
    shared expert and the router both read the full-width input. The per-expert
    matrices are therefore [2·1536, 1536] and [1536, 1536], not sized by
    hidden_size.
  5. A sink in every softmax. Each head carries one learned logit that joins the
    softmax denominator and is then dropped, so the attention weights sum to less
    than one by design. Treating it as a bias (or ignoring it) is a silent rescale.
  6. Gated MLA. The attention output is multiplied by a sigmoid of a separate
    full-width projection before the output projection.
  7. Interleaved rotary pairs, not the half-split rotation that Llama-style
    implementations use: the reference pairs lanes (2j, 2j+1) and broadcasts one
    frequency onto both. This is the single easiest error to make here, and getting
    it wrong still produces fluent text.
  8. rms_norm_eps = 1e-5, not the DeepSeek lineage's 1e-20 and not the
    sibling hy_v4's 1e-6.
  9. Engram is not applied. 126.7 GB of n-gram tables on layers 1/11/21/31. The
    reference does implement it — the hash multipliers are drawn from a seeded
    RNG rather than shipped as weights — so this is a scope decision, not an
    unknowable, and it is recorded as the highest-value follow-up.

Reference used: the implementation the checkpoint's own README pins,
huggingface/transformers PR #50903 (models/hy_v4_flash). The initial
stage-0 triage concluded that no such implementation existed and that reference
parity was impossible without guessing; that conclusion was wrong
, and the
harness now in tools/hy4_flash_reference.py loads all 1230 text-backbone
parameters and the 41 router biases (1271 tensors, missing: 0) from the
checkpoint and runs it.

The defect that decided the ingestion

The first real-weight decode produced fluent, off-target English ("a country that
has a long history of") instead of " Paris". The cause was not a subtle numeric
difference, and it was not the 4-bit weights:

On 41 of the 42 layers the hyper-connection post gain multiplied the
pre-norm MoE output instead of the normed one, so the MLP contribution to
the four residual streams was effectively dropped.

Each branch writes its sublayer output into a shared buffer that the residual
update reads, and the two FFN paths disagreed about which buffer held the normed
value: the dense layer cast it there, the MoE path left the raw routed+shared sum
there and kept the normed value elsewhere. A if (!is_moe) guard on the cast hid
the mismatch. The pre-norm sum's rms is ~240× below the normed output, so the term
vanished without producing a single illegal value — the model kept producing
well-formed English, which is why the symptom looked like precision.

It was found by a self-consistency check instead of a reference comparison.
Every layer must satisfy

streams_out == streams_in + post_attn ⊗ attn_out + post_ffn ⊗ mlp_out

Reconstructing that from the engine's own per-boundary dumps was off by 27.5% at
layer 1
; the reference satisfied the same identity to 0.21%. That check needs no
second implementation, costs one run, and named the layer immediately. After the
fix the identity holds to 0.000000 on all 41 layers, first token becomes
15641 ( Paris), and SelfCheck answers "The capital of France is Paris."
three times bit-identically.

Two further defects surfaced as the fix unblocked later gates: the tool-call
parser decoded into a fixed n_ids*8+64 buffer and reported "no tool call" when a
well-formed 198-byte call overflowed a 176-byte buffer; and Scratch::Free freed
the arena but only nulled the 25 GB expert slot-cache pointer, leaking the card
on every unload (which is what made the second case in any test binary fail with
scratch allocation failed while 965 MiB remained).

Artifact-level traps found in the checkpoint

  1. engram_layer_ids is 1-based. The config lists [2, 12, 22, 32]; the
    engram tensors and the model card put those four layers at 1, 11, 21, 31. The
    reference compensates with layer_idx + 1 in config.engram_layer_ids. A
    converter that trusts the list verbatim cuts the four wrong layers, succeeds,
    and produces a model with four instead of zero uncut memory layers. Suggestion
    to the authors:
    state the indexing convention in config.json or emit
    0-based ids as every other layer list in the file does.
  2. The checkpoint's tensor names are not the reference's module names. Fourteen
    renames (learnable_sink_param→sinks, linear_gate→gate_proj,
    routed_expert_down_proj→pre_expert_proj, hc_attn_layer.hc_pre.hc_fn→
    attn_hc.fn, …) live only in the transformers conversion_mapping.py. Loading
    with the raw names leaves 422 parameters at their random initialisation — no
    error, no warning, and fluent garbage out. Suggestion: ship a
    conversion_map.json beside the weights.
  3. The router's expert bias is an nn.Buffer, not a parameter. A reference
    loader that iterates named_parameters() silently routes every MoE layer with
    a zero bias (the file ships 512 fp32 values, |max| up to 0.075). Half a day was
    spent chasing the resulting false 98% divergence.
  4. The chat frame is load-bearing. "The capital of France is" fed as raw
    token ids does not continue with a capital city, and even framed as a
    question the reference echoes it back (" The capital of France is …") rather
    than answering. Only the <|hy_start:opensource|>{role}… frame with
    reasoning_effort=no_think asks what the model was trained to answer. Any
    harness comparing this family will "discover" a model that cannot answer.
  5. reasoning_effort defaults to high. The template's default opens a
    thinking block, so a serving path that does not pass no_think gets a reasoning
    monologue before the answer. This engine defaults to no_think and exposes
    LSX_HY4_REASONING_EFFORT=high for the other mode.
  6. Two RoPE bases and two head counts are declared per layer type, not per
    model.
    rope_parameters is keyed by sliding_attention /
    deepseek_sparse_attention; swa_num_attention_heads sits beside
    num_attention_heads. Reading only the top-level fields silently gives the
    sparse layers the sliding layers' rope and head count.

Measured

item result
conversion 1232 pinned tensors, 20 992 experts, 83 GB, ~10 min; every one of the checkpoint's 1964 tensor names classified, zero unknown
tokenizer bridge new pre-tokenizer family (kind 13); 34/34 adversarial cases reproduce the reference ids exactly, including the 1–3 digit isolation, CJK isolation, the apostrophe-as-punctuation alternative and the <|hy_*|> markers
frame parity the engine's framed ids are byte-identical to apply_chat_template(tokenize=True) on a probe
tool bridge native Hunyuan frame (<tool_calls:opensource> + <arg_key>/<arg_value>); on the real weights the model emits get_weather with {"city":"Paris"} and the parser returns it
kernels 7676 assertions across iHC (mix/post/head), the sandwich norm, the router, the sliding-window list, the SwiGLU clamp and the sink softmax, each against a host reference
per-component audit on the artifact's own weights, against numpy: o_proj GEMM 0.036%, the LatentMoE block (router + 16 experts + shared) 0.06%, the final head 0.016% — every stage exact
residual invariant 0.000000 on all 41 MoE layers (was 0.2747)
reference agreement engine vs the reference fed the artifact's weights: cos 0.9999–0.9996 on layers 0–8, 0.9993–0.9990 on 9–20, 0.994–0.996 on 21–29, 0.991–0.969 on 30–41, 0.985 on logits, same argmax (the 42-layer series decays steadily; below
0.9995 from layer 9, a step to ~0.994 at 21, ~0.99 from 30)
SelfCheck (Paris) PASS ×3, 923e8ab7c8027eb2, 'The capital of France is **Paris**.'
FSI 2/3 — FSI-02 numericises where the golden is symbolic
CODE / DET 3/3 each (template goldens)
perf 6.02 t/s on the Release/AOCC tree at the canonical 2048-token protocol (TTFT 5.1 s, 6661 expert slots = 24.7 GiB resident, perf row seeded by running the target)

The throughput is PCIe-bound, and the arithmetic says so: 16 experts × 41 layers
× 3.8 MiB is 2.43 GiB of expert weights read per token, the slot cache covers
31.7% of the 20 992 experts, and 6.02 t/s corresponds to ~15 GiB/s of host→device
traffic. The lever on this card is expert prefetch (the selection is known a
layer ahead, so staging can overlap the current layer's GEMVs); a larger card
would also help, and only through the slot cache — 6661 of 20 992 experts fit in
24.7 GiB, and every extra resident expert is PCIe traffic removed. This is the
number to quote for "what a 220B model costs on one consumer card", and it is the
baseline any Engram or prefetch work has to beat.

What was not measured, and why

  • FSI-02 under reasoning_effort=high. That switch is the natural experiment
    for "does this model follow the symbolic-output instruction when it thinks
    first", and it is unaffordable on the reference harness (~6 min/step at full
    prefix). It is the only open quality item.
  • Long-context behaviour. The sparse indexer's group selection is implemented
    (pooled gated keys, top-512 groups, own-group always recalled) and is exercised
    at short context only; the first context beyond 2048 tokens has not been run.
  • Engram. Cut, not measured.

BMH SelfCheck Model Standard

Full ingestion report, pass-sweep breakdown, and open-source C++20/CUDA runtime:
https://github.com/Alex7MV/logestix/blob/main/docs/model-reports/2026-11-24-Hy4-flash.md

Sign up or log in to comment