From 1252353fef21282e09b2c6aa2d963aeda6a814a3 Mon Sep 17 00:00:00 2001 From: enkilee Date: Mon, 1 May 2023 17:26:40 +0000 Subject: [PATCH 1/9] fix --- python/tests/ops/test_divide_op.py | 93 +++++++++++++++++++----------- 1 file changed, 58 insertions(+), 35 deletions(-) diff --git a/python/tests/ops/test_divide_op.py b/python/tests/ops/test_divide_op.py index 34f682c63a..0c3fe343a2 100644 --- a/python/tests/ops/test_divide_op.py +++ b/python/tests/ops/test_divide_op.py @@ -17,6 +17,7 @@ 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 @@ -28,17 +29,18 @@ "x86 test will be skipped due to timeout.") class TestDivOp(OpTest): def setUp(self): + print(f"\nRunning {self.__class__.__name__}: {self.case}") self.init_case() def init_case(self): - self.inputs = { - "x": np.random.random([32]).astype("float32"), - "y": np.random.random([32]).astype("float32") - } + self.x_np = self.random( + shape=self.case["x_shape"], dtype=self.case["x_dtype"]) + self.y_np = self.random( + shape=self.case["y_shape"], dtype=self.case["y_dtype"]) 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) + x = paddle.to_tensor(self.x_np, stop_gradient=True) + y = paddle.to_tensor(self.y_np, stop_gradient=True) out = paddle.divide(x, y) @@ -46,43 +48,64 @@ def build_paddle_program(self, target): def build_cinn_program(self, target): builder = NetBuilder("div") - x = builder.create_input(Float(32), self.inputs["x"].shape, "x") - y = builder.create_input(Float(32), self.inputs["y"].shape, "y") + x = builder.create_input( + self.nptype2cinntype(self.case["x_dtype"]), self.case["x_shape"], + "x") + y = builder.create_input( + self.nptype2cinntype(self.case["y_dtype"]), self.case["y_shape"], + "y") out = builder.divide(x, y) prog = builder.build() res = self.get_cinn_output(prog, target, [x, y], - [self.inputs["x"], self.inputs["y"]], [out]) + [self.x_np, self.y_np], [out]) self.cinn_outputs = [res[0]] def test_check_results(self): - self.check_outputs_and_grads() - - -class TestDivCase1(TestDivOp): - def init_case(self): - self.inputs = { - "x": np.random.random([32, 64]).astype("float32"), - "y": np.random.random([32, 64]).astype("float32") - } - - -class TestDivCase2(TestDivOp): - def init_case(self): - self.inputs = { - "x": np.random.random([2, 2, 32]).astype("float32"), - "y": np.random.random([32]).astype("float32") - } - - -class TestDivCase3(TestDivOp): - def init_case(self): - self.inputs = { - "x": np.random.random([2, 32]).astype("float32"), - "y": np.random.random([1]).astype("float32") - } + max_relative_error = self.case[ + "max_relative_error"] if "max_relative_error" in self.case else 1e-5 + self.check_outputs_and_grads(max_relative_error=max_relative_error) + + +class TestDivAll(TestCaseHelper): + def init_attrs(self): + self.class_name = "TestDivOpCase" + self.cls = TestDivOp + self.inputs = [ + { + "x_shape": [32, 64], + "y_shape": [32, 64], + }, + { + "x_shape": [2, 2, 32], + "y_shape": [32], + }, + { + "x_shape": [2, 32], + "y_shape": [1], + }, + ] + self.dtypes = [ + { + "x_dtype": "int32", + "y_dtype": "int32", + }, + { + "x_dtype": "int64", + "y_dtype": "int64", + }, + { + "x_dtype": "float32", + "y_dtype": "float32", + }, + { + "x_dtype": "float64", + "y_dtype": "float64", + }, + ] + self.attrs = [] if __name__ == "__main__": - unittest.main() + TestDivAll().run() From 393356ceeaf5d7dfc2736d2f10d36b75611bd6d4 Mon Sep 17 00:00:00 2001 From: enkilee Date: Tue, 2 May 2023 05:00:34 +0000 Subject: [PATCH 2/9] fix --- python/tests/ops/test_divide_op.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/python/tests/ops/test_divide_op.py b/python/tests/ops/test_divide_op.py index 0c3fe343a2..185cdebcc8 100644 --- a/python/tests/ops/test_divide_op.py +++ b/python/tests/ops/test_divide_op.py @@ -73,6 +73,10 @@ def init_attrs(self): self.class_name = "TestDivOpCase" self.cls = TestDivOp self.inputs = [ + { + "x_shape": [2, 3, 4], + "y_shape": [1, 5, 2], + }, { "x_shape": [32, 64], "y_shape": [32, 64], @@ -82,8 +86,8 @@ def init_attrs(self): "y_shape": [32], }, { - "x_shape": [2, 32], - "y_shape": [1], + "x_shape": [32], + "y_shape": [32], }, ] self.dtypes = [ From 789f7caf7a6f9b69caef787de47875ed343b11da Mon Sep 17 00:00:00 2001 From: enkilee Date: Tue, 2 May 2023 05:41:35 +0000 Subject: [PATCH 3/9] fix --- python/tests/ops/test_divide_op.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/python/tests/ops/test_divide_op.py b/python/tests/ops/test_divide_op.py index 185cdebcc8..e8e753a434 100644 --- a/python/tests/ops/test_divide_op.py +++ b/python/tests/ops/test_divide_op.py @@ -91,14 +91,14 @@ def init_attrs(self): }, ] self.dtypes = [ - { - "x_dtype": "int32", - "y_dtype": "int32", - }, - { - "x_dtype": "int64", - "y_dtype": "int64", - }, + #{ + # "x_dtype": "int32", + # "y_dtype": "int32", + #}, + #{ + # "x_dtype": "int64", + # "y_dtype": "int64", + #}, { "x_dtype": "float32", "y_dtype": "float32", From 7de0fcc32d72cfd2e999b4730149212dc573fe0d Mon Sep 17 00:00:00 2001 From: enkilee Date: Wed, 3 May 2023 07:54:30 +0000 Subject: [PATCH 4/9] fix --- python/tests/ops/test_divide_op.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/python/tests/ops/test_divide_op.py b/python/tests/ops/test_divide_op.py index e8e753a434..127d5ad7c5 100644 --- a/python/tests/ops/test_divide_op.py +++ b/python/tests/ops/test_divide_op.py @@ -34,9 +34,15 @@ def setUp(self): def init_case(self): self.x_np = self.random( - shape=self.case["x_shape"], dtype=self.case["x_dtype"]) + shape=self.case["x_shape"], + dtype=self.case["x_dtype"], + low=-10, + high=10) self.y_np = self.random( - shape=self.case["y_shape"], dtype=self.case["y_dtype"]) + shape=self.case["y_shape"], + dtype=self.case["y_dtype"], + low=-10, + high=10) def build_paddle_program(self, target): x = paddle.to_tensor(self.x_np, stop_gradient=True) @@ -91,14 +97,14 @@ def init_attrs(self): }, ] self.dtypes = [ - #{ - # "x_dtype": "int32", - # "y_dtype": "int32", - #}, - #{ - # "x_dtype": "int64", - # "y_dtype": "int64", - #}, + { + "x_dtype": "int32", + "y_dtype": "int32", + }, + { + "x_dtype": "int64", + "y_dtype": "int64", + }, { "x_dtype": "float32", "y_dtype": "float32", From 0e6a5803c875bcd6835aff590c78bf1213e73bfe Mon Sep 17 00:00:00 2001 From: enkilee Date: Thu, 4 May 2023 00:55:31 +0000 Subject: [PATCH 5/9] fix --- python/tests/ops/test_divide_op.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/python/tests/ops/test_divide_op.py b/python/tests/ops/test_divide_op.py index 127d5ad7c5..3448bc7935 100644 --- a/python/tests/ops/test_divide_op.py +++ b/python/tests/ops/test_divide_op.py @@ -97,14 +97,14 @@ def init_attrs(self): }, ] self.dtypes = [ - { - "x_dtype": "int32", - "y_dtype": "int32", - }, - { - "x_dtype": "int64", - "y_dtype": "int64", - }, + #{ + # "x_dtype": "int32", + # "y_dtype": "int32", + #}, + #{ + # "x_dtype": "int64", + # "y_dtype": "int64", + #}, { "x_dtype": "float32", "y_dtype": "float32", From 7b60fc04f09a38a62bde701a9fc1c1f9993b9938 Mon Sep 17 00:00:00 2001 From: enkilee Date: Thu, 4 May 2023 06:22:55 +0000 Subject: [PATCH 6/9] fix --- python/tests/ops/test_divide_op.py | 34 ++++++++++++++++++------------ 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/python/tests/ops/test_divide_op.py b/python/tests/ops/test_divide_op.py index 3448bc7935..d612125dda 100644 --- a/python/tests/ops/test_divide_op.py +++ b/python/tests/ops/test_divide_op.py @@ -41,7 +41,7 @@ def init_case(self): self.y_np = self.random( shape=self.case["y_shape"], dtype=self.case["y_dtype"], - low=-10, + low=1, high=10) def build_paddle_program(self, target): @@ -80,31 +80,39 @@ def init_attrs(self): self.cls = TestDivOp self.inputs = [ { - "x_shape": [2, 3, 4], - "y_shape": [1, 5, 2], + "x_shape": [32], + "y_shape": [32], }, { "x_shape": [32, 64], "y_shape": [32, 64], }, + { + "x_shape": [2, 3, 4], + "y_shape": [1, 5, 2], + }, { "x_shape": [2, 2, 32], "y_shape": [32], }, { - "x_shape": [32], - "y_shape": [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 = [ - #{ - # "x_dtype": "int32", - # "y_dtype": "int32", - #}, - #{ - # "x_dtype": "int64", - # "y_dtype": "int64", - #}, + { + "x_dtype": "int32", + "y_dtype": "int32", + }, + { + "x_dtype": "int64", + "y_dtype": "int64", + }, { "x_dtype": "float32", "y_dtype": "float32", From a9c61fe38934a71be4bf49c82004e797f2d6b01c Mon Sep 17 00:00:00 2001 From: enkilee Date: Fri, 5 May 2023 01:53:57 +0000 Subject: [PATCH 7/9] fix --- python/tests/ops/test_divide_op.py | 93 +++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 1 deletion(-) diff --git a/python/tests/ops/test_divide_op.py b/python/tests/ops/test_divide_op.py index d612125dda..9c824f4acb 100644 --- a/python/tests/ops/test_divide_op.py +++ b/python/tests/ops/test_divide_op.py @@ -92,9 +92,99 @@ def init_attrs(self): "y_shape": [1, 5, 2], }, { - "x_shape": [2, 2, 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 = [ + { + "x_dtype": "int32", + "y_dtype": "int32", + }, + { + "x_dtype": "int64", + "y_dtype": "int64", + }, + { + "x_dtype": "float32", + "y_dtype": "float32", + }, + { + "x_dtype": "float64", + "y_dtype": "float64", + }, + ] + self.attrs = [] + + +class TestDivNegOp(OpTest): + def setUp(self): + print(f"\nRunning {self.__class__.__name__}: {self.case}") + self.init_case() + + def init_case(self): + self.x_np = self.random( + shape=self.case["x_shape"], + dtype=self.case["x_dtype"], + low=-10, + high=10) + self.y_np = self.random( + shape=self.case["y_shape"], + dtype=self.case["y_dtype"], + low=-10, + high=-1) + + def build_paddle_program(self, target): + x = paddle.to_tensor(self.x_np, stop_gradient=True) + y = paddle.to_tensor(self.y_np, stop_gradient=True) + + out = paddle.divide(x, y) + + self.paddle_outputs = [out] + + def build_cinn_program(self, target): + builder = NetBuilder("div") + x = builder.create_input( + self.nptype2cinntype(self.case["x_dtype"]), self.case["x_shape"], + "x") + y = builder.create_input( + self.nptype2cinntype(self.case["y_dtype"]), self.case["y_shape"], + "y") + out = builder.divide(x, y) + + prog = builder.build() + res = self.get_cinn_output(prog, target, [x, y], + [self.x_np, self.y_np], [out]) + + self.cinn_outputs = [res[0]] + + def test_check_results(self): + max_relative_error = self.case[ + "max_relative_error"] if "max_relative_error" in self.case else 1e-5 + self.check_outputs_and_grads(max_relative_error=max_relative_error) + + +class TestDivNegAll(TestCaseHelper): + def init_attrs(self): + self.class_name = "TestDivNegOpCase" + self.cls = TestDivNegOp + self.inputs = [ + { + "x_shape": [32], "y_shape": [32], }, + { + "x_shape": [32, 64], + "y_shape": [32, 64], + }, + { + "x_shape": [2, 3, 4], + "y_shape": [1, 5, 2], + }, { "x_shape": [16, 8, 4, 2], "y_shape": [16, 8, 4, 2], @@ -127,3 +217,4 @@ def init_attrs(self): if __name__ == "__main__": TestDivAll().run() + TestDivNegAll().run() From 44d5e1c43eb495ee474411c268b797a8245a7b8e Mon Sep 17 00:00:00 2001 From: enkilee Date: Fri, 5 May 2023 06:31:40 +0000 Subject: [PATCH 8/9] fix --- python/tests/ops/test_divide_op.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/python/tests/ops/test_divide_op.py b/python/tests/ops/test_divide_op.py index 9c824f4acb..0bddb9e23d 100644 --- a/python/tests/ops/test_divide_op.py +++ b/python/tests/ops/test_divide_op.py @@ -95,10 +95,6 @@ def init_attrs(self): "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 = [ { @@ -189,10 +185,6 @@ def init_attrs(self): "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 = [ { From 66f75e7a32a071ce89f416c62a2204532d11bd79 Mon Sep 17 00:00:00 2001 From: enkilee Date: Fri, 5 May 2023 06:34:55 +0000 Subject: [PATCH 9/9] fix --- python/tests/ops/test_divide_op.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/python/tests/ops/test_divide_op.py b/python/tests/ops/test_divide_op.py index 0bddb9e23d..9c824f4acb 100644 --- a/python/tests/ops/test_divide_op.py +++ b/python/tests/ops/test_divide_op.py @@ -95,6 +95,10 @@ def init_attrs(self): "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 = [ { @@ -185,6 +189,10 @@ def init_attrs(self): "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 = [ {