Repository navigation
[flux] Count both attention matmuls in the FLOPs estimate - #4366
Merged
Merged
Conversation
pytorch#4300 moved every model onto quadratic_attention_flops_per_token, which carries the two attention contractions in (qk_head_dim + v_head_dim): 6 * num_heads * (qk_head_dim + v_head_dim) * attended_tokens flux was the one model left on an inline formula, and that formula uses 6 * hidden_size * seq_len -- one head_dim, so only one of the two matmuls. Its own comment says it covers "2 matmuls", but it counts one. The attention term is exactly half what every other model reports for the same computation, which flows through trainer.py into metrics.py's mfu = 100 * num_flops_per_token * tps / gpu_peak_flops. Reported MFU comes out 9.9% low on flux-dev and 16.6% low on flux-debug at seq_len 4096. Switch to the shared helper. Adds a test that rebuilds flops_per_token from the terms the docstring names; it fails on main with 776752512 != 795626880.
alanhuangyoo
requested review from
fegin,
tianyu-l,
wconstab and
wwwjn
as code owners
August 28, 2026 06:22
tianyu-l
reviewed
Aug 28, 2026
Contributor
There was a problem hiding this comment.
One comment: I am not sure how useful this new test file would be.
Can we remove this test?
Contributor
Author
There was a problem hiding this comment.
Removed. The FLOPs change in model.py stands on its own.
Removed at review request; the model change stands on its own.
tianyu-l
approved these changes
Aug 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Purpose
#4300 moved every model onto the shared
quadratic_attention_flops_per_token, which carries the two attention contractions in(qk_head_dim + v_head_dim):flux was the one model left on an inline formula:
6 * hidden_sizeis6 * num_heads * head_dim— one head_dim, so one of the two matmuls. The comment says it covers "2 matmuls"; the expression covers one. The factor of 6 already accounts for fwd+bwd and multiply-add, which is why the shared helper needs the sum of the two head dims on top of it.So flux's attention term is exactly half of what every other model reports for the same computation. That value reaches
trainer.py:381and thenmetrics.py:493:so the
mfu:line in a flux training log reads low.flux-dev(hidden 3072, 24 heads, 19 double + 38 single)flux-debug(hidden 1536, 12 heads, 2 double + 2 single)(at
seq_len4096)Test Plan
The new test rebuilds
flops_per_tokenfrom the terms the docstring names —6 * nparams, minus one side of each symmetric double-stream block, minus the per-sample modulation amortization, plus attention from the shared helper — and compares it to whatget_nparams_and_flopsreturns.Test Result
On
mainthe test fails, which is the defect:With the fix:
get_nparams_and_flopsbefore → after, atseq_len4096:Regression over the neighbouring CPU tests (
test_flux_flops,test_dataset_flux,test_weight_tying,test_module,test_train_spec):ufmt checkclean on both files with the pinnedufmt==2.3.0 / black==22.12.0 / usort==1.0.5.This does not touch the sequence length used for the double-stream blocks, which attend over the concatenated image and text streams — that is a separate question from the factor of two, and I left it alone.