OpUT为UT测试框架的基类,提供了测试用例定义及测试用例执行的接口,主要包含如下两个接口:
@check_op_params(REQUIRED_INPUT, REQUIRED_INPUT, REQUIRED_OUTPUT, KERNEL_NAME) def add(input_x, input_y, output_z, kernel_name="add"):
{
"params": [
{
"shape": (32, 64),
"ori_shape": (32, 64),
"format": "ND",
"ori_format": (32, 64),
"dtype": "float16"
},
{
"shape": (32, 64),
"ori_shape": (32, 64),
"format": "ND",
"ori_format": (32, 64),
"dtype": "float16"
}
],
"case_name": "test_add_case_1",
"expect": "success"
}
# 以conv2d算子为例,详细算子实现请参见“样例工程”提供的conv2d算子实现文件。
# def get_op_support_info(inputs, weights, bias, offset_w, outputs, strides, pads, dilations,groups=1, data_format='NCHW', offset_x=0, kernel_name="conv2d")
{
"params": [
# inputs参数(tensor)
{'shape': (1, 1, 16, 16, 16), "format": "NC1HWC0", 'ori_shape': (1, 16, 16, 16), 'ori_format': 'NCHW', 'dtype': 'float16', "param_type": "input", "value_range": [1.0, 2.0]},
# weights参数(tensor)
{'shape': (1, 1, 16, 16), "format":"FRACTAL_Z", 'ori_shape': (16, 16, 1, 1), 'ori_format': 'NCHW', 'dtype': 'float16', "param_type": "input", "value_range": [1.0, 2.0]},
# bias参数
None,
# offset_w参数
None,
# outputs参数(tensor)
{'shape':(1, 1, 16, 16, 16), 'ori_shape':(1, 16, 16, 16), "format": "NC1HWC0", 'ori_format': 'NCHW', "param_type": "output", 'dtype': 'float16'},
# strides参数
(1, 1, 1, 1),
# pads参数
(0, 0, 0, 0),
# dilations参数
(1, 1, 1, 1),
# 可选groups参数
1,
# 可选data_format参数
'NCHW',
# 可选offset_x参数
0
],
"case_name": "test_conv2d_case_1",
"expect": "success"
}
参数 |
值 |
|---|---|
params |
该字段在测试用例运行时透传给算子接口。该字段中的参数应与算子接口的参数顺序一致。
|
case_name |
测试用例的名称,可选参数。 若不设置,测试框架会自动生成用例名称,生成规则如下: test_{op_type}_auto_case_name_{case_count} 例如: test_Add_auto_case_name_1 |
expect |
期望结果。 默认为期望“success”,也可以是预期抛出的异常,例如RuntimeError。 |
{
"params": [
{
"shape": (32, 64),
"ori_shape": (32, 64),
"format": "ND",
"ori_format": "ND",
"dtype": "float16",
"param_type": "input"
},
{
"shape": (32, 64),
"ori_shape": (32, 64),
"format": "ND",
"ori_format": "ND",
"dtype": "float16",
"param_type": "output"
}
],
"case_name": "test_add_case_1",
"calc_expect_func": np_add #一个函数
"precision_standard": precision_info.PrecisionStandard(0.001, 0.001) #可选字段
}
# 以conv2d算子为例,详细算子实现请参见“样例工程”提供的conv2d算子实现文件。
# def get_op_support_info(inputs, weights, bias, offset_w, outputs, strides, pads, dilations,groups=1, data_format='NCHW', offset_x=0, kernel_name="conv2d")
{
"params": [
# inputs参数(tensor)
{'shape': (1, 1, 16, 16, 16), "format": "NC1HWC0", 'ori_shape': (1, 16, 16, 16), 'ori_format': 'NCHW', 'dtype': 'float16', "param_type": "input", "value_range": [1.0, 2.0]},
# weights参数(tensor)
{'shape': (1, 1, 16, 16), "format":"FRACTAL_Z", 'ori_shape': (16, 16, 1, 1), 'ori_format': 'NCHW', 'dtype': 'float16', "param_type": "input", "value_range": [1.0, 2.0]},
# bias参数
None,
# offset_w参数
None,
# outputs参数(tensor)
{'shape':(1, 1, 16, 16, 16), 'ori_shape':(1, 16, 16, 16), "format": "NC1HWC0", 'ori_format': 'NCHW', "param_type": "output", 'dtype': 'float16'},
# strides参数
(1, 1, 1, 1),
# pads参数
(0, 0, 0, 0),
# dilations参数
(1, 1, 1, 1),
# 可选groups参数
1,
# 可选data_format参数
'NCHW',
# 可选offset_x参数
0
"case_name": "test_conv2d_case_1",
"calc_expect_func": np_conv2d #一个函数
"precision_standard": precision_info.PrecisionStandard(0.001, 0.001) #可选字段
}
参数 |
值 |
|---|---|
params |
该字段在测试用例运行时透传给算子接口。该字段中的参数应与算子接口的参数顺序一致。
|
case_name |
测试用例的名称,可选参数。若不设置,测试框架会自动生成用例名称,生成规则如下: test_{op_type}_auto_case_name_{case_count} 例如: test_Add_auto_case_name_1 |
calc_expect_func |
期望结果生成函数。 |
precision_standard |
自定义精度标准,取值为:(rtol, atol, Max_atol)。
说明:
若不配置此字段,按照如下默认精度与期望数据进行比对:
|
from op_test_frame.ut import OpUT
# "ut_case" 为UT测试框架的关键字,不可修改
ut_case = OpUT("Add", "impl.add", "add")
def np_add(x1, x2, y):
y = (x1.get("value") + x2.get("value"), )
return y
ut_case.add_precision_case(case={
"params": [
{
"shape": (32, 64),
"ori_shape": (32, 64),
"format": "ND",
"ori_format": "ND",
"dtype": "float16",
"param_type": "input"
},
{
"shape": (32, 64),
"ori_shape": (32, 64),
"format": "ND",
"ori_format": "ND",
"dtype": "float16",
"param_type": "input"
},
{
"shape": (32, 64),
"ori_shape": (32, 64),
"format": "ND",
"ori_format": "ND",
"dtype": "float16",
"param_type": "output"
}
],
"case_name": "test_add_case_1",
"calc_expect_func": np_add
})
if __name__ == '__main__':
ut_case.run("Ascend910",None,"ca","/home/allan/Ascend/toolkit/tools/simulator")
此样例未设置算子的输入数据值,测试框架将默认使用np.random.uniform(value_range, size=shape).astype(dtype)为每一个输入自动生成输入数据。
其中value_range使用默认值[0.1, 1.0],shape和dtype的取值为“params”中的每一个输入。value_range也可以通过参数指定,如下所示:
{
"shape": (32, 64),
"ori_shape": (32, 64),
"format": "ND",
"ori_format": "ND",
"dtype": "float16",
"param_type": "input",
"value_range": [2.0, 3.0]
}
也可以指定输入的值,如下所示:
{
"shape": (32, 64),
"ori_shape": (32, 64),
"format": "ND",
"ori_format": "ND",
"dtype": "float16",
"param_type": "input",
"value": np.zeros((32, 64), np.float16)
}
示例2:
自定义精度标准示例如下:
from op_test_frame.common import precision_info
from op_test_frame.ut import OpUT
ut_case = OpUT("Add")
ut_case.add_precision_case(case={
"params": [
{
"shape": (32, 64),
"ori_shape": (32, 64),
"format": "ND",
"ori_format": "ND",
"dtype": "float16",
"param_type": "input"
},
{
"shape": (32, 64),
"ori_shape": (32, 64),
"format": "ND",
"ori_format": "ND",
"dtype": "float16",
"param_type": "input"
},
{
"shape": (32, 64),
"ori_shape": (32, 64),
"format": "ND",
"ori_format": "ND",
"dtype": "float16",
"param_type": "output"
}
],
"case_name": "test_add_case_1",
"calc_expect_func": np_add,
"precision_standard": precision_info.PrecisionStandard(0.1, 0.1) # 使用精度标准双十分之一,与期望数据进行比对
})
OpUT.run(soc, case_name=None, simulator_mode=None, simulator_lib_path=None)
simulator_lib_path/
Ascendxxx/
lib/
libpv_model.so
...
Ascendxxx/
lib/
libpv_model.so
...
使用MindStudio运行UT测试用例时,无需用户手工调用OpUT.run接口。