Skip to content

[flux] Count both attention matmuls in the FLOPs estimate - #4366

Merged
tianyu-l merged 2 commits into
pytorch:mainfrom
alanhuangyoo:fix/flux-attention-flops
Aug 30, 2026
Merged

tianyu-l merged 2 commits into
pytorch:mainfrom
alanhuangyoo:fix/flux-attention-flops

Conversation

@alanhuangyoo

Copy link
Copy Markdown
Contributor

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):

return 6 * num_heads * (qk_head_dim + v_head_dim) * attended_tokens

flux was the one model left on an inline formula:

# Add non-parameterized self-attention FLOPs (QK^T and attn*V).
# Per PaLM convention: 6 * hidden_size * seq_len per token per
# layer (covers 2 matmuls × fwd+bwd × multiply-add).
num_flops_per_token += (
    6 * sb_h * seq_len * self.depth_single_blocks
    + 6 * db_h * seq_len * self.depth
)

6 * hidden_size is 6 * 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:381 and then metrics.py:493:

mfu = 100 * self.num_flops_per_token * tps / self.gpu_peak_flops

so the mfu: line in a flux training log reads low.

flavor attention FLOPs/token (inline vs shared) reported MFU understated by
flux-dev (hidden 3072, 24 heads, 19 double + 38 single) 4,303,355,904 vs 8,606,711,808 — 2.00× 9.9%
flux-debug (hidden 1536, 12 heads, 2 double + 2 single) 150,994,944 vs 301,989,888 — 2.00× 16.6%

(at seq_len 4096)

Test Plan

pytest tests/unit_tests/cpu/test_flux_flops.py

The new test rebuilds flops_per_token from 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 what get_nparams_and_flops returns.

Test Result

On main the test fails, which is the defect:

E       AssertionError: 776752512 != 795626880
tests/unit_tests/cpu/test_flux_flops.py:69: AssertionError

With the fix:

tests/unit_tests/cpu/test_flux_flops.py .                          [100%]
1 passed

get_nparams_and_flops before → after, at seq_len 4096:

flux-debug    908,098,944 -> 1,059,093,888
flux-dev   43,266,767,232 -> 47,570,123,136

Regression over the neighbouring CPU tests (test_flux_flops, test_dataset_flux, test_weight_tying, test_module, test_train_spec):

46 passed, 3 subtests passed

ufmt check clean on both files with the pinned ufmt==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.

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.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 28, 2026
@tianyu-l
tianyu-l requested a review from jinsooihm August 28, 2026 07:30

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

Thank you! I didn't realize that flux has this issue.

One comment: I am not sure how useful this new test file would be. What do you think @tianyu-l ?

Comment thread tests/unit_tests/cpu/test_flux_flops.py Outdated

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 comment: I am not sure how useful this new test file would be.

Can we remove this test?

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.

Removed. The FLOPs change in model.py stands on its own.

Removed at review request; the model change stands on its own.
@tianyu-l
tianyu-l merged commit 1a89db0 into pytorch:main Aug 30, 2026
18 checks passed
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.

3 participants