Skip to content

Fused QKV GQAttention implementation - #2878

Merged
wwwjn merged 12 commits into
mainfrom
fused-qkv
Apr 16, 2026
Merged

wwwjn merged 12 commits into
mainfrom
fused-qkv

Conversation

@wwwjn

@wwwjn wwwjn commented Apr 7, 2026 •

Copy link
Copy Markdown
Contributor

Implementation

Replaces separate wq, wk, wv with single wqkv linear of shape [n_kv_heads * R * head_dim, dim], where R = heads_per_kv + 2. Fused q/k/v project to reduce number of matmul kernel launches, and also reduce the number of TP communication kernel launches.

Model-wise:

  • DeepSeek-v3 is untouched as the Attention has 2 different computation path (depend on q_lora_rank), makes it not easy to interact with fusing decision.
  • Flux is also untouched

Test

using llama3 debug model:

  • Numerics test: When initialized using the same seed checkpoint:
Screenshot 2026-04-07 at 6 02 38 PM
  • Performance test fused v.s. unfused:

    • CPU bounded model: Screenshot 2026-04-07 at 6 04 39 PM
    • GPU (memory-bandwidth bounded, or GPU compute bounded): Screenshot 2026-04-07 at 7 22 47 PM
  • Test with DCP + state_dict_adapter:

Screenshot 2026-04-09 at 2 19 35 PM

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Apr 7, 2026
@wwwjn
wwwjn marked this pull request as ready for review April 8, 2026 04:13

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

looks reasonable, had some suggestions

Comment thread torchtitan/models/common/attention.py Resolved
Comment thread torchtitan/models/common/attention.py Resolved

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.

are these changes tested with TP applied on DCP side?

Comment thread torchtitan/models/llama3/parallelize.py Resolved
Comment thread torchtitan/models/common/attention.py Resolved
Comment thread torchtitan/models/common/attention.py Resolved
Comment thread torchtitan/models/common/attention.py Resolved
Comment thread torchtitan/models/qwen3/__init__.py Resolved

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

I didn't check state dict adapter code.

Comment thread tests/integration_tests/models.py Resolved
@wwwjn
wwwjn force-pushed the fused-qkv branch 2 times, most recently from 18997fd to ba6a29d Compare April 14, 2026 18:19
@wwwjn
wwwjn merged commit ad7a865 into main Apr 16, 2026
22 of 33 checks passed
saforem2 added a commit to saforem2/torchtitan that referenced this pull request Apr 18, 2026
Replay upstream changes from PRs pytorch#2878 (fused QKV) and pytorch#2842/pytorch#2960
(attention backend abstraction) onto the ezpz agpt experiment:

- Replace inner_attention/mask_type params with attn_backend string
  and fuse_qkv bool in _build_agpt_layers and _build_agpt_config
- Add _ezpz_get_attention_config() that overrides the sdpa case with
  XPU-aware EzpzScaledDotProductAttention, delegates others to upstream
- Remove deepcopy-based _apply_flex_attn/_apply_varlen_attn overlays;
  configs now specify attn_backend directly
- Add FusedQKVLinear detection and branching TP plan in parallelize.py
- Pass fuse_qkv through to make_gqa_config()
- Preserve all XPU customizations (SDPA workaround, Megatron init,
  capture_scalar_outputs fix)
SherlockNoMad added a commit that referenced this pull request Apr 20, 2026
…3031)

## Summary

- Updates hardcoded expected hashes in `test_bitwise_deterministic.py`
for `TestLlama3BitwiseDeterministic` and
`TestLlama3FlexAttnBitwiseDeterministic`

## Root Cause

`git bisect` identified **#2878** (`ad7a8653` — "Fused QKV GQAttention
implementation") as the commit that broke these tests. That PR replaced
separate `wq`/`wk`/`wv` projections with a single fused `wqkv` linear in
`GQAttention`, which changed the parameter layout and therefore the
deterministic model/gradient hashes.

**Verified:**
- Parent commit `aa0214b8` (before #2878): both tests pass
- Commit `ad7a8653` (the PR itself): both tests fail

**Loss values are unchanged** — only the model and gradient hashes
differ, confirming the fused QKV change altered parameter layout but not
numerics.

## Test plan

- [x] `pytest
torchtitan/experiments/graph_trainer/tests/test_bitwise_deterministic.py::TestLlama3BitwiseDeterministic::test_eager_self_deterministic`
passes
- [x] `pytest
torchtitan/experiments/graph_trainer/tests/test_bitwise_deterministic.py::TestLlama3FlexAttnBitwiseDeterministic::test_eager_self_deterministic`
passes
wwwjn added a commit that referenced this pull request Apr 25, 2026
Port the fused QKV (FusedQKVLinear) handling from Qwen3's adapter to
Qwen3-VL. When fuse_qkv is enabled, to_hf() splits wqkv into separate
q/k/v projections, and from_hf() fuses them back. This is needed for PR
#2878 which switches Qwen3-VL to use FusedQKVLinear.

## Test
Load Qwen3-VL-2B-Instruct weight into a fusedQKV Qwen3-VL model
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/8gpu 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.

2 participants