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/reshape_grad_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ void ReshapeGradKernel(const Context& dev_ctx,
const DenseTensor& x,
const DenseTensor& out_grad,
DenseTensor* x_grad) {
if (x_grad->numel() == 0) {
dev_ctx.Alloc(x_grad, x_grad->dtype());
return;
}
// NOTE: [Why not to use x.dims() ?]
// Because inplace strategy is different between old IR and PIR,
// we need fix it into x.dims() after cleaning old IR system.
Expand Down
2 changes: 2 additions & 0 deletions paddle/phi/kernels/reshape_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ void ReshapeKernel(const Context& dev_ctx,
return;
}
dev_ctx.Alloc(out, x.dtype());
if (out->numel() == 0) return;

// TODO(chenweihang): the output dims are overwrite after copying,
// here we need to use copy method that only copy data
auto dims = out->dims();
Expand Down
29 changes: 29 additions & 0 deletions test/legacy_test/test_reshape_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,35 @@ def init_data(self):
self.inferred_shape = ()


class TestReshapeOp_ZeroSize(OpTest):
def init_data(self):
self.ori_shape = (0, 2)
self.new_shape = (2, 0)
self.inferred_shape = (2, 0)

def setUp(self):
self.init_data()
self.op_type = "reshape2"
self.python_api = paddle.tensor.reshape
self.public_python_api = paddle.tensor.reshape
self.python_out_sig = ['Out']
self.inputs = {"X": np.random.random(self.ori_shape).astype("float32")}
self.attrs = {"shape": self.new_shape}
self.outputs = {
"Out": self.inputs["X"].reshape(self.inferred_shape),
'XShape': np.random.random(self.ori_shape).astype("float32"),
}

def test_check_output(self):
self.check_output(no_check_set=['XShape'])

def test_check_grad(self):
self.check_grad(
["X"],
"Out",
)


@unittest.skipIf(
not paddle.is_compiled_with_cuda() or paddle.is_compiled_with_rocm(),
"BFP16 test runs only on CUDA",
Expand Down