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

【SCU】【Paddle Tensor 第二期 API支持 0-size Tensor】(paddle.sum、paddle.mean、paddle.prod、 paddle.var、 paddle.std)支持 0-size tensor #70146

Closed
wants to merge 7 commits into from

Conversation

mori0umi
Copy link
Contributor

@mori0umi mori0umi commented Dec 11, 2024

PR Category

User Experience

PR Types

Bug fixes

Description

解决 sum, mean, prod, var, std 五个 reduce_op 输入 Tensor 为 0-size Tensor 时出现的报错。
image

Copy link

paddle-bot bot commented Dec 11, 2024

你的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 Dec 11, 2024
@luotao1 luotao1 added the HappyOpenSource Pro 进阶版快乐开源活动,更具挑战性的任务 label Dec 12, 2024
@mori0umi mori0umi changed the title 【SCU】【Paddle Tensor 第二期 API支持 0-size Tensor】(paddle.sum、paddle.mean、paddle.prod)支持 0-size tensor 【SCU】【Paddle Tensor 第二期 API支持 0-size Tensor】(paddle.sum、paddle.mean、paddle.prod、 paddle.var、 paddle.std)支持 0-size tensor Dec 12, 2024
}
};

// Prod操作的特化
Copy link
Contributor

Choose a reason for hiding this comment

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

中文注释删掉

dev_ctx.template Alloc<T>(out);

Copy link
Contributor

Choose a reason for hiding this comment

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

多余换行删除

Comment on lines 1374 to 1384
// Sum操作的特化
template <typename Context, typename T, typename OutT>
struct SpecialCaseReduceOp<phi::funcs::SumFunctor, Context, T, OutT> {
static void HandleEmptyTensor(const Context& dev_ctx,
const phi::DenseTensor& input,
phi::DenseTensor* output) {
auto out = dev_ctx.template Alloc<OutT>(output);
*out = static_cast<OutT>(0);
}
};

Copy link
Contributor

Choose a reason for hiding this comment

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

对于Sum的处理,是不是改这里就行了,
image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

确实,我感觉是的,当时没有想到,我修改一下,感谢reveiw!另外请问GPU部分的实现是不是也是在phi/kps/reduce_kernel.cu里面改比较合适?

Copy link
Contributor

Choose a reason for hiding this comment

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

确实,我感觉是的,当时没有想到,我修改一下,感谢reveiw!另外请问GPU部分的实现是不是也是在phi/kps/reduce_kernel.cu里面改比较合适?

好像是的,改SumRawKernel更合适,因为SumKernel只是一个转发的


// Mean操作的特化
template <typename Context, typename T, typename OutT>
struct SpecialCaseReduceOp<phi::funcs::MeanFunctor, Context, T, OutT> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Mean同理

// Prod操作的特化
template <typename Context, typename T, typename OutT>
struct SpecialCaseReduceOp<phi::funcs::ProdFunctor, Context, T, OutT> {
static void HandleEmptyTensor(const Context& dev_ctx,
Copy link
Contributor

Choose a reason for hiding this comment

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

Empty同理

@@ -201,7 +201,7 @@ def var(
)
n = n.astype(dtype)
if unbiased:
one_const = paddle.ones([], x.dtype)
Copy link
Contributor

Choose a reason for hiding this comment

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

这里有必要加上to吗?paddle默认创建的Tensor都在GPU上

Copy link
Contributor Author

Choose a reason for hiding this comment

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

如果是cpu创建的数据过一下var就被放在gpu上了,感觉不太符合逻辑

Copy link
Contributor

@HydrogenSulfate HydrogenSulfate Dec 12, 2024

Choose a reason for hiding this comment

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

如果是cpu创建的数据过一下var就被放在gpu上了,感觉不太符合逻辑

这个是Paddle设备切换的逻辑问题,在 #68635 中尝试修复过,跟其他同事讨论的结果是,Paddle算子运行设备由当前全局设备控制,而不是算子输入的设备控制,所以这里不用管这个问题(有一个非公开的设备上下文管理器,但是不可能给每个API都加上)。这块跟pytorch存在一定差异

Copy link
Contributor Author

Choose a reason for hiding this comment

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

明白了,下个commit去掉,感谢解答!

Copy link
Contributor

@HydrogenSulfate HydrogenSulfate left a comment

Choose a reason for hiding this comment

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

输出Tensor的形状需要考虑各种情况

Comment on lines +33 to +45
if (x.numel() == 0 && y.numel() != 0) {
out->Resize(y.dims());
dev_ctx.template Alloc<T>(out);
ActivationImpl<T, T, Context, phi::funcs::NegativeFunctor<T>>(
dev_ctx, y, out, phi::funcs::NegativeFunctor<T>());
return;
}
if (y.numel() == 0 && x.numel() != 0) {
out->Resize(x.dims());
dev_ctx.template Alloc<T>(out);
phi::Copy(dev_ctx, x, dev_ctx.GetPlace(), false, out);
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

是不是缺少当x和y都为0-size的逻辑?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

我想了一下,这里做特判好像本身就不合适。因为 numpy 和 torch 的表现是即使有一方是 0-size ,也要先处理广播的逻辑。比如(0,3)和(2,)的 tensor 相减, numpy 报错是 ValueError: operands could not be broadcast together with shapes (0,3) (2,) 。

Copy link
Contributor

Choose a reason for hiding this comment

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

我想了一下,这里做特判好像本身就不合适。因为 numpy 和 torch 的表现是即使有一方是 0-size ,也要先处理广播的逻辑。比如(0,3)和(2,)的 tensor 相减, numpy 报错是 ValueError: operands could not be broadcast together with shapes (0,3) (2,) 。

对的,广播是有条件限制的,并不是任意两种形状都能广播

Comment on lines +33 to +39
if (x.numel() == 0) {
DataType dtype = phi::CppTypeToDataType<T>::Type();
int value = 1;
FullKernel<T, Context>(
dev_ctx, std::vector<int64_t>({}), value, dtype, out);
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

这里可能有问题,prod是允许在某个维度上进行reduce,而不是全部reduce的,即输出的形状需要考虑dims

Comment on lines +33 to +39
if (x.numel() == 0) {
DataType dtype = phi::CppTypeToDataType<T>::Type();
double nanValue = std::numeric_limits<double>::quiet_NaN();
FullKernel<T, Context>(
dev_ctx, std::vector<int64_t>({}), nanValue, dtype, out);
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

同理,输出形状需要考虑dims和keep_dim以及reduce_all三个变量

Comment on lines +34 to +40
if (x.numel() == 0) {
DataType dtype = phi::CppTypeToDataType<T>::Type();
int value = 0;
FullKernel<T, Context>(
dev_ctx, std::vector<int64_t>({}), value, dtype, out);
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

同上

Comment on lines +38 to +50
if (x.numel() == 0 && y.numel() != 0) {
out->Resize(y.dims());
dev_ctx.template Alloc<T>(out);
ActivationImpl<T, T, Context, phi::funcs::NegativeFunctor<T>>(
dev_ctx, y, out, phi::funcs::NegativeFunctor<T>());
return;
}
if (y.numel() == 0 && x.numel() != 0) {
out->Resize(x.dims());
dev_ctx.template Alloc<T>(out);
phi::Copy(dev_ctx, x, dev_ctx.GetPlace(), false, out);
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

同理,0-size减去0-size会如何

Copy link
Contributor

@HydrogenSulfate HydrogenSulfate left a comment

Choose a reason for hiding this comment

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

以及添加形状足够丰富的单测来验证代码的正确性

@HydrogenSulfate
Copy link
Contributor

@mori0umi 好像有编译上的问题,
image

Copy link

paddle-ci-bot bot commented Dec 20, 2024

Sorry to inform you that cf92bb1's CIs have passed for more than 7 days. To prevent PR conflicts, you need to re-run all CIs manually.

@luotao1 luotao1 closed this Jan 8, 2025
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