Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
Add Op UnitTest for divide (#1383)
Browse files Browse the repository at this point in the history
* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix
  • Loading branch information
enkilee authored May 6, 2023
1 parent a879da0 commit 670151d
Showing 1 changed file with 161 additions and 29 deletions.
190 changes: 161 additions & 29 deletions python/tests/ops/test_divide_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,61 +29,192 @@
"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"],
low=-10,
high=10)
self.y_np = self.random(
shape=self.case["y_shape"],
dtype=self.case["y_dtype"],
low=1,
high=10)

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)

self.paddle_outputs = [out]

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()

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],
"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],
},
{
"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()

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")
}
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)

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")
}
out = paddle.divide(x, y)

self.paddle_outputs = [out]

class TestDivCase3(TestDivOp):
def init_case(self):
self.inputs = {
"x": np.random.random([2, 32]).astype("float32"),
"y": np.random.random([1]).astype("float32")
}
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],
},
{
"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 = []


if __name__ == "__main__":
unittest.main()
TestDivAll().run()
TestDivNegAll().run()

0 comments on commit 670151d

Please sign in to comment.