From b1a36751678890fa2d4820efff461e87ddae7d36 Mon Sep 17 00:00:00 2001 From: r00853487 Date: Mon, 20 Nov 2023 11:14:47 +0800 Subject: [PATCH 1/8] transfer to yellow --- .../aie_compile.py | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/aie_compile.py diff --git a/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/aie_compile.py b/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/aie_compile.py new file mode 100644 index 0000000000..52acff76ae --- /dev/null +++ b/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/aie_compile.py @@ -0,0 +1,105 @@ +import os +import sys +import config +import argparse + +import torch +import torch_aie +from torch_aie import _enums + +import lib.models.crnn as crnn + +COSINE_THRESHOLD = 0.999 + + +def cosine_similarity(gt_tensor, pred_tensor): + gt_tensor = gt_tensor.flatten().to(torch.float32) + pred_tensor = pred_tensor.flatten().to(torch.float32) + if torch.sum(gt_tensor) == 0.0 or torch.sum(pred_tensor) == 0.0: + if torch.allclose(gt_tensor, pred_tensor, atol=1e-4, rtol=1e-4, equal_nan=True): + return 1.0 + res = torch.nn.functional.cosine_similarity(gt_tensor, pred_tensor, dim=0, eps=1e-6) + res = res.cpu().detach().item() + + return res + + +def __load_checkpoint(model, checkpoint_filepath, device): + checkpoint = torch.load(checkpoint_filepath, map_location=device) + if 'state_dict' in checkpoint.keys(): + model.load_state_dict(checkpoint['state_dict']) + else: + model.load_state_dict(checkpoint) + + +def trace_compile(torch_model, args): + input_shape = (args.batch_size, 1, 32, 160) + inputs = torch.randn(size = input_shape, dtype = torch.float32) + input_data = [ inputs ] + + # trace model + print("trace start. ") + traced_model = torch.jit.trace(torch_model, input_data) + print("trace done. ") + # print("traced model is ", traced_model.graph) + + traced_model.eval() + jit_result = traced_model(inputs) + print("torch_aie compile start !") + torch_aie.set_device(0) + compile_inputs = [torch_aie.Input(shape = input_shape, dtype = torch.float32, format = torch_aie.TensorFormat.NCHW)] + compiled_model = torch_aie.compile( + traced_model, + inputs = compile_inputs, + precision_policy = _enums.PrecisionPolicy.FP16, + soc_version = "Ascend310P3", + optimization_level = 0 + ) + print("torch_aie compile done !") + aie_result = compiled_model(inputs) + print("compiled model is ", compiled_model.graph) + compiled_model.save(args.pt_dir) + print("torch aie compiled model saved. ") + + com_res = True + res = cosine_similarity(jit_result, aie_result) + print(res) + if res < COSINE_THRESHOLD: + com_res = False + if com_res: + print("Compare success ! NPU model have the same output with CPU model !") + else: + print("Compare failed ! Outputs of NPU model are not the same with CPU model !") + + + + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + parser.add_argument("--checkpoint_filepath", + default="/home/Code/modeltest/CRNN_Chinese_Characters_Rec/output/checkpoints/mixed_second_finetune_acc_97P7.pth", + type=str, + help="The original torch pt file from pretraining") + parser.add_argument("--save_dir", + default="./", + type=str, + help="The path of the directory that stores the compiled model") + parser.add_argument("--config_file", + default="/home/Code/modeltest/CRNN_Chinese_Characters_Rec/lib/config/360CC_config.yaml", + type=str, + help="The BERT model config") + parser.add_argument('--batch_size', + default=8, + type=int, + help="batch size") + args = parser.parse_args() + + config_filepath = args.config_file + checkpoint_filepath = args.checkpoint_filepath + args.pt_dir = args.save_dir + 'crnn_sierkinhane_bs{}.pt'.format(args.batch_size) + + torch_model = crnn.get_crnn(config.get_config(config_filepath)).to("cpu") + __load_checkpoint(torch_model, checkpoint_filepath, "cpu") + torch_model.eval() + + trace_compile(torch_model, args) \ No newline at end of file -- Gitee From 99547d94bee358869d296d75c81d8ba33adca629 Mon Sep 17 00:00:00 2001 From: r00853487 Date: Mon, 20 Nov 2023 11:37:20 +0800 Subject: [PATCH 2/8] transfer --- .../aie_compile.py | 2 +- .../CRNN_Sierkinhane_for_Pytorch/config.py | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/config.py diff --git a/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/aie_compile.py b/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/aie_compile.py index 52acff76ae..bef18019cb 100644 --- a/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/aie_compile.py +++ b/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/aie_compile.py @@ -87,7 +87,7 @@ if __name__ == '__main__': parser.add_argument("--config_file", default="/home/Code/modeltest/CRNN_Chinese_Characters_Rec/lib/config/360CC_config.yaml", type=str, - help="The BERT model config") + help="The crnn model config") parser.add_argument('--batch_size', default=8, type=int, diff --git a/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/config.py b/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/config.py new file mode 100644 index 0000000000..e2ca215c2a --- /dev/null +++ b/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/config.py @@ -0,0 +1,33 @@ +# Copyright 2021 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. + +# coding=utf-8 + +import os +import sys + +import yaml +from easydict import EasyDict + +sys.path.append(os.path.dirname(os.path.dirname(__file__))) + +import lib.config.alphabets as alphabets + + +def get_config(config_filepath): + with open(config_filepath) as f: + config = EasyDict(yaml.safe_load(f)) + config.DATASET.ALPHABETS = alphabets.alphabet + config.MODEL.NUM_CLASSES = len(config.DATASET.ALPHABETS) + return config -- Gitee From fe892fd2bcc10ece012ae964e47ff86c1896ac69 Mon Sep 17 00:00:00 2001 From: r00853487 Date: Tue, 21 Nov 2023 15:15:25 +0800 Subject: [PATCH 3/8] =?UTF-8?q?[=E8=87=AA=E7=A0=94][=E6=8E=A8=E7=90=86?= =?UTF-8?q?=E5=BC=95=E6=93=8E][ocr]CRNN=5FSierkinhane=E6=8E=A8=E7=90=86?= =?UTF-8?q?=E6=A0=B7=E4=BE=8B=E4=B8=8A=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ocr/CRNN_Sierkinhane_for_Pytorch/LICENSE | 29 +++++ .../CRNN_Sierkinhane_for_Pytorch/README.md | 123 ++++++++++++++++++ .../aie_compile.py | 6 +- .../CRNN_Sierkinhane_for_Pytorch/aie_val.py | 104 +++++++++++++++ .../requirements.txt | 8 ++ 5 files changed, 266 insertions(+), 4 deletions(-) create mode 100644 AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/LICENSE create mode 100644 AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/README.md create mode 100644 AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/aie_val.py create mode 100644 AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/requirements.txt diff --git a/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/LICENSE b/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/LICENSE new file mode 100644 index 0000000000..a0e0310359 --- /dev/null +++ b/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/LICENSE @@ -0,0 +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 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/README.md b/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/README.md new file mode 100644 index 0000000000..6031b157f9 --- /dev/null +++ b/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/README.md @@ -0,0 +1,123 @@ +# CRNN_Sierkinhane 推理指导 + +- [概述](#概述) + - [输入输出数据](#输入输出数据) +- [推理环境准备](#推理环境准备) +- [快速上手](#快速上手) + - [获取源码](#获取源码) + - [准备数据集](#准备数据集) + - [编译模型](#编译模型) + - [执行推理](#执行推理) +- [精度和性能](#精度和性能) + +## 概述 + +CRNN_Sierkinhane 是一个基于卷积循环网络的中文 OCR 模型。 +v +参考实现: + +``` +https://github.com/Sierkinhane/CRNN_Chinese_Characters_Rec +branch=stable +commit_id=a565687c4076b729d4059593b7570dd388055af4 +``` + +## 输入输出数据 + +| 输入数据 | 数据类型 | 大小 | 数据排布格式 | +| ---- | ---- | ---- | ---- | +| input | FLOAT32 | x 1 x 32 x 160 | NCHW | + +| 输出数据 | 数据类型 | 大小 | 数据排布格式 | +| ---- | ---- | ---- | ---- | +| output | FLOAT32 | 41 x x <字符集总数 + 1> | ND | + +数据集:GitHub 仓库提供的 360 万数据集。 + +## 推理环境准备 + +- 该模型需要以下固件与插件 + + **表 1** 版本配套表 + +| 配套 | 版本 | +| ------------------------------------------------------------ | ------- | +| 固件与驱动 | 23.0.RC1 | +| CANN | 7.0.RC1.alpha003 | +| Python | 3.9.11 | +| PyTorch | 2.0.1 | +| Torch_AIE | 6.3.rc2 | +| 芯片类型 | Ascend310P3 | + + +## 快速上手 +### 获取源码 +1. 拉取Gitee仓代码,并安装依赖。 + ```bash + git clone https://gitee.com/ascend/ModelZoo-PyTorch.git + cd ModelZoo-PyTorch/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch + pip install -r requirements.txt + ``` + +2. 克隆 GitHub 仓库,切换到指定分支、指定 commit_id。 + ```bash + git clone https://github.com/Sierkinhane/CRNN_Chinese_Characters_Rec.git + cd CRNN_Chinese_Characters_Rec + git checkout stable + git reset --hard a565687c4076b729d4059593b7570dd388055af4 + cd .. + ``` +3. 将本仓代码拷贝至CRNN_Chinese_Characters_Rec目录下。 + ```bash + cp aie_compile.py ./CRNN_Chinese_Characters_Rec + cp aie_val.py ./CRNN_Chinese_Characters_Rec + cp config.py ./CRNN_Chinese_Characters_Rec + cd ./CRNN_Chinese_Characters_Rec + ``` + +### 准备数据集 + +1. 请按照 GitHub 仓中提供的方式下载原始数据集和label,并遵守数据集提供方要求使用。下载好原始数据集并解压后,更改/lib/config/360CC_config.yaml文件中的对应内容。 + + ```bash + DATASET: + ROOT: 'to/your/images/path' + ``` + +### 编译模型 + +1. 执行模型编译脚本,使用torch_aie编译模型使其可以运行在昇腾npu上。(以bs8为例) + + ```bash + python aie_compile.py --batch_size=8 + ``` + 参数说明: + + - --batch_size:批大小。 + +### 执行推理 + +1. 执行推理脚本,获得在数据集上的模型准确度与吞吐量。(以bs8为例) + + ```bash + python aie_val.py --cfg=./lib/config/360CC_config.yaml --batch_size=8 --model_path=./crnn_sierkinhane_bs8.pt + ``` + + 参数说明: + - --cfg:360CC_config.yaml文件路径。 + - --batch_size:批大小。 + - --model_path:编译后模型的路径。 + + +## 精度和性能 + +1. 纯静态输入 + +| 芯片型号 | Batch Size | 数据集 | ACC-精度 | 性能 | +| ---- | ---- | ---- | ---- | ----| +| 310P3 | 1 | GitHub 仓库提供的 360 万数据集 | 78.37% | 583.00 | +| 310P3 | 4 | GitHub 仓库提供的 360 万数据集 | - | 2172.97 | +| 310P3 | 8 | GitHub 仓库提供的 360 万数据集 | - | 3622.37 | +| 310P3 | 16 | GitHub 仓库提供的 360 万数据集 | - | 5425.79 | +| 310P3 | 32 | GitHub 仓库提供的 360 万数据集 | - | 6864.79 | +| 310P3 | 64 | GitHub 仓库提供的 360 万数据集 | - | 7608.74 | diff --git a/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/aie_compile.py b/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/aie_compile.py index bef18019cb..7c8d1c0575 100644 --- a/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/aie_compile.py +++ b/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/aie_compile.py @@ -72,12 +72,10 @@ def trace_compile(torch_model, args): print("Compare failed ! Outputs of NPU model are not the same with CPU model !") - - if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument("--checkpoint_filepath", - default="/home/Code/modeltest/CRNN_Chinese_Characters_Rec/output/checkpoints/mixed_second_finetune_acc_97P7.pth", + default="./output/checkpoints/mixed_second_finetune_acc_97P7.pth", type=str, help="The original torch pt file from pretraining") parser.add_argument("--save_dir", @@ -85,7 +83,7 @@ if __name__ == '__main__': type=str, help="The path of the directory that stores the compiled model") parser.add_argument("--config_file", - default="/home/Code/modeltest/CRNN_Chinese_Characters_Rec/lib/config/360CC_config.yaml", + default="./lib/config/360CC_config.yaml", type=str, help="The crnn model config") parser.add_argument('--batch_size', diff --git a/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/aie_val.py b/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/aie_val.py new file mode 100644 index 0000000000..58edb9b606 --- /dev/null +++ b/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/aie_val.py @@ -0,0 +1,104 @@ +import argparse +from easydict import EasyDict as edict +import yaml +import time +import os +import torch +import torch_aie +from tqdm import tqdm +from torch.utils.data import DataLoader +import lib.models.crnn as crnn +import lib.utils.utils as utils +from lib.dataset import get_dataset +from lib.core import function +import lib.config.alphabets as alphabets +from lib.utils.utils import model_info + + +def parse_arg(): + parser = argparse.ArgumentParser(description="eval crnn") + + parser.add_argument('--cfg', help='experiment configuration filename', required=True, type=str) + parser.add_argument('--batch_size', help='batch size', default=1, required=True, type=int) + parser.add_argument('--model_path', help='path to compiled aie model', required=True, type=str) + + args = parser.parse_args() + + with open(args.cfg, 'r') as f: + config = yaml.load(f, Loader=yaml.FullLoader) + # config = yaml.load(f) + config = edict(config) + + config.DATASET.ALPHABETS = alphabets.alphabet + config.MODEL.NUM_CLASSES = len(config.DATASET.ALPHABETS) + + return config, args + + +def main(): + # load config + config, args = parse_arg() + + # set device + torch_aie.set_device(0) + + #load model + model = torch.jit.load(args.model_path) + model.eval() + + #prepare for eval + val_dataset = get_dataset(config)(config, is_train=False) + val_loader = DataLoader( + dataset=val_dataset, + batch_size=args.batch_size, + shuffle=False, + num_workers=config.WORKERS, + pin_memory=config.PIN_MEMORY, + drop_last=True + ) + converter = utils.strLabelConverter(config.DATASET.ALPHABETS) + n_correct = 0 + eval_step = 0 + inf_stream = torch_aie.npu.Stream("npu:0") + inf_time = [] + print("Ready to infer on dataset.") + + # eval on dataset + with torch.no_grad(): + for i, (inp, idx) in tqdm(enumerate(val_loader), total=len(val_loader)): + eval_step += 1 + labels = utils.get_batch_label(val_dataset, idx) + inp_npu = inp.to("npu:0") + + # inference + inf_s = time.time() + with torch_aie.npu.stream(inf_stream): + preds = model(inp_npu) + inf_stream.synchronize() + inf_e = time.time() + inf_time.append(inf_e - inf_s) + + # checkout result + preds = preds.to("cpu") + preds_size = torch.IntTensor([preds.size(0)] * args.batch_size) + _, preds = preds.max(2) + preds = preds.transpose(1, 0).contiguous().view(-1) + sim_preds = converter.decode(preds.data, preds_size.data, raw=False) + for pred, target in zip(sim_preds, labels): + if pred == target: + n_correct += 1 + + print("Infer on dataset done, calculating results.") + num_test_sample = eval_step * args.batch_size + print("[#correct:{} / #total:{}]".format(n_correct, num_test_sample)) + accuracy = n_correct / float(num_test_sample) + print('Accuray on val set is: {:.4f}'.format(accuracy)) + + avg_inf_time = sum(inf_time[3:]) / len(inf_time[3:]) + throughput = args.batch_size / avg_inf_time + print('Throughput on val set is: {:.2f}'.format(throughput)) + + +if __name__ == '__main__': + + main() \ No newline at end of file diff --git a/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/requirements.txt b/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/requirements.txt new file mode 100644 index 0000000000..7b9410c4e6 --- /dev/null +++ b/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/requirements.txt @@ -0,0 +1,8 @@ +torch==2.0.1 +pyyaml +easydict +opencv-python +tqdm +decorator +scipy +attrs \ No newline at end of file -- Gitee From ebf402fd4b905ec78824bec7f2ee788d03d51271 Mon Sep 17 00:00:00 2001 From: r00853487 Date: Tue, 21 Nov 2023 15:18:33 +0800 Subject: [PATCH 4/8] repair readme --- .../built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/README.md b/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/README.md index 6031b157f9..ff25e07488 100644 --- a/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/README.md +++ b/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/README.md @@ -13,7 +13,6 @@ ## 概述 CRNN_Sierkinhane 是一个基于卷积循环网络的中文 OCR 模型。 -v 参考实现: ``` -- Gitee From 7289401abc582e1ec303836a5f5b6e362028c2b1 Mon Sep 17 00:00:00 2001 From: r00853487 Date: Tue, 21 Nov 2023 15:23:23 +0800 Subject: [PATCH 5/8] add license --- .../CRNN_Sierkinhane_for_Pytorch/aie_compile.py | 14 ++++++++++++++ .../cv/ocr/CRNN_Sierkinhane_for_Pytorch/aie_val.py | 14 ++++++++++++++ .../cv/ocr/CRNN_Sierkinhane_for_Pytorch/config.py | 6 +++--- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/aie_compile.py b/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/aie_compile.py index 7c8d1c0575..baacde18f3 100644 --- a/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/aie_compile.py +++ b/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/aie_compile.py @@ -1,3 +1,17 @@ +# Copyright(C) 2023. Huawei Technologies Co.,Ltd. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# 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 os import sys import config diff --git a/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/aie_val.py b/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/aie_val.py index 58edb9b606..8039645052 100644 --- a/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/aie_val.py +++ b/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/aie_val.py @@ -1,3 +1,17 @@ +# Copyright(C) 2023. Huawei Technologies Co.,Ltd. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# 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 argparse from easydict import EasyDict as edict import yaml diff --git a/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/config.py b/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/config.py index e2ca215c2a..760f9087f5 100644 --- a/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/config.py +++ b/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/config.py @@ -1,10 +1,10 @@ -# Copyright 2021 Huawei Technologies Co., Ltd +# Copyright(C) 2023. Huawei Technologies Co.,Ltd. All rights reserved. # -# Licensed under the BSD 3-Clause License (the "License"); +# Licensed under the Apache License, Version 2.0 (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 +# http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, -- Gitee From e5c8e8f2c18ec850691a07d65732ecbdbc5805bf Mon Sep 17 00:00:00 2001 From: r00853487 Date: Wed, 22 Nov 2023 10:26:58 +0800 Subject: [PATCH 6/8] repair aie_compile --- .../cv/ocr/CRNN_Sierkinhane_for_Pytorch/aie_compile.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/aie_compile.py b/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/aie_compile.py index baacde18f3..88e0e8f598 100644 --- a/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/aie_compile.py +++ b/AscendIE/TorchAIE/built-in/cv/ocr/CRNN_Sierkinhane_for_Pytorch/aie_compile.py @@ -70,13 +70,13 @@ def trace_compile(torch_model, args): optimization_level = 0 ) print("torch_aie compile done !") - aie_result = compiled_model(inputs) + aie_result = compiled_model(inputs.to("npu")) print("compiled model is ", compiled_model.graph) compiled_model.save(args.pt_dir) print("torch aie compiled model saved. ") com_res = True - res = cosine_similarity(jit_result, aie_result) + res = cosine_similarity(jit_result, aie_result.to("cpu")) print(res) if res < COSINE_THRESHOLD: com_res = False -- Gitee From fab67be101ee69137ed5e31e74f0061a930536e8 Mon Sep 17 00:00:00 2001 From: r00853487 Date: Wed, 7 Feb 2024 15:16:18 +0800 Subject: [PATCH 7/8] =?UTF-8?q?Albert=E6=96=B0=E5=A2=9E=E6=94=AF=E6=8C=81t?= =?UTF-8?q?orch.export=E5=AF=BC=E5=87=BAfx=E8=BF=9B=E8=A1=8C=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E6=8E=A8=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TorchAIE/built-in/nlp/Albert/README.md | 83 +++++++---- .../built-in/nlp/Albert/albert_dyn.sh | 3 + .../built-in/nlp/Albert/albert_fx.patch | 104 +++++++++++++ .../Albert/{albert.patch => albert_ts.patch} | 0 .../built-in/nlp/Albert/export_albert_fx.py | 141 ++++++++++++++++++ ...port_albert_aie.py => export_albert_ts.py} | 28 ++-- .../built-in/nlp/Albert/requirements.txt | 2 +- .../built-in/nlp/Albert/run_aie_eval.py | 12 +- 8 files changed, 327 insertions(+), 46 deletions(-) create mode 100644 AscendIE/TorchAIE/built-in/nlp/Albert/albert_dyn.sh create mode 100644 AscendIE/TorchAIE/built-in/nlp/Albert/albert_fx.patch rename AscendIE/TorchAIE/built-in/nlp/Albert/{albert.patch => albert_ts.patch} (100%) mode change 100644 => 100755 create mode 100644 AscendIE/TorchAIE/built-in/nlp/Albert/export_albert_fx.py rename AscendIE/TorchAIE/built-in/nlp/Albert/{export_albert_aie.py => export_albert_ts.py} (80%) diff --git a/AscendIE/TorchAIE/built-in/nlp/Albert/README.md b/AscendIE/TorchAIE/built-in/nlp/Albert/README.md index 0fb595c2f7..599e52c046 100644 --- a/AscendIE/TorchAIE/built-in/nlp/Albert/README.md +++ b/AscendIE/TorchAIE/built-in/nlp/Albert/README.md @@ -55,9 +55,9 @@ ALBERT是BERT 的“改进版”,主要通过通过Factorized embedding parame | ------------------------------------------------------------ | ------- | | 固件与驱动 | 23.0.RC1 | | CANN | 7.0.RC1.alpha003 | -| Python | 3.9.11 | -| PyTorch | 2.0.1 | -| Torch_AIE | 6.3.rc2 | +| Python | 3.10.0 | +| PyTorch | 2.1.0 | +| MindIETorch | 1.0.RC1 | | 芯片类型 | Ascend310P3 | # 快速上手 @@ -85,7 +85,6 @@ ALBERT是BERT 的“改进版”,主要通过通过Factorized embedding parame git clone https://github.com/lonePatient/albert_pytorch.git cd albert_pytorch git checkout 46de9ec - patch -p1 < ../albert.patch cd ../ ``` @@ -132,14 +131,40 @@ ALBERT是BERT 的“改进版”,主要通过通过Factorized embedding parame ## 模型推理 1. 编译模型。 + + mindietorch现在支持两种模型编译方式,一种是将原始PyTorch模型通过torch.jit.trace转换为torchscript模型后进行编译,一种是对原始PyTorch模型通过torch.export导出的fx模型进行编译。下面分别介绍两种编译方式,用户可自行选择其中一种。 + + 1.1. torchscript模型编译 - 使用PyTorch将原始模型首先trace成torchscript模型,然后使用torch_aie编译成.pt文件。 + 使用PyTorch将原始模型首先trace成torchscript模型,然后使用mindietorch编译成.pt文件。 ``` - # 以bs32,seq128为例 - python3.9 export_albert_aie.py --batch_size=32 --max_seq_length=128 --compare_cpu + # 为原始模型定义代码打补丁 + cd ./albert_pytorch + git checkout 46de9ec -f + patch -p1 < ../albert_ts.patch + cd ../ + # 以bs32,seq128的静态模型为例 + python3 export_albert_ts.py --batch_size=32 --max_seq_length=128 --compare_cpu ``` - - 执行以上命令将在会把编译好的模型存储值当前目录下的albert_seq128_bs32.pt文件,使用--compare_cpu参数则脚本还会验证编译后的模型与原始torch模型的输出是否一致。 + - 执行以上命令将在会把编译好的静态模型存储到当前目录下的albert_ts_seq128_bs32.pt文件,添加--compare_cpu参数则脚本还会验证编译后的模型与原始torch模型的输出是否一致。 +\ + 1.2. torch.export导出模型编译 + + 使用PyTorch将原始模型导出为fx模型,然后将该fx模型编译成.pt文件。 + ``` + # 为原始模型定义代码打补丁 + cd ./albert_pytorch + git checkout 46de9ec -f + patch -p1 < ../albert_fx.patch + cd ../ + # 以bs32,seq128的静态模型为例 + python3 export_albert_fx.py --batch_size=32 --max_seq_length=128 --compare_cpu + # 也可以选择导出并编译动态模型 + bash albert_dyn.sh + ``` + + - 编译静态模型时,编译后的模型会存储到当前目录下的albert_fx_seq128_bs32.pt文件(以bs32, seq128为例)。编译动态模型时,编译后的模型会存储至albert_fx_seq128_range.pt文件,静态的模型支持输入的batch维度范围为1~64。 2. 推理验证。 @@ -153,32 +178,40 @@ ALBERT是BERT 的“改进版”,主要通过通过Factorized embedding parame 执行推理,验证模型的精度和吞吐量。 ``` - # 以推理bs32,seq128模型为例 - python3.9 ./run_aie_eval.py --aie_model_dir=../albert_seq128_bs32.pt --model_type=SST --model_name_or_path="./prev_trained_model/albert_base_v2" --task_name="SST-2" --data_dir="./dataset/SST-2" --spm_model_file="./prev_trained_model/albert_base_v2/30k-clean.model" --output_dir="./outputs/SST-2/" --do_lower_case --max_seq_length=128 --batch_size=32 + # 当使用torchscript模型编译方式时,以bs32, seq128的静态模型为例 + python3 ./run_aie_eval.py --aie_model_dir=../albert_ts_seq128_bs32.pt --model_type=SST --model_name_or_path="./prev_trained_model/albert_base_v2" --task_name="SST-2" --data_dir="./dataset/SST-2" --spm_model_file="./prev_trained_model/albert_base_v2/30k-clean.model" --output_dir="./outputs/SST-2/" --do_lower_case --max_seq_length=128 --batch_size=32 + + # 当使用torch.export导出模型编译方式时,以bs32, seq128的静态模型为例 + python3 ./run_aie_eval.py --aie_model_dir=../albert_fx_seq128_bs32.pt --model_type=SST --model_name_or_path="./prev_trained_model/albert_base_v2" --task_name="SST-2" --data_dir="./dataset/SST-2" --spm_model_file="./prev_trained_model/albert_base_v2/30k-clean.model" --output_dir="./outputs/SST-2/" --do_lower_case --max_seq_length=128 --batch_size=32 + + # 当使用torch.export导出模型编译方式时,以seq128的动态模型为例 + python3 ./run_aie_eval.py --aie_model_dir=../albert_fx_seq128_range.pt --model_type=SST --model_name_or_path="./prev_trained_model/albert_base_v2" --task_name="SST-2" --data_dir="./dataset/SST-2" --spm_model_file="./prev_trained_model/albert_base_v2/30k-clean.model" --output_dir="./outputs/SST-2/" --do_lower_case --max_seq_length=128 --batch_size=16 ``` - - 推理完成后会输出模型在数据集上的分类准确率以及单位时间内推理的样本数量(吞吐率)。若要推理不同bs和seq配置的模型,只需要更改--aie_model_dir、--max_seq_length和--batch_size三个参数即可。 + - 推理完成后会输出模型在数据集上的分类准确率以及单位时间内推理的样本数量(吞吐率)。对于静态模型,若要推理不同bs和seq配置的模型,需要重复编译模型步骤并设置对应的bs和seq_len,然后再执行推理验证步骤,并更改--aie_model_dir、--max_seq_length和--batch_size三个参数。对于torch.export导出模型编译的方式且模型为动态时,则可以不用重复编译模型步骤,多次执行推理验证步骤使用不同的--batch_size参数(bs的动态范围是1~64)。 # 模型推理性能&精度 -bs32seq128对应的精度性能如下: +通过mindietorch编译后的模型精度性能如下: 精度: -| 输入类型 | 芯片型号 | ACC(bs32seq128) | +| 模型 | 芯片型号 | ACC(bs1seq128) | | --------- | -------- | ------------- | -| 静态 | Ascend310P3 | 92.82% | - -静态模型性能: - -| 模型 | batch size | 310P3性能 | -| :------: | :------: | :------: | -| Albert base v2 | 1 | 532.61 | -| Albert base v2 | 4 | 841.79 | -| Albert base v2 | 8 | 1020.77 | -| Albert base v2 | 16 | 982.26 | -| Albert base v2 | 32 | 988.63 | -| Albert base v2 | 64 | 891.06 | +| 原始Pytorch模型 | CPU | 92.7% | +| torchscript路线编译 | Ascend310P3 | 92.7% | +| torch.export路线编译 | Ascend310P3 | 92.7% | + +静态模型性能(seq128): + +| 模型 | batch size | torchscript路线编译 | torch.export路线编译 | +| :------: | :------: | :------: | :------: | +| Albert base v2 | 1 | 532.61 | 373.29 | +| Albert base v2 | 4 | 841.79 | 354.49 | +| Albert base v2 | 8 | 1020.77 | 415.50 | +| Albert base v2 | 16 | 982.26 | 496.13 | +| Albert base v2 | 32 | 988.63 | 441.28 | +| Albert base v2 | 64 | 891.06 | 415.09 | diff --git a/AscendIE/TorchAIE/built-in/nlp/Albert/albert_dyn.sh b/AscendIE/TorchAIE/built-in/nlp/Albert/albert_dyn.sh new file mode 100644 index 0000000000..a1aa8a8f81 --- /dev/null +++ b/AscendIE/TorchAIE/built-in/nlp/Albert/albert_dyn.sh @@ -0,0 +1,3 @@ +export ASCEND_GLOBAL_LOG_LEVEL=0 +python3 export_albert_fx.py --compare_cpu --is_range +unset ASCEND_GLOBAL_LOG_LEVEL \ No newline at end of file diff --git a/AscendIE/TorchAIE/built-in/nlp/Albert/albert_fx.patch b/AscendIE/TorchAIE/built-in/nlp/Albert/albert_fx.patch new file mode 100644 index 0000000000..2e355daae8 --- /dev/null +++ b/AscendIE/TorchAIE/built-in/nlp/Albert/albert_fx.patch @@ -0,0 +1,104 @@ +diff --git a/model/modeling_albert.py b/model/modeling_albert.py +index 899e6e6..26a837f 100644 +--- a/model/modeling_albert.py ++++ b/model/modeling_albert.py +@@ -1,6 +1,5 @@ + """PyTorch ALBERT model. """ + from __future__ import absolute_import, division, print_function, unicode_literals +-import logging + import math + import os + import sys +@@ -10,7 +9,7 @@ from torch.nn import CrossEntropyLoss, MSELoss + from .modeling_utils import PreTrainedModel, prune_linear_layer + from .configuration_albert import AlbertConfig + from .file_utils import add_start_docstrings +-logger = logging.getLogger(__name__) ++from tools.common import logger # get args via logger + + ALBERT_PRETRAINED_MODEL_ARCHIVE_MAP = { + 'albert-base': "", +@@ -127,7 +126,7 @@ class AlbertEmbeddings(nn.Module): + def forward(self, input_ids, token_type_ids=None, position_ids=None): + seq_length = input_ids.size(1) + if position_ids is None: +- position_ids = torch.arange(seq_length, dtype=torch.long, device=input_ids.device) ++ position_ids = torch.arange(seq_length, dtype=torch.int32, device=input_ids.device) + position_ids = position_ids.unsqueeze(0).expand_as(input_ids) + if token_type_ids is None: + token_type_ids = torch.zeros_like(input_ids) +@@ -453,7 +452,7 @@ class AlbertPreTrainedModel(PreTrainedModel): + + ALBERT_START_DOCSTRING = r""" The ALBERT model was proposed in + `ALBERT: A Lite BERT for Self-supervised Learning of Language Representations`_ +- by Zhenzhong Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut. ++ by Zhenzhong Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut. + This model is a PyTorch `torch.nn.Module`_ sub-class. Use it as a regular PyTorch Module and + refer to the PyTorch documentation for all matter related to general usage and behavior. + .. _`BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding`: +@@ -461,7 +460,7 @@ ALBERT_START_DOCSTRING = r""" The ALBERT model was proposed in + .. _`torch.nn.Module`: + https://pytorch.org/docs/stable/nn.html#module + Parameters: +- config (:class:`~transformers.ALbertConfig`): Model configuration class with all the parameters of the model. ++ config (:class:`~transformers.ALbertConfig`): Model configuration class with all the parameters of the model. + Initializing with a config file does not load the weights associated with the model, only the configuration. + Check out the :meth:`~transformers.PreTrainedModel.from_pretrained` method to load the model weights. + """ +diff --git a/model/modeling_utils.py b/model/modeling_utils.py +index 56a52e9..4a49178 100644 +--- a/model/modeling_utils.py ++++ b/model/modeling_utils.py +@@ -12,6 +12,7 @@ from torch.nn import CrossEntropyLoss + from torch.nn import functional as F + + from model.configuration_utils import PretrainedConfig ++from model.configuration_albert import AlbertConfig + from model.file_utils import cached_path, WEIGHTS_NAME, TF_WEIGHTS_NAME + + logger = logging.getLogger(__name__) +@@ -54,7 +55,7 @@ class PreTrainedModel(nn.Module): + + def __init__(self, config, *inputs, **kwargs): + super(PreTrainedModel, self).__init__() +- if not isinstance(config, PretrainedConfig): ++ if not 'AlbertConfig' in str(type(config)) : # modify via infer changes root + raise ValueError( + "Parameter config in `{}(config)` should be an instance of class `PretrainedConfig`. " + "To create a model from a pretrained model use " +@@ -123,7 +124,7 @@ class PreTrainedModel(nn.Module): + Arguments: + + new_num_tokens: (`optional`) int: +- New number of tokens in the embedding matrix. Increasing the size will add newly initialized vectors at the end. Reducing the size will remove vectors from the end. ++ New number of tokens in the embedding matrix. Increasing the size will add newly initialized vectors at the end. Reducing the size will remove vectors from the end. + If not provided or None: does nothing and just returns a pointer to the input tokens ``torch.nn.Embeddings`` Module of the model. + + Return: ``torch.nn.Embeddings`` +diff --git a/processors/glue.py b/processors/glue.py +index 6628226..8836416 100644 +--- a/processors/glue.py ++++ b/processors/glue.py +@@ -14,10 +14,6 @@ def collate_fn(batch): + Returns a padded tensor of sequences sorted from longest to shortest, + """ + all_input_ids, all_attention_mask, all_token_type_ids, all_lens, all_labels = map(torch.stack, zip(*batch)) +- max_len = max(all_lens).item() +- all_input_ids = all_input_ids[:, :max_len] +- all_attention_mask = all_attention_mask[:, :max_len] +- all_token_type_ids = all_token_type_ids[:, :max_len] + return all_input_ids, all_attention_mask, all_token_type_ids, all_labels + + +@@ -266,6 +262,11 @@ class Sst2Processor(DataProcessor): + return self._create_examples( + self._read_tsv(os.path.join(data_dir, "dev.tsv")), "dev") + ++ def get_test_examples(self, data_dir): ++ """See base class.""" ++ return self._create_examples( ++ self._read_tsv(os.path.join(data_dir, "test.tsv")), "test") ++ + def get_labels(self): + """See base class.""" + return ["0", "1"] diff --git a/AscendIE/TorchAIE/built-in/nlp/Albert/albert.patch b/AscendIE/TorchAIE/built-in/nlp/Albert/albert_ts.patch old mode 100644 new mode 100755 similarity index 100% rename from AscendIE/TorchAIE/built-in/nlp/Albert/albert.patch rename to AscendIE/TorchAIE/built-in/nlp/Albert/albert_ts.patch diff --git a/AscendIE/TorchAIE/built-in/nlp/Albert/export_albert_fx.py b/AscendIE/TorchAIE/built-in/nlp/Albert/export_albert_fx.py new file mode 100644 index 0000000000..f7f0443c74 --- /dev/null +++ b/AscendIE/TorchAIE/built-in/nlp/Albert/export_albert_fx.py @@ -0,0 +1,141 @@ +# Copyright(C) 2023. Huawei Technologies Co.,Ltd. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# 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 os +import argparse +import torch +from torch._export import export, dynamic_dim +import mindietorch +from mindietorch import _enums +import parse + + +COSINE_THRESHOLD = 0.999 + + +def cosine_similarity(gt_tensor, pred_tensor): + gt_tensor = gt_tensor.flatten().to(torch.float32) + pred_tensor = pred_tensor.flatten().to(torch.float32) + if torch.sum(gt_tensor) == 0.0 or torch.sum(pred_tensor) == 0.0: + if torch.allclose(gt_tensor, pred_tensor, atol=1e-4, rtol=1e-4, equal_nan=True): + return 1.0 + res = torch.nn.functional.cosine_similarity(gt_tensor, pred_tensor, dim=0, eps=1e-6) + res = res.cpu().detach().item() + return res + + +def get_torch_model(ar): + _, model, _ = parse.load_data_model(ar) + return model + + +def aie_compile(torch_model, ar): + torch_model.eval() + input_shape = (ar.batch_size, ar.max_seq_length) + input_ids = torch.randint(high = 1, size = input_shape, dtype = torch.int32) + att_mask = torch.randint(high = 3, size = input_shape, dtype = torch.int32) + token_ids = torch.randint(high = 1, size = input_shape, dtype = torch.int32) + input_data = [ input_ids, att_mask, token_ids ] + input_npu_data = [ input_ids.to("npu:0"), att_mask.to("npu:0"), token_ids.to("npu:0") ] + + print("mindietorch compile start.") + mindietorch.set_device(0) + if ar.is_range: + print("compiling shape range model") + min_shape = (1, ar.max_seq_length) + max_shape = (64, ar.max_seq_length) + compile_inputs = [mindietorch.Input(min_shape = min_shape, max_shape = max_shape, dtype = torch.int32), + mindietorch.Input(min_shape = min_shape, max_shape = max_shape, dtype = torch.int32), + mindietorch.Input(min_shape = min_shape, max_shape = max_shape, dtype = torch.int32)] + + constraint = [ + 1 <= dynamic_dim(input_ids, 0), + dynamic_dim(input_ids, 0) <= 64, + dynamic_dim(input_ids, 0) == dynamic_dim(att_mask, 0), + dynamic_dim(input_ids, 0) == dynamic_dim(token_ids, 0), + ] + torch_model = export(torch_model, args=(input_ids, att_mask, token_ids), constraints=constraint) + else: + print("compiling static model") + compile_inputs = [mindietorch.Input(shape = input_shape, dtype = torch.int32), + mindietorch.Input(shape = input_shape, dtype = torch.int32), + mindietorch.Input(shape = input_shape, dtype = torch.int32)] + compiled_model = mindietorch.compile( + torch_model, + inputs = compile_inputs, + precision_policy = _enums.PrecisionPolicy.FP16, + soc_version = "Ascend310P3", + ir = "dynamo" + ) + print("mindietorch compile done !") + traced_model = torch.jit.trace(compiled_model, input_npu_data) + traced_model.save(ar.pt_dir) + + + if ar.compare_cpu: + print("start to check the percision of npu model.") + com_res = True + mrt_model = torch.jit.load(ar.pt_dir) + mrt_res = mrt_model(*input_npu_data) + print("mindie infer done !") + ref_res = torch_model(*input_data) + print("torch infer done !") + + for j, a in zip(mrt_res, ref_res): + res = cosine_similarity(j.to("cpu"), a) + print(res) + if res < COSINE_THRESHOLD: + com_res = False + + if com_res: + print("Compare success ! NPU model have the same output with CPU model !") + else: + print("Compare failed ! Outputs of NPU model are not the same with CPU model !") + return compiled_model + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("--batch_size", type=int, default=32, + help="seq length for input data.") + parser.add_argument("--prefix_dir", type=str, default='./albert_pytorch', + help="prefix dir for ori model code") + parser.add_argument("--pth_dir", type=str, default='./albert_pytorch/outputs/SST-2', + help="dir of pth, load args.bin and model.bin") + parser.add_argument("--data_dir", type=str, default='./albert_pytorch/dataset/SST-2', + help="dir of dataset") + parser.add_argument("--max_seq_length", type=int, default=128, + help="seq length for input data.") + parser.add_argument("--save_dir", type=str, default='./', + help="save dir of model compiled by mindietorch") + parser.add_argument("--compare_cpu", action='store_true', + help="Whether to check the percision of npu model.") + parser.add_argument("--is_range", action='store_true', + help="Whether to compile shape range model.") + + ar = parser.parse_args() + + ar.pth_arg_path = os.path.join(ar.pth_dir, "training_args.bin") + ar.data_type = 'dev' + if ar.is_range: + ar.model_name = "albert_fx_seq{}_range".format(ar.max_seq_length) + else: + ar.model_name = "albert_fx_seq{}_bs{}".format(ar.max_seq_length, ar.batch_size) + ar.pt_dir = ar.save_dir + ar.model_name + ".pt" + torch_model = get_torch_model(ar) + aie_compile(torch_model, ar) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/AscendIE/TorchAIE/built-in/nlp/Albert/export_albert_aie.py b/AscendIE/TorchAIE/built-in/nlp/Albert/export_albert_ts.py similarity index 80% rename from AscendIE/TorchAIE/built-in/nlp/Albert/export_albert_aie.py rename to AscendIE/TorchAIE/built-in/nlp/Albert/export_albert_ts.py index a59038a0be..b18279f297 100644 --- a/AscendIE/TorchAIE/built-in/nlp/Albert/export_albert_aie.py +++ b/AscendIE/TorchAIE/built-in/nlp/Albert/export_albert_ts.py @@ -15,8 +15,8 @@ import os import argparse import torch -import torch_aie -from torch_aie import _enums +import mindietorch +from mindietorch import _enums import parse @@ -45,25 +45,25 @@ def aie_compile(torch_model, ar): input_ids = torch.randint(high = 1, size = input_shape, dtype = torch.int32) att_mask = torch.randint(high = 3, size = input_shape, dtype = torch.int32) token_ids = torch.randint(high = 1, size = input_shape, dtype = torch.int32) - input_data = [ input_ids, att_mask, token_ids] + input_data = [ input_ids, att_mask, token_ids ] print("start to trace model.") traced_model = torch.jit.trace(torch_model, input_data) traced_model.eval() print("trace done !") - print("torch_aie compile start.") - torch_aie.set_device(0) - compile_inputs = [torch_aie.Input(shape = input_shape, dtype = torch.int32, format = torch_aie.TensorFormat.ND), - torch_aie.Input(shape = input_shape, dtype = torch.int32, format = torch_aie.TensorFormat.ND), - torch_aie.Input(shape = input_shape, dtype = torch.int32, format = torch_aie.TensorFormat.ND)] - compiled_model = torch_aie.compile( + print("mindietorch compile start.") + mindietorch.set_device(0) + compile_inputs = [mindietorch.Input(shape = input_shape, dtype = torch.int32, format = mindietorch.TensorFormat.ND), + mindietorch.Input(shape = input_shape, dtype = torch.int32, format = mindietorch.TensorFormat.ND), + mindietorch.Input(shape = input_shape, dtype = torch.int32, format = mindietorch.TensorFormat.ND)] + compiled_model = mindietorch.compile( traced_model, inputs = compile_inputs, precision_policy = _enums.PrecisionPolicy.FP16, soc_version = "Ascend310P3", optimization_level = 0 ) - print("torch_aie compile done !") + print("mindietorch compile done !") compiled_model.save(ar.pt_dir) @@ -71,13 +71,13 @@ def aie_compile(torch_model, ar): print("start to check the percision of npu model.") com_res = True compiled_model = torch.jit.load(ar.pt_dir) - jit_res = traced_model(input_ids, att_mask, token_ids) + jit_res = traced_model(input_ids.to("npu:0"), att_mask.to("npu:0"), token_ids.to("npu:0")) print("jit infer done !") aie_res = compiled_model(input_ids, att_mask, token_ids) print("aie infer done !") for j, a in zip(jit_res, aie_res): - res = cosine_similarity(j, a) + res = cosine_similarity(j.to("cpu"), a) print(res) if res < COSINE_THRESHOLD: com_res = False @@ -102,14 +102,14 @@ def main(): parser.add_argument("--max_seq_length", type=int, default=128, help="seq length for input data.") parser.add_argument("--save_dir", type=str, default='./', - help="save dir of model compiled by torch_aie") + help="save dir of model compiled by mindietorch") parser.add_argument("--compare_cpu", action='store_true', help="Whether to check the percision of npu model.") ar = parser.parse_args() ar.pth_arg_path = os.path.join(ar.pth_dir, "training_args.bin") ar.data_type = 'dev' - ar.model_name = "albert_seq{}_bs{}".format(ar.max_seq_length, ar.batch_size) + ar.model_name = "albert_ts_seq{}_bs{}".format(ar.max_seq_length, ar.batch_size) ar.pt_dir = ar.save_dir + ar.model_name + ".pt" torch_model = get_torch_model(ar) aie_compile(torch_model, ar) diff --git a/AscendIE/TorchAIE/built-in/nlp/Albert/requirements.txt b/AscendIE/TorchAIE/built-in/nlp/Albert/requirements.txt index e7a234a9f8..2ad896e75b 100644 --- a/AscendIE/TorchAIE/built-in/nlp/Albert/requirements.txt +++ b/AscendIE/TorchAIE/built-in/nlp/Albert/requirements.txt @@ -1,7 +1,7 @@ ptvsd==4.3.2 six==1.16.0 sentencepiece==0.1.96 -torch==2.0.1 +torch==2.1.0 boto3==1.17.97 botocore==1.20.97 requests==2.25.1 diff --git a/AscendIE/TorchAIE/built-in/nlp/Albert/run_aie_eval.py b/AscendIE/TorchAIE/built-in/nlp/Albert/run_aie_eval.py index 3d0828f3e5..c4e6aed6de 100644 --- a/AscendIE/TorchAIE/built-in/nlp/Albert/run_aie_eval.py +++ b/AscendIE/TorchAIE/built-in/nlp/Albert/run_aie_eval.py @@ -41,7 +41,7 @@ from tools.common import seed_everything from tools.common import init_logger, logger from callback.progressbar import ProgressBar -import torch_aie +import mindietorch def evaluate(args, model, tokenizer, prefix=""): @@ -62,8 +62,8 @@ def evaluate(args, model, tokenizer, prefix=""): collate_fn=collate_fn, drop_last=True) # Eval! - infer_stream = torch_aie.npu.Stream("npu:0") - h2d = torch_aie.npu.Stream("npu:0") + infer_stream = mindietorch.npu.Stream("npu:0") + h2d = mindietorch.npu.Stream("npu:0") logger.info("***** Running evaluation {} *****".format(prefix)) logger.info(" Num examples = %d", len(eval_dataset)) @@ -77,7 +77,7 @@ def evaluate(args, model, tokenizer, prefix=""): for step, batch in enumerate(eval_dataloader): model.eval() # prepare data on npu - with torch_aie.npu.stream(h2d): + with mindietorch.npu.stream(h2d): b = [] for t in batch: t_npu = t.to(args.device) @@ -90,7 +90,7 @@ def evaluate(args, model, tokenizer, prefix=""): # forward with torch.no_grad(): inf_s = time.time() - with torch_aie.npu.stream(infer_stream): + with mindietorch.npu.stream(infer_stream): outputs = model(b[0], b[1], b[2]) infer_stream.synchronize() inf_e = time.time() @@ -220,7 +220,7 @@ def main(): init_logger(log_file=args.output_dir + '/{}-{}.log'.format(args.model_type, args.task_name)) # setup npu device - torch_aie.set_device(0) + mindietorch.set_device(0) device = torch.device("npu:0") args.n_gpu = 1 args.device = device -- Gitee From 6a4cbf6ba23f13479d9bf079232af0dbc217ff7e Mon Sep 17 00:00:00 2001 From: r00853487 Date: Wed, 7 Feb 2024 15:27:41 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=8B=B7=E8=B4=9D=E7=9A=84=E7=AC=94=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AscendIE/TorchAIE/built-in/nlp/Albert/export_albert_ts.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/AscendIE/TorchAIE/built-in/nlp/Albert/export_albert_ts.py b/AscendIE/TorchAIE/built-in/nlp/Albert/export_albert_ts.py index b18279f297..6072a5ca18 100644 --- a/AscendIE/TorchAIE/built-in/nlp/Albert/export_albert_ts.py +++ b/AscendIE/TorchAIE/built-in/nlp/Albert/export_albert_ts.py @@ -71,13 +71,13 @@ def aie_compile(torch_model, ar): print("start to check the percision of npu model.") com_res = True compiled_model = torch.jit.load(ar.pt_dir) - jit_res = traced_model(input_ids.to("npu:0"), att_mask.to("npu:0"), token_ids.to("npu:0")) + jit_res = traced_model(input_ids, att_mask, token_ids) print("jit infer done !") - aie_res = compiled_model(input_ids, att_mask, token_ids) + aie_res = compiled_model(input_ids.to("npu:0"), att_mask.to("npu:0"), token_ids.to("npu:0")) print("aie infer done !") for j, a in zip(jit_res, aie_res): - res = cosine_similarity(j.to("cpu"), a) + res = cosine_similarity(j, a.to("cpu")) print(res) if res < COSINE_THRESHOLD: com_res = False -- Gitee