Skip to content

fix sdpa-varlen attention mismatch in qwen3 - #2229

Merged
tianyu-l merged 4 commits into
pytorch:mainfrom
francesco-bertolotti:f14-qwen-attn-fix
Jan 19, 2026
Merged

tianyu-l merged 4 commits into
pytorch:mainfrom
francesco-bertolotti:f14-qwen-attn-fix

Conversation

@francesco-bertolotti

Copy link
Copy Markdown
Collaborator

Fix for #2223

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jan 14, 2026

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

thanks for fixing this :)) do you mind:

  1. adding the same fix for the llama 3 model https://github.com/pytorch/torchtitan/blob/main/torchtitan/models/llama3/model/model.py#L299-L301
  2. can you verify that loss is on par for both models with flex (ss of graphs in the PR summary)? you can run smth like CONFIG_FILE="./torchtitan/models/qwen3/train_configs/qwen3_1.7b.toml" ./run_train.sh and set enable_tensorboard = True here

@francesco-bertolotti

Copy link
Copy Markdown
Collaborator Author

@liangel-02 no problem at all. Thank you for your work on this repo.

I should be able to commit something tomorrow.

@francesco-bertolotti

francesco-bertolotti commented Jan 15, 2026 •

Copy link
Copy Markdown
Collaborator Author

Hello @liangel-02,

I’ve applied the same fix to the LLaMA-3 model.

I also added a test to verify that the forward pass of LLaMA-3 attention behaves identically for the flex and varlen backends.

Additionally, I ran ablation experiments using sub-1B models for 300 steps. The results appear to be close to the expected behavior, as shown below:

Screenshot 2026-01-15 at 10 48 20 Screenshot 2026-01-15 at 10 47 45

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

Comment thread tests/unit_tests/test_gqa.py Outdated
hidden_dim = 512,
attn_type = "sdpa"
),
).to(device="cuda:0", dtype=dtype)

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.

btw: our CI cannot run unit test on GPU, it's CPU only right now.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I remove those tests? I can also do something like if no gpu available: return

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.

If it's not guarded by CI, it's better to remove them. In the future we should make torchtitan test more flexible, but for now showing results before vs. after the change in PR summary should be good.

@francesco-bertolotti

Copy link
Copy Markdown
Collaborator Author

New runs were performed with the seed set to 42 and deterministic mode enabled. Small differences remain, but they are now significantly more contained.

Screenshot 2026-01-15 at 12 48 24 Screenshot 2026-01-15 at 12 51 00

@liangel-02

liangel-02 commented Jan 15, 2026 •

Copy link
Copy Markdown
Contributor

New runs were performed with the seed set to 42 and deterministic mode enabled. Small differences remain, but they are now significantly more contained.

Screenshot 2026-01-15 at 12 48 24

@francesco-bertolotti
the qwen3 losses lgtm, but i think the llama ones are still a bit off esp after ~75 steps. for reference this is what the loss curve currently looks like. i think we should at least understand why the varlen loss performs better.

Screenshot 2026-01-15 at 10 57 09 AM

cc @drisspg

@francesco-bertolotti

Copy link
Copy Markdown
Collaborator Author

Hi @liangel-02,
I believe that, for some reason, smoothing was messing with the visualization. I have re-run the llama varlen vs fles baselines. Here's what we are at:

flex-varlen-llama

@tianyu-l tianyu-l linked an issue Jan 19, 2026 that may be closed by this pull request
@tianyu-l
tianyu-l merged commit 09c6d74 into pytorch:main Jan 19, 2026
tf170898 pushed a commit to tf170898/torchtitan that referenced this pull request Jan 21, 2026
wwwjn pushed a commit to wwwjn/torchtitan that referenced this pull request Jan 30, 2026
xrsrke pushed a commit to NousResearch/torchtitan that referenced this pull request Feb 13, 2026
TXacs pushed a commit to McmillanTAC/torchtitan that referenced this pull request Apr 13, 2026
ACharacterInASimulation pushed a commit to ACharacterInASimulation/torchtitan that referenced this pull request Apr 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect transpose in Qwen3 varlen attention backend

3 participants