Repository navigation
fix: respect explicit head_dim in Qwen3-VL FLOPs estimation - #426
Conversation
|
Thanks for contributing to Relax, @kkyyxhll! 感谢你为 Relax 做出贡献! Contribution guide / 贡献指南Describe the problem, your changes, and how you validated them. Keep each PR focused and run 请说明问题、改动和验证方式,保持 PR 聚焦,并在提交前运行 CI commands / CI 指令
Put one command on the first line of a new PR comment. Rerun/cancel require PR authorship or repository write access. 在新 PR 评论的首行写一条指令。PR 作者或有仓库写权限的贡献者可以重跑、取消 CI。 |
Nyanpasu 审查看板审查状态: ✅ 已通过 审查版本: 73aea79 目标分支: main 新一轮审查(force-push 后的 head 73aea79)已完成:作者按 P3 建议改为单行 getattr 写法,与被审内容逐字节差异仅此一处;21 项测试全部通过,CI 当前无失败。F1 已解决,无剩余问题,结论为通过。
审查发现待处理
已解决或已取代
提交范围 · 接收 2 · 建议移出 0 · 待确认 0接收 2 个文件 · 建议移出 0 个文件 · 待确认 0 个文件。移出与待确认部分暂停深审,不代表审查通过。
精简审查与验证依据
生产代码的必要性与替代方案
测试的必要性与替代方案
Powered by Nyanpasu with glm-5.3-flash max, please check the suggestions carefully.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
审查结论:修复正确(1 条非阻塞建议)
Qwen3-VL 在 text_config 显式设置 head_dim=128(4B:hidden_size 2560 / 32 heads;32B:5120 / 64 heads),旧推导 hidden_size // num_attention_heads 得到 80,会同时低估 q/k/v/o 投影与注意力分数 FLOPS(按新增测试两批形状独立复算,低估 8.6%–10.3%,与 PR 描述一致);MFU 以估算 FLOPs 为分子(relax/utils/training/train_metric_utils.py),因此报告的 MFU 同样被低估。本次改为读取 config.text_config.head_dim 并为无该属性的配置保留回退,与 _estimate_qwen3_vl_moe_flops 等同文件其余估算器一致;生产路径(relax/backends/megatron/actor.py 构建 FlopsCounter 供 log_perf_data 计算 MFU 指标)直接受益。已按 Qwen/Qwen3-VL-4B-Instruct 官方 config 逐字段核对测试输入,独立复算的 FLOPS 与测试期望值完全一致,pytest tests/utils/test_flops_counter.py 21 项全部通过。仅 1 条 P3 非阻塞精简建议,见行内评论。
Qwen3-VL sets an explicit head_dim (128) in its text_config that does not equal hidden_size // num_attention_heads: - Qwen3-VL-4B: 32 * 128 = 4096 != 2560 (hidden_size) - Qwen3-VL-32B: 64 * 128 = 8192 != 5120 (hidden_size) Deriving head_dim from hidden_size underestimates both the attention projection FLOPS and the 6N(D + seq_len * num_attn_heads * d_head * L) attention FLOPS term (~9-10% low for Qwen3-VL-4B). Read head_dim from config with the same fallback pattern already used by _estimate_qwen3_vl_moe_flops, and add a regression test using the exact Qwen3-VL-4B-Instruct config.
70cbc03 to
73aea79
Compare
rai-studio-bot
left a comment
There was a problem hiding this comment.
审查结论:通过
force-push 后的 head 73aea79 与上一轮已审范围的差异仅为按 P3 建议应用的写法调整(head_dim 改为单行 getattr(config.text_config, "head_dim", hidden_size // num_attention_heads),复用已有局部变量),测试文件逐字节未变。已在新 head 复跑 pytest tests/utils/test_flops_counter.py(21 项全部通过,含钉住绝对 FLOPS 值的 qwen3_vl 回归用例),修复正确性与必要性结论不变。此前唯一建议(F1)已解决,无剩余问题;CI 在当前 head 上暂无失败。
…er-profiler Brings in upstream/main 653c43c..dce533b (redai-studio#257, redai-studio#258, redai-studio#285, redai-studio#323, redai-studio#330, redai-studio#335, redai-studio#408, redai-studio#409, redai-studio#412, redai-studio#414, redai-studio#416, redai-studio#417, redai-studio#419-redai-studio#421, redai-studio#424, redai-studio#426, redai-studio#427, redai-studio#430, and 42 commits synced on 10-12 without PR numbers). The only conflict is relax/utils/env.py: cc4b052 adds the DeepSeek-V4 MXFP4 QAT variables at the same spot where this branch adds the straggler profiler variables. Resolved by keeping both blocks, straggler first.

What
修复
relax/utils/training/flops_counter.py中的_estimate_qwen3_vl_flops,使其直接从模型配置中读取head_dim,而不是通过hidden_size // num_attention_heads计算(该问题由上游 verl 引入)。同时,基于
Qwen/Qwen3-VL-4B-Instruct的完整官方配置,为qwen3_vl新增回归测试用例。Why
Qwen3-VL 在
text_config中显式指定了head_dim=128,该值不等于hidden_size // num_attention_heads:通过
hidden_size推导head_dim会导致以下两部分 FLOPs 被低估:q/k/v/o四个线性投影层。QKᵀ和AV的计算,原公式中的相关项为6 * seq_len² * num_attn_heads * d_head * num_full_attn_layers(等长序列还需乘以 batch size)。对于 Qwen3-VL-4B,旧公式计算得到
head_dim = 2560 // 32 = 80,而正确值应为128,导致总 FLOPs 估算值低估约 9%~10%。由于 MFU 的计算方式为:
因此,在设备峰值算力和实际训练时间不变的情况下,FLOPs 被低估也会导致报告的 MFU 偏低。
How
采用
_estimate_qwen3_vl_moe_flops(以及 Qwen2/Qwen3 系列 FLOPs 估算函数)中已有的实现方式:优先读取模型配置中显式定义的
head_dim;如果配置中没有该字段,则回退到hidden_size // num_attention_heads。这一回退机制保证了对未显式定义
head_dim的模型配置的向后兼容性。Testing
新增
qwen3_vl回归测试用例使用Qwen/Qwen3-VL-4B-Instruct的完整官方配置(显式head_dim=128),覆盖 batch shapes[512, 1024, 2048]与[4096, 4096, 4096],并携带 image tokens。pre-commit run --all-filespassespytest tests/utils/test_flops_counter.py)Type of Change
Screenshots / Logs
N/A