Skip to content

spark-model: opt-in GDN deferred commit for the wyN verify (ATLAS_GDN_DEFERRED_COMMIT) - #128

Closed
AzeezIsh wants to merge 3 commits into
mainfrom
perf/gdn-deferred-commit-v2
Closed

AzeezIsh wants to merge 3 commits into
mainfrom
perf/gdn-deferred-commit-v2

Conversation

@AzeezIsh

Copy link
Copy Markdown
Contributor

Summary

Opt-in ATLAS_GDN_DEFERRED_COMMIT=1 for the wyN GDN verify (K=5..16, serial and batched). This targets the top kernel in the DFlash2 C=16 profile (gated_delta_rule_wy8, 21.7 % of GPU time in reiner job 596).

The problem. The storing verify writes K-1 full h_state snapshots plus the final H per (seq, v-head, layer), ~64 KB each on qwen3.8-27b. Every accept path consumes only intermediates[a-1].

What the lever does:

  • gated_delta_rule_wy{K}_defer runs the identical gated_delta_rule_wyn_impl arithmetic with every state store removed, so h_state stays at H0.
  • gated_delta_rule_commit replays the accepted prefix 0..a-1 through the same impl body and stores only H_{a-1}.
  • Conv outputs and gates are staged into per-slot commit buffers, because the shared scratch is reused by the next forward.
  • Accept paths (commit_accepted_prefix, rollback_ssm_states, batched DFlash) run the commit whenever a layer's gdn_commit_pending is set. A full accept is no longer a no-op for deferred layers.
  • With the lever off, the legacy gated_delta_rule_wy{K} entries keep main's extern signature and store contract (impl<K, 0>).

Scope limits:

  • wy2/3/4/wy17 and the f16/resident variants are untouched.
  • The strix-hip wyN port has no table form, so its handles resolve to 0 and the lever is inert there.

Commits:

  • 2f2ad17: the lever.
  • 0f5ec00: makes it build and run.
    • It defines the missing ATLAS_WYN_DEFER_INSTANTIATE and fixes impl<K, 0> arity in ATLAS_WYN_INSTANTIATE.
    • It fixes an eager then_some(arr[k-5]) index that panicked for k<=4 and went out of bounds at k=17.
    • It skips a full-accept conv restore from an unwritten conv_intermediate(k-1) slot.
    • It adds a launcher/kernel arity pin.

Draft: stays opt-in until reiner proves parity and a benefit.

Test plan

  • cargo test -p spark-model --lib: 856 passed. This includes the new gdn_wyn_launcher_arg_count_matches_kernel_param_count (18/18/14; it fails on the lever commit alone) and the r4/r7 dispatch tests that the lever commit alone broke.
  • CI nvcc -> PTX (all gb10 targets): the first compile of the new entries.
  • reiner job 621, which needs all of the following:
    • cargo test -p spark-model --release --features cuda gdn_deferred_commit -- --ignored (K in {5,8,9,12,16}, every a in 1..=K, outputs and committed state vs the storing oracle);
    • a DFlash2 concurrency A/B, lever off vs on, interleaved ×2;
    • greedy probe hashes identical lever off vs on;
    • lever-off greedy identical to main 10d4ec7 (MODE 0 identity);
    • 0 errors or watchdog events.

Generated with Devin

Supersedes #126: same tree, with the lever commit re-authored so the CLA check can pass.

AzeezIsh and others added 2 commits September 27, 2026 18:16
…OMMIT)

wyN verify (K=5..16, serial and batched) writes K-1 full h_state
snapshots + the final H per (seq, v-head, layer) — ~64 KB each on
qwen3.8-27b — but every accept path consumes only intermediates[a-1].
Under the opt-in lever the new gated_delta_rule_wy{K}_defer entries
run the identical gated_delta_rule_wyn_impl arithmetic with all state
stores removed (h_state stays H0), and gated_delta_rule_commit replays
the accepted prefix 0..a-1 through the same impl body, storing only
H_{a-1} — bit-identical to intermediates[a-1]/final H.

Conv outputs/gates are staged into per-slot commit buffers at verify
time (the shared ctx.buffers scratch is reused by the next forward).
Accept paths (commit_accepted_prefix, rollback_ssm_states, batched
DFlash) launch the commit kernel whenever a layer's
gdn_commit_pending flag is set — full accept is no longer a no-op for
deferred layers, since h_state was never written. Conv intermediates
are unchanged; wy2/3/4/wy17 and the f16/resident variants are
untouched; the strix-hip wyN port predates the table form so the
lever stays inert there (handles resolve to 0).

Lever off is byte-identical to main.

Test: cargo test -p spark-model --release --features cuda
  gdn_deferred_commit -- --ignored   (K in {5,8,9,12,16}, every
  a in 1..=K, outputs + committed state vs storing oracle)

Devin-Review-Token: gdn-deferred-commit
cd02041 could not reach a GPU:
- ATLAS_WYN_INSTANTIATE still called gated_delta_rule_wyn_impl<K>(...)
  after the impl gained a MODE parameter and an accepted_count argument
  (nvcc error). The legacy wy{K} entries now call impl<K, 0> with an
  unused accepted_count; their extern signature is unchanged from main.
- ATLAS_WYN_DEFER_INSTANTIATE was invoked for K=5..16 but never defined.
  It now emits gated_delta_rule_wy{K}_defer with the same 18-param
  signature (same gdn_decode_wyn launcher) calling impl<K, 1>.
- then_some(self.gdn_wyn_defer_k[k - 5]) evaluated the index eagerly:
  an underflow panic for k <= 4 (the existing r4/r7 dispatch tests hit
  it) and an out-of-bounds index at k = 17. Both sites now branch first.
- Under defer a full accept still runs commit_accepted_prefix, and the
  deferred branch restored conv state from conv_intermediate(k - 1), a
  slot the fused conv path never writes. Skip the conv restore on a full
  accept; the verify's final conv state is already canonical.

gdn_wyn_launcher_arg_count_matches_kernel_param_count pins the wy{K},
wy{K}_defer and commit entries against their launchers (18/18/14); it
fails on cd02041 because the _defer entry does not exist.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@AzeezIsh

Copy link
Copy Markdown
Contributor Author

reiner job 621 (0f5ec00): the lever changes outputs, so this stays draft. Root cause is below; a fix is in progress.

Passes:

  • CUDA oracle gdn_deferred_commit_byte_identical (K in {5,8,9,12,16}, every a).
  • Lever-off greedy output is identical to main 10d4ec7 on 5/5 probe prompts (MODE 0 identity).
  • Verify at n=16 takes 269 ms with the lever against 367/368 ms without it, over two interleaved reps each.

Fails:

  • With the lever on, greedy output differs from lever-off on code (181 vs 256 tokens) and json ("population": 2847000 vs 1250000, within ~20 tokens). The three 8-token needle prompts match.
  • The lever-on concurrency arms flag their C1 cell as under 80 % of OSL.

Cause: gdn_commit_pending = true and the lazy staging alloc run as host code inside the verify forward, and that forward is graph-captured (verify_d: Captured CUDA graph for K=γ verify (slot=0 K=8)).

  • On replay, the graph still runs the MODE-1 _defer kernel, which stores nothing.
  • The pending flag stays false, so commit_accepted_prefix takes the legacy branch and restores h_state from h_state_intermediates[a-1]. MODE 1 never wrote those.
  • The kernel oracle can't see this, because it never replays a graph.

Fix direction:

  • Decide "this verify ran deferred" on the host at verify dispatch, on the capture pass and every replay, using the same pure gdn_defer_active the layer uses to pick the kernel.
  • Preallocate staging outside capture, and make staging destinations match how each verify path keys its graphs.

… in the captured forward (#58)

Reiner job 621: with ATLAS_GDN_DEFERRED_COMMIT=1 greedy output diverged
after ~20 tokens (json "population" 1250000 -> 2847000, code 256 -> 181
tokens) although the kernel oracle passes. The verify forward is graph-
captured (verify_d logs "Captured CUDA graph for K=gamma verify"), and the
layer set gdn_commit_pending and lazily allocated its staging buffers as
host code inside that forward. On replay the graph still ran the MODE-1
_defer kernel (no state stores) while the flag stayed false, so the
accept path restored h_state from intermediates MODE 1 never wrote.

- mark_gdn_deferred_commit assigns the flag at every decode_verify*
  entry, on the capture pass and on every replay, from
  TransformerLayer::gdn_deferred_wyn (the wyN arm's own gates) plus the
  shared SsmLayerState::h_inter_pool_layout precondition.
- Staging lives in lever-gated SsmStatePool pools, bound at slot bind
  and rebind like h_state; the forward only issues the capturable copies
  and fails fast if the staging is missing.
- decode_verify_batched checks that it has one depth per sequence.

gdn_commit_pending_is_set_at_dispatch_not_in_the_layer fails on
0f5ec00; cargo test -p spark-model --lib: 857 passed.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@AzeezIsh

Copy link
Copy Markdown
Contributor Author

Folded into #127 (same three commits, patch-identical per git range-diff) to keep the DFlash2 work in one PR.

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