Qualcomm® AI Engine Direct 使用手册(18)

150 篇文章 14 订阅
50 篇文章 3 订阅


6.4 分析

6.4.1 qnn-量化-检查器(实验)

qnn-quantization-checker工具用于分析 qnn-converter 中单个模型文件或模型目录的每个后续层可用的所有可能量化选项的激活、权重和偏差。分析涉及将量化选项的权重、偏差和激活张量与未量化选项进行比较。该工具运行模型来生成浮点激活并分析浮点权重、偏差和激活以确定编码的质量。它最终输出所有量化选项的有问题的权重、偏差和激活张量。

X86-Linux/ WSL Usage: qnn-quantization-checker [--model MODEL_PATH]
                                [--input_list INPUT_LIST_PATH]
                                [--activation_width ACT_BW]
                                [--bias_width BIAS_BW]
                                [--output_dir OUTPUT_DIR_PATH]
                                [--skip_building_model]
                                [--skip_generator] [--skip_runner]
                                [--generate_histogram] [--per_channel_histogram]
                                [--output_csv]
                                --config_file CONFIG_FILE_PATH

X86-Windows/ Windows on Snapdragon Usage: python qnn-quantization-checker [--model MODEL_PATH]
                                [--input_list INPUT_LIST_PATH]
                                [--activation_width ACT_BW]
                                [--bias_width BIAS_BW]
                                [--output_dir OUTPUT_DIR_PATH]
                                [--skip_building_model]
                                [--skip_generator] [--skip_runner]
                                [--generate_histogram] [--per_channel_histogram]
                                [--output_csv]
                                --config_file CONFIG_FILE_PATH

所需参数:
–config_file - 配置文件的路径,指定执行所需的所有可能选项。
例如,[RUN_IN_REPAIR_MODE、MODEL_PATH、INPUT_LIST_PATH、ACTIVATION_WIDTH、BIAS_WIDTH、WEIGHT_WIDTH、INPUT_LAYOUT、OUTPUT_DIR_PATH、CLANG_PATH、BASH_PATH、BIN_PATH、PY3_PATH、TENSORFLOW_HOME、TFLITE_HOME、ONNX_HOME、QUANTIZATION_OVERRIDES、 WEIGHT_COMPARISON_ALGORITHMS、BIAS_COMPARISON_ALGORITHMS、ACT_COMPARISON_ALGORITHMS、INPUT_DATA_ANALYSIS_ALGORITHMS、OUTPUT_CSV、GENERATE_HISTOGRAM、QUANTIZATION_VARIATIONS、QUANTIZATION_ALGORITHMS ]

可选参数:
–model - 模型图形文件的路径,如果配置文件中未设置 MODEL_PATH,则需要此参数。–input_list - 包含输入文件列表的文本文件的路径。如果 MODEL_PATH/model 引用单个模型文件并且 INPUT_LIST_PATH 不存在于配置文件中,则这是必需的。–activation_width - [可选] 用于激活的位宽度。例如,8、16。默认值为 8。也可以使用 config_file 中的 ACTIVATION_WIDTH 字段进行设置。–bias_width - [可选] 用于偏差的位宽度。例如,8、32。默认为8。也可以使用config_file 中的BIAS_WIDTH 进行设置。–output_dir - [可选] 存储未量化输出文件的路径。也可以使用 config_file 中的 OUTPUT_DIR_PATH 进行设置。–skip_building_model - [可选] 停止工具构建模型。假设 model.so 是预先构建的。–skip_generator - [可选] 停止工具运行 QNN 转换器。假设必要的文件已经可用。–skip_runner - [可选] 停止工具运行模型。假设必要的文件已经可用。–generate_histogram - [可选] 生成权重/偏差的直方图分析。默认是跳过 histgoram 生成。–per_channel_histogram - [可选] 生成每个通道的权重/偏差直方图分析。默认是跳过 histgoram 生成。–output_csv - [可选] 将分析结果存储在输出目录中的 csv 文件中。

请注意:如果存在重叠,命令行上接受的参数将覆盖配置文件中的参数。

以下是配置文件中提到的不同算法和阈值的示例:

{
   // All other required user options can be added here

   "WEIGHT_COMPARISON_ALGORITHMS": [{"algo_name":"minmax", "threshold":"10"}, {"algo_name":"maxdiff", "threshold":"10"}, {"algo_name":"sqnr", "threshold":"26"}, {"algo_name":"stats", "threshold":"2"}, {"algo_name":"data_range_analyzer"}, {"algo_name":"data_distribution_analyzer", "threshold":"0.6"}],
   "BIAS_COMPARISON_ALGORITHMS": [{"algo_name":"minmax", "threshold":"10"}, {"algo_name":"maxdiff", "threshold":"10"}, {"algo_name":"sqnr", "threshold":"26"}, {"algo_name":"stats", "threshold":"2"}, {"algo_name":"data_range_analyzer"}, {"algo_name":"data_distribution_analyzer", "threshold":"0.6"}],
   "ACT_COMPARISON_ALGORITHMS": [{"algo_name":"minmax", "threshold":"10"}, {"algo_name":"data_range_analyzer"}]
}

该工具生成一个html目录,其中包含所有 .html 文件,其中包含量化选项和输入文件的不同组合的失败和成功的明确指示符。该工具还生成一个csv目录,其中包含存储每个指标的详细计算结果的所有 .csv 文件。此外,该工具还会在qnn-quantization-checker-log目录下生成一个输出日志文件,其中包含所有日志输出。

命令示例

qnn-quantization-checker --config_file ${SDK_ROOT}/lib/python/qti/aisw/quantization_checker/configs/quant_checker_config_normal.json

示例配置文件

{
"CLANG_PATH": "{Path_To_Clang_Lib}/clang-9.0.0/bin",
"PY3_PATH": "/{PY3_PATH}/py3env/bin",
"BASH_PATH": "/bin",
"BIN_PATH": "/usr/bin",
"TENSORFLOW_HOME": "/{Path_To_Tensorflow}/bionic/py3",
"TFLITE_HOME": "/{Path_To_TFLITE}/bionic/py3",
"ONNX_HOME": "/{Path_To_Onnx}/bionic/py3",
"MODEL_PATH": "/{Path_To_Model}/model_frozen.pb",
"INPUT_LIST_PATH": "/{Path_To_Input}/input_list.txt",
"OUTPUT_DIR_PATH": "{Path_To_Output_Dir}"
"WEIGHT_COMPARISON_ALGORITHMS": [{"algo_name":"minmax", "threshold":"10"}, {"algo_name":"maxdiff", "threshold":"10"}, {"algo_name":"sqnr", "threshold":"26"}, {"algo_name":"stats", "threshold":"2"}, {"algo_name":"data_range_analyzer"}, {"algo_name":"data_distribution_analyzer", "threshold":"0.6"}],
"BIAS_COMPARISON_ALGORITHMS": [{"algo_name":"minmax", "threshold":"10"}, {"algo_name":"maxdiff", "threshold":"10"}, {"algo_name":"sqnr", "threshold":"26"}, {"algo_name":"stats", "threshold":"2"}, {"algo_name":"data_range_analyzer"}, {"algo_name":"data_distribution_analyzer", "threshold":"0.6"}],
"ACT_COMPARISON_ALGORITHMS": [{"algo_name":"minmax", "threshold":"10"}, {"algo_name":"data_range_analyzer"}],
"INPUT_DATA_ANALYSIS_ALGORITHMS": [{"algo_name":"stats", "threshold":"2"}],
"OUTPUT_CSV": "True",
}

查看结果(html、csv 或日志文件)
所有结果都存储在用户指定目录下的输出目录中。

# In user_specified_dir, all .html files are stored in "user_specified_dir/html", .csv files are stored in "user_specified_dir/csv" and log files are stored in "user_specified_dir/snpe-quantization-checker-log".
cd ${Path_to_the_model}/user_specified_dir/html
# This directory contains all the html files produced by the snpe-quantization-checker which can be opened with any standard web browser.
cd ${Path_to_the_model}/user_specified_dir/csv
# This directory contains all the csv files produced by the snpe-quantization-checker which can be viewed with any standard text editor or spreadsheet application such as Excel, LibreOffice, etc.
cd ${Path_to_the_model}/user_specified_dir/snpe-quantization-checker-log
# This directory contains all the log files produced by the snpe-quantization-checker. The files are time stamped and can be viewed in any text editor.

If histogram generation is specified in the config file, histogram analysis for weights and biases are also produced into the respective directories as followed:
cd ${Path_to_the_model}/user_specified_dir/hist_analysis_weights
# This directory contains all the png files representing pixelwise data distribution for unquantized and quantized weights. The files can be viewed in any image viewer.
cd ${Path_to_the_model}/user_specified_dir/hist_analysis_biases
# This directory contains all the png files representing pixelwise data distribution for unquantized and quantized biases. The files can be viewed in any image viewer.

HTML 结果文件
每个 HTML 文件都包含每个量化选项和提供的每个输入文件的结果摘要。

笔记
为了清晰起见,添加了 HTML 内容的快照。

请添加图片描述
CSV 结果文件
每个 CSV 文件包含特定节点类型(激活/权重/偏差)和量化选项的详细计算结果。csv 文件中的每一行显示操作名称、节点名称、传递精度(真/假)、计算结果(精度差异)、每个算法使用的阈值以及算法名称。根据不同的算法/指标,计算结果(精度差异)格式可能会有所不同。

以下是有关不同算法和每个 csv 行包含的信息的一些简短说明:

  • minmax:表示未量化最小值与反量化最小值之间的差异。相应地,表示最大未量化值和反量化值的差值相同。
    计算结果示例:“min: #VALUE max: #VALUE”

  • maxdiff:计算所有数据点的未量化和反量化数据之间的绝对差,并显示结果的最大值。
    计算结果示例:“#VALUE”

  • sqnr:计算未量化和反量化数据的两个张量之间的信号与量化噪声比。
    计算结果示例:“#VALUE”

  • data_range_analyzer:计算张量中最大值和最小值之间的差,并将其与位宽支持的最大值进行比较,以确定所选量化位宽是否可以合理地表示值的范围。
    计算结果示例:“唯一小数位数:#INT_VALUE 数据范围:#VALUE”。计算结果字段中的信息包括我们需要多少位唯一的小数位来以量化格式表达非量化数据以及实际的数据范围是多少。

  • data_distribution_analyzer:计算数据的聚类,以查找大量唯一的未量化值是否被量化为相同的值。
    计算结果示例:“高于阈值的像素分布:#VALUE”

  • stats:计算接收到的数据的一些基本统计数据,例如最小值、最大值、中值、方差、标准差、众数和偏斜。偏度用于指示数据的对称程度。
    计算结果示例: 偏斜:#VALUE 最小值:#VALUE 最大值:#VALUE 中值:#VALUE 方差:#VALUE stdDev:#VALUE 模式:#VALUE

笔记
下面添加了量化选项之一的重量数据的 CSV 文件内容快照。
请添加图片描述
单独的 .csv 文件可用于每个量化选项的激活、权重和偏差。与激活相关的结果还包括对所提供的每个输入文件的分析。

记录结果文件
日志文件包含以下信息:

  1. 作为脚本运行的一部分执行的所有命令。这将包括具有不同量化选项的 qnn-converter 工具的不同运行

  2. 激活分析失败

  3. 权重分析失败

  4. 偏差分析失败

示例日志输出如下所示:

<====ACTIVATIONS ANALYSIS FAILURES====>

Results for the enhanced quantization:
|         Op Name         | Activation Node | Passes Accuracy |         Accuracy Difference          | Threshold Used | Algorithm Used |
|  conv_tanh_comp1_conv0  |    ReLU_6919    |      False      | minabs_diff: 0.59 maxabs_diff: 17.16 |      0.05      |     minmax     |

在哪里,

  • Op Name:Op Name,如 qnn_model.cpp 中所示

  • 激活节点:Op中的激活节点名称

  • 通过准确性:如果与 float32 图表中的值相比,量化激活(或权重或偏差)满足阈值,则通过

  • 准确度差异:有关所使用算法的准确度的详细信息

  • 使用的阈值:用于影响“Passes Accuracy”列结果的阈值

  • 使用的算法:用于将实际量化激活/权重/偏差与未量化浮点数据进行比较或分析未量化浮点数据质量的指标。指标可以是 minmax、maxdiff、sqnr、stats、data_range_analyzer、data_distribution_analyzer。

修复模式
此功能增加了“修复”图中由于量化而导致精度较差的节点的能力。该工具运行并识别由于量化而表现出较差精度的节点。根据违反的规则,确定纠正措施以提高性能(准确性)。然后使用所决定的纠正措施重新运行该工具,并再次评估节点以查看准确性是否有所提高。确定节点的性能是使用量化检查器工具中已使用的当前比较算法集来完成的。当前的实现仅限于增加表现最差的张量(权重、偏差或激活)的位宽,作为要采取的纠正措施,即“修复”。

笔记
修复模式并不能保证准确性的提高,而只是尝试进行最简单的更改,从而为最终用户消除了一个额外的调试步骤。

修复模式还通过向配置文件提供以下参数来支持手动模式:
RUN_IN_REPAIR_MODE 类型:布尔(真/假)
描述:在修复模式下运行 [snpe, qnn]-quantization-checker。该模式将识别由于量化而表现出较差精度的节点,并进一步调整量化编码以提高所识别节点的精度。然后使用更新的编码值重新运行该工具,并在完成后给出准确性改进的报告。

修复模式的输出示例:

MINMAX: Percentage of Failed Nodes

---------------------------------------------------------------------------------------------------------------------
| Tensor Type | unquantized |         tf          |       tf_cle        |      tf_pcq         |     tf_cle_pcq      |
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
|   Weights   |     N/A     |        0.0          |         0.0         |         0.0         |         0.0         |
---------------------------------------------------------------------------------------------------------------------
|   Biases    |     N/A     |        0.0          |         0.0         |         0.0         |         0.0         |
---------------------------------------------------------------------------------------------------------------------
| Activations |     N/A     | 0.11634671320535195 | 0.11634671320535195 | 0.11634671320535195 | 0.11634671320535195 |
---------------------------------------------------------------------------------------------------------------------

故障排除步骤
以下步骤适用该工具有一些源自 snpe 二进制文件要求的要求/依赖项:

  • 必须安装 clang 9

  • 必须使用 python 3 venv

  • 必须安装 bash shell 解释器

  • 根据正在分析的模型类型,必须至少安装以下框架之一:于使用该工具有困难的最终用户。这些是需要调查的常见领域,但并不全面。

    • Tensorflow

    • Onnx

    • TFLite

此外,可以进行一些检查以确保模型在第一次尝试时运行:

  • 确保指定给要分析的模型的路径是正确的

  • 确保输出目录的路径存在并且可写

  • 使用提供的预定义配置文件并根据您的本地设置填写详细信息,即本地路径

  • 该工具突出显示的故障可能是可接受的故障,具体取决于模型,由用户根据所分析的特定模型的用户知识来确定故障是否表明存在问题

  • python 错误通常是由于版本不匹配造成的,该工具仅适用于 Python 3.6 及更高版本,目前主要针对 Python 3.8

6.4.2 qnn-accuracy-evaluator(实验)

qnn-accuracy-evaluator工具用于识别给定输入集上模型的最佳量化选项。此外,该工具还提供了一个框架来评估给定数据集上模型的端到端准确性指标。

依赖关系

QNN 精度评估器假定已遵循“设置”页面中概述的平台依赖性和环境设置说明。此外,还应为此工具安装以下 python 软件包。

torch==1.13.1
torchvision==0.14.1
onnxruntime==1.9.0
onnxsim==0.4.5

运行以下命令检查并安装所有 python 依赖项:

$ source ${QNN_SDK_ROOT}/bin/envsetup.sh
$ ${QNN_SDK_ROOT}/bin/check-python-dependency
$ python3 -m pip install torch==1.13.1
$ python3 -m pip install torchvision==0.14.1
$ python3 -m pip install onnxruntime==1.9.0
$ python3 -m pip install onnxsim==0.4.5

笔记
要进行设置,QNN_SDK_ROOT请参阅Linux 的环境设置。

用法
用户需要将 QNN_SDK_ROOT 环境变量设置为 QNN SDK 的根目录。可能需要使用适当的值设置以下环境变量: QNN_MODEL_ZOO :模型动物园的路径。如果未设置 model_zoo 基目录路径,则假定为“/home/model_zoo”。注意:仅当提供的模型路径不是绝对路径且相对于设置的模型动物园路径时,才需要此环境变量。ADB_PATH :设置 ADB 二进制文件的路径。如果未设置,则假定 ADB 位于“/opt/bin/adb”。

该工具有两种使用模式 - 最小模式和配置模式。

最小模式选项是对所有可能的量化选项执行精度分析,并在与 cpu fp32 输出进行比较时根据给定的比较器对它们进行排序。

配置模式通过自定义后端、量化选项和参考推理模式,支持给定数据集上模型的准确性分析。

支持机型
qnn-accuracy-evaluator 目前支持 ONNX 和 PyTorch (torchscript) 模型。

注意:对于 PyTorch 模型,必须根据 QNN 的命名约定(在适用的情况下)提供模型的输入和输出节点信息,在使用 qnn-pytorch-converter 执行此类模型时可以观察到该命名约定。

最小模式
最小模式选项是对所有可能的量化选项执行精度分析,并在与 cpu fp32 输出进行比较时根据给定的比较器对它们进行排序。

qnn-accuracy-evaluator Options

minimal mode options:
    -model MODEL          path to model or model directory
    -backend {cpu,htp,aic,htp_mcp,dspv69,dspv73,dspv75,gpu}
                          Backend to run the inference
    -target-arch {aarch64-android,x86_64-linux-clang}
                          Target architecture to compile.
    -preproc-file PREPROC_FILE
                          Path to a file specifying paths to raw inputs.
    -comparator COMPARATOR
                          comparator to be used.
    -tol-thresh TOL_THRESH
                          Tolerance threshold to be used for the comparator
    -act-bw ACT_BW        [Optional] bitwidth to use for activations. E.g., 8,
                          1.  Default is 8.
    -bias-bw BIAS_BW      [Optional] bitwidth to use for biases. either 8
                          (default) or 32.
    -box-input BOX_INPUT  Path to the json file. Used only with the box
                          comparator

other options:
    -input-info INPUT_INFO INPUT_INFO
                            The name and dimension of all the input buffers to the
                            network specified in the format [input_name comma-
                            separated-dimensions], for example: 'data'
                            1,224,224,3. This option is mandatory for pytorch
                            models in minimal mode.
    -onnx-symbol ONNX_SYMBOL [ONNX_SYMBOL ...]
                            Replace onnx symbols in input/output shapes.Can be
                            passed multiple timesDefault replaced by 1. e.g
                            __unk_200:1
    -device-id DEVICE_ID  Target device-id/device-serial to be provided
    -work-dir WORK_DIR    working directory path. default is ./qacc_temp
    -silent               Run in silent mode
    -debug DEBUG          Set logging level to DEBUG within the tool

笔记
对于主机上的 HTP 后端模拟,请在 CLI 命令中将 backend 设置为“htp”,将 target-arch 设置为“x86_64-linux-clang”。
对于 Android 设备上的 HTP 后端执行,用户必须在 CLI 命令中指定后端以及版本(例如“dspv69/v73/v75”)和 target-arch 为“aarch64-android”。

命令示例

qnn-accuracy-evaluator -model ${QNN_MODEL_ZOO}/onnx-cnns_mobilenet/Source_model/model.onnx
                       -preproc-file ${QNN_MODEL_ZOO}/onnx-cnns_mobilenet/inputs/input_list.txt
                       -backend htp
                       -target-arch x86_64-linux-clang
                       -comparator abs
                       -tol-thresh 0.1

结果

该工具显示一个表格,其中包含根据所选比较器按输出匹配排序的量化选项,并生成具有相同数据的 csv 文件。比较器列显示基于所选比较器的输出匹配百分比/值。定量参数列显示用于该运行的量化参数。其他列还显示使用的后端、运行时/编译参数。该信息还存储在位于{work_dir}/metrics-info.csv的 csv 文件中。

笔记
HTP MCP 编译参数“elf_path”默认设置为 $QNN_SDK_ROOT/lib/hexagon-v68/unsigned/libQnnHtpMcpV68.elf。用户可以通过更新 $QNN_SDK_ROOT/lib/python/qti/aisw/accuracy_evaluator/common/defaults.py 中的 qacc.default_inference_schemas.htp_mcp_x86.inference_schema.compiler_params.elf_path 来更改路径

在最小模式下运行的量化选项组合:

param_quantizer:  tf | symmetric | enhanced | adjusted
algorithms: default | cle
use_per_channel_quantization: True | False

每个量化选项工作目录都存储在{work_dir}/infer/schema{i}qnn{quant_option}中。QNN IR 文件存储在 {work_dir}/infer/schema{i}qnn{quant_option}/qnn_ir 中,输出存储在{work_dir}/infer/schema{i}qnn{quant_option}/Result_{i} 中。

笔记
为了清晰起见,添加了控制台日志的快照。

请添加图片描述

笔记
为了清晰起见,添加了 csv 文件的快照。

请添加图片描述
配置模式
配置模式通过自定义后端、量化选项和参考推理模式,支持给定数据集上模型的准确性分析。示例配置文件可以在${QNN_SDK_ROOT}/lib/python/qti/aisw/accuracy_evaluator/configs/samples/model_configs找到 。

模型配置的高级结构如下所示:

model
    info
    globals
    dataset
    processing
        preprocessing
        postprocessing
    inference-engine
    evaluator

用户需要在模型配置文件中的数据集部分下提供所有数据集信息,否则将引发错误。下面显示了一个示例:

dataset:
    name: COCO2014
    path: '/home/ml-datasets/COCO/2014/'
    inputlist_file: inputlist.txt
    calibration:
        type: index
        file: calibration-index.txt

数据集字段详细信息如下:

在这里插入图片描述
推理引擎用于在多个推理模式上运行模型。下面显示了示例推理引擎部分,后面是推理部分中不同可配置条目的描述。

inference-engine:
    model_path: MLPerfModels/ResNetV1.5/modelFiles/ONNX/resnet50_v1.onnx
    simplify_model : True
    inference_schemas:
        - inference_schema:
            name: qnn
            target_arch: x86_64-linux-clang
            backend: aic
            precision: quant
            tag: aic_int8
            converter_params:
                param_quantizer: tf | symmetric | enhanced | adjusted
            compiler_params:
                compiler_num_of_cores: 8
            runtime_params:
                runtime_device_id: 0
    inputs_info:
        - input_tensor_0:
              type: float32
              shape: ["*", 3, 224, 224]
    outputs_info:
        - ArgMax_0:
              type: int64
              shape: ["*"]
        - softmax_tensor_0:
              type: float32
              shape: ["*", 1001]

每个可配置条目的详细信息如下:

在这里插入图片描述

笔记
对于主机上的 HTP 后端模拟,请在配置文件中将 backend 设置为“htp”,将 target_arch 设置为“x86_64-linux-clang”。
对于 Android 设备上的 HTP 后端执行,用户必须在配置文件中将后端以及版本(例如 dspv69 / dspv73 / dspv75 和 target_arch )指定为“aarch64-android”。

评估器部分提供有关在多个推理模式的情况下用于比较推理输出的比较器的信息。下面显示了示例评估器部分,后面是该部分中不同可配置条目的描述。

evaluator:
    comparator:
        enabled: True
        fetch-top: 1
        type: avg
        tol: 0.001

每个可配置条目的详细信息如下:

在这里插入图片描述
配置模式可用的命令行选项如下:

qnn-acc-evaluator options

required options:
    -config CONFIG        path to model config yaml

    pipeline options:
    -preproc-file PREPROC_FILE
                            preprocessed input file, overrides inputfile provided in model config
    -calib-file CALIB_FILE
                            calibration input file, overrides calibration file provided in model config

other options:
    -device-id DEVICE_ID    Target device id to be provided
    -work-dir WORK_DIR      working directory path. default is ./qacc_temp
    -silent                 Run in silent mode
    -debug DEBUG            Set logging level to DEBUG within the tool
    -inference-schema INFERENCE_SCHEMA
                            run only on this inference schema
    -inference-schema-tag INFERENCE_SCHEMA_TAG
                            run only this inference schema tag
    -batchsize BATCHSIZE    overrides batchsize provided in model config
    -onnx-symbol ONNX_SYMBOL [ONNX_SYMBOL ...]
                            Replace onnx symbols in input/output shapes.Can be
                            passed multiple timesDefault replaced by 1. e.g
                            __unk_200:1
    -set-global SET_GLOBAL [SET_GLOBAL ...]
                            Replace global symbols with given value.Can be passed
                            multiple times. e.g <symbol>:2

命令示例

qnn-accuracy-evaluator -config {path to configs}/qnn_resnet50_config.yaml

结果

请参阅最小模式部分

配置文件选项

- inference_schema:
    name: qnn
    target_arch: x86_64-linux-clang
    backend: cpu
    precision: fp32
    tag: qnn_cpu_x86

- inference_schema:
    name: qnn
    target_arch: aarch64-android
    backend: cpu
    precision: fp32
    tag: qnn_cpu_android

- inference_schema:
    name: qnn
    target_arch: aarch64-android
    backend: gpu
    precision: fp32
    tag: qnn_gpu_android

- inference_schema:
    name: qnn
    target_arch: x86_64-linux-clang
    backend: htp
    precision: quant
    tag: htp_int8
    runtime_params:
        vtcm_mb: 4
        rpc_control_latency: 100
    converter_params:
        param_quantizer: tf | symmetric | enhanced | adjusted
        act_quantizer: tf | symmetric
        algorithms: default | cle
        use_per_channel_quantization: True | False
        quantization_overrides: "path to the ext quant json"
        act_bw: 8 | 16
        bias_bw: 8 | 32
        weight_bw: 8

- inference_schema:
    name: qnn
    target_arch: aarch64-android
    backend: dspv69/dspv73/dspv75
    precision: quant
    tag: htp_int8
    runtime_params:
        vtcm_mb: 4
        rpc_control_latency: 100
    converter_params:
        param_quantizer: tf | symmetric | enhanced | adjusted
        act_quantizer: tf | symmetric
        algorithms: default | cle
        use_per_channel_quantization: True | False
        quantization_overrides: "path to the ext quant json"
        act_bw: 8 | 16
        bias_bw: 8 | 32
        weight_bw: 8

- inference_schema:
    name: qnn
    target_arch: x86_64-linux-clang
    backend: aic
    precision: fp16
    tag: aic_fp16
    runtime_params:
        runtime_device_id: 0
        runtime_num_activations: 100
    compiler_params:
        compiler_num_of_cores: 8
        compiler_convert_to_FP16: true
        compiler_perfWarnings: true

- inference_schema:
    name: qnn
    target_arch: x86_64-linux-clang
    backend: aic
    precision: quant
    tag: aic_int8
    runtime_params:
        runtime_device_id: 0
        runtime_num_activations: 100
    converter_params:
        param_quantizer: tf | symmetric | enhanced | adjusted
        act_quantizer: tf | symmetric
        algorithms: default | cle
        use_per_channel_quantization: True | False
        quantization_overrides: "path to the ext quant json"
        act_bw: 8 | 16
        bias_bw: 8 | 32
        weight_bw: 8
    compiler_params:
        compiler_num_of_cores: 8
        compiler_perfWarnings: true

- inference_schema:
    name: qnn
    target_arch: x86_64-linux-clang
    backend: htp_mcp
    precision: fp16
    tag: htp_mcp_fp16
    runtime_params:
        device_id: 0
        timeout: 5000
    compiler_params:
        fp16_relaxed_precision: 1
        num_cores: 1
        elf_path: "path to .elf"     # Should be either an absolute path or
                                     # relative to QNN_SDK_ROOT. Sample file
                                     # can be found in
                                     # $QNN_SDK_ROOT/lib/hexagon-v68/unsigned/libQnnHtpMcpV68.elf

- inference_schema:
    name: qnn
    target_arch: x86_64-linux-clang
    backend: htp_mcp
    precision: int8
    tag: htp_mcp_int8
    runtime_params:
        device_id: 0
        timeout: 5000
    converter_params:
        param_quantizer: tf | symmetric | enhanced | adjusted
        act_quantizer: tf | symmetric
        algorithms: default | cle
        use_per_channel_quantization: True | False
    compiler_params:
        num_cores: 1
        elf_path: "path to .elf"     # Should be either an absolute path or
                                     # relative to QNN_SDK_ROOT. Sample file
                                     # can be found in
                                     # $QNN_SDK_ROOT/lib/hexagon-v68/unsigned/libQnnHtpMcpV68.elf

比较器

以下是可用于比较输出的比较器。一些比较器输出两个张量之间的百分比匹配,一些比较器输出与所选比较器相对应的绝对值。

  1. abs - 基于相对容差阈值的两个张量之间的百分比匹配

  2. cos - 基于余弦相似度得分的两个张量之间的百分比匹配

  3. topk - 基于两个张量之间的 topk 匹配的两个张量之间的百分比匹配

  4. avg - 基于两个张量之间的平均差的两个张量之间的百分比匹配

  5. l1norm - 基于差异的 L1 范数的两个张量之间的百分比匹配

  6. l2norm - 基于差异的 L2 范数的两个张量之间的百分比匹配

  7. std - 基于标准差差异的两个张量之间的百分比匹配

  8. rme - 基于张量之间的 RMSE 的两个张量之间的百分比匹配

  9. snr - 两个张量之间的信噪比

  10. maxerror - 两个张量之间的最大误差值

  11. kld - KL 两个张量之间的散度值

  12. PixelbyPixel - 两个张量之间的逐像素图差异。对于每个输入 i,绘图保存在{work_dir}/{schema}/Result_{i}

  13. box - box 验证程序需要–box_input参数,该参数接受以下格式的 json 文件的文件名:

    {"box":"Result_0/detection_boxes_0.raw",
    "class":"Result_0/detection_classes_0.raw",
    "score":"Result_0/detection_scores_0.raw"}
    
    

型号目录

在最小模式下,用户还可以传递模型目录。目录结构预计采用以下格式:

model_dir/
  |__model1/
  |     |__Source_model/
  |     |       |__model.onnx
  |     |__inputs/
  |     |       |__data/images.raw
  |     |       |__input_list.txt
  |__model2/
  |     |__Source_model/
  |     |       |__model.onnx
  |     |__inputs/
  |     |       |__data/images.raw
  |     |       |__input_list.txt

每个模型的输出目录将位于{work_dir}/{model_dir}

qnn-architecture-checker(实验)
Architecture Checker 是一款针对 HTP 后端运行的模型而设计的工具,包括量化 8 位、量化 16 位和 FP16 模型。它输出模型中的问题列表,这些问题使模型在 HTP 后端运行时无法获得更好的性能。可以使用修改器功能调用架构检查器工具,该修改器功能将为这些问题应用建议的修改。这将有助于可视化可应用于模型的更改,使其更适合 HTP 后端。

X86-Linux/ WSL Usage:
$ qnn-architecture-checker -i <path>/model.json
                         -b <optional_path>/model.bin
                         -o <optional_output_path>
                         -m <optional_modifier_argument>

X86-Windows/ Windows on Snapdragon Usage:
$ python qnn-architecture-checker -i <path>/model.json
                         -b <optional_path>/model.bin
                         -o <optional_output_path>
                         -m <optional_modifier_argument>

 required arguments:
     -i INPUT_JSON, --input_json INPUT_JSON
                             Path to json file

 optional arguments:
     -b BIN, --bin BIN
                     Path to a bin file
     -o OUTPUT_PATH, --output_path OUTPUT_PATH
                     Path where the output csv should be saved. If not specified, the output csv will be written to the same path as the input file
     -m MODIFY, --modify MODIFY
                     The query to select the modifications to apply.
                         --modify or --modify show - To see all the possible modifications. Display list of rule names and details of the modifications.
                         --modify all - To apply all the possible modifications found for the model.
                         --modify apply=rule_name1,rule_name2 - To apply modifications for specified rule names. The list of rules should be comma separated without spaces

笔记:
如果在量化模型上运行,则使用一幅输入图像生成的量化模型足以满足使工具正常运行的量化要求。
运行该工具之前必须配置 QNN_SDK_ROOT 环境变量。
弃用注意事项:
通过在上面列出的每个转换器中传递“–arch_checker”来启用架构检查器的选项将被弃用。例如:运行 qnn-tflite-converter -i <路径>/model.tflite -d <网络输入名称> -o <可选输出路径> -p <可选包名称> –arch_checker 将被弃用。
要启用架构检查器,请运行转换器工具而不传递“–arch_checker”参数,然后运行 ​​qnn-architecture-checker 命令以查看架构检查器输出。
仅 qnn-architecture-checker 命令支持使用“–modify”。

输出是 csv 文件,将保存为 <可选输出路径>/<模型名称>_architecture_checker.csv。示例输出如下所示:

在这里插入图片描述
如何读取示例输出 csv?
第 1 行:这是图上的一个问题,该图使用 16 位激活数据,正如建议中所述,将激活从 16 位更改为 8 位可以提供更好的性能。
第 2 行:问题出现在 QNN 节点名称为“Node_name_1”的节点上。该节点具有三个输入:input_1、__param_1 和 convolution_0_bias,其中维度分别为 [1, 250, 250, 3]、[5, 5, 3, 32] 和 [32]。该节点有一个输出,QNN 张量名称为 output_1,该张量的维度为 [1, 123, 123, 32]。该节点的类型是Conv2d。“参数”列中提供了上一个/下一个节点名称以及完整的附加节点参数集,可用于在原始模型内定位节点。该节点的问题是输入张量的通道较低,因为通道小于 32,建议将通道增加到至少 32,以便在 HTP 后端获得更好的性能。目前输入维度为 [1, 250, 250, 3],理想情况下为 [1, x, x, 32]。Modification 和 Modification_info 列提供有关应用于节点的修改的详细信息。如果架构检查器未使用修改器调用,或者没有任何适用的修改,则这些值将为 N/A。
QNN 节点/张量名称与原始模型中的名称相同吗?
不一样但应该相似。为了满足 QNN 命名标准,转换器中进行了命名清理。输入张量、输出张量、前一个节点、下一个节点以及所有附加参数在输出 csv 文件中可用,以帮助在原始模型中找到正确的节点。
命令示例

qnn-architecture-checker --input_json ./model_net.json
                       --bin ./model.bin
                       --output_path ./archCheckerOutput

架构检查器 - 模型修改器

为了将修改应用于模型,可以使用“–modify”或“–modify show”调用架构检查器,这将显示可能修改的列表。在这种情况下,架构检查器工具将仅显示规则名称和修改详细信息。它将在不对模型进行任何更改的情况下运行并生成 csv 输出。使用上述运行中的规则名称,可以通过“–modify all”或“–modify apply=rule_name1,rule_name2”调用架构检查器。在这种情况下,规则特定的更改将应用​​于模型,并且可以在更新的模型 json 中查看更改。此外,输出 csv 还将包含与修改相关的信息。

考虑在示例模型上应用“–modify apply=elwisediv”修改后生成的以下 csv 输出。

在这里插入图片描述
如何读取示例输出 csv?
第 1 行:QNN 节点名称为“Node_name_1”的节点的问题在于它具有元素除法,与元素乘法相比,性能较差。使用“–modify apply=elwisediv”调用架构检查器后,修改已成功应用,即元素明智除法被元素明智乘以倒数值取代。此信息可在 Modification 和 Modification_info 列中找到。
第 2 行:QNN 节点名称为“Node_name_2”的节点上的问题是该节点的输入张量的通道数小于 32。建议将通道数增加到 32 或更多以获得更好的性能。对于此问题,通过工具进行的修改不适用,因此 Modification 和 Modification_info 列不适用。
修改模型后,上述运行将生成更新的 model.cpp、model_net.json 和/或 model.bin 以及 csv 输出。在更新后的模型 json 上运行架构检查器将不再显示 Node_Name_1 上的元素明智划分问题。
以下是使用修改器调用架构检查器以显示修改列表的命令:

命令示例

qnn-architecture-checker --input_json ./model_net.json
                       --bin ./model.bin
                       --output_path ./archCheckerOutput
                       --modify

命令示例

qnn-architecture-checker --input_json ./model_net.json
                       --bin ./model.bin
                       --output_path ./archCheckerOutput
                       --modify show

以下是将修改应用于所有可能的修改或特定规则的命令:

命令示例

qnn-architecture-checker --input_json ./model_net.json
                       --bin ./model.bin
                       --output_path ./archCheckerOutput
                       --modify all

命令示例

qnn-architecture-checker --input_json ./model_net.json
                       --bin ./model.bin
                       --output_path ./archCheckerOutput
                       --modify apply=prelu,elwisediv

笔记:
带修改器的架构检查器是一项增强功能,可帮助可视化可应用于模型的更改,以更好地适应 HTP。为了看到实际的性能改进,模型可能需要重新训练/重新设计。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值