!26137 [MS][LITE][Develop] add c api demo
Merge pull request !26137 from sunsuodong/add_c_api_demo
This commit is contained in:
commit
5df8af4886
|
@ -218,6 +218,8 @@ if(PLATFORM_ARM64)
|
|||
COMPONENT ${RUNTIME_COMPONENT_NAME})
|
||||
install(DIRECTORY ${TOP_DIR}/include/api/ DESTINATION ${RUNTIME_INC_DIR}/api
|
||||
COMPONENT ${RUNTIME_COMPONENT_NAME} FILES_MATCHING PATTERN "*.h" PATTERN "ops*" EXCLUDE)
|
||||
install(DIRECTORY ${TOP_DIR}/include/c_api/ DESTINATION ${RUNTIME_INC_DIR}/c_api
|
||||
COMPONENT ${RUNTIME_COMPONENT_NAME} FILES_MATCHING PATTERN "*.h")
|
||||
__install_micro_wrapper()
|
||||
if(MSLITE_ENABLE_TOOLS)
|
||||
install(TARGETS ${BENCHMARK_NAME} RUNTIME DESTINATION ${BENCHMARK_ROOT_DIR} COMPONENT ${RUNTIME_COMPONENT_NAME})
|
||||
|
@ -269,6 +271,8 @@ elseif(PLATFORM_ARM32)
|
|||
COMPONENT ${RUNTIME_COMPONENT_NAME})
|
||||
install(DIRECTORY ${TOP_DIR}/include/api/ DESTINATION ${RUNTIME_INC_DIR}/api
|
||||
COMPONENT ${RUNTIME_COMPONENT_NAME} FILES_MATCHING PATTERN "*.h" PATTERN "ops*" EXCLUDE)
|
||||
install(DIRECTORY ${TOP_DIR}/include/c_api/ DESTINATION ${RUNTIME_INC_DIR}/c_api
|
||||
COMPONENT ${RUNTIME_COMPONENT_NAME} FILES_MATCHING PATTERN "*.h")
|
||||
__install_micro_wrapper()
|
||||
if(MSLITE_ENABLE_TOOLS AND NOT TARGET_OHOS_LITE)
|
||||
install(TARGETS ${BENCHMARK_NAME} RUNTIME DESTINATION ${BENCHMARK_ROOT_DIR}
|
||||
|
@ -305,6 +309,8 @@ elseif(WIN32)
|
|||
COMPONENT ${RUNTIME_COMPONENT_NAME} FILES_MATCHING PATTERN "*.h")
|
||||
install(DIRECTORY ${TOP_DIR}/include/api/ DESTINATION ${CONVERTER_ROOT_DIR}/include/api
|
||||
COMPONENT ${RUNTIME_COMPONENT_NAME} FILES_MATCHING PATTERN "*.h" PATTERN "ops*" EXCLUDE)
|
||||
install(DIRECTORY ${TOP_DIR}/include/c_api/ DESTINATION ${RUNTIME_INC_DIR}/c_api
|
||||
COMPONENT ${RUNTIME_COMPONENT_NAME} FILES_MATCHING PATTERN "*.h")
|
||||
install(FILES ${TOP_DIR}/mindspore/ccsrc/backend/optimizer/common/pass.h
|
||||
DESTINATION ${CONVERTER_ROOT_DIR}/include COMPONENT ${RUNTIME_COMPONENT_NAME})
|
||||
install(FILES ${TOP_DIR}/mindspore/lite/tools/converter/model_parser.h
|
||||
|
@ -368,6 +374,8 @@ elseif(WIN32)
|
|||
COMPONENT ${RUNTIME_COMPONENT_NAME})
|
||||
install(DIRECTORY ${TOP_DIR}/include/api/ DESTINATION ${RUNTIME_INC_DIR}/api
|
||||
COMPONENT ${RUNTIME_COMPONENT_NAME} FILES_MATCHING PATTERN "*.h" PATTERN "ops*" EXCLUDE)
|
||||
install(DIRECTORY ${TOP_DIR}/include/c_api/ DESTINATION ${RUNTIME_INC_DIR}/c_api
|
||||
COMPONENT ${RUNTIME_COMPONENT_NAME} FILES_MATCHING PATTERN "*.h")
|
||||
if(MSVC)
|
||||
install(FILES ${TOP_DIR}/build/mindspore/src/${MINDSPORE_LITE_LIB_NAME}.lib DESTINATION ${RUNTIME_LIB_DIR}
|
||||
COMPONENT ${RUNTIME_COMPONENT_NAME})
|
||||
|
@ -408,6 +416,8 @@ else()
|
|||
COMPONENT ${RUNTIME_COMPONENT_NAME})
|
||||
install(DIRECTORY ${TOP_DIR}/include/api/ DESTINATION ${RUNTIME_INC_DIR}/api
|
||||
COMPONENT ${RUNTIME_COMPONENT_NAME} FILES_MATCHING PATTERN "*.h" PATTERN "ops*" EXCLUDE)
|
||||
install(DIRECTORY ${TOP_DIR}/include/c_api/ DESTINATION ${RUNTIME_INC_DIR}/c_api
|
||||
COMPONENT ${RUNTIME_COMPONENT_NAME} FILES_MATCHING PATTERN "*.h")
|
||||
install(FILES ${TOP_DIR}/mindspore/lite/build/src/${MINDSPORE_LITE_LIB_NAME}.so DESTINATION ${RUNTIME_LIB_DIR}
|
||||
COMPONENT ${RUNTIME_COMPONENT_NAME})
|
||||
install(FILES ${TOP_DIR}/mindspore/lite/build/src/${MINDSPORE_LITE_LIB_NAME}.a DESTINATION ${RUNTIME_LIB_DIR}
|
||||
|
@ -435,6 +445,8 @@ else()
|
|||
COMPONENT ${RUNTIME_COMPONENT_NAME} FILES_MATCHING PATTERN "*.h")
|
||||
install(DIRECTORY ${TOP_DIR}/include/api/ DESTINATION ${CONVERTER_ROOT_DIR}/include/api
|
||||
COMPONENT ${RUNTIME_COMPONENT_NAME} FILES_MATCHING PATTERN "*.h" PATTERN "ops*" EXCLUDE)
|
||||
install(DIRECTORY ${TOP_DIR}/include/c_api/ DESTINATION ${RUNTIME_INC_DIR}/c_api
|
||||
COMPONENT ${RUNTIME_COMPONENT_NAME} FILES_MATCHING PATTERN "*.h")
|
||||
install(FILES ${TOP_DIR}/mindspore/ccsrc/backend/optimizer/common/pass.h
|
||||
DESTINATION ${CONVERTER_ROOT_DIR}/include COMPONENT ${RUNTIME_COMPONENT_NAME})
|
||||
install(FILES ${TOP_DIR}/mindspore/lite/tools/converter/model_parser.h
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "include/c_api/types_c.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
|
@ -43,7 +43,7 @@ typedef struct MSCallBackParamC {
|
|||
} MSCallBackParamC;
|
||||
|
||||
typedef bool (*MSKernelCallBackC)(const MSTensorHandleArray inputs, const MSTensorHandleArray outputs,
|
||||
const MSCallBackParamC &kernel_Info);
|
||||
const MSCallBackParamC kernel_Info);
|
||||
|
||||
/// \brief Create a model object. Only valid for Lite.
|
||||
///
|
||||
|
|
|
@ -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()
|
|
@ -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
|
|
@ -0,0 +1,118 @@
|
|||
/**
|
||||
* 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]);
|
||||
for (size_t j = 0; j < num; j++) {
|
||||
input_data[j] = (float)(rand() % 10) / 10; // 0.1f--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;
|
||||
}
|
||||
MSContextSetThreadNum(context, 2);
|
||||
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");
|
||||
for (int j = 0; j < element_num && j <= 50; ++j) {
|
||||
printf("%f ", data[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
// Delete model.
|
||||
MSModelDestroy(model);
|
||||
return kMSStatusSuccess;
|
||||
}
|
||||
|
||||
int main(int argc, const char **argv) { return QuickStart(argc, argv); }
|
|
@ -438,7 +438,7 @@ 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) {
|
||||
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)));
|
||||
}
|
||||
|
@ -452,7 +452,7 @@ bool TimeBeforeCallback(const MSTensorHandleArray inputs, const MSTensorHandleAr
|
|||
}
|
||||
|
||||
bool TimeAfterCallback(const MSTensorHandleArray inputs, const MSTensorHandleArray outputs,
|
||||
const MSCallBackParamC &kernel_Info) {
|
||||
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;
|
||||
|
|
|
@ -26,9 +26,9 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
bool TimeBeforeCallback(const MSTensorHandleArray inputs, const MSTensorHandleArray outputs,
|
||||
const MSCallBackParamC &kernel_Info);
|
||||
const MSCallBackParamC kernel_Info);
|
||||
bool TimeAfterCallback(const MSTensorHandleArray inputs, const MSTensorHandleArray outputs,
|
||||
const MSCallBackParamC &kernel_Info);
|
||||
const MSCallBackParamC kernel_Info);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue