Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion paddle/phi/kernels/gpu/expand_as_grad_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/full_kernel.h"
#include "paddle/phi/kernels/funcs/reduce_function.h"
#include "paddle/phi/kernels/reduce_sum_kernel.h"

namespace phi {

template <typename T, typename Context>
Expand All @@ -28,6 +28,11 @@ void ExpandAsGradKernel(const Context& context,
const DenseTensor& out_grad,
const std::vector<int64_t>& target_shape,
DenseTensor* in_grad) {
if (out_grad.numel() == 0) {
phi::Full<T, Context>(
context, phi::IntArray(common::vectorize(in_grad->dims())), 0, in_grad);
return;
}
auto in_dims = x.dims();
auto out_dims = out_grad.dims();
int in_rank = in_dims.size();
Expand Down
12 changes: 8 additions & 4 deletions paddle/phi/kernels/gpu/expand_as_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ void ExpandAsKernel(const Context& dev_ctx,
const paddle::optional<DenseTensor>& y,
const std::vector<int64_t>& target_shape_t,
DenseTensor* out) {
if (x.numel() == 0 || (y.get_ptr() && y.get_ptr()->numel() == 0)) {
dev_ctx.template Alloc<T>(out);
return;
}
std::vector<int64_t> target_shape = target_shape_t;

if (y.get_ptr()) {
Expand All @@ -43,10 +47,10 @@ void ExpandAsKernel(const Context& dev_ctx,
vec_in_dims.insert(vec_in_dims.begin(), diff, 1);

for (unsigned int i = 0; i < vec_in_dims.size(); ++i) {
PADDLE_ENFORCE_NE(
target_shape[i],
0,
errors::InvalidArgument("The value of target shape cannot be zero."));
if (target_shape[i] == 0) {
dev_ctx.template Alloc<T>(out);
return;
}
if (i < diff) {
PADDLE_ENFORCE_GT(
target_shape[i],
Expand Down
7 changes: 6 additions & 1 deletion paddle/phi/kernels/impl/expand_as_grad_kernel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
#pragma once

#include "paddle/phi/core/tensor_utils.h"
#include "paddle/phi/kernels/full_kernel.h"
#include "paddle/phi/kernels/impl/expand_as_kernel_impl.h"

namespace phi {
template <typename Context, typename T, int Dims>
void ExpandAsBackward(const Context& dev_ctx,
Expand Down Expand Up @@ -48,6 +48,11 @@ void ExpandAsGradKernel(const Context& context,
const DenseTensor& out_grad,
const std::vector<int64_t>& target_shape,
DenseTensor* in_grad) {
if (out_grad.numel() == 0) {
phi::Full<T, Context>(
context, phi::IntArray(common::vectorize(in_grad->dims())), 0, in_grad);
return;
}
auto x_dims = x.dims();
auto out_grad_dims = out_grad.dims();
std::vector<int64_t> real_target_shape =
Expand Down
13 changes: 8 additions & 5 deletions paddle/phi/kernels/impl/expand_as_kernel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

#include "paddle/phi/kernels/funcs/eigen/common.h"
#include "paddle/phi/kernels/funcs/eigen/eigen_function.h"

#define MAX_RANK_SUPPORTED 8

namespace phi {
Expand All @@ -39,10 +38,10 @@ void ExpandAs(const Context& context,
return;
}
for (size_t i = 0; i < vec_in_dims.size(); ++i) {
PADDLE_ENFORCE_NE(
target_shape[i],
0,
errors::InvalidArgument("The value of target shape cannot be zero."));
if (target_shape[i] == 0) {
context.template Alloc<T>(out);
return;
}
if (i < diff) {
PADDLE_ENFORCE_GT(
target_shape[i],
Expand Down Expand Up @@ -100,6 +99,10 @@ void ExpandAsKernel(const Context& dev_ctx,
const paddle::optional<DenseTensor>& y,
const std::vector<int64_t>& target_shape,
DenseTensor* out) {
if (x.numel() == 0 || (y.get_ptr() && y.get_ptr()->numel() == 0)) {
dev_ctx.template Alloc<T>(out);
return;
}
auto rank = x.dims().size();
auto target_rank = target_shape.size();
PADDLE_ENFORCE_GE(target_rank,
Expand Down
12 changes: 8 additions & 4 deletions paddle/phi/kernels/xpu/expand_as_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ void ExpandAs(const Context& context,
auto diff = target_shape.size() - vec_in_dims.size();
vec_in_dims.insert(vec_in_dims.begin(), diff, 1);
for (size_t i = 0; i < vec_in_dims.size(); ++i) {
PADDLE_ENFORCE_NE(target_shape[i],
0,
common::errors::InvalidArgument(
"The value of target shape cannot be zero."));
if (target_shape[i] == 0) {
context.template Alloc<T>(out);
return;
}
if (vec_in_dims[i] != 1) {
PADDLE_ENFORCE_EQ(
vec_in_dims[i],
Expand Down Expand Up @@ -89,6 +89,10 @@ void ExpandAsKernel(const Context& dev_ctx,
const paddle::optional<DenseTensor>& y,
const std::vector<int64_t>& target_shape,
DenseTensor* out) {
if (x.numel() == 0 || (y.get_ptr() && y.get_ptr()->numel() == 0)) {
dev_ctx.template Alloc<T>(out);
return;
}
auto rank = x.dims().size();
auto target_rank = target_shape.size();
PADDLE_ENFORCE_GE(target_rank,
Expand Down
26 changes: 26 additions & 0 deletions test/legacy_test/test_expand_as_v2_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,32 @@ def if_enable_cinn(self):
self.enable_cinn = False


class TestExpandAs_ZeroSize(TestExpandAsBasic):
def init_inputs_and_outputs(self):
x = np.random.random([2, 1]).astype(self.dtype)
target_tensor = np.random.random([2, 0]).astype(self.dtype)
self.inputs = {'X': x, "Y": target_tensor}
self.attrs = {'target_shape': target_tensor.shape}
output = np.random.random([2, 0]).astype(self.dtype)
self.outputs = {'Out': output}

def test_check_output(self):
self.check_output(check_pir=True)

def test_check_grad(self):
self.check_grad(['X'], 'Out', check_pir=True)


class TestExpandAs_ZeroSize2(TestExpandAs_ZeroSize):
def init_inputs_and_outputs(self):
x = np.random.random([3, 0]).astype(self.dtype)
target_tensor = np.random.random([3, 0]).astype(self.dtype)
self.inputs = {'X': x, "Y": target_tensor}
self.attrs = {'target_shape': target_tensor.shape}
output = np.random.random([3, 0]).astype(self.dtype)
self.outputs = {'Out': output}


@unittest.skipIf(
not core.is_compiled_with_cuda()
or not core.is_bfloat16_supported(core.CUDAPlace(0)),
Expand Down
3 changes: 2 additions & 1 deletion test/legacy_test/test_solve_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ def setUp(self):
self.place.append(paddle.CUDAPlace(0))

def check_static_result(self, place):
paddle.enable_static()
with base.program_guard(base.Program(), base.Program()):
paddle_input_x = paddle.static.data(
name="input_x", shape=[3, 3], dtype=self.dtype
Expand Down Expand Up @@ -968,7 +969,7 @@ def test_static(self):
self.check_static_result(
place=place,
x_shape=[10, 0, 0],
y_shape=[6, 0, 0],
y_shape=[10, 0, 0],
np_y_shape=[10, 0, 0],
)
with self.assertRaises(ValueError) as context:
Expand Down
Loading