Skip to content

[FLOPs] Fix attention FLOPs estimate - #1923

Merged
shuhuayu merged 5 commits into
pytorch:mainfrom
shuhuayu:modeldev
Oct 29, 2025
Merged

shuhuayu merged 5 commits into
pytorch:mainfrom
shuhuayu:modeldev

Conversation

@shuhuayu

Copy link
Copy Markdown
Contributor

Fix the FLOPs estimate in non-parameterized attention matmuls. This part estimate of FLOPs is customized for each model due to different Attention mechanisms.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 21, 2025
Comment thread torchtitan/models/utils.py Resolved
Comment thread torchtitan/models/utils.py Resolved
Comment thread torchtitan/models/utils.py Resolved
Comment thread torchtitan/models/utils.py Resolved
Comment thread torchtitan/models/utils.py Resolved
Comment thread torchtitan/models/llama3/model/args.py Resolved

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

LGTM!

Comment thread torchtitan/models/utils.py Resolved
Comment thread torchtitan/models/utils.py Outdated
Comment on lines 386 to 389
model_args.n_layers,
model_args.n_heads,
model_args.dim // model_args.n_heads,
head_dims,
seq_len,

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.

Do you think it's better if we set n_layers and n_heads as args to this function? I feel it might be more clear as a pure util function. E.g. a model can define number of layers using model_args.num_layers or model_args.L but still be able to use this util function.

We can even consider putting it to tools/utils.py instead of model/utils.py.

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.

I agree. Do you think we can also move the following part to args.py so get_moe_model_nparams_and_flops does not need to take model_args as input, and thus become model_args agnostic?

if hasattr(model_args, "enable_weight_tying") and model_args.enable_weight_tying:
nparams = nparams - nparams_embedding

For your last point, I think these two functions are still more model relevant than those functions currently in tools/utils.py.

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.

oh, it seems the moe part has top_k and num_experts in model_args as well. Yeah NVM we can keep model_args as input arg for now.

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.

one minor thing is that we might need n_layers , and n_heads, and head_dim, and dim as the function args. This is because in Qwen3, the head_dim != dim / n_heads

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.

Thanks for the suggestions! Code updated accordingly.

Comment thread torchtitan/models/utils.py Outdated
)

l, h, q, t = (
l, h, qv, t = (

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.

It might be better to be more specific on the naming n_layers n_heads, etc. especially for qv which looks a bit confusing.

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

Had one more comments. O/w lgtm.

Comment thread torchtitan/models/utils.py Outdated
Comment on lines +366 to +367
n_layers: int,
n_heads: int,

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.

oh sorry I didn't make myself clear -- I think if we need to pass in model_args anyway, we shouldn't pass in n_layers, n_heads redundantly.

@shuhuayu
shuhuayu merged commit 5a090f2 into pytorch:main Oct 29, 2025
jquesnelle pushed a commit to NousResearch/torchtitan that referenced this pull request Nov 10, 2025
Fix the FLOPs estimate in non-parameterized attention matmuls. This part estimate of FLOPs is customized for each model due to different Attention mechanisms.
xrsrke pushed a commit to NousResearch/torchtitan that referenced this pull request Feb 13, 2026
Fix the FLOPs estimate in non-parameterized attention matmuls. This part estimate of FLOPs is customized for each model due to different Attention mechanisms.
xrsrke pushed a commit to NousResearch/torchtitan that referenced this pull request Feb 25, 2026
Fix the FLOPs estimate in non-parameterized attention matmuls. This part estimate of FLOPs is customized for each model due to different Attention mechanisms.
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.

Potentially incorrect attention flop calculation due to wrong head_dim?

3 participants