Skip to content

Potentially incorrect attention flop calculation due to wrong head_dim? #1920

Closed

Description

@gau-nernst

Bug description

l, h, q, t = (
model_args.n_layers,
model_args.n_heads,
model_args.dim // model_args.n_heads,
seq_len,
)

However, head_dim is not necessarily equal to dim / n_heads

e.g. Qwen3-4B, dim=2560, n_heads=32, head_dim=128

Versions

latest main

Activity

  1. added theissue type on Oct 19, 2025
  2. tianyu-l commented on Oct 19, 2025

    @tianyu-l
    Contributor

    It seems we need to revisit the formula of attention flops computation, especially for Qwen3 and DeepSeek V3.

    Maybe we can separate num params computation from total flops calculation -- the former can be shared across models, the latter may need to be customized for each model due to different attention details.

    Overall I think attention part may not matter much in flop computation due to the its scale compared with the matmul part.

    cc @shuhuayu @wwwjn

  3. gau-nernst commented on Oct 20, 2025

    @gau-nernst
    Author

    Also just noticed that, due to causal attention, shouldn't the attention FLOPs be further divided by 2?

  4. tianyu-l commented on Oct 20, 2025

    @tianyu-l
    Contributor

    Also just noticed that, due to causal attention, shouldn't the attention FLOPs be further divided by 2?

    See

    # 4. we follow the convention and do not account for sparsity in causal attention

  5. self-assigned this
    on Oct 20, 2025
  6. shuhuayu commented on Oct 21, 2025

    @shuhuayu
    Contributor

    Proposed a fix in this pr: #1923. Following the previously mentioned convention, the sparsity introduced by fixed_block_size (attention window) in local layers of Llama 4 models is also ignored.

  7. linked a pull request that will close this issue[FLOPs] Fix attention FLOPs estimate #1923on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions