Skip to content

align shape suffix between attn and linear attn - #4390

Open
acisseJZhong wants to merge 1 commit into
mainfrom
linearattncp
Open

align shape suffix between attn and linear attn#4390
acisseJZhong wants to merge 1 commit into
mainfrom
linearattncp

Conversation

@acisseJZhong

@acisseJZhong acisseJZhong commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

PR title

Align attention shape suffixes across TorchTitan

Summary

  • Standardize attention tensor suffixes:
    • H: attention heads
    • K: query/key head dimension
    • V: value head dimension
    • C: projection channels
    • L: sequence length
  • Add back shape suffix doc string to gdn and kda.

Stack created with GitHub Stacks CLIGive Feedback 💬

@pytorch-bot pytorch-bot Bot added the ciflow/rl label Sep 1, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 1, 2026

# Shape suffixes:
# B = singleton kernel batch, T = packed tokens, H = attention heads,
# D = projection rows per head, I = input features.

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.

what is I, compared with D and F, in attention.py, and in
https://github.com/pytorch/torchtitan/blob/main/torchtitan/models/common/moe.py#L27

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.

D and F refers to model dim and FFN hidden dim. I is input feature dim/input width.
Here in this file
Image

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.

couldn't understand, you have two I's in side the same model, how could it be used as identifier?

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.

_scale_mla_heads

heads_NDI = weight.view(num_heads, rows_per_head, weight.shape[1])
is called on q_projection and kv_projection twice, whose input dim are different.

scales_N11 = _replicated_scales(scales_N, weight).view(-1, 1, 1)
heads_NDI = weight.view(num_heads, rows_per_head, weight.shape[1])
heads_NDI[:, :nope_rows_per_head].mul_(scales_N11.pow(nope_scale_exponent))
scales_H11 = _replicated_scales(scales_H, weight).view(-1, 1, 1)

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.

why 1 here, but B = singleton kernel batch

@acisseJZhong acisseJZhong Sep 1, 2026

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.

the 1 here is not batch dim. It's from .view(-1, 1, 1), to multiply with heads_HDI.

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.

what is "singleton kernel batch" and is it not always 1?

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.

B is always 1 here since we removed batch dim, it is needed only because kda still has the batch dim. #4164 (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.

@drisspg
I saw KDA kernels accepting inputs with both batch (B) and tokens (T). Curious that if the inputs are restricted to only have T dim, would it be further optimized?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/rl 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