From 3df895f477b7b449610a96c14655b377ede28eb2 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 1 Jun 2021 17:35:27 +0800 Subject: [PATCH] ncf && textrcnn && bgcf && tinybert 310 infer modified: official/recommend/ncf/preprocess.py --- model_zoo/official/gnn/bgcf/README.md | 36 ++++ model_zoo/official/gnn/bgcf/README_CN.md | 36 ++++ .../gnn/bgcf/ascend310_infer/CMakeLists.txt | 14 ++ .../gnn/bgcf/ascend310_infer/build.sh | 29 ++++ .../gnn/bgcf/ascend310_infer/inc/utils.h | 32 ++++ .../gnn/bgcf/ascend310_infer/src/main.cc | 160 ++++++++++++++++++ .../gnn/bgcf/ascend310_infer/src/utils.cc | 129 ++++++++++++++ model_zoo/official/gnn/bgcf/postprocess.py | 75 ++++++++ model_zoo/official/gnn/bgcf/preprocess.py | 90 ++++++++++ .../gnn/bgcf/scripts/run_infer_310.sh | 128 ++++++++++++++ model_zoo/official/nlp/tinybert/README.md | 37 ++++ model_zoo/official/nlp/tinybert/README_CN.md | 37 ++++ .../tinybert/ascend310_infer/CMakeLists.txt | 14 ++ .../nlp/tinybert/ascend310_infer/build.sh | 29 ++++ .../nlp/tinybert/ascend310_infer/inc/utils.h | 32 ++++ .../nlp/tinybert/ascend310_infer/src/main.cc | 142 ++++++++++++++++ .../nlp/tinybert/ascend310_infer/src/utils.cc | 129 ++++++++++++++ .../official/nlp/tinybert/postprocess.py | 103 +++++++++++ model_zoo/official/nlp/tinybert/preprocess.py | 75 ++++++++ .../nlp/tinybert/scripts/run_infer_310.sh | 131 ++++++++++++++ model_zoo/official/recommend/ncf/README.md | 35 ++++ .../ncf/ascend310_infer/CMakeLists.txt | 14 ++ .../recommend/ncf/ascend310_infer/build.sh | 29 ++++ .../recommend/ncf/ascend310_infer/inc/utils.h | 32 ++++ .../recommend/ncf/ascend310_infer/src/main.cc | 140 +++++++++++++++ .../ncf/ascend310_infer/src/utils.cc | 129 ++++++++++++++ .../recommend/ncf/default_config.yaml | 16 +- .../official/recommend/ncf/postprocess.py | 52 ++++++ .../official/recommend/ncf/preprocess.py | 46 +++++ .../recommend/ncf/scripts/run_infer_310.sh | 120 +++++++++++++ .../textrcnn/ascend310_infer/CMakeLists.txt | 14 ++ .../nlp/textrcnn/ascend310_infer/build.sh | 29 ++++ .../nlp/textrcnn/ascend310_infer/inc/utils.h | 32 ++++ .../nlp/textrcnn/ascend310_infer/src/main.cc | 130 ++++++++++++++ .../nlp/textrcnn/ascend310_infer/src/utils.cc | 129 ++++++++++++++ .../research/nlp/textrcnn/postprocess.py | 44 +++++ model_zoo/research/nlp/textrcnn/preprocess.py | 45 +++++ model_zoo/research/nlp/textrcnn/readme.md | 35 ++++ .../nlp/textrcnn/scripts/run_infer_310.sh | 120 +++++++++++++ 39 files changed, 2645 insertions(+), 4 deletions(-) create mode 100644 model_zoo/official/gnn/bgcf/ascend310_infer/CMakeLists.txt create mode 100644 model_zoo/official/gnn/bgcf/ascend310_infer/build.sh create mode 100644 model_zoo/official/gnn/bgcf/ascend310_infer/inc/utils.h create mode 100644 model_zoo/official/gnn/bgcf/ascend310_infer/src/main.cc create mode 100644 model_zoo/official/gnn/bgcf/ascend310_infer/src/utils.cc create mode 100644 model_zoo/official/gnn/bgcf/postprocess.py create mode 100644 model_zoo/official/gnn/bgcf/preprocess.py create mode 100644 model_zoo/official/gnn/bgcf/scripts/run_infer_310.sh create mode 100644 model_zoo/official/nlp/tinybert/ascend310_infer/CMakeLists.txt create mode 100644 model_zoo/official/nlp/tinybert/ascend310_infer/build.sh create mode 100644 model_zoo/official/nlp/tinybert/ascend310_infer/inc/utils.h create mode 100644 model_zoo/official/nlp/tinybert/ascend310_infer/src/main.cc create mode 100644 model_zoo/official/nlp/tinybert/ascend310_infer/src/utils.cc create mode 100644 model_zoo/official/nlp/tinybert/postprocess.py create mode 100644 model_zoo/official/nlp/tinybert/preprocess.py create mode 100644 model_zoo/official/nlp/tinybert/scripts/run_infer_310.sh create mode 100644 model_zoo/official/recommend/ncf/ascend310_infer/CMakeLists.txt create mode 100644 model_zoo/official/recommend/ncf/ascend310_infer/build.sh create mode 100644 model_zoo/official/recommend/ncf/ascend310_infer/inc/utils.h create mode 100644 model_zoo/official/recommend/ncf/ascend310_infer/src/main.cc create mode 100644 model_zoo/official/recommend/ncf/ascend310_infer/src/utils.cc create mode 100644 model_zoo/official/recommend/ncf/postprocess.py create mode 100644 model_zoo/official/recommend/ncf/preprocess.py create mode 100644 model_zoo/official/recommend/ncf/scripts/run_infer_310.sh create mode 100644 model_zoo/research/nlp/textrcnn/ascend310_infer/CMakeLists.txt create mode 100644 model_zoo/research/nlp/textrcnn/ascend310_infer/build.sh create mode 100644 model_zoo/research/nlp/textrcnn/ascend310_infer/inc/utils.h create mode 100644 model_zoo/research/nlp/textrcnn/ascend310_infer/src/main.cc create mode 100644 model_zoo/research/nlp/textrcnn/ascend310_infer/src/utils.cc create mode 100644 model_zoo/research/nlp/textrcnn/postprocess.py create mode 100644 model_zoo/research/nlp/textrcnn/preprocess.py create mode 100644 model_zoo/research/nlp/textrcnn/scripts/run_infer_310.sh diff --git a/model_zoo/official/gnn/bgcf/README.md b/model_zoo/official/gnn/bgcf/README.md index d0bc2c5d3c2..c6bfaade3e1 100644 --- a/model_zoo/official/gnn/bgcf/README.md +++ b/model_zoo/official/gnn/bgcf/README.md @@ -16,6 +16,10 @@ - [Training](#training) - [Evaluation Process](#evaluation-process) - [Evaluation](#evaluation) + - [Inference Process](#inference-process) + - [Export MindIR](#export-mindir) + - [Infer on Ascend310](#infer-on-ascend310) + - [result](#result) - [Model Description](#model-description) - [Performance](#performance) - [Description of random situation](#description-of-random-situation) @@ -244,6 +248,38 @@ Parameters for both training and evaluation can be set in config.py. sedp_@10:0.01926, sedp_@20:0.01547, nov_@10:7.60851, nov_@20:7.81969 ``` +## Inference Process + +### [Export MindIR](#contents) + +```shell +python export.py --ckpt_file [CKPT_PATH] --file_name [FILE_NAME] --file_format [FILE_FORMAT] +``` + +The ckpt_file parameter is required, +`EXPORT_FORMAT` should be in ["AIR", "MINDIR"] + +### Infer on Ascend310 + +Before performing inference, the mindir file must be exported by `export.py` script. We only provide an example of inference using MINDIR model. + +```shell +# Ascend310 inference +bash run_infer_310.sh [MINDIR_PATH] [DATASET_NAME] [NEED_PREPROCESS] [DEVICE_ID] +``` + +- `NEED_PREPROCESS` means weather need preprocess or not, it's value is 'y' or 'n'. +- `DEVICE_ID` is optional, default value is 0. + +### result + +Inference result is saved in current path, you can find result like this in acc.log file. + +```bash +recall_@10:0.10383, recall_@20:0.15524, ndcg_@10:0.07503, ndcg_@20:0.09249, + sedp_@10:0.01926, sedp_@20:0.01547, nov_@10:7.60851, nov_@20:7.81969 +``` + ## [Model Description](#contents) ### [Performance](#contents) diff --git a/model_zoo/official/gnn/bgcf/README_CN.md b/model_zoo/official/gnn/bgcf/README_CN.md index f9040c9f89b..3c6f767a48e 100644 --- a/model_zoo/official/gnn/bgcf/README_CN.md +++ b/model_zoo/official/gnn/bgcf/README_CN.md @@ -17,6 +17,10 @@ - [训练](#训练) - [评估过程](#评估过程) - [评估](#评估) + - [推理过程](#推理过程) + - [导出MindIR](#导出mindir) + - [在Ascend310执行推理](#在ascend310执行推理) + - [结果](#结果) - [模型描述](#模型描述) - [性能](#性能) - [随机情况说明](#随机情况说明) @@ -271,6 +275,38 @@ BGCF包含两个主要模块。首先是抽样,它生成基于节点复制的 ``` +## 推理过程 + +### [导出MindIR](#contents) + +```shell +python export.py --ckpt_file [CKPT_PATH] --file_name [FILE_NAME] --file_format [FILE_FORMAT] +``` + +参数ckpt_file为必填项, +`EXPORT_FORMAT` 必须在 ["AIR", "MINDIR"]中选择。 + +### 在Ascend310执行推理 + +在执行推理前,mindir文件必须通过`export.py`脚本导出。以下展示了使用minir模型执行推理的示例。 + +```shell +# Ascend310 inference +bash run_infer_310.sh [MINDIR_PATH] [DATASET_NAME] [NEED_PREPROCESS] [DEVICE_ID] +``` + +- `NEED_PREPROCESS` 表示数据是否需要预处理,取值范围为 'y' 或者 'n'。 +- `DEVICE_ID` 可选,默认值为0。 + +### 结果 + +推理结果保存在脚本执行的当前路径,你可以在acc.log中看到以下精度计算结果。 + +```bash +recall_@10:0.10383, recall_@20:0.15524, ndcg_@10:0.07503, ndcg_@20:0.09249, + sedp_@10:0.01926, sedp_@20:0.01547, nov_@10:7.60851, nov_@20:7.81969 +``` + ## 模型描述 ### 训练性能 diff --git a/model_zoo/official/gnn/bgcf/ascend310_infer/CMakeLists.txt b/model_zoo/official/gnn/bgcf/ascend310_infer/CMakeLists.txt new file mode 100644 index 00000000000..170e6c5275e --- /dev/null +++ b/model_zoo/official/gnn/bgcf/ascend310_infer/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.14.1) +project(Ascend310Infer) +add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -g -std=c++17 -Werror -Wall -fPIE -Wl,--allow-shlib-undefined") +set(PROJECT_SRC_ROOT ${CMAKE_CURRENT_LIST_DIR}/) +option(MINDSPORE_PATH "mindspore install path" "") +include_directories(${MINDSPORE_PATH}) +include_directories(${MINDSPORE_PATH}/include) +include_directories(${PROJECT_SRC_ROOT}) +find_library(MS_LIB libmindspore.so ${MINDSPORE_PATH}/lib) +file(GLOB_RECURSE MD_LIB ${MINDSPORE_PATH}/_c_dataengine*) + +add_executable(main src/main.cc src/utils.cc) +target_link_libraries(main ${MS_LIB} ${MD_LIB} gflags) diff --git a/model_zoo/official/gnn/bgcf/ascend310_infer/build.sh b/model_zoo/official/gnn/bgcf/ascend310_infer/build.sh new file mode 100644 index 00000000000..285514e19f2 --- /dev/null +++ b/model_zoo/official/gnn/bgcf/ascend310_infer/build.sh @@ -0,0 +1,29 @@ +#!/bin/bash +# Copyright 2021 Huawei Technologies Co., Ltd +# +# 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. +# ============================================================================ +if [ -d out ]; then + rm -rf out +fi + +mkdir out +cd out || exit + +if [ -f "Makefile" ]; then + make clean +fi + +cmake .. \ + -DMINDSPORE_PATH="`pip3.7 show mindspore-ascend | grep Location | awk '{print $2"/mindspore"}' | xargs realpath`" +make diff --git a/model_zoo/official/gnn/bgcf/ascend310_infer/inc/utils.h b/model_zoo/official/gnn/bgcf/ascend310_infer/inc/utils.h new file mode 100644 index 00000000000..efebe03a8c1 --- /dev/null +++ b/model_zoo/official/gnn/bgcf/ascend310_infer/inc/utils.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_INFERENCE_UTILS_H_ +#define MINDSPORE_INFERENCE_UTILS_H_ + +#include +#include +#include +#include +#include +#include "include/api/types.h" + +std::vector GetAllFiles(std::string_view dirName); +DIR *OpenDir(std::string_view dirName); +std::string RealPath(std::string_view path); +mindspore::MSTensor ReadFileToTensor(const std::string &file); +int WriteResult(const std::string& imageFile, const std::vector &outputs); +#endif diff --git a/model_zoo/official/gnn/bgcf/ascend310_infer/src/main.cc b/model_zoo/official/gnn/bgcf/ascend310_infer/src/main.cc new file mode 100644 index 00000000000..94296af69cf --- /dev/null +++ b/model_zoo/official/gnn/bgcf/ascend310_infer/src/main.cc @@ -0,0 +1,160 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "include/api/model.h" +#include "include/api/context.h" +#include "include/api/types.h" +#include "include/api/serialization.h" +#include "include/dataset/execute.h" +#include "include/dataset/vision.h" +#include "inc/utils.h" + +using mindspore::Context; +using mindspore::Serialization; +using mindspore::Model; +using mindspore::Status; +using mindspore::MSTensor; +using mindspore::dataset::Execute; +using mindspore::ModelType; +using mindspore::GraphCell; +using mindspore::kSuccess; + +DEFINE_string(mindir_path, "", "mindir path"); +DEFINE_string(input0_path, ".", "input0 path"); +DEFINE_string(input1_path, ".", "input1 path"); +DEFINE_string(input2_path, ".", "input2 path"); +DEFINE_string(input3_path, ".", "input3 path"); +DEFINE_string(input4_path, ".", "input4 path"); +DEFINE_string(input5_path, ".", "input5 path"); +DEFINE_string(input6_path, ".", "input6 path"); +DEFINE_int32(device_id, 0, "device id"); + +int main(int argc, char **argv) { + gflags::ParseCommandLineFlags(&argc, &argv, true); + if (RealPath(FLAGS_mindir_path).empty()) { + std::cout << "Invalid mindir" << std::endl; + return 1; + } + + auto context = std::make_shared(); + auto ascend310 = std::make_shared(); + ascend310->SetDeviceID(FLAGS_device_id); + context->MutableDeviceInfo().push_back(ascend310); + mindspore::Graph graph; + Serialization::Load(FLAGS_mindir_path, ModelType::kMindIR, &graph); + + Model model; + Status ret = model.Build(GraphCell(graph), context); + if (ret != kSuccess) { + std::cout << "ERROR: Build failed." << std::endl; + return 1; + } + + std::vector model_inputs = model.GetInputs(); + if (model_inputs.empty()) { + std::cout << "Invalid model, inputs is empty." << std::endl; + return 1; + } + + auto input0_files = GetAllFiles(FLAGS_input0_path); + auto input1_files = GetAllFiles(FLAGS_input1_path); + auto input2_files = GetAllFiles(FLAGS_input2_path); + auto input3_files = GetAllFiles(FLAGS_input3_path); + auto input4_files = GetAllFiles(FLAGS_input4_path); + auto input5_files = GetAllFiles(FLAGS_input5_path); + auto input6_files = GetAllFiles(FLAGS_input6_path); + + if (input0_files.empty() || input1_files.empty() || input2_files.empty() || input3_files.empty() + || input4_files.empty() || input5_files.empty() || input6_files.empty()) { + std::cout << "ERROR: input data empty." << std::endl; + return 1; + } + + std::map costTime_map; + size_t size = input0_files.size(); + + for (size_t i = 0; i < size; ++i) { + struct timeval start = {0}; + struct timeval end = {0}; + double startTimeMs, endTimeMs; + std::vector inputs, outputs; + std::cout << "Start predict input files:" << input0_files[i] << std::endl; + + auto input0 = ReadFileToTensor(input0_files[i]); + auto input1 = ReadFileToTensor(input1_files[i]); + auto input2 = ReadFileToTensor(input2_files[i]); + auto input3 = ReadFileToTensor(input3_files[i]); + auto input4 = ReadFileToTensor(input4_files[i]); + auto input5 = ReadFileToTensor(input5_files[i]); + auto input6 = ReadFileToTensor(input6_files[i]); + + inputs.emplace_back(model_inputs[0].Name(), model_inputs[0].DataType(), model_inputs[0].Shape(), + input0.Data().get(), input0.DataSize()); + inputs.emplace_back(model_inputs[1].Name(), model_inputs[1].DataType(), model_inputs[1].Shape(), + input1.Data().get(), input1.DataSize()); + inputs.emplace_back(model_inputs[2].Name(), model_inputs[2].DataType(), model_inputs[2].Shape(), + input2.Data().get(), input2.DataSize()); + inputs.emplace_back(model_inputs[3].Name(), model_inputs[3].DataType(), model_inputs[3].Shape(), + input3.Data().get(), input3.DataSize()); + inputs.emplace_back(model_inputs[4].Name(), model_inputs[4].DataType(), model_inputs[4].Shape(), + input4.Data().get(), input4.DataSize()); + inputs.emplace_back(model_inputs[5].Name(), model_inputs[5].DataType(), model_inputs[5].Shape(), + input5.Data().get(), input5.DataSize()); + inputs.emplace_back(model_inputs[6].Name(), model_inputs[6].DataType(), model_inputs[6].Shape(), + input6.Data().get(), input6.DataSize()); + + gettimeofday(&start, nullptr); + ret = model.Predict(inputs, &outputs); + gettimeofday(&end, nullptr); + if (ret != kSuccess) { + std::cout << "Predict " << input0_files[i] << " failed." << std::endl; + return 1; + } + startTimeMs = (1.0 * start.tv_sec * 1000000 + start.tv_usec) / 1000; + endTimeMs = (1.0 * end.tv_sec * 1000000 + end.tv_usec) / 1000; + costTime_map.insert(std::pair(startTimeMs, endTimeMs)); + WriteResult(input0_files[i], outputs); + } + double average = 0.0; + int inferCount = 0; + + for (auto iter = costTime_map.begin(); iter != costTime_map.end(); iter++) { + double diff = 0.0; + diff = iter->second - iter->first; + average += diff; + inferCount++; + } + average = average / inferCount; + std::stringstream timeCost; + timeCost << "NN inference cost average time: "<< average << " ms of infer_count " << inferCount << std::endl; + std::cout << "NN inference cost average time: "<< average << "ms of infer_count " << inferCount << std::endl; + std::string fileName = "./time_Result" + std::string("/test_perform_static.txt"); + std::ofstream fileStream(fileName.c_str(), std::ios::trunc); + fileStream << timeCost.str(); + fileStream.close(); + costTime_map.clear(); + return 0; +} diff --git a/model_zoo/official/gnn/bgcf/ascend310_infer/src/utils.cc b/model_zoo/official/gnn/bgcf/ascend310_infer/src/utils.cc new file mode 100644 index 00000000000..c947e4d5f45 --- /dev/null +++ b/model_zoo/official/gnn/bgcf/ascend310_infer/src/utils.cc @@ -0,0 +1,129 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#include +#include +#include +#include "inc/utils.h" + +using mindspore::MSTensor; +using mindspore::DataType; + +std::vector GetAllFiles(std::string_view dirName) { + struct dirent *filename; + DIR *dir = OpenDir(dirName); + if (dir == nullptr) { + return {}; + } + std::vector res; + while ((filename = readdir(dir)) != nullptr) { + std::string dName = std::string(filename->d_name); + if (dName == "." || dName == ".." || filename->d_type != DT_REG) { + continue; + } + res.emplace_back(std::string(dirName) + "/" + filename->d_name); + } + std::sort(res.begin(), res.end()); + for (auto &f : res) { + std::cout << "image file: " << f << std::endl; + } + return res; +} + +int WriteResult(const std::string& imageFile, const std::vector &outputs) { + std::string homePath = "./result_Files"; + for (size_t i = 0; i < outputs.size(); ++i) { + size_t outputSize; + std::shared_ptr netOutput; + netOutput = outputs[i].Data(); + outputSize = outputs[i].DataSize(); + int pos = imageFile.rfind('/'); + std::string fileName(imageFile, pos + 1); + fileName.replace(fileName.find('.'), fileName.size() - fileName.find('.'), '_' + std::to_string(i) + ".bin"); + std::string outFileName = homePath + "/" + fileName; + FILE * outputFile = fopen(outFileName.c_str(), "wb"); + fwrite(netOutput.get(), outputSize, sizeof(char), outputFile); + fclose(outputFile); + outputFile = nullptr; + } + return 0; +} + +mindspore::MSTensor ReadFileToTensor(const std::string &file) { + if (file.empty()) { + std::cout << "Pointer file is nullptr" << std::endl; + return mindspore::MSTensor(); + } + + std::ifstream ifs(file); + if (!ifs.good()) { + std::cout << "File: " << file << " is not exist" << std::endl; + return mindspore::MSTensor(); + } + + if (!ifs.is_open()) { + std::cout << "File: " << file << "open failed" << std::endl; + return mindspore::MSTensor(); + } + + ifs.seekg(0, std::ios::end); + size_t size = ifs.tellg(); + mindspore::MSTensor buffer(file, mindspore::DataType::kNumberTypeUInt8, {static_cast(size)}, nullptr, size); + + ifs.seekg(0, std::ios::beg); + ifs.read(reinterpret_cast(buffer.MutableData()), size); + ifs.close(); + + return buffer; +} + + +DIR *OpenDir(std::string_view dirName) { + if (dirName.empty()) { + std::cout << " dirName is null ! " << std::endl; + return nullptr; + } + std::string realPath = RealPath(dirName); + struct stat s; + lstat(realPath.c_str(), &s); + if (!S_ISDIR(s.st_mode)) { + std::cout << "dirName is not a valid directory !" << std::endl; + return nullptr; + } + DIR *dir; + dir = opendir(realPath.c_str()); + if (dir == nullptr) { + std::cout << "Can not open dir " << dirName << std::endl; + return nullptr; + } + std::cout << "Successfully opened the dir " << dirName << std::endl; + return dir; +} + +std::string RealPath(std::string_view path) { + char realPathMem[PATH_MAX] = {0}; + char *realPathRet = nullptr; + realPathRet = realpath(path.data(), realPathMem); + + if (realPathRet == nullptr) { + std::cout << "File: " << path << " is not exist."; + return ""; + } + + std::string realPath(realPathMem); + std::cout << path << " realpath is: " << realPath << std::endl; + return realPath; +} diff --git a/model_zoo/official/gnn/bgcf/postprocess.py b/model_zoo/official/gnn/bgcf/postprocess.py new file mode 100644 index 00000000000..e8de0a5350f --- /dev/null +++ b/model_zoo/official/gnn/bgcf/postprocess.py @@ -0,0 +1,75 @@ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# 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. +# ============================================================================ +""" +postprocess. +""" +import os +import argparse +import numpy as np + +from src.metrics import BGCFEvaluate +from src.dataset import load_graph + +parser = argparse.ArgumentParser() +parser.add_argument("--dataset", type=str, default="Beauty", help="choose which dataset") +parser.add_argument("--datapath", type=str, default="./scripts/data_mr", help="minddata path") +parser.add_argument('--input_dim', type=int, default=64, choices=[64, 128], + help="user and item embedding dimension") +parser.add_argument('--Ks', type=list, default=[5, 10, 20, 100], help="top K") +parser.add_argument('--workers', type=int, default=8, help="number of process to generate data") +parser.add_argument("--result_path", type=str, default="./result_Files", help="result path") +args = parser.parse_args() + + +def get_acc(): + """calculate accuracy""" + train_graph, test_graph, _ = load_graph(args.datapath) + + num_user = train_graph.graph_info()["node_num"][0] + num_item = train_graph.graph_info()["node_num"][1] + input_dim = args.input_dim + user_reps = np.zeros([num_user, input_dim * 3]) + item_reps = np.zeros([num_item, input_dim * 3]) + + for i in range(50): + sub_folder = os.path.join(args.result_path, 'result_Files_' + str(i)) + user_rep = np.fromfile(os.path.join(sub_folder, 'amazon-beauty_0.bin'), np.float16) + user_rep = user_rep.reshape(num_user, input_dim * 3) + item_rep = np.fromfile(os.path.join(sub_folder, 'amazon-beauty_1.bin'), np.float16) + item_rep = item_rep.reshape(num_item, input_dim * 3) + + user_reps += user_rep + item_reps += item_rep + user_reps /= 50 + item_reps /= 50 + + eval_class = BGCFEvaluate(args, train_graph, test_graph, args.Ks) + + test_recall_bgcf, test_ndcg_bgcf, \ + test_sedp, test_nov = eval_class.eval_with_rep(user_reps, item_reps, args) + + print('recall_@10:%.5f, recall_@20:%.5f, ndcg_@10:%.5f, ndcg_@20:%.5f, ' + 'sedp_@10:%.5f, sedp_@20:%.5f, nov_@10:%.5f, nov_@20:%.5f\n' % (test_recall_bgcf[1], + test_recall_bgcf[2], + test_ndcg_bgcf[1], + test_ndcg_bgcf[2], + test_sedp[0], + test_sedp[1], + test_nov[1], + test_nov[2])) + + +if __name__ == "__main__": + get_acc() diff --git a/model_zoo/official/gnn/bgcf/preprocess.py b/model_zoo/official/gnn/bgcf/preprocess.py new file mode 100644 index 00000000000..50655ba275e --- /dev/null +++ b/model_zoo/official/gnn/bgcf/preprocess.py @@ -0,0 +1,90 @@ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# 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. +# ============================================================================ +""" +preprocess. +""" +import os +import argparse +import numpy as np + +from mindspore import Tensor +from mindspore.common import dtype as mstype +from src.utils import convert_item_id +from src.dataset import TestGraphDataset, load_graph + +parser = argparse.ArgumentParser() +parser.add_argument("--dataset", type=str, default="Beauty", help="choose which dataset") +parser.add_argument("--datapath", type=str, default="./scripts/data_mr", help="minddata path") +parser.add_argument("--num_neg", type=int, default=10, help="negative sampling rate ") +parser.add_argument("--raw_neighs", type=int, default=40, help="num of sampling neighbors in raw graph") +parser.add_argument("--gnew_neighs", type=int, default=20, help="num of sampling neighbors in sample graph") +parser.add_argument("--result_path", type=str, default="./preprocess_Result/", help="result path") +args = parser.parse_args() + + + +def get_bin(): + """generate bin files.""" + train_graph, _, sampled_graph_list = load_graph(args.datapath) + test_graph_dataset = TestGraphDataset(train_graph, sampled_graph_list, num_samples=args.raw_neighs, + num_bgcn_neigh=args.gnew_neighs, + num_neg=args.num_neg) + + num_user = train_graph.graph_info()["node_num"][0] + num_item = train_graph.graph_info()["node_num"][1] + + for i in range(50): + data_path = os.path.join(args.result_path, "data_" + str(i)) + users_path = os.path.join(data_path, "00_users") + os.makedirs(users_path) + items_path = os.path.join(data_path, "01_items") + os.makedirs(items_path) + neg_items_path = os.path.join(data_path, "02_neg_items") + os.makedirs(neg_items_path) + u_test_neighs_path = os.path.join(data_path, "03_u_test_neighs") + os.makedirs(u_test_neighs_path) + u_test_gnew_neighs_path = os.path.join(data_path, "04_u_test_gnew_neighs") + os.makedirs(u_test_gnew_neighs_path) + i_test_neighs_path = os.path.join(data_path, "05_i_test_neighs") + os.makedirs(i_test_neighs_path) + i_test_gnew_neighs_path = os.path.join(data_path, "06_i_test_gnew_neighs") + os.makedirs(i_test_gnew_neighs_path) + + test_graph_dataset.random_select_sampled_graph() + u_test_neighs, u_test_gnew_neighs = test_graph_dataset.get_user_sapmled_neighbor() + i_test_neighs, i_test_gnew_neighs = test_graph_dataset.get_item_sampled_neighbor() + + u_test_neighs = Tensor(convert_item_id(u_test_neighs, num_user), mstype.int32) + u_test_gnew_neighs = Tensor(convert_item_id(u_test_gnew_neighs, num_user), mstype.int32) + i_test_neighs = Tensor(i_test_neighs, mstype.int32) + i_test_gnew_neighs = Tensor(i_test_gnew_neighs, mstype.int32) + + users = Tensor(np.arange(num_user).reshape(-1,), mstype.int32) + items = Tensor(np.arange(num_item).reshape(-1,), mstype.int32) + neg_items = Tensor(np.arange(num_item).reshape(-1, 1), mstype.int32) + + file_name = 'amazon-beauty.bin' + users.asnumpy().tofile(os.path.join(users_path, file_name)) + items.asnumpy().tofile(os.path.join(items_path, file_name)) + neg_items.asnumpy().tofile(os.path.join(neg_items_path, file_name)) + u_test_neighs.asnumpy().tofile(os.path.join(u_test_neighs_path, file_name)) + u_test_gnew_neighs.asnumpy().tofile(os.path.join(u_test_gnew_neighs_path, file_name)) + i_test_neighs.asnumpy().tofile(os.path.join(i_test_neighs_path, file_name)) + i_test_gnew_neighs.asnumpy().tofile(os.path.join(i_test_gnew_neighs_path, file_name)) + print("=" * 20, "export bin files finished.", "=" * 20) + + +if __name__ == "__main__": + get_bin() diff --git a/model_zoo/official/gnn/bgcf/scripts/run_infer_310.sh b/model_zoo/official/gnn/bgcf/scripts/run_infer_310.sh new file mode 100644 index 00000000000..4dc165b51f1 --- /dev/null +++ b/model_zoo/official/gnn/bgcf/scripts/run_infer_310.sh @@ -0,0 +1,128 @@ +#!/bin/bash +# Copyright 2021 Huawei Technologies Co., Ltd +# +# 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. +# ============================================================================ + +if [[ $# -lt 3 || $# -gt 4 ]]; then + echo "Usage: bash run_infer_310.sh [MINDIR_PATH] [DATASET_NAME] [NEED_PREPROCESS] [DEVICE_ID] + NEED_PREPROCESS means weather need preprocess or not, it's value is 'y' or 'n'. + DEVICE_ID is optional, it can be set by environment variable device_id, otherwise the value is zero" +exit 1 +fi + +get_real_path(){ + if [ "${1:0:1}" == "/" ]; then + echo "$1" + else + echo "$(realpath -m $PWD/$1)" + fi +} +model=$(get_real_path $1) + +dataset_path=$(get_real_path $2) + +if [ "$3" == "y" ] || [ "$3" == "n" ];then + need_preprocess=$3 +else + echo "weather need preprocess or not, it's value must be in [y, n]" + exit 1 +fi + +device_id=0 +if [ $# == 4 ]; then + device_id=$4 +fi + +echo "mindir name: "$model +echo "dataset path: "$dataset_path +echo "need preprocess: "$need_preprocess +echo "device id: "$device_id + +export ASCEND_HOME=/usr/local/Ascend/ +if [ -d ${ASCEND_HOME}/ascend-toolkit ]; then + export PATH=$ASCEND_HOME/fwkacllib/bin:$ASCEND_HOME/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/ascend-toolkit/latest/atc/bin:$PATH + export LD_LIBRARY_PATH=$ASCEND_HOME/fwkacllib/lib64:/usr/local/lib:$ASCEND_HOME/ascend-toolkit/latest/atc/lib64:$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/lib64:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/add-ons:$LD_LIBRARY_PATH + export TBE_IMPL_PATH=$ASCEND_HOME/ascend-toolkit/latest/opp/op_impl/built-in/ai_core/tbe + export PYTHONPATH=$ASCEND_HOME/fwkacllib/python/site-packages:${TBE_IMPL_PATH}:$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/python/site-packages:$PYTHONPATH + export ASCEND_OPP_PATH=$ASCEND_HOME/ascend-toolkit/latest/opp +else + export PATH=$ASCEND_HOME/fwkacllib/bin:$ASCEND_HOME/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/atc/ccec_compiler/bin:$ASCEND_HOME/atc/bin:$PATH + export LD_LIBRARY_PATH=$ASCEND_HOME/fwkacllib/lib64:/usr/local/lib:$ASCEND_HOME/atc/lib64:$ASCEND_HOME/acllib/lib64:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/add-ons:$LD_LIBRARY_PATH + export PYTHONPATH=$ASCEND_HOME/fwkacllib/python/site-packages:$ASCEND_HOME/atc/python/site-packages:$PYTHONPATH + export ASCEND_OPP_PATH=$ASCEND_HOME/opp +fi + +function preprocess_data() +{ + if [ -d preprocess_Result ]; then + rm -rf ./preprocess_Result + fi + mkdir preprocess_Result + python3.7 ../preprocess.py --datapath=$dataset_path --result_path=./preprocess_Result/ +} + +function compile_app() +{ + cd ../ascend310_infer || exit + bash build.sh &> build.log +} + +function infer() +{ + cd - || exit + if [ -d result_Files ]; then + rm -rf ./result_Files + fi + if [ -d time_Result ]; then + rm -rf ./time_Result + fi + rm -rf ./result_Files_all infer.log + mkdir result_Files_all + mkdir time_Result + + for i in {0..49} + do + mkdir result_Files + ../ascend310_infer/out/main --mindir_path=$model --input0_path=./preprocess_Result/data_$i/00_users --input1_path=./preprocess_Result/data_$i/01_items --input2_path=./preprocess_Result/data_$i/02_neg_items --input3_path=./preprocess_Result/data_$i/03_u_test_neighs --input4_path=./preprocess_Result/data_$i/04_u_test_gnew_neighs --input5_path=./preprocess_Result/data_$i/05_i_test_neighs --input6_path=./preprocess_Result/data_$i/06_i_test_gnew_neighs --device_id=$device_id &>> infer.log + mv result_Files result_Files_all/result_Files_$i + done +} + +function cal_acc() +{ + python3.7 ../postprocess.py --result_path=./result_Files_all --datapath=$dataset_path &> acc.log +} + +if [ $need_preprocess == "y" ]; then + preprocess_data + if [ $? -ne 0 ]; then + echo "preprocess dataset failed" + exit 1 + fi +fi +compile_app +if [ $? -ne 0 ]; then + echo "compile app code failed" + exit 1 +fi +infer +if [ $? -ne 0 ]; then + echo " execute inference failed" + exit 1 +fi +cal_acc +if [ $? -ne 0 ]; then + echo "calculate accuracy failed" + exit 1 +fi \ No newline at end of file diff --git a/model_zoo/official/nlp/tinybert/README.md b/model_zoo/official/nlp/tinybert/README.md index 12324da606e..2e38dac7918 100644 --- a/model_zoo/official/nlp/tinybert/README.md +++ b/model_zoo/official/nlp/tinybert/README.md @@ -26,6 +26,10 @@ - [evaluation on SST-2 dataset](#evaluation-on-sst-2-dataset) - [evaluation on MNLI dataset](#evaluation-on-mnli-dataset) - [evaluation on QNLI dataset](#evaluation-on-qnli-dataset) + - [Inference Process](#inference-process) + - [Export MindIR](#export-mindir) + - [Infer on Ascend310](#infer-on-ascend310) + - [result](#result) - [Model Description](#model-description) - [Performance](#performance) - [training Performance](#training-performance) @@ -409,6 +413,39 @@ The best acc is 0.891176 ... ``` +## Inference Process + +### [Export MindIR](#contents) + +```shell +python export.py --ckpt_file [CKPT_PATH] --file_name [FILE_NAME] --file_format [FILE_FORMAT] +``` + +The ckpt_file parameter is required, +`EXPORT_FORMAT` should be in ["AIR", "MINDIR"] + +### Infer on Ascend310 + +Before performing inference, the mindir file must be exported by `export.py` script. We only provide an example of inference using MINDIR model. + +```shell +# Ascend310 inference +bash run_infer_310.sh [MINDIR_PATH] [DATASET_PATH] [SCHEMA_DIR] [DATASET_TYPE] [TASK_NAME] [ASSESSMENT_METHOD] [NEED_PREPROCESS] [DEVICE_ID] +``` + +- `NEED_PREPROCESS` means weather need preprocess or not, it's value is 'y' or 'n'. +- `DEVICE_ID` is optional, default value is 0. + +### result + +Inference result is saved in current path, you can find result like this in acc.log file. + +```bash +================================================================= +============== acc is 0.8862132352941177 +================================================================= +``` + ## [Model Description](#contents) ## [Performance](#contents) diff --git a/model_zoo/official/nlp/tinybert/README_CN.md b/model_zoo/official/nlp/tinybert/README_CN.md index 33b0f5af205..23569c0b79b 100644 --- a/model_zoo/official/nlp/tinybert/README_CN.md +++ b/model_zoo/official/nlp/tinybert/README_CN.md @@ -29,6 +29,10 @@ - [基于SST-2数据集进行评估](#基于sst-2数据集进行评估) - [基于MNLI数据集进行评估](#基于mnli数据集进行评估) - [基于QNLI数据集进行评估](#基于qnli数据集进行评估) + - [推理过程](#推理过程) + - [导出MindIR](#导出mindir) + - [在Ascend310执行推理](#在ascend310执行推理) + - [结果](#结果) - [模型描述](#模型描述) - [性能](#性能) - [评估性能](#评估性能) @@ -410,6 +414,39 @@ The best acc is 0.891176 ... ``` +## 推理过程 + +### [导出MindIR](#contents) + +```shell +python export.py --ckpt_file [CKPT_PATH] --file_name [FILE_NAME] --file_format [FILE_FORMAT] +``` + +参数ckpt_file为必填项, +`EXPORT_FORMAT` 必须在 ["AIR", "MINDIR"]中选择。 + +### 在Ascend310执行推理 + +在执行推理前,mindir文件必须通过`export.py`脚本导出。以下展示了使用minir模型执行推理的示例。 + +```shell +# Ascend310 inference +bash run_infer_310.sh [MINDIR_PATH] [DATASET_PATH] [SCHEMA_DIR] [DATASET_TYPE] [TASK_NAME] [ASSESSMENT_METHOD] [NEED_PREPROCESS] [DEVICE_ID] +``` + +- `NEED_PREPROCESS` 表示数据是否需要预处理,取值范围为 'y' 或者 'n'。 +- `DEVICE_ID` 可选,默认值为0。 + +### 结果 + +推理结果保存在脚本执行的当前路径,你可以在acc.log中看到以下精度计算结果。 + +```bash +================================================================= +============== acc is 0.8862132352941177 +================================================================= +``` + ## 模型描述 ## 性能 diff --git a/model_zoo/official/nlp/tinybert/ascend310_infer/CMakeLists.txt b/model_zoo/official/nlp/tinybert/ascend310_infer/CMakeLists.txt new file mode 100644 index 00000000000..ee3c8544734 --- /dev/null +++ b/model_zoo/official/nlp/tinybert/ascend310_infer/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.14.1) +project(Ascend310Infer) +add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -g -std=c++17 -Werror -Wall -fPIE -Wl,--allow-shlib-undefined") +set(PROJECT_SRC_ROOT ${CMAKE_CURRENT_LIST_DIR}/) +option(MINDSPORE_PATH "mindspore install path" "") +include_directories(${MINDSPORE_PATH}) +include_directories(${MINDSPORE_PATH}/include) +include_directories(${PROJECT_SRC_ROOT}) +find_library(MS_LIB libmindspore.so ${MINDSPORE_PATH}/lib) +file(GLOB_RECURSE MD_LIB ${MINDSPORE_PATH}/_c_dataengine*) + +add_executable(main src/main.cc src/utils.cc) +target_link_libraries(main ${MS_LIB} ${MD_LIB} gflags) diff --git a/model_zoo/official/nlp/tinybert/ascend310_infer/build.sh b/model_zoo/official/nlp/tinybert/ascend310_infer/build.sh new file mode 100644 index 00000000000..285514e19f2 --- /dev/null +++ b/model_zoo/official/nlp/tinybert/ascend310_infer/build.sh @@ -0,0 +1,29 @@ +#!/bin/bash +# Copyright 2021 Huawei Technologies Co., Ltd +# +# 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. +# ============================================================================ +if [ -d out ]; then + rm -rf out +fi + +mkdir out +cd out || exit + +if [ -f "Makefile" ]; then + make clean +fi + +cmake .. \ + -DMINDSPORE_PATH="`pip3.7 show mindspore-ascend | grep Location | awk '{print $2"/mindspore"}' | xargs realpath`" +make diff --git a/model_zoo/official/nlp/tinybert/ascend310_infer/inc/utils.h b/model_zoo/official/nlp/tinybert/ascend310_infer/inc/utils.h new file mode 100644 index 00000000000..efebe03a8c1 --- /dev/null +++ b/model_zoo/official/nlp/tinybert/ascend310_infer/inc/utils.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_INFERENCE_UTILS_H_ +#define MINDSPORE_INFERENCE_UTILS_H_ + +#include +#include +#include +#include +#include +#include "include/api/types.h" + +std::vector GetAllFiles(std::string_view dirName); +DIR *OpenDir(std::string_view dirName); +std::string RealPath(std::string_view path); +mindspore::MSTensor ReadFileToTensor(const std::string &file); +int WriteResult(const std::string& imageFile, const std::vector &outputs); +#endif diff --git a/model_zoo/official/nlp/tinybert/ascend310_infer/src/main.cc b/model_zoo/official/nlp/tinybert/ascend310_infer/src/main.cc new file mode 100644 index 00000000000..ca105f8aab1 --- /dev/null +++ b/model_zoo/official/nlp/tinybert/ascend310_infer/src/main.cc @@ -0,0 +1,142 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "include/api/model.h" +#include "include/api/context.h" +#include "include/api/types.h" +#include "include/api/serialization.h" +#include "include/dataset/execute.h" +#include "include/dataset/vision.h" +#include "inc/utils.h" + +using mindspore::Context; +using mindspore::Serialization; +using mindspore::Model; +using mindspore::Status; +using mindspore::MSTensor; +using mindspore::dataset::Execute; +using mindspore::ModelType; +using mindspore::GraphCell; +using mindspore::kSuccess; + +DEFINE_string(mindir_path, "", "mindir path"); +DEFINE_string(input0_path, ".", "input0 path"); +DEFINE_string(input1_path, ".", "input1 path"); +DEFINE_string(input2_path, ".", "input2 path"); +DEFINE_int32(device_id, 0, "device id"); + +int main(int argc, char **argv) { + gflags::ParseCommandLineFlags(&argc, &argv, true); + if (RealPath(FLAGS_mindir_path).empty()) { + std::cout << "Invalid mindir" << std::endl; + return 1; + } + + auto context = std::make_shared(); + auto ascend310 = std::make_shared(); + ascend310->SetDeviceID(FLAGS_device_id); + ascend310->SetPrecisionMode("allow_fp32_to_fp16"); + ascend310->SetOpSelectImplMode("high_precision"); + context->MutableDeviceInfo().push_back(ascend310); + mindspore::Graph graph; + Serialization::Load(FLAGS_mindir_path, ModelType::kMindIR, &graph); + + Model model; + Status ret = model.Build(GraphCell(graph), context); + if (ret != kSuccess) { + std::cout << "ERROR: Build failed." << std::endl; + return 1; + } + + std::vector model_inputs = model.GetInputs(); + if (model_inputs.empty()) { + std::cout << "Invalid model, inputs is empty." << std::endl; + return 1; + } + + auto input0_files = GetAllFiles(FLAGS_input0_path); + auto input1_files = GetAllFiles(FLAGS_input1_path); + auto input2_files = GetAllFiles(FLAGS_input2_path); + + if (input0_files.empty() || input1_files.empty() || input2_files.empty()) { + std::cout << "ERROR: input data empty." << std::endl; + return 1; + } + + std::map costTime_map; + size_t size = input0_files.size(); + + for (size_t i = 0; i < size; ++i) { + struct timeval start = {0}; + struct timeval end = {0}; + double startTimeMs; + double endTimeMs; + std::vector inputs; + std::vector outputs; + std::cout << "Start predict input files:" << input0_files[i] << std::endl; + + auto input0 = ReadFileToTensor(input0_files[i]); + auto input1 = ReadFileToTensor(input1_files[i]); + auto input2 = ReadFileToTensor(input2_files[i]); + inputs.emplace_back(model_inputs[0].Name(), model_inputs[0].DataType(), model_inputs[0].Shape(), + input0.Data().get(), input0.DataSize()); + inputs.emplace_back(model_inputs[1].Name(), model_inputs[1].DataType(), model_inputs[1].Shape(), + input1.Data().get(), input1.DataSize()); + inputs.emplace_back(model_inputs[2].Name(), model_inputs[2].DataType(), model_inputs[2].Shape(), + input2.Data().get(), input2.DataSize()); + + gettimeofday(&start, nullptr); + ret = model.Predict(inputs, &outputs); + gettimeofday(&end, nullptr); + if (ret != kSuccess) { + std::cout << "Predict " << input0_files[i] << " failed." << std::endl; + return 1; + } + startTimeMs = (1.0 * start.tv_sec * 1000000 + start.tv_usec) / 1000; + endTimeMs = (1.0 * end.tv_sec * 1000000 + end.tv_usec) / 1000; + costTime_map.insert(std::pair(startTimeMs, endTimeMs)); + WriteResult(input0_files[i], outputs); + } + double average = 0.0; + int inferCount = 0; + + for (auto iter = costTime_map.begin(); iter != costTime_map.end(); iter++) { + double diff = 0.0; + diff = iter->second - iter->first; + average += diff; + inferCount++; + } + average = average / inferCount; + std::stringstream timeCost; + timeCost << "NN inference cost average time: "<< average << " ms of infer_count " << inferCount << std::endl; + std::cout << "NN inference cost average time: "<< average << "ms of infer_count " << inferCount << std::endl; + std::string fileName = "./time_Result" + std::string("/test_perform_static.txt"); + std::ofstream fileStream(fileName.c_str(), std::ios::trunc); + fileStream << timeCost.str(); + fileStream.close(); + costTime_map.clear(); + return 0; +} diff --git a/model_zoo/official/nlp/tinybert/ascend310_infer/src/utils.cc b/model_zoo/official/nlp/tinybert/ascend310_infer/src/utils.cc new file mode 100644 index 00000000000..c947e4d5f45 --- /dev/null +++ b/model_zoo/official/nlp/tinybert/ascend310_infer/src/utils.cc @@ -0,0 +1,129 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#include +#include +#include +#include "inc/utils.h" + +using mindspore::MSTensor; +using mindspore::DataType; + +std::vector GetAllFiles(std::string_view dirName) { + struct dirent *filename; + DIR *dir = OpenDir(dirName); + if (dir == nullptr) { + return {}; + } + std::vector res; + while ((filename = readdir(dir)) != nullptr) { + std::string dName = std::string(filename->d_name); + if (dName == "." || dName == ".." || filename->d_type != DT_REG) { + continue; + } + res.emplace_back(std::string(dirName) + "/" + filename->d_name); + } + std::sort(res.begin(), res.end()); + for (auto &f : res) { + std::cout << "image file: " << f << std::endl; + } + return res; +} + +int WriteResult(const std::string& imageFile, const std::vector &outputs) { + std::string homePath = "./result_Files"; + for (size_t i = 0; i < outputs.size(); ++i) { + size_t outputSize; + std::shared_ptr netOutput; + netOutput = outputs[i].Data(); + outputSize = outputs[i].DataSize(); + int pos = imageFile.rfind('/'); + std::string fileName(imageFile, pos + 1); + fileName.replace(fileName.find('.'), fileName.size() - fileName.find('.'), '_' + std::to_string(i) + ".bin"); + std::string outFileName = homePath + "/" + fileName; + FILE * outputFile = fopen(outFileName.c_str(), "wb"); + fwrite(netOutput.get(), outputSize, sizeof(char), outputFile); + fclose(outputFile); + outputFile = nullptr; + } + return 0; +} + +mindspore::MSTensor ReadFileToTensor(const std::string &file) { + if (file.empty()) { + std::cout << "Pointer file is nullptr" << std::endl; + return mindspore::MSTensor(); + } + + std::ifstream ifs(file); + if (!ifs.good()) { + std::cout << "File: " << file << " is not exist" << std::endl; + return mindspore::MSTensor(); + } + + if (!ifs.is_open()) { + std::cout << "File: " << file << "open failed" << std::endl; + return mindspore::MSTensor(); + } + + ifs.seekg(0, std::ios::end); + size_t size = ifs.tellg(); + mindspore::MSTensor buffer(file, mindspore::DataType::kNumberTypeUInt8, {static_cast(size)}, nullptr, size); + + ifs.seekg(0, std::ios::beg); + ifs.read(reinterpret_cast(buffer.MutableData()), size); + ifs.close(); + + return buffer; +} + + +DIR *OpenDir(std::string_view dirName) { + if (dirName.empty()) { + std::cout << " dirName is null ! " << std::endl; + return nullptr; + } + std::string realPath = RealPath(dirName); + struct stat s; + lstat(realPath.c_str(), &s); + if (!S_ISDIR(s.st_mode)) { + std::cout << "dirName is not a valid directory !" << std::endl; + return nullptr; + } + DIR *dir; + dir = opendir(realPath.c_str()); + if (dir == nullptr) { + std::cout << "Can not open dir " << dirName << std::endl; + return nullptr; + } + std::cout << "Successfully opened the dir " << dirName << std::endl; + return dir; +} + +std::string RealPath(std::string_view path) { + char realPathMem[PATH_MAX] = {0}; + char *realPathRet = nullptr; + realPathRet = realpath(path.data(), realPathMem); + + if (realPathRet == nullptr) { + std::cout << "File: " << path << " is not exist."; + return ""; + } + + std::string realPath(realPathMem); + std::cout << path << " realpath is: " << realPath << std::endl; + return realPath; +} diff --git a/model_zoo/official/nlp/tinybert/postprocess.py b/model_zoo/official/nlp/tinybert/postprocess.py new file mode 100644 index 00000000000..769609bbbd9 --- /dev/null +++ b/model_zoo/official/nlp/tinybert/postprocess.py @@ -0,0 +1,103 @@ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# 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. +# ============================================================================ + +"""postprocess""" + +import os +import argparse +import numpy as np +from mindspore import Tensor +from src.assessment_method import Accuracy, F1 +from src.td_config import eval_cfg + +parser = argparse.ArgumentParser(description='postprocess') +parser.add_argument("--task_name", type=str, default="", choices=["SST-2", "QNLI", "MNLI", "TNEWS", "CLUENER"], + help="The name of the task to train.") +parser.add_argument("--assessment_method", type=str, default="accuracy", choices=["accuracy", "bf1", "mf1"], + help="assessment_method include: [accuracy, bf1, mf1], default is accuracy") +parser.add_argument("--result_path", type=str, default="./result_Files", help="result path") +parser.add_argument("--label_path", type=str, default="./preprocess_Result/label_ids.npy", help="label path") +args_opt = parser.parse_args() + + +DEFAULT_NUM_LABELS = 2 +DEFAULT_SEQ_LENGTH = 128 +task_params = {"SST-2": {"num_labels": 2, "seq_length": 64}, + "QNLI": {"num_labels": 2, "seq_length": 128}, + "MNLI": {"num_labels": 3, "seq_length": 128}, + "TNEWS": {"num_labels": 15, "seq_length": 128}, + "CLUENER": {"num_labels": 43, "seq_length": 128}} + + +class Task: + """ + Encapsulation class of get the task parameter. + """ + def __init__(self, task_name): + self.task_name = task_name + + @property + def num_labels(self): + if self.task_name in task_params and "num_labels" in task_params[self.task_name]: + return task_params[self.task_name]["num_labels"] + return DEFAULT_NUM_LABELS + + @property + def seq_length(self): + if self.task_name in task_params and "seq_length" in task_params[self.task_name]: + return task_params[self.task_name]["seq_length"] + return DEFAULT_SEQ_LENGTH +task = Task(args_opt.task_name) + +def eval_result_print(assessment_method="accuracy", callback=None): + """print eval result""" + if assessment_method == "accuracy": + print("============== acc is {}".format(callback.acc_num / callback.total_num)) + elif assessment_method == "bf1": + print("Precision {:.6f} ".format(callback.TP / (callback.TP + callback.FP))) + print("Recall {:.6f} ".format(callback.TP / (callback.TP + callback.FN))) + print("F1 {:.6f} ".format(2 * callback.TP / (2 * callback.TP + callback.FP + callback.FN))) + elif assessment_method == "mf1": + print("F1 {:.6f} ".format(callback.eval())) + else: + raise ValueError("Assessment method not supported, support: [accuracy, f1]") + +def get_acc(): + """ + calculate accuracy + """ + if args_opt.assessment_method == "accuracy": + callback = Accuracy() + elif args_opt.assessment_method == "bf1": + callback = F1(num_labels=task.num_labels) + elif args_opt.assessment_method == "mf1": + callback = F1(num_labels=task.num_labels, mode="MultiLabel") + else: + raise ValueError("Assessment method not supported, support: [accuracy, f1]") + labels = np.load(args_opt.label_path) + file_num = len(os.listdir(args_opt.result_path)) + for i in range(file_num): + f_name = "tinybert_bs" + str(eval_cfg.batch_size) + "_" + str(i) + "_0.bin" + logits = np.fromfile(os.path.join(args_opt.result_path, f_name), np.float32) + logits = logits.reshape(eval_cfg.batch_size, task.num_labels) + label_ids = labels[i] + callback.update(Tensor(logits), Tensor(label_ids)) + print("==============================================================") + eval_result_print(args_opt.assessment_method, callback) + print("==============================================================") + + +if __name__ == '__main__': + get_acc() diff --git a/model_zoo/official/nlp/tinybert/preprocess.py b/model_zoo/official/nlp/tinybert/preprocess.py new file mode 100644 index 00000000000..9c5857e9dbe --- /dev/null +++ b/model_zoo/official/nlp/tinybert/preprocess.py @@ -0,0 +1,75 @@ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# 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. +# ============================================================================ + +"""preprocess""" + +import os +import argparse +import numpy as np +from src.td_config import eval_cfg +from src.dataset import create_tinybert_dataset, DataType + + +parser = argparse.ArgumentParser(description='preprocess') +parser.add_argument("--eval_data_dir", type=str, default="", help="Data path, it is better to use absolute path") +parser.add_argument("--schema_dir", type=str, default="", help="Schema path, it is better to use absolute path") +parser.add_argument("--dataset_type", type=str, default="tfrecord", + help="dataset type tfrecord/mindrecord, default is tfrecord") +parser.add_argument("--result_path", type=str, default="./preprocess_Result/", help="result path") +args_opt = parser.parse_args() + +if args_opt.dataset_type == "tfrecord": + dataset_type = DataType.TFRECORD +elif args_opt.dataset_type == "mindrecord": + dataset_type = DataType.MINDRECORD +else: + raise Exception("dataset format is not supported yet") + +def get_bin(): + """ + generate bin files. + """ + input_ids_path = os.path.join(args_opt.result_path, "00_input_ids") + token_type_id_path = os.path.join(args_opt.result_path, "01_token_type_id") + input_mask_path = os.path.join(args_opt.result_path, "02_input_mask") + label_ids_path = os.path.join(args_opt.result_path, "label_ids.npy") + + os.makedirs(input_ids_path) + os.makedirs(token_type_id_path) + os.makedirs(input_mask_path) + + eval_dataset = create_tinybert_dataset('td', batch_size=eval_cfg.batch_size, + device_num=1, rank=0, do_shuffle="false", + data_dir=args_opt.eval_data_dir, + schema_dir=args_opt.schema_dir, + data_type=dataset_type) + columns_list = ["input_ids", "input_mask", "segment_ids", "label_ids"] + label_list = [] + for j, data in enumerate(eval_dataset.create_dict_iterator(output_numpy=True, num_epochs=1)): + file_name = "tinybert_bs" + str(eval_cfg.batch_size) + "_" + str(j) + ".bin" + input_data = [] + for i in columns_list: + input_data.append(data[i]) + input_ids, input_mask, token_type_id, label_ids = input_data + input_ids.tofile(os.path.join(input_ids_path, file_name)) + input_mask.tofile(os.path.join(input_mask_path, file_name)) + token_type_id.tofile(os.path.join(token_type_id_path, file_name)) + label_list.append(label_ids) + np.save(label_ids_path, label_list) + print("=" * 20, 'export files finished', "=" * 20) + + +if __name__ == '__main__': + get_bin() diff --git a/model_zoo/official/nlp/tinybert/scripts/run_infer_310.sh b/model_zoo/official/nlp/tinybert/scripts/run_infer_310.sh new file mode 100644 index 00000000000..ad0d3c1e86d --- /dev/null +++ b/model_zoo/official/nlp/tinybert/scripts/run_infer_310.sh @@ -0,0 +1,131 @@ +#!/bin/bash +# Copyright 2021 Huawei Technologies Co., Ltd +# +# 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. +# ============================================================================ + +if [[ $# -lt 7 || $# -gt 8 ]]; then + echo "Usage: bash run_infer_310.sh [MINDIR_PATH] [DATASET_PATH] [SCHEMA_DIR] [DATASET_TYPE] [TASK_NAME] [ASSESSMENT_METHOD] [NEED_PREPROCESS] [DEVICE_ID] + NEED_PREPROCESS means weather need preprocess or not, it's value is 'y' or 'n'. + DEVICE_ID is optional, it can be set by environment variable device_id, otherwise the value is zero" +exit 1 +fi + +get_real_path(){ + if [ "${1:0:1}" == "/" ]; then + echo "$1" + else + echo "$(realpath -m $PWD/$1)" + fi +} +model=$(get_real_path $1) +dataset_path=$(get_real_path $2) +schema_dir=$(get_real_path $3) +dataset_type=$4 +task_name=$5 +assessment_method=$6 + + +if [ "$7" == "y" ] || [ "$7" == "n" ];then + need_preprocess=$7 +else + echo "weather need preprocess or not, it's value must be in [y, n]" + exit 1 +fi + +device_id=0 +if [ $# == 8 ]; then + device_id=$8 +fi + +echo "mindir name: "$model +echo "dataset path: "$dataset_path +echo "schema dir: "$schema_dir +echo "dataset_type: "$dataset_type +echo "task_name: "$task_name +echo "assessment_method: "$assessment_method +echo "need preprocess: "$need_preprocess +echo "device id: "$device_id + +export ASCEND_HOME=/usr/local/Ascend/ +if [ -d ${ASCEND_HOME}/ascend-toolkit ]; then + export PATH=$ASCEND_HOME/fwkacllib/bin:$ASCEND_HOME/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/ascend-toolkit/latest/atc/bin:$PATH + export LD_LIBRARY_PATH=$ASCEND_HOME/fwkacllib/lib64:/usr/local/lib:$ASCEND_HOME/ascend-toolkit/latest/atc/lib64:$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/lib64:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/add-ons:$LD_LIBRARY_PATH + export TBE_IMPL_PATH=$ASCEND_HOME/ascend-toolkit/latest/opp/op_impl/built-in/ai_core/tbe + export PYTHONPATH=$ASCEND_HOME/fwkacllib/python/site-packages:${TBE_IMPL_PATH}:$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/python/site-packages:$PYTHONPATH + export ASCEND_OPP_PATH=$ASCEND_HOME/ascend-toolkit/latest/opp +else + export PATH=$ASCEND_HOME/fwkacllib/bin:$ASCEND_HOME/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/atc/ccec_compiler/bin:$ASCEND_HOME/atc/bin:$PATH + export LD_LIBRARY_PATH=$ASCEND_HOME/fwkacllib/lib64:/usr/local/lib:$ASCEND_HOME/atc/lib64:$ASCEND_HOME/acllib/lib64:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/add-ons:$LD_LIBRARY_PATH + export PYTHONPATH=$ASCEND_HOME/fwkacllib/python/site-packages:$ASCEND_HOME/atc/python/site-packages:$PYTHONPATH + export ASCEND_OPP_PATH=$ASCEND_HOME/opp +fi + +function preprocess_data() +{ + if [ -d preprocess_Result ]; then + rm -rf ./preprocess_Result + fi + mkdir preprocess_Result + python3.7 ../preprocess.py --eval_data_dir=$dataset_path --schema_dir=$schema_dir --dataset_type=$dataset_type --result_path=./preprocess_Result/ +} + +function compile_app() +{ + cd ../ascend310_infer || exit + bash build.sh &> build.log +} + +function infer() +{ + cd - || exit + if [ -d result_Files ]; then + rm -rf ./result_Files + fi + if [ -d time_Result ]; then + rm -rf ./time_Result + fi + mkdir result_Files + mkdir time_Result + + ../ascend310_infer/out/main --mindir_path=$model --input0_path=./preprocess_Result/00_input_ids --input1_path=./preprocess_Result/01_token_type_id --input2_path=./preprocess_Result/02_input_mask --device_id=$device_id &> infer.log + +} + +function cal_acc() +{ + python3.7 ../postprocess.py --task_name=$task_name --assessment_method=$assessment_method --result_path=./result_Files --label_path=./preprocess_Result/label_ids.npy &> acc.log +} + +if [ $need_preprocess == "y" ]; then + preprocess_data + if [ $? -ne 0 ]; then + echo "preprocess dataset failed" + exit 1 + fi +fi +compile_app +if [ $? -ne 0 ]; then + echo "compile app code failed" + exit 1 +fi +infer +if [ $? -ne 0 ]; then + echo " execute inference failed" + exit 1 +fi +cal_acc +if [ $? -ne 0 ]; then + echo "calculate accuracy failed" + exit 1 +fi \ No newline at end of file diff --git a/model_zoo/official/recommend/ncf/README.md b/model_zoo/official/recommend/ncf/README.md index 80def6f4362..8da8c6e2b76 100644 --- a/model_zoo/official/recommend/ncf/README.md +++ b/model_zoo/official/recommend/ncf/README.md @@ -15,6 +15,10 @@ - [Distributed Training](#distributed-training) - [Evaluation Process](#evaluation-process) - [Evaluation](#evaluation) + - [Inference Process](#inference-process) + - [Export MindIR](#export-mindir) + - [Infer on Ascend310](#infer-on-ascend310) + - [result](#result) - [Model Description](#model-description) - [Performance](#performance) - [Evaluation Performance](#evaluation-performance) @@ -220,6 +224,37 @@ Parameters for both training and evaluation can be set in config.py. HR:0.6846,NDCG:0.410 ``` +## Inference Process + +### [Export MindIR](#contents) + +```shell +python export.py --ckpt_file [CKPT_PATH] --file_name [FILE_NAME] --file_format [FILE_FORMAT] +``` + +The ckpt_file parameter is required, +`EXPORT_FORMAT` should be in ["AIR", "MINDIR"] + +### Infer on Ascend310 + +Before performing inference, the mindir file must be exported by `export.py` script. We only provide an example of inference using MINDIR model. + +```shell +# Ascend310 inference +bash run_infer_310.sh [MINDIR_PATH] [NEED_PREPROCESS] [DEVICE_ID] +``` + +- `NEED_PREPROCESS` means weather need preprocess or not, it's value is 'y' or 'n'. +- `DEVICE_ID` is optional, default value is 0. + +### result + +Inference result is saved in current path, you can find result like this in acc.log file. + +```grep "accuracy: " acc.log + HR:0.6846,NDCG:0.410 +``` + # [Model Description](#contents) ## [Performance](#contents) diff --git a/model_zoo/official/recommend/ncf/ascend310_infer/CMakeLists.txt b/model_zoo/official/recommend/ncf/ascend310_infer/CMakeLists.txt new file mode 100644 index 00000000000..170e6c5275e --- /dev/null +++ b/model_zoo/official/recommend/ncf/ascend310_infer/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.14.1) +project(Ascend310Infer) +add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -g -std=c++17 -Werror -Wall -fPIE -Wl,--allow-shlib-undefined") +set(PROJECT_SRC_ROOT ${CMAKE_CURRENT_LIST_DIR}/) +option(MINDSPORE_PATH "mindspore install path" "") +include_directories(${MINDSPORE_PATH}) +include_directories(${MINDSPORE_PATH}/include) +include_directories(${PROJECT_SRC_ROOT}) +find_library(MS_LIB libmindspore.so ${MINDSPORE_PATH}/lib) +file(GLOB_RECURSE MD_LIB ${MINDSPORE_PATH}/_c_dataengine*) + +add_executable(main src/main.cc src/utils.cc) +target_link_libraries(main ${MS_LIB} ${MD_LIB} gflags) diff --git a/model_zoo/official/recommend/ncf/ascend310_infer/build.sh b/model_zoo/official/recommend/ncf/ascend310_infer/build.sh new file mode 100644 index 00000000000..285514e19f2 --- /dev/null +++ b/model_zoo/official/recommend/ncf/ascend310_infer/build.sh @@ -0,0 +1,29 @@ +#!/bin/bash +# Copyright 2021 Huawei Technologies Co., Ltd +# +# 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. +# ============================================================================ +if [ -d out ]; then + rm -rf out +fi + +mkdir out +cd out || exit + +if [ -f "Makefile" ]; then + make clean +fi + +cmake .. \ + -DMINDSPORE_PATH="`pip3.7 show mindspore-ascend | grep Location | awk '{print $2"/mindspore"}' | xargs realpath`" +make diff --git a/model_zoo/official/recommend/ncf/ascend310_infer/inc/utils.h b/model_zoo/official/recommend/ncf/ascend310_infer/inc/utils.h new file mode 100644 index 00000000000..efebe03a8c1 --- /dev/null +++ b/model_zoo/official/recommend/ncf/ascend310_infer/inc/utils.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_INFERENCE_UTILS_H_ +#define MINDSPORE_INFERENCE_UTILS_H_ + +#include +#include +#include +#include +#include +#include "include/api/types.h" + +std::vector GetAllFiles(std::string_view dirName); +DIR *OpenDir(std::string_view dirName); +std::string RealPath(std::string_view path); +mindspore::MSTensor ReadFileToTensor(const std::string &file); +int WriteResult(const std::string& imageFile, const std::vector &outputs); +#endif diff --git a/model_zoo/official/recommend/ncf/ascend310_infer/src/main.cc b/model_zoo/official/recommend/ncf/ascend310_infer/src/main.cc new file mode 100644 index 00000000000..3879ed4d9b9 --- /dev/null +++ b/model_zoo/official/recommend/ncf/ascend310_infer/src/main.cc @@ -0,0 +1,140 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "include/api/model.h" +#include "include/api/context.h" +#include "include/api/types.h" +#include "include/api/serialization.h" +#include "include/dataset/execute.h" +#include "include/dataset/vision.h" +#include "inc/utils.h" + +using mindspore::Context; +using mindspore::Serialization; +using mindspore::Model; +using mindspore::Status; +using mindspore::MSTensor; +using mindspore::dataset::Execute; +using mindspore::ModelType; +using mindspore::GraphCell; +using mindspore::kSuccess; + +DEFINE_string(mindir_path, "", "mindir path"); +DEFINE_string(input0_path, ".", "input0 path"); +DEFINE_string(input1_path, ".", "input1 path"); +DEFINE_string(input2_path, ".", "input2 path"); +DEFINE_int32(device_id, 0, "device id"); + +int main(int argc, char **argv) { + gflags::ParseCommandLineFlags(&argc, &argv, true); + if (RealPath(FLAGS_mindir_path).empty()) { + std::cout << "Invalid mindir" << std::endl; + return 1; + } + + auto context = std::make_shared(); + auto ascend310 = std::make_shared(); + ascend310->SetDeviceID(FLAGS_device_id); + context->MutableDeviceInfo().push_back(ascend310); + mindspore::Graph graph; + Serialization::Load(FLAGS_mindir_path, ModelType::kMindIR, &graph); + + Model model; + Status ret = model.Build(GraphCell(graph), context); + if (ret != kSuccess) { + std::cout << "ERROR: Build failed." << std::endl; + return 1; + } + + std::vector model_inputs = model.GetInputs(); + if (model_inputs.empty()) { + std::cout << "Invalid model, inputs is empty." << std::endl; + return 1; + } + + auto input0_files = GetAllFiles(FLAGS_input0_path); + auto input1_files = GetAllFiles(FLAGS_input1_path); + auto input2_files = GetAllFiles(FLAGS_input2_path); + + if (input0_files.empty() || input1_files.empty() || input2_files.empty()) { + std::cout << "ERROR: input data empty." << std::endl; + return 1; + } + + std::map costTime_map; + size_t size = input0_files.size(); + + for (size_t i = 0; i < size; ++i) { + struct timeval start = {0}; + struct timeval end = {0}; + double startTimeMs; + double endTimeMs; + std::vector inputs; + std::vector outputs; + std::cout << "Start predict input files:" << input0_files[i] << std::endl; + + auto input0 = ReadFileToTensor(input0_files[i]); + auto input1 = ReadFileToTensor(input1_files[i]); + auto input2 = ReadFileToTensor(input2_files[i]); + inputs.emplace_back(model_inputs[0].Name(), model_inputs[0].DataType(), model_inputs[0].Shape(), + input0.Data().get(), input0.DataSize()); + inputs.emplace_back(model_inputs[1].Name(), model_inputs[1].DataType(), model_inputs[1].Shape(), + input1.Data().get(), input1.DataSize()); + inputs.emplace_back(model_inputs[2].Name(), model_inputs[2].DataType(), model_inputs[2].Shape(), + input2.Data().get(), input2.DataSize()); + + gettimeofday(&start, nullptr); + ret = model.Predict(inputs, &outputs); + gettimeofday(&end, nullptr); + if (ret != kSuccess) { + std::cout << "Predict " << input0_files[i] << " failed." << std::endl; + return 1; + } + startTimeMs = (1.0 * start.tv_sec * 1000000 + start.tv_usec) / 1000; + endTimeMs = (1.0 * end.tv_sec * 1000000 + end.tv_usec) / 1000; + costTime_map.insert(std::pair(startTimeMs, endTimeMs)); + WriteResult(input0_files[i], outputs); + } + double average = 0.0; + int inferCount = 0; + + for (auto iter = costTime_map.begin(); iter != costTime_map.end(); iter++) { + double diff = 0.0; + diff = iter->second - iter->first; + average += diff; + inferCount++; + } + average = average / inferCount; + std::stringstream timeCost; + timeCost << "NN inference cost average time: "<< average << " ms of infer_count " << inferCount << std::endl; + std::cout << "NN inference cost average time: "<< average << "ms of infer_count " << inferCount << std::endl; + std::string fileName = "./time_Result" + std::string("/test_perform_static.txt"); + std::ofstream fileStream(fileName.c_str(), std::ios::trunc); + fileStream << timeCost.str(); + fileStream.close(); + costTime_map.clear(); + return 0; +} diff --git a/model_zoo/official/recommend/ncf/ascend310_infer/src/utils.cc b/model_zoo/official/recommend/ncf/ascend310_infer/src/utils.cc new file mode 100644 index 00000000000..c947e4d5f45 --- /dev/null +++ b/model_zoo/official/recommend/ncf/ascend310_infer/src/utils.cc @@ -0,0 +1,129 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#include +#include +#include +#include "inc/utils.h" + +using mindspore::MSTensor; +using mindspore::DataType; + +std::vector GetAllFiles(std::string_view dirName) { + struct dirent *filename; + DIR *dir = OpenDir(dirName); + if (dir == nullptr) { + return {}; + } + std::vector res; + while ((filename = readdir(dir)) != nullptr) { + std::string dName = std::string(filename->d_name); + if (dName == "." || dName == ".." || filename->d_type != DT_REG) { + continue; + } + res.emplace_back(std::string(dirName) + "/" + filename->d_name); + } + std::sort(res.begin(), res.end()); + for (auto &f : res) { + std::cout << "image file: " << f << std::endl; + } + return res; +} + +int WriteResult(const std::string& imageFile, const std::vector &outputs) { + std::string homePath = "./result_Files"; + for (size_t i = 0; i < outputs.size(); ++i) { + size_t outputSize; + std::shared_ptr netOutput; + netOutput = outputs[i].Data(); + outputSize = outputs[i].DataSize(); + int pos = imageFile.rfind('/'); + std::string fileName(imageFile, pos + 1); + fileName.replace(fileName.find('.'), fileName.size() - fileName.find('.'), '_' + std::to_string(i) + ".bin"); + std::string outFileName = homePath + "/" + fileName; + FILE * outputFile = fopen(outFileName.c_str(), "wb"); + fwrite(netOutput.get(), outputSize, sizeof(char), outputFile); + fclose(outputFile); + outputFile = nullptr; + } + return 0; +} + +mindspore::MSTensor ReadFileToTensor(const std::string &file) { + if (file.empty()) { + std::cout << "Pointer file is nullptr" << std::endl; + return mindspore::MSTensor(); + } + + std::ifstream ifs(file); + if (!ifs.good()) { + std::cout << "File: " << file << " is not exist" << std::endl; + return mindspore::MSTensor(); + } + + if (!ifs.is_open()) { + std::cout << "File: " << file << "open failed" << std::endl; + return mindspore::MSTensor(); + } + + ifs.seekg(0, std::ios::end); + size_t size = ifs.tellg(); + mindspore::MSTensor buffer(file, mindspore::DataType::kNumberTypeUInt8, {static_cast(size)}, nullptr, size); + + ifs.seekg(0, std::ios::beg); + ifs.read(reinterpret_cast(buffer.MutableData()), size); + ifs.close(); + + return buffer; +} + + +DIR *OpenDir(std::string_view dirName) { + if (dirName.empty()) { + std::cout << " dirName is null ! " << std::endl; + return nullptr; + } + std::string realPath = RealPath(dirName); + struct stat s; + lstat(realPath.c_str(), &s); + if (!S_ISDIR(s.st_mode)) { + std::cout << "dirName is not a valid directory !" << std::endl; + return nullptr; + } + DIR *dir; + dir = opendir(realPath.c_str()); + if (dir == nullptr) { + std::cout << "Can not open dir " << dirName << std::endl; + return nullptr; + } + std::cout << "Successfully opened the dir " << dirName << std::endl; + return dir; +} + +std::string RealPath(std::string_view path) { + char realPathMem[PATH_MAX] = {0}; + char *realPathRet = nullptr; + realPathRet = realpath(path.data(), realPathMem); + + if (realPathRet == nullptr) { + std::cout << "File: " << path << " is not exist."; + return ""; + } + + std::string realPath(realPathMem); + std::cout << path << " realpath is: " << realPath << std::endl; + return realPath; +} diff --git a/model_zoo/official/recommend/ncf/default_config.yaml b/model_zoo/official/recommend/ncf/default_config.yaml index 84dcc0ede61..d8e27b50bed 100644 --- a/model_zoo/official/recommend/ncf/default_config.yaml +++ b/model_zoo/official/recommend/ncf/default_config.yaml @@ -28,9 +28,15 @@ checkpoint_file_path: "./checkpoint/NCF-14_19418.ckpt" # Export options device_id: 0 -ckpt_file: "" -file_name: "" -file_format: "" +ckpt_file: "./checkpoint/NCF-25_19418.ckpt" +file_name: "ncf" +file_format: "MINDIR" + +# Preprocess options +pre_result_path: "./preprocess_Result" + +# Postprocess options +post_result_path: "./result_Files" --- @@ -51,4 +57,6 @@ layers: "The sizes of hidden layers for MLP" num_factors: "The Embedding size of MF model." checkpoint_path: "The location of the checkpoint file." eval_file_name: "Eval output file." -checkpoint_file_path: "The location of the checkpoint file." \ No newline at end of file +checkpoint_file_path: "The location of the checkpoint file." +pre_result_path: "saving dataset to numpy format path." +post_result_path: "inference result path." diff --git a/model_zoo/official/recommend/ncf/postprocess.py b/model_zoo/official/recommend/ncf/postprocess.py new file mode 100644 index 00000000000..ea3a0b59325 --- /dev/null +++ b/model_zoo/official/recommend/ncf/postprocess.py @@ -0,0 +1,52 @@ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# 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. +# ============================================================================ +"""postprocess""" +import os +import numpy as np + +from mindspore import Tensor +from src.metrics import NCFMetric +from model_utils.config import config + +def get_acc(): + """calculate accuracy""" + if not os.path.exists(config.output_path): + os.makedirs(config.output_path) + + ncf_metric = NCFMetric() + rst_path = config.post_result_path + file_num = len(os.listdir(rst_path))//3 + bs = config.eval_batch_size + for i in range(file_num): + indice_name = os.path.join(rst_path, "ncf_bs" + str(bs) + "_" + str(i) + "_0.bin") + item_name = os.path.join(rst_path, "ncf_bs" + str(bs) + "_" + str(i) + "_1.bin") + weight_name = os.path.join(rst_path, "ncf_bs" + str(bs) + "_" + str(i) + "_2.bin") + + batch_indices = np.fromfile(indice_name, np.int32).reshape(1600, 10) + batch_items = np.fromfile(item_name, np.int32).reshape(1600, 100) + metric_weights = np.fromfile(weight_name, bool) + ncf_metric.update(Tensor(batch_indices), Tensor(batch_items), Tensor(metric_weights)) + + out = ncf_metric.eval() + + eval_file_path = os.path.join(config.output_path, config.eval_file_name) + eval_file = open(eval_file_path, "a+") + eval_file.write("EvalCallBack: HR = {}, NDCG = {}\n".format(out[0], out[1])) + eval_file.close() + print("EvalCallBack: HR = {}, NDCG = {}".format(out[0], out[1])) + print("=" * 100 + "Eval Finish!" + "=" * 100) + +if __name__ == '__main__': + get_acc() diff --git a/model_zoo/official/recommend/ncf/preprocess.py b/model_zoo/official/recommend/ncf/preprocess.py new file mode 100644 index 00000000000..aa2851c1daf --- /dev/null +++ b/model_zoo/official/recommend/ncf/preprocess.py @@ -0,0 +1,46 @@ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# 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. +# ============================================================================ +"""preprocess""" +import os + +from src.dataset import create_dataset +from model_utils.config import config + +def get_bin(): + """generate bin files""" + ds_eval, _, _ = create_dataset(test_train=False, data_dir=config.data_path, + dataset=config.dataset, train_epochs=0, + eval_batch_size=config.eval_batch_size) + bs = config.eval_batch_size + user_folder = os.path.join(config.pre_result_path, "00_user") + os.makedirs(user_folder) + item_folder = os.path.join(config.pre_result_path, "01_item") + os.makedirs(item_folder) + mask_folder = os.path.join(config.pre_result_path, "02_mask") + os.makedirs(mask_folder) + + for i, dataset in enumerate(ds_eval.create_tuple_iterator(output_numpy=True)): + users, items, masks = dataset + file_name = "ncf_bs" + str(bs) + "_" + str(i) + ".bin" + users_path = os.path.join(user_folder, file_name) + users.tofile(users_path) + items_path = os.path.join(item_folder, file_name) + items.tofile(items_path) + masks_path = os.path.join(mask_folder, file_name) + masks.tofile(masks_path) + print("=" * 20, "Export bin files success", "=" * 20) + +if __name__ == '__main__': + get_bin() diff --git a/model_zoo/official/recommend/ncf/scripts/run_infer_310.sh b/model_zoo/official/recommend/ncf/scripts/run_infer_310.sh new file mode 100644 index 00000000000..2f1779dfcb7 --- /dev/null +++ b/model_zoo/official/recommend/ncf/scripts/run_infer_310.sh @@ -0,0 +1,120 @@ +#!/bin/bash +# Copyright 2021 Huawei Technologies Co., Ltd +# +# 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. +# ============================================================================ + +if [[ $# -lt 2 || $# -gt 3 ]]; then + echo "Usage: bash run_infer_310.sh [MINDIR_PATH] [NEED_PREPROCESS] [DEVICE_ID] + NEED_PREPROCESS means weather need preprocess or not, it's value is 'y' or 'n'. + DEVICE_ID is optional, it can be set by environment variable device_id, otherwise the value is zero" +exit 1 +fi + +get_real_path(){ + if [ "${1:0:1}" == "/" ]; then + echo "$1" + else + echo "$(realpath -m $PWD/$1)" + fi +} +model=$(get_real_path $1) + +if [ "$2" == "y" ] || [ "$2" == "n" ];then + need_preprocess=$2 +else + echo "weather need preprocess or not, it's value must be in [y, n]" + exit 1 +fi + +device_id=0 +if [ $# == 3 ]; then + device_id=$3 +fi + +echo "mindir name: "$model +echo "need preprocess: "$need_preprocess +echo "device id: "$device_id + +export ASCEND_HOME=/usr/local/Ascend/ +if [ -d ${ASCEND_HOME}/ascend-toolkit ]; then + export PATH=$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/ascend-toolkit/latest/atc/bin:$PATH + export LD_LIBRARY_PATH=/usr/local/lib:$ASCEND_HOME/ascend-toolkit/latest/atc/lib64:$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/lib64:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/add-ons:$LD_LIBRARY_PATH + export TBE_IMPL_PATH=$ASCEND_HOME/ascend-toolkit/latest/opp/op_impl/built-in/ai_core/tbe + export PYTHONPATH=${TBE_IMPL_PATH}:$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/python/site-packages:$PYTHONPATH + export ASCEND_OPP_PATH=$ASCEND_HOME/ascend-toolkit/latest/opp +else + export PATH=$ASCEND_HOME/fwkacllib/bin:$ASCEND_HOME/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/atc/ccec_compiler/bin:$ASCEND_HOME/atc/bin:$PATH + export LD_LIBRARY_PATH=$ASCEND_HOME/fwkacllib/lib64:/usr/local/lib:$ASCEND_HOME/atc/lib64:$ASCEND_HOME/acllib/lib64:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/add-ons:$LD_LIBRARY_PATH + export PYTHONPATH=$ASCEND_HOME/fwkacllib/python/site-packages:$ASCEND_HOME/atc/python/site-packages:$PYTHONPATH + export ASCEND_OPP_PATH=$ASCEND_HOME/opp +fi + +function preprocess_data() +{ + if [ -d preprocess_Result ]; then + rm -rf ./preprocess_Result + fi + mkdir preprocess_Result + python3.7 ../preprocess.py +} + +function compile_app() +{ + cd ../ascend310_infer || exit + bash build.sh &> build.log +} + +function infer() +{ + cd - || exit + if [ -d result_Files ]; then + rm -rf ./result_Files + fi + if [ -d time_Result ]; then + rm -rf ./time_Result + fi + mkdir result_Files + mkdir time_Result + + ../ascend310_infer/out/main --mindir_path=$model --input0_path=./preprocess_Result/00_user --input1_path=./preprocess_Result/01_item --input2_path=./preprocess_Result/02_mask --device_id=$device_id &> infer.log + +} + +function cal_acc() +{ + python3.7 ../postprocess.py &> acc.log +} + +if [ $need_preprocess == "y" ]; then + preprocess_data + if [ $? -ne 0 ]; then + echo "preprocess dataset failed" + exit 1 + fi +fi +compile_app +if [ $? -ne 0 ]; then + echo "compile app code failed" + exit 1 +fi +infer +if [ $? -ne 0 ]; then + echo " execute inference failed" + exit 1 +fi +cal_acc +if [ $? -ne 0 ]; then + echo "calculate accuracy failed" + exit 1 +fi \ No newline at end of file diff --git a/model_zoo/research/nlp/textrcnn/ascend310_infer/CMakeLists.txt b/model_zoo/research/nlp/textrcnn/ascend310_infer/CMakeLists.txt new file mode 100644 index 00000000000..170e6c5275e --- /dev/null +++ b/model_zoo/research/nlp/textrcnn/ascend310_infer/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.14.1) +project(Ascend310Infer) +add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -g -std=c++17 -Werror -Wall -fPIE -Wl,--allow-shlib-undefined") +set(PROJECT_SRC_ROOT ${CMAKE_CURRENT_LIST_DIR}/) +option(MINDSPORE_PATH "mindspore install path" "") +include_directories(${MINDSPORE_PATH}) +include_directories(${MINDSPORE_PATH}/include) +include_directories(${PROJECT_SRC_ROOT}) +find_library(MS_LIB libmindspore.so ${MINDSPORE_PATH}/lib) +file(GLOB_RECURSE MD_LIB ${MINDSPORE_PATH}/_c_dataengine*) + +add_executable(main src/main.cc src/utils.cc) +target_link_libraries(main ${MS_LIB} ${MD_LIB} gflags) diff --git a/model_zoo/research/nlp/textrcnn/ascend310_infer/build.sh b/model_zoo/research/nlp/textrcnn/ascend310_infer/build.sh new file mode 100644 index 00000000000..285514e19f2 --- /dev/null +++ b/model_zoo/research/nlp/textrcnn/ascend310_infer/build.sh @@ -0,0 +1,29 @@ +#!/bin/bash +# Copyright 2021 Huawei Technologies Co., Ltd +# +# 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. +# ============================================================================ +if [ -d out ]; then + rm -rf out +fi + +mkdir out +cd out || exit + +if [ -f "Makefile" ]; then + make clean +fi + +cmake .. \ + -DMINDSPORE_PATH="`pip3.7 show mindspore-ascend | grep Location | awk '{print $2"/mindspore"}' | xargs realpath`" +make diff --git a/model_zoo/research/nlp/textrcnn/ascend310_infer/inc/utils.h b/model_zoo/research/nlp/textrcnn/ascend310_infer/inc/utils.h new file mode 100644 index 00000000000..efebe03a8c1 --- /dev/null +++ b/model_zoo/research/nlp/textrcnn/ascend310_infer/inc/utils.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_INFERENCE_UTILS_H_ +#define MINDSPORE_INFERENCE_UTILS_H_ + +#include +#include +#include +#include +#include +#include "include/api/types.h" + +std::vector GetAllFiles(std::string_view dirName); +DIR *OpenDir(std::string_view dirName); +std::string RealPath(std::string_view path); +mindspore::MSTensor ReadFileToTensor(const std::string &file); +int WriteResult(const std::string& imageFile, const std::vector &outputs); +#endif diff --git a/model_zoo/research/nlp/textrcnn/ascend310_infer/src/main.cc b/model_zoo/research/nlp/textrcnn/ascend310_infer/src/main.cc new file mode 100644 index 00000000000..4a054dc5c9f --- /dev/null +++ b/model_zoo/research/nlp/textrcnn/ascend310_infer/src/main.cc @@ -0,0 +1,130 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "include/api/model.h" +#include "include/api/context.h" +#include "include/api/types.h" +#include "include/api/serialization.h" +#include "include/dataset/execute.h" +#include "include/dataset/vision.h" +#include "inc/utils.h" + +using mindspore::Context; +using mindspore::Serialization; +using mindspore::Model; +using mindspore::Status; +using mindspore::MSTensor; +using mindspore::dataset::Execute; +using mindspore::ModelType; +using mindspore::GraphCell; +using mindspore::kSuccess; + +DEFINE_string(mindir_path, "", "mindir path"); +DEFINE_string(input0_path, ".", "input0 path"); +DEFINE_int32(device_id, 0, "device id"); + +int main(int argc, char **argv) { + gflags::ParseCommandLineFlags(&argc, &argv, true); + if (RealPath(FLAGS_mindir_path).empty()) { + std::cout << "Invalid mindir" << std::endl; + return 1; + } + + auto context = std::make_shared(); + auto ascend310 = std::make_shared(); + ascend310->SetDeviceID(FLAGS_device_id); + context->MutableDeviceInfo().push_back(ascend310); + mindspore::Graph graph; + Serialization::Load(FLAGS_mindir_path, ModelType::kMindIR, &graph); + + Model model; + Status ret = model.Build(GraphCell(graph), context); + if (ret != kSuccess) { + std::cout << "ERROR: Build failed." << std::endl; + return 1; + } + + std::vector model_inputs = model.GetInputs(); + if (model_inputs.empty()) { + std::cout << "Invalid model, inputs is empty." << std::endl; + return 1; + } + + auto input0_files = GetAllFiles(FLAGS_input0_path); + + if (input0_files.empty()) { + std::cout << "ERROR: input data empty." << std::endl; + return 1; + } + + std::map costTime_map; + size_t size = input0_files.size(); + + for (size_t i = 0; i < size; ++i) { + struct timeval start = {0}; + struct timeval end = {0}; + double startTimeMs; + double endTimeMs; + std::vector inputs; + std::vector outputs; + std::cout << "Start predict input files:" << input0_files[i] << std::endl; + + auto input0 = ReadFileToTensor(input0_files[i]); + inputs.emplace_back(model_inputs[0].Name(), model_inputs[0].DataType(), model_inputs[0].Shape(), + input0.Data().get(), input0.DataSize()); + + gettimeofday(&start, nullptr); + ret = model.Predict(inputs, &outputs); + gettimeofday(&end, nullptr); + if (ret != kSuccess) { + std::cout << "Predict " << input0_files[i] << " failed." << std::endl; + return 1; + } + startTimeMs = (1.0 * start.tv_sec * 1000000 + start.tv_usec) / 1000; + endTimeMs = (1.0 * end.tv_sec * 1000000 + end.tv_usec) / 1000; + costTime_map.insert(std::pair(startTimeMs, endTimeMs)); + WriteResult(input0_files[i], outputs); + } + double average = 0.0; + int inferCount = 0; + + for (auto iter = costTime_map.begin(); iter != costTime_map.end(); iter++) { + double diff = 0.0; + diff = iter->second - iter->first; + average += diff; + inferCount++; + } + average = average / inferCount; + std::stringstream timeCost; + timeCost << "NN inference cost average time: "<< average << " ms of infer_count " << inferCount << std::endl; + std::cout << "NN inference cost average time: "<< average << "ms of infer_count " << inferCount << std::endl; + std::string fileName = "./time_Result" + std::string("/test_perform_static.txt"); + std::ofstream fileStream(fileName.c_str(), std::ios::trunc); + fileStream << timeCost.str(); + fileStream.close(); + costTime_map.clear(); + return 0; +} diff --git a/model_zoo/research/nlp/textrcnn/ascend310_infer/src/utils.cc b/model_zoo/research/nlp/textrcnn/ascend310_infer/src/utils.cc new file mode 100644 index 00000000000..c947e4d5f45 --- /dev/null +++ b/model_zoo/research/nlp/textrcnn/ascend310_infer/src/utils.cc @@ -0,0 +1,129 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#include +#include +#include +#include "inc/utils.h" + +using mindspore::MSTensor; +using mindspore::DataType; + +std::vector GetAllFiles(std::string_view dirName) { + struct dirent *filename; + DIR *dir = OpenDir(dirName); + if (dir == nullptr) { + return {}; + } + std::vector res; + while ((filename = readdir(dir)) != nullptr) { + std::string dName = std::string(filename->d_name); + if (dName == "." || dName == ".." || filename->d_type != DT_REG) { + continue; + } + res.emplace_back(std::string(dirName) + "/" + filename->d_name); + } + std::sort(res.begin(), res.end()); + for (auto &f : res) { + std::cout << "image file: " << f << std::endl; + } + return res; +} + +int WriteResult(const std::string& imageFile, const std::vector &outputs) { + std::string homePath = "./result_Files"; + for (size_t i = 0; i < outputs.size(); ++i) { + size_t outputSize; + std::shared_ptr netOutput; + netOutput = outputs[i].Data(); + outputSize = outputs[i].DataSize(); + int pos = imageFile.rfind('/'); + std::string fileName(imageFile, pos + 1); + fileName.replace(fileName.find('.'), fileName.size() - fileName.find('.'), '_' + std::to_string(i) + ".bin"); + std::string outFileName = homePath + "/" + fileName; + FILE * outputFile = fopen(outFileName.c_str(), "wb"); + fwrite(netOutput.get(), outputSize, sizeof(char), outputFile); + fclose(outputFile); + outputFile = nullptr; + } + return 0; +} + +mindspore::MSTensor ReadFileToTensor(const std::string &file) { + if (file.empty()) { + std::cout << "Pointer file is nullptr" << std::endl; + return mindspore::MSTensor(); + } + + std::ifstream ifs(file); + if (!ifs.good()) { + std::cout << "File: " << file << " is not exist" << std::endl; + return mindspore::MSTensor(); + } + + if (!ifs.is_open()) { + std::cout << "File: " << file << "open failed" << std::endl; + return mindspore::MSTensor(); + } + + ifs.seekg(0, std::ios::end); + size_t size = ifs.tellg(); + mindspore::MSTensor buffer(file, mindspore::DataType::kNumberTypeUInt8, {static_cast(size)}, nullptr, size); + + ifs.seekg(0, std::ios::beg); + ifs.read(reinterpret_cast(buffer.MutableData()), size); + ifs.close(); + + return buffer; +} + + +DIR *OpenDir(std::string_view dirName) { + if (dirName.empty()) { + std::cout << " dirName is null ! " << std::endl; + return nullptr; + } + std::string realPath = RealPath(dirName); + struct stat s; + lstat(realPath.c_str(), &s); + if (!S_ISDIR(s.st_mode)) { + std::cout << "dirName is not a valid directory !" << std::endl; + return nullptr; + } + DIR *dir; + dir = opendir(realPath.c_str()); + if (dir == nullptr) { + std::cout << "Can not open dir " << dirName << std::endl; + return nullptr; + } + std::cout << "Successfully opened the dir " << dirName << std::endl; + return dir; +} + +std::string RealPath(std::string_view path) { + char realPathMem[PATH_MAX] = {0}; + char *realPathRet = nullptr; + realPathRet = realpath(path.data(), realPathMem); + + if (realPathRet == nullptr) { + std::cout << "File: " << path << " is not exist."; + return ""; + } + + std::string realPath(realPathMem); + std::cout << path << " realpath is: " << realPath << std::endl; + return realPath; +} diff --git a/model_zoo/research/nlp/textrcnn/postprocess.py b/model_zoo/research/nlp/textrcnn/postprocess.py new file mode 100644 index 00000000000..cddd58acab6 --- /dev/null +++ b/model_zoo/research/nlp/textrcnn/postprocess.py @@ -0,0 +1,44 @@ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# 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. +# ============================================================================ +"""postprocess""" +import os +import argparse +import numpy as np + +from mindspore.nn.metrics import Accuracy +from src.config import textrcnn_cfg as cfg + +parser = argparse.ArgumentParser(description='postprocess') +parser.add_argument('--label_path', type=str, default="./preprocess_Result/label_ids.npy") +parser.add_argument('--result_path', type=str, default="./result_Files") +args = parser.parse_args() + +def get_acc(): + '''calculate accuracy''' + metric = Accuracy() + metric.clear() + label_list = np.load(args.label_path, allow_pickle=True) + file_num = len(os.listdir(args.result_path)) + + for i in range(file_num): + f_name = "textcrnn_bs" + str(cfg.batch_size) + "_" + str(i) + "_0.bin" + pred = np.fromfile(os.path.join(args.result_path, f_name), np.float16) + pred = pred.reshape(cfg.batch_size, int(pred.shape[0]/cfg.batch_size)) + metric.update(pred, label_list[i]) + acc = metric.eval() + print("============== Accuracy:{} ==============".format(acc)) + +if __name__ == '__main__': + get_acc() diff --git a/model_zoo/research/nlp/textrcnn/preprocess.py b/model_zoo/research/nlp/textrcnn/preprocess.py new file mode 100644 index 00000000000..9ac666b9899 --- /dev/null +++ b/model_zoo/research/nlp/textrcnn/preprocess.py @@ -0,0 +1,45 @@ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# 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. +# ============================================================================ +"""preprocess""" +import os +import argparse +import numpy as np + +from src.config import textrcnn_cfg as cfg +from src.dataset import create_dataset + +parser = argparse.ArgumentParser(description='preprocess') +parser.add_argument('--result_path', type=str, default="./preprocess_Result") +args = parser.parse_args() + +def get_bin(): + '''generate bin files.''' + ds_eval = create_dataset(cfg.preprocess_path, cfg.batch_size, False) + img_path = os.path.join(args.result_path, "00_feature") + os.makedirs(img_path) + label_list = [] + + for i, data in enumerate(ds_eval.create_dict_iterator(output_numpy=True)): + file_name = "textcrnn_bs" + str(cfg.batch_size) + "_" + str(i) + ".bin" + file_path = os.path.join(img_path, file_name) + + data["feature"].tofile(file_path) + label_list.append(data["label"]) + + np.save(os.path.join(args.result_path, "label_ids.npy"), label_list) + print("=" * 20, "bin files finished", "=" * 20) + +if __name__ == '__main__': + get_bin() diff --git a/model_zoo/research/nlp/textrcnn/readme.md b/model_zoo/research/nlp/textrcnn/readme.md index 05fb507f263..2ee5b60e5ae 100644 --- a/model_zoo/research/nlp/textrcnn/readme.md +++ b/model_zoo/research/nlp/textrcnn/readme.md @@ -8,6 +8,10 @@ - [Environment Requirements](#environment-requirements) - [Quick Start](#quick-start) - [Script Description](#script-description) +- [Inference Process](#inference-process) + - [Export MindIR](#export-mindir) + - [Infer on Ascend310](#infer-on-ascend310) + - [result](#result) - [ModelZoo Homepage](#modelzoo-homepage) ## [TextRCNN Description](#contents) @@ -138,6 +142,37 @@ Parameters for both training and evaluation can be set in config.py | Accuracy | 0.78 | 0.78 | | Speed | 35ms/step | 77ms/step | +## Inference Process + +### [Export MindIR](#contents) + +```shell +python export.py --ckpt_file [CKPT_PATH] --file_name [FILE_NAME] --file_format [FILE_FORMAT] +``` + +The ckpt_file parameter is required, +`EXPORT_FORMAT` should be in ["AIR", "MINDIR"] + +### Infer on Ascend310 + +Before performing inference, the mindir file must be exported by `export.py` script. We only provide an example of inference using MINDIR model. + +```shell +# Ascend310 inference +bash run_infer_310.sh [MINDIR_PATH] [NEED_PREPROCESS] [DEVICE_ID] +``` + +- `NEED_PREPROCESS` means weather need preprocess or not, it's value is 'y' or 'n'. +- `DEVICE_ID` is optional, default value is 0. + +### result + +Inference result is saved in current path, you can find result like this in acc.log file. + +```bash +============== Accuracy:{} ============== 0.8008 +``` + ## [ModelZoo Homepage](#contents) Please check the official [homepage](https://gitee.com/mindspore/mindspore/tree/master/model_zoo). diff --git a/model_zoo/research/nlp/textrcnn/scripts/run_infer_310.sh b/model_zoo/research/nlp/textrcnn/scripts/run_infer_310.sh new file mode 100644 index 00000000000..34bf7a120d8 --- /dev/null +++ b/model_zoo/research/nlp/textrcnn/scripts/run_infer_310.sh @@ -0,0 +1,120 @@ +#!/bin/bash +# Copyright 2021 Huawei Technologies Co., Ltd +# +# 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. +# ============================================================================ + +if [[ $# -lt 2 || $# -gt 3 ]]; then + echo "Usage: bash run_infer_310.sh [MINDIR_PATH] [NEED_PREPROCESS] [DEVICE_ID] + NEED_PREPROCESS means weather need preprocess or not, it's value is 'y' or 'n'. + DEVICE_ID is optional, it can be set by environment variable device_id, otherwise the value is zero" +exit 1 +fi + +get_real_path(){ + if [ "${1:0:1}" == "/" ]; then + echo "$1" + else + echo "$(realpath -m $PWD/$1)" + fi +} +model=$(get_real_path $1) + +if [ "$2" == "y" ] || [ "$2" == "n" ];then + need_preprocess=$2 +else + echo "weather need preprocess or not, it's value must be in [y, n]" + exit 1 +fi + +device_id=0 +if [ $# == 3 ]; then + device_id=$3 +fi + +echo "mindir name: "$model +echo "need preprocess: "$need_preprocess +echo "device id: "$device_id + +export ASCEND_HOME=/usr/local/Ascend/ +if [ -d ${ASCEND_HOME}/ascend-toolkit ]; then + export PATH=$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/ascend-toolkit/latest/atc/bin:$PATH + export LD_LIBRARY_PATH=/usr/local/lib:$ASCEND_HOME/ascend-toolkit/latest/atc/lib64:$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/lib64:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/add-ons:$LD_LIBRARY_PATH + export TBE_IMPL_PATH=$ASCEND_HOME/ascend-toolkit/latest/opp/op_impl/built-in/ai_core/tbe + export PYTHONPATH=${TBE_IMPL_PATH}:$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/python/site-packages:$PYTHONPATH + export ASCEND_OPP_PATH=$ASCEND_HOME/ascend-toolkit/latest/opp +else + export PATH=$ASCEND_HOME/fwkacllib/bin:$ASCEND_HOME/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/atc/ccec_compiler/bin:$ASCEND_HOME/atc/bin:$PATH + export LD_LIBRARY_PATH=$ASCEND_HOME/fwkacllib/lib64:/usr/local/lib:$ASCEND_HOME/atc/lib64:$ASCEND_HOME/acllib/lib64:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/add-ons:$LD_LIBRARY_PATH + export PYTHONPATH=$ASCEND_HOME/fwkacllib/python/site-packages:$ASCEND_HOME/atc/python/site-packages:$PYTHONPATH + export ASCEND_OPP_PATH=$ASCEND_HOME/opp +fi + +function preprocess_data() +{ + if [ -d preprocess_Result ]; then + rm -rf ./preprocess_Result + fi + mkdir preprocess_Result + python3.7 ../preprocess.py --result_path=./preprocess_Result +} + +function compile_app() +{ + cd ../ascend310_infer || exit + bash build.sh &> build.log +} + +function infer() +{ + cd - || exit + if [ -d result_Files ]; then + rm -rf ./result_Files + fi + if [ -d time_Result ]; then + rm -rf ./time_Result + fi + mkdir result_Files + mkdir time_Result + + ../ascend310_infer/out/main --mindir_path=$model --input0_path=./preprocess_Result/00_feature --device_id=$device_id &> infer.log + +} + +function cal_acc() +{ + python3.7 ../postprocess.py --label_path=./preprocess_Result/label_ids.npy --result_path=./result_Files &> acc.log +} + +if [ $need_preprocess == "y" ]; then + preprocess_data + if [ $? -ne 0 ]; then + echo "preprocess dataset failed" + exit 1 + fi +fi +compile_app +if [ $? -ne 0 ]; then + echo "compile app code failed" + exit 1 +fi +infer +if [ $? -ne 0 ]; then + echo " execute inference failed" + exit 1 +fi +cal_acc +if [ $? -ne 0 ]; then + echo "calculate accuracy failed" + exit 1 +fi \ No newline at end of file