Skip to content

[Feature] Add initial support for sequence parallelism - #1436

Closed
Ying1123 wants to merge 1 commit into
mainfrom
seq-parallel
Closed

Ying1123 wants to merge 1 commit into
mainfrom
seq-parallel

Conversation

@Ying1123

Copy link
Copy Markdown
Contributor

@ZYHowell @ivanium Moved from #1041

@Ying1123 Ying1123 changed the title Add initial support for sequence parallelism [Feature] Add initial support for sequence parallelism Sep 16, 2024
@merrymercy merrymercy mentioned this pull request Sep 17, 2024
3 tasks
@Ying1123
Ying1123 marked this pull request as draft September 19, 2024 01:39
@merrymercy merrymercy mentioned this pull request Sep 22, 2024
21 of 37 tasks
@kuangdao

Copy link
Copy Markdown

From the code i see, the prefill stage after attention, the shape of output is [padded_total_num_tokens, q_head_num // SP_SIZE, head_dim], and then out * RowSeqParallelLinear which need use allreduce. the input of qkv_proj_linear is [padded_total_num_tokens, q_head_num, head_dim] which not spilted by sp_size. i want to know why done use ring attention , ring attention seems better then it in both Computing and Communication.

Comment on lines +273 to +284
For each SP worker, we have either (1) QKV of entire sequences:
q tensor: [padded_total_num_tokens, q_head_num // SP_SIZE, head_dim]
k tensor: [padded_total_num_tokens, k_head_num, head_dim]
v tensor: [padded_total_num_tokens, v_head_num, head_dim]
Or (2) Q of entire sequences and KV of the current SP shard:
q tensor: [padded_total_num_tokens, q_head_num // SP_SIZE, head_dim]
k tensor: [padded_sp_shard_num_tokens, k_head_num, head_dim]
v tensor: [padded_sp_shard_num_tokens, v_head_num, head_dim]

Case (1) saves cross-SP-worker communication, while case (2) saves computation
to get K and V for entire sequences but need computation in SP attn.
"""

@Edenzzzz Edenzzzz Dec 3, 2024 •

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.

(2) seems to be able to split workload and overlap even with single query. But just curious, does anyone have opinions on TreeAttention (just all-reduce lse instead of sending KV), which seems optimized for decoding?

Comment on lines +442 to +444
# TODO: in fact we can use all-to-all to gather the output and state here
# to collect only q head shards that are needed by the current SP worker.
# All-to-all will save communication and `merge_state` computation.

@Edenzzzz Edenzzzz Dec 3, 2024 •

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.

Later all-reduce in ColumnSeqParallelLinear ?

@fangtaosong

fangtaosong commented Dec 31, 2024 •

Copy link
Copy Markdown

Could this feature be integrated in the early months of 2025, and by the way, why not use the ring-attention which offers better performance? @merrymercy @Ying1123

@Edenzzzz

Edenzzzz commented Jan 1, 2025

Copy link
Copy Markdown
Contributor

Could this feature be integrated in the early months of 2025, and by the way, why not use the ring-attention which offers better performance? @merrymercy @Ying1123

This PR already implements ring attention.

@fangtaosong

Copy link
Copy Markdown

Could this feature be integrated in the early months of 2025, and by the way, why not use the ring-attention which offers better performance? @merrymercy @Ying1123

This PR already implements ring attention.

However, the code heavily relies on tensor-parallelism, and its layout [s, h//sp, d] seems to be inequivalent to flash-linear-attention in xDit [s//sp, h, d] in both computation and memory access.

@HJSang

HJSang commented Jan 31, 2025

Copy link
Copy Markdown
Collaborator

Is this still active? Looking forward for this change. It will be super helpful if we really want to handle long context.

@Edenzzzz

Edenzzzz commented Feb 5, 2025 •

Copy link
Copy Markdown
Contributor

However, the code heavily relies on tensor-parallelism, and its layout [s, h//sp, d] seems to be inequivalent to flash-linear-attention in xDit [s//sp, h, d] in both computation and memory access.

The reason for coupling with TP is probably that if we use pure ring attn, we can only replicate Q on all ranks, which causes redundant computation, while if we shard Q there's no redundancy.
I think this could be solved with Tree attention (replicate Q, all-reduce lse)

@zhaochenyang20 zhaochenyang20 mentioned this pull request Mar 3, 2025
1 of 22 tasks
@zhyncs zhyncs mentioned this pull request Mar 4, 2025
35 of 67 tasks
@Edenzzzz

Edenzzzz commented Mar 4, 2025 •

Copy link
Copy Markdown
Contributor

I'm interested in helping with finalizing this if possible :)

@merrymercy merrymercy closed this Apr 21, 2025
@zhyncs
zhyncs deleted the seq-parallel branch July 22, 2025 05:05
efschu pushed a commit to efschu/htsglang that referenced this pull request Sep 16, 2026
…iles. PP0 during the park prefill (py-spy, 60 s): 69 % GPU wait, 19 % radix walks of which half was the producer-phase census classifying every KEY of every walked node (4096 keys x ~25 nodes x 2 walks per chunk, quadratic in the prefix) -- sgl-project#1435: O(1) per node (first/last key classify the node, tokens credited in one lump, same fields). sgl-project#1436: the arena is registered once at bind in 1 GiB pieces (KV, draft, mamba; SGLANG_HICACHE_ARENA_PREPIN=0 keeps the lazy form) -- lazy pinning was 3.4 % of P's loop and sat on D's re-admission of every 100k prompt (~9 s each, serial); plus a read-side timing instrument (sgl-project#1436 ARENA-GET find/ref/resolve). sgl-project#1437: the after-part seam digest (~100 ms per tag, 8 tags on the wake's critical path) runs on a side thread and is joined before the verdict (SGLANG_WEG2_SEAM_DIGEST_ASYNC=0 keeps it inline). L3 fill made tolerant of backends without it. Tests: census 1061/631, 1175, seam 1418, arena 1427/1424/1433 green.
efschu pushed a commit to efschu/htsglang that referenced this pull request Sep 16, 2026
…l-project#1435 census walk), needles 6/6, but D still took ~9.5 s per 100k prompt, serial. py-spy on TP0 around the flip: 17 % presence probe (batch_exists_v2 stat()ing 100k stems on disk), 12 % draft page flags (a Python genexpr per page), 7 % blake2 of the same stems for find/ref/draft, 4 % lazy pinning -- the KV/draft pre-pin of sgl-project#1436 never ran (its anchor missed the sgl-project#1427 lines; only the mamba arena was pre-pinned). Now: KV/draft arenas pre-pinned at bind; presence answered from the arena (COMPLETE slot = present, one C call per width) before any disk stat; key128 cached; draft row checks and resolve_rows as tensor ops. Tests: arena 1427/1424/1433/0828, told 1400, 861, 931, 706 green.
efschu pushed a commit to efschu/htsglang that referenced this pull request Sep 16, 2026
…gl-project#1440 progress witness as a no-op (8 of 10 sockel-red cases pass again)
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.

6 participants