Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce 操作中,重复维度可能导致错误规约 #71477

Open
cangtianhuang opened this issue Mar 7, 2025 · 0 comments
Open

Reduce 操作中,重复维度可能导致错误规约 #71477

cangtianhuang opened this issue Mar 7, 2025 · 0 comments
Assignees

Comments

@cangtianhuang
Copy link

bug描述 Describe the Bug

Issue 描述

paddle.sum 为例,使用 Reduce 相关的规约操作时,若 axis 参数包含 重复维度长度恰好与输入张量的维度数相等 ,将导致规约所有维度但保留正确形状,与预期行为不符

经测试,PyTorch 和 NumPy 在遇到重复维度时将直接抛出错误,paddle 未对重复维度进行任何检查。

最小可复现代码

import paddle

x = paddle.to_tensor([[1, 2, 3], [4, 5, 6]], dtype='float32')
result = paddle.sum(x, axis=[0, 0], keepdim=False) # 重复维度 [0, 0]
expected = paddle.to_tensor([5, 7, 9], dtype='float32')  # 预期结果:仅对第 0 维求和 或者 抛出错误
assert paddle.allclose(result, expected), "PaddlePaddle test failed for dims=[0, 0]"

报错信息

未报错,但结果与预期不符。

x = paddle.to_tensor([[1, 2, 3], [4, 5, 6]], dtype='float32')
test_cases = [
        ([0, 0], [5, 7, 9]),  # 重复维度 [0, 0]
        ([1, 1], [6, 15]),    # 重复维度 [1, 1]
        ([0, 1], 21.)        # 维度 [0, 1]
    ]

result: Tensor(shape=[3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       [21., 0. , 0. ])
expected: Tensor(shape=[3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       [5., 7., 9.])

result: Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       [21., 0. ])
expected: Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       [6. , 15.])

result: Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       21.)
expected: Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       21.)

其他补充信息 Additional Supplementary Information

其他框架行为:

  • PyTorch:
RuntimeError: dim 0 appears multiple times in the list of dims
  • NumPy:
ValueError: duplicate value in 'axis'

修改建议

问题位于 paddle\phi\core\kernel_utils.h 中的 recompute_reduce_all 函数中,其直接判断规约维度大小是否与输入维度数相等: static_cast<int>(dims.size()) == x.dims().size() ,导致在重复维度情况下错误触发 reduce_all 行为。

inline bool recompute_reduce_all(const DenseTensor& x,
                                 const IntArray& dims,
                                 bool reduce_all = false) {
  if (dims.size() == 0 || x.dims().size() == 0 ||
      static_cast<int>(dims.size()) == x.dims().size() || reduce_all) {
    // when input 0D, it can only reduce_all
    return true;
  } else {
    return false;
  }
}

若这确实是 paddle 的 bug,我将在随后的 PR 中修复它🎈。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants