Throws TypeError When Logical APIs Do Broadcast Operation (#26490)

As the title.

Co-authored-by: wujionghao <wujionghao@email.com>
test_feature_precision_test_c
joejiong 5 years ago committed by GitHub
parent 407de03905
commit da1efe24d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -12146,7 +12146,10 @@ def logical_and(x, y, out=None, name=None):
res = paddle.logical_and(x, y)
print(res.numpy()) # [True False False False]
"""
if x.shape != y.shape:
raise TypeError(
'Input tensors must be same shape, but received x \'s shape: %s, y \'s shape: %s '
% (x.shape, y.shape))
return _logical_op(
op_name="logical_and", x=x, y=y, name=name, out=out, binary_op=True)
@ -12188,7 +12191,10 @@ def logical_or(x, y, out=None, name=None):
res = paddle.logical_or(x, y)
print(res.numpy()) # [True True True False]
"""
if x.shape != y.shape:
raise TypeError(
'Input tensors must be same shape, but received x \'s shape: %s, y \'s shape: %s '
% (x.shape, y.shape))
return _logical_op(
op_name="logical_or", x=x, y=y, name=name, out=out, binary_op=True)
@ -12230,7 +12236,10 @@ def logical_xor(x, y, out=None, name=None):
res = paddle.logical_xor(x, y)
print(res.numpy()) # [False True True False]
"""
if x.shape != y.shape:
raise TypeError(
'Input tensors must be same shape, but received x \'s shape: %s, y \'s shape: %s '
% (x.shape, y.shape))
return _logical_op(
op_name="logical_xor", x=x, y=y, name=name, out=out, binary_op=True)

@ -17,8 +17,9 @@ from __future__ import print_function
import op_test
import unittest
import numpy as np
import paddle
import paddle.fluid as fluid
from paddle.fluid import Program, program_guard
from paddle.static import Program, program_guard
def create_test_class(op_type, callback, binary_op=True):
@ -42,6 +43,8 @@ def create_test_class(op_type, callback, binary_op=True):
def test_error(self):
with program_guard(Program(), Program()):
# test 1 type error, x, y must be bool type
x = fluid.layers.data(name='x', shape=[2], dtype='bool')
y = fluid.layers.data(name='y', shape=[2], dtype='bool')
a = fluid.layers.data(name='a', shape=[2], dtype='int32')
@ -54,7 +57,16 @@ def create_test_class(op_type, callback, binary_op=True):
self.assertRaises(TypeError, op, x=x, out=1)
self.assertRaises(TypeError, op, x=a)
Cls.__name__ = op_type
# test 2 type error, x, y must be same shape
x_data = fluid.layers.data(
name='x_data', shape=[2], dtype='bool')
y_data = fluid.layers.data(
name='y_data', shape=[2, 2], dtype='bool')
if self.op_type != "logical_not":
self.assertRaises(TypeError, op, x=x_data, y=y_data, out=1)
self.assertRaises(TypeError, op, x=y_data, y=x_data)
globals()[op_type] = Cls

Loading…
Cancel
Save