Conversation
…nce parallelism Two bugs in the Qwen2 / Qwen3 pipeline forward when pipeline parallelism is combined with sequence parallelism: 1. On stages after the first, the forward does `seq_length *= sp_size`, but `seq_length_with_past` was computed before and not updated, so the mask is built as (B, 1, S, S / sp_size). With flash attention on and ColoAttention falling back to the SDPA kernel (no flash_attn), this fails with "The expanded size of the tensor (24) must match the existing size (12)". 2. With sequence parallelism the policy replaces the attention forward, whose non-flash path needs an explicit 4d causal mask and skips masking on None. The pipeline forward however built the mask for the transformers attention implementation; for sdpa and an all-ones padding mask that is None, so the attention silently became bidirectional. Recompute `seq_length_with_past` after scaling `seq_length` (as the llama forward does), and always build the 4d causal mask when sequence parallelism is on without flash attention, which is exactly when the attention forward is replaced. Add a pp 2 + sp 2 (all_to_all) config without flash attention to the qwen2 and qwen3 tests.
11 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📌 Checklist before creating the PR
[doc/gemini/tensor/...]: A concise descriptionpip install pre-commit && pre-commit install(ran the pinned hooks manually instead: black 24.10.0--line-length=120, isort 5.13.2--profile black, autoflake 2.3.1, all clean)🚨 Issue number
No existing issue; described below.
📝 What does this PR do?
Fixes two bugs in the pipeline forward of Qwen2 and Qwen3 (
Qwen2PipelineForwards.qwen2_model_forward,Qwen3PipelineForwards.qwen3_model_forward, identical code) when pipeline parallelism is combined with sequence parallelism:1. Wrong key length of the attention mask on later stages. For stages after the first, the sequence is split along the SP group, so the forward does
seq_length *= sp_size— butseq_length_with_pastwas computed before that and is not updated. The mask is built as(B, 1, S, S / sp_size):This happens with
enable_flash_attention=Truewhenever ColoAttention falls back to the SDPA kernel (noflash_attninstalled); the Dao kernel path ignores the mask shape, which is probably why CI did not catch it. The existing 4-GPU config "Ulysess + Flash attention" intest_shard_qwen3.pyfails this way on main withoutflash_attn(I ran it as is); Qwen2 has the same code and fails the same way with the equivalent config (table below).2. Causal mask dropped without flash attention (silently wrong results). With SP the policy replaces the attention forward (
get_qwen{2,3}_flash_attention_forward), whose non-flash branch needs an explicit 4d causal mask and skips masking when it getsNone. The pipeline forward however builds the mask for the transformers attention implementation; forsdpa(the default) and an all-ones padding mask,_prepare_4d_causal_attention_mask_for_sdpareturnsNone, so attention becomes bidirectional. Training runs, but the outputs are wrong.Fix.
seq_length_with_pastafterseq_length *= sp_size(as the llama forward does).Test. Added a "Ulysses without flash attention" config (
tp 1, pp 2, sp 2, all_to_all, enable_flash_attention=False) torun_qwen2_testandrun_qwen3_test.Results (4x A800, torch 2.5.1+cu124, transformers 4.51.3, no
flash_attn; the repo's owncheck_forward_backward, which compares loss, output and grads with the unsharded model)[.., 24, 12]vs[.., 24, 24]test_shard_qwen3.py::test_qwen3(all 4-GPU configs, incl. the new one)test_shard_qwen2.py::test_qwen2(all 4-GPU configs, incl. the new one)Not in this PR
LLaMA with the same config (pp 2 + sp 2, all_to_all, flash attention off) also fails, but differently (
The size of tensor a (12) must match the size of tensor b (24)atllama.py:143), so it is a separate issue; I have not changed LLaMA here.💥 Checklist before requesting a review
⭐️ Do you enjoy contributing to Colossal-AI?