UT测试: UT测试是为了测试算子代码的正确性和不同场景下的覆盖率的测试,UT测试在硬件CPU上执行,MindStudio提供了gtest框架方便进行UT测试。
参考:https://www.hiascend.com/document/detail/zh/mindstudio/50RC2/msug/msug_000125.html
1.打开MindStudio软件
cd MindStudio/bin
./MindStudio.sh
2.新建UT测试
Project右键New Cases,选择TBE UT Case。
在testcases文件夹下便会自动生成相关的文件。sinh文件夹下的两个文件,test_sinh_proto.cc用来测试算子原型,test_sinh_impl.py用来测试算子实现。
3.算子原型测试
test_sinh_proto.cc是算子原型定义的UT C++测试用例,详细代码解释见参考官方教程。
#include <gtest/gtest.h>
#include <vector>
#include "sinh.h"
class SinhTest : public testing::Test {
protected:
static void SetUpTestCase() {
std::cout << "sinh test SetUp" << std::endl;
}
static void TearDownTestCase() {
std::cout << "sinh test TearDown" << std::endl;
}
};
TEST_F(SinhTest, sinh_test_case_1) {
ge::op::Sinh sinh_op;
ge::TensorDesc tensorDesc;
ge::Shape shape({2, 3, 4});
tensorDesc.SetDataType(ge::DT_FLOAT16);
tensorDesc.SetShape(shape);
tensorDesc.SetOriginShape(shape);
sinh_op.UpdateInputDesc("x", tensorDesc);
auto ret = sinh_op.InferShapeAndType();
EXPECT_EQ(ret, ge::GRAPH_SUCCESS);
auto output_desc = sinh_op.GetOutputDescByName("y");
EXPECT_EQ(output_desc.GetDataType(), ge::DT_FLOAT16);
std::vector<int64_t> expected_output_shape = {2, 3, 4};
EXPECT_EQ(output_desc.GetShape().GetDims(), expected_output_shape);
}
4.运行算子原型测试用例
右键单击“testcases/ut/ops_test”文件夹,选择“Run Tbe Operator‘All’UT Proto”
,执行整个文件夹下算子原型定义的UT C++测试用例。
配置如图并点击run。
算子原型测试通过。
5.算子实现测试
test_sinh_impl.py是算子实现代码的UT Python测试用例,详细代码解释见官方教程。
precision_standard,自定义精度标准,取值为:(rtol,atol,Max_atol)。atol:绝对容忍度,rtol:相对容忍度。
Sinh算子作业中我的代码只能满足 rtol= 0.01,atol = 0.01的精度要求。当精度调为(0.001,0.001)时,会因为Error count>Threshold count而测试失败。
# # -*- coding:utf-8 -*-
import sys
from op_test_frame.ut import BroadcastOpUT
from op_test_frame.common import precision_info
import tensorflow as tf
import numpy as np
ut_case = BroadcastOpUT("sinh")
def calc_expect_func(x, y):
# op_run = tf.raw_ops.Sinh(x=x["value"])
#
# with tf.Session() as sess:
# res = sess.run(op_run)
res = np.sinh(x["value"])
return [res, ]
ut_case.add_precision_case("all", {
"params": [{"dtype": "float16", "format": "ND", "ori_format": "ND", "ori_shape": (32,), "shape": (32,1024),
"param_type": "input"},
{"dtype": "float16", "format": "ND", "ori_format": "ND", "ori_shape": (32,), "shape": (32,1024),
"param_type": "output"}],
"case_name": "case_float16_succ",
"calc_expect_func": calc_expect_func,
#rtol:0.01, atol:0.01
"precision_standard": precision_info.PrecisionStandard(0.01,0.01)
})
6.运行算子实现测试用例
算子实现测试有三种环境:
- Simulator_Function:功能仿真环境。
- Simulator_Performance:性能仿真环境。该功能仅支持昇腾310P AI处理器。
- Simulator_TMModel:快速展示算子执行的调度流水线,不进行实际算子计算。该功能仅支持昇腾310 AI处理器和昇腾910 AI处理器。
1)功能仿真
右键单击“testcases/ut/ops_test”文件夹,选择“Run Tbe Operator‘All’UT Impl with coverage”
, 执行整个文件夹下算子实现代码的测试用例。
如图所示完成配置:
仿真功能测试结果:
2) 性能仿真
右键单击“testcases/ut/ops_test”文件夹,选择“Run Tbe Operator‘All’UT Impl with coverage”,执行整个文件夹下算子实现代码的测试用例。
切换Target为Simulator_Performance:
在Profiling窗口查看性能仿真结果:
3) 快速展示算子执行的调度流水线
右键单击“testcases/ut/ops_test”文件夹,选择“Run Tbe Operator‘All’UT Impl with coverage”,执行整个文件夹下算子实现代码的测试用例。
切换Target为Simulator_TMModel并查看专家对性能优化的建议。
查看执行流水线和专家建议如下:
如果对算子性能优化方法感兴趣,可以继续研究TIK算子开发。