!29213 [MS][LITE][develop] sync c api demo, benchmark and ut

Merge pull request !29213 from sunsuodong/code_docs_c_api_C
This commit is contained in:
i-robot 2022-01-17 12:40:19 +00:00 committed by Gitee
commit d25a40240a
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
9 changed files with 876 additions and 1 deletions

View File

@ -61,4 +61,6 @@
"mindspore/mindspore/lite/src/ops/ops_def.cc" "runtime/int"
"mindspore/mindspore/lite/examples/runtime_gpu_extend/src/cl" "legal/copyright"
"mindspore/mindspore/lite/examples/runtime_gpu_extend/src/cl" "readability/casting"
"mindspore/mindspore/lite/examples/runtime_gpu_extend/src/cl" "readability/fn_size"
"mindspore/mindspore/lite/examples/runtime_gpu_extend/src/cl" "readability/fn_size"
"mindspore/mindspore/lite/examples/quick_start_c/main.c" "readability/casting"
"mindspore/mindspore/lite/examples/quick_start_c/main.c" "runtime/threadsafe_fn"

View File

@ -0,0 +1,30 @@
cmake_minimum_required(VERSION 3.14)
project(QuickStartC)
if(CMAKE_C_COMPILER_ID STREQUAL "GNU" AND CMAKE_C_COMPILER_VERSION VERSION_LESS 7.3.0)
message(FATAL_ERROR "GCC version ${CMAKE_C_COMPILER_VERSION} must not be less than 7.3.0")
endif()
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/lib)
add_executable(mindspore_quick_start_c main.c)
target_link_libraries(
mindspore_quick_start_c
-Wl,--whole-archive mindspore-lite -Wl,--no-whole-archive
pthread
)
if(WIN32)
target_link_libraries(
mindspore_quick_start_c
ssp
)
else()
target_link_libraries(
mindspore_quick_start_c
dl
)
endif()

View File

@ -0,0 +1,26 @@
@rem Copyright 2021 Huawei Technologies Co., Ltd
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem http://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem ============================================================================
@echo off
@title mindspore_lite_quick_start_c_demo_build
SET BASEPATH=%CD%
IF NOT EXIST "%BASEPATH%/build" (
md build
)
cd %BASEPATH%/build
cmake -G "CodeBlocks - MinGW Makefiles" %BASEPATH%
cmake --build .

View File

@ -0,0 +1,30 @@
#!/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.
# ============================================================================
set -e
BASEPATH=$(cd "$(dirname $0)"; pwd)
MODEL_DOWNLOAD_URL="https://download.mindspore.cn/model_zoo/official/lite/quick_start/mobilenetv2.ms"
if [ ! -e ${BASEPATH}/model/mobilenetv2.ms ]; then
mkdir -p model
wget -c -O ${BASEPATH}/model/mobilenetv2.ms --no-check-certificate ${MODEL_DOWNLOAD_URL}
fi
mkdir -p build
cd build
cmake ..
make

View File

@ -0,0 +1,121 @@
/**
* 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 <stdlib.h>
#include <stdio.h>
#include "include/c_api/model_c.h"
int GenerateInputDataWithRandom(MSTensorHandleArray inputs) {
for (size_t i = 0; i < inputs.handle_num; ++i) {
float *input_data = (float *)MSTensorGetMutableData(inputs.handle_list[i]);
if (input_data == NULL) {
printf("MSTensorGetMutableData failed.\n");
return kMSStatusLiteError;
}
int64_t num = MSTensorGetElementNum(inputs.handle_list[i]);
const int divisor = 10;
for (size_t j = 0; j < num; j++) {
input_data[j] = (float)(rand() % divisor) / divisor; // 0--0.9f
}
}
return kMSStatusSuccess;
}
int QuickStart(int argc, const char **argv) {
if (argc < 2) {
printf("Model file must be provided.\n");
return kMSStatusLiteError;
}
// Create and init context, add CPU device info
MSContextHandle context = MSContextCreate();
if (context == NULL) {
printf("MSContextCreate failed.\n");
return kMSStatusLiteError;
}
const int thread_num = 2;
MSContextSetThreadNum(context, thread_num);
MSContextSetThreadAffinityMode(context, 1);
MSDeviceInfoHandle cpu_device_info = MSDeviceInfoCreate(kMSDeviceTypeCPU);
if (cpu_device_info == NULL) {
printf("MSDeviceInfoCreate failed.\n");
MSContextDestroy(&context);
return kMSStatusLiteError;
}
MSDeviceInfoSetEnableFP16(cpu_device_info, false);
MSContextAddDeviceInfo(context, cpu_device_info);
// Create model
MSModelHandle model = MSModelCreate();
if (model == NULL) {
printf("MSModelCreate failed.\n");
MSContextDestroy(&context);
return kMSStatusLiteError;
}
// Build model
int ret = MSModelBuildFromFile(model, argv[1], kMSModelTypeMindIR, context);
if (ret != kMSStatusSuccess) {
printf("MSModelBuildFromFile failed, ret: %d.\n", ret);
MSModelDestroy(&model);
return ret;
}
// Get Inputs
MSTensorHandleArray inputs = MSModelGetInputs(model);
if (inputs.handle_list == NULL) {
printf("MSModelGetInputs failed, ret: %d.\n", ret);
MSModelDestroy(&model);
return ret;
}
// Generate random data as input data.
ret = GenerateInputDataWithRandom(inputs);
if (ret != kMSStatusSuccess) {
printf("GenerateInputDataWithRandom failed, ret: %d.\n", ret);
MSModelDestroy(&model);
return ret;
}
// Model Predict
MSTensorHandleArray outputs;
ret = MSModelPredict(model, inputs, &outputs, NULL, NULL);
if (ret != kMSStatusSuccess) {
printf("MSModelPredict failed, ret: %d.\n", ret);
MSModelDestroy(&model);
return ret;
}
// Print Output Tensor Data.
for (size_t i = 0; i < inputs.handle_num; ++i) {
MSTensorHandle tensor = inputs.handle_list[i];
int64_t element_num = MSTensorGetElementNum(tensor);
printf("Tensor name: %s, elements num: %ld.\n", MSTensorGetName(tensor), element_num);
const float *data = (const float *)MSTensorGetData(tensor);
printf("output data is:\n");
const int max_print_num = 50;
for (int j = 0; j < element_num && j <= max_print_num; ++j) {
printf("%f ", data[i]);
}
printf("\n");
}
// Delete model.
MSModelDestroy(&model);
return kMSStatusSuccess;
}
int main(int argc, const char **argv) { return QuickStart(argc, argv); }

View File

@ -0,0 +1,73 @@
/**
* 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/c_api/context_c.h"
#include "common/common_test.h"
namespace mindspore {
class ContextCTest : public mindspore::CommonTest {
public:
ContextCTest() {}
};
TEST_F(ContextCTest, common_test) {
MSDeviceInfoHandle npu_device_info = MSDeviceInfoCreate(kMSDeviceTypeKirinNPU);
ASSERT_TRUE(npu_device_info != nullptr);
ASSERT_EQ(MSDeviceInfoGetDeviceType(npu_device_info), kMSDeviceTypeKirinNPU);
MSDeviceInfoSetProvider(npu_device_info, "vendor name");
ASSERT_STREQ(MSDeviceInfoGetProvider(npu_device_info), "vendor name");
MSDeviceInfoSetProviderDevice(npu_device_info, "npu_a");
ASSERT_STREQ(MSDeviceInfoGetProviderDevice(npu_device_info), "npu_a");
MSDeviceInfoSetFrequency(npu_device_info, 3);
ASSERT_EQ(MSDeviceInfoGetFrequency(npu_device_info), 3);
MSContextHandle context = MSContextCreate();
ASSERT_TRUE(context != nullptr);
MSContextSetThreadNum(context, 4);
ASSERT_EQ(MSContextGetThreadNum(context), 4);
MSContextSetThreadAffinityMode(context, 2);
ASSERT_EQ(MSContextGetThreadAffinityMode(context), 2);
constexpr size_t core_num = 4;
int32_t core_list[core_num] = {1, 3, 2, 0};
MSContextSetThreadAffinityCoreList(context, core_list, core_num);
size_t ret_core_num;
const int32_t *ret_core_list = nullptr;
ret_core_list = MSContextGetThreadAffinityCoreList(context, &ret_core_num);
ASSERT_EQ(ret_core_num, core_num);
for (size_t i = 0; i < ret_core_num; i++) {
ASSERT_EQ(ret_core_list[i], core_list[i]);
}
MSContextSetEnableParallel(context, true);
ASSERT_EQ(MSContextGetEnableParallel(context), true);
MSDeviceInfoHandle cpu_device_info = MSDeviceInfoCreate(kMSDeviceTypeCPU);
MSDeviceInfoDestroy(&cpu_device_info);
cpu_device_info = MSDeviceInfoCreate(kMSDeviceTypeCPU);
MSDeviceInfoSetEnableFP16(cpu_device_info, true);
ASSERT_EQ(MSDeviceInfoGetEnableFP16(cpu_device_info), true);
MSContextAddDeviceInfo(context, cpu_device_info);
MSContextAddDeviceInfo(context, npu_device_info);
MSContextDestroy(&context);
}
} // namespace mindspore

View File

@ -0,0 +1,89 @@
/**
* 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/c_api/tensor_c.h"
#include "common/common_test.h"
namespace mindspore {
class TensorCTest : public mindspore::CommonTest {
public:
TensorCTest() {}
};
TEST_F(TensorCTest, common_test) {
constexpr size_t shape_num = 2;
int64_t shape[shape_num] = {2, 3};
MSTensorHandle tensor = MSTensorCreate("name001", kMSDataTypeNumberTypeInt32, shape, shape_num, nullptr, 0);
ASSERT_TRUE(tensor != nullptr);
ASSERT_STREQ(MSTensorGetName(tensor), "name001");
ASSERT_EQ(MSTensorGetDataType(tensor), kMSDataTypeNumberTypeInt32);
size_t ret_shape_num;
const int64_t *ret_shape = MSTensorGetShape(tensor, &ret_shape_num);
ASSERT_EQ(ret_shape_num, shape_num);
for (size_t i = 0; i < ret_shape_num; i++) {
ASSERT_EQ(ret_shape[i], shape[i]);
}
ASSERT_EQ(MSTensorGetElementNum(tensor), 6);
ASSERT_EQ(MSTensorGetDataSize(tensor), 6 * sizeof(int32_t));
ASSERT_EQ(MSTensorGetData(tensor), nullptr);
ASSERT_TRUE(MSTensorGetMutableData(tensor) != nullptr);
MSTensorSetName(tensor, "name002");
ASSERT_STREQ(MSTensorGetName(tensor), "name002");
MSTensorSetDataType(tensor, kMSDataTypeNumberTypeFloat32);
ASSERT_EQ(MSTensorGetDataType(tensor), kMSDataTypeNumberTypeFloat32);
constexpr size_t new_shape_num = 4;
int64_t new_shape[new_shape_num] = {1, 2, 3, 1};
MSTensorSetShape(tensor, new_shape, new_shape_num);
size_t new_ret_shape_num;
const int64_t *new_ret_shape = MSTensorGetShape(tensor, &new_ret_shape_num);
ASSERT_EQ(new_ret_shape_num, new_shape_num);
for (size_t i = 0; i < new_ret_shape_num; i++) {
ASSERT_EQ(new_ret_shape[i], new_shape[i]);
}
MSTensorSetFormat(tensor, kMSFormatNCHW);
ASSERT_EQ(MSTensorGetFormat(tensor), kMSFormatNCHW);
constexpr size_t data_len = 6;
ASSERT_EQ(MSTensorGetElementNum(tensor), data_len);
ASSERT_EQ(MSTensorGetDataSize(tensor), data_len * sizeof(float));
float data[data_len] = {1, 2, 3, 4, 5, 6};
MSTensorSetData(tensor, data);
const float *ret_data = static_cast<const float *>(MSTensorGetData(tensor));
for (size_t i = 0; i < data_len; i++) {
ASSERT_EQ(ret_data[i], data[i]);
}
MSTensorHandle clone = MSTensorClone(tensor);
ASSERT_TRUE(clone != nullptr);
ASSERT_STREQ(MSTensorGetName(clone), "");
ASSERT_EQ(MSTensorGetDataType(clone), kMSDataTypeNumberTypeFloat32);
size_t clone_shape_num;
const int64_t *clone_shape = MSTensorGetShape(clone, &clone_shape_num);
ASSERT_EQ(clone_shape_num, new_ret_shape_num);
for (size_t i = 0; i < clone_shape_num; i++) {
ASSERT_EQ(clone_shape[i], new_ret_shape[i]);
}
ASSERT_EQ(MSTensorGetElementNum(clone), MSTensorGetElementNum(tensor));
ASSERT_EQ(MSTensorGetDataSize(clone), MSTensorGetDataSize(tensor));
ASSERT_TRUE(MSTensorGetData(clone) != MSTensorGetData(tensor));
MSTensorDestroy(&tensor);
MSTensorDestroy(&clone);
}
} // namespace mindspore

View File

@ -0,0 +1,428 @@
/**
* 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 "tools/benchmark/benchmark_c_api.h"
#include <algorithm>
#include <map>
#include <string>
#include <utility>
using mindspore::lite::GetTimeUs;
using mindspore::lite::kFloatMSEC;
using mindspore::lite::RET_ERROR;
using mindspore::lite::RET_OK;
namespace mindspore {
namespace tools {
int BenchmarkCApi::RunBenchmark() {
auto start_prepare_time = GetTimeUs();
int ret = InitContext();
if (ret != RET_OK) {
BENCHMARK_LOG_ERROR("InitContext failed, ret: " << ret);
return ret;
}
model_ = MSModelCreate();
ret = MSModelBuildFromFile(model_, flags_->model_file_.c_str(), kMSModelTypeMindIR, context_);
if (ret != kMSStatusSuccess) {
BENCHMARK_LOG_ERROR("MSModelBuildFromFile failed, ret: " << ret);
return ret;
}
inputs_ = MSModelGetInputs(model_);
if (inputs_.handle_list == nullptr) {
BENCHMARK_LOG_ERROR("MSModelGetInputs failed, ret: " << ret);
return ret;
}
if (!flags_->resize_dims_.empty()) {
std::vector<MSShapeInfo> shape_infos;
std::transform(flags_->resize_dims_.begin(), flags_->resize_dims_.end(), std::back_inserter(shape_infos),
[&](auto &shapes) {
MSShapeInfo shape_info;
shape_info.shape_num = shapes.size();
for (size_t i = 0; i < shape_info.shape_num; i++) {
shape_info.shape[i] = shapes[i];
}
return shape_info;
});
ret = MSModelResize(model_, inputs_, shape_infos.data(), inputs_.handle_num);
if (ret != kMSStatusSuccess) {
BENCHMARK_LOG_ERROR("MSModelResize failed, ret: " << ret);
return ret;
}
}
auto end_prepare_time = GetTimeUs();
MS_LOG(INFO) << "PrepareTime = " << ((end_prepare_time - start_prepare_time) / kFloatMSEC) << " ms";
std::cout << "PrepareTime = " << ((end_prepare_time - start_prepare_time) / kFloatMSEC) << " ms" << std::endl;
ret = LoadInput();
if (ret != kMSStatusSuccess) {
BENCHMARK_LOG_ERROR("LoadInput failed, ret: " << ret)
return ret;
}
if (!flags_->benchmark_data_file_.empty()) {
ret = MarkAccuracy();
} else {
ret = MarkPerformance();
}
if (ret != kMSStatusSuccess) {
BENCHMARK_LOG_ERROR("Run failed, ret: " << ret);
return ret;
}
if (flags_->dump_tensor_data_) {
BENCHMARK_LOG_ERROR("Dumped file is saved to : " + dump_file_output_dir_)
}
return RET_OK;
}
int BenchmarkCApi::InitContext() {
constexpr int kFrequencyDefault = 3;
context_ = MSContextCreate();
if (context_ == nullptr) {
BENCHMARK_LOG_ERROR("MSContextCreate failed");
return RET_ERROR;
}
MSContextSetThreadNum(context_, flags_->num_threads_);
MSContextSetEnableParallel(context_, flags_->enable_parallel_);
MSContextSetThreadAffinityMode(context_, flags_->cpu_bind_mode_);
if (flags_->device_ == "GPU") {
MSDeviceInfoHandle gpu_device_info = MSDeviceInfoCreate(kMSDeviceTypeGPU);
MSDeviceInfoSetEnableFP16(gpu_device_info, flags_->enable_fp16_);
MSContextAddDeviceInfo(context_, gpu_device_info);
}
if (flags_->device_ == "NPU") {
MSDeviceInfoHandle npu_device_info = MSDeviceInfoCreate(kMSDeviceTypeKirinNPU);
MSDeviceInfoSetFrequency(npu_device_info, kFrequencyDefault);
MSContextAddDeviceInfo(context_, npu_device_info);
}
MSDeviceInfoHandle cpu_device_info = MSDeviceInfoCreate(kMSDeviceTypeCPU);
MSDeviceInfoSetEnableFP16(cpu_device_info, flags_->enable_fp16_);
MSContextAddDeviceInfo(context_, cpu_device_info);
return RET_OK;
}
int BenchmarkCApi::GenerateInputData() {
for (size_t i = 0; i < inputs_.handle_num; i++) {
MSTensorHandle tensor = inputs_.handle_list[i];
auto data_type = MSTensorGetDataType(tensor);
if (data_type == kMSDataTypeObjectTypeString) {
BENCHMARK_LOG_ERROR("Unsupported kMSDataTypeObjectTypeString");
return RET_ERROR;
} else {
auto data_ptr = MSTensorGetMutableData(tensor);
auto data_size = MSTensorGetDataSize(tensor);
(void)GenerateRandomData(data_size, data_ptr, static_cast<int>(data_type));
}
}
return RET_OK;
}
int BenchmarkCApi::ReadInputFile() {
if (this->flags_->in_data_type_ == lite::kImage) {
BENCHMARK_LOG_ERROR("Unsupported image input");
return RET_ERROR;
} else {
for (size_t i = 0; i < flags_->input_data_list_.size(); i++) {
MSTensorHandle tensor = inputs_.handle_list[i];
size_t size;
auto bin_buf = lite::ReadFile(flags_->input_data_list_[i].c_str(), &size);
if (bin_buf == nullptr) {
BENCHMARK_LOG_ERROR("ReadFile failed");
return RET_ERROR;
}
if (MSTensorGetDataType(tensor) == kMSDataTypeObjectTypeString) {
BENCHMARK_LOG_ERROR("Unsupported kMSDataTypeObjectTypeString");
return RET_ERROR;
} else {
auto tensor_data_size = MSTensorGetDataSize(tensor);
if (tensor_data_size != size) {
BENCHMARK_LOG_ERROR("Input file size error, required: " << tensor_data_size << ", in fact: " << size);
delete[] bin_buf;
return RET_ERROR;
}
auto input_data = MSTensorGetMutableData(tensor);
if (input_data == nullptr) {
BENCHMARK_LOG_ERROR("MSTensorGetMutableData failed");
return RET_ERROR;
}
memcpy(input_data, bin_buf, size);
}
delete[] bin_buf;
}
}
return RET_OK;
}
int BenchmarkCApi::MarkAccuracy() {
MS_LOG(INFO) << "MarkAccuracy";
std::cout << "MarkAccuracy" << std::endl;
auto status = PrintInputData();
if (status != RET_OK) {
BENCHMARK_LOG_ERROR("PrintInputData failed, ret: " << status);
return status;
}
status = MSModelPredict(model_, inputs_, &outputs_, before_call_back_, after_call_back_);
if (status != kMSStatusSuccess) {
BENCHMARK_LOG_ERROR("MSModelPredict failed, ret: " << status);
return RET_ERROR;
}
status = ReadCalibData();
if (status != RET_OK) {
BENCHMARK_LOG_ERROR("ReadCalibData failed, ret: " << status);
return status;
}
status = CompareOutput();
if (status != RET_OK) {
BENCHMARK_LOG_ERROR("CompareOutput failed, ret: " << status);
return status;
}
return RET_OK;
}
int BenchmarkCApi::MarkPerformance() {
MS_LOG(INFO) << "Running warm up loops...";
std::cout << "Running warm up loops..." << std::endl;
for (int i = 0; i < flags_->warm_up_loop_count_; i++) {
auto ret = MSModelPredict(model_, inputs_, &outputs_, before_call_back_, after_call_back_);
if (ret != kMSStatusSuccess) {
BENCHMARK_LOG_ERROR("MSModelPredict failed, ret: " << kMSStatusSuccess);
return RET_ERROR;
}
}
MS_LOG(INFO) << "Running benchmark loops...";
std::cout << "Running benchmark loops..." << std::endl;
uint64_t time_min = 1000000;
uint64_t time_max = 0;
uint64_t time_avg = 0;
for (int i = 0; i < flags_->loop_count_; i++) {
auto start = GetTimeUs();
auto ret = MSModelPredict(model_, inputs_, &outputs_, before_call_back_, after_call_back_);
if (ret != kMSStatusSuccess) {
BENCHMARK_LOG_ERROR("MSModelPredict failed, ret: " << kMSStatusSuccess);
return RET_ERROR;
}
auto end = GetTimeUs();
auto time = end - start;
time_min = std::min(time_min, time);
time_max = std::max(time_max, time);
time_avg += time;
}
if (flags_->time_profiling_) {
const std::vector<std::string> per_op_name = {"opName", "avg(ms)", "percent", "calledTimes", "opTotalTime"};
const std::vector<std::string> per_op_type = {"opType", "avg(ms)", "percent", "calledTimes", "opTotalTime"};
PrintResult(per_op_name, op_times_by_name_);
PrintResult(per_op_type, op_times_by_type_);
}
if (flags_->loop_count_ > 0) {
time_avg /= flags_->loop_count_;
MS_LOG(INFO) << "Model = "
<< flags_->model_file_.substr(flags_->model_file_.find_last_of(lite::DELIM_SLASH) + 1).c_str()
<< ", NumThreads = " << flags_->num_threads_ << ", MinRunTime = " << time_min / lite::kFloatMSEC
<< ", MaxRuntime = " << time_max / lite::kFloatMSEC
<< ", AvgRunTime = " << time_avg / lite::kFloatMSEC;
printf("Model = %s, NumThreads = %d, MinRunTime = %f ms, MaxRuntime = %f ms, AvgRunTime = %f ms\n",
flags_->model_file_.substr(flags_->model_file_.find_last_of(lite::DELIM_SLASH) + 1).c_str(),
flags_->num_threads_, time_min / lite::kFloatMSEC, time_max / lite::kFloatMSEC, time_avg / lite::kFloatMSEC);
}
return RET_OK;
}
int BenchmarkCApi::GetDataTypeByTensorName(const std::string &tensor_name) {
return MSTensorGetDataType(MSModelGetOutputByTensorName(model_, tensor_name.c_str()));
}
int BenchmarkCApi::CompareOutput() {
constexpr int kPercentageDivisor = 100;
std::cout << "================ Comparing Output data ================" << std::endl;
float total_bias = 0;
int total_size = 0;
for (const auto &calib_tensor : benchmark_data_) {
std::string tensor_name = calib_tensor.first;
MSTensorHandle tensor = MSModelGetOutputByTensorName(model_, tensor_name.c_str());
if (tensor == nullptr) {
BENCHMARK_LOG_ERROR("Get tensor failed, tensor name: " << tensor_name);
return RET_ERROR;
}
int ret;
if (static_cast<int>(MSTensorGetDataType(tensor)) == kObjectTypeString) {
BENCHMARK_LOG_ERROR("Unsupported kMSDataTypeObjectTypeString");
return RET_ERROR;
} else {
ret = CompareDataGetTotalBiasAndSize(tensor_name, tensor, &total_bias, &total_size);
}
if (ret != RET_OK) {
BENCHMARK_LOG_ERROR("Error in CompareData");
BENCHMARK_LOG_ERROR("=======================================================");
return ret;
}
}
float mean_bias;
if (total_size != 0) {
mean_bias = ((total_bias / float_t(total_size)) * kPercentageDivisor);
} else {
mean_bias = 0;
}
std::cout << "Mean bias of all nodes/tensors: " << mean_bias << "%" << std::endl;
std::cout << "=======================================================" << std::endl << std::endl;
if (mean_bias > this->flags_->accuracy_threshold_) {
BENCHMARK_LOG_ERROR("Mean bias of all nodes/tensors is too big: " << mean_bias << "%");
return RET_ERROR;
}
return RET_OK;
}
int BenchmarkCApi::CompareDataGetTotalBiasAndSize(const std::string &name, MSTensorHandle tensor, float *total_bias,
int *total_size) {
auto tensor_data = MSTensorGetData(tensor);
if (tensor_data == nullptr) {
BENCHMARK_LOG_ERROR("MSTensorGetData failed.");
return RET_ERROR;
}
size_t shape_num;
const int64_t *shape = MSTensorGetShape(tensor, &shape_num);
std::vector<int64_t> vec_shape(shape, shape + shape_num);
float bias = 0;
switch (static_cast<TypeId>(MSTensorGetDataType(tensor))) {
case TypeId::kNumberTypeFloat:
case TypeId::kNumberTypeFloat32: {
bias = CompareData<float, int64_t>(name, vec_shape, tensor_data);
break;
}
case TypeId::kNumberTypeInt8: {
bias = CompareData<int8_t, int64_t>(name, vec_shape, tensor_data);
break;
}
case TypeId::kNumberTypeUInt8: {
bias = CompareData<uint8_t, int64_t>(name, vec_shape, tensor_data);
break;
}
case TypeId::kNumberTypeInt32: {
bias = CompareData<int32_t, int64_t>(name, vec_shape, tensor_data);
break;
}
case TypeId::kNumberTypeInt16: {
bias = CompareData<int16_t, int64_t>(name, vec_shape, tensor_data);
break;
}
case TypeId::kNumberTypeBool: {
bias = CompareData<bool, int64_t>(name, vec_shape, tensor_data);
break;
}
default:
BENCHMARK_LOG_ERROR("Unsupported data type" << static_cast<int>(MSTensorGetDataType(tensor)));
return RET_ERROR;
}
if (bias < 0) {
BENCHMARK_LOG_ERROR("CompareData failed, name: " << name);
return RET_ERROR;
}
*total_bias += bias;
*total_size += 1;
return RET_OK;
}
int BenchmarkCApi::PrintInputData() {
constexpr int64_t kPrintDataNum = 20;
for (size_t i = 0; i < inputs_.handle_num; i++) {
auto input = inputs_.handle_list[i];
std::cout << "InData" << i << ": ";
auto data_type = static_cast<TypeId>(MSTensorGetDataType(input));
if (data_type == TypeId::kObjectTypeString) {
BENCHMARK_LOG_ERROR("Unsupported kMSDataTypeObjectTypeString.");
return RET_ERROR;
}
auto tensor_data = MSTensorGetData(input);
size_t print_num = std::min(MSTensorGetElementNum(input), kPrintDataNum);
for (size_t j = 0; j < print_num; j++) {
if (data_type == TypeId::kNumberTypeFloat32 || data_type == TypeId::kNumberTypeFloat) {
std::cout << static_cast<const float *>(tensor_data)[j] << " ";
} else if (data_type == TypeId::kNumberTypeInt8) {
std::cout << static_cast<const int8_t *>(tensor_data)[j] << " ";
} else if (data_type == TypeId::kNumberTypeUInt8) {
std::cout << static_cast<const uint8_t *>(tensor_data)[j] << " ";
} else if (data_type == TypeId::kNumberTypeInt32) {
std::cout << static_cast<const int32_t *>(tensor_data)[j] << " ";
} else if (data_type == TypeId::kNumberTypeInt64) {
std::cout << static_cast<const int64_t *>(tensor_data)[j] << " ";
} else if (data_type == TypeId::kNumberTypeBool) {
std::cout << static_cast<const bool *>(tensor_data)[j] << " ";
} else {
BENCHMARK_LOG_ERROR("Datatype: " << data_type << " is not supported.");
return RET_ERROR;
}
}
std::cout << std::endl;
}
return RET_OK;
}
int BenchmarkCApi::InitTimeProfilingCallbackParameter() {
before_call_back_ = TimeBeforeCallback;
after_call_back_ = TimeAfterCallback;
return RET_OK;
}
int BenchmarkCApi::InitPerfProfilingCallbackParameter() {
BENCHMARK_LOG_ERROR("Unsupported feature.");
return RET_ERROR;
}
int BenchmarkCApi::InitPrintTensorDataCallbackParameter() {
BENCHMARK_LOG_ERROR("Unsupported feature.");
return RET_ERROR;
}
int BenchmarkCApi::InitDumpTensorDataCallbackParameter() {
BENCHMARK_LOG_ERROR("Unsupported feature.");
return RET_ERROR;
}
} // namespace tools
} // namespace mindspore
uint64_t g_op_begin_ = 0;
int g_op_call_times_total_ = 0;
float g_op_cost_total_ = 0.0f;
std::map<std::string, std::pair<int, float>> g_op_times_by_type_;
std::map<std::string, std::pair<int, float>> g_op_times_by_name_;
bool TimeBeforeCallback(const MSTensorHandleArray inputs, const MSTensorHandleArray outputs,
const MSCallBackParamC kernel_Info) {
if (g_op_times_by_type_.find(kernel_Info.node_type) == g_op_times_by_type_.end()) {
g_op_times_by_type_.insert(std::make_pair(kernel_Info.node_type, std::make_pair(0, 0.0f)));
}
if (g_op_times_by_name_.find(kernel_Info.node_name) == g_op_times_by_name_.end()) {
g_op_times_by_name_.insert(std::make_pair(kernel_Info.node_name, std::make_pair(0, 0.0f)));
}
g_op_call_times_total_++;
g_op_begin_ = mindspore::lite::GetTimeUs();
return true;
}
bool TimeAfterCallback(const MSTensorHandleArray inputs, const MSTensorHandleArray outputs,
const MSCallBackParamC kernel_Info) {
uint64_t opEnd = mindspore::lite::GetTimeUs();
float cost = static_cast<float>(opEnd - g_op_begin_) / mindspore::lite::kFloatMSEC;
g_op_cost_total_ += cost;
g_op_times_by_type_[kernel_Info.node_type].first++;
g_op_times_by_type_[kernel_Info.node_type].second += cost;
g_op_times_by_name_[kernel_Info.node_name].first++;
g_op_times_by_name_[kernel_Info.node_name].second += cost;
return true;
}

View File

@ -0,0 +1,76 @@
/**
* 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_LITE_TOOLS_BENCHMARK_BENCHMARK_C_API_H_
#define MINDSPORE_LITE_TOOLS_BENCHMARK_BENCHMARK_C_API_H_
#include <vector>
#include <string>
#include "tools/benchmark/benchmark_base.h"
#include "include/c_api/model_c.h"
#include "include/c_api/context_c.h"
#ifdef __cplusplus
extern "C" {
#endif
bool TimeBeforeCallback(const MSTensorHandleArray inputs, const MSTensorHandleArray outputs,
const MSCallBackParamC kernel_Info);
bool TimeAfterCallback(const MSTensorHandleArray inputs, const MSTensorHandleArray outputs,
const MSCallBackParamC kernel_Info);
#ifdef __cplusplus
}
#endif
using mindspore::lite::BenchmarkBase;
using mindspore::lite::BenchmarkFlags;
namespace mindspore::tools {
class MS_API BenchmarkCApi : public BenchmarkBase {
public:
explicit BenchmarkCApi(BenchmarkFlags *flags) : BenchmarkBase(flags) {}
virtual ~BenchmarkCApi() { MSModelDestroy(&model_); }
int RunBenchmark() override;
protected:
int CompareDataGetTotalBiasAndSize(const std::string &name, MSTensorHandle tensor, float *total_bias,
int *total_size);
int InitContext();
int GenerateInputData() override;
int ReadInputFile() override;
int GetDataTypeByTensorName(const std::string &tensor_name) override;
int CompareOutput() override;
int InitTimeProfilingCallbackParameter() override;
int InitPerfProfilingCallbackParameter() override;
int InitDumpTensorDataCallbackParameter() override;
int InitPrintTensorDataCallbackParameter() override;
int PrintInputData();
int MarkPerformance();
int MarkAccuracy();
private:
MSModelHandle model_ = nullptr;
MSContextHandle context_ = nullptr;
MSTensorHandleArray inputs_;
MSTensorHandleArray outputs_;
MSKernelCallBackC before_call_back_ = nullptr;
MSKernelCallBackC after_call_back_ = nullptr;
};
} // namespace mindspore::tools
#endif // MINDSPORE_LITE_TOOLS_BENCHMARK_BENCHMARK_C_API_H_