From 27a6631587772228f56e359852437d7a4e5655e8 Mon Sep 17 00:00:00 2001 From: zzk0 Date: Wed, 31 May 2023 00:58:46 +0000 Subject: [PATCH 1/2] op unittest for comparison op --- python/tests/ops/test_comparison_op.py | 357 +++++++++++++++++++++++++ 1 file changed, 357 insertions(+) create mode 100644 python/tests/ops/test_comparison_op.py diff --git a/python/tests/ops/test_comparison_op.py b/python/tests/ops/test_comparison_op.py new file mode 100644 index 0000000000..f3ef911f92 --- /dev/null +++ b/python/tests/ops/test_comparison_op.py @@ -0,0 +1,357 @@ +# Copyright (c) 2022 CINN Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import paddle +from cinn.frontend import * +from cinn.common import * +from op_test import OpTest, OpTestTool +from op_test_helper import TestCaseHelper + + +@OpTestTool.skip_if(not is_compiled_with_cuda(), + "x86 test will be skipped due to timeout.") +class TestComparisonOp(OpTest): + def setUp(self): + print(f"\nRunning {self.__class__.__name__}: {self.case}") + self.inputs = {} + self.prepare_inputs() + + def prepare_inputs(self): + if self.case["broadcast"]: + self.inputs = { + "x": self.random(self.case["x_shape"], self.case["dtype"]), + "y": self.random(self.case["y_shape"], self.case["dtype"]) + } + else: + self.inputs = { + "x": self.random(self.case["shape"], self.case["dtype"]), + "y": self.random(self.case["shape"], self.case["dtype"]) + } + self.operation = self.case["operation"] + + def build_paddle_program(self, target): + x = paddle.to_tensor(self.inputs["x"], stop_gradient=True) + y = paddle.to_tensor(self.inputs["y"], stop_gradient=True) + if self.operation == "equal": + out = paddle.equal(x, y) + elif self.operation == "not_equal": + out = paddle.not_equal(x, y) + elif self.operation == "greater_than": + out = paddle.greater_than(x, y) + elif self.operation == "less_than": + out = paddle.less_than(x, y) + elif self.operation == "greater_equal": + out = paddle.greater_equal(x, y) + elif self.operation == "less_equal": + out = paddle.less_equal(x, y) + else: + raise NotImplementedError + self.paddle_outputs = [out] + + def build_cinn_program(self, target): + builder = NetBuilder("select") + x = builder.create_input( + self.nptype2cinntype(self.inputs["x"].dtype), + self.inputs["x"].shape, "x") + y = builder.create_input( + self.nptype2cinntype(self.inputs["y"].dtype), + self.inputs["y"].shape, "y") + + if self.operation == "equal": + out = builder.equal(x, y) + elif self.operation == "not_equal": + out = builder.not_equal(x, y) + elif self.operation == "greater_than": + out = builder.greater_than(x, y) + elif self.operation == "less_than": + out = builder.less_than(x, y) + elif self.operation == "greater_equal": + out = builder.greater_equal(x, y) + elif self.operation == "less_equal": + out = builder.less_equal(x, y) + else: + raise NotImplementedError + prog = builder.build() + res = self.get_cinn_output( + prog, target, [x, y], [self.inputs["x"], self.inputs["y"]], [out]) + self.cinn_outputs = res + + def test_check_results(self): + self.check_outputs_and_grads(all_equal=True) + + +class TestComparisonOpShape(TestCaseHelper): + def init_attrs(self): + self.class_name = "TestComparisonOpShape" + self.cls = TestComparisonOp + self.inputs = [ + { + "shape": [64], + }, + { + "shape": [64, 32], + }, + { + "shape": [64, 1], + }, + { + "shape": [64, 32, 128], + }, + { + "shape": [1, 32, 128], + }, + { + "shape": [64, 32, 16, 32], + }, + { + "shape": [64, 32, 1, 32], + }, + { + "shape": [64, 32, 16, 1, 128], + }, + { + "shape": [1], + }, + { + "shape": [1, 1], + }, + { + "shape": [1, 1, 1], + }, + { + "shape": [1, 1, 1, 1], + }, + { + "shape": [1, 1, 1, 1, 1], + }, + { + "shape": [1, 1, 1024, 1, 1], + }, + { + "shape": [65536], + }, + { + "shape": [131072], + }, + { + "shape": [1048576] + }, + { + "shape": [64, 32, 16, 8, 4], + }, + ] + self.dtypes = [ + { + "dtype": "float32" + }, + ] + self.attrs = [ + { + "operation": "equal", + "broadcast": False + }, + { + "operation": "not_equal", + "broadcast": False + }, + { + "operation": "greater_than", + "broadcast": False + }, + { + "operation": "less_than", + "broadcast": False + }, + { + "operation": "greater_equal", + "broadcast": False + }, + { + "operation": "less_equal", + "broadcast": False + }, + ] + + +class TestComparisonOpDtype(TestCaseHelper): + def init_attrs(self): + self.class_name = "TestComparisonOpDtype" + self.cls = TestComparisonOp + self.inputs = [ + { + "shape": [64, 1, 128], + }, + { + "shape": [64, 32, 1], + }, + ] + self.dtypes = [ + { + "dtype": "float16" + }, + { + "dtype": "float32" + }, + { + "dtype": "float64" + }, + { + "dtype": "bool" + }, + { + "dtype": "int32" + }, + { + "dtype": "int64" + }, + ] + self.attrs = [ + { + "operation": "equal", + "broadcast": False + }, + { + "operation": "not_equal", + "broadcast": False + }, + { + "operation": "greater_than", + "broadcast": False + }, + { + "operation": "less_than", + "broadcast": False + }, + { + "operation": "greater_equal", + "broadcast": False + }, + { + "operation": "less_equal", + "broadcast": False + }, + ] + + +class TestComparisonOpBroadcastTest(TestCaseHelper): + def init_attrs(self): + self.class_name = "TestComparisonOpShapeTest" + self.cls = TestComparisonOp + self.inputs = [ + { + "x_shape": [64], + "y_shape": [1], + }, + { + "x_shape": [1], + "y_shape": [64], + }, + { + "x_shape": [64, 32], + "y_shape": [64, 1], + }, + { + "x_shape": [1, 1], + "y_shape": [64, 32], + }, + { + "x_shape": [64, 1], + "y_shape": [1, 32], + }, + { + "x_shape": [64, 1, 128], + "y_shape": [64, 32, 128], + }, + { + "x_shape": [64, 32, 128], + "y_shape": [64, 32, 1], + }, + { + "x_shape": [64, 1, 128], + "y_shape": [1, 32, 128], + }, + { + "x_shape": [1, 1, 1], + "y_shape": [64, 32, 128], + }, + { + "x_shape": [64, 1, 16, 32], + "y_shape": [64, 32, 16, 32], + }, + { + "x_shape": [64, 32, 16, 32], + "y_shape": [64, 32, 1, 32], + }, + { + "x_shape": [64, 1, 1, 32], + "y_shape": [64, 32, 16, 32], + }, + { + "x_shape": [64, 32, 16, 1], + "y_shape": [64, 1, 16, 32], + }, + { + "x_shape": [1, 1, 1, 1], + "y_shape": [64, 32, 16, 32], + }, + { + "x_shape": [1, 32, 16, 32], + "y_shape": [64, 32, 16, 32], + }, + { + "x_shape": [64, 32, 16, 32], + "y_shape": [64, 32, 16, 32], + }, + { + "x_shape": [65536], + "y_shape": [1], + }, + ] + self.dtypes = [ + { + "dtype": "float32" + }, + ] + self.attrs = [ + { + "operation": "equal", + "broadcast": True + }, + { + "operation": "not_equal", + "broadcast": True + }, + { + "operation": "greater_than", + "broadcast": True + }, + { + "operation": "less_than", + "broadcast": True + }, + { + "operation": "greater_equal", + "broadcast": True + }, + { + "operation": "less_equal", + "broadcast": True + }, + ] + + +if __name__ == "__main__": + TestComparisonOpShape().run() + TestComparisonOpDtype().run() + TestComparisonOpBroadcastTest().run() From 91dd07b9e212572738c6a5d0af0302b1860982f8 Mon Sep 17 00:00:00 2001 From: zzk0 Date: Wed, 31 May 2023 01:18:04 +0000 Subject: [PATCH 2/2] format --- python/tests/ops/test_comparison_op.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/tests/ops/test_comparison_op.py b/python/tests/ops/test_comparison_op.py index f3ef911f92..fb1578dd10 100644 --- a/python/tests/ops/test_comparison_op.py +++ b/python/tests/ops/test_comparison_op.py @@ -83,8 +83,8 @@ def build_cinn_program(self, target): else: raise NotImplementedError prog = builder.build() - res = self.get_cinn_output( - prog, target, [x, y], [self.inputs["x"], self.inputs["y"]], [out]) + res = self.get_cinn_output(prog, target, [x, y], + [self.inputs["x"], self.inputs["y"]], [out]) self.cinn_outputs = res def test_check_results(self):