Skip to content

[0-Size Tensor问题修复] paddle.mean - #71961

Merged
luotao1 merged 5 commits into
PaddlePaddle:developfrom
cangtianhuang:mean
Apr 1, 2025
Merged

luotao1 merged 5 commits into
PaddlePaddle:developfrom
cangtianhuang:mean

Conversation

@cangtianhuang

@cangtianhuang cangtianhuang commented Mar 27, 2025 •

Copy link
Copy Markdown
Contributor

PR Category

Execute Infrastructure

PR Types

Bug fixes

Description

经检查和测试,当前 paddle.mean 的 特判逻辑 不能正确拦截所有 0-size tensor 的 reduce 情况。gpu 上运行即会报错(cpu 上运行正常,详见 #71555 ):

若输入形状为 [2, 0, 4],沿着 axis=1 求 mean,预期输出形状应为 [2, 4],填充为 NAN 。不符合特判中 if (out && out->numel() == 0) 或 if (x.numel() == 0 && out && out->dims().size() == 0) 情况。

import paddle
from paddle import Tensor
import numpy as np

paddle.set_device('gpu')
x = np.array([], dtype=np.float32).reshape([2, 0, 4])
ref = np.mean(x, axis=1, keepdims=False)
out = paddle.mean(Tensor(x), axis=1)
assert np.allclose(ref, out.numpy(), equal_nan=True), f"ref: {ref}, out: {out.numpy()}"

经检查,FullKernel 中实现了对 0 显存长度的分配,因此我们可以直接调用 Full 填充并返回,修改方式类似于 #71578 。最后添加了前向测试用例。

使用 PaddleAPITest 测试通过,使用自己新增的配置:

python engine.py --accuracy=True --api_config_file=tester/api_config/api_config_0_size_mean.txt > tester/api_config/test_log_0_size_mean/log.log 2>&1

image

@wanghuancoder

@paddle-bot

paddle-bot Bot commented Mar 27, 2025

Copy link
Copy Markdown

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册。
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@paddle-bot paddle-bot Bot added the contributor External developers label Mar 27, 2025
@cangtianhuang

Copy link
Copy Markdown
Contributor Author

将 paddle/phi/kernels/funcs/reduce_function.h 处的 GE 修改为 GT ,是因为更底层的代码对 0-size tensor 的支持有奇怪的报错,不应该将处理逻辑下放,如 #71826 。经测试, eigen3 库对 0-size tensor 的支持也并不完善:

  • 以 Paddle 当前所用 eigen3 版本为例,最小复现代码:
    Eigen::Tensor<float, 3> tensor(2, 0, 4);
    tensor.setZero();
    auto mean_op = tensor.mean(Eigen::array<int, 1>{0});
    Eigen::Tensor<float, 2> mean_tensor = mean_op;
  • 报错:
Assertion failed: divider > 0, file D:\Projects\C_Project\eigen3\unsupported\Eigen\CXX11\src\Tensor\TensorIntDiv.h, line 156
  • 报错原因:
    Tensor\TensorReduction.h 尝试加速计算,通过维度大小计算步长 m_outputStrides ,但是规约后的维度为 (0, 4),相乘得到 0 步长,随后在 Tensor\TensorIntDiv.h 报错。

@luotao1 luotao1 added the HappyOpenSource Pro 进阶版快乐开源活动,更具挑战性的任务 label Mar 28, 2025

@wanghuancoder wanghuancoder left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@luotao1
luotao1 merged commit fd37718 into PaddlePaddle:develop Apr 1, 2025
@cangtianhuang
cangtianhuang deleted the mean branch April 1, 2025 09:40
YqGe585 pushed a commit to YqGe585/Paddle that referenced this pull request May 7, 2025
* Fix 0-size tensor support in mean when out->numel() > 0, add test.

* Restore GT check in reduce_function.

* Fix bug.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor External developers HappyOpenSource Pro 进阶版快乐开源活动,更具挑战性的任务

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants