Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【complex op】No.6 add complex support for logical_and/or/xor/not #56323

Merged
merged 12 commits into from
Aug 30, 2023
4 changes: 3 additions & 1 deletion paddle/phi/kernels/cpu/logical_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ void LogicalNotKernel(const Context& dev_ctx,
int64_t, \
int, \
int8_t, \
int16_t) { \
int16_t, \
phi::dtype::complex<float>, \
phi::dtype::complex<double>) { \
kernel->OutputAt(0).SetDataType(phi::DataType::BOOL); \
}

Expand Down
4 changes: 3 additions & 1 deletion paddle/phi/kernels/kps/logical_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ PD_REGISTER_KERNEL(logical_xor, KPS, ALL_LAYOUT, phi::LogicalXorKernel, int) {
int64_t, \
int, \
int8_t, \
int16_t) { \
int16_t, \
phi::dtype::complex<float>, \
phi::dtype::complex<double>) { \
kernel->OutputAt(0).SetDataType(phi::DataType::BOOL); \
}

Expand Down
18 changes: 11 additions & 7 deletions python/paddle/tensor/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ def _logical_op(op_name, x, y, out=None, name=None, binary_op=True):
"float32",
"float64",
"uint16",
"complex64",
"complex128",
],
op_name,
)
Expand All @@ -68,6 +70,8 @@ def _logical_op(op_name, x, y, out=None, name=None, binary_op=True):
"float32",
"float64",
"uint16",
"complex64",
"complex128",
],
op_name,
)
Expand Down Expand Up @@ -112,8 +116,8 @@ def logical_and(x, y, out=None, name=None):
.. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor

Args:
x (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, float16, float32, float64.
y (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, float16, float32, float64.
x (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, float16, float32, float64, complex64, complex128.
y (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, float16, float32, float64, complex64, complex128.
out(Tensor, optional): The ``Tensor`` that specifies the output of the operator, which can be any ``Tensor`` that has been created in the program. The default value is None, and a new ``Tensor`` will be created to save the output.
name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

Expand Down Expand Up @@ -154,8 +158,8 @@ def logical_or(x, y, out=None, name=None):
.. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor

Args:
x (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, float16, float32, float64.
y (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, float16, float32, float64.
x (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, float16, float32, float64, complex64, complex128.
y (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, float16, float32, float64, complex64, complex128.
out(Tensor): The ``Variable`` that specifies the output of the operator, which can be any ``Tensor`` that has been created in the program. The default value is None, and a new ``Tensor`` will be created to save the output.
name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

Expand Down Expand Up @@ -198,8 +202,8 @@ def logical_xor(x, y, out=None, name=None):
.. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor

Args:
x (Tensor): the input tensor, it's data type should be one of bool, int8, int16, int32, int64, float16, float32, float64.
y (Tensor): the input tensor, it's data type should be one of bool, int8, int16, int32, int64, float16, float32, float64.
x (Tensor): the input tensor, it's data type should be one of bool, int8, int16, int32, int64, float16, float32, float64, complex64, complex128.
y (Tensor): the input tensor, it's data type should be one of bool, int8, int16, int32, int64, float16, float32, float64, complex64, complex128.
out(Tensor): The ``Tensor`` that specifies the output of the operator, which can be any ``Tensor`` that has been created in the program. The default value is None, and a new ``Tensor`` will be created to save the output.
name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

Expand Down Expand Up @@ -243,7 +247,7 @@ def logical_not(x, out=None, name=None):
.. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor

Args:
x(Tensor): Operand of logical_not operator. Must be a Tensor of type bool, int8, int16, in32, in64, float16, float32, or float64.
x(Tensor): Operand of logical_not operator. Must be a Tensor of type bool, int8, int16, in32, in64, float16, float32, or float64, complex64, complex128.
out(Tensor): The ``Tensor`` that specifies the output of the operator, which can be any ``Tensor`` that has been created in the program. The default value is None, and a new ``Tensor` will be created to save the output.
name(str|None): The default value is None. Normally there is no need for users to set this property. For more information, please refer to :ref:`api_guide_Name`.

Expand Down
10 changes: 9 additions & 1 deletion test/legacy_test/test_logical_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
np.float16,
np.float32,
np.float64,
np.complex64,
np.complex128,
]

TEST_META_OP_DATA = [
Expand Down Expand Up @@ -124,6 +126,10 @@ def np_data_generator(np_shape, dtype, *args, **kwargs):
elif dtype == np.uint16:
x = np.random.uniform(0.0, 1.0, np_shape).astype(np.float32)
return convert_float_to_uint16(x)
elif dtype == np.complex64 or dtype == np.complex128:
return np.random.normal(0, 1, np_shape) + 1.0j * np.random.normal(
0, 1, np_shape
)
jinyouzhi marked this conversation as resolved.
Show resolved Hide resolved
else:
return np.random.normal(0, 1, np_shape).astype(dtype)

Expand Down Expand Up @@ -180,7 +186,9 @@ def check_type(op_str, x, y, binary_op):
y = paddle.to_tensor(y)
error_type = BaseException
if binary_op:
if type_str_map['x'] != type_str_map['y']:
if type_str_map['x'] != type_str_map['y'] and type_str_map[
'x'
] not in [np.complex64, np.complex128]:
jinyouzhi marked this conversation as resolved.
Show resolved Hide resolved
unit_test.assertRaises(error_type, op, x=x, y=y)
if not in_dynamic_mode():
error_type = TypeError
Expand Down