更新时间:2024-05-08 GMT+08:00
分享

AIGC工具tailor使用指导

tailor简介

tailor是AIGC场景下用于模型转换(onnx到mindir)和性能分析的辅助工具,当前支持以下功能。

表1 功能总览

功能大类

具体功能

模型转换

  • 固定shape转模型
  • 动态shape传入指定档位转模型
  • 支持fp32
  • 支持AOE优化

benchmark

  • 支持测试性能
  • 支持精度测试

profiling

支持分析算子的profiling

环境准备

本工具支持x86和ARM的系统环境,使用前需要安装以下软件。

表2 安装软件及步骤

软件

安装步骤

mindspore-lite

安装版本:2.2.10

下载地址:https://www.mindspore.cn/lite/docs/zh-CN/r2.2/use/downloads.html

需要下载的安装包与机器规格有关,请根据需要选择合适的安装包。

安装方式如下:

MindSpore Lite云侧推理包解压缩后,设置`LITE_HOME`环境变量为解压缩的路径,例如:

export LITE_HOME=$some_path/mindspore-lite-2.2.10-linux-aarch64

设置环境变量LD_LIBRARY_PATH:

export LD_LIBRARY_PATH=$LITE_HOME/runtime/lib:$LITE_HOME/runtime/third_party/dnnl:$LITE_HOME/tools/converter/lib:$LD_LIBRARY_PATH

如果需要使用convert_lite或者benchmark工具,则需要设置环境变量PATH。

export PATH=$LITE_HOME/tools/converter/converter:$LITE_HOME/tools/benchmark:$PATH

cann

安装版本:CANN 7.0.0

下载地址:https://support.huawei.com/enterprise/zh/ascend-computing/cann-pid-251168373/software/258923273?idAbsPath=fixnode01%7C23710424%7C251366513%7C22892968%7C251168373

请下载toolkit和对应机器的kernels包,以Snt9B为例则下载“Ascend-cann-toolkit_7.0.0_linux-aarch64.run”和“Ascend-cann-kernels-型号_7.0.0_linux.run”。

安装命令(以Snt9B的cann安装为例):

./Ascend-cann-toolkit_7.0.0_linux-aarch64.run --full
./Ascend-cann-kernels-型号_7.0.0_linux.run --install

请安装在默认路径下:/usr/local/Ascend,暂不支持安装在自定义路径下。

tailor

安装版本:0.3.4

下载地址:

https://cneast3-modelarts-sdk.obs.cn-east-3.myhuaweicloud.com/tailor-0.3.4-py3-none-any.whl

SHA-256:

https://cneast3-modelarts-sdk.obs.cn-east-3.myhuaweicloud.com/tailor-0.3.4-py3-none-any.whl/1713929258832/tailor-0.3.4-py3-none-any.whl.sha256

安装命令:

pip install tailor-0.3.4-py3-none-any.whl

使用指导

tailor支持“命令行”和“Python API”两种方式使用。

  • 命令行方式

    命令行运行样例:

    tailor --model_path="./resnet50-v2-7.onnx"--config_path="./config.ini"--input_shape="data:1,3,224,224"--output_path="/home/"--accuracy="fp32"--aoe=True

    config.ini参考内容如下:

    [ascend_context]
    input_shape=data:[-1,3,224,224]
    dynamic_dims=[1],[2],[3]
    表3 参数说明

    参数名称

    功能描述

    参数类型

    是否必填

    默认值

    备注

    --model_path

    指定onnx模型路径。

    string

    -

    -

    --config_path

    指定模型配置文件路径。

    string

    -

    tailor支持动态分档转换功能,需要指定配置文件路径,需要注意即便有配置文件,只要是动态模型就需要指定--input_shape参数。

    --input_shape

    指定模型转换的shape。

    string

    -

    固定shape模型转换可以不填,动态模型转换必填。

    --output_path

    指定结果输出路径。

    string

    默认为当前目录下。

    -

    --aoe

    是否在转换时进行AOE优化。

    bool

    False

    AOE优化可以提升模型性能,但不是一定有提升,需要注意开启AOE,会导致模型转换耗时极大延长。

    --accuracy

    指定模型精度,只支持fp16和fp32。

    string

    fp16

    -

  • Python API
    • 导入包并创建tailor对象。
      from tailor.tailor import Tailor
      onnx_model_path = "./resnet50-v2-7.onnx"    # 相对路径或者绝对路径均可以
      t = Tailor(onnx_model_path)
    • 查询onnx模型的输入信息。
      # 查询onnx模型的输入信息
      t.get_model_input_info()
      图1 查询onnx模型的输入输出信息
    • 查询onnx模型的输出信息。
      # 查询模型的输出信息
      t.get_model_output_info()
      图2 查询onnx模型的输出信息
    • 固定shape模型,可以直接运行。
      t.run()
    • 指定档位信息运行。
      input_shape="data:1,3,224,224"
      t.run(input_shape=input_shape)
    • 动态档位执行config_path运行。需要注意,只要是动态模型,就必须要传入input_shape,因为转换模型后的benchmark和profiling都依赖单个shape操作。
      input_shape="data:1,3,224,224"
      config_path = "./resnet/config.ini"
      t.run(input_shape=input_shape, config_path=config_path)
    • 指定精度为fp32。
      input_shape="data:1,3,224,224"
      t.run(input_shape=input_shape, accuracy='fp32')
    • 开启AOE优化。
      input_shape="data:1,3,224,224"
      t.run(input_shape=input_shape, aoe=True)
    • 指定输出位置。
      input_shape="data:1,3,224,224"
      # 不指定输出路径,则默认在当前执行目录存储结果
      t.run(input_shape=input_shape, output_path="/home/xxx")

运行结果将存储在output文件夹中,如果用户指定了output_path,会指定位置保存,如果不指定则在当前代码执行目录生成文件夹保存输出。整体运行的结果都存放在output文件夹中,每转一次模型就会根据模型名称以及相关参数生成结果文件,如下图所示。

图3 output文件

在每次运行的结果文件中,分为三部分:convert、benchmark、profiling,相关的文件及存储内容如下。

表4 输出文件介绍(以模型名称为resnet50-v2-7.onnx为例)

类别

文件名称

是否一定生成

文件存储内容

convert

resnet50-v2-7.mindir

转换后的mindir模型。

resnet50-v2-7.om

转换过程中的om文件,不是必定生成。

onnx_to_mindspore.sh

模型转换命令,可以本地直接运行。

resnet50-v2-7_convert.log

模型转换过程的日志。

config.ini

配置文件,在指定fp32精度或者AOE打开时会生成。

onnx_to_mindspore_aoe.sh

在打开AOE功能时会生成。

benchmark

run_benchmark.sh

运行benchmark的脚本,可本地直接运行。

run_benchmark_accuracy.sh

benchmark运行精度的脚本,可本地直接运行。

performance.txt

benchmark性能测试结果。

accuracy.txt

精度测试结果。

*.bin

自动构造的输入随机bin文件,可能存在多个。

resnet50-v2-7_output.txt

上述bin文件作为输入时onnx模型运行的结果。

profiling

run_profiling.sh

运行profiling的脚本,可本地直接运行。

profiling.config

运行profiling的配置文件。

profiling.json

运行profiling的配置文件。

PROF_xxx开头的文件夹

运行profiling的结果文件夹。

run_aggregate.sh

运行数据聚合的脚本,可直接本地运行。

run_profiling.log

存储运行profiling的日志信息。

分享:

    相关文档

    相关产品