Skip to content

【Paddle Tensor 第二期 API支持 0-size Tensor】Fix frobenius_norm to support 0-size tensor #72570

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

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions paddle/phi/kernels/gpu/frobenius_norm_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/activation_kernel.h"
#include "paddle/phi/kernels/full_kernel.h"
#include "paddle/phi/kernels/funcs/activation_functor.h"
#include "paddle/phi/kernels/gpu/reduce.h"

Expand All @@ -28,6 +29,27 @@ void FrobeniusNormKernel(const Context& dev_ctx,
bool keep_dim,
bool reduce_all,
DenseTensor* out) {
if (x.numel() == 0) {
auto dim_x = x.dims();
std::set<int> axis_set;
for (auto ax : dims.GetData()) {
if (ax < 0) ax += dim_x.size();
axis_set.insert(ax);
}

std::vector<int64_t> out_dims_vec;
for (int i = 0; i < dim_x.size(); ++i) {
if (axis_set.count(i) == 0) {
out_dims_vec.push_back(dim_x[i]);
} else if (keep_dim) {
out_dims_vec.push_back(1);
}
}

phi::Full<T, Context>(
dev_ctx, phi::IntArray(out_dims_vec), static_cast<T>(0), out);
return;
}
reduce_all = recompute_reduce_all(x, dims.GetData(), reduce_all);
auto out_dtype = x.dtype();
phi::Reduce<T, kps::AddFunctor, kps::SquareFunctor>(
Expand Down
5 changes: 5 additions & 0 deletions paddle/phi/kernels/impl/frobenius_norm_kernel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ void FrobeniusNormKernel(const Context& ctx,
bool keep_dim,
bool reduce_all,
DenseTensor* out) {
if (x.numel() == 0) {
ctx.template Alloc<T>(out);
phi::funcs::SetConstant<Context, T>()(ctx, out, 0);
return;
}
reduce_all = recompute_reduce_all(x, axis.GetData(), reduce_all);
Reduce<Context, T, funcs::FrobeniusNormFunctor>(
ctx, x, reduce_all, axis.GetData(), keep_dim, x.dtype(), out);
Expand Down
Loading