Skip to content

feat(gemma4): full 12B forward from one token through window-edge pfill - #879

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

feat(gemma4): full 12B forward from one token through window-edge pfill#879
FeathBow merged 1 commit into
pegainfer-project:mainfrom
FeathBow:feat/gemma4-full-forward

Conversation

@FeathBow

Copy link
Copy Markdown
Collaborator

Description

Closes #878.

The full text-tower forward, prefill form: scaled embedding, all 48 layers dispatched by the parsed layer map, the final norm, the tied LM head, and the final-logit softcap — every prompt at or below the 1024-token window, where causal attention IS sliding attention. Geometry and the end constants come from the weights' own config inside the forward — one source of truth for every buffer size handed to CUDA — and token ids are bounds-checked against the embedding before upload, since the embedding kernel validates neither its output width nor the row it reads.

The end constants are the ones a from-the-paper implementation gets wrong: the embedding multiplier is the bf16 rounding of sqrt(hidden_size) (exactly 62.0 at hidden 3840 — the reference casts the scale to the weight dtype before multiplying), derived in one place and asserted against the fixture's recorded value before use; the LM head is the embedding matrix itself; the softcap rides the typed config, validated positive at parse.

The oracle compares log-probabilities at the reference's top-64 ids per position (host log-softmax over the f32 upcast, f64 accumulation for the 262k-way sum), with per-case gates declared from measurement and the window-edge gate distributional — the reasoning and the HF-self-comparison baseline are in the issue.

Test Env

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

Verification

  • single (1 token): max |dlogprob| 0.162 over the top-64, top-1 exact — under the 0.3 gate with top-1 required exact.
  • short (9 tokens): max |dlogprob| 0.638, top-1 exact at all nine positions — under the 0.9 gate with top-1 required exact. Both short cases sit at the bf16 rounding floor.
  • edge (1024 tokens, exactly the window): top-1 agreement 821/1024 with max |dlogprob| 16.19, misses spread across all four quarters (229/190/204/198, first miss at position 27) — against the distributional gate: global top-1 >= 0.75, chaos ceiling 20, and per-quarter top-1 >= 0.65 bounding localized damage — a whole-quarter failure cannot pass on the strength of the others. The baseline making those numbers meaningful: two HF attention backends — sdpa vs eager, same weights, same sequence — agree at 802/1024 with max 16.4, per-quarter 224/186/199/193 (worst quarter 72.7%); our forward sits at that noise floor.

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

…fill

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. Swish!

Reviewed commit: b2fff6f08b

ℹ️ 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".

@FeathBow
FeathBow merged commit 935d990 into pegainfer-project:main Aug 14, 2026
13 checks passed
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: the full 12B text-tower forward, gated against the HF top-64 logprobs

1 participant