diff --git a/model_zoo/official/cv/vgg16/README.md b/model_zoo/official/cv/vgg16/README.md index f59c86dab3a..902fc41e3c9 100644 --- a/model_zoo/official/cv/vgg16/README.md +++ b/model_zoo/official/cv/vgg16/README.md @@ -27,6 +27,7 @@ - [Export MindIR](#export-mindir) - [Infer on Ascend310](#infer-on-ascend310) - [result](#result) + - [Post Training Quantization](#post-training-quantization) - [Model Description](#model-description) - [Performance](#performance) - [Training Performance](#training-performance) @@ -530,6 +531,40 @@ Inference result is saved in current path, you can find result like this in acc. 'acc': 0.92 ``` +### [Post Training Quantization](#contents) + +Relative executing script files reside in the directory "ascend310_quant_infer". Please implement following steps sequentially to complete post quantization. +Current quantization project bases on CIFAR-10 dataset. + +1. Generate data of .bin format required for AIR model inference at Ascend310 platform. + +```shell +python export_bin.py --config_path [YMAL CONFIG PATH] --data_dir [DATA DIR] --result_path [RESULT PATH] +``` + +2. Export quantized AIR model. + +Post quantization of model requires special toolkits for exporting quantized AIR model. Please refer to [official website](https://www.hiascend.com/software/cann/community). + +```shell +python post_quant.py --config_path [YMAL CONFIG PATH] --ckpt_file [CKPT_PATH] --data_dir [DATASET PATH] +``` + +The quantized AIR file will be stored as "./results/vgg_quant.air". + +3. Implement inference at Ascend310 platform. + +```shell +# Ascend310 quant inference +bash run_quant_infer.sh [AIR_PATH] [DATA_PATH] [LABEL_PATH] +``` + +Inference result is saved in current path, you can find result like this in acc.log file. + +```bash +'acc': 0.91 +``` + ## [Model Description](#contents) ### [Performance](#contents) diff --git a/model_zoo/official/cv/vgg16/README_CN.md b/model_zoo/official/cv/vgg16/README_CN.md index 4efbb025f44..740a1d3b57a 100644 --- a/model_zoo/official/cv/vgg16/README_CN.md +++ b/model_zoo/official/cv/vgg16/README_CN.md @@ -29,6 +29,7 @@ - [导出MindIR](#导出mindir) - [在Ascend310执行推理](#在ascend310执行推理) - [结果](#结果) + - [训练后量化推理](#训练后量化推理) - [模型描述](#模型描述) - [性能](#性能) - [训练性能](#训练性能) @@ -533,6 +534,39 @@ bash run_infer_310.sh [MINDIR_PATH] [DATASET_NAME] [DATASET_PATH] [NEED_PREPROCE 'acc': 0.92 ``` +### [训练后量化推理](#contents) + +训练后量化推理的相关执行脚本文件在"ascend310_quant_infer"目录下,依次执行以下步骤实现训练后量化推理。本训练后量化工程基于CIFAR-10数据集。 + +1、生成Ascend310平台AIR模型推理需要的.bin格式数据。 + +```shell +python export_bin.py --config_path [YMAL CONFIG PATH] --data_dir [DATA DIR] --result_path [RESULT PATH] +``` + +2、导出训练后量化的AIR格式模型。 + +导出训练后量化模型需要配套的量化工具包,参考[官方地址](https://www.hiascend.com/software/cann/community) + +```shell +python post_quant.py --config_path [YMAL_CONFIG_PATH] --ckpt_file [CKPT_PATH] --data_dir [DATASET PATH] +``` + +导出的模型会存储在./result/vgg_quant.air。 + +3、在Ascend310执行推理量化模型。 + +```shell +# Ascend310 inference +bash run_quant_infer.sh [AIR_PATH] [DATA_PATH] [LABEL_PATH] +``` + +推理结果保存在脚本执行的当前路径,可以在acc.log中看到精度计算结果。 + +```bash +'acc': 0.91 +``` + ## 模型描述 ### 性能 diff --git a/model_zoo/official/cv/vgg16/ascend310_quant_infer/acc.py b/model_zoo/official/cv/vgg16/ascend310_quant_infer/acc.py new file mode 100644 index 00000000000..1204fe43203 --- /dev/null +++ b/model_zoo/official/cv/vgg16/ascend310_quant_infer/acc.py @@ -0,0 +1,49 @@ +# 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 for 310 inference""" +import os +import argparse +import numpy as np + +from mindspore.nn import Top1CategoricalAccuracy + + +parser = argparse.ArgumentParser("vgg16 quant postprocess") +parser.add_argument("--result_path", type=str, required=True, help="path to inference results.") +parser.add_argument("--label_path", type=str, required=True, help="path to label.npy.") + +args, _ = parser.parse_known_args() + +def calculate_acc(result_path, label_path): + """ + Calculate accuracy of VGG16 inference. + + Args: + result_path (str): the directory or inference result. + label_path (str): the path of data label in .npy format. + """ + top1_acc = Top1CategoricalAccuracy() + labels = np.load(label_path, allow_pickle=True) + batch_size = 1 + for idx, _ in enumerate(labels): + f_name = os.path.join(result_path, "VGG16_data_bs" + str(batch_size) + "_" + str(idx) + "_output_0.bin") + pred = np.fromfile(f_name, np.float32) + pred = pred.reshape(batch_size, int(pred.shape[0] / batch_size)) + top1_acc.update(pred, labels[idx]) + print("acc: ", top1_acc.eval()) + + +if __name__ == '__main__': + calculate_acc(args.result_path, args.label_path) diff --git a/model_zoo/official/cv/vgg16/ascend310_quant_infer/export_bin.py b/model_zoo/official/cv/vgg16/ascend310_quant_infer/export_bin.py new file mode 100644 index 00000000000..eeddb5cd8cc --- /dev/null +++ b/model_zoo/official/cv/vgg16/ascend310_quant_infer/export_bin.py @@ -0,0 +1,50 @@ +# 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. +# ============================================================================ +"""generate data and label needed for AIR model inference""" +import os +import sys +import shutil +import numpy as np + + +def generate_data(): + """ + Generate data and label needed for AIR model inference at Ascend310 platform. + """ + config.batch_size = 1 + config.image_size = list(map(int, config.image_size.split(','))) + config.dataset = "cifar10" + + dataset = vgg_create_dataset(config.data_dir, config.image_size, config.batch_size, training=False) + img_path = os.path.join(config.result_path, "00_data") + if os.path.exists(img_path): + shutil.rmtree(img_path) + os.makedirs(img_path) + label_list = [] + for idx, data in enumerate(dataset.create_dict_iterator(output_numpy=True)): + file_name = "VGG16_data_bs" + str(config.batch_size) + "_" + str(idx) + ".bin" + file_path = os.path.join(img_path, file_name) + data["image"].tofile(file_path) + label_list.append(data["label"]) + np.save(os.path.join(config.result_path, "cifar10_label_ids.npy"), label_list) + print("=" * 20, "export bin files finished", "=" * 20) + + +if __name__ == "__main__": + sys.path.append("..") + from src.dataset import vgg_create_dataset + from model_utils.moxing_adapter import config + + generate_data() diff --git a/model_zoo/official/cv/vgg16/ascend310_quant_infer/inc/model_process.h b/model_zoo/official/cv/vgg16/ascend310_quant_infer/inc/model_process.h new file mode 100644 index 00000000000..79e19833c7a --- /dev/null +++ b/model_zoo/official/cv/vgg16/ascend310_quant_infer/inc/model_process.h @@ -0,0 +1,111 @@ +/** + * 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. + */ + +#pragma once +#include +#include "../inc/utils.h" +#include "acl/acl.h" + +/** +* ModelProcess +*/ +class ModelProcess { + public: + /** + * @brief Constructor + */ + ModelProcess(); + + /** + * @brief Destructor + */ + ~ModelProcess(); + + /** + * @brief load model from file with mem + * @param [in] modelPath: model path + * @return result + */ + Result LoadModelFromFileWithMem(const char *modelPath); + + /** + * @brief unload model + */ + void Unload(); + + /** + * @brief create model desc + * @return result + */ + Result CreateDesc(); + + /** + * @brief destroy desc + */ + void DestroyDesc(); + + /** + * @brief create model input + * @param [in] inputDataBuffer: input buffer + * @param [in] bufferSize: input buffer size + * @return result + */ + Result CreateInput(void *inputDataBuffer, size_t bufferSize); + + /** + * @brief destroy input resource + */ + void DestroyInput(); + + /** + * @brief create output buffer + * @return result + */ + Result CreateOutput(); + + /** + * @brief destroy output resource + */ + void DestroyOutput(); + + /** + * @brief model execute + * @return result + */ + Result Execute(); + + /** + * @brief dump model output result to file + */ + void DumpModelOutputResult(char *output_name); + + /** + * @brief get model output result + */ + void OutputModelResult(); + + private: + uint32_t modelId_; + size_t modelMemSize_; + size_t modelWeightSize_; + void *modelMemPtr_; + void *modelWeightPtr_; + bool loadFlag_; // model load flag + aclmdlDesc *modelDesc_; + aclmdlDataset *input_; + aclmdlDataset *output_; +}; + diff --git a/model_zoo/official/cv/vgg16/ascend310_quant_infer/inc/sample_process.h b/model_zoo/official/cv/vgg16/ascend310_quant_infer/inc/sample_process.h new file mode 100644 index 00000000000..24d6ea01e59 --- /dev/null +++ b/model_zoo/official/cv/vgg16/ascend310_quant_infer/inc/sample_process.h @@ -0,0 +1,58 @@ +/** + * 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. + */ + +#pragma once +#include +#include +#include "../inc/utils.h" +#include "acl/acl.h" + +/** +* SampleProcess +*/ +class SampleProcess { + public: + /** + * @brief Constructor + */ + SampleProcess(); + + /** + * @brief Destructor + */ + ~SampleProcess(); + + /** + * @brief init reousce + * @return result + */ + Result InitResource(); + + /** + * @brief sample process + * @return result + */ + Result Process(char *om_path, char *input_folder); + + void GetAllFiles(std::string path, std::vector *files); + + private: + void DestroyResource(); + + int32_t deviceId_; + aclrtContext context_; + aclrtStream stream_; +}; diff --git a/model_zoo/official/cv/vgg16/ascend310_quant_infer/inc/utils.h b/model_zoo/official/cv/vgg16/ascend310_quant_infer/inc/utils.h new file mode 100644 index 00000000000..3ae2a571b8e --- /dev/null +++ b/model_zoo/official/cv/vgg16/ascend310_quant_infer/inc/utils.h @@ -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. + */ + +#pragma once +#include +#include + +#define INFO_LOG(fmt, args...) fprintf(stdout, "[INFO] " fmt "\n", ##args) +#define WARN_LOG(fmt, args...) fprintf(stdout, "[WARN] " fmt "\n", ##args) +#define ERROR_LOG(fmt, args...) fprintf(stdout, "[ERROR] " fmt "\n", ##args) + +typedef enum Result { + SUCCESS = 0, + FAILED = 1 +} Result; + +/** +* Utils +*/ +class Utils { + public: + /** + * @brief create device buffer of file + * @param [in] fileName: file name + * @param [out] fileSize: size of file + * @return device buffer of file + */ + static void *GetDeviceBufferOfFile(std::string fileName, uint32_t *fileSize); + + /** + * @brief create buffer of file + * @param [in] fileName: file name + * @param [out] fileSize: size of file + * @return buffer of pic + */ + static void* ReadBinFile(std::string fileName, uint32_t *fileSize); +}; + +#pragma once diff --git a/model_zoo/official/cv/vgg16/ascend310_quant_infer/post_quant.py b/model_zoo/official/cv/vgg16/ascend310_quant_infer/post_quant.py new file mode 100644 index 00000000000..16f1274155d --- /dev/null +++ b/model_zoo/official/cv/vgg16/ascend310_quant_infer/post_quant.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. +# ============================================================================ +"""do post training quantization for Ascend310""" +import sys +import numpy as np + +from amct_mindspore.quantize_tool import create_quant_config +from amct_mindspore.quantize_tool import quantize_model +from amct_mindspore.quantize_tool import save_model +import mindspore.nn as nn +from mindspore import Tensor, context +from mindspore.nn.optim.momentum import Momentum +from mindspore.train.model import Model +from mindspore.train.serialization import load_checkpoint +from mindspore.common import dtype as mstype + + +def quant_vgg(network, dataset, input_data): + """ + Export post training quantization model of AIR format. + + Args: + network: the origin network for inference. + dataset: the data for inference. + input_data: the data used for constructing network. The shape and format of input data should be the same as + actual data for inference. + """ + + # step2: create the quant config json file + create_quant_config("./config.json", network, input_data) + + # step3: do some network modification and return the modified network + calibration_network = quantize_model("./config.json", network, input_data) + calibration_network.set_train(False) + + # step4: perform the evaluation of network to do activation calibration + loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean") + opt = Momentum(filter(lambda x: x.requires_grad, calibration_network.get_parameters()), 0.01, config.momentum, + weight_decay=config.weight_decay) + model = Model(calibration_network, loss_fn=loss, optimizer=opt, metrics={"acc"}) + _ = model.eval(dataset, dataset_sink_mode=False) + + # step5: export the air file + save_model("results/vgg_quant", calibration_network, input_data) + print("[INFO] the quantized AIR file has been stored at: \n {}".format("results/vgg_quant.air")) + + +def run_export(): + """ + Prepare input parameters needed for exporting quantization model. + """ + context.set_context(mode=context.GRAPH_MODE, device_target=config.device_target) + if config.device_target == "Ascend": + config.device_id = get_device_id() + context.set_context(device_id=config.device_id) + + config.image_size = list(map(int, config.image_size.split(','))) + if config.dataset == "cifar10": + net = vgg16(num_classes=config.num_classes, args=config) + else: + net = vgg16(config.num_classes, config, phase="test") + + load_checkpoint(config.ckpt_file, net=net) + net.set_train(False) + batch_size = 1 + input_data = Tensor(np.zeros([batch_size, 3, config.image_size[0], config.image_size[1]]), mstype.float32) + dataset = vgg_create_dataset(config.data_dir, config.image_size, batch_size, training=False) + ds = dataset.take(1) + quant_vgg(net, ds, input_data) + + +if __name__ == "__main__": + sys.path.append("..") + from src.vgg import vgg16 + from src.dataset import vgg_create_dataset + from model_utils.moxing_adapter import config + from model_utils.device_adapter import get_device_id + run_export() diff --git a/model_zoo/official/cv/vgg16/ascend310_quant_infer/run_quant_infer.sh b/model_zoo/official/cv/vgg16/ascend310_quant_infer/run_quant_infer.sh new file mode 100644 index 00000000000..56f958ea641 --- /dev/null +++ b/model_zoo/official/cv/vgg16/ascend310_quant_infer/run_quant_infer.sh @@ -0,0 +1,101 @@ +#!/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 ]; then + echo "Usage: bash run_quant_infer.sh [AIR_PATH] [DATA_PATH] [LABEL_PATH]" +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) +data_path=$(get_real_path $2) +label_path=$(get_real_path $3) + +echo "air name: "$model +echo "dataset path: "$data_path +echo "label path: "$label_path + +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/atc/ccec_compiler/bin:$ASCEND_HOME/atc/bin:$PATH + export LD_LIBRARY_PATH=/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/atc/python/site-packages:$PYTHONPATH + export ASCEND_OPP_PATH=$ASCEND_HOME/opp +fi + +function air_to_om() +{ + atc --input_format=NCHW --framework=1 --model=$model --output=vgg_quant --soc_version=Ascend310 &> atc.log +} + +function compile_app() +{ + bash ./src/build.sh &> build.log +} + +function infer() +{ + if [ -d result ]; then + rm -rf ./result + fi + mkdir result + ./out/main ./vgg_quant.om $data_path &> infer.log +} + +function cal_acc() +{ + python3.7 ./acc.py --result_path=./result --label_path=$label_path &> acc.log +} + +echo "start atc================================================" +air_to_om +if [ $? -ne 0 ]; then + echo "air to om code failed" + exit 1 +fi + +echo "start compile============================================" +compile_app +if [ $? -ne 0 ]; then + echo "compile app code failed" + exit 1 +fi + +echo "start infer==============================================" +infer +if [ $? -ne 0 ]; then + echo " execute inference failed" + exit 1 +fi + +echo "start calculate acc======================================" +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/cv/vgg16/ascend310_quant_infer/src/CMakeLists.txt b/model_zoo/official/cv/vgg16/ascend310_quant_infer/src/CMakeLists.txt new file mode 100644 index 00000000000..655026d7d91 --- /dev/null +++ b/model_zoo/official/cv/vgg16/ascend310_quant_infer/src/CMakeLists.txt @@ -0,0 +1,43 @@ +# Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. + +# CMake lowest version requirement +cmake_minimum_required(VERSION 3.5.1) +# project information +project(InferClassification) +# Check environment variable +if(NOT DEFINED ENV{ASCEND_HOME}) + message(FATAL_ERROR "please define environment variable:ASCEND_HOME") +endif() + +# Compile options +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") + +# Skip build rpath +set(CMAKE_SKIP_BUILD_RPATH True) + +# Set output directory +set(PROJECT_SRC_ROOT ${CMAKE_CURRENT_LIST_DIR}/) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SRC_ROOT}/../out) + +# Set include directory and library directory +set(FWKACL_LIB_DIR $ENV{ASCEND_HOME}/fwkacllib) +set(ACL_LIB_DIR $ENV{ASCEND_HOME}/acllib) +set(ATLAS_ACL_LIB_DIR $ENV{ASCEND_HOME}/ascend-toolkit/latest/acllib) + +# Header path +include_directories(${ACL_LIB_DIR}/include/) +include_directories(${FWKACL_LIB_DIR}/include/) +include_directories(${ATLAS_ACL_LIB_DIR}/include/) +include_directories(${PROJECT_SRC_ROOT}/../inc) + +# add host lib path +link_directories(${ACL_LIB_DIR} ${FWKACL_LIB_DIR}) +find_library(acl libascendcl.so ${ACL_LIB_DIR}/lib64 ${FWKACL_LIB_DIR}/lib64 ${ATLAS_ACL_LIB_DIR}/lib64) + +add_executable(main utils.cpp + sample_process.cpp + model_process.cpp + main.cpp) + +target_link_libraries(main ${acl} gflags pthread) diff --git a/model_zoo/official/cv/vgg16/ascend310_quant_infer/src/acl.json b/model_zoo/official/cv/vgg16/ascend310_quant_infer/src/acl.json new file mode 100644 index 00000000000..0967ef424bc --- /dev/null +++ b/model_zoo/official/cv/vgg16/ascend310_quant_infer/src/acl.json @@ -0,0 +1 @@ +{} diff --git a/model_zoo/official/cv/vgg16/ascend310_quant_infer/src/build.sh b/model_zoo/official/cv/vgg16/ascend310_quant_infer/src/build.sh new file mode 100755 index 00000000000..b5979b68e60 --- /dev/null +++ b/model_zoo/official/cv/vgg16/ascend310_quant_infer/src/build.sh @@ -0,0 +1,55 @@ +#!/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. +# ============================================================================ +path_cur=$(cd "`dirname $0`" || exit; pwd) + +function preparePath() { + rm -rf $1 + mkdir -p $1 + cd $1 || exit +} + +function buildA300() { + if [ ! "${ARCH_PATTERN}" ]; then + # set ARCH_PATTERN to acllib when it was not specified by user + export ARCH_PATTERN=acllib + echo "ARCH_PATTERN is set to the default value: ${ARCH_PATTERN}" + else + echo "ARCH_PATTERN is set to ${ARCH_PATTERN} by user, reset it to ${ARCH_PATTERN}/acllib" + export ARCH_PATTERN=${ARCH_PATTERN}/acllib + fi + + path_build=$path_cur/build + preparePath $path_build + cmake .. + make -j + ret=$? + cd .. + return ${ret} +} + +# set ASCEND_VERSION to ascend-toolkit/latest when it was not specified by user +if [ ! "${ASCEND_VERSION}" ]; then + export ASCEND_VERSION=ascend-toolkit/latest + echo "Set ASCEND_VERSION to the default value: ${ASCEND_VERSION}" +else + echo "ASCEND_VERSION is set to ${ASCEND_VERSION} by user" +fi + +buildA300 + +if [ $? -ne 0 ]; then + exit 1 +fi diff --git a/model_zoo/official/cv/vgg16/ascend310_quant_infer/src/main.cpp b/model_zoo/official/cv/vgg16/ascend310_quant_infer/src/main.cpp new file mode 100644 index 00000000000..80165505f44 --- /dev/null +++ b/model_zoo/official/cv/vgg16/ascend310_quant_infer/src/main.cpp @@ -0,0 +1,42 @@ +/** + * 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 "../inc/sample_process.h" +#include "../inc/utils.h" +bool g_is_device = false; + +int main(int argc, char **argv) { + if (argc != 3) { + ERROR_LOG("usage:./main path_of_om path_of_inputFolder"); + return FAILED; + } + SampleProcess processSample; + Result ret = processSample.InitResource(); + if (ret != SUCCESS) { + ERROR_LOG("sample init resource failed"); + return FAILED; + } + + ret = processSample.Process(argv[1], argv[2]); + if (ret != SUCCESS) { + ERROR_LOG("sample process failed"); + return FAILED; + } + + INFO_LOG("execute sample success"); + return SUCCESS; +} diff --git a/model_zoo/official/cv/vgg16/ascend310_quant_infer/src/model_process.cpp b/model_zoo/official/cv/vgg16/ascend310_quant_infer/src/model_process.cpp new file mode 100644 index 00000000000..913c5989f4b --- /dev/null +++ b/model_zoo/official/cv/vgg16/ascend310_quant_infer/src/model_process.cpp @@ -0,0 +1,339 @@ +/** + * 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 "../inc/model_process.h" +#include +#include +#include +#include +#include "../inc/utils.h" +extern bool g_is_device; + +ModelProcess::ModelProcess() :modelId_(0), modelMemSize_(0), modelWeightSize_(0), modelMemPtr_(nullptr), +modelWeightPtr_(nullptr), loadFlag_(false), modelDesc_(nullptr), input_(nullptr), output_(nullptr) { +} + +ModelProcess::~ModelProcess() { + Unload(); + DestroyDesc(); + DestroyInput(); + DestroyOutput(); +} + +Result ModelProcess::LoadModelFromFileWithMem(const char *modelPath) { + if (loadFlag_) { + ERROR_LOG("has already loaded a model"); + return FAILED; + } + + aclError ret = aclmdlQuerySize(modelPath, &modelMemSize_, &modelWeightSize_); + if (ret != ACL_ERROR_NONE) { + ERROR_LOG("query model failed, model file is %s", modelPath); + return FAILED; + } + + ret = aclrtMalloc(&modelMemPtr_, modelMemSize_, ACL_MEM_MALLOC_HUGE_FIRST); + if (ret != ACL_ERROR_NONE) { + ERROR_LOG("malloc buffer for mem failed, require size is %zu", modelMemSize_); + return FAILED; + } + + ret = aclrtMalloc(&modelWeightPtr_, modelWeightSize_, ACL_MEM_MALLOC_HUGE_FIRST); + if (ret != ACL_ERROR_NONE) { + ERROR_LOG("malloc buffer for weight failed, require size is %zu", modelWeightSize_); + return FAILED; + } + + ret = aclmdlLoadFromFileWithMem(modelPath, &modelId_, modelMemPtr_, + modelMemSize_, modelWeightPtr_, modelWeightSize_); + if (ret != ACL_ERROR_NONE) { + ERROR_LOG("load model from file failed, model file is %s", modelPath); + return FAILED; + } + + loadFlag_ = true; + INFO_LOG("load model %s success", modelPath); + return SUCCESS; +} + +Result ModelProcess::CreateDesc() { + modelDesc_ = aclmdlCreateDesc(); + if (modelDesc_ == nullptr) { + ERROR_LOG("create model description failed"); + return FAILED; + } + + aclError ret = aclmdlGetDesc(modelDesc_, modelId_); + if (ret != ACL_ERROR_NONE) { + ERROR_LOG("get model description failed"); + return FAILED; + } + + INFO_LOG("create model description success"); + + return SUCCESS; +} + +void ModelProcess::DestroyDesc() { + if (modelDesc_ != nullptr) { + (void)aclmdlDestroyDesc(modelDesc_); + modelDesc_ = nullptr; + } +} + +Result ModelProcess::CreateInput(void *inputDataBuffer, size_t bufferSize) { + input_ = aclmdlCreateDataset(); + if (input_ == nullptr) { + ERROR_LOG("can't create dataset, create input failed"); + return FAILED; + } + + aclDataBuffer* inputData = aclCreateDataBuffer(inputDataBuffer, bufferSize); + if (inputData == nullptr) { + ERROR_LOG("can't create data buffer, create input failed"); + return FAILED; + } + + aclError ret = aclmdlAddDatasetBuffer(input_, inputData); + if (ret != ACL_ERROR_NONE) { + ERROR_LOG("add input dataset buffer failed"); + aclDestroyDataBuffer(inputData); + inputData = nullptr; + return FAILED; + } + + return SUCCESS; +} + +void ModelProcess::DestroyInput() { + if (input_ == nullptr) { + return; + } + + for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(input_); ++i) { + aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(input_, i); + aclDestroyDataBuffer(dataBuffer); + } + aclmdlDestroyDataset(input_); + input_ = nullptr; +} + +Result ModelProcess::CreateOutput() { + if (modelDesc_ == nullptr) { + ERROR_LOG("no model description, create output failed"); + return FAILED; + } + + output_ = aclmdlCreateDataset(); + if (output_ == nullptr) { + ERROR_LOG("can't create dataset, create output failed"); + return FAILED; + } + + size_t outputSize = aclmdlGetNumOutputs(modelDesc_); + for (size_t i = 0; i < outputSize; ++i) { + size_t buffer_size = aclmdlGetOutputSizeByIndex(modelDesc_, i); + + void *outputBuffer = nullptr; + aclError ret = aclrtMalloc(&outputBuffer, buffer_size, ACL_MEM_MALLOC_NORMAL_ONLY); + if (ret != ACL_ERROR_NONE) { + ERROR_LOG("can't malloc buffer, size is %zu, create output failed", buffer_size); + return FAILED; + } + + aclDataBuffer* outputData = aclCreateDataBuffer(outputBuffer, buffer_size); + if (ret != ACL_ERROR_NONE) { + ERROR_LOG("can't create data buffer, create output failed"); + aclrtFree(outputBuffer); + return FAILED; + } + + ret = aclmdlAddDatasetBuffer(output_, outputData); + if (ret != ACL_ERROR_NONE) { + ERROR_LOG("can't add data buffer, create output failed"); + aclrtFree(outputBuffer); + aclDestroyDataBuffer(outputData); + return FAILED; + } + } + + INFO_LOG("create model output success"); + return SUCCESS; +} + +void ModelProcess::DumpModelOutputResult(char *output_name) { + size_t outputNum = aclmdlGetDatasetNumBuffers(output_); + + for (size_t i = 0; i < outputNum; ++i) { + std::stringstream ss; + ss << "result/" << output_name << "_output_" << i << ".bin"; + std::string outputFileName = ss.str(); + FILE *outputFile = fopen(outputFileName.c_str(), "wb"); + if (outputFile) { + aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); + void* data = aclGetDataBufferAddr(dataBuffer); + uint32_t len = aclGetDataBufferSizeV2(dataBuffer); + + void* outHostData = NULL; + aclError ret = ACL_ERROR_NONE; + if (!g_is_device) { + ret = aclrtMallocHost(&outHostData, len); + if (ret != ACL_ERROR_NONE) { + ERROR_LOG("aclrtMallocHost failed, ret[%d]", ret); + return; + } + + ret = aclrtMemcpy(outHostData, len, data, len, ACL_MEMCPY_DEVICE_TO_HOST); + if (ret != ACL_ERROR_NONE) { + ERROR_LOG("aclrtMemcpy failed, ret[%d]", ret); + (void)aclrtFreeHost(outHostData); + return; + } + + fwrite(outHostData, len, sizeof(char), outputFile); + + ret = aclrtFreeHost(outHostData); + if (ret != ACL_ERROR_NONE) { + ERROR_LOG("aclrtFreeHost failed, ret[%d]", ret); + return; + } + } else { + fwrite(data, len, sizeof(char), outputFile); + } + fclose(outputFile); + outputFile = nullptr; + } else { + ERROR_LOG("create output file [%s] failed", outputFileName.c_str()); + return; + } + } + + INFO_LOG("dump data success"); + return; +} + +void ModelProcess::OutputModelResult() { + for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(output_); ++i) { + aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); + void* data = aclGetDataBufferAddr(dataBuffer); + uint32_t len = aclGetDataBufferSizeV2(dataBuffer); + + void *outHostData = NULL; + aclError ret = ACL_ERROR_NONE; + float *outData = NULL; + if (!g_is_device) { + ret = aclrtMallocHost(&outHostData, len); + if (ret != ACL_ERROR_NONE) { + ERROR_LOG("aclrtMallocHost failed, ret[%d]", ret); + return; + } + + ret = aclrtMemcpy(outHostData, len, data, len, ACL_MEMCPY_DEVICE_TO_HOST); + if (ret != ACL_ERROR_NONE) { + ERROR_LOG("aclrtMemcpy failed, ret[%d]", ret); + return; + } + + outData = reinterpret_cast(outHostData); + } else { + outData = reinterpret_cast(data); + } + std::map > resultMap; + for (unsigned int j = 0; j < len / sizeof(float); ++j) { + resultMap[*outData] = j; + outData++; + } + + int cnt = 0; + for (auto it = resultMap.begin(); it != resultMap.end(); ++it) { + // print top 5 + if (++cnt > 5) { + break; + } + + INFO_LOG("top %d: index[%d] value[%lf]", cnt, it->second, it->first); + } + if (!g_is_device) { + ret = aclrtFreeHost(outHostData); + if (ret != ACL_ERROR_NONE) { + ERROR_LOG("aclrtFreeHost failed, ret[%d]", ret); + return; + } + } + } + + INFO_LOG("output data success"); + return; +} + +void ModelProcess::DestroyOutput() { + if (output_ == nullptr) { + return; + } + + for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(output_); ++i) { + aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); + void* data = aclGetDataBufferAddr(dataBuffer); + (void)aclrtFree(data); + (void)aclDestroyDataBuffer(dataBuffer); + } + + (void)aclmdlDestroyDataset(output_); + output_ = nullptr; +} + +Result ModelProcess::Execute() { + aclError ret = aclmdlExecute(modelId_, input_, output_); + if (ret != ACL_ERROR_NONE) { + ERROR_LOG("execute model failed, modelId is %u", modelId_); + return FAILED; + } + + INFO_LOG("model execute success"); + return SUCCESS; +} + +void ModelProcess::Unload() { + if (!loadFlag_) { + WARN_LOG("no model had been loaded, unload failed"); + return; + } + + aclError ret = aclmdlUnload(modelId_); + if (ret != ACL_ERROR_NONE) { + ERROR_LOG("unload model failed, modelId is %u", modelId_); + } + + if (modelDesc_ != nullptr) { + (void)aclmdlDestroyDesc(modelDesc_); + modelDesc_ = nullptr; + } + + if (modelMemPtr_ != nullptr) { + aclrtFree(modelMemPtr_); + modelMemPtr_ = nullptr; + modelMemSize_ = 0; + } + + if (modelWeightPtr_ != nullptr) { + aclrtFree(modelWeightPtr_); + modelWeightPtr_ = nullptr; + modelWeightSize_ = 0; + } + + loadFlag_ = false; + INFO_LOG("unload model success, modelId is %u", modelId_); +} diff --git a/model_zoo/official/cv/vgg16/ascend310_quant_infer/src/sample_process.cpp b/model_zoo/official/cv/vgg16/ascend310_quant_infer/src/sample_process.cpp new file mode 100644 index 00000000000..6d0dc6cc6af --- /dev/null +++ b/model_zoo/official/cv/vgg16/ascend310_quant_infer/src/sample_process.cpp @@ -0,0 +1,256 @@ +/** + * 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 "../inc/sample_process.h" +#include +#include +#include +#include +#include +#include +#include "../inc/model_process.h" +#include "acl/acl.h" +#include "../inc/utils.h" +extern bool g_is_device; +using std::string; +using std::vector; + +SampleProcess::SampleProcess() :deviceId_(0), context_(nullptr), stream_(nullptr) { +} + +SampleProcess::~SampleProcess() { + DestroyResource(); +} + +Result SampleProcess::InitResource() { + // ACL init + + const char *aclConfigPath = "./src/acl.json"; + aclError ret = aclInit(aclConfigPath); + if (ret != ACL_ERROR_NONE) { + ERROR_LOG("acl init failed"); + return FAILED; + } + INFO_LOG("acl init success"); + + // open device + ret = aclrtSetDevice(deviceId_); + if (ret != ACL_ERROR_NONE) { + ERROR_LOG("acl open device %d failed", deviceId_); + return FAILED; + } + INFO_LOG("open device %d success", deviceId_); + + // create context (set current) + ret = aclrtCreateContext(&context_, deviceId_); + if (ret != ACL_ERROR_NONE) { + ERROR_LOG("acl create context failed"); + return FAILED; + } + INFO_LOG("create context success"); + + // create stream + ret = aclrtCreateStream(&stream_); + if (ret != ACL_ERROR_NONE) { + ERROR_LOG("acl create stream failed"); + return FAILED; + } + INFO_LOG("create stream success"); + + // get run mode + aclrtRunMode runMode; + ret = aclrtGetRunMode(&runMode); + if (ret != ACL_ERROR_NONE) { + ERROR_LOG("acl get run mode failed"); + return FAILED; + } + g_is_device = (runMode == ACL_DEVICE); + INFO_LOG("get run mode success"); + return SUCCESS; +} + +void SampleProcess::GetAllFiles(std::string path, std::vector *files) { + DIR *pDir = NULL; + struct dirent* ptr; + if (!(pDir = opendir(path.c_str()))) { + return; + } + while ((ptr = readdir(pDir)) != 0) { + if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0) { + files->push_back(path + "/" + ptr->d_name); + } + } + closedir(pDir); +} + +Result SampleProcess::Process(char *om_path, char *input_folder) { + // model init + double second_to_millisecond = 1000; + double second_to_microsecond = 1000000; + + double whole_cost_time = 0.0; + struct timeval start_global = {0}; + struct timeval end_global = {0}; + double startTimeMs_global = 0.0; + double endTimeMs_global = 0.0; + + gettimeofday(&start_global, nullptr); + + ModelProcess processModel; + const char* omModelPath = om_path; + + Result ret = processModel.LoadModelFromFileWithMem(omModelPath); + if (ret != SUCCESS) { + ERROR_LOG("execute LoadModelFromFileWithMem failed"); + return FAILED; + } + + ret = processModel.CreateDesc(); + if (ret != SUCCESS) { + ERROR_LOG("execute CreateDesc failed"); + return FAILED; + } + + ret = processModel.CreateOutput(); + if (ret != SUCCESS) { + ERROR_LOG("execute CreateOutput failed"); + return FAILED; + } + + std::vector testFile; + GetAllFiles(input_folder, &testFile); + + if (testFile.size() == 0) { + WARN_LOG("no input data under folder"); + } + + // loop begin + + double model_cost_time = 0.0; + double edge_to_edge_model_cost_time = 0.0; + + for (size_t index = 0; index < testFile.size(); ++index) { + INFO_LOG("start to process file:%s", testFile[index].c_str()); + // model process + + struct timeval time_init = {0}; + double timeval_init = 0.0; + gettimeofday(&time_init, nullptr); + timeval_init = (time_init.tv_sec * second_to_microsecond + time_init.tv_usec) / second_to_millisecond; + + uint32_t devBufferSize; + void *picDevBuffer = Utils::GetDeviceBufferOfFile(testFile[index], &devBufferSize); + if (picDevBuffer == nullptr) { + ERROR_LOG("get pic device buffer failed,index is %zu", index); + return FAILED; + } + ret = processModel.CreateInput(picDevBuffer, devBufferSize); + if (ret != SUCCESS) { + ERROR_LOG("execute CreateInput failed"); + aclrtFree(picDevBuffer); + return FAILED; + } + + struct timeval start = {0}; + struct timeval end = {0}; + double startTimeMs = 0.0; + double endTimeMs = 0.0; + gettimeofday(&start, nullptr); + startTimeMs = (start.tv_sec * second_to_microsecond + start.tv_usec) / second_to_millisecond; + + ret = processModel.Execute(); + + gettimeofday(&end, nullptr); + endTimeMs = (end.tv_sec * second_to_microsecond + end.tv_usec) / second_to_millisecond; + + double cost_time = endTimeMs - startTimeMs; + INFO_LOG("model infer time: %lf ms", cost_time); + + model_cost_time += cost_time; + + double edge_to_edge_cost_time = endTimeMs - timeval_init; + edge_to_edge_model_cost_time += edge_to_edge_cost_time; + + if (ret != SUCCESS) { + ERROR_LOG("execute inference failed"); + aclrtFree(picDevBuffer); + return FAILED; + } + + int pos = testFile[index].find_last_of('/'); + std::string name = testFile[index].substr(pos+1); + std::string outputname = name.substr(0, name.rfind(".")); + + // dump output result to file in the current directory + processModel.DumpModelOutputResult(const_cast(outputname.c_str())); + + // release model input buffer + aclrtFree(picDevBuffer); + processModel.DestroyInput(); + } + double test_file_size = 0.0; + test_file_size = testFile.size(); + INFO_LOG("infer dataset size:%lf", test_file_size); + + gettimeofday(&end_global, nullptr); + startTimeMs_global = (start_global.tv_sec * second_to_microsecond + start_global.tv_usec) / second_to_millisecond; + endTimeMs_global = (end_global.tv_sec * second_to_microsecond + end_global.tv_usec) / second_to_millisecond; + whole_cost_time = (endTimeMs_global - startTimeMs_global) / test_file_size; + + model_cost_time /= test_file_size; + INFO_LOG("model cost time per sample: %lf ms", model_cost_time); + edge_to_edge_model_cost_time /= test_file_size; + INFO_LOG("edge-to-edge model cost time per sample:%lf ms", edge_to_edge_model_cost_time); + INFO_LOG("whole cost time per sample: %lf ms", whole_cost_time); + + // loop end + + return SUCCESS; +} + +void SampleProcess::DestroyResource() { + aclError ret; + if (stream_ != nullptr) { + ret = aclrtDestroyStream(stream_); + if (ret != ACL_ERROR_NONE) { + ERROR_LOG("destroy stream failed"); + } + stream_ = nullptr; + } + INFO_LOG("end to destroy stream"); + + if (context_ != nullptr) { + ret = aclrtDestroyContext(context_); + if (ret != ACL_ERROR_NONE) { + ERROR_LOG("destroy context failed"); + } + context_ = nullptr; + } + INFO_LOG("end to destroy context"); + + ret = aclrtResetDevice(deviceId_); + if (ret != ACL_ERROR_NONE) { + ERROR_LOG("reset device failed"); + } + INFO_LOG("end to reset device is %d", deviceId_); + + ret = aclFinalize(); + if (ret != ACL_ERROR_NONE) { + ERROR_LOG("finalize acl failed"); + } + INFO_LOG("end to finalize acl"); +} + diff --git a/model_zoo/official/cv/vgg16/ascend310_quant_infer/src/utils.cpp b/model_zoo/official/cv/vgg16/ascend310_quant_infer/src/utils.cpp new file mode 100644 index 00000000000..d9208c8cfd9 --- /dev/null +++ b/model_zoo/official/cv/vgg16/ascend310_quant_infer/src/utils.cpp @@ -0,0 +1,113 @@ +/** + * 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 "../inc/utils.h" +#include +#include +#include +#include +#include "acl/acl.h" + +extern bool g_is_device; + +void* Utils::ReadBinFile(std::string fileName, uint32_t *fileSize) { + struct stat sBuf; + int fileStatus = stat(fileName.data(), &sBuf); + if (fileStatus == -1) { + ERROR_LOG("failed to get file"); + return nullptr; + } + if (S_ISREG(sBuf.st_mode) == 0) { + ERROR_LOG("%s is not a file, please enter a file", fileName.c_str()); + return nullptr; + } + + std::ifstream binFile(fileName, std::ifstream::binary); + if (binFile.is_open() == false) { + ERROR_LOG("open file %s failed", fileName.c_str()); + return nullptr; + } + + binFile.seekg(0, binFile.end); + uint32_t binFileBufferLen = binFile.tellg(); + if (binFileBufferLen == 0) { + ERROR_LOG("binfile is empty, filename is %s", fileName.c_str()); + binFile.close(); + return nullptr; + } + + binFile.seekg(0, binFile.beg); + + void* binFileBufferData = nullptr; + aclError ret = ACL_ERROR_NONE; + if (!g_is_device) { + ret = aclrtMallocHost(&binFileBufferData, binFileBufferLen); + if (ret != ACL_ERROR_NONE) { + ERROR_LOG("malloc for binFileBufferData failed"); + binFile.close(); + return nullptr; + } + if (binFileBufferData == nullptr) { + ERROR_LOG("malloc binFileBufferData failed"); + binFile.close(); + return nullptr; + } + } else { + ret = aclrtMalloc(&binFileBufferData, binFileBufferLen, ACL_MEM_MALLOC_NORMAL_ONLY); + if (ret != ACL_ERROR_NONE) { + ERROR_LOG("malloc device buffer failed. size is %u", binFileBufferLen); + binFile.close(); + return nullptr; + } + } + binFile.read(static_cast(binFileBufferData), binFileBufferLen); + binFile.close(); + *fileSize = binFileBufferLen; + return binFileBufferData; +} + +void* Utils::GetDeviceBufferOfFile(std::string fileName, uint32_t *fileSize) { + uint32_t inputHostBuffSize = 0; + void* inputHostBuff = Utils::ReadBinFile(fileName, &inputHostBuffSize); + if (inputHostBuff == nullptr) { + return nullptr; + } + if (!g_is_device) { + void *inBufferDev = nullptr; + uint32_t inBufferSize = inputHostBuffSize; + aclError ret = aclrtMalloc(&inBufferDev, inBufferSize, ACL_MEM_MALLOC_NORMAL_ONLY); + if (ret != ACL_ERROR_NONE) { + ERROR_LOG("malloc device buffer failed. size is %u", inBufferSize); + aclrtFreeHost(inputHostBuff); + return nullptr; + } + + ret = aclrtMemcpy(inBufferDev, inBufferSize, inputHostBuff, inputHostBuffSize, ACL_MEMCPY_HOST_TO_DEVICE); + if (ret != ACL_ERROR_NONE) { + ERROR_LOG("memcpy failed. device buffer size is %u, input host buffer size is %u", + inBufferSize, inputHostBuffSize); + aclrtFree(inBufferDev); + aclrtFreeHost(inputHostBuff); + return nullptr; + } + aclrtFreeHost(inputHostBuff); + *fileSize = inBufferSize; + return inBufferDev; + } else { + *fileSize = inputHostBuffSize; + return inputHostBuff; + } +}