Skip to content

spark-model: grammar-mask the DFlash2 drafter's pos+1 rows so draft 0 is always legal (#102) - #103

Merged
AzeezIsh merged 1 commit into
mainfrom
fix/dflash2-grammar-mask-draft0
Sep 27, 2026
Merged

AzeezIsh merged 1 commit into
mainfrom
fix/dflash2-grammar-mask-draft0

Conversation

@AzeezIsh

Copy link
Copy Markdown
Contributor

Fixes the first limiter in #102. DFlash2 drafts in grammar-constrained (tool-call) output were not grammar-masked: propose_drafts_on_lane received the live bitmask and ignored it (_grammar_bitmask). truncate_drafts_at_grammar_boundary therefore often truncated at draft 0 (kept=0), which threw away the step's γ-row propose. On the 27B agentic leg that meant thousands of truncations and mean accepted of 0.8–1.3.

What changes:

  • New kernel. atlas_apply_grammar_bitmask (kernels/{gb10,strix-hip}/common/grammar_bitmask.cu) writes bf16 −inf over disallowed ids in one logits row. It follows the xgrammar convention: a set bit means allowed.
  • Which rows. Before the tail argmax / DFlash2 candidate selector, the kernel masks logits rows 0 and 1. Row 0 is the anchor, bonus and selector chain seed; row 1 is draft 0. Both predict pos+1, the position the live matcher covers, so draft 0 is always grammar-legal. Rows 2..γ−1 keep the existing truncation guard.
  • Serial and batched paths. The serial path uploads the mask into DflashScratch.grammar_bitmask_dev. Batched propose uploads per-sequence masks into batch_grammar_bitmask (batch_capacity × ceil(vocab/32) words) and masks rows i·γ and i·γ+1. The scheduler passes per-sequence masks (mtp_grammar_masks_for) on the DFlash batch, K4-batch and bootstrap paths.
  • Graphs. A mask forces the eager tail (graph_eligible &&= mask.is_none()), because a captured graph would bake in a stale mask.
  • No mask, no change. A null mask is byte-identical to before, with no extra launch or upload. The kernel handle comes from try_kernel. It's a common/ kernel, so every target ships it.

Validation:

  • fmt, clippy (spark-model, spark-server --tests), cargo check --features cuda, typos, check_kernel_shadows, line cap.
  • GB10: cargo test -p spark-model --release grammar_bitmask -- --ignored --nocapture. The masked argmax must pick a legal id, rows 2 and 3 must be untouched, and −inf must be written.
  • GB10: a 27B DFlash2 agentic probe, against the job-448 baseline of 5591 truncations in 129 requests and mean accepted 0.81. Measure kept=0 truncations, mean accepted and turn time.

#102)

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

Triage (2026-09-26). Small and targeted. It fixes #102's first limiter by masking rows 0–1 so draft 0 is always grammar-legal, and CI is green apart from the non-blocking benchmark gate.

Merge condition: a re-run of the job-448 config on this branch, showing:

  • mean accepted per step (0.81 before);
  • grammar truncations (5591 in 129 requests before);
  • plus the ST-995 on the 27B DFlash2 recipe.

If mean accepted is still well below MTP's on that leg, the DFlash2 agentic lane should stop here (see the note on #104).

@AzeezIsh

Copy link
Copy Markdown
Contributor Author

HIP compile check for the strix-hip twin (CI only builds the gb10 PTX): kernels/strix-hip/common/grammar_bitmask.cu @ d6b3d0a0e compiles for gfx1151. Setup: TheRock/ROCm hipcc, -O3 -ffp-contract=off --offload-arch=gfx1151, the same mask-widen mirror step as build.rs. It emits atlas_apply_grammar_bitmask with 4 VGPRs and 0 scratch. Strix queue job 035-hipcc-pr103. This covers compilation only; nothing ran on hardware.

@AzeezIsh

Copy link
Copy Markdown
Contributor Author

GB10 validation (reiner job 483, d6b3d0a0e):

  • CUDA test grammar_bitmask_masks_row_01_only: ok.
  • 30-min DFlash2 agentic probe, with the same serve as job 448 (27B DFlash2 γ=8, prefix caching on, reasoning off):
job 448 (main, no mask) job 483 (this PR)
turns completed in ~30 min 127 (1775 s) 146 (1794 s), +15 %
kept=0 truncations frequent 0
grammar truncations per request 43 39
mean accepted per step 0.81 0.81
TTFT average 1.09 s 1.10 s
server errors / failed turns 0 / 0 0 / 0

Reading:

This PR doesn't make DFlash2 competitive with MTP for agentic serving. It does remove a structural waste at no measured cost. Merging.

@AzeezIsh
AzeezIsh marked this pull request as ready for review September 27, 2026 05:25
@AzeezIsh
AzeezIsh merged commit 3fcf8ff into main Sep 27, 2026
25 of 26 checks passed
AzeezIsh added a commit that referenced this pull request Sep 27, 2026
docs(status): #84/#103 merged, release 3fcf8ff verified, reiner queue
@AzeezIsh
AzeezIsh deleted the fix/dflash2-grammar-mask-draft0 branch September 29, 2026 06:01
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