Repository navigation
fix(dflash): pad KV mask to 256 when TQ3_0 is selected via -ctk/-ctv - #128
Merged
Merged
Conversation
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.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.