Skip to content

fix(dflash): pad KV mask to 256 when TQ3_0 is selected via -ctk/-ctv - #128

Merged
davide221 merged 1 commit into
Luce-Org:mainfrom
phazei:fix/dflash-tq3-mask-stride
May 8, 2026
Merged

davide221 merged 1 commit into
Luce-Org:mainfrom
phazei:fix/dflash-tq3-mask-stride

Conversation

@phazei

@phazei phazei commented May 8, 2026 •

Copy link
Copy Markdown
Contributor

Tried using TQ3_0 KV cache on a 32 GB RTX 5090 to free VRAM for a larger context window. Server came up clean, but every reply past a short prompt was a single "!" token followed by an empty response. Short prompts worked. Output was non-deterministic across runs, so the prime suspect was uninitialized memory.

Testing across ubatch sizes showed the failure wasn't a simple threshold, it hit specific values clustered around multiples of 32 ({26-32, 63-64, 95-96, 128, 160, 192, 224, 320, 384}) while powers of 2 at or above 256 passed cleanly. Instrumenting the chunked flash-attention path traced the NaN to the softmax kernel reading the causal mask out of bounds: the mask tensor was (384, 384) when the kernel expected (512, 384). The read at mask[383*384 + 442] landed past the buffer end into adjacent GPU memory, where stray fp16 bits like 0xfe19 decoded as NaN and poisoned the softmax.

The stride bump from 32 to 256 already existed for the legacy DFLASH27B_KV_TQ3=1 env path, but the newer -ctk/-ctv flags (which set DFLASH27B_KV_K / DFLASH27B_KV_V) never triggered it.

Fix: re-check DFLASH27B_KV_K / DFLASH27B_KV_V after argv parsing and bump g_kq_stride_pad to 256 if either starts with "tq3". The legacy path is left in place; this is purely additive.

Reproducer: -ctk tq3_0 -ctv tq3_0 with any prompt long enough to hit PREFILL_UBATCH > 25 (default switches at 2048 tokens) returns "!" before this fix and correct output after.

The bug is not architecture-specific, it affects any GPU when TQ3_0 is selected via the newer -ctk/-ctv flags rather than the legacy DFLASH27B_KV_TQ3=1 env. It was caught on an RTX 5090 but would reproduce identically on a 3090 under the same flag path.

The host-side `build_causal_mask` sizes the mask buffer as
`align_up(kv_len, g_kq_stride_pad)`. `g_kq_stride_pad` is bumped from
`KQ_MASK_PAD=32` to `256` so the mask matches the TurboQuant chunked-FA
driver, which pads `kv_len` to `align_up(kv_len, FATTN_KQ_STRIDE=256)`
when KV is TQ3_0.

The bump used to fire only on the legacy `DFLASH27B_KV_TQ3=1` path. When
the KV type is selected via `-ctk tq3_0` / `-ctv tq3_0` (which sets
`DFLASH27B_KV_K` / `DFLASH27B_KV_V` instead), the bump never ran. With
stride at 32 the mask was sized at `align_up(N, 32)` while the kernel
read at `align_up(N, 256)`, walking off the end of the mask buffer into
uninitialized GPU memory. The OOB fp16 garbage occasionally encoded NaN,
producing per-thread NaN at any prefill ubatch `N` where
`align_up(N, 256) > align_up(N, 32)` — notably 256, 512, 1024, 2048.

Re-check `DFLASH27B_KV_K` / `DFLASH27B_KV_V` after argv parsing and bump
`g_kq_stride_pad` to 256 if either starts with "tq3" (case-insensitive).
The legacy `DFLASH27B_KV_TQ3=1` block is left in place; this is
additive.

Reproducer: `-ctk tq3_0 -ctv tq3_0` with prefill ubatch in {256, 512,
1024, 2048} produced random non-determinism on RTX 5090 (sm_120) before
this fix, and produces the same output across runs after.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No issues found across 1 file

@davide221
davide221 merged commit 27cff04 into Luce-Org:main May 8, 2026
1 check passed
dusterbloom added a commit to dusterbloom/lucebox-hub that referenced this pull request May 8, 2026
Replaces the layer-by-layer gemma4_pflash_prefill() with a single-graph-
per-chunk path using the new GGML_OP_FLASH_ATTN_SPARSE op for full-
attention layers. SWA layers continue to use ggml_flash_attn_ext.

Perf (MoE 26B-A4B at 64K, RTX 3090, Q8_0 KV):
  chunked baseline:  1867 tok/s prefill, 100.6 tok/s decode, 10.67/16 accept
  + --pflash:        3374 tok/s prefill (1.81x), 101.8 tok/s decode

Changes:
- Adapter (pflash_ggml_adapter.cpp/h) registers the pFlash CUDA kernel
  with the ggml op. Maps alpha>=1.0 to fully-dense mode.
- build_full_attn_block() conditionally uses ggml_flash_attn_sparse
  when use_pflash is set.
- attn_mask is skipped (in graph + driver) when use_pflash=true since
  the sparse op applies block-level causal internally.
- gemma4_pflash_prefill.cpp removed (replaced by chunked path).
- test/test_flash_attn_sparse.cpp: TDD coverage for the ggml op
  (dense vs sparse @ alpha=1.0 within BF16 precision; alpha<1.0 liveness).

Ported upstream fixes:
- TQ3_0 mask stride (PR Luce-Org#128): bump g_kq_stride_pad to 256 when KV is
  selected via DFLASH27B_KV_K/V env vars. Prevents NaN at chunk sizes
  256/512/1024/2048 with TQ3_0 KV.
- last_token_logits_only (PR Luce-Org#108): skip lm_head matmul over all but
  last token during prefill chunks. Saves ~1GB output tensor and
  ~1000x lm_head compute per chunk on Gemma4-31B (vocab=262144).
smpurkis pushed a commit to smpurkis/lucebox-hub that referenced this pull request May 10, 2026
fix(dflash): pad KV mask to 256 when TQ3_0 is selected via -ctk/-ctv
oliveagle pushed a commit to oliveagle/lucebox-hub that referenced this pull request May 22, 2026
fix(dflash): pad KV mask to 256 when TQ3_0 is selected via -ctk/-ctv
oliveagle pushed a commit to oliveagle/lucebox-hub that referenced this pull request May 22, 2026
fix(dflash): pad KV mask to 256 when TQ3_0 is selected via -ctk/-ctv
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.

2 participants