已关闭
[Bug]: Qwen3.5-9B/35B训练使用thd格式时,存在精度问题 #41
lixionglong创建于 7月25日关闭于 4 天前
7月25日 添加了label:bug
7月25日 关联了pull request:fix qwen3.5 thd format precision issue
7月25日 关联了pull request:fix qwen3.5 thd format precision issue
daixzh
4 天前 评论:
4 天前 评论:
关联PR已合入,感谢您的贡献!我们将根据流程关闭此 issue。
如后续有新的疑问或补充内容,欢迎随时重新打开本 issue,或另起一个新的 issue 进行提问。
感谢您的支持!


4 天前 添加了label:resolved
在提交新问题之前,请确保您已经在社区中搜索过相关问题,并使用了社区中提供的资源/工具后,仍未找到满意的解决方式。
⚠️ 安全信息提醒:请仔细检查提供的文本内容,确保其不包含敏感数据信息,包括但不限于:
在分享配置信息或代码示例时,请将敏感信息脱敏处理,或使用
<TOKEN>等占位符替代原有内容。环境信息
CANN:9.0.0
torch:2.10.0
torch-npu:2.10.0
triton-ascend:3.2.1
megatron:core_v0.16.1
mindspeed:core_r0.16.0
megatron-bridge:v0.3.1
mindSpeed-bridge:3655c07cbcc9
🐛 问题描述
问题概述
使用
qkv_format="thd"训练 Qwen3.5-9B 时,loss 高达 21.37,grad_norm 达 570+。而qkv_format="bshd"格式下 loss 约 0.37,正常。根因:
Qwen3VLModel.forward中创建一个dtype=torch.int32的attention_mask,传入preprocess_packed_seqs后,input_ids[i, attention_mask[i]]将 int 值当作行号索引而非 bool 掩码,导致combined_embeddings(embedding 输出)和position_ids全部被破坏。复现步骤
--qkv-format thd在 Qwen3.5 模型上通过 mindspeed-bridge 启动训练train/loss ≈ 21.37,train/grad_norm ≈ 571,而非正常值(~0.3–1.0)BSHD 格式(
--qkv-format bshd)不受影响,因为此时packed_seq_params is None,Qwen3VLModel.forward中整个if packed_seq_params is not None分支被跳过。根因分析
model.py中attention_mask的创建方式:# 约第503行和第562行,共两处 if attention_mask is None: attention_mask = torch.ones_like(input_ids, dtype=torch.int32, device=input_ids.device)这个 mask 随后被传入 3 处
preprocess_packed_seqs调用:input_ids(token 序列)combined_embeddings(embedding 输出 / 隐藏状态)position_ids(RoPE 位置编码)preprocess_packed_seqs内部(utils.py:724):当
attention_mask[i]是torch.int32且值全为1时:# int32 mask: [1, 1, 1, ..., 1] input_ids[i, attention_mask[i]] # → input_ids[0, [1, 1, ..., 1]] ← PyTorch 整型索引! # → 取第 1 行,重复 2048 次! # → 所有 token 都被替换为 position=1 的 embedding/position_id!这导致进入 transformer 层的隐藏状态完全错误,后续所有计算产生垃圾输出。
具体例子
假设单条数据长度 2000 tokens,TP=4,CP=1:
修复方案
将两处
dtype从torch.int32改为torch.bool:if attention_mask is None: - attention_mask = torch.ones_like(input_ids, dtype=torch.int32, device=input_ids.device) + attention_mask = torch.ones_like(input_ids, dtype=torch.bool, device=input_ids.device)这使
input_ids[i, attention_mask[i]]从整型索引(按行号选取特定行)变为 bool 掩码(保留 True 对应的行),即预期行为。验证结果
train/losstrain/grad_norm修复请求
请将
Qwen3VLModel.forward中两处attention_mask初始化的dtype=torch.int32改为dtype=torch.bool。欢迎加入社区,感谢您对社区的贡献 🎉!