From da1efe24d5437f6945196c6a3783db2e0b48e49d Mon Sep 17 00:00:00 2001 From: joejiong Date: Mon, 24 Aug 2020 17:35:49 +0800 Subject: [PATCH] Throws TypeError When Logical APIs Do Broadcast Operation (#26490) As the title. Co-authored-by: wujionghao --- python/paddle/fluid/layers/nn.py | 15 ++++++++++++--- .../fluid/tests/unittests/test_logical_op.py | 16 ++++++++++++++-- 2 files changed, 26 insertions(+), 5 deletions(-) mode change 100644 => 100755 python/paddle/fluid/layers/nn.py mode change 100644 => 100755 python/paddle/fluid/tests/unittests/test_logical_op.py diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py old mode 100644 new mode 100755 index a9db201132..a6914fb26d --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -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) diff --git a/python/paddle/fluid/tests/unittests/test_logical_op.py b/python/paddle/fluid/tests/unittests/test_logical_op.py old mode 100644 new mode 100755 index 8f0049a8d3..b26b6ab6c3 --- a/python/paddle/fluid/tests/unittests/test_logical_op.py +++ b/python/paddle/fluid/tests/unittests/test_logical_op.py @@ -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 -- GitLab