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
15 changes: 15 additions & 0 deletions paddle/phi/kernels/stride/as_complex_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ void AsComplexStridedKernel(const Context& dev_ctx,
"FLAGS_use_stride_kernel is closed. Strided kernel "
"be called, something wrong has happened!"));
}
if (out && out->numel() == 0) {
if (x.dtype() == DataType::FLOAT32) {
out->set_type(DataType::COMPLEX64);
} else if (x.dtype() == DataType::FLOAT64) {
out->set_type(DataType::COMPLEX128);
} else {
PADDLE_THROW(common::errors::Unimplemented(
"as_complex is not supported data type (%s).",
DataTypeToString(x.dtype())));
}
out->set_offset(x.offset());
out->ResetHolder(x.Holder());
out->ShareInplaceVersionCounterWith(x);
return;
}

PADDLE_ENFORCE_EQ(
x.strides()[x.strides().size() - 1],
Expand Down
31 changes: 31 additions & 0 deletions test/legacy_test/test_sgn.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,37 @@ def test_float_static_and_pir(self):
z_expected = np_sgn(np_x)
np.testing.assert_allclose(z, z_expected, rtol=1e-05)

def test_zero_size_complex_dynamic(self):
for dtype in ['complex64', 'complex128']:
np_x = np.empty((0, 4), dtype=dtype) # 空张量 shape=[0, 4]
x = paddle.to_tensor(np_x)
z = paddle.sgn(x)
np_z = z.numpy()
z_expected = np_sgn(np_x)
np.testing.assert_allclose(np_z, z_expected, rtol=1e-05)
np.testing.assert_equal(np_z.shape, (0, 4))

def test_zero_size_complex_static_and_pir(self):
with static_guard():
for dtype in ['complex64', 'complex128']:
exe = paddle.static.Executor()
train_program = paddle.static.Program()
startup_program = paddle.static.Program()
with paddle.static.program_guard(
train_program, startup_program
):
x = paddle.static.data(name='X', shape=[0, 4], dtype=dtype)
z = paddle.sgn(x)

exe.run(startup_program)
x_np = np.empty((0, 4), dtype=dtype)
(z_out,) = exe.run(
train_program, feed={"X": x_np}, fetch_list=[z]
)
z_expected = np_sgn(x_np)
np.testing.assert_allclose(z_out, z_expected, rtol=1e-05)
np.testing.assert_equal(z_out.shape, (0, 4))


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