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
13 changes: 12 additions & 1 deletion paddle/phi/kernels/set_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
#include "paddle/phi/kernels/set_kernel.h"
#include "paddle/phi/core/kernel_registry.h"

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

template <typename T, typename Context>
Expand All @@ -28,6 +28,17 @@ void SetKernel(const Context& dev_ctx,
meta.dims = DDim(dims.data(), static_cast<int>(dims.size()));
meta.strides = DDim(stride.data(), static_cast<int>(stride.size()));
meta.offset = offset;
if (x.numel() == 0 || source.numel() == 0) {
if (source.numel() != 0) {
out->clear();
*out = DenseTensor{source.Holder(), meta};
} else if (x.numel() == 0) {
phi::Full<T, Context>(
dev_ctx, phi::IntArray(common::vectorize(out->dims())), 0, out);
}
out->ShareInplaceVersionCounterWith(x);
return;
}
if (x.IsSharedWith(source)) {
out->set_meta(meta);
} else {
Expand Down
12 changes: 12 additions & 0 deletions test/legacy_test/test_inplace.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import unittest

import numpy as np
from op_test import get_places

import paddle

Expand Down Expand Up @@ -2491,5 +2492,16 @@ def test_inplace_api(self):
self.assertTrue(id(x) == id(inplace_x2))


class TestSet_API_ZeroSize(unittest.TestCase):
def setUp(self):
self.places = get_places()

def test_set_api(self):
for place in self.places:
with paddle.base.dygraph.guard(place):
out = paddle.randn([20]).set_(paddle.randn([0, 3]), [20], [2])
np.testing.assert_allclose(out.shape, [20])


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