Conversation
…flash attention With sequence parallelism the policy replaces the llama attention forward, whose non-flash path adds the attention mask only if it is not None. The model forward built that mask with transformers' `_update_causal_mask`, which returns None for sdpa (the default) in training without padding, relying on sdpa's is_causal. The attention was therefore bidirectional: training ran, but outputs and gradients were wrong (checked for all_to_all and split_gather). With pipeline parallelism on top, later stages passed the local part of the sequence to `_update_causal_mask` together with the full-length cache_position, which failed with "The size of tensor a (12) must match the size of tensor b (24)". When sequence parallelism is on without flash attention, which is exactly when the attention forward is replaced, build the 4d causal mask over the full sequence with `_prepare_4d_causal_attention_mask`. Add Ulysses, Ulysses + PP and TP + split_gather configs without flash attention to the llama test; the existing sequence parallel configs all enable flash attention.
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. Same class of bug as #6451 (Qwen2/Qwen3), in the LLaMA forward.
📝 What does this PR do?
With sequence parallelism enabled and flash attention disabled, LLaMA trains with the wrong attention:
1. Causal mask silently dropped (bidirectional attention). With SP the policy replaces the attention forward (
get_llama_flash_attention_forward); its non-flash branch adds the mask only if it is notNone.llama_model_forwardbuilds that mask with transformers'_update_causal_mask, which forsdpa(the default attention implementation), in training and without padding returnsNone— it relies on SDPA'sis_causalinstead:So every token attends to the whole sequence. Training runs without errors; the outputs and gradients are wrong. This is the common pretraining setup (packed samples, no padding, default
sdpa), for every SP mode that goes through this path (checkedall_to_allandsplit_gather).2. Crash with pipeline + sequence parallelism. On stages after the first,
hidden_statesonly holds the local part of the sequence whilecache_positioncovers the full sequence, so_update_causal_maskfails:Fix. When SP is enabled (and flash attention is not) — exactly when the policy replaces the attention forward — build the 4d causal mask over the full sequence with
_prepare_4d_causal_attention_mask. Without SP the transformers attention is used and nothing changes. All SP modes compute attention over the full sequence (q_len *= sp_sizefor the modes sharing the TP group, all-to-all for Ulysses), so the(B, 1, S, S)mask fits all of them.The existing LLaMA SP configs all enable flash attention (explicitly or via
enable_all_optimization), which is why the tests did not catch this. Added three 4-GPU configs without flash attention: Ulysses, Ulysses + PP, TP + SPsplit_gather.Results (4x H200, torch 2.5.1+cu124, transformers 4.51.3; the repo's own
check_forward_backward, which compares loss, outputs and grads with the unsharded model)llama.py:143)run_llama_test(incl. the 3 new ones), run one by onering_attnconfigs were skipped because they needflash_attn, which I cannot install on my machine💥 Checklist before requesting a review
⭐️ Do you enjoy contributing to Colossal-AI?