diff --git a/MindIE/MindIE-Torch/built-in/nlp/BERT/ReadMe.md b/MindIE/MindIE-Torch/built-in/nlp/BERT/ReadMe.md new file mode 100644 index 0000000000000000000000000000000000000000..64a74ebf59162b9df8172ef899f6fa532f609848 --- /dev/null +++ b/MindIE/MindIE-Torch/built-in/nlp/BERT/ReadMe.md @@ -0,0 +1,175 @@ +# Bert_Base_Uncased模型-推理指导 + +[TOC] + +****** + + + +# 概述 + +BERT,即Bidirectional Encoder Representations from Transformers,是一种基于Transformer的自然语言处理预训练模型,由Google于2018年发布。当时它在许多自然语言任务中表现出了卓越的性能,之后也成为了几乎所有NLP研究中的性能基线。本文使用的是BERT_base模型。 + + +- 参考实现: + + ``` + url = https://github.com/NVIDIA/DeepLearningExamples.git + commit_id = dd6b8ca2bb80e17b015c0f61e71c2a84733a5b32 + code_path = DeepLearningExamples/PyTorch/LanguageModeling/BERT/ + model_name = "BERTBASE" + ``` + + + +## 输入与输出数据 + +- 输入数据 + + | 输入数据 | 数据类型 | 大小 | 数据排布格式 | + | :-------: | :----: | :-------------: | :-------: | + |input_ids | INT64 | batchsize × 512 | ND | + |segment_ids| INT64 | batchsize × 512 | ND | + |input_mask | INT64 | batchsize × 512 | ND | + + +- 输出数据 + + | 输出数据 | 数据类型 | 大小 | 数据排布格式 | + | :-------: | :----: | :-------------: | :-------: | + | output | INT64 | batchsize × 512 | ND | + + +# 配置推理环境 + +- 该模型需要以下插件与驱动 + + | 配套 | 版本 | 环境准备指导 | + | ------------------------------------------------------------ | ------- | ------------------------------------------------------------ | + | 固件与驱动 | 24.1.RC3 | [Pytorch框架推理环境准备](https://www.hiascend.com/document/detail/zh/ModelZoo/pytorchframework/pies) | + | CANN | 8.0.0 | - | + | Python | 3.10.0 | - | + | PyTorch | 2.1.0 | - | + | 说明:Atlas 300I Duo 推理卡请以CANN版本选择实际固件与驱动版本。 | | | + + +# 新手指南 + +## 获取源代码 + + +1. 获取本仓代码 + + ```bash + git clone https://gitee.com/ascend/ModelZoo-PyTorch.git + cd ./ModelZoo-PyTorch/AscendIE/TorchAIE/built-in/nlp/Bert_Base_Uncased_for_Pytorch/ + ``` + + 文件说明 + ``` + Bert_Base_Uncased_for_Pytorch + ├── bert_config.json + //bert_base模型网络配置参数 + ├── aie_compile.py + //trace并编译模型 + ├── run_aie_eval.py + //在squadv1.1上验证模型的精度和性能 + ├── evaluate_data.py + //计算模型输出的f1得分 + ├── ReadMe.md + //此文档 + ``` + +2. 安装依赖 + + ```bash + pip3 install -r requirements.txt + ``` + + +3. 获取BERT源码 + + ```bash + git clone https://github.com/NVIDIA/DeepLearningExamples.git + cd ./DeepLearningExamples + git reset --hard dd6b8ca2bb80e17b015c0f61e71c2a84733a5b32 + cd .. + ``` + + +## 数据集准备 + +1. 获取原始数据集(请遵循数据集提供方要求使用)。 + + 本模型支持使用squad QA的验证集。 + + 以squad v1.1为例,执行以下指令获取[squad v1.1](https://rajpurkar.github.io/SQuAD-explorer/dataset/dev-v1.1.json)数据集。 + + ```bash + mkdir squadv1.1 && cd squadv1.1 + wget https://rajpurkar.github.io/SQuAD-explorer/dataset/dev-v1.1.json -O ./dev-v1.1.json --no-check-certificate + cd .. + ``` + +## 模型推理 + +1. 编译模型。 + + 将PyTorch的.pth通过mindietorch编译,使得模型可以在昇腾NPU上进行推理。 + + 1. 获取权重文件。 + + 在PyTorch开源框架中获取[bert_base_qa.pt](https://catalog.ngc.nvidia.com/orgs/nvidia/models/bert_pyt_ckpt_base_qa_squad11_amp/files)文件。 + + ```bash + wget 'https://api.ngc.nvidia.com/v2/models/nvidia/bert_pyt_ckpt_base_qa_squad11_amp/versions/19.09.0/files/bert_base_qa.pt' -O ./bert_base_qa.pt --no-check-certificate + ``` + + 2. 执行编译脚本(以batch_size=8为例)。 + + ```bash + #设置gelu算子为高性能模式编译 + export ASCENDIE_FASTER_MODE=1 + #执行编译 + python3.10 aie_compile.py --batch_size=8 --compare_cpu + ``` + + 参数说明: + - --batch_size:批次大小。 + - --compare_cpu:将编译好的模型与原本的模型比对输出,确保精度。 + + + 运行成功后,在当前目录下会生成```bert_base_batch_8.pt```模型文件(```_8```表示batch_size为8) + +2. 开始推理验证。 + + 1. 执行推理脚本(以batch 8为例)。 + + ```bash + #在squadv1.1上推理 + python3.10 run_aie_eval.py --aie_model="./bert_base_batch_8.pt" --predict_file="./squadv1.1/dev-v1.1.json" --vocab_file="./DeepLearningExamples/PyTorch/LanguageModeling/BERT/vocab/vocab" --predict_batch_size=8 --do_lower_case + ``` + + - 参数说明: + + - --aie_model:编译后的模型。 + - --predict_file:推理预测用的数据集。 + - --vocab_file:数据字典映射表文件。 + - --predict_batch_size:推理时的批大小。 + - --do_lower_case:是否进行大小写转化。 + + 执行推理脚本时,模型的预测结果会输出到./output_predictions/下生成.json文件,并且在推理脚本中会拉起子进程运行evaluate_data.py计算f1得分并打屏显示。模型执行推理时的吞吐量也会一并显示。 + + +# 模型推理性能与精度 + +通过mindietorch编译推理,性能参考下列数据。 + +| 推理产品型号 | Batch Size | 数据集 | 精度 | 性能 | +| :-------: | :--------------: | :--------: | :--------: | :-------------: | +| Atlas 300I DUO | 1 | SQuAD v1.1 | 88.715% | 108 fps | +| Atlas 300I DUO | 4 | SQuAD v1.1 | 88.705% | 146 fps | +| Atlas 300I DUO | 8 | SQuAD v1.1 | 88.705% | 138 fps | +| Atlas 300I DUO | 16 | SQuAD v1.1 | 88.662% | 141 fps | +| Atlas 300I DUO | 32 | SQuAD v1.1 | 88.523% | 144 fps | +| Atlas 300I DUO | 64 | SQuAD v1.1 | 88.523% | 145 fps |