fix rmsnorm grad when input size is zero#79397
Conversation
PaddlePaddle-bot
left a comment
There was a problem hiding this comment.
🤖 Paddle-CI-Agent | pr_review |
2026-06-30 20:17:18
📋 Review 摘要
PR 概述:修复 GPU rms_norm_grad 在空输入时 dscale 未清零导致梯度随机值的问题
变更范围:paddle/phi/kernels/gpu/rms_norm_cuda_kernel.h
影响面 Tag:[Operator Mechanism]
问题
| 级别 | 文件 | 概述 |
|---|---|---|
| 🟡 建议 | paddle/phi/kernels/gpu/rms_norm_cuda_kernel.h:1424 |
空输入分支缺少回归测试,无法防止 dscale 再次变为未初始化梯度 |
📝 PR 规范检查
标题缺少官方 Tag;同时 是否引起精度变化 填写为 是,但描述中没有说明精度变化来源、影响范围和验证方式。
标题建议(可直接复制):
[Operator Mechanism] fix rmsnorm grad when input size is zero
PR 描述建议(点击展开,可直接复制)
### PR Category
<!-- One of [ User Experience | Execute Infrastructure | Operator Mechanism | CINN | Custom Device | Performance Optimization | Distributed Strategy | Parameter Server | Communication Library | Auto Parallel | Inference | Environment Adaptation ] -->
Operator Mechanism
### PR Types
<!-- One of [ New features | Bug fixes | Improvements | Performance | BC Breaking | Deprecations | Docs | Devs | Not User Facing | Security | Others ] -->
Bug fixes
### Description
<!-- Describe what you’ve done -->
修复 GPU `rms_norm_grad` 在 `X.numel() == 0` 时只分配 `dscale` 但不清零的问题。空输入没有元素贡献到 `dscale`,梯度应为全 0;本 PR 在空输入分支使用 `Full` 对 `dscale` 进行 0 填充,避免未初始化内存进入梯度计算。
验证方式:N/A(当前 diff 未包含测试变更)
### 是否引起精度变化
<!-- one of the following [ 是 | 否 ]-->
是。精度变化来源于空输入场景下 `scale` 梯度从未初始化随机值修正为数学上应有的 0;影响范围为 GPU `rms_norm_grad` 的空 Tensor 输入场景。验证方式:N/A(当前 diff 未包含测试变更)。总体评价
代码层面使用 Full<T, Context> 与非空路径的 dscale dtype 一致,修复方向合理。主要缺口是这个空 Tensor 梯度场景没有同步测试保护,PR 描述也需要补足精度变化说明。
| // exactly zero. We must fill it with 0 instead of leaving the allocated | ||
| // buffer uninitialized (which would otherwise inject random values into | ||
| // the gradient computation). | ||
| Full<T, Context>(dev_ctx, dscale->dims(), 0, dscale); |
There was a problem hiding this comment.
🟡 建议 这个修复缺少覆盖 X.numel() == 0 的回归测试。
本 PR 改变的是空输入时 dscale 的数值语义:之前只分配缓冲区,现在应稳定为全 0。现有 test/legacy_test/test_rms_norm_op.py 只覆盖正常形状和 eps=None 反向,没有形如 x.shape == [0, N] 且 scale.stop_gradient = False 的用例;后续改动很容易重新引入未初始化梯度。
建议修复方式:
在 test/legacy_test/test_rms_norm_op.py 增加 GPU 可运行的动态图用例,例如构造 x = paddle.randn([0, cols])、scale.stop_gradient = False,执行 paddle.nn.functional.rms_norm(x, (cols,), scale).sum().backward(),并断言 scale.grad.numpy() 与 np.zeros([cols], dtype=...) 一致。
CI报告基于以下代码生成(30分钟更新一次): 1 Required任务 : 45/48 通过
2 失败详情🔴 Check approval — 需要 Approval(置信度: 高)该 Job 需要人工 Approval,完成审批后 CI 才会继续执行。
🔴 Check PR Template / Check — PR问题:缺少必需审批(置信度: 高)错误类型: PR问题 | 置信度: 高
关键日志:
修复建议:
关联变更: 未分析 |
|
/re-run all-failed |
PR Category
Operator MechanismPR Types
Bug fixesDescription
fix rmsnorm grad when input size is zero是否引起精度变化
是