CANN训练营第三季_昇腾CANN算子精讲课_TBE算子Sinh开发笔记_UT测试(三)

本文档详细介绍了如何使用MindStudio进行UT测试,包括算子原型和实现的测试步骤。通过gtest框架进行算子原型的C++测试,利用Python进行算子实现的测试,并在不同仿真环境下验证精度和性能。测试过程覆盖了功能仿真、性能仿真和调度流水线展示,对于算子性能优化有深入探讨。
摘要由CSDN通过智能技术生成
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。
image.png
在testcases文件夹下便会自动生成相关的文件。sinh文件夹下的两个文件,test_sinh_proto.cc用来测试算子原型,test_sinh_impl.py用来测试算子实现。
image.png

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++测试用例。
image.png
配置如图并点击run。
image.png
算子原型测试通过。
image.png

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”, 执行整个文件夹下算子实现代码的测试用例。
image.png
如图所示完成配置:
image.png
仿真功能测试结果:
image.png
image.png

2) 性能仿真

右键单击“testcases/ut/ops_test”文件夹,选择“Run Tbe Operator‘All’UT Impl with coverage”,执行整个文件夹下算子实现代码的测试用例。
image.png
切换Target为Simulator_Performance:
image.png
在Profiling窗口查看性能仿真结果:
image.png

3) 快速展示算子执行的调度流水线

右键单击“testcases/ut/ops_test”文件夹,选择“Run Tbe Operator‘All’UT Impl with coverage”,执行整个文件夹下算子实现代码的测试用例。
image.png
切换Target为Simulator_TMModel并查看专家对性能优化的建议。
image.png
查看执行流水线和专家建议如下:
image.png
如果对算子性能优化方法感兴趣,可以继续研究TIK算子开发。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值