编译器使用
优质
小牛编辑
137浏览
2023-12-01
工具链介绍
gxnpuc
用于把模型文件编译成能在 NPU 上运行的 npu 文件。
usage: gxnpuc [-h] [-V] [-L] [-v] [-m] [-c CMD [CMD ...]] [config_filename]
NPU Compiler
positional arguments:
config_filename config file
optional arguments:
-h, --help show this help message and exit
-V, --version show program's version number and exit
-L, --list list supported ops
-v, --verbose verbosely list the processed ops
-m, --meminfo verbosely list memory info of ops
-c CMD [CMD ...], --cmd CMD [CMD ...]
use command line configuration
配置文件说明
配置项 | 选项 | 说明 |
---|---|---|
CORENAME | LEO | 芯片型号 |
PB_FILE | 包含 CKPT 的 PB 文件 | |
OUTPUT_FILE | 编译后生成的文件名 | |
NPU_UNIT | NPU32 / NPU64 | NPU 型号对应的 MAC 数(SNPU 选 NPU32,主 NPU 选 NPU64) |
COMPRESS | true / false | 是否启动压缩模式 |
COMPRESS_QUANT_BITS | 4/5/6/7/8 | 量化压缩的 bit 数 |
COMPRESS_TYPE | LINEAR / GAUSSIAN | 线性压缩还是高斯压缩,线性压缩准确率更高,但压缩率不如高斯 |
OUTPUT_TYPE | raw / c_code | Linux 环境下运行的模型选择 raw ,VSP 下运行选择 c_code |
INPUT_OPS | op_name: [shape] ... | 设置输入的 OP 名和 shape |
OUTPUT_OPS | [out_op_names, ...] | 设置输出的 OP 名列表 |
INPUT_DATA | op_name: [data] ... | 有些 Placeholder 的数据在部署时是确定的,需要写明 |
gxnpudebug
如果编译时配置文件中的 DEBUG_INFO_ENABLE 选项设置为 true,编译出的 npu 文件带上了调试信息,此时可以使用调试工具 gxnpudebug 工具来处理该文件。
usage: gxnpudebug [-h] [-S] [-P] file [file ...]
optional arguments:
-h, --help show this help message and exit
-P, --print_info print debug info
-S, --strip strip debug info
gxnpu_rebuild_ckpt
对权重数据做量化或做float16,并重新生成 ckpt 文件,用于评估模型压缩后对结果的影响。
usage: gxnpu_rebuild_ckpt [-h] config_filename
配置文件说明
配置项 | 选项 | 说明 |
---|---|---|
GRAPH_FILE | 包含 CKPT 的 PB 文件 | |
OUTPUT_CKPT | 输出的 CKPT 文件 | |
MODE | quant / float16 | 目前只用到float16 |
COMPRESS_TYPE | linear / gaussian | 压缩类型 |
MODE_BITS | 4 / 5 / 6 / 7 / 8 | 量化比特数 |
NPU_UNIT | NPU32 / NPU64 | NPU 型号对应的 MAC 数(SNPU 选 NPU32,主 NPU 选 NPU64) |
EXPECTED_OPS_NAME | input_name: output_name ... | 设置输入的 OP 名和 shape |
编译模型
模型文件准备
- 准备TensorFlow生成的PB和CKPT文件,或saved_model方式生成的模型文件。
- 通过TensorFlow提供的
freeze_graph.py
脚本生成frozen pb文件。
编写配置文件
- 编写yaml配置文件,包括pb文件名,输出文件名,输出文件类型,是否压缩,压缩类型,输入节点名和维度信息,输出节点名等。
编译
编译命令如下
gxnpuc config.yaml
需要注意,NPU工具链必须在安装有TensorFlow的环境下使用。
优化模型
为了让模型更高效地运行在 NPU 处理器上,需要对模型做一些优化。
- 做卷积和降采样的数据格式需要用 NCHW 格式,优化过程可以参考这里 。
- Placeholder 的维度信息需要确定。
- 各 OP 的 shape 需要确定,即和 shape 有关的 OP value 需要确定。
- Softmax 不建议放在 NPU 中,因为 NPU 使用 FP16 数据格式,容易导致数据溢出。