• 极智AI | 教你使用深度学习模型调试器 polygraphy


    欢迎关注我的公众号 [极智视界],获取我的更多笔记分享

      大家好,我是极智视界,本文讲解一下 深度学习模型调试器 polygraphy 的使用方法。

      对于深度学习模型的部署,往往涉及多种框架之间的转换,一般是 训练框架 ( PyTorch、TensorFlow … ) => 推理框架 ( TensorRT、OnnxRuntime … ),每个框架都有不同的模型表示,所以这个过程中往往最关心的是转换之后不要掉精度。不过往往理想很丰满,现实不丰满,在出现掉精度的时候,总想有一个好用的工具能帮助我们定位是哪里出了问题,这个时候,polygraphy 就呼之欲出了。

    1 polygraphy 介绍

       polygraphy 是一个深度学习模型调试工具,包含 python API命令行工具 ,关于 polygraphy 的相关介绍其实比较少,它有的一些功能如下:

    • 使用多种后端运行推理计算,包括 TensorRT, onnxruntime, TensorFlow;
    • 比较不同后端的逐层计算结果;
    • 由模型恩建生成 TensorRT 引擎并序列化为.plan;
    • 查看模型网络的逐层信息;
    • 修改 Onnx 模型,如提取子图,计算图化简;
    • 分析 Onnx 转 TensorRT 失败原因,将原计算图中可以 / 不可以转 TensorRT 的子图分割保存;
    • 隔离 TensorRT 终端 错误 tactic;

      polygraphy 的安装方式比较简单,直接 pip 安装即可:

    pip install polygraphy
    
    • 1

    2 polygraphy 使用示例

      这里介绍一个 polygraphy 使用的示例,对 onnxruntime 和 TensorRT 进行精度对比,流程差不多是这样的:

    • 首先生成一个 .onnx 文件;
    • 其次使用 polygraphy 生成一个 FP16 的 TRT 引擎,并对比使用 onnxruntime 和 TensorRT 的计算结果;
    • 然后使用 polygraphy 生成一个 FP32 的 TRT 引擎,将网络中所有层都标记为输出,并对比使用 onnxruntime 和 TensorRT 的计算结果 (逐层结果对比);

      相关代码示意如下:

    # 生成一个 .onnx 模型作为 polygraphy 的输入
    # export model.onnx from pytorch
    # or
    # export model.onnx from tensorflow
    
    # 使用上面生成的 model.onnx 构建 TensorRT 引擎,使用 FP16 精度同时在 TensorRT 和 onnxruntime 中运行
    polygraphy run model.onnx \
        --onnxrt --trt \
        --workspace 100000000 \
        --save-engine=model_FP16.plan \
        --atol 1e-3 --rtol 1e-3 \
        --fp16 \
        --verbose \
        --trt-min-shapes 'x:0:[1,1,28,28]' \
        --trt-opt-shapes 'x:0:[4,1,28,28]' \
        --trt-max-shapes 'x:0:[16,1,28,28]' \
        --input-shapes 'x:0:[4,1,28,28]' \
        > result-run-FP16.txt
    
    # 使用上面生成的 model.onnx 构建 TensorRT 引擎,使用 FP32 精度同时在 TensorRT 和 onnxruntime 中运行
    # 输出所有层的计算结果作对比
    polygraphy run model.onnx \
        --onnxrt --trt \
        --workspace 100000000 \
        --save-engine=model_FP32_MarkAll.plan \
        --atol 1e-3 --rtol 1e-3 \
        --verbose \
        --onnx-outputs mark all \
        --trt-outputs mark all \
        --trt-min-shapes 'x:0:[1,1,28,28]' \
        --trt-opt-shapes 'x:0:[4,1,28,28]' \
        --trt-max-shapes 'x:0:[16,1,28,28]' \
        --input-shapes 'x:0:[4,1,28,28]' \
        > result-run-FP32-MarkAll.txt
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

      选取 FP16 推理对比结果进行展示,如下:


      好了,以上分享了 教你使用深度学习模型调试器 polygraphy。希望我的分享能对你的学习有一点帮助。


     【公众号传送】

    《极智AI | 教你使用深度学习模型调试器 polygraphy》


    在这里插入图片描述

    扫描下方二维码即可关注我的微信公众号【极智视界】,获取我的更多经验分享,让我们用极致+极客的心态来迎接AI !

  • 相关阅读:
    web安全渗透测试十大常规项(一):web渗透测试之任意文件读取
    GVA快速使用
    YGG 购买了 DigiDaigaku 公司的 NFT,入局 Limit Break 的“免费拥有”游戏
    vue引入sm-crypto通过sm4对文件进行加解密,用户输入密码
    WPF 常用功能整合
    beego框架自学笔记1
    C++入门篇10---stack+queue+priority_queue
    【Git从青铜到王者】第二篇:Git的初始
    Windows10家庭版安装docker toolbox的坑
    11-js事件基础
  • 原文地址:https://blog.csdn.net/weixin_42405819/article/details/125507316