Skip to content

GatedDelta: opt-in float16 recurrent state storage (gemma4-qat#1697 spike) - #11

Merged
jeethu merged 2 commits into
gdn-fused-1693from
gdn-state-fp16-1697
Nov 16, 2026
Merged

jeethu merged 2 commits into
gdn-fused-1693from
gdn-state-fp16-1697

Conversation

@jeethu

@jeethu jeethu commented Nov 15, 2026

Copy link
Copy Markdown
Member

Stacked on #10 (base gdn-fused-1693). Retarget to the merged base once #10 lands.

Spike for numen-tech/gemma4-qat#1697: an opt-in float16 storage dtype for the Qwen3.5 Gated DeltaNet recurrent state ([B, Hv, Dv, Dk], float32 today). It is off unless the host asks for it.

What changes:

  • GatedDeltaRecurrentStateStorage, a public protocol (recurrentStateDType), adopted by the LLM Qwen35GatedDeltaNet and the VLM Qwen35.GatedDeltaNet. The default stays .float32. The host sets it per layer before the first forward.
  • With .float16:
    • The layer seeds its first state as explicit fp16 zeros.
    • gatedDeltaUpdate keeps an fp16 state as it is. bf16 is still widened to fp32, as before.
    • The per-step kernel gated_delta_step already reads the state into float, sums it in float with compensation and writes StT, so fp16 is storage only.
    • The fast route (mlx core's fused op, GatedDelta fast route: activation-dtype inputs, 9+ rows only (gemma4-qat#1693) #10) returns a float32 state. It is cast back to the input state's dtype.
    • The ops fallback computes in float32 and casts back.
  • Decode, verify and every recurrence sum stay in float32. Only the stored state between calls is rounded to fp16.

Tests (GatedDeltaTests, macOS M3 Max):

  • testFloat16StateIsStorageOnlyOnEveryPath: the kernel, ops and MLXFast paths fed an fp16 state each return an fp16 state that equals the float32 path's state rounded to fp16, and the same output.
  • testStateDTypeDefaultsStayFloat32.
  • All 11 GatedDeltaTests pass. A full MLXLMTests run has not been done on this branch. GatedDelta fast route: activation-dtype inputs, 9+ rows only (gemma4-qat#1693) #10's full run on its parent passed.

The model-level parity, the memory saved and the greedy quality are measured on the host side (numen-tech/gemma4-qat#1697).

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-16T04:11:19.865766Z 002e1c9 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5c02e0c485

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +813 to +816
let recState =
cache?[1]
?? (recurrentStateDType == .float32
? nil : zeroStates(batch: inputs.dim(0), dtype: inputs.dtype).rec)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve the float16 seed when replaying an initially empty cache

When float16 storage is enabled and rollback recording begins on an empty MambaCache, this seeds the forward pass with float16 zeros without putting that seed in the recorded base. If commitRollback(keepingRows:) subsequently keeps a shorter prefix, the rollback closure passes base[1] (still nil) to gatedDeltaReplayState, which defaults to float32 zeros and returns a float32 state. Subsequent forwards prefer that cached state, silently disabling float16 storage for the rest of the session and producing a different state than a direct float16 prefix pass. Have the rollback rebuild use the effective seeded recurrent state when the recorded base is nil.

Useful? React with 👍 / 👎.

@jeethu

jeethu commented Nov 16, 2026

Copy link
Copy Markdown
Member Author

Codex review (gemma4-qat side) found that a verify pass recorded on a cold MambaCache rebuilt its rollback state from base[1] == nil, so a partial commit returned a float32 state and the float16 arm became float32 for the rest of the session. Fixed in 002e1c9: the replay starts from the pass's own initial state (base[1] ?? recState). New test MambaCacheHoldTests.testColdRollbackKeepsTheStorageDType (float32 and float16). MambaCacheHoldTests, GatedDeltaTests, MambaCacheRestoreDropsRecordingTests: 28/28 pass on the M3 Max.

@jeethu
jeethu added this pull request to stack #12 November 16, 2026 06:46
@jeethu

jeethu commented Nov 16, 2026

Copy link
Copy Markdown
Member Author

The new test does fail without the fix. I reverted the one-line fix in the working tree for one run only, then restored it. MambaCacheHoldTests.testColdRollbackKeepsTheStorageDType then failed in its float16 case: the rebuilt state came back as float32 where float16 was expected (MambaCacheHoldTests.swift:296). With 002e1c9 it passes.

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.

1 participant