-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
Conversation
你的PR提交成功,感谢你对开源项目的贡献! |
} | ||
}; | ||
|
||
// Prod操作的特化 |
There was a problem hiding this comment.
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); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
多余换行删除
// 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); | ||
} | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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里面改比较合适?
There was a problem hiding this comment.
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> { |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Empty同理
python/paddle/tensor/stat.py
Outdated
@@ -201,7 +201,7 @@ def var( | |||
) | |||
n = n.astype(dtype) | |||
if unbiased: | |||
one_const = paddle.ones([], x.dtype) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里有必要加上to吗?paddle默认创建的Tensor都在GPU上
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果是cpu创建的数据过一下var就被放在gpu上了,感觉不太符合逻辑
There was a problem hiding this comment.
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存在一定差异
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
明白了,下个commit去掉,感谢解答!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
输出Tensor的形状需要考虑各种情况
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; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是不是缺少当x和y都为0-size的逻辑?
There was a problem hiding this comment.
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,) 。
There was a problem hiding this comment.
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,) 。
对的,广播是有条件限制的,并不是任意两种形状都能广播
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; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里可能有问题,prod是允许在某个维度上进行reduce,而不是全部reduce的,即输出的形状需要考虑dims
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; | ||
} |
There was a problem hiding this comment.
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三个变量
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; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同上
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; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同理,0-size减去0-size会如何
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
以及添加形状足够丰富的单测来验证代码的正确性
@mori0umi 好像有编译上的问题, |
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. |
PR Category
User Experience
PR Types
Bug fixes
Description
解决 sum, mean, prod, var, std 五个 reduce_op 输入 Tensor 为 0-size Tensor 时出现的报错。
