From bfcc723e943c492f9e56d3ceeb39761369e22716 Mon Sep 17 00:00:00 2001 From: Fisher Date: Tue, 6 Jun 2023 09:04:20 +0000 Subject: [PATCH] Refactor op test on remainder, uniform and gaussian --- python/tests/ops/test_gaussian_random_op.py | 139 +++++++++--- python/tests/ops/test_remainder_op.py | 236 +++++++++++++------- python/tests/ops/test_uniform_random_op.py | 144 +++++++++--- 3 files changed, 370 insertions(+), 149 deletions(-) diff --git a/python/tests/ops/test_gaussian_random_op.py b/python/tests/ops/test_gaussian_random_op.py index 776ee6548f..c5643c6cd3 100644 --- a/python/tests/ops/test_gaussian_random_op.py +++ b/python/tests/ops/test_gaussian_random_op.py @@ -14,9 +14,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -import unittest -import numpy as np from op_test import OpTest, OpTestTool +from op_test_helper import TestCaseHelper import paddle import cinn from cinn.frontend import * @@ -27,53 +26,133 @@ "x86 test will be skipped due to timeout.") class TestGaussianRandomOp(OpTest): def setUp(self): - self.init_case() - - def init_case(self): - self.shape = [2, 3] - self.mean = 0.0 - self.std = 1.0 - self.seed = 10 - self.dtype = "float32" + # print(f"\n{self.__class__.__name__}: {self.case}") + pass def build_paddle_program(self, target): out = paddle.tensor.random.gaussian( - shape=self.shape, mean=self.mean, std=self.std, dtype=self.dtype) + shape=self.case["shape"], + mean=self.case["mean"], + std=self.case["std"], + dtype=self.case["dtype"]) self.paddle_outputs = [out] def build_cinn_program(self, target): builder = NetBuilder("gaussian_random") - out = builder.gaussian_random(self.shape, self.mean, self.std, - self.seed, self.dtype) + out = builder.gaussian_random(self.case["shape"], self.case["mean"], + self.case["std"], self.case["seed"], + self.case["dtype"]) prog = builder.build() res = self.get_cinn_output(prog, target, [], [], [out], passes=[]) - self.cinn_outputs = [res[0]] + self.cinn_outputs = res def test_check_results(self): # Due to the different random number generation numbers implemented # in the specific implementation, the random number results generated # by CINN and Paddle are not the same, but they all conform to the # Uniform distribution. - self.check_outputs_and_grads(max_relative_error=10000) + self.check_outputs_and_grads( + max_relative_error=10000, max_absolute_error=10000) + + +class TestGaussianRandomOpShape(TestCaseHelper): + def init_attrs(self): + self.class_name = "TestGaussianRandomOpCase" + self.cls = TestGaussianRandomOp + self.inputs = [ + { + "shape": [1], + }, + { + "shape": [1024], + }, + { + "shape": [512, 256], + }, + { + "shape": [128, 64, 32], + }, + { + "shape": [16, 8, 4, 2], + }, + { + "shape": [16, 8, 4, 2, 1], + }, + ] + self.dtypes = [ + { + "dtype": "float32", + }, + ] + self.attrs = [ + { + "mean": 0.0, + "std": 0.0, + "seed": 1234, + }, + ] -class TestGaussianRandomCase1(TestGaussianRandomOp): - def init_case(self): - self.shape = [2, 3, 4] - self.mean = 1.0 - self.std = 2.0 - self.seed = 10 - self.dtype = "float32" +class TestGaussianRandomOpDtype(TestCaseHelper): + def init_attrs(self): + self.class_name = "TestGaussianRandomOpCase" + self.cls = TestGaussianRandomOp + self.inputs = [ + { + "shape": [1024], + }, + ] + self.dtypes = [ + { + "dtype": "float32", + }, + { + "dtype": "float64", + }, + ] + self.attrs = [ + { + "mean": 0.0, + "std": 0.0, + "seed": 1234, + }, + ] -class TestGaussianRandomCase2(TestGaussianRandomOp): - def init_case(self): - self.shape = [2, 3, 4] - self.mean = 2.0 - self.std = 3.0 - self.seed = 10 - self.dtype = "float64" +class TestGaussianRandomOpAttr(TestCaseHelper): + def init_attrs(self): + self.class_name = "TestGaussianRandomOpCase" + self.cls = TestGaussianRandomOp + self.inputs = [ + { + "shape": [1024], + }, + ] + self.dtypes = [ + { + "dtype": "float32", + }, + ] + self.attrs = [ + { + "mean": 1.0, + "std": 0.0, + "seed": 1, + }, + { + "mean": 0.0, + "std": 1.0, + "seed": 2, + }, + { + "mean": 1.0, + "std": 1.0, + "seed": 3, + }, + ] if __name__ == "__main__": - unittest.main() + TestGaussianRandomOpShape().run() + TestGaussianRandomOpDtype().run() + TestGaussianRandomOpAttr().run() diff --git a/python/tests/ops/test_remainder_op.py b/python/tests/ops/test_remainder_op.py index e183199c21..58532d588f 100644 --- a/python/tests/ops/test_remainder_op.py +++ b/python/tests/ops/test_remainder_op.py @@ -14,11 +14,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import unittest import numpy as np from op_test import OpTest, OpTestTool +from op_test_helper import TestCaseHelper import paddle -import paddle.nn.functional as F import cinn from cinn.frontend import * from cinn.common import * @@ -28,105 +27,172 @@ "x86 test will be skipped due to timeout.") class TestRemainderOp(OpTest): def setUp(self): - self.init_case() - - def init_case(self): - self.inputs = { - "x": np.array([7]).astype('float32'), - "y": np.array([-3]).astype('float32') - } + # print(f"\n{self.__class__.__name__}: {self.case}") + self.prepare_inputs() + + def prepare_inputs(self): + self.x_np = self.random( + shape=self.case["x_shape"], + dtype=self.case["dtype"], + low=self.case["x_low"], + high=self.case["x_high"]) + self.y_np = self.random( + shape=self.case["y_shape"], + dtype=self.case["dtype"], + low=self.case["y_low"], + high=self.case["y_high"]) + self.y_np = np.where(self.y_np == 0, 1, self.y_np) def build_paddle_program(self, target): - x = paddle.to_tensor(self.inputs["x"], stop_gradient=False) - y = paddle.to_tensor(self.inputs["y"], stop_gradient=False) - + x = paddle.to_tensor(self.x_np, stop_gradient=False) + y = paddle.to_tensor(self.y_np, stop_gradient=False) out = paddle.remainder(x, y) - self.paddle_outputs = [out] def build_cinn_program(self, target): - builder = NetBuilder("pow") + builder = NetBuilder("remainder") x = builder.create_input( - self.nptype2cinntype(self.inputs["x"].dtype), - self.inputs["x"].shape, "x") + self.nptype2cinntype(self.x_np.dtype), self.x_np.shape, "x") y = builder.create_input( - self.nptype2cinntype(self.inputs["y"].dtype), - self.inputs["y"].shape, "y") + self.nptype2cinntype(self.y_np.dtype), self.y_np.shape, "y") out = builder.remainder(x, y) - prog = builder.build() res = self.get_cinn_output(prog, target, [x, y], - [self.inputs["x"], self.inputs["y"]], [out]) - - self.cinn_outputs = [res[0]] + [self.x_np, self.y_np], [out]) + self.cinn_outputs = res def test_check_results(self): self.check_outputs_and_grads() -class TestRemainderCase1(TestRemainderOp): - def init_case(self): - self.inputs = { - "x": self.random([32, 64], "float32", 20, 100), - "y": self.random([32, 64], "float32", 1, 20), - } - - -class TestRemainderCase2(TestRemainderOp): - def init_case(self): - self.inputs = { - "x": self.random([32, 64], "int32", 20, 100), - "y": self.random([32, 64], "int32", 1, 20), - } - - -class TestRemainderCase3(TestRemainderOp): - def init_case(self): - self.inputs = { - "x": self.random([32, 64], "float32", 20, 100), - "y": self.random([32, 64], "float32", -20, -1), - } - - -class TestRemainderCase4(TestRemainderOp): - def init_case(self): - self.inputs = { - "x": self.random([32, 64], "int32", 20, 100), - "y": self.random([32, 64], "int32", -20, -1), - } - - -class TestRemainderCase5(TestRemainderOp): - def init_case(self): - self.inputs = { - "x": self.random([32, 64], "float32", -100, -20), - "y": self.random([32, 64], "float32", 1, 20), - } - - -class TestRemainderCase6(TestRemainderOp): - def init_case(self): - self.inputs = { - "x": self.random([32, 64], "float32", -100, -20), - "y": self.random([32, 64], "float32", -20, -1), - } - - -class TestRemainderCase7(TestRemainderOp): - def init_case(self): - self.inputs = { - "x": self.random([32, 64], "int32", -100, -20), - "y": self.random([32, 64], "int32", 1, 20), - } - - -class TestRemainderCase8(TestRemainderOp): - def init_case(self): - self.inputs = { - "x": self.random([32, 64], "int32", -100, -20), - "y": self.random([32, 64], "int32", -20, -1), - } +class TestRemainderOpShape(TestCaseHelper): + def init_attrs(self): + self.class_name = "TestRemainderOpCase" + self.cls = TestRemainderOp + self.inputs = [ + { + "x_shape": [1], + "y_shape": [1], + }, + { + "x_shape": [1024], + "y_shape": [1024], + }, + { + "x_shape": [512, 256], + "y_shape": [512, 256], + }, + { + "x_shape": [128, 64, 32], + "y_shape": [128, 64, 32], + }, + { + "x_shape": [16, 8, 4, 2], + "y_shape": [16, 8, 4, 2], + }, + { + "x_shape": [16, 8, 4, 2, 1], + "y_shape": [16, 8, 4, 2, 1], + }, + ] + self.dtypes = [ + { + "dtype": "int32", + }, + ] + self.attrs = [ + { + "x_low": -10, + "x_high": 10, + "y_low": -10, + "y_high": 10, + }, + ] + + +class TestRemainderOpDtype(TestCaseHelper): + def init_attrs(self): + self.class_name = "TestRemainderOpCase" + self.cls = TestRemainderOp + self.inputs = [ + { + "x_shape": [1024], + "y_shape": [1024], + }, + ] + self.dtypes = [ + { + "dtype": "int32", + }, + { + "dtype": "int64", + }, + { + "dtype": "float16", + }, + { + "dtype": "float32", + }, + { + "dtype": "float64", + }, + ] + self.attrs = [ + { + "x_low": -10, + "x_high": 10, + "y_low": -10, + "y_high": 10, + }, + ] + + +class TestRemainderOpBroadcast(TestCaseHelper): + def init_attrs(self): + self.class_name = "TestRemainderOpCase" + self.cls = TestRemainderOp + self.inputs = [ + { + "x_shape": [1], + "y_shape": [1], + }, + { + "x_shape": [1024], + "y_shape": [1], + }, + { + "x_shape": [512, 256], + "y_shape": [1, 256], + }, + { + "x_shape": [128, 64, 32], + "y_shape": [128, 1, 32], + }, + { + "x_shape": [16, 8, 4, 2], + "y_shape": [16, 1, 1, 2], + }, + { + "x_shape": [16, 8, 4, 2, 1], + "y_shape": [16, 1, 1, 1, 1], + }, + ] + self.dtypes = [ + { + "dtype": "int32", + }, + ] + self.attrs = [ + { + "x_low": -10, + "x_high": 10, + "y_low": -10, + "y_high": 10, + }, + ] if __name__ == "__main__": - unittest.main() + TestRemainderOpShape().run() + TestRemainderOpDtype().run() + TestRemainderOpBroadcast().run() diff --git a/python/tests/ops/test_uniform_random_op.py b/python/tests/ops/test_uniform_random_op.py index 049bd57ebd..f64ffe8b8d 100644 --- a/python/tests/ops/test_uniform_random_op.py +++ b/python/tests/ops/test_uniform_random_op.py @@ -14,9 +14,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -import unittest -import numpy as np from op_test import OpTest, OpTestTool +from op_test_helper import TestCaseHelper import paddle import cinn from cinn.frontend import * @@ -27,57 +26,134 @@ "x86 test will be skipped due to timeout.") class TestUniformRandomOp(OpTest): def setUp(self): - self.init_case() - - def init_case(self): - self.shape = [2, 3] - self.min = -1.0 - self.max = 1.0 - self.seed = 10 - self.dtype = "float32" + # print(f"\n{self.__class__.__name__}: {self.case}") + pass def build_paddle_program(self, target): out = paddle.uniform( - shape=self.shape, - dtype=self.dtype, - min=self.min, - max=self.max, - seed=self.seed) + shape=self.case["shape"], + dtype=self.case["dtype"], + min=self.case["min"], + max=self.case["max"], + seed=self.case["seed"]) self.paddle_outputs = [out] def build_cinn_program(self, target): builder = NetBuilder("uniform_random") - out = builder.uniform_random(self.shape, self.min, self.max, self.seed, - self.dtype) + out = builder.uniform_random(self.case["shape"], self.case["min"], + self.case["max"], self.case["seed"], + self.case["dtype"]) prog = builder.build() res = self.get_cinn_output(prog, target, [], [], [out], passes=[]) - self.cinn_outputs = [res[0]] + self.cinn_outputs = res def test_check_results(self): # Due to the different random number generation numbers implemented # in the specific implementation, the random number results generated # by CINN and Paddle are not the same, but they all conform to the # Uniform distribution. - self.check_outputs_and_grads(max_relative_error=10000) + self.check_outputs_and_grads( + max_relative_error=10000, max_absolute_error=10000) + + +class TestUniformRandomOpShape(TestCaseHelper): + def init_attrs(self): + self.class_name = "TestUniformRandomOpCase" + self.cls = TestUniformRandomOp + self.inputs = [ + { + "shape": [1], + }, + { + "shape": [1024], + }, + { + "shape": [512, 256], + }, + { + "shape": [128, 64, 32], + }, + { + "shape": [16, 8, 4, 2], + }, + { + "shape": [16, 8, 4, 2, 1], + }, + ] + self.dtypes = [ + { + "dtype": "float32", + }, + ] + self.attrs = [ + { + "min": -1.0, + "max": 1.0, + "seed": 1234, + }, + ] -class TestUniformRandomCase1(TestUniformRandomOp): - def init_case(self): - self.shape = [2, 3, 4] - self.min = -5.5 - self.max = 5.5 - self.seed = 10 - self.dtype = "float32" +class TestUniformRandomOpDtype(TestCaseHelper): + def init_attrs(self): + self.class_name = "TestUniformRandomOpCase" + self.cls = TestUniformRandomOp + self.inputs = [ + { + "shape": [1024], + }, + ] + self.dtypes = [ + { + "dtype": "float32", + }, + { + "dtype": "float64", + }, + ] + self.attrs = [ + { + "min": -1.0, + "max": 1.0, + "seed": 1234, + }, + ] -class TestUniformRandomCase2(TestUniformRandomOp): - def init_case(self): - self.shape = [2, 3, 4] - self.min = -10.0 - self.max = 10.0 - self.seed = 10 - self.dtype = "float64" +class TestUniformRandomOpAttr(TestCaseHelper): + def init_attrs(self): + self.class_name = "TestUniformRandomOpCase" + self.cls = TestUniformRandomOp + self.inputs = [ + { + "shape": [1024], + }, + ] + self.dtypes = [ + { + "dtype": "float32", + }, + ] + self.attrs = [ + { + "min": -10.0, + "max": 0, + "seed": 1, + }, + { + "min": 0, + "max": 10.0, + "seed": 2, + }, + { + "min": -100.0, + "max": 100.0, + "seed": 3, + }, + ] if __name__ == "__main__": - unittest.main() + TestUniformRandomOpShape().run() + TestUniformRandomOpDtype().run() + TestUniformRandomOpAttr().run()