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
20 changes: 14 additions & 6 deletions paddle/phi/infermeta/binary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2015,12 +2015,14 @@ void GatherInferMeta(const MetaTensor& x,
auto index_dims = index.dims();

if (index_dims.size() == 2) {
PADDLE_ENFORCE_EQ(
index_dims[1],
1,
common::errors::InvalidArgument(
"The last dim of index should be 1 when it is 2D, but we get %d",
index_dims[1]));
if (index_dims[1] != 0) {
PADDLE_ENFORCE_EQ(
index_dims[1],
1,
common::errors::InvalidArgument("The last dim of index should be 0 "
"or 1 when it is 2D, but we get %d",
index_dims[1]));
}
} else {
PADDLE_ENFORCE_EQ(
index_dims.size() == 1 || index_dims.size() == 0,
Expand Down Expand Up @@ -2089,13 +2091,19 @@ void GatherInferMeta(const MetaTensor& x,
if (axis.FromTensor() || axis_v == 0) {
// if axis.FromTensor(), we can not obtain correct shape of output
int batch_size = static_cast<int>(index_dims[0]);
if (index_dims.size() == 2 && index_dims[1] == 0) {
batch_size = 0;
}
phi::DDim output_dims(input_dim);
output_dims[0] = batch_size;
out->set_dims(output_dims);
out->set_dtype(x.dtype());
out->share_lod(x);
} else {
int index_size = static_cast<int>(index_dims[0]);
if (index_dims.size() == 2 && index_dims[1] == 0) {
index_size = 0;
}
std::vector<int> out_dim_vec;
for (int i = 0; i < axis_v; i++) {
out_dim_vec.push_back(input_dim[i]); // NOLINT
Expand Down
8 changes: 8 additions & 0 deletions paddle/phi/kernels/cpu/gather_grad_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "paddle/phi/common/bfloat16.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/full_kernel.h"
#include "paddle/phi/kernels/funcs/eigen/common.h"
#include "paddle/phi/kernels/funcs/gather.h"
#include "paddle/phi/kernels/funcs/scatter.h"
Expand All @@ -29,6 +30,13 @@ void GatherGradKernel(const Context& dev_ctx,
const DenseTensor& out_grad,
const Scalar& axis,
DenseTensor* x_grad) {
if (out_grad.numel() == 0) {
if (x_grad) {
phi::Full<T, Context>(
dev_ctx, phi::IntArray(common::vectorize(x_grad->dims())), 0, x_grad);
}
return;
}
const auto& index_type = index.dtype();
auto axis_v = axis.to<int>();
if (axis_v < 0) {
Expand Down
4 changes: 4 additions & 0 deletions paddle/phi/kernels/cpu/gather_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ void GatherKernel(const Context& dev_ctx,
const DenseTensor& index,
const Scalar& axis,
DenseTensor* out) {
if (out && out->numel() == 0) {
dev_ctx.template Alloc<T>(out);
return;
}
const auto& index_type = index.dtype();
auto axis_v = axis.to<int>();
if (axis_v < 0) {
Expand Down
10 changes: 9 additions & 1 deletion paddle/phi/kernels/gpu/gather_grad_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
#include "paddle/phi/common/bfloat16.h"
#include "paddle/phi/common/float16.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/full_kernel.h"
#include "paddle/phi/kernels/funcs/eigen/common.h"
#include "paddle/phi/kernels/funcs/gather.cu.h"
#include "paddle/phi/kernels/funcs/scatter.cu.h"
#include "paddle/phi/kernels/gather_kernel.h"

namespace phi {

template <typename T, typename Context>
Expand All @@ -29,6 +29,14 @@ void GatherGradKernel(const Context& dev_ctx,
const DenseTensor& out_grad,
const Scalar& axis,
DenseTensor* x_grad) {
// x [4, 2], index [2, 0], out [2, 0], x_grad [4, 2]
if (out_grad.numel() == 0) {
if (x_grad) {
phi::Full<T, Context>(
dev_ctx, phi::IntArray(common::vectorize(x_grad->dims())), 0, x_grad);
}
return;
}
const auto& index_type = index.dtype();
auto axis_v = axis.to<int>();
if (axis_v < 0) {
Expand Down
4 changes: 4 additions & 0 deletions paddle/phi/kernels/gpu/gather_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ void GatherKernel(const Context& dev_ctx,
const DenseTensor& index,
const Scalar& axis,
DenseTensor* out) {
if (out && out->numel() == 0) {
dev_ctx.template Alloc<T>(out);
return;
}
const auto& index_type = index.dtype();
auto axis_v = axis.to<int>();
if (axis_v < 0) {
Expand Down
6 changes: 5 additions & 1 deletion paddle/phi/kernels/xpu/gather_grad_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#include "paddle/phi/backends/xpu/enforce_xpu.h"
#include "paddle/phi/core/kernel_registry.h"

#include "paddle/phi/kernels/full_kernel.h"
namespace phi {

template <typename T, typename Context>
Expand All @@ -34,6 +34,10 @@ void GatherGradKernel(const Context& dev_ctx,
const auto& index_type = index.dtype();

if (out_grad.numel() == 0) {
if (x_grad) {
phi::Full<T, Context>(
dev_ctx, phi::IntArray(common::vectorize(x_grad->dims())), 0, x_grad);
}
return;
}

Expand Down
40 changes: 40 additions & 0 deletions test/legacy_test/test_gather_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,46 @@ def test_gather_backward(self):
np.testing.assert_allclose(res_list[0], res_list[1])


class TestGatherOp_ZeroSize(OpTest):
def setUp(self):
self.op_type = "gather"
self.python_api = paddle.gather
self.public_python_api = paddle.gather
self.config()
self.init_inputs_and_outputs()

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

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

def config(self):
self.x_shape = (3, 0, 4)
self.config_dtype()
self.index = [2]
self.index_type = "int32"

def config_dtype(self):
self.x_type = "float64"

def init_inputs_and_outputs(self):
xnp = np.random.random(self.x_shape).astype(self.x_type)
self.inputs = {
'X': xnp,
'Index': np.array(self.index).astype(self.index_type),
}
self.outputs = {'Out': self.inputs["X"][self.inputs["Index"]]}


class TestGatherOp_ZeroSize2(TestGatherOp_ZeroSize):
def config(self):
self.x_shape = (10, 20)
self.config_dtype()
self.index = [2, 0]
self.index_type = "int32"


if __name__ == "__main__":
paddle.enable_static()
unittest.main()
Loading