247 lines
15 KiB
Markdown
247 lines
15 KiB
Markdown
# Transformer
|
||
 <br> <br><a href="../../index.md#nlp" target="_blank"><img alt="模型清单" src="https://img.shields.io/badge/nlp-模型清单-cornflowerblue?logo=quicklook"></a><br>
|
||
|
||
# 下载
|
||
|
||
✨ 一键下载开发流程中所需的各种文件,包括编译使用的量化校准集、运行时工程的依赖库,以及输入输出文件。
|
||
|
||
💡 推荐使用linux版下载脚本,其wget包含断网自动重连功能,不会出现下载文件遗漏情况。
|
||
|
||
## windows
|
||
📌 第一次使用,请在C盘根目录下新建`icraft_auth.txt`,保存下载站账号密码,以换行符分隔
|
||
|
||
需要事先下载windows版本wget:
|
||
|
||
(若点击以下链接后未直接下载,请选择 ***1.20.3*** 版本下的对应系统链接进行下载)
|
||
|
||
[x86系统wget下载](https://eternallybored.org/misc/wget/1.20.3/32/wget.exe) [x64系统wget下载](https://eternallybored.org/misc/wget/1.20.3/64/wget.exe)
|
||
|
||
使用时需要将wget.exe的路径作为命令行参数传入,注意不是exe的父文件夹目录,而是包含wget.exe的完整绝对路径:
|
||
|
||
不下载Deps:`./download.ps1 "PATH_TO_WGET_EXE"`
|
||
|
||
如果您是第一次使用我们的模型库,请下载包括工程依赖库的所有文件:`./download.ps1 "PATH_TO_WGET_EXE" -d`
|
||
|
||
💡 下载过程中可能因网络问题出现中断情况,需 **自行重新运行** 下载脚本。
|
||
|
||
## linux
|
||
|
||
📌 第一次使用,请在/usr根目录下新建`icraft_auth.txt`,保存下载站账号密码,以换行符分隔
|
||
|
||
为确保文件格式正确,请在运行脚本前安装格式转换工具`dos2unix`,并执行格式转换命令:
|
||
```shell
|
||
sudo apt-get install dos2unix
|
||
dos2unix /usr/icraft_auth.txt
|
||
dos2unix ./download.sh
|
||
```
|
||
|
||
如果您是第一次使用我们的模型库,请下载包括工程依赖库的所有文件:`./download.sh -d`
|
||
|
||
如果之前已经在使用别的模型时下载过Deps依赖库,可以直接将其中的thirdparty部分复制到路径`3_deploy/Deps`,只需下载量化校准集和输入输出文件即可:`./download.sh`
|
||
|
||
|
||
🌟 Tips:
|
||
|
||
- 若想要直接获取原始weights和导出保存的模型,可分别前往 [weights](https://download.fdwxhb.com/data/04_FMSH-100AI/100AI/04_modelzoo/modelzoo_pub/weights/) 和 [fmodels](https://download.fdwxhb.com/data/04_FMSH-100AI/100AI/04_modelzoo/modelzoo_pub/compile/fmodels/) 网页上根据框架及模型名寻找并下载。
|
||
|
||
|
||
# 0. 文件结构说明
|
||
|
||
AI部署模型需要以下几部分文件
|
||
|
||
- 0_Chinese-Text-Classification-Pytorch >存放原始权重,需要自行下载
|
||
- 1_scripts >若干脚本,用于保存Icraft编译器需要的模型、编译后仿真等功能
|
||
- 2_compile >Icraft编译器编译模型时所需要的文件
|
||
- 3_deploy >将Icraft编译器编译出的模型部署到硬件时需要的python工程
|
||
|
||
# 1. python工程准备
|
||
|
||
## 1. **模型来源:**
|
||
|
||
- code:https://github.com/649453932/Chinese-Text-Classification-Pytorch.git
|
||
- branch:master
|
||
- commit_id:6cb2681
|
||
- weights:https://download.fdwxhb.com/data/04_FMSH-100AI/100AI/04_modelzoo/modelzoo_pub/weights/pytorch/Transformer/Transformer.ckpt
|
||
|
||
## 2. **保存模型**
|
||
|
||
**目的:将模型保存成可以被Icraft编译器编译的形态**
|
||
|
||
1)根据模型来源中的地址:https://download.fdwxhb.com/data/04_FMSH-100AI/100AI/04_modelzoo/modelzoo_pub/weights/pytorch/Transformer/Transformer.ckpt,下载原始weights,存放于 `/weights`文件夹中
|
||
|
||
<div style="background-color: #FFFFCC; color: #000000; padding: 10px; border-left: 5px solid #FFA500;">
|
||
注意:
|
||
|
||
* 有时开源的weights url可能会变更。如果上述weights url失效,请根据原工程相应的branch以及commit版本寻找正确的下载链接
|
||
* 若上述weights url永久失效,请联系本模型库相关人员获取权限下载
|
||
</div>
|
||
**1_scripts提供脚本说明:**
|
||
|
||
- **环境要求:**Icraft编译器对**导出框架模型时**使用的**框架版本**有要求。即以下脚本中所有导出模型的脚本`1_save.py `,必须在要求的框架版本下执行,其他脚本不限制。要求的版本:
|
||
|
||
- **pytorch**:1.9
|
||
- **paddle**: 2.3.2
|
||
- **onnx**: opset11
|
||
- **darknet**: [GitHub - pjreddie/darknet: Convolutional Neural Networks](https://github.com/pjreddie/darknet)
|
||
|
||
- 0_infer.py >可以推理一句话并得到最终结果,模型原始权重会从 `/weights `中寻找,需要您预先下载
|
||
|
||
<div style="background-color: #FFFFCC; color: #000000; padding: 10px; border-left: 5px solid #FFA500;">
|
||
源码的修改点:<br>
|
||
1. 0_Chinese-Text-Classification-Pytorch /models/Transformer.py,将31行的self.batch_size改为 1<br>
|
||
</div>
|
||
|
||
- 1_save.py >保存模型,保存好的用于Icraft编译器的模型,会存放在 `/2_compile/fmodel`
|
||
|
||
<div style="background-color: #FFFFCC; color: #000000; padding: 10px; border-left: 5px solid #FFA500;">
|
||
保存模型时的修改点:<br>
|
||
1. Model前向进行替换,去除embedding<br>
|
||
</div>
|
||
|
||
- 2_save_infer.py >用修改后保存的模型做前向推理,验证保存的模型与原模型是否一致
|
||
|
||
|
||
|
||
|
||
# 2.使用Icraft编译器编译模型
|
||
|
||
目的: 使用[Icraft编译器](https://gitee.com/mxh-spiger/icraft-introduction.git)将上一步保存好的**框架模型**转化为**硬件可部署模型**
|
||
|
||
- **1)相关命名说明:**
|
||
|
||
1)**fmodel**:frame model >用于Icraft编译器的框架模型
|
||
|
||
2)**imodel**:icraft model >用Icraft编译器编译出的模型
|
||
|
||
3)**qtset**:Quantitative Calibration Set >Icraft编译器所需的量化校准集
|
||
|
||
|
||
|
||
- **2)确认已安装正确的icraft版本**
|
||
|
||
检查方法:打开cmd运行:`icraft --version`
|
||
|
||
若已正常安装则会显示当前icraft版本,例如:
|
||
|
||
```
|
||
Icraft 版本:
|
||
* 3.7.1
|
||
|
||
CLI 版本:
|
||
3.7.0.0-a90988f(2412231401)
|
||
```
|
||
|
||
|
||
|
||
- 3)**执行编译:**
|
||
|
||
|
||
**在 `/2_compile`目录下执行编译:**
|
||
|
||
```shell
|
||
icraft compile config/Transformer_8.toml
|
||
```
|
||
|
||
如果过程顺利,将得到 icraft model(以 `.json` (graph)`.raw`(param)的格式保存)
|
||
|
||
其中包括编译各阶段产生的中间结果模型和最终用于片上部署的BY模型,直接被保存到: 3_deploy/modelzoo/Transformer/imodel
|
||
|
||
|
||
|
||
|
||
# 3. 部署模型
|
||
|
||
## 部署环境检查
|
||
|
||
* 以root账户登录片上系统terminal(ssh或串口皆可),模型库默认的模型存放路径为以下目录,如果没有请预先创建:
|
||
|
||
```
|
||
/home/fmsh/ModelZoo/
|
||
```
|
||
|
||
* 检查板上环境是否正确:
|
||
1. 查看环境变量,指令:
|
||
`icraft --version`
|
||
|
||
看打印信息是否如下:
|
||
|
||
```shell
|
||
Icraft 版本:
|
||
* v3.7.1
|
||
|
||
CLI 版本:
|
||
3.7.0.0-a90988f(2412231401)
|
||
```
|
||
|
||
2. 若是,在任意目录下输入`icraft-serve`即可打开server
|
||
|
||
3. 检查icraft和customop安装包版本是否为`arm64`
|
||
|
||
```shell
|
||
# 检查icraft安装包版本
|
||
dpkg -l | grep icraft
|
||
# 检查customop安装包版本
|
||
dpkg -l | grep customop
|
||
```
|
||
|
||
如果依次显示如下信息,则安装版本正确:
|
||
|
||
```shell
|
||
ii icraft 3.7.1 arm64 This is Icraft for arm64
|
||
ii customop 3.7.1 arm64 This is Icraft CustomOp for arm64
|
||
```
|
||
|
||
4. 如果环境配置有误,请参考[Part 1_1 2.3.1 片上系统环境 编译环境准备](https://gitee.com/mxh-spiger/tutorial-runtime/blob/tt3.7.1/docs/Part%201_1%20quick-start.md#1%E7%BC%96%E8%AF%91%E7%8E%AF%E5%A2%83%E5%87%86%E5%A4%87-2)进行部署环境配置。
|
||
|
||
5. 根据此模型使用的硬算子,选择合适的位流,并在板上安装,所用硬算子及可选位流版本可参见本说明文档起始处的状态徽章,位流下载及安装说明请参考[1/4) 其他下载资源](https://gitee.com/mxh-spiger/icraft-introduction/tree/icraft_v3.7.1/#4%E5%85%B6%E4%BB%96%E4%B8%8B%E8%BD%BD%E8%B5%84%E6%BA%90)。
|
||
|
||
## python runtime:
|
||
|
||
目的:在AI硬件上执行模型前向推理
|
||
|
||
1. **python运行环境要求与准备**
|
||
|
||
- python版本:3.8(否则无法使用icraft的python API)
|
||
|
||
- 确保已安装icraft的python安装包
|
||
|
||
- socket模式使用:`pip install icraft-3.x.x-cp38-none-win_amd64.whl`
|
||
- axi模式使用:`pip install icraft-3.x.x-cp38-none-manylinux2014_aarch64.whl`
|
||
|
||
- 安装python运行时所需要的依赖包
|
||
|
||
```
|
||
cd 3_deploy/modelzoo/Transformer
|
||
pip install -r requirements.txt
|
||
```
|
||
|
||
2. **执行程序**
|
||
|
||
将0_Chinese-Text-Classification-Pytorch\THUCNews下文件移到3_deploy\modelzoo\Transformer\vocab文件夹下,再将weights下权重移到saved_dict下,然后执行
|
||
|
||
```
|
||
|
||
python ./infer_Transformer.py
|
||
```
|
||
|
||
在终端查看结果
|
||
|
||
# 4. 精度测试
|
||
|
||
将0_Chinese-Text-Classification-Pytorch\THUCNews下文件移到3_deploy\modelzoo\Transformer\vocab文件夹下,再将weights下权重移到saved_dict下,然后执行
|
||
|
||
```
|
||
python ./Transformer.py
|
||
```
|
||
|
||
|
||
|
||
# 5. 模型性能记录
|
||
|
||
| Transformer | input shape | hard time | qt_strategy | 精度 |
|
||
| ----- | ------------ | ---------- | ----------- | ----------- |
|
||
| float | [1, 32, 300] | - | - | Acc: 90.51% |
|
||
| int8 | [1, 32, 300] | 0.9705 ms | null-pt | Acc: 89.24% |
|
||
| int16 | [1, 32, 300] | 1.1952 ms | null-pt | Acc: 87.40% |
|
||
|