Skip to content

perf: Replace Q8_0 format for KV with Q4_0 + Rotation, fix window_filled for long context - #108

Merged
davide221 merged 2 commits into
Luce-Org:mainfrom
howard0su:q4rot
May 8, 2026
Merged

davide221 merged 2 commits into
Luce-Org:mainfrom
howard0su:q4rot

Conversation

@howard0su

@howard0su howard0su commented May 6, 2026 •

Copy link
Copy Markdown
Contributor

Three prefill optimizations that eliminate the Turing-specific ubatch scaling workaround and make long-context prefill dramatically faster:

  1. FA windowed prefill: use g_fa_window (default 2048) during prefill, capping attention cost at O(ubatch × window) instead of O(ubatch × kv_len). Compute time stays flat at ~1.15s/step across 100K context (was 3.5s→13s).

  2. last_token_logits_only: skip lm_head matmul for all but the last token during prefill (only last_tok is needed to seed decode). Saves ~233MB scratch at ubatch=384 and eliminates a large [vocab × ubatch] matmul.

  3. gallocr pre-reserve: build max-shape graph before prefill loop so gallocr allocates its CUDA buffer once upfront, preventing hundreds of reallocations as the mask grows.

Three prefill optimizations that eliminate the Turing-specific ubatch
scaling workaround and make long-context prefill dramatically faster:

1. FA windowed prefill: use g_fa_window (default 2048) during prefill,
   capping attention cost at O(ubatch × window) instead of O(ubatch × kv_len).
   Compute time stays flat at ~1.15s/step across 100K context (was 3.5s→13s).

2. last_token_logits_only: skip lm_head matmul for all but the last token
   during prefill (only last_tok is needed to seed decode). Saves ~233MB
   scratch at ubatch=384 and eliminates a large [vocab × ubatch] matmul.

3. gallocr pre-reserve: build max-shape graph before prefill loop so gallocr
   allocates its CUDA buffer once upfront, preventing hundreds of
   reallocations as the mask grows.

Results (RTX 2080 Ti 22GB, Qwen3.5-27B Q4_K_M, Q4_0+rot_K, 100K tokens):
  Before: ubatch=16 (forced on Turing), 846s, degrading 97→30 tok/s
  After:  ubatch=384, 301s, constant 330 tok/s  (2.8× faster)

Co-authored-by: Copilot <[email protected]>
@howard0su

Copy link
Copy Markdown
Contributor Author

need additional change in llama.cpp
0001-turbo_wht-support-strided-input-fused-wht-quantize-k.patch

@howard0su howard0su changed the title perf: windowed prefill + last-token logits, 2.8× faster at 100K perf: Replace Q8_0 format for KV with Q4_0 + Rotation, fix window_filled for long context May 6, 2026

@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.

1 issue found across 5 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="dflash/src/qwen35_target_graph.cpp">

<violation number="1" location="dflash/src/qwen35_target_graph.cpp:74">
P1: Do not force graph-side K rotation for TQ3_0; that type already applies the WHT during quantization, so this double-rotates the K cache.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Comment thread dflash/src/qwen35_target_graph.cpp Resolved
…g quantization)

TQ3_0 applies WHT during its own quantization path. Setting
kv_k_rotated=true unconditionally caused double-rotation of the K
cache when TQ3_0 was selected, corrupting attention scores.

Now kv_k_rotated is only set when K type is not TQ3_0.

Co-authored-by: Copilot <[email protected]>
@howard0su

Copy link
Copy Markdown
Contributor Author

need double check the quality of the output

@davide221

davide221 commented May 7, 2026 •

Copy link
Copy Markdown
Contributor
Screenshot 2026-05-07 alle 11 58 35 Screenshot 2026-05-07 alle 11 58 11

@howard0su

@howard0su

Copy link
Copy Markdown
Contributor Author

Looks good although I cannot explain why AL is increased.

@davide221
davide221 merged commit 4cc21be into Luce-Org:main May 8, 2026
1 check passed
davide221 added a commit that referenced this pull request May 8, 2026
Resolve conflict in dflash/test/test_dflash.cpp: keep both main's
TQ3_0 KV-stride padding block (q4rot fix from PR #108) and the PR's
laguna-aware early-exit (skips the prompt/out positional-arg check
when the target is laguna and dispatches to run_laguna_daemon).

Co-Authored-By: WOZCODE <[email protected]>
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
perf: Replace Q8_0 format for KV with Q4_0 + Rotation, fix window_filled for long context
smpurkis pushed a commit to smpurkis/lucebox-hub that referenced this pull request May 10, 2026
Resolve conflict in dflash/test/test_dflash.cpp: keep both main's
TQ3_0 KV-stride padding block (q4rot fix from PR Luce-Org#108) and the PR's
laguna-aware early-exit (skips the prompt/out positional-arg check
when the target is laguna and dispatches to run_laguna_daemon).

Co-Authored-By: WOZCODE <[email protected]>
@howard0su
howard0su deleted the q4rot branch May 22, 2026 04:10
oliveagle pushed a commit to oliveagle/lucebox-hub that referenced this pull request May 22, 2026
perf: Replace Q8_0 format for KV with Q4_0 + Rotation, fix window_filled for long context
oliveagle pushed a commit to oliveagle/lucebox-hub that referenced this pull request May 22, 2026
Resolve conflict in dflash/test/test_dflash.cpp: keep both main's
TQ3_0 KV-stride padding block (q4rot fix from PR Luce-Org#108) and the PR's
laguna-aware early-exit (skips the prompt/out positional-arg check
when the target is laguna and dispatches to run_laguna_daemon).

Co-Authored-By: WOZCODE <[email protected]>
oliveagle pushed a commit to oliveagle/lucebox-hub that referenced this pull request May 22, 2026
perf: Replace Q8_0 format for KV with Q4_0 + Rotation, fix window_filled for long context
oliveagle pushed a commit to oliveagle/lucebox-hub that referenced this pull request May 22, 2026
Resolve conflict in dflash/test/test_dflash.cpp: keep both main's
TQ3_0 KV-stride padding block (q4rot fix from PR Luce-Org#108) and the PR's
laguna-aware early-exit (skips the prompt/out positional-arg check
when the target is laguna and dispatches to run_laguna_daemon).

Co-Authored-By: WOZCODE <[email protected]>
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