Skip to content

Validate MXFP8 fused QKV head alignment - #4677

Merged
anijain2305 merged 24 commits into
mainfrom
gh/anijain2305/22/head
Sep 15, 2026
Merged

anijain2305 merged 24 commits into
mainfrom
gh/anijain2305/22/head

Conversation

@anijain2305

@anijain2305 anijain2305 commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Require fused QKV head_dim to align with the 32-row MXFP8 weight block so a scale tile cannot span Q, K, or V.

[ghstack-poisoned]
[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Sep 14, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 14, 2026
[ghstack-poisoned]
[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Sep 14, 2026
[ghstack-poisoned]
[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Sep 14, 2026
[ghstack-poisoned]
[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Sep 14, 2026
[ghstack-poisoned]
[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Sep 14, 2026
anijain2305 added a commit that referenced this pull request Sep 14, 2026
anijain2305 added a commit that referenced this pull request Sep 14, 2026
@anijain2305
anijain2305 marked this pull request as ready for review September 14, 2026 19:15
anijain2305 added a commit that referenced this pull request Sep 14, 2026
Extend StackedLinear to fused QKV with weight layout [R, num_kv_heads * head_dim, model_dim], where R = num_query_heads / num_kv_heads + 2. The first R - 2 slices contain the query heads, followed by one slice for all key heads and one for all value heads.

ghstack-source-id: 2381544
Pull-Request: #4677
Comment on lines +248 to +251
wqkv=StackedLinear.Config(
in_features=dim,
out_features=(n_heads + 2 * n_kv) * per_head_dim,
out_features=n_kv * per_head_dim,
num_linears=n_heads // n_kv + 2,

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.

Reasoning about the motivation of this change:

TP usually shards on num_heads dimension, so it's different from MLP linears where we need an extra dimension to denote TP.

For quantization, I don't believe it's popular to do quantization on wqkv.

For lora, it's less clear to me. But our earlier argument of lora probably don't care much if they are fused or not anyway.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

One correction: we do quantize wqkv today, but you are right that quantization does not motivate this layout change.

Since q, k, v are stored in interleaved fashion, I thought that they suffer from the same quantization problem as we saw in w13.

But here the layout, even though interleaved, is still ok for quantization.

For heads_per_kv = 4, the old flattened order is:

Q0 Q1 Q2 Q3 K0 V0 | Q4 Q5 Q6 Q7 K1 V1 | ...

where each element (like Q0) is itself head_dim rows. Since head_dim is typically a multiple of 32, we are unlikely to have overlapping regions. This is quite different from w13 where 32 rows had 16 rows of w1 and 16 rows of w3.

That said, there are 2 options

  • Still keep the PR because it makes it consistent with w13 linear.
  • Remove this PR and just add a check for MxFP8linear that the head_dim is a multiple of 32.

I think both of us prefer the option 2, but looking for opinions.

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.

yes I prefer option 2 as well -- hoping to keep things as minimal as possible until changes are necessary

anijain2305 added a commit that referenced this pull request Sep 14, 2026
Extend StackedLinear to fused QKV with weight layout [R, num_kv_heads * head_dim, model_dim], where R = num_query_heads / num_kv_heads + 2. The first R - 2 slices contain the query heads, followed by one slice for all key heads and one for all value heads.

ghstack-source-id: 2381544
Pull-Request: #4677
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
"debugmodel": (_debugmodel, 131072),
# Preserve the debug model's dimensions and QKV GEMM shape, but use
# 32-wide heads so MXFP8 weight-scale tiles align with head boundaries.
"debugmodel_mxfp8": (partial(_debugmodel, n_heads=8), 131072),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@tianyu-l I had to change the debug model to have head_dim as 32. Otherwise, the assertion was firing. I checked the head_dims of a few OSS models - all of them exceed way beyond 32, so our debug model was not just enough.

I thought of changing the debugmodel but that seems like a big change as we have large number of tests on it.

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@anijain2305 anijain2305 added ciflow/8gpu ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/real-pg Run the full 8-GPU Real PG integration suite labels Sep 15, 2026
@pytorch-bot

pytorch-bot Bot commented Sep 15, 2026

Copy link
Copy Markdown

Warning: Unknown label ciflow/8gpu.
Currently recognized labels are

  • ciflow/b200
  • ciflow/h100.8
  • ciflow/rl

Please add the new label to .github/pytorch-probot.yml

@pytorch-bot

pytorch-bot Bot commented Sep 15, 2026

Copy link
Copy Markdown

Warning: Unknown label ciflow/fake-pg.
Currently recognized labels are

  • ciflow/b200
  • ciflow/h100.8
  • ciflow/rl

Please add the new label to .github/pytorch-probot.yml

@pytorch-bot

pytorch-bot Bot commented Sep 15, 2026

Copy link
Copy Markdown

Warning: Unknown label ciflow/real-pg.
Currently recognized labels are

  • ciflow/b200
  • ciflow/h100.8
  • ciflow/rl

Please add the new label to .github/pytorch-probot.yml

@anijain2305
anijain2305 changed the base branch from gh/anijain2305/22/base to main September 15, 2026 19:59
[ghstack-poisoned]
[ghstack-poisoned]
@anijain2305
anijain2305 changed the base branch from main to gh/anijain2305/22/base September 15, 2026 20:02
@anijain2305
anijain2305 changed the base branch from gh/anijain2305/22/base to main September 15, 2026 20:03
@anijain2305
anijain2305 merged commit 1f29de0 into main Sep 15, 2026
26 of 27 checks passed
submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Sep 15, 2026
Require fused QKV head_dim to align with the 32-row MXFP8 weight block so a scale tile cannot span Q, K, or V.

Pull-Request: pytorch/torchtitan#4677
ghstack-source-id: c68dc93
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/real-pg Run the full 8-GPU Real PG integration suite ciflow/rl 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