Skip to content

【快乐开源】Paddle Tensor 规范化二期 API 支持 0-size Tensor No.16-17:paddle.var、paddle.std - #71580

Closed
cangtianhuang wants to merge 17 commits into
PaddlePaddle:developfrom
cangtianhuang:support-0-size-var-std
Closed

cangtianhuang wants to merge 17 commits into
PaddlePaddle:developfrom
cangtianhuang:support-0-size-var-std

Conversation

@cangtianhuang

@cangtianhuang cangtianhuang commented Mar 11, 2025 •

Copy link
Copy Markdown
Contributor

PR Category

Operator Mechanism

PR Types

Bug fixes

Description

该 PR 修改了 paddle.var 的 C++ 与 python 代码,使其支持处理 0-size Tensor 。 paddle.std 未作修改。增加了测试用例,包括多个形状的 0-size Tensor ,如 [], [0,], [0, 3], [2, 0, 4]。

注: #71555 合并后,经测试已经解决了 unbiased=False 时的 0-size tensor 情况,但当 unbiased=True 时,输出仍不符合预期。

期望的值:("std", float("nan")), ("var", float("nan"))。具体修改如下:

paddle.var

在 C++ 侧,修改了 xpu/elementwise.h 与 xpu/reduce.h 文件,使的 xpu 支持 0-size Tensor。

在 Python 侧,在 python/paddle/tensor/stat.py 中对 n > one_const 处做了一个巧妙的修改,使得当 n<=1 时 n 值不变( unbiased = True 时,n=1 无法无偏,n=0 则 out/0 得到期望的 NaN 值; unbiased = False 时,n=0 out/0 也得到期望的 NaN 值),确保形状与数值永远满足期望。

paddle.std

在 Python 侧,paddle.std 调用 paddle.var 并开方,对于 0-size Tensor NaN 开方后仍旧为 NaN ,因此代码不变。

经测试, paddle.var 、 paddle.std 通过了 “0-size tensorAPI 支持 0-size Tensor No.16、17” 的测试。且在 [], [0,], [0, 3], [2, 0, 4] 等多个 0-size Tensor 形状中表现与预期一致。测试结果如下:

image

@paddle-bot

paddle-bot Bot commented Mar 11, 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 11, 2025
@cangtianhuang
cangtianhuang deleted the support-0-size-var-std branch March 12, 2025 06:20
@cangtianhuang cangtianhuang reopened this Mar 18, 2025
@luotao1 luotao1 added the HappyOpenSource Pro 进阶版快乐开源活动,更具挑战性的任务 label Mar 19, 2025

@HydrogenSulfate HydrogenSulfate left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

同样添加一下int、complex相关的类型单测,预期输出参考:https://data-apis.org/array-api/latest/API_specification/generated/array_api.mean.html#mean

Comment thread test/legacy_test/test_variance_layer.py Resolved

@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.

var是一个比较“高层”的API,对于elementwise、reduce类的问题修复,最好以elementwise相关API和reduce相关API先做为case做全量摸底修复。

Comment on lines +1052 to +1056
PADDLE_ENFORCE_GE(x.numel(),
0,
common::errors::InvalidArgument(
"Tensor need be reduced must not empty."));
"The input size (numel) of reduce op should be larger "
"than or equal to 0"));

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.

这里还不能直接放行。最好配合0-Size报错里的所有reduce相关API一起验证修复~

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

是的,新的pr中未做此修改

axis));

// Intercept the 0-size dimension case
bool has_zero_dim = false;

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.

这个叫has_0_size更好。0 dim指的是len(shape)==0的Tensor

GetReduceDims(x.dims(), dims, reduce_all, &reduce_dims);

int r = xpu::SUCCESS;
if (reduce_dims.size() == 0) {

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.

这个不能删除:
reduce_dims.size() == 0 代表 axis=None,比如 paddle.sum(x, axis=None)
x.dims().size() == 0 代表 0D Tensor
x.numel() == 0 代表 0Size Tensor
不能混淆~

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

明白了,感谢解惑!

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.

4 participants