Skip to content

perf(qwen35): upload only the live columns of the causal mask - #765

Merged
davide221 merged 4 commits into
mainfrom
perf/qwen35-mask-live-width
Sep 26, 2026
Merged

davide221 merged 4 commits into
mainfrom
perf/qwen35-mask-live-width

Conversation

@davide221

@davide221 davide221 commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The Qwen3.5/3.6/3.8 target graph sizes its attention mask to max_ctx, so the graph allocator never has to grow. The host then built and uploaded the whole max_ctx-wide mask on every prefill chunk and every verify step. At --max-ctx 131072 that is about 128 MiB per 512-token prefill chunk and a few MiB per verify step, regardless of the real context length.

Flash attention reads mask columns only inside the KV view: the window rounded up to at most 256 keys. This change builds and copies only those columns plus one 256 stride, using a strided 2-D write into the same tensor. The graph shape is unchanged, and every mask value a kernel reads is unchanged.

  • qwen35_causal_mask_live_width() + upload_qwen35_causal_mask_window() in qwen35/prefill_helpers.h
  • used by chunked prefill, the linear verify path in qwen35_dflash_target.cpp, and the layer-split forward in layer_split_forward.cpp, which uploaded the mask once per attention layer (tree and kvflash-pool masks unchanged)
  • kill switch LUCE_QWEN35_MASK_FULL_WIDTH=1, documented in docs/ENVIRONMENT.md
  • unit test test_qwen35_mask_upload.cpp: the width covers the attention view, and every uploaded row equals the matching prefix of the full-width mask

Results

R9700 (gfx1201), Qwen3.8-27B UD-IQ4_XS + DFlash2 q8_0 drafter, published profile (--draft-block-size 16 --max-ctx 131072 --cache-type-k q8_0 --cache-type-v q8_0), main a5050c703 vs this branch, same server settings:

main this PR
output sha256, 11 prompts (5 code, 4 chat, 1 x 26.7K-token context, warm-up) identical on all 11
decode, per-request wall (same step counts) 2.715 / 5.017 s 2.639 / 4.875 s (-2.8%)
host build + upload per 512-token prefill chunk 67.6 ms 21.4 ms
26.7K-token prefill 34.7 s 31.8 s (-8.4%)

Layer split across R9700 + Strix Halo (--target-split-mode layer --target-layer-split 0.75,0.25), same model and flags, 7 prompts byte-identical:

main this PR
code decode 24.8 / 33.1 tok/s 35.0 / 46.5 tok/s
7.6K-token prefill 20.6 s 12.5 s
7.6K-context decode 17.1 tok/s 22.4 tok/s

The ~2.3 ms H2D copy on 479 of 504 verify steps in a rocprofv3 trace is the mask; it drops to a few KB at short context.

Test plan

  • test_server_unit: 600/600 pass on gfx1201 (includes the 2 new cases)
  • Byte-identical outputs vs main on the prompt set above
  • Prefill timing via LUCE_PREFILL_TIMING=1

🤖 Generated with Claude Code

The Qwen3.5/3.6/3.8 target graph sizes its attention mask to max_ctx so the
graph allocator never has to grow. The host then built and copied the whole
max_ctx-wide mask on every prefill chunk and every verify step: at
--max-ctx 131072 that is about 128 MiB per 512-token prefill chunk and a
few MiB per verify step, whatever the real context length.

Flash attention reads mask columns only inside the KV view (the window
rounded up to at most 256 keys). Build and copy just those columns plus one
256 stride with a strided 2-D write into the same tensor. The graph shape
is unchanged and every value the kernels read is unchanged.

R9700, Qwen3.8-27B UD-IQ4_XS + DFlash2 block 16, published profile:
output byte-identical on 11 prompts; decode steps 2.8% faster; 26.7K-token
prefill 34.7 s -> 31.9 s (host build+upload 67 -> 21 ms per chunk).

LUCE_QWEN35_MASK_FULL_WIDTH=1 restores the full-width upload.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

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

All reported issues were addressed across 5 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread server/docs/ENVIRONMENT.md Resolved
Comment thread server/test/test_qwen35_mask_upload.cpp Resolved
davide221 and others added 2 commits September 25, 2026 15:45
The layer-split forward rebuilt and copied the full max_ctx-wide causal mask
for every attention layer of every step (16 per step on Qwen3.8). Use the
same live-width upload as the single-device path.

R9700 + Strix Halo layer split 0.75/0.25, Qwen3.8-27B UD-IQ4_XS + DFlash2
block 16, --max-ctx 131072: outputs byte-identical on 7 prompts; code
decode 24.8 -> 35.0 and 33.1 -> 46.5 tok/s; 7.6K-token prefill
20.6 -> 12.5 s, its decode 17.1 -> 22.4 tok/s.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Upload into a max_ctx-wide CPU tensor filled with a sentinel and check that
every row's live columns match the full-width mask and the columns past
them are left untouched.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@davide221

Copy link
Copy Markdown
Contributor Author

Addressed both review points: the layer-split forward now goes through the same live-width upload (5d1b281, measured +40% decode / -39% prefill on the R9700 + Strix layer split, outputs identical), so the ENVIRONMENT.md row is accurate; and d7ffd15 adds a CPU-backend test that calls upload_qwen35_causal_mask_window on a max_ctx-wide tensor and checks the live columns and the untouched tail. test_server_unit 601/601 on gfx1201.

@davide221

Copy link
Copy Markdown
Contributor Author

@cubic-dev-ai review the current head

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review the current head

@davide221 I have started the AI code review. It will take a few minutes to complete.

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

All reported issues were addressed across 6 files

Requires human review: Auto-approval blocked because this review re-detected 1 unresolved issue already reported by Cubic.

Re-trigger cubic

A second ctest runs the mask upload cases with LUCE_QWEN35_MASK_FULL_WIDTH=1
and checks that every column is written.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@davide221

Copy link
Copy Markdown
Contributor Author

The remaining point, kill-switch coverage for LUCE_QWEN35_MASK_FULL_WIDTH, is added in 6d6e518. The switch is read once per process, so a second ctest, server_unit_qwen35_mask_full_width, runs the Qwen35MaskUploadFixture cases with LUCE_QWEN35_MASK_FULL_WIDTH=1; in that mode the upload case checks that every column is written. The existing server_unit CI filters match the new name. On gfx1201: 4/4 mask tests pass, 3 cases in the kill-switch run, test_server_unit 601/601.

@cubic-dev-ai review the current head

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

The remaining point, kill-switch coverage for LUCE_QWEN35_MASK_FULL_WIDTH, is added in 6d6e518. The switch is read once per process, so a second ctest, server_unit_qwen35_mask_full_width, runs the Qwen35MaskUploadFixture cases with LUCE_QWEN35_MASK_FULL_WIDTH=1; in that mode the upload case checks that every column is written. The existing server_unit CI filters match the new name. On gfx1201: 4/4 mask tests pass, 3 cases in the kill-switch run, test_server_unit 601/601.

@cubic-dev-ai review the current head

@davide221 I have started the AI code review. It will take a few minutes to complete.

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

Re-trigger cubic

@davide221
davide221 merged commit 6592820 into main Sep 26, 2026
9 checks passed
@davide221
davide221 deleted the perf/qwen35-mask-live-width branch November 9, 2026 10:25
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