Repository navigation
fix(opd): keep tail bin under mask, avoid double-summing low_var_kl, and clip 1D advantages - #827
flyingdoubleG wants to merge 1 commit into
Conversation
…and clip 1D advantages
|
Thanks for contributing to Relax, @flyingdoubleG! 感谢你为 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。 🔎 Merge requirements / 合入条件GitHub:⏳ 合入条件未满足
|
Nyanpasu 审查看板审查状态: 💬 已完成 · 有补充意见 审查版本: c01c972 目标分支: main 四个定向 CPU 用例在本提交全部通过,其中三个在基线复现了目标缺陷。建议补上 norm 模式部分填充行的梯度回归覆盖。GPU 训练未验证。
CI 异常检查版本: c01c972 · 已分析
审查发现待处理
已解决或已取代
改进建议(非阻塞)精简💡 生产代码 · 1 条建议正向和反向 KL 的 padding 项在求和前已是精确零,可省去重复遮罩。
Powered by Nyanpasu with gpt-6.2-sol high, please check the suggestions carefully.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_opd_kl_masked_all_false_row_stays_finite() -> None: | ||
| s = torch.tensor([[-1.0, -2.0], [-1.0, -2.0]], dtype=torch.float32, requires_grad=True) | ||
| t = torch.tensor([[-1.2, -1.8], [-1.2, -1.8]], dtype=torch.float32) | ||
| mask = torch.tensor([[True, True], [False, False]]) |
There was a problem hiding this comment.
当前两行分别全有效、全遮罩,这个用例在基线 97ccd1d4 上也通过。常规 union 批次会有部分填充行。在 PyTorch 2.11 CPU 上,用 [True, True, False] 的行计算 norm 模式 JSD,基线和本提交的前向值一致,但基线两个有效位置的梯度都是 NaN,本提交梯度有限。
建议把第一行改成两个有效位置加一个 padding,保留第二行的全遮罩断言。这样现有的梯度有限性检查就能捕获真实的归一化回归。测试准则
|
|
||
| unmasked = compute_opd_kl_topk(s, t, kl_type=kl_type, jsd_alpha=0.5, norm_mode="tail", mask=None) | ||
| masked = compute_opd_kl_topk(s, t, kl_type=kl_type, jsd_alpha=0.5, norm_mode="tail", mask=all_true) | ||
| torch.testing.assert_close(masked, unmasked, atol=1e-6, rtol=1e-6) |
There was a problem hiding this comment.
这里的等价性检查能发现 mask 改变结果,但如果实现只在有 padding 的行保留 tail,两边都会漏算 tail,仍会相等。上面的独立参考目前只检查有 padding 的行。
可以保留这里的等价性断言,并将独立参考用例的第一行 mask 改为全 True,第二行继续覆盖 padding。这样两类输入都能直接和 tail 完整分布核对,无需再增加用例。测试质量准则
| if jsd_alpha == 0.0: | ||
| result = s_t.exp() * (s_t - t_t) | ||
| if mask is not None: | ||
| result = result[..., :K_orig].masked_fill(~mask, 0.0) | ||
| if eff_mask is not None: | ||
| result = result.masked_fill(~eff_mask, 0.0) | ||
| return result.sum(dim=-1) | ||
| if jsd_alpha == 1.0: | ||
| result = t_t.exp() * (t_t - s_t) | ||
| if mask is not None: | ||
| result = result[..., :K_orig].masked_fill(~mask, 0.0) | ||
| if eff_mask is not None: | ||
| result = result.masked_fill(~eff_mask, 0.0) | ||
| return result.sum(dim=-1) |
There was a problem hiding this comment.
进入这两个分支前,padding 上的 s_t 和 t_t 都已由 torch.where 置零。两个 KL 端点在这里的 padding 项恒为 exp(0) * (0 - 0) = 0,前面的 where 已挡住这些位置的输入梯度。可以直接求和,省去每个端点的整张量遮罩。中间权重的 JSD 继续显式清零,避免混合概率计算的舍入残差。
| if jsd_alpha == 0.0: | |
| result = s_t.exp() * (s_t - t_t) | |
| if mask is not None: | |
| result = result[..., :K_orig].masked_fill(~mask, 0.0) | |
| if eff_mask is not None: | |
| result = result.masked_fill(~eff_mask, 0.0) | |
| return result.sum(dim=-1) | |
| if jsd_alpha == 1.0: | |
| result = t_t.exp() * (t_t - s_t) | |
| if mask is not None: | |
| result = result[..., :K_orig].masked_fill(~mask, 0.0) | |
| if eff_mask is not None: | |
| result = result.masked_fill(~eff_mask, 0.0) | |
| return result.sum(dim=-1) | |
| if jsd_alpha == 0.0: | |
| return (s_t.exp() * (s_t - t_t)).sum(dim=-1) | |
| if jsd_alpha == 1.0: | |
| return (t_t.exp() * (t_t - s_t)).sum(dim=-1) |


What
Fix three bugs in
relax/utils/opd/opd_utils.py:compute_opd_kldrops the tail bin whennorm_mode="tail"andmaskis provided (uniontop-K path).compute_opd_kl_topksumslow_var_kltwice on[R, K]inputs, collapsing the[R]per-token vector to a 0-D scalar.apply_opd_to_advantagesignores--opd-per-token-clipon the 1D sampled-token / EOPD path.Why
compute_opd_kl,_add_tailappends the tail probability bin as columnK(makings_t/t_tshape[R, K + 1]). Whenmask([R, K]) is passed, slicingresult[..., :K_orig]discards columnKand silently reducesnorm_mode="tail"(the default) tonorm_mode="trunc":compute_opd_kl_topk(s, t, kl_type="forward_kl", norm_mode="tail", mask=all_true)disagrees withmask=None.s = [-0.5],t = [-0.7], the true tail-completed forward KL is+0.02466with student gradient+0.2797, whereas dropping the tail bin returned-0.09932(negative KL) with student gradient-0.4966(opposite sign).compute_opd_klalready reduces 2D[R, K]inputs overdim=-1forkl_type="low_var_kl"(return per_kl.sum(dim=-1) if per_kl.dim() > 1 else per_kl).compute_opd_kl_topkcalled.sum(dim=-1)a second time, returning a 0-D scalar instead of[R], which causestorch.catto raiseRuntimeErrorincompute_policy_opd_lossand broadcasts the sequence sum across every token inapply_opd_to_advantages.apply_opd_to_advantagesappliedargs.opd_per_token_clipin theis_topkbranch but omitted it in the 1Dstudent_sampled/ EOPD branch right below it.How
compute_opd_kl:maskwith aTruecolumn for the tail bin whennorm_mode == "tail"instead of slicingresult[..., :K_orig], and zero out inactive entries ons_t/t_tbefore computingexpand log differences so padded-infpositions stay finite._normagainst all-Falsemasked rows (-inf - (-inf)).compute_opd_kl_topk: returncompute_opd_kl(...)directly forlow_var_klwithout the redundant.sum(dim=-1).apply_opd_to_advantages: applytorch.clamp(kl_term, max=float(per_token_clip))on the 1D sampled-token path as well.tests/cpu/utils/test_opd_kl.pyverifying forward values and student gradients against exact tail-completed distributions,low_var_kl[R]output shape andcompute_policy_opd_lossintegration, 1Dopd_per_token_clip, and all-Falsemasked rows.Testing
Verified the 8 new regression cases fail on
mainand pass with this fix:CUDA_VISIBLE_DEVICES="" pytest tests/cpu/utils/test_opd_kl.py tests/cpu/backends/megatron/test_opd_loss_aggregation.py tests/cpu/utils/test_arguments_opd_teacher_colocate.py -vResult:
22 passed.Lint and format checks:
Result: all checks passed, 2 files already formatted.
pre-commit run --all-filespassesTests pass (
pytest tests/cpu/utils/test_opd_kl.py tests/cpu/backends/megatron/test_opd_loss_aggregation.py tests/cpu/utils/test_arguments_opd_teacher_colocate.py)New tests added (if applicable)
Documentation updated (if applicable)
Type of Change