[XPU] Fix precision for paddle.Tensor.__getitem__ backward pass#78772
Open
YqGe585 wants to merge 1 commit intoPaddlePaddle:developfrom
Open
[XPU] Fix precision for paddle.Tensor.__getitem__ backward pass#78772YqGe585 wants to merge 1 commit intoPaddlePaddle:developfrom
YqGe585 wants to merge 1 commit intoPaddlePaddle:developfrom
Conversation
…r atomic scatter-add in backward pass - Add INT64 to xpu3_op_list.cc for index_elementwise_get_grad - Replace xpu::index_elementwise_get_grad with xpu::scatter_nd for accumulate=true cases to fix race conditions on duplicate indices - Also use scatter_nd for int64 output (no SDK specialization) - Fix resolves backward gradient precision gaps across all dtypes (float32, float16, bfloat16, int32, int64, int8)
|
你的PR提交成功,感谢你对开源项目的贡献! |
Member
Author
|
/re-run all-failed |
5 similar comments
Member
Author
|
/re-run all-failed |
Member
Author
|
/re-run all-failed |
Member
Author
|
/re-run all-failed |
Member
Author
|
/re-run all-failed |
Member
Author
|
/re-run all-failed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Category
Operator Mechanism
PR Types
Bug fixes
Description
背景
paddle.Tensor.__getitem__在 XPU 上进行 advanced indexing(使用 Tensor 作为索引)时,前向精度正常,但反向梯度存在精度偏差,与 GPU 结果不一致。根本原因
反向 kernel
IndexElementwiseGetGradKernel(paddle/phi/kernels/xpu/index_elementwise_get_grad_kernel.cc)调用了xpu::index_elementwise_get_grad,该函数存在两个问题:accumulate=true(标准反向传播场景)时,若存在重复索引,缺少原子操作会导致竞争条件(race condition),产生错误的梯度累加结果。index_elementwise_get_grad没有<long, long>特化版本,导致 int64_t 类型输出产生错误值。修复方案
参考
index_put_grad_kernel.cc的实现,当满足accumulate=true && slice_offset==0(标准反向传播路径)或输出类型为int64_t时,改用XPUDealWithIndices + xpu::scatter_nd替换原xpu::index_elementwise_get_grad调用:xpu::scatter_nd(is_overwrite=false)天然支持原子 scatter-add,可正确处理重复索引此外,在
xpu3_op_list.cc中为index_elementwise_get_grad增加了 INT64 类型支持。验证
在 38 个测试配置中,修复前 37 个存在反向梯度精度失败;修复后全部通过(float32、float16、bfloat16、int32、int64、int8 各类型均验证通过)。
是否引起精度变化
是——XPU 上
paddle.Tensor.__getitem__反向梯度精度与 GPU 对齐。