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
12 changes: 12 additions & 0 deletions paddle/phi/kernels/cpu/index_put_grad_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,18 @@ void IndexPutGradKernel(const Context& dev_ctx,
bool accumulate,
DenseTensor* x_grad,
DenseTensor* value_grad) {
if (out_grad.numel() == 0) {
dev_ctx.template Alloc<T>(x_grad);
// Fill value_grad with 0.
if (value_grad) {
phi::Full<T, Context>(
dev_ctx,
phi::IntArray(common::vectorize(value_grad->dims())),
0,
value_grad);
}
return;
}
PADDLE_ENFORCE_EQ(
x.dtype(),
value.dtype(),
Expand Down
4 changes: 4 additions & 0 deletions paddle/phi/kernels/cpu/index_put_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ void IndexPutKernel(const Context& dev_ctx,
const DenseTensor& value,
bool accumulate,
DenseTensor* out) {
if (out && out->numel() == 0) {
dev_ctx.template Alloc<T>(out);
return;
}
PADDLE_ENFORCE_EQ(
x.dtype(),
value.dtype(),
Expand Down
13 changes: 13 additions & 0 deletions paddle/phi/kernels/gpu/index_put_grad_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,19 @@ void IndexPutGradKernel(const Context& dev_ctx,
bool accumulate,
DenseTensor* x_grad,
DenseTensor* value_grad) {
if (out_grad.numel() == 0) {
dev_ctx.template Alloc<T>(x_grad);
// Fill value_grad with 0.
if (value_grad) {
phi::Full<T, Context>(
dev_ctx,
phi::IntArray(common::vectorize(value_grad->dims())),
0,
value_grad);
}
return;
}

PADDLE_ENFORCE_EQ(
x.dtype(),
value.dtype(),
Expand Down
4 changes: 4 additions & 0 deletions paddle/phi/kernels/gpu/index_put_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ void IndexPutKernel(const Context& dev_ctx,
const DenseTensor& value,
bool accumulate,
DenseTensor* out) {
if (out && out->numel() == 0) {
dev_ctx.template Alloc<T>(out);
return;
}
PADDLE_ENFORCE_EQ(
x.dtype(),
value.dtype(),
Expand Down
12 changes: 12 additions & 0 deletions paddle/phi/kernels/xpu/index_put_grad_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ void IndexPutGradKernel(const Context& dev_ctx,
bool accumulate,
DenseTensor* x_grad,
DenseTensor* value_grad) {
if (out_grad.numel() == 0) {
dev_ctx.template Alloc<T>(x_grad);
// Fill value_grad with 0.
if (value_grad) {
phi::Full<T, Context>(
dev_ctx,
phi::IntArray(common::vectorize(value_grad->dims())),
0,
value_grad);
}
return;
}
PADDLE_ENFORCE_EQ(
x.dtype(),
value.dtype(),
Expand Down
4 changes: 4 additions & 0 deletions paddle/phi/kernels/xpu/index_put_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ void IndexPutKernel(const Context& dev_ctx,
const DenseTensor& value,
bool accumulate,
DenseTensor* out) {
if (out && out->numel() == 0) {
dev_ctx.template Alloc<T>(out);
return;
}
PADDLE_ENFORCE_EQ(
x.dtype(),
value.dtype(),
Expand Down
58 changes: 58 additions & 0 deletions test/legacy_test/test_index_put_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -1028,5 +1028,63 @@ def init_dtype_type(self):
self.index_type_pd1 = "bool"


class TestIndexPutAPI_ZeroSize(unittest.TestCase):
def setUp(self):
self.init_dtype_type()
self.setPlace()

def init_dtype_type(self):
self.dtype_np = np.float32
self.index_type_np = np.int64
self.x_shape = (10, 0)
self.indices_shapes = [[10]]
self.value_shape = [1, 1]
self.dtype_pd = paddle.float32
self.index_type_pd = paddle.int64

def setPlace(self):
self.place = []
if (
os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower()
in ['1', 'true', 'on']
or not paddle.is_compiled_with_cuda()
):
self.place.append('cpu')
if self.dtype_np is np.float16:
self.place = []
if paddle.is_compiled_with_cuda():
self.place.append('gpu')

def test_dygraph_forward(self):
paddle.disable_static()
for place in self.place:
paddle.device.set_device(place)
x_pd = paddle.randn(self.x_shape, dtype=self.dtype_pd)
x_np = x_pd.numpy()
value_pd = paddle.randn(self.value_shape, dtype=self.dtype_pd)
value_np = value_pd.numpy()
x_pd.stop_gradient = False
value_pd.stop_gradient = False
indices_pd = [
paddle.randn(indices_shape).astype(dtype=self.index_type_pd)
for indices_shape in self.indices_shapes
]
indices_np = [item.numpy() for item in indices_pd]
indices_pd = tuple(indices_pd)
accumulate = False
ref_res = compute_index_put_ref(
x_np, indices_np, value_np, accumulate
)
pd_res = paddle.index_put(x_pd, indices_pd, value_pd, accumulate)
np.testing.assert_allclose(ref_res, pd_res.numpy(), atol=1e-7)

# check grad
pd_res.sum().backward()
np.testing.assert_allclose(x_pd.grad.shape, x_pd.shape)
np.testing.assert_allclose(
value_pd.grad.numpy(), np.zeros(value_pd.shape)
)


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