Skip to content

feat(gemma4): global decoder layer and its HF golden oracle probes - #877

Merged
FeathBow merged 1 commit into
pegainfer-project:mainfrom
FeathBow:feat/gemma4-global-layer
Aug 14, 2026
Merged

feat(gemma4): global decoder layer and its HF golden oracle probes#877
FeathBow merged 1 commit into
pegainfer-project:mainfrom
FeathBow:feat/gemma4-global-layer

Conversation

@FeathBow

Copy link
Copy Markdown
Collaborator

Description

Closes #876

The global (full-attention) decoder layer at head_dim 512, forward only — everything downstream of attention was already kind-agnostic and is now one shared epilogue. The differences from the local kind are the ones the checkpoint and the reference pin:

  • K=V is a runtime fork, not shared storage. The checkpoint ships no v_proj on any full_attention layer (the loader enforces it; this layer refuses one that carries it), and the reference forks value off k_proj's raw output BEFORE k_norm and RoPE touch K — after the norms the two tensors differ bitwise.
  • Proportional RoPE is table-borne. The inverse frequencies keep the full head_dim as exponent denominator, the un-rotated band is zero-padded, and rotate_half pairs (d, d + 256) across the whole head — so the prep kernel runs at rotary_dim = head_dim and the partiality lives in the tables. Passing the intuitive 128 rotates the wrong pairs entirely; a control below shows the oracle catches exactly that. The rope contract is parsed and validated once on the typed config — each family's declared rope_type is asserted against the implemented algorithm, theta must be positive, and partial_rotary_factor * global_head_dim must land on a positive even width within the head — and the table builder re-checks its inputs at the seam. The global forward range-checks positions on the host before upload, because its batched prep kernel __trap()s on out-of-range positions instead of returning an error (the hd256 prefill wrapper rejects them itself).
  • Attention stays unscaled (scaling = 1.0) through the hd512 single-prefill's explicit sm_scale, and the layer is prefill-from-zero only (start_pos > 0 rejected), like the local kind.

The oracle extends to both global boundaries — indices from the layer-map parse, cross-checked against the fixture metadata's own parse — eight comparisons in one checkpoint load. The last global layer's input is the sliding side's output cut: the fixture dedups adjacent cuts by depth, and that adjacency is asserted rather than assumed.

Test Env

Single GPU (sm_89, x86_64), CUDA 12.9, against the pinned 12B checkpoint.

Verification

  • Eight comparisons (single/short x sliding/global x first/last boundary) against the declared tolerance of 0.4 absolute + 2% relative:
    • All four one-token comparisons are bitwise exact (max_abs = 0) — including both global layers. With softmax over one key, query, RoPE and scale drop out of the result, so this pins the V/O path — the K=V fork and the weightless V norm included — the norms, the MLP and layer_scalar exactly; the tables and the scale are gated by the nine-token comparisons and the controls below.
    • The four nine-token comparisons show scattered rounding noise only, zero elements over tolerance, token 0 exact throughout; the global layers sit below the local pair (max_abs 0.125 / 0.0625 vs 0.1875 / 0.25).
  • Five negative controls, each against the full checkpoint: swapping the reference cut reddens all eight; sm_scale pinned to rsqrt(512) fails exactly the two multi-token global comparisons; the V fork moved after the norms fails both multi-token global comparisons while one-token stays in tolerance (the weightless norm cancels k_norm's magnitude until multi-key softmax amplifies the residue — why the multi-token probe exists); rotary_dim = 128 — the intuitive reading — fails both multi-token global comparisons; rebuilding the tables with rotary_dim as exponent denominator fails the first global comparison.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update

Signed-off-by: Feathbow <feathbow@gmail.com>
@FeathBow

Copy link
Copy Markdown
Collaborator Author

@codex review please

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: d033bd1cb9

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gemma4: one global decoder layer, forward only, against the HF golden probes

1 participant