Skip to content

draft: sliding window attention for Qwen3.6 draft model - #129

Merged
davide221 merged 1 commit into
Luce-Org:mainfrom
howard0su:swa
May 10, 2026
Merged

davide221 merged 1 commit into
Luce-Org:mainfrom
howard0su:swa

Conversation

@howard0su

@howard0su howard0su commented May 8, 2026 •

Copy link
Copy Markdown
Contributor

Implement per-layer K/V truncation in the draft graph for SWA layers (Qwen3.6 pattern: layers 0..n-2 use sliding window, last layer full, while Qwen3.5 draft model use all full layers).

When --draft-swa= is set and ctx_len > window, SWA layers:

  • Window target_feat to last positions via ggml_view_3d
  • Adjust positions_k to start from the window offset
  • Use eff_total_k = window + q_len for K/V reshape

Also raises the effective DRAFT_CTX_MAX when SWA is enabled so that ctx_len can exceed the window size and SWA truncation can activate.

Usage: --draft-swa=2048 or DFLASH27B_DRAFT_SWA=2048

Benchmark (RTX 2080 Ti, Qwen3.6-27B Q4_K_M + DFlash draft)

Context Baseline AL SWA=2048 AL Δ AL Baseline tok/s SWA tok/s
6K 3.27 4.57 +40% 27.00 34.49
9K 4.92 5.57 +13% 41.16 42.01
16K 4.49 7.76 +73% 38.10 58.29
24K 4.34 4.41 +2% 35.91 33.10

Significant AL and throughput improvement at 6K–16K context. No regression on short prompts (HumanEval bench: identical AL at 5.47).

@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 4 files

@davide221

davide221 commented May 8, 2026 •

Copy link
Copy Markdown
Contributor
Screenshot_2026-05-08_alle_19 15 39

Getting a bit of mixed results on rtx 3090

@howard0su
howard0su marked this pull request as draft May 8, 2026 23:20
Implement per-layer K/V truncation in the draft graph for SWA layers
(Qwen3.6 pattern: layers 0..n-2 use sliding window, last layer full).

When --draft-swa=<window> is set and ctx_len > window, SWA layers:
- Window target_feat to last <window> positions via ggml_view_3d
- Adjust positions_k to start from the window offset
- Use eff_total_k = window + q_len for K/V reshape

The draft context cap is controlled separately via --draft-ctx-max=N
(default 2048). To exercise SWA, set draft-ctx-max > draft-swa so that
the full-attention last layer sees more context while SWA layers stay
windowed.

Usage:
  --draft-swa=2048 --draft-ctx-max=4096
  DFLASH27B_DRAFT_SWA=2048 DFLASH27B_DRAFT_CTX_MAX=4096

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

Copy link
Copy Markdown
Contributor
Screenshot 2026-05-10 alle 12 40 02 very good results now

@howard0su
howard0su marked this pull request as ready for review May 10, 2026 11:23

@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 5 files

@davide221
davide221 merged commit 02750ff into Luce-Org:main May 10, 2026
1 check passed
dusterbloom added a commit to dusterbloom/lucebox-hub that referenced this pull request May 10, 2026
Adapts PR Luce-Org#129 (howard0su/swa) — sliding-window attention truncation
for the qwen3 draft graph — to the gemma4 cached-KV draft layout.

draft graph (gemma4_dflash_graph.cpp):
  * draft_swa_trunc_enabled() — opt-in via DFLASH_DRAFT_SWA_TRUNC=1.
    For SWA layers (first n-1; final layer stays full attn), restrict
    K_full / V_full views to the last (sliding_window + n_tokens) slots
    and copy a contiguous mask slice (ggml_cont) for FA.
  * draft_rope() — single wrapper around ggml_rope_ext used at all
    three draft RoPE sites. Optional YaRN scaling via
    DFLASH_DRAFT_YARN=1 (default n_ctx_orig=32768, override via
    DFLASH_DRAFT_YARN_NCTX_ORIG).

test harness (test_gemma4_dflash.cpp):
  * --draft-swa-trunc CLI flag mirroring the env var.
  * Bundles the bench-harness infrastructure that has been in-progress
    on this branch: adaptive draft_max, --draft-kv-cap override,
    --mem-diag, --fa-window plumbing through build_gemma4_step.

Bench (RTX 3090, gemma-4-31B-Q4_K_M target + qwen3 5-layer draft,
50K-token prompt, n_predict=64, ctx=65536, NO_VMM=1):

  cap   | SWA | AL   | decode tok/s
  ------+-----+------+--------------
  2096  | off | 1.36 | 1.31
  2096  |  on | 1.73 | 1.68
  8192  | off | 1.02 | 4.29
  8192  |  on | 1.68 | 6.96   <-- +65% AL, +62% decode

The SWA truncation does not fix the underlying long-position
acceptance collapse (the qwen3 draft model itself appears to have
been effectively trained at <=32K positions; see comment near the
sliding re-prefill block in test_gemma4_dflash.cpp). It is a real
partial improvement shippable today; the residual cliff needs a
long-context drafter.

The diff is large because it bundles the pre-existing harness
infrastructure noted above; happy to split if reviewers prefer.
oliveagle pushed a commit to oliveagle/lucebox-hub that referenced this pull request May 22, 2026
draft: sliding window attention for Qwen3.6 draft model
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