diff --git a/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased/LICENSE b/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/LICENSE similarity index 98% rename from ACL_PyTorch/built-in/nlp/Bert_Base_Uncased/LICENSE rename to ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/LICENSE index 4ba4fdcab3dbdb4d64ce4cccdfd990698b4d596a..a0e03103591c1158a839681f3c404ee9118b182e 100644 --- a/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased/LICENSE +++ b/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/LICENSE @@ -1,29 +1,29 @@ -BSD 3-Clause License - -Copyright (c) 2017, -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - -* Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - -* Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - -* Neither the name of the copyright holder nor the names of its - contributors may be used to endorse or promote products derived from - this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +BSD 3-Clause License + +Copyright (c) 2017, +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +* Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased/ReadMe.md b/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/ReadMe.md similarity index 88% rename from ACL_PyTorch/built-in/nlp/Bert_Base_Uncased/ReadMe.md rename to ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/ReadMe.md index 51951756557283070b9e7a0daed48d14d05c24c3..ca4abd33e9ad741e8968f59984cff338e14b6f25 100644 --- a/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased/ReadMe.md +++ b/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/ReadMe.md @@ -1,267 +1,269 @@ -# 参考库文 - -- [BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding](https://arxiv.org/abs/1810.04805) - -# 参考实现 - -- ``` - url=https://github.com/NVIDIA/DeepLearningExamples/tree/master/PyTorch/LanguageModeling/BERT - branch=master - commit_id=dd6b8ca2bb80e17b015c0f61e71c2a84733a5b32 - ``` - -# 依赖 - -| 依赖名称 | 版本 | -| -------- | :----- | -| ONNX | 1.7.0 | -| Pytorch | 1.6.0 | -| onnxsim | 0.3.3 | -| boto3 | 1.21.1 | -| numpy | 1.19.5 | - -# 快速上手 - -#### 获取源码: - -1. 获取github BERT源码 - - - - 下载代码仓。后续基本操作都是在DeepLearningExamples/PyTorch/LanguageModeling/BERT/目录下进行。 - - ``` - git clone https://github.com/NVIDIA/DeepLearningExamples.git - cd DeepLearningExamples - git reset --hard dd6b8ca2bb80e17b015c0f61e71c2a84733a5b32 - ``` - - - -2. 下载modelzoo上源码包。 - -3. 上传源码包到服务器任意目录并解压(如:/home/HwHiAiUser)。 - - - - ``` - ├── bert_config.json //bert_base模型网络配置参数 - ├── bert_base_get_info.py //生成推理输入的数据集二进制info文件 - ├── bert_preprocess_data.py //数据集预处理脚本,生成二进制文件 - ├── ReadMe.md - ├── bert_base_uncased_atc.sh //onnx模型转换om模型脚本 - ├── bert_base_pth2onnx.py //用于转换pth模型文件到onnx模型文件 - ├── bert_postprocess_data.py //bert_base数据后处理脚本,用于将推理结果处理映射成文本 - └── evaluate-v1.1.py //验证推理结果脚本,比对benchmark输出的分类结果,给出accuracy - ``` - - - - ![img](https://obs-9be7.obs.cn-east-2.myhuaweicloud.com/turing/resourcecenter/img/public_sys-resources/note_3.0-zh-cn.png) - - benchmark离线推理工具使用请参见《[CANN V100R020C20 推理benchmark工具用户指南](https://support.huawei.com/enterprise/zh/doc/EDOC1100180792)》 - - - -4. 将ModelZoo源码包中的文件移动并替换到DeepLearningExamples/PyTorch/LanguageModeling/BERT目录中。 - -#### 准备数据集 - -1. 获取原始数据集。 - - - - 本模型支持使用squad QA的验证集。以squad v1.1为例,请用户自行获取squad v1.1数据集,上传数据集到服务器目录DeepLearningExamples/PyTorch/LanguageModeling/BERT/data/squad/v1.1/。 - - - - - -2. 数据预处理。 - - - - - - 数据预处理将原始数据集转换为模型输入的数据。模型输入数据为二进制输入。 - - 将原始数据(dev-v1.1.json文本)转化为二进制文件(.bin)。转化方法参考bert_preprocess_data.py训练预处理方法处理数据,以获得最佳精度,输出为二进制文件。 - - 执行bert_preprocess_data.py脚本。 - - ``` - python3 bert_preprocess_data.py --max_seq_length=512 --do_lower_case --vocab_file=./vocab/vocab --predict_file=./data/squad/v1.1/dev-v1.1.json - ``` - - 参数说明: - - - --max_seq_length:句子最大长度。 - - --vocab_file:数据字典映射表文件。 - - --do_lower_case:是否进行大小写转化。 - - --predict_file:原始验证数据文本,将后处理数据位置映射到原始文件。 - - - -3. 生成数据集info文件 - - - - 使用benchmark推理需要输入二进制数据集的info文件,用于获取数据集。使用bert_base_get_info.py脚本,输入已经得到的二进制文件,输出生成二进制数据集的info文件。运行bert_base_get_info.py脚本。 - - ``` - python3 bert_base_get_info.py --batchsize=8 - ``` - - 参数为batchsize的大小,默认为8,运行成功后,在当前目录中生成bert_base_uncased.info。 - - - -#### 模型推理 - -1. ##### 模型转换。 - - - - 本模型基于开源框架PyTorch训练的bert_base_uncased进行模型转换。 - - 使用PyTorch将模型权重文件.pt转换为.onnx文件,再使用ATC工具将.onnx文件转为离线推理模型文件.om文件。 - - 1. ###### 获取权重文件。 - - - 在PyTorch开源框架中获取bert_base_qa.pt文件。 - - 2. ###### 导出onnx文件。 - - 将模型权重文件.pt转换为.onnx文件。 - - 1. 修改bert_base_qa.pt名称为pytorch_model.bin。 - - ``` - mv bert_base_qa.pt pytorch_model.bin - ``` - - 因为脚本读取已训练好的权重文件名为pytorch_model.bin。 - - 2. 进入BERT目录下,执行bert_base_pth2onnx.py脚本将.pt文件转换为.onnx文件,执行如下命令。 - - ``` - python3.7 bert_base_pth2onnx.py --init_checkpoint=pytorch_model.bin --config_file=bert_config.json - ``` - - 参数说明: - - - --init_checkpoint:输入权重文件。 - - --config_file:网络参数配置文件。 - - 运行成功后,在当前目录生成bert_base_batch_1.onnx模型文件。 - - 3. 将生成的onnx模型简化,执行如下命令。 - - ``` - python3.7 -m onnxsim bert_base_batch_8.onnx bert_base_batch_8_sim.onnx - ``` - - ![img](https://obs-9be7.obs.cn-east-2.myhuaweicloud.com/turing/resourcecenter/img/public_sys-resources/notice_3.0-zh-cn.png) - - 使用ATC工具将.onnx文件转换为.om文件,需要.onnx算子版本需为11。在bert_base_pth2onnx.py脚本中torch.onnx.export方法中的输入参数opset_version的值需为11,请勿修改。 - - 3. ###### 使用ATC工具将ONNX模型转OM模型。 - - 1. 修改bert_base_uncased_atc.sh脚本,通过ATC工具使用脚本完成转换,具体的脚本示例如下: - - ``` - # 配置环境变量 - export install_path=/usr/local/Ascend/ascend-toolkit/latest - export PATH=/usr/local/python3.7.5/bin:${install_path}/atc/ccec_compiler/bin:${install_path}/atc/bin:$PATH - export PYTHONPATH=${install_path}/atc/python/site-packages:$PYTHONPATH - export LD_LIBRARY_PATH=${install_path}/atc/lib64:${install_path}/acllib/lib64:$LD_LIBRARY_PATH - export ASCEND_OPP_PATH=${install_path}/opp - - # 使用二进制输入时,执行如下命令 - atc --input_format=ND --framework=5 --model=bert_base_batch_8_sim.onnx --input_shape="input_ids:8,512;token_type_ids:8,512;attention_mask:8,512" --output=bert_base_batch_8_sim_auto --auto_tune_mode="RL,GA" --log=info --soc_version=Ascend710 - ``` - - ![img](https://obs-9be7.obs.cn-east-2.myhuaweicloud.com/turing/resourcecenter/img/public_sys-resources/note_3.0-zh-cn.png) - - 该脚本中环境变量仅供参考,请以实际安装环境配置环境变量。 - - - - **Auto Tune**工具在“RL”模式下需要安装TensorFlow框架。 - - - 参数说明: - - --model:为ONNX模型文件。 - - --framework:5代表ONNX模型。 - - --output:输出的OM模型。 - - --input_format:输入数据的格式。 - - --input_shape:输入数据的shape。 - - --log:日志等级。 - - --soc_version:部署芯片类型。 - - 2. 执行atc转换脚本,将.onnx文件转为离线推理模型文件.om文件。 - - ``` - bash atc_bert_base_uncased.sh - ``` - - 运行成功后生成bert_base_batch_8_sim_auto.om用于二进制输入推理的模型文件。 - - - -2. ##### 开始推理验证。 - - - - 1. 使用Benchmark工具进行推理。 - - 增加benchmark.*{arch}可执行权限*。 - - ``` - chmod u+x benchmark.x86_64 - ``` - - 验证模型性能: - - ``` - ./benchmark.x86_64 -batch_size=8 -device_id=0 -om_path=bert_base_batch_8_sim_auto.om -round=10 - ``` - - 获得**模型性能ave_throughputRate: 198.61samples/s**, 略微有点波动。 - - 执行命令,进行完整推理。 - - ``` - ./benchmark.x86_64 -model_type=bert -batch_size=8 -device_id=0 -om_path=bert_base_batch_8_sim_auto.om -input_text_path=./bert_base_uncased.info -output_binary=true - ``` - - bert_base_uncased.info为处理后的数据集信息。 - - benchmark.*{arch}*请根据运行环境架构选择,如运行环境为x86_64,需执行./benchmark.x86_64。推理后的输出默认在当前目录result下。 - - 2. 推理结果后处理 - - 将结果转化为json文本数据,执行命令如下。 - - ``` - python3.7 bert_postprocess_data.py --max_seq_length=512 --vocab_file=./vocab/vocab --do_lower_case --predict_file=./data/squad/v1.1/dev-v1.1.json --npu_result=./result/dumpOutput/ - ``` - - 参数说明: - - - --max_seq_length:句子最大长度。 - - --vocab_file:数据字典映射表文件。 - - --do_lower_case:是否进行大小写转化。 - - --predict_file:原始验证数据文本,将后处理数据位置映射到原始文件。 - - --npu_result:benchmark推理结果目录。 - - 3. 精度验证 - - 调用evaluate-v1.1.py脚本将原始数据dev-v1.1.json与推理结果数据文本predictions.json比对,可以获得Accuracy数据,结果保存在中,执行命令如下。 - - ``` - python3.7 evaluate-v1.1.py ./data/squad/v1.1/dev-v1.1.json predictions.json - ``` - - 第一个参数为原始推理数据文本,第二个参数为生成推理结果文本。 - +# 参考库文 + +- [BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding](https://arxiv.org/abs/1810.04805) + +# 参考实现 + +- ``` + url=https://github.com/NVIDIA/DeepLearningExamples/tree/master/PyTorch/LanguageModeling/BERT + branch=master + commit_id=dd6b8ca2bb80e17b015c0f61e71c2a84733a5b32 + ``` + +# 依赖 + +| 依赖名称 | 版本 | +| -------- | :----- | +| ONNX | 1.7.0 | +| Pytorch | 1.6.0 | +| onnxsim | 0.3.3 | +| boto3 | 1.21.1 | +| numpy | 1.19.5 | + +# 快速上手 + +#### 获取源码: + +1. 获取github BERT源码 + + + + 下载代码仓。后续基本操作都是在DeepLearningExamples/PyTorch/LanguageModeling/BERT/目录下进行。 + + ``` + git clone https://github.com/NVIDIA/DeepLearningExamples.git + cd DeepLearningExamples + git reset --hard dd6b8ca2bb80e17b015c0f61e71c2a84733a5b32 + ``` + + + +2. 下载modelzoo上源码包。 + +3. 上传源码包到服务器任意目录并解压(如:/home/HwHiAiUser)。 + + + + ``` + ├── bert_config.json //bert_base模型网络配置参数 + ├── bert_base_get_info.py //生成推理输入的数据集二进制info文件 + ├── bert_preprocess_data.py //数据集预处理脚本,生成二进制文件 + ├── ReadMe.md + ├── bert_base_uncased_atc.sh //onnx模型转换om模型脚本 + ├── bert_base_pth2onnx.py //用于转换pth模型文件到onnx模型文件 + ├── bert_postprocess_data.py //bert_base数据后处理脚本,用于将推理结果处理映射成文本 + ├── add_attr_transB.py //对可能存在的transpose进行优化 + └── evaluate-v1.1.py //验证推理结果脚本,比对benchmark输出的分类结果,给出accuracy + ``` + + + + ![img](https://obs-9be7.obs.cn-east-2.myhuaweicloud.com/turing/resourcecenter/img/public_sys-resources/note_3.0-zh-cn.png) + + benchmark离线推理工具使用请参见《[CANN V100R020C20 推理benchmark工具用户指南](https://support.huawei.com/enterprise/zh/doc/EDOC1100180792)》 + + + +4. 将ModelZoo源码包中的文件移动并替换到DeepLearningExamples/PyTorch/LanguageModeling/BERT目录中。 + +#### 准备数据集 + +1. 获取原始数据集。 + + + + 本模型支持使用squad QA的验证集。以squad v1.1为例,请用户自行获取squad v1.1数据集,上传数据集到服务器目录DeepLearningExamples/PyTorch/LanguageModeling/BERT/data/squad/v1.1/。 + + + + + +2. 数据预处理。 + + + + + + 数据预处理将原始数据集转换为模型输入的数据。模型输入数据为二进制输入。 + + 将原始数据(dev-v1.1.json文本)转化为二进制文件(.bin)。转化方法参考bert_preprocess_data.py训练预处理方法处理数据,以获得最佳精度,输出为二进制文件。 + + 执行bert_preprocess_data.py脚本。 + + ``` + python3 bert_preprocess_data.py --max_seq_length=512 --do_lower_case --vocab_file=./vocab/vocab --predict_file=./data/squad/v1.1/dev-v1.1.json + ``` + + 参数说明: + + - --max_seq_length:句子最大长度。 + - --vocab_file:数据字典映射表文件。 + - --do_lower_case:是否进行大小写转化。 + - --predict_file:原始验证数据文本,将后处理数据位置映射到原始文件。 + + + +3. 生成数据集info文件 + + + + 使用benchmark推理需要输入二进制数据集的info文件,用于获取数据集。使用bert_base_get_info.py脚本,输入已经得到的二进制文件,输出生成二进制数据集的info文件。运行bert_base_get_info.py脚本。 + + ``` + python3 bert_base_get_info.py --batchsize=8 + ``` + + 参数为batchsize的大小,默认为8,运行成功后,在当前目录中生成bert_base_uncased.info。 + + + +#### 模型推理 + +1. ##### 模型转换。 + + + + 本模型基于开源框架PyTorch训练的bert_base_uncased进行模型转换。 + + 使用PyTorch将模型权重文件.pt转换为.onnx文件,再使用ATC工具将.onnx文件转为离线推理模型文件.om文件。 + + 1. ###### 获取权重文件。 + + - 在PyTorch开源框架中获取bert_base_qa.pt文件。 + + 2. ###### 导出onnx文件。 + + 将模型权重文件.pt转换为.onnx文件。 + + 1. 修改bert_base_qa.pt名称为pytorch_model.bin。 + + ``` + mv bert_base_qa.pt pytorch_model.bin + ``` + + 因为脚本读取已训练好的权重文件名为pytorch_model.bin。 + + 2. 进入BERT目录下,执行bert_base_pth2onnx.py脚本将.pt文件转换为.onnx文件,执行如下命令。 + + ``` + python3.7 bert_base_pth2onnx.py --init_checkpoint=pytorch_model.bin --config_file=bert_config.json + ``` + + 参数说明: + + - --init_checkpoint:输入权重文件。 + - --config_file:网络参数配置文件。 + + 运行成功后,在当前目录生成bert_base_batch_8.onnx模型文件。 + + + ![img](https://obs-9be7.obs.cn-east-2.myhuaweicloud.com/turing/resourcecenter/img/public_sys-resources/notice_3.0-zh-cn.png) + + 使用ATC工具将.onnx文件转换为.om文件,需要.onnx算子版本需为11。在bert_base_pth2onnx.py脚本中torch.onnx.export方法中的输入参数opset_version的值需为11,请勿修改。 + + 3. 此步可选,根据onnx图里是否存在(0,2,3,1)的transpose进行优化,若存在,根据这类Transpose和紧跟它的MatMul的name更新add_attr_transB.py,也就是更新里面的transpose_nodes和bmm_nodes两个list,然后运行下面命令。 + + ``` + python3 add_attr_transB.py bert_base_batch_8.onnx bert_base_batch_8.onnx + ``` + + 因为脚本读取已训练好的权重文件名为pytorch_model.bin。 + + 3. ###### 使用ATC工具将ONNX模型转OM模型。 + + 1. 修改bert_base_uncased_atc.sh脚本,通过ATC工具使用脚本完成转换,具体的脚本示例如下: + + ``` + # 配置环境变量 + export install_path=/usr/local/Ascend/ascend-toolkit/latest + export PATH=/usr/local/python3.7.5/bin:${install_path}/atc/ccec_compiler/bin:${install_path}/atc/bin:$PATH + export PYTHONPATH=${install_path}/atc/python/site-packages:$PYTHONPATH + export LD_LIBRARY_PATH=${install_path}/atc/lib64:${install_path}/acllib/lib64:$LD_LIBRARY_PATH + export ASCEND_OPP_PATH=${install_path}/opp + + # 使用二进制输入时,执行如下命令 + atc --input_format=ND --framework=5 --model=bert_base_batch_8.onnx --input_shape="input_ids:8,512;token_type_ids:8,512;attention_mask:8,512" --output=bert_base_batch_8_auto --log=info --soc_version=Ascend710 + ``` + + ![img](https://obs-9be7.obs.cn-east-2.myhuaweicloud.com/turing/resourcecenter/img/public_sys-resources/note_3.0-zh-cn.png) + + 该脚本中环境变量仅供参考,请以实际安装环境配置环境变量。 + + + + - 参数说明: + - --model:为ONNX模型文件。 + - --framework:5代表ONNX模型。 + - --output:输出的OM模型。 + - --input_format:输入数据的格式。 + - --input_shape:输入数据的shape。 + - --log:日志等级。 + - --soc_version:部署芯片类型。 + + 2. 执行atc转换脚本,将.onnx文件转为离线推理模型文件.om文件。 + + ``` + bash atc_bert_base_uncased.sh + ``` + + 运行成功后生成bert_base_batch_8_auto.om用于二进制输入推理的模型文件。 + + + +2. ##### 开始推理验证。 + + + + 1. 使用Benchmark工具进行推理。 + + 增加benchmark.*{arch}可执行权限*。 + + ``` + chmod u+x benchmark.x86_64 + ``` + + 验证模型性能: + + ``` + bash test_perf.sh + ``` + + 获得**模型性能ave_throughputRate: 198.61samples/s**, 略微有点波动。 + + 执行命令,进行完整推理。 + + ``` + bash infer_all.sh + ``` + + bert_base_uncased.info为处理后的数据集信息。 + + benchmark.*{arch}*请根据运行环境架构选择,如运行环境为x86_64,需执行./benchmark.x86_64。推理后的输出默认在当前目录result下。 + + 2. 推理结果后处理 + + 将结果转化为json文本数据,执行命令如下。 + + ``` + python3.7 bert_postprocess_data.py --max_seq_length=512 --vocab_file=./vocab/vocab --do_lower_case --predict_file=./data/squad/v1.1/dev-v1.1.json --npu_result=./result/dumpOutput/ + ``` + + 参数说明: + + - --max_seq_length:句子最大长度。 + - --vocab_file:数据字典映射表文件。 + - --do_lower_case:是否进行大小写转化。 + - --predict_file:原始验证数据文本,将后处理数据位置映射到原始文件。 + - --npu_result:benchmark推理结果目录。 + + 3. 精度验证 + + 调用evaluate-v1.1.py脚本将原始数据dev-v1.1.json与推理结果数据文本predictions.json比对,可以获得Accuracy数据,结果保存在中,执行命令如下。 + + ``` + python3.7 evaluate-v1.1.py ./data/squad/v1.1/dev-v1.1.json predictions.json + ``` + + 第一个参数为原始推理数据文本,第二个参数为生成推理结果文本。 + 获得**模型精度:“f1”: 47.3825** \ No newline at end of file diff --git a/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/add_attr_transB.py b/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/add_attr_transB.py new file mode 100644 index 0000000000000000000000000000000000000000..45b2063c18bf1bd62bb74e81cd27d73efbe21fbb --- /dev/null +++ b/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/add_attr_transB.py @@ -0,0 +1,58 @@ +# Copyright 2022 Huawei Technologies Co., Ltd +# +# Licensed under the BSD 3-Clause License (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://opensource.org/licenses/BSD-3-Clause +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import sys +from gener_core.mod_modify.onnx_graph import OXGraph +from gener_core.mod_modify.interface import AttrType as AT + + +input_model = sys.argv[1] +output_model = sys.argv[2] + +mod = OXGraph(input_model) + +transpose_nodes = ['Transpose_60', + 'Transpose_154', + 'Transpose_248', + 'Transpose_342', + 'Transpose_436', + 'Transpose_530', + 'Transpose_624', + 'Transpose_718', + 'Transpose_812', + 'Transpose_906', + 'Transpose_1000', + 'Transpose_1094'] +bmm_nodes = ['MatMul_72', + 'MatMul_166', + 'MatMul_260', + 'MatMul_354', + 'MatMul_448', + 'MatMul_542', + 'MatMul_636', + 'MatMul_730', + 'MatMul_824', + 'MatMul_918', + 'MatMul_1012', + 'MatMul_1106'] +io_map = mod.get_net_in_out_map() + +for transpose_node in transpose_nodes: + now_trans = mod.get_node(transpose_node) + now_trans.set_attr({"perm": (AT.LIST_INT, [0, 2, 1, 3])}) +for bmm in bmm_nodes: + now_bmm = mod.get_node(bmm) + now_bmm.set_attr({"transB": (AT.INT, 1)}) + +mod.save_new_model(output_model) +print("OK") \ No newline at end of file diff --git a/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased/atc_bert_base_uncased.sh b/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/atc_bert_base_uncased.sh similarity index 72% rename from ACL_PyTorch/built-in/nlp/Bert_Base_Uncased/atc_bert_base_uncased.sh rename to ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/atc_bert_base_uncased.sh index c48758ab0668a3b3d625c85454bcd9b421c224e9..3b208c71dac34fa639c7f5bee5f4d39cabf24c5f 100644 --- a/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased/atc_bert_base_uncased.sh +++ b/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/atc_bert_base_uncased.sh @@ -3,8 +3,11 @@ export PATH=/usr/local/python3.7.5/bin:${install_path}/atc/ccec_compiler/bin:${i export PYTHONPATH=${install_path}/atc/python/site-packages:$PYTHONPATH export LD_LIBRARY_PATH=${install_path}/atc/lib64:${install_path}/acllib/lib64:$LD_LIBRARY_PATHexport ASCEND_OPP_PATH=${install_path}/opp -atc --input_format=ND --framework=5 --model=bert_base_batch_8_sim.onnx\ +export ASCEND_GLOBAL_LOG_LEVEL=3 +for i in $(seq 0 7); do /usr/local/Ascend/driver/tools/msnpureport -g error -d $i; done + +atc --input_format=ND --framework=5 --model=bert_base_batch_8.onnx\ --input_shape="input_ids:8,512;token_type_ids:8,512;attention_mask:8,512"\ - --output=bert_base_batch_8_sim_auto --auto_tune_mode="RL,GA"\ + --output=bert_base_batch_8_auto\ --log=error --soc_version=Ascend710 --optypelist_for_implmode="Gelu"\ --op_select_implmode=high_performance --input_fp16_nodes="attention_mask" \ No newline at end of file diff --git a/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased/bert_base_get_info.py b/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/bert_base_get_info.py similarity index 100% rename from ACL_PyTorch/built-in/nlp/Bert_Base_Uncased/bert_base_get_info.py rename to ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/bert_base_get_info.py diff --git a/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased/bert_base_pth2onnx.py b/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/bert_base_pth2onnx.py similarity index 96% rename from ACL_PyTorch/built-in/nlp/Bert_Base_Uncased/bert_base_pth2onnx.py rename to ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/bert_base_pth2onnx.py index e5f503a6e401dec7268a188589fae309c096b477..7cbeb1d0d21cf36fe695100456b2d0ed18bff58a 100644 --- a/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased/bert_base_pth2onnx.py +++ b/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/bert_base_pth2onnx.py @@ -64,7 +64,6 @@ if __name__ == '__main__': operator_export_type=OPERATOR_EXPORT_TYPE, input_names=['input_ids', 'token_type_ids', 'attention_mask'], output_names=['output'], - dynamic_axes={'input_ids': [0], 'token_type_ids': [0], 'attention_mask': [0]}, opset_version=11 ) print("Export of torch_model.onnx complete!") \ No newline at end of file diff --git a/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased/bert_config.json b/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/bert_config.json similarity index 100% rename from ACL_PyTorch/built-in/nlp/Bert_Base_Uncased/bert_config.json rename to ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/bert_config.json diff --git a/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased/bert_postprocess_data.py b/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/bert_postprocess_data.py similarity index 100% rename from ACL_PyTorch/built-in/nlp/Bert_Base_Uncased/bert_postprocess_data.py rename to ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/bert_postprocess_data.py diff --git a/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased/bert_preprocess_data.py b/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/bert_preprocess_data.py similarity index 100% rename from ACL_PyTorch/built-in/nlp/Bert_Base_Uncased/bert_preprocess_data.py rename to ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/bert_preprocess_data.py diff --git a/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased/evaluate-v1.1.py b/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/evaluate-v1.1.py similarity index 100% rename from ACL_PyTorch/built-in/nlp/Bert_Base_Uncased/evaluate-v1.1.py rename to ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/evaluate-v1.1.py diff --git a/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/infer_all.sh.sh b/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/infer_all.sh.sh new file mode 100644 index 0000000000000000000000000000000000000000..0e9028a272f391eac2418d959086f4d79df9646e --- /dev/null +++ b/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/infer_all.sh.sh @@ -0,0 +1 @@ +./benchmark.x86_64 -model_type=bert -batch_size=8 -device_id=0 -om_path=bert_base_batch_8_auto.om -input_text_path=./bert_base_uncased.info -output_binary=true \ No newline at end of file diff --git a/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased/modelzoo_level.txt b/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/modelzoo_level.txt similarity index 95% rename from ACL_PyTorch/built-in/nlp/Bert_Base_Uncased/modelzoo_level.txt rename to ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/modelzoo_level.txt index 0b49b4fb26c2694a86567bea1b462e7dcb03cc31..31529da2e68f25b61e2a3e698a07537281443c03 100644 --- a/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased/modelzoo_level.txt +++ b/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/modelzoo_level.txt @@ -1,3 +1,3 @@ -FuncStatus:OK -PerfStatus:OK +FuncStatus:OK +PerfStatus:OK PrecisionStatus:OK \ No newline at end of file diff --git a/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/requirements.txt.txt b/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/requirements.txt.txt new file mode 100644 index 0000000000000000000000000000000000000000..30b0ef1d21f84b564d16735beaa01f2b872a5fb5 --- /dev/null +++ b/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/requirements.txt.txt @@ -0,0 +1,5 @@ +torch==1.6.0 +onnx==1.7.0 +onnxsim==0.3.3 +numpy==1.19.5 +boto3==1.21.1 \ No newline at end of file diff --git a/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/test_perf.sh.sh b/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/test_perf.sh.sh new file mode 100644 index 0000000000000000000000000000000000000000..1b3aa4b09485b3b3baa3391257137b8135e6584f --- /dev/null +++ b/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/test_perf.sh.sh @@ -0,0 +1 @@ +./benchmark.x86_64 -batch_size=8 -device_id=0 -om_path=bert_base_batch_8_sim_auto.om -round=10 \ No newline at end of file