Skip to content

[Qwen3.8 CP 1/4] Context parallelism for QSA attention and the sparse indexer - #39721

Merged
Fridge003 merged 7 commits into
sgl-project:mainfrom
Shunkangz:cp/1-qsa
Oct 1, 2026
Merged

Fridge003 merged 7 commits into
sgl-project:mainfrom
Shunkangz:cp/1-qsa

Conversation

@Shunkangz

@Shunkangz Shunkangz commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Adds the prefill-CP path to the Qwen sparse attention backend and its indexer, plus the shared block-layout helpers the CP collectives need.

QwenSparseAttnBackend._forward_extend_cp all-gathers the local K/V into global token order, writes the complete sequence into every rank's pool (out_cache_loc is trimmed to the full extend count by the runner), and issues one sparse_gqa call per zigzag block. QSAIndexer.forward_cuda_cp projects q/k on local rows with local positions, all-gathers the raw index-K into global order for the unchanged ring store and block compression, then scores and selects top-k on local rows -- so the output rows already line up with the local q rows and need no gather after.

layers/cp/collocated.py holds the row-layout helpers shared by any collocated-CP model: the rank-major block gather/scatter and the reduce-scatter that lands each rank's zigzag rows. The CPU unit test pins the invariant they rest on -- RS(cp_rank_major_blocks(x))[rank] must equal what ZigzagCPStrategy.shard_hidden_states gives that rank.

Depends only on the existing prefill-CP infrastructure (is_cp_active, attn_cp_metadata, ZigzagCPStrategy); no model is wired to it yet.

Modifications

Accuracy Tests

Speed Tests and Profiling

Checklist

Review and Merge Process

  1. Ping Merge Oncalls to start the process. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • Common commands include /tag-and-rerun-ci, /tag-run-ci-label, /rerun-failed-ci
  4. After green CI and required approvals, ask Merge Oncalls or people with Write permission to merge the PR.

CI States

Latest PR Test (Base): 🚫 Run #36677787006
Latest PR Test (Extra): ❌ Run #36677786667
Latest PR Test (AMD ROCm 10): ❌ Run #36677786943

@Shunkangz Shunkangz changed the title [CP] Context parallelism for QSA attention and the sparse indexer [Qwen3.8 CP 1/4] Context parallelism for QSA attention and the sparse indexer Sep 16, 2026
detain added a commit to detain/sglang that referenced this pull request Sep 17, 2026
Manifest of the nine upstream PRs and the local commits this branch
carries on top of upstream main, in build order, with the reasoning for
the CP series (sgl-project#39721-sgl-project#39724) that was evaluated and left out, the
HiCache caveat (sgl-project#39830), and the editable-install trap that makes git
worktrees useless for baselining library changes here.

Generated from git, so the SHAs match the branch exactly.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Comment thread python/sglang/srt/layers/cp/collocated.py Outdated
Comment thread python/sglang/srt/layers/attention/qwen_sparse_attn_backend.py
@Fridge003

Copy link
Copy Markdown
Collaborator

Please be aware that the layer communicator is under refactor (see this PR list #41557)

It would be great if the communication operations between Attention and FFN can be rebased on the latest implementation of layer communicator

Shunkangz and others added 2 commits September 28, 2026 22:00
Adds the prefill-CP path to the Qwen sparse attention backend and its
indexer, plus the shared block-layout helpers the CP collectives need.

QwenSparseAttnBackend._forward_extend_cp all-gathers the local K/V into
global token order, writes the complete sequence into every rank's pool
(out_cache_loc is trimmed to the full extend count by the runner), and
issues one sparse_gqa call per zigzag block. QSAIndexer.forward_cuda_cp
projects q/k on local rows with local positions, all-gathers the raw
index-K into global order for the unchanged ring store and block
compression, then scores and selects top-k on local rows -- so the output
rows already line up with the local q rows and need no gather after.

layers/cp/collocated.py holds the row-layout helpers shared by any
collocated-CP model: the rank-major block gather/scatter and the
reduce-scatter that lands each rank's zigzag rows. The CPU unit test
pins the invariant they rest on -- RS(cp_rank_major_blocks(x))[rank]
must equal what ZigzagCPStrategy.shard_hidden_states gives that rank.

Depends only on the existing prefill-CP infrastructure (is_cp_active,
attn_cp_metadata, ZigzagCPStrategy); no model is wired to it yet.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@Shunkangz

Copy link
Copy Markdown
Contributor Author

Please be aware that the layer communicator is under refactor (see this PR list #41557)

It would be great if the communication operations between Attention and FFN can be rebased on the latest implementation of layer communicator

For this PR, I only support the context parallel inside qsa. There is no change of communication between Attention and FFN. I will pay more attention about this when I integrate the E2E cp in the latter PR. Thank you.

@Shunkangz

Copy link
Copy Markdown
Contributor Author

/tag-run-ci-label

@github-actions github-actions Bot added the run-ci CI: run the baseline test suite on this PR label Sep 30, 2026
@Shunkangz
Shunkangz requested a review from Fridge003 September 30, 2026 06:41
@Fridge003

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/unit/cp/test_qsa_cp_unit.py

@github-actions

github-actions Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/unit/cp/test_qsa_cp_unit.py:

🚀 ubuntu-latest (1 test): ✅ View workflow run

cd test/ && python3 registered/unit/cp/test_qsa_cp_unit.py

@Fridge003
Fridge003 merged commit 266d9d1 into sgl-project:main Oct 1, 2026
149 of 169 checks passed
alexnails added a commit to alexnails/sglang that referenced this pull request Oct 3, 2026
The merge broke every CPU, GPU and jit-kernel shard with

    ImportError: cannot import name 'MultiPlatformOp'
    from 'sglang.srt.layers.utils'

This branch drops that re-export on purpose: it was the only reason
sglang.srt.layers.utils -- imported by dozens of model files for
PPMissingLayer / get_layer_id -- pulled in sglang.kernels.fused_op. The
supported spelling is the module path, and the alias module stays.

Since then main grew two importers of the old spelling, dsa_indexer_kpool
(sgl-project#39273) and qsa_indexer (sgl-project#39721). Neither conflicts textually, so the merge
took both sides cleanly and the break only showed up in CI.

The blast radius was wider than the two files because
import_model_classes catches every exception and logs a warning, so any
model whose chain reached either one silently vanished from the arch table
and surfaced much later as "KimiLinearForCausalLM has no SGlang
implementation" in the unified-memory e2e test.

Verified: every sglang.srt module now imports except for absent optional
deps (polars, numba, ray, vllm, cutlass), and all 36 attributes that
sglang.srt.platforms consumers read off current_platform resolve on
SRTPlatform.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants