Skip to content

fix: respect explicit head_dim in Qwen3-VL FLOPs estimation - #426

Merged
SigureMo merged 1 commit into
redai-studio:mainfrom
kkyyxhll:bugfix/qwen3vl_flops_cal
Oct 11, 2026
Merged

SigureMo merged 1 commit into
redai-studio:mainfrom
kkyyxhll:bugfix/qwen3vl_flops_cal

Conversation

@kkyyxhll

@kkyyxhll kkyyxhll commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

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 num_attention_heads head_dim heads × head_dim
Qwen3-VL-4B 2560 32 128 4096 ≠ 2560
Qwen3-VL-32B 5120 64 128 8192 ≠ 5120

通过 hidden_size 推导 head_dim 会导致以下两部分 FLOPs 被低估:

  1. Attention 线性投影 FLOPs:包括 q/k/v/o 四个线性投影层。
  2. Attention 核心计算 FLOPs:对应 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 的计算方式为:

MFU = estimated_FLOPs / (peak_FLOPs * step_time * num_gpus)

因此,在设备峰值算力和实际训练时间不变的情况下,FLOPs 被低估也会导致报告的 MFU 偏低。

How

采用 _estimate_qwen3_vl_moe_flops(以及 Qwen2/Qwen3 系列 FLOPs 估算函数)中已有的实现方式:

head_dim = getattr(
    config.text_config, "head_dim", config.text_config.hidden_size // config.text_config.num_attention_heads
)

优先读取模型配置中显式定义的 head_dim;如果配置中没有该字段,则回退到 hidden_size // num_attention_heads。

这一回退机制保证了对未显式定义 head_dim 的模型配置的向后兼容性。

Testing

$ pytest tests/utils/test_flops_counter.py -q
21 passed, 1 warning in 0.20s

新增 qwen3_vl 回归测试用例使用 Qwen/Qwen3-VL-4B-Instruct 的完整官方配置(显式 head_dim=128),覆盖 batch shapes [512, 1024, 2048] 与 [4096, 4096, 4096],并携带 image tokens。

  • pre-commit run --all-files passes
  • Tests pass (pytest tests/utils/test_flops_counter.py)
  • New tests added (if applicable)
  • Documentation updated (if applicable)

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Refactoring (no functional changes)
  • Performance improvement
  • CI/CD or build changes

Screenshots / Logs

N/A

@rai-studio-bot

Copy link
Copy Markdown
Contributor

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 pre-commit run --all-files before submitting.

请说明问题、改动和验证方式,保持 PR 聚焦,并在提交前运行 pre-commit run --all-files。

English contribution guide · 中文贡献指南

CI commands / CI 指令
Command / 指令 Usage / 用途
/rerun Retry failed CI / 重跑失败的 CI
/rerun <target> Rerun a workflow or check / 重跑指定 workflow 或检查
/cancel <workflow> Cancel an entire workflow / 取消整个 workflow
/help Show commands and targets / 查看指令和 target
/review Request a code review / 请求代码 review

Put one command on the first line of a new PR comment. Rerun/cancel require PR authorship or repository write access.

在新 PR 评论的首行写一条指令。PR 作者或有仓库写权限的贡献者可以重跑、取消 CI。

CI usage and targets · CI 用法与 target

@rai-studio-bot

rai-studio-bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Nyanpasu 审查看板

审查状态: ✅ 已通过

审查版本: 73aea79

目标分支: main

新一轮审查(force-push 后的 head 73aea79)已完成:作者按 P3 建议改为单行 getattr 写法,与被审内容逐字节差异仅此一处;21 项测试全部通过,CI 当前无失败。F1 已解决,无剩余问题,结论为通过。

审查阶段进度范围与结果
常规审查 ✅ 已完成 已在新 head 重新验证:与 merge-base 的完整 diff 为生产 +1/-1(单行 head_dim 读取)与测试 +32(与上一轮逐字节一致);pytest tests/utils/test_flops_counter.py 21 项通过(含钉住绝对 FLOPS 的 qwen3_vl 回归用例)。修复正确性与必要性结论沿用上一轮证据(官方 config 核实、独立复算、生产调用链 megatron/actor.py:426 → MFU 指标)。CI 当前无失败。
深度审查 ✅ 已完成 必要性与精简审计沿用上一轮结论并对照新 head 复核:生产侧保留单行 getattr+回退形态(即上一轮 F1 建议的写法);测试侧保留绝对 FLOPS 断言。F1 已由作者应用解决。

审查发现

待处理
编号 严重性 问题状态规则来源
暂无待处理的记录。
已解决或已取代
编号 严重性 问题状态规则来源
F1 Low severity 复用已有局部变量,与同文件其余估算器写法一致
精简建议(非阻塞)
✅ 已解决 —
提交范围 · 接收 2 · 建议移出 0 · 待确认 0

接收 2 个文件 · 建议移出 0 个文件 · 待确认 0 个文件。移出与待确认部分暂停深审,不代表审查通过。

文件结论仓库维护必要性依据替代去向或方案
relax/utils/training/flops_counter.py
接收 修复 _estimate_qwen3_vl_flops 的 head_dim 推导:Qwen3-VL 在 text_config 显式设置 head_dim=128,与 hidden_size//num_attention_heads(4B 为 80)不一致,导致 q/k/v/o 投影与 attention 分数 FLOPS 低估、报告的 MFU 偏低。force-push 后该修复保持单行 getattr 形式(采用评审建议的局部变量写法),职责与上一轮已验证范围一致。 PR 描述引用 Qwen/Qwen3-VL-4B-Instruct 与 Qwen3-VL-32B-Instruct 官方 config.json(显式 head_dim);flops_counter.py 为仓库现有 FLOPS/MFU 统计模块,生产调用方 relax/backends/megatron/actor.py:426 依赖其输出计算 MFU。 无替代去向:对现有单一估算函数的最小修正,与同文件其余估算器的 getattr 惯用写法一致;不改公共接口。
tests/utils/test_flops_counter.py
接收 新增 qwen3_vl 回归测试用例(Qwen3-VL-4B-Instruct 精确 config,显式 head_dim=128,钉住绝对 FLOPS),该估算器此前无测试覆盖;用例覆盖 head_dim != hidden_size//num_attention_heads 的关键边界,force-push 后内容与上一轮逐字节一致。 PR 描述 Testing 段与仓库现有 FLOPS_TEST_VL_CONFIGS 参数化测试结构一致;qwen3_moe 用例已覆盖 MoE 估算器同类边界,本用例补齐 VL 估算器。 不新增文件,直接扩展现有参数化表项——即本 PR 的做法;删除该用例会让此缺陷回归不可检出。
精简审查与验证依据
审查范围进度结论
生产代码 ✅ 已完成 单行 getattr+回退形态保留:即上一轮 F1 建议的写法,作者已采纳;无进一步可精简对象。
测试 ✅ 已完成 qwen3_vl 用例保留:绝对 FLOPS 断言可检出回退到旧推导的回归,force-push 后内容与上一轮逐字节一致。

生产代码的必要性与替代方案

范围必须保留的契约更简单的方案结论依据与限制
_estimate_qwen3_vl_flops 的 head_dim 推导(relax/utils/training/flops_counter.py:149) 估算器须匹配真实 Qwen3-VL 文本栈(q/k/v/o 投影与注意力分数项按 num_heads×head_dim 计算),供 megatron/actor.py:426 → train_metric_utils.log_perf_data_raw 的 MFU 指标消费。 恢复 hidden_size//num_attention_heads 推导(已被官方 config 证伪);要求显式 head_dim、去掉回退(破坏无该属性的轻量配置对象,偏离同文件惯用写法);共享辅助函数(为 1 行引入跨估算器耦合)。 保留 上一轮已核实 4B/32B 官方 config 显式 head_dim=128,独立复算证明旧公式低估 8.56%/10.31%;新 head 复跑 21 项测试通过。当前单行写法即 F1 建议形态,与本文件其余 5 处估算器一致。

测试的必要性与替代方案

范围必须保留的契约更简单的方案结论依据与限制
FLOPS_TEST_VL_CONFIGS 的 qwen3_vl 表项(tests/utils/test_flops_counter.py) 以 Qwen3-VL-4B-Instruct 精确 config 钉住绝对 FLOPS,使 head_dim 回退到 hidden_size//num_attention_heads 时断言失败。 仅断言 head_dim 取值来源(覆盖面更窄且需新增断言机制);或不加用例(无回归保护)。 保留 上一轮独立复算:新公式值与钉住值逐位一致,旧公式值必然失败(等效变异验证);新 head 复跑 21 项测试通过。
Powered by Nyanpasu with glm-5.3-flash max, please check the suggestions carefully.

@kkyyxhll kkyyxhll changed the title fix(flops): read head_dim from config for Qwen3-VL FLOPS estimation fix: respect explicit head_dim in Qwen3-VL FLOPs estimation Oct 10, 2026

@rai-studio-bot rai-studio-bot 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.

审查结论:修复正确(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 非阻塞精简建议,见行内评论。

看板:nyanpasu-review

Powered by Nyanpasu with glm-5.3-flash max, please check the suggestions carefully.

Comment thread relax/utils/training/flops_counter.py Resolved
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.
@kkyyxhll
kkyyxhll force-pushed the bugfix/qwen3vl_flops_cal branch from 70cbc03 to 73aea79 Compare October 10, 2026 05:17

@rai-studio-bot rai-studio-bot 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.

审查结论:通过

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 上暂无失败。

看板:nyanpasu-review

Powered by Nyanpasu with glm-5.3-flash max, please check the suggestions carefully.

@SigureMo SigureMo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTMeow 🐾 Thx!

@SigureMo
SigureMo merged commit f268f42 into redai-studio:main Oct 11, 2026
12 checks passed
Mukayi added a commit to Mukayi/Relax that referenced this pull request Oct 12, 2026
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants