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

op unittest for select/sigmoid/sign/slice_assign/slice #1401

Merged
merged 7 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cinn/frontend/net_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,7 @@ class NetBuilder {
* @param ends The ending indices of corresponding axis in axes. Default: None.
* @param infer_flags Whether the output shape can be infered in compile time. Now only support all 1. Default: None.
* @param strides The slice step of corresponding axis in axes. Default: None.
* @param decrease_axis Eliminate the specified dimension. Default: None.
* @return A variable with the same dimension as x. The data type is same as x.
*/
Variable Slice(const Variable& x,
Expand Down
149 changes: 123 additions & 26 deletions python/tests/ops/test_select_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,26 @@
# 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
import paddle
import cinn
from cinn.frontend import *
from cinn.common import *
import logging
import os
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 TestSelectOp(OpTest):
def setUp(self):
self.init_case()
print(f"\nRunning {self.__class__.__name__}: {self.case}")
self.inputs = {}
self.prepare_inputs()

def init_case(self):
def prepare_inputs(self):
self.inputs = {
"Condition": np.zeros(100).astype('bool'),
"X": np.random.uniform((-3), 5, 100).astype('float32'),
"Y": np.random.uniform((-3), 5, 100).astype('float32')
"Condition": self.random(self.case["shape"], "bool", 0, 2),
thisjiang marked this conversation as resolved.
Show resolved Hide resolved
"X": self.random(self.case["shape"], self.case["dtype"]),
"Y": self.random(self.case["shape"], self.case["dtype"])
}

def build_paddle_program(self, target):
Expand All @@ -46,10 +44,15 @@ def build_paddle_program(self, target):

def build_cinn_program(self, target):
builder = NetBuilder("select")
c = builder.create_input(Bool(), self.inputs["Condition"].shape,
"Condition")
x = builder.create_input(Float(32), self.inputs["X"].shape, "X")
y = builder.create_input(Float(32), self.inputs["Y"].shape, "Y")
c = builder.create_input(
self.nptype2cinntype(self.inputs["Condition"].dtype),
self.inputs["Condition"].shape, "Condition")
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")

out = builder.select(c, x, y)
prog = builder.build()
Expand All @@ -63,19 +66,113 @@ def test_check_results(self):
self.check_outputs_and_grads(all_equal=True)


class TestSelectOp1(TestSelectOp):
def init_config(self):
self.x = np.random.uniform((-5), 5, (60, 2)).astype('float32')
self.y = np.random.uniform((-5), 5, (60, 2)).astype('float32')
self.cond = np.ones((60, 2)).astype('bool')
class TestSelectOpShape(TestCaseHelper):
def init_attrs(self):
self.class_name = "TestSelectOpShape"
self.cls = TestSelectOp
self.inputs = [
{
"shape": [10],
},
{
"shape": [8, 5],
},
{
"shape": [10, 3, 5],
},
{
"shape": [80, 40, 5, 7],
thisjiang marked this conversation as resolved.
Show resolved Hide resolved
},
{
"shape": [80, 1, 5, 7],
},
{
"shape": [80, 3, 1024, 7],
},
{
"shape": [10, 5, 1024, 2048],
},
{
"shape": [1],
},
{
"shape": [512],
},
{
"shape": [1024],
},
{
"shape": [2048],
},
{
"shape": [1, 1, 1, 1],
},
]
self.dtypes = [
{
"dtype": "float32"
},
]
self.attrs = []


class TestSelectOp2(TestSelectOp):
def init_config(self):
self.x = np.random.uniform((-3), 5, (20, 2, 4)).astype('float32')
self.y = np.random.uniform((-3), 5, (20, 2, 4)).astype('float32')
self.cond = np.array(np.random.randint(2, size=(20, 2, 4)), dtype=bool)
class TestSelectOpDtype(TestCaseHelper):
def init_attrs(self):
self.class_name = "TestSelectOpDtype"
self.cls = TestSelectOp
self.inputs = [
{
"shape": [1],
},
{
"shape": [5],
},
{
"shape": [80, 40, 5, 7],
},
]
self.dtypes = [
{
"dtype": "float16"
},
{
"dtype": "float32"
},
{
"dtype": "float64"
},
{
"dtype": "int32"
},
{
"dtype": "int64"
},
{
"dtype": "uint16"
},
# Paddle does not support the following data type
# {
# "dtype": "bool"
# },
# {
# "dtype": "int8"
# },
# {
# "dtype": "int16"
# },
# {
# "dtype": "uint8"
# },
# {
# "dtype": "uint32"
# },
# {
# "dtype": "uint64"
# },
]
self.attrs = []


if __name__ == "__main__":
unittest.main()
TestSelectOpShape().run()
TestSelectOpDtype().run()
103 changes: 91 additions & 12 deletions python/tests/ops/test_sigmoid_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,25 @@
# 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
import paddle
import paddle.nn.functional as F
import cinn
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 TestSigmoidOp(OpTest):
def setUp(self):
self.init_case()
print(f"\nRunning {self.__class__.__name__}: {self.case}")
self.inputs = {}
self.prepare_inputs()

def init_case(self):
def prepare_inputs(self):
self.inputs = {
"x": np.random.random([
32,
64,
]).astype("float32")
"x": self.random(self.case["shape"], self.case["dtype"], -1.0, 1.0)
}

def build_paddle_program(self, target):
Expand All @@ -48,7 +45,9 @@ def build_paddle_program(self, target):
# the forward result will be incorrect.
def build_cinn_program(self, target):
builder = NetBuilder("sigmoid")
x = builder.create_input(Float(32), self.inputs["x"].shape, "x")
x = builder.create_input(
self.nptype2cinntype(self.inputs["x"].dtype),
self.inputs["x"].shape, "x")
out = builder.sigmoid(x)

prog = builder.build()
Expand All @@ -61,5 +60,85 @@ def test_check_results(self):
self.check_outputs_and_grads()


class TestSigmoidOpShape(TestCaseHelper):
def init_attrs(self):
self.class_name = "TestSigmoidOpShape"
self.cls = TestSigmoidOp
self.inputs = [
{
"shape": [10],
},
{
"shape": [8, 5],
},
{
"shape": [10, 3, 5],
},
{
"shape": [80, 40, 5, 7],
},
{
"shape": [80, 1, 5, 7],
},
{
"shape": [80, 3, 1024, 7],
},
{
"shape": [10, 5, 1024, 2048],
},
{
"shape": [1],
},
{
"shape": [512],
},
{
"shape": [1024],
},
{
"shape": [2048],
},
{
"shape": [1, 1, 1, 1],
},
]
self.dtypes = [
{
"dtype": "float32"
},
]
self.attrs = []


class TestSigmoidOpDtype(TestCaseHelper):
def init_attrs(self):
self.class_name = "TestSigmoidOpDtype"
self.cls = TestSigmoidOp
self.inputs = [
{
"shape": [1],
},
{
"shape": [5],
},
{
"shape": [80, 40, 5, 7],
},
]
self.dtypes = [
{
"dtype": "float16"
},
{
"dtype": "float32"
},
{
"dtype": "float64"
},
]
self.attrs = []


if __name__ == "__main__":
unittest.main()
TestSigmoidOpShape().run()
TestSigmoidOpDtype().run()
Loading