Skip to content
Merged
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
4 changes: 4 additions & 0 deletions paddle/phi/kernels/cpu/reduce_amax_grad_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ void ReduceAMaxGradKernel(const Context& dev_ctx,
bool keep_dim,
bool reduce_all,
DenseTensor* x_grad) {
if (x_grad && x_grad->numel() == 0) {
dev_ctx.template Alloc<T>(x_grad);
return;
}
reduce_all = recompute_reduce_all(x, dims, reduce_all);
ReduceGradKernel<Context, T, funcs::AMaxOrAMinGradFunctor>(
dev_ctx, x, out, out_grad, dims, keep_dim, reduce_all, x_grad);
Expand Down
4 changes: 4 additions & 0 deletions paddle/phi/kernels/cpu/reduce_amin_grad_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ void ReduceAMinGradKernel(const Context& dev_ctx,
bool keep_dim,
bool reduce_all,
DenseTensor* x_grad) {
if (x_grad && x_grad->numel() == 0) {
dev_ctx.template Alloc<T>(x_grad);
return;
}
reduce_all = recompute_reduce_all(x, dims, reduce_all);
ReduceGradKernel<Context, T, funcs::AMaxOrAMinGradFunctor>(
dev_ctx, x, out, out_grad, dims, keep_dim, reduce_all, x_grad);
Expand Down
8 changes: 3 additions & 5 deletions paddle/phi/kernels/funcs/reduce_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -1460,12 +1460,10 @@ void ReduceKernelImpl(const Context& dev_ctx,
const std::vector<int64_t>& dims,
bool keep_dim,
bool reduce_all) {
PADDLE_ENFORCE_GT(input.numel(),
0,
common::errors::InvalidArgument(
"Tensor need be reduced must not empty."));

dev_ctx.template Alloc<OutT>(output);
if (input.numel() == 0) {
return;
}

if (reduce_all) {
// Flatten and reduce 1-D tensor
Expand Down
4 changes: 4 additions & 0 deletions paddle/phi/kernels/xpu/reduce_max_grad_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ void ReduceMaxGradKernel(const Context& dev_ctx,
bool keep_dim,
bool reduce_all,
DenseTensor* x_grad) {
if (x_grad && x_grad->numel() == 0) {
dev_ctx.template Alloc<T>(x_grad);
return;
}
using XPUDataType = typename XPUTypeTrait<T>::Type;
reduce_all = recompute_reduce_all(x, dims_arr, reduce_all);
auto dims = dims_arr.GetData();
Expand Down
4 changes: 4 additions & 0 deletions paddle/phi/kernels/xpu/reduce_min_grad_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ void ReduceMinGradKernel(const Context& dev_ctx,
bool keep_dim,
bool reduce_all,
DenseTensor* x_grad) {
if (x_grad && x_grad->numel() == 0) {
dev_ctx.template Alloc<T>(x_grad);
return;
}
reduce_all = recompute_reduce_all(x, dims_arr, reduce_all);
auto dims = dims_arr.GetData();

Expand Down
20 changes: 20 additions & 0 deletions test/legacy_test/test_max_min_amax_amin_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def _choose_paddle_func(self, func, x):

def test_static_graph(self):
def _test_static_graph(func):
paddle.enable_static()
startup_program = base.Program()
train_program = base.Program()
with base.program_guard(startup_program, train_program):
Expand All @@ -107,6 +108,7 @@ def _test_static_graph(func):
fetch_list=[out],
)
self.assertTrue((np.array(res[0]) == self.np_out[func]).all())
paddle.disable_static()

_test_static_graph('amax')
_test_static_graph('amin')
Expand Down Expand Up @@ -264,5 +266,23 @@ def _test_dygraph(func):
_test_dygraph('min')


class TestMaxMinAmaxAminAPI_ZeroSize(TestMaxMinAmaxAminAPI):
def init_case(self):
self.x_np = np.random.randn(1, 0, 10).astype(np.float32)
self.shape = [1, 0, 10]
self.dtype = 'float32'
self.axis = 0
self.keepdim = False


class TestMaxMinAmaxAminAPI_ZeroSize2(TestMaxMinAmaxAminAPI):
def init_case(self):
self.x_np = np.random.randn(1, 0, 10).astype(np.float32)
self.shape = [1, 0, 10]
self.dtype = 'float32'
self.axis = -1
self.keepdim = True


if __name__ == '__main__':
unittest.main()
Loading