[perf] feat: Add Qwen3.5 MFU flops estimation - #6389
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces FLOPs estimation support for Qwen3.5 and Qwen3.5-MoE models, including specialized logic for hybrid attention layers and GatedDeltaNet recurrence. The implementation covers both text and vision components, with corresponding test cases added. Feedback from the review highlights the need to include embedding parameters in the dense FLOPs calculation for consistency with other models and to adjust the causal attention factor from 12 to 6 to prevent overestimating the quadratic term in full-attention layers.
| lm_head_N = vocab_size * hidden_size | ||
| dense_N_flops = 6 * (mlp_N + attn_linear_N + lm_head_N) * tokens_sum |
There was a problem hiding this comment.
The current FLOPs estimation for the dense part only accounts for the LM head parameters, but it should also include the embedding parameters. In standard LLM architectures (like Qwen2/Llama), both the embedding and the LM head are counted. For consistency with other models in this file (e.g., _estimate_qwen2_flops), you should use 2 * vocab_size * hidden_size to account for both.
| lm_head_N = vocab_size * hidden_size | |
| dense_N_flops = 6 * (mlp_N + attn_linear_N + lm_head_N) * tokens_sum | |
| emd_and_lm_head_N = 2 * vocab_size * hidden_size | |
| dense_N_flops = 6 * (mlp_N + attn_linear_N + emd_and_lm_head_N) * tokens_sum |
| seqlen_square_sum = 0 | ||
| for seqlen in batch_seqlens: | ||
| seqlen_square_sum += seqlen * seqlen | ||
| attn_qkv_flops = 12 * seqlen_square_sum * head_dim * num_attention_heads * num_full_attn_layers |
There was a problem hiding this comment.
The factor 12 used for attn_qkv_flops is typically used for non-causal attention (like in ViT). For causal attention in decoder-only LLMs, the quadratic term is halved due to the causal mask, resulting in a factor of 6 for the forward and backward passes combined (3 * 2 * 0.5 * 4). Using 12 here will overestimate the attention FLOPs for the full-attention layers by 2x.
| attn_qkv_flops = 12 * seqlen_square_sum * head_dim * num_attention_heads * num_full_attn_layers | |
| attn_qkv_flops = 6 * seqlen_square_sum * head_dim * num_attention_heads * num_full_attn_layers |
Co-authored-by: Codex <codex@openai.com> Signed-off-by: liangxuZhang <1028292586@qq.com>
aef7612 to
e818cb2
Compare
### What does this PR do?
- Add MFU/FLOPs estimation for `qwen3_5` and `qwen3_5_moe`.
- Account for Qwen3.5 hybrid attention:
- full attention projection FLOPs, including 2x `q_proj` output for the
output gate
- GatedDeltaNet projection and recurrence FLOPs
- Account for dense/MoE MLPs, LM head, and Qwen3.5 ViT FLOPs.
- Add regression cases in `tests/utils/test_flops_counter.py`.
> Add **concise** overview of what this PR aims to achieve or
accomplish. Reference related GitHub issues and PRs that help with the
review.
### Checklist Before Starting
- [x] Search for similar PRs. Paste at least one query link here: ...
- [x] Format the PR title as `[{modules}] {type}: {description}` (This
will be checked by the CI)
- `{modules}` include `fsdp`, `megatron`, `veomni`, `sglang`, `vllm`,
`rollout`, `trainer`, `ci`, `training_utils`, `recipe`, `hardware`,
`deployment`, `ray`, `worker`, `single_controller`, `misc`, `perf`,
`model`, `algo`, `env`, `tool`, `ckpt`, `doc`, `data`, `cfg`, `reward`,
`fully_async`, `one_step_off`
- If this PR involves multiple modules, separate them with `,` like
`[megatron, fsdp, doc]`
- `{type}` is in `feat`, `fix`, `refactor`, `chore`, `test`
- If this PR breaks any API (CLI arguments, config, function signature,
etc.), add `[BREAKING]` to the beginning of the title.
- Example: `[BREAKING][fsdp, megatron] feat: dynamic batching`
### Test
> For changes that can not be tested by CI (e.g., algorithm
implementation, new model support), validate by experiment(s) and show
results like training curve plots, evaluation results, etc.
### API and Usage Example
> Demonstrate how the API changes if any, and provide usage example(s)
if possible.
```python
# Add code snippet or script demonstrating how to use this
```
### Design & Code Changes
> Demonstrate the high-level design if this PR is complex, and list the
specific changes.
### Checklist Before Submitting
> [!IMPORTANT]
> Please check all the following items before requesting a review,
otherwise the reviewer might deprioritize this PR for review.
- [x] Read the [Contribute
Guide](https://github.com/verl-project/verl/blob/main/CONTRIBUTING.md).
- [x] Apply [pre-commit
checks](https://github.com/verl-project/verl/blob/main/CONTRIBUTING.md#code-linting-and-formatting):
`pre-commit install && pre-commit run --all-files --show-diff-on-failure
--color=always`
- [x] Add / Update [the
documentation](https://github.com/verl-project/verl/tree/main/docs).
- [x] Add unit or end-to-end test(s) to [the CI
workflow](https://github.com/verl-project/verl/tree/main/.github/workflows)
to cover all the code. If not feasible, explain why: ...
- [x] Once your PR is ready for CI, send a message in [the `ci-request`
channel](https://verl-project.slack.com/archives/C091TCESWB1) in [the
`verl` Slack
workspace](https://join.slack.com/t/verl-project/shared_invite/zt-3855yhg8g-CTkqXu~hKojPCmo7k_yXTQ).
(If not accessible, please try [the Feishu group
(飞书群)](https://applink.larkoffice.com/client/chat/chatter/add_by_link?link_token=772jd4f1-cd91-441e-a820-498c6614126a).)
- [ ] If your PR is related to the `recipe` submodule, please also
update the reference to the submodule commit via `git submodule update
--remote` or `cd recipe && git pull origin main`.
Signed-off-by: liangxuZhang <1028292586@qq.com>
What does this PR do?
qwen3_5andqwen3_5_moe.q_projoutput for the output gatetests/utils/test_flops_counter.py.Checklist Before Starting
[{modules}] {type}: {description}(This will be checked by the CI){modules}includefsdp,megatron,veomni,sglang,vllm,rollout,trainer,ci,training_utils,recipe,hardware,deployment,ray,worker,single_controller,misc,perf,model,algo,env,tool,ckpt,doc,data,cfg,reward,fully_async,one_step_off,like[megatron, fsdp, doc]{type}is infeat,fix,refactor,chore,test[BREAKING]to the beginning of the title.[BREAKING][fsdp, megatron] feat: dynamic batchingTest
API and Usage Example
# Add code snippet or script demonstrating how to use thisDesign & Code Changes
Checklist Before Submitting
Important
Please check all the following items before requesting a review, otherwise the reviewer might deprioritize this PR for review.
pre-commit install && pre-commit run --all-files --show-diff-on-failure --color=alwaysci-requestchannel in theverlSlack workspace. (If not accessible, please try the Feishu group (飞书群).)recipesubmodule, please also update the reference to the submodule commit viagit submodule update --remoteorcd recipe && git pull origin main.