add micro custom
This commit is contained in:
parent
97749e4290
commit
1921c1662d
|
@ -446,7 +446,7 @@ if(PLATFORM_ARM64)
|
|||
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(ANDROID_NDK_TOOLCHAIN_INCLUDED OR MSLITE_ENABLE_CONVERTER)
|
||||
if(ANDROID_NDK_TOOLCHAIN_INCLUDED OR MSLITE_ENABLE_CONVERTER OR TARGET_HIMIX)
|
||||
__install_micro_wrapper()
|
||||
endif()
|
||||
if(MSLITE_ENABLE_RUNTIME_GLOG)
|
||||
|
@ -466,6 +466,10 @@ if(PLATFORM_ARM64)
|
|||
${TOP_DIR}/mindspore/lite/build/tools/benchmark/nnie_proposal/${MSLITE_PROPOSAL_LIB_NAME}.so
|
||||
DESTINATION ${PROVIDERS_LIB_DIR}/${MSLITE_REGISTRY_DEVICE}
|
||||
COMPONENT ${RUNTIME_COMPONENT_NAME})
|
||||
install(FILES
|
||||
${TOP_DIR}/mindspore/lite/build/nnie_micro/${MICRO_NNIE_LIB_NAME}.so
|
||||
DESTINATION ${PROVIDERS_LIB_DIR}/${MSLITE_REGISTRY_DEVICE}
|
||||
COMPONENT ${RUNTIME_COMPONENT_NAME})
|
||||
endif()
|
||||
elseif(TARGET_MIX210)
|
||||
if(${MSLITE_REGISTRY_DEVICE} STREQUAL "SD3403" AND (NOT MSLITE_ENABLE_ACL))
|
||||
|
@ -685,7 +689,7 @@ elseif(PLATFORM_ARM32)
|
|||
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(ANDROID_NDK_TOOLCHAIN_INCLUDED OR MSLITE_ENABLE_CONVERTER OR TARGET_OHOS_LITE)
|
||||
if(ANDROID_NDK_TOOLCHAIN_INCLUDED OR MSLITE_ENABLE_CONVERTER OR TARGET_OHOS_LITE OR TARGET_HIMIX)
|
||||
__install_micro_wrapper()
|
||||
endif()
|
||||
if(MSLITE_ENABLE_TOOLS AND NOT TARGET_OHOS_LITE)
|
||||
|
@ -701,15 +705,13 @@ elseif(PLATFORM_ARM32)
|
|||
${TOP_DIR}/mindspore/lite/build/tools/benchmark/nnie_proposal/${MSLITE_PROPOSAL_LIB_NAME}.so
|
||||
DESTINATION ${PROVIDERS_LIB_DIR}/${MSLITE_REGISTRY_DEVICE}
|
||||
COMPONENT ${RUNTIME_COMPONENT_NAME})
|
||||
if(${MSLITE_REGISTRY_DEVICE} STREQUAL "Hi3516D")
|
||||
install(FILES
|
||||
${TOP_DIR}/mindspore/lite/providers/nnie/third_patry/${MICRO_NNIE_LIB_NAME}.so
|
||||
${TOP_DIR}/mindspore/lite/build/nnie_micro/${MICRO_NNIE_LIB_NAME}.so
|
||||
DESTINATION ${PROVIDERS_LIB_DIR}/${MSLITE_REGISTRY_DEVICE}
|
||||
COMPONENT ${RUNTIME_COMPONENT_NAME})
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
if(SUPPORT_TRAIN)
|
||||
install(TARGETS ${BENCHMARK_TRAIN_NAME} RUNTIME DESTINATION ${BENCHMARK_TRAIN_ROOT_DIR} COMPONENT
|
||||
${RUNTIME_COMPONENT_NAME})
|
||||
|
|
|
@ -852,7 +852,7 @@ if(MSLITE_MINDDATA_IMPLEMENT STREQUAL "lite" OR MSLITE_MINDDATA_IMPLEMENT STREQU
|
|||
endif()
|
||||
|
||||
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src/common/ops)
|
||||
if(ANDROID_NDK_TOOLCHAIN_INCLUDED OR TARGET_OHOS_LITE)
|
||||
if(ANDROID_NDK_TOOLCHAIN_INCLUDED OR TARGET_OHOS_LITE OR TARGET_HIMIX)
|
||||
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tools/converter/micro/coder)
|
||||
endif()
|
||||
|
||||
|
@ -862,6 +862,9 @@ add_subdirectory(${CCSRC_DIR}/plugin/device/cpu/kernel/nnacl build)
|
|||
if(MSLITE_ENABLE_TOOLS)
|
||||
if(NOT MSLITE_COMPILE_TWICE)
|
||||
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tools/benchmark)
|
||||
if(TARGET_HIMIX)
|
||||
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tools/converter/micro/providers/nnie nnie_micro)
|
||||
endif()
|
||||
endif()
|
||||
if(SUPPORT_TRAIN)
|
||||
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tools/benchmark_train)
|
||||
|
|
|
@ -267,17 +267,21 @@ build_lite() {
|
|||
MSLITE_REGISTRY_DEVICE=Hi3516D
|
||||
check_Hi35xx
|
||||
MSLITE_COMPILE_TWICE=ON
|
||||
twice_target='benchmark micro_nnie'
|
||||
elif [[ "${MSLITE_REGISTRY_DEVICE}" == "Hi3559A" && "${local_lite_platform}" == "arm64" ]]; then
|
||||
TOOLCHAIN_NAME="himix100"
|
||||
check_Hi35xx
|
||||
MSLITE_COMPILE_TWICE=ON
|
||||
twice_target='benchmark micro_nnie'
|
||||
elif [[ "${MSLITE_REGISTRY_DEVICE}" == "SD3403" && "${local_lite_platform}" == "arm64" ]]; then
|
||||
TOOLCHAIN_NAME="mix210"
|
||||
MSLITE_COMPILE_TWICE=ON
|
||||
twice_target=benchmark
|
||||
elif [[ "${MSLITE_REGISTRY_DEVICE}" == "Hi3519A" && "${local_lite_platform}" == "arm32" ]]; then
|
||||
TOOLCHAIN_NAME="himix200"
|
||||
check_Hi35xx
|
||||
MSLITE_COMPILE_TWICE=ON
|
||||
twice_target='benchmark micro_nnie'
|
||||
elif [[ ("${MSLITE_ENABLE_NNIE}" == "on" || "${MSLITE_REGISTRY_DEVICE}" == "Hi3516D") && "${local_lite_platform}" == "x86_64" ]]; then
|
||||
MSLITE_REGISTRY_DEVICE=Hi3516D
|
||||
elif [[ "${MSLITE_MICRO_PLATFORM}" == cortex-m* && "${local_lite_platform}" == "x86_64" ]]; then
|
||||
|
@ -405,9 +409,10 @@ build_lite() {
|
|||
if [[ "X$MSLITE_ENABLE_TOOLS" != "XOFF" ]]; then
|
||||
LITE_CMAKE_ARGS=`echo $LITE_CMAKE_ARGS | sed 's/-DMSLITE_COMPILE_TWICE=ON/-DMSLITE_COMPILE_TWICE=OFF/g'`
|
||||
cp -r ${INSTALL_PREFIX}/mindspore*/runtime ${BASEPATH}/mindspore/lite/providers
|
||||
echo "cmake ${LITE_CMAKE_ARGS} ${BASEPATH}/mindspore/lite"
|
||||
cmake ${LITE_CMAKE_ARGS} "${BASEPATH}/mindspore/lite"
|
||||
cmake --build "${BASEPATH}/mindspore/lite/build" --target benchmark -j$THREAD_NUM
|
||||
PKG_PATH=${INSTALL_PREFIX}/`ls ${INSTALL_PREFIX}/`
|
||||
echo "cmake ${LITE_CMAKE_ARGS} -DPKG_PATH=${PKG_PATH} ${BASEPATH}/mindspore/lite"
|
||||
cmake ${LITE_CMAKE_ARGS} -DPKG_PATH=${PKG_PATH} "${BASEPATH}/mindspore/lite"
|
||||
cmake --build "${BASEPATH}/mindspore/lite/build" --target ${twice_target} -j$THREAD_NUM
|
||||
make install
|
||||
fi
|
||||
fi
|
||||
|
|
|
@ -356,7 +356,7 @@ static int NnieParamInit(NnieCfg *nnie_cfg, NnieParam *nnie_param) {
|
|||
return RET_OK;
|
||||
}
|
||||
|
||||
static int NnieLoadModel(char *model_buf, int size, NnieModel *nnie_model) {
|
||||
int NnieLoadModel(char *model_buf, int size, NnieModel *nnie_model) {
|
||||
HI_S32 ret = HI_INVALID_VALUE;
|
||||
HI_U64 phy_addr = 0;
|
||||
HI_U8 *vir_addr = nullptr;
|
||||
|
@ -622,14 +622,9 @@ HI_U32 GetBlobSize(const SVP_SRC_BLOB_S &blob) {
|
|||
}
|
||||
}
|
||||
|
||||
static int NnieFillSrcData(NnieCfg *nnie_cfg, NnieParam *nnie_param, NnieDataIndex *input_data_idx, int64_t *shape,
|
||||
int size) {
|
||||
static int NnieFillSrcData(NnieCfg *nnie_cfg, NnieParam *nnie_param, NnieDataIndex *input_data_idx, HI_U32 input_size) {
|
||||
HI_U32 i, ret;
|
||||
HI_U32 input_size = 1;
|
||||
SVP_SRC_BLOB_S *blob = &nnie_param->seg_data_[input_data_idx->seg_idx_].src_[input_data_idx->node_idx_];
|
||||
for (i = 0; i < (HI_U32)size; i++) {
|
||||
input_size *= shape[i];
|
||||
}
|
||||
|
||||
if (SVP_BLOB_TYPE_SEQ_S32 == blob->enType) {
|
||||
return NnieFillSrcDataSeq(nnie_cfg, blob, input_size);
|
||||
|
@ -715,13 +710,8 @@ static int NnieGetDstDataSEQ(SVP_SRC_BLOB_S *blob, HI_U32 input_num, NnieDataInd
|
|||
}
|
||||
return RET_OK;
|
||||
}
|
||||
static int NnieGetDstData(NnieCfg *nnie_cfg, NnieParam *nnie_param, NnieDataIndex *input_data_idx, int64_t *shape,
|
||||
int size) {
|
||||
static int NnieGetDstData(NnieCfg *nnie_cfg, NnieParam *nnie_param, NnieDataIndex *input_data_idx, HI_U32 input_num) {
|
||||
SVP_SRC_BLOB_S *blob = &nnie_param->seg_data_[input_data_idx->seg_idx_ - 1].dst_[input_data_idx->node_idx_];
|
||||
HI_U32 input_num = 1;
|
||||
for (HI_U32 i = 0; i < (HI_U32)size; i++) {
|
||||
input_num *= shape[i];
|
||||
}
|
||||
if (SVP_BLOB_TYPE_U8 <= blob->enType && SVP_BLOB_TYPE_YVU422SP >= blob->enType) {
|
||||
LOGE("Nnie output type error");
|
||||
return RET_ERROR;
|
||||
|
@ -825,47 +815,7 @@ int CheckMsShapeN(NnieRunCfg *nnie_run_cfg, const std::vector<int64_t> &input_sh
|
|||
return RET_OK;
|
||||
}
|
||||
|
||||
size_t GetFillIndex(const std::vector<mindspore::MSTensor> &inputs, size_t input_size, const HI_CHAR *name) {
|
||||
size_t j;
|
||||
for (j = 0; j < input_size; j++) {
|
||||
auto input_str = inputs[j].Name();
|
||||
if (input_str.length() > 4) {
|
||||
if (input_str.substr(input_str.length() - 4) == "_pre") {
|
||||
input_str = input_str.substr(0, input_str.length() - 4);
|
||||
} else if (input_str.length() > 5) {
|
||||
if (input_str.substr(input_str.length() - 5) == "_post") {
|
||||
input_str = input_str.substr(0, input_str.length() - 5);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (strcmp(input_str.c_str(), name) == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (j == input_size) {
|
||||
for (j = 0; j < input_size; j++) {
|
||||
auto input_str = inputs[j].Name();
|
||||
if (input_str.length() > 4) {
|
||||
if (input_str.substr(input_str.length() - 4) == "_pre") {
|
||||
input_str = input_str.substr(0, input_str.length() - 4);
|
||||
} else if (input_str.length() > 5) {
|
||||
if (input_str.substr(input_str.length() - 5) == "_post") {
|
||||
input_str = input_str.substr(0, input_str.length() - 5);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (strncmp(input_str.c_str(), name, input_str.length()) == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return j;
|
||||
}
|
||||
|
||||
int NnieCommCreate(NnieRunCfg *nnie_run_cfg, char *model_buf, int size,
|
||||
const std::vector<mindspore::MSTensor> &inputs) {
|
||||
int NnieCommCreate(NnieRunCfg *nnie_run_cfg, const std::vector<int64_t> &input_shape) {
|
||||
HI_U8 *vir_addr = nullptr;
|
||||
HI_U32 seg_num;
|
||||
HI_U32 off_set;
|
||||
|
@ -877,27 +827,12 @@ int NnieCommCreate(NnieRunCfg *nnie_run_cfg, char *model_buf, int size,
|
|||
NnieCfg *cfg = &nnie_run_cfg->cfg_;
|
||||
HI_U32 step = cfg->step_; // time step
|
||||
|
||||
ret = NnieLoadModel(model_buf, size, model);
|
||||
if (ret != RET_OK) {
|
||||
LOGE("NnieLoadModel failed!");
|
||||
return RET_ERROR;
|
||||
}
|
||||
if (inputs.size() <= 1) {
|
||||
LOGE("inputs size need greater than 1!");
|
||||
return RET_ERROR;
|
||||
}
|
||||
if (inputs[0].Shape().size() <= 1) {
|
||||
if (input_shape.size() <= 1) {
|
||||
LOGE("input shape size need greater than 1!");
|
||||
return RET_ERROR;
|
||||
}
|
||||
|
||||
j = GetFillIndex(inputs, inputs.size() - 1, model->model_.astSeg[0].astSrcNode[0].szName);
|
||||
if (j == (inputs.size() - 1)) {
|
||||
j = 0;
|
||||
LOGI("input tensor name(%s) can't match wk node name(%s).", inputs[0].Name().c_str(),
|
||||
model->model_.astSeg[0].astSrcNode[0].szName);
|
||||
}
|
||||
if (CheckMsShapeN(nnie_run_cfg, inputs[j].Shape(), model->model_.astSeg[0].astSrcNode[0]) != RET_OK) {
|
||||
if (CheckMsShapeN(nnie_run_cfg, input_shape, model->model_.astSeg[0].astSrcNode[0]) != RET_OK) {
|
||||
return RET_ERROR;
|
||||
}
|
||||
|
||||
|
@ -964,7 +899,7 @@ void NnieCommDelete(NnieParam *pstNnieParamm, NnieModel *nnie_model) {
|
|||
NnieUnloadModel(nnie_model);
|
||||
}
|
||||
|
||||
int NnieCommGetOutputData(NnieRunCfg *nnie_run_cfg, float *data, int64_t *shape, int size, int tensor_index) {
|
||||
int NnieCommGetOutputData(NnieRunCfg *nnie_run_cfg, float *data, HI_U32 output_size, int tensor_index) {
|
||||
if (nnie_run_cfg->run_idx_.seg_idx_ <= 0) {
|
||||
LOGE("output seg index error.");
|
||||
return RET_ERROR;
|
||||
|
@ -974,7 +909,7 @@ int NnieCommGetOutputData(NnieRunCfg *nnie_run_cfg, float *data, int64_t *shape,
|
|||
|
||||
nnie_run_cfg->run_idx_.node_idx_ = id;
|
||||
nnie_run_cfg->cfg_.data_ptr_ = data;
|
||||
ret = NnieGetDstData(&nnie_run_cfg->cfg_, &nnie_run_cfg->param_, &nnie_run_cfg->run_idx_, shape, size);
|
||||
ret = NnieGetDstData(&nnie_run_cfg->cfg_, &nnie_run_cfg->param_, &nnie_run_cfg->run_idx_, output_size);
|
||||
if (ret != RET_OK) {
|
||||
LOGE("NnieGetDstData failed!");
|
||||
return RET_ERROR;
|
||||
|
@ -982,8 +917,7 @@ int NnieCommGetOutputData(NnieRunCfg *nnie_run_cfg, float *data, int64_t *shape,
|
|||
return RET_OK;
|
||||
}
|
||||
|
||||
int NnieCommFillData(NnieRunCfg *nnie_run_cfg, void *data, mindspore::DataType dtype, int64_t *shape, int size,
|
||||
int tensor_index) {
|
||||
int NnieCommFillData(NnieRunCfg *nnie_run_cfg, void *data, HI_U32 input_size, int tensor_index) {
|
||||
HI_U32 ret = 0;
|
||||
int id = tensor_index;
|
||||
HI_U32 seg_idx = nnie_run_cfg->run_idx_.seg_idx_;
|
||||
|
@ -992,21 +926,9 @@ int NnieCommFillData(NnieRunCfg *nnie_run_cfg, void *data, mindspore::DataType d
|
|||
LOGE("Nnie input node index error!");
|
||||
return RET_ERROR;
|
||||
}
|
||||
SVP_BLOB_TYPE_E src_type = nnie_run_cfg->param_.seg_data_[seg_idx].src_[id].enType;
|
||||
if (SVP_BLOB_TYPE_U8 <= src_type && src_type <= SVP_BLOB_TYPE_YVU422SP) {
|
||||
if (!(dtype == DataType::kNumberTypeUInt8 || dtype == DataType::kNumberTypeInt8)) {
|
||||
LOGE("Nnie input node type error!");
|
||||
return RET_ERROR;
|
||||
}
|
||||
} else {
|
||||
if (dtype != DataType::kNumberTypeFloat32) {
|
||||
LOGE("Nnie input node type error!");
|
||||
return RET_ERROR;
|
||||
}
|
||||
}
|
||||
nnie_run_cfg->run_idx_.node_idx_ = id;
|
||||
nnie_run_cfg->cfg_.data_ptr_ = data;
|
||||
ret = NnieFillSrcData(&nnie_run_cfg->cfg_, &nnie_run_cfg->param_, &nnie_run_cfg->run_idx_, shape, size);
|
||||
ret = NnieFillSrcData(&nnie_run_cfg->cfg_, &nnie_run_cfg->param_, &nnie_run_cfg->run_idx_, input_size);
|
||||
if (ret != RET_OK) {
|
||||
LOGE("NnieFillSrcData failed!");
|
||||
return RET_ERROR;
|
||||
|
|
|
@ -20,7 +20,6 @@
|
|||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include "include/api/types.h"
|
||||
#include "include/mpi_vb.h"
|
||||
#include "include/hi_comm_svp.h"
|
||||
#include "include/hi_nnie.h"
|
||||
|
@ -104,17 +103,17 @@ typedef struct {
|
|||
NnieDataIndex run_idx_;
|
||||
} NnieRunCfg;
|
||||
|
||||
int NnieCommCreate(NnieRunCfg *nnie_run_cfg, char *model_buf, int size, const std::vector<mindspore::MSTensor> &inputs);
|
||||
int NnieLoadModel(char *model_buf, int size, NnieModel *nnie_model);
|
||||
|
||||
size_t GetFillIndex(const std::vector<mindspore::MSTensor> &inputs, size_t input_size, const HI_CHAR *name);
|
||||
int NnieCommCreate(NnieRunCfg *nnie_run_cfg, const std::vector<int64_t> &input_shape);
|
||||
|
||||
void NnieCommDelete(NnieParam *pstNnieParamm, NnieModel *nnie_model);
|
||||
|
||||
int NnieCommRun(NnieRunCfg *nnie_run_cfg, bool run_box);
|
||||
|
||||
int NnieCommFillData(NnieRunCfg *nnie_run_cfg, void *data, mindspore::DataType dtype, int64_t *shape, int size, int id);
|
||||
int NnieCommFillData(NnieRunCfg *nnie_run_cfg, void *data, HI_U32 input_size, int id);
|
||||
|
||||
int NnieCommGetOutputData(NnieRunCfg *nnie_run_cfg, float *data, int64_t *shape, int size, int tensor_index);
|
||||
int NnieCommGetOutputData(NnieRunCfg *nnie_run_cfg, float *data, HI_U32 output_size, int tensor_index);
|
||||
|
||||
HI_U32 GetBlobSize(const SVP_SRC_BLOB_S &blob);
|
||||
} // namespace nnie
|
||||
|
|
|
@ -29,6 +29,48 @@ namespace mindspore {
|
|||
namespace nnie {
|
||||
constexpr int kUINT16_MAX = 65535;
|
||||
constexpr int kNumInput2 = 2;
|
||||
constexpr int kPreSize = 4;
|
||||
constexpr int kPostSize = 5;
|
||||
|
||||
static size_t GetFillIndex(const std::vector<mindspore::MSTensor> &inputs, size_t input_size, const HI_CHAR *name) {
|
||||
size_t j;
|
||||
for (j = 0; j < input_size; j++) {
|
||||
auto input_str = inputs[j].Name();
|
||||
if (input_str.length() > kPreSize) {
|
||||
if (input_str.substr(input_str.length() - kPreSize) == "_pre") {
|
||||
input_str = input_str.substr(0, input_str.length() - kPreSize);
|
||||
} else if (input_str.length() > kPostSize) {
|
||||
if (input_str.substr(input_str.length() - kPostSize) == "_post") {
|
||||
input_str = input_str.substr(0, input_str.length() - kPostSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (strcmp(input_str.c_str(), name) == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (j == input_size) {
|
||||
for (j = 0; j < input_size; j++) {
|
||||
auto input_str = inputs[j].Name();
|
||||
if (input_str.length() > kPreSize) {
|
||||
if (input_str.substr(input_str.length() - kPreSize) == "_pre") {
|
||||
input_str = input_str.substr(0, input_str.length() - kPreSize);
|
||||
} else if (input_str.length() > kPostSize) {
|
||||
if (input_str.substr(input_str.length() - kPostSize) == "_post") {
|
||||
input_str = input_str.substr(0, input_str.length() - kPostSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (strncmp(input_str.c_str(), name, input_str.length()) == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return j;
|
||||
}
|
||||
|
||||
int NNIEManager::CfgInit(const Flags &flags, int max_seg_id) {
|
||||
memset(&nnie_cfg_, 0, sizeof(NnieRunCfg));
|
||||
|
||||
|
@ -162,7 +204,25 @@ int NNIEManager::LoadOutputs(std::vector<mindspore::MSTensor> *outputs, std::sha
|
|||
void NNIEManager::SetInputNum(int max_input_num) { nnie_cfg_.cfg_.max_input_num_ = max_input_num; }
|
||||
|
||||
int NNIEManager::Init(char *model_buf, int size, const std::vector<mindspore::MSTensor> &inputs) {
|
||||
if (NnieCommCreate(&nnie_cfg_, model_buf, size, inputs) != RET_OK) {
|
||||
NnieModel *model = &nnie_cfg_.model_;
|
||||
|
||||
if (inputs.size() <= 1) {
|
||||
LOGE("inputs size need greater than 1!");
|
||||
return RET_ERROR;
|
||||
}
|
||||
auto ret = NnieLoadModel(model_buf, size, model);
|
||||
if (ret != RET_OK) {
|
||||
LOGE("NnieLoadModel failed!");
|
||||
return RET_ERROR;
|
||||
}
|
||||
auto j = GetFillIndex(inputs, inputs.size() - 1, model->model_.astSeg[0].astSrcNode[0].szName);
|
||||
if (j == (inputs.size() - 1)) {
|
||||
j = 0;
|
||||
LOGI("input tensor name(%s) can't match wk node name(%s).", inputs[0].Name().c_str(),
|
||||
model->model_.astSeg[0].astSrcNode[0].szName);
|
||||
}
|
||||
|
||||
if (NnieCommCreate(&nnie_cfg_, inputs[j].Shape()) != RET_OK) {
|
||||
NnieCommDelete(&nnie_cfg_.param_, &nnie_cfg_.model_);
|
||||
return RET_ERROR;
|
||||
}
|
||||
|
@ -244,9 +304,9 @@ int NNIEManager::GetOutputData(std::vector<mindspore::MSTensor> *outputs,
|
|||
|
||||
auto input_data_type = (*outputs)[j].DataType();
|
||||
if (input_data_type == DataType::kNumberTypeFloat32) {
|
||||
auto ptr_shape = (*outputs)[j].Shape();
|
||||
HI_U32 output_element = static_cast<HI_U32>((*outputs)[j].ElementNum());
|
||||
auto ptr = reinterpret_cast<float *>((*outputs)[j].MutableData());
|
||||
if (NnieCommGetOutputData(&nnie_cfg_, ptr, ptr_shape.data(), ptr_shape.size(), i) != RET_OK) {
|
||||
if (NnieCommGetOutputData(&nnie_cfg_, ptr, output_element, i) != RET_OK) {
|
||||
return RET_ERROR;
|
||||
}
|
||||
} else {
|
||||
|
@ -459,16 +519,21 @@ int NNIEManager::FillData(std::vector<mindspore::MSTensor> *inputs, unsigned int
|
|||
}
|
||||
|
||||
auto input_data_type = (*inputs)[j].DataType();
|
||||
if ((input_data_type == DataType::kNumberTypeFloat32) || (input_data_type == DataType::kNumberTypeUInt8) ||
|
||||
(input_data_type == DataType::kNumberTypeInt8)) {
|
||||
auto ptr_shape = (*inputs)[j].Shape();
|
||||
if (NnieCommFillData(&nnie_cfg_, (*inputs)[j].MutableData(), input_data_type, ptr_shape.data(), ptr_shape.size(),
|
||||
i) != RET_OK) {
|
||||
LOGE("FillData failed!");
|
||||
SVP_BLOB_TYPE_E src_type = nnie_cfg_.param_.seg_data_[seg_id].src_[i].enType;
|
||||
if (SVP_BLOB_TYPE_U8 <= src_type && src_type <= SVP_BLOB_TYPE_YVU422SP) {
|
||||
if (!(input_data_type == DataType::kNumberTypeUInt8 || input_data_type == DataType::kNumberTypeInt8)) {
|
||||
LOGE("Nnie input node type error!");
|
||||
return RET_ERROR;
|
||||
}
|
||||
} else {
|
||||
LOGE("Unsupported DataType!");
|
||||
if (input_data_type != DataType::kNumberTypeFloat32) {
|
||||
LOGE("Nnie input node type error!");
|
||||
return RET_ERROR;
|
||||
}
|
||||
}
|
||||
HI_U32 input_element = static_cast<HI_U32>((*inputs)[j].ElementNum());
|
||||
if (NnieCommFillData(&nnie_cfg_, (*inputs)[j].MutableData(), input_element, i) != RET_OK) {
|
||||
LOGE("FillData failed!");
|
||||
return RET_ERROR;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,7 +26,6 @@ using mindspore::lite::RET_OK;
|
|||
namespace mindspore {
|
||||
namespace proposal {
|
||||
constexpr int kNumInput2 = 2;
|
||||
constexpr int kNCHWDims = 4;
|
||||
constexpr int kScoreSizeIndex = 2;
|
||||
constexpr int kKeyConfidenceIndex = 4;
|
||||
constexpr int kPredWeightIndex = 2;
|
||||
|
@ -544,8 +543,7 @@ static void Rpn(float **inputs, uint32_t num_ratio_anchors, uint32_t num_scale_a
|
|||
*num_rois = roi_count;
|
||||
}
|
||||
|
||||
int32_t ProposalInit(ProposalParam *param, const std::vector<mindspore::MSTensor> &inputs, uint32_t max_roi_num,
|
||||
uint32_t ori_image_height, uint32_t ori_image_width) {
|
||||
int32_t ProposalInit(ProposalParam *param, uint32_t max_roi_num, uint32_t ori_image_height, uint32_t ori_image_width) {
|
||||
uint32_t tmp_buf_size = 0;
|
||||
uint32_t bbox_buf_size = 0;
|
||||
uint32_t total_size = 0;
|
||||
|
@ -577,25 +575,6 @@ int32_t ProposalInit(ProposalParam *param, const std::vector<mindspore::MSTensor
|
|||
param->rpn_bounding_box_.width_ = COORDI_NUM;
|
||||
param->rpn_bounding_box_.stride_ = COORDI_NUM * sizeof(float);
|
||||
param->rpn_bounding_box_.num_ = 1;
|
||||
if (inputs.size() < kNumInput2) {
|
||||
LOGE("inputs tensor size error.");
|
||||
return RET_ERROR;
|
||||
}
|
||||
|
||||
for (int i = 0; i < kNumInput2; i++) {
|
||||
auto input_data_type = inputs[i].DataType();
|
||||
if (input_data_type == DataType::kNumberTypeFloat32) {
|
||||
auto ptr_shape = inputs[i].Shape();
|
||||
if ((ptr_shape.size() == kNCHWDims)) {
|
||||
param->inputs_height_[i] = ptr_shape[2];
|
||||
param->inputs_width_[i] = ptr_shape[3];
|
||||
param->inputs_channel_[i] = ptr_shape[1];
|
||||
if (i == 0) {
|
||||
param->inputs_stride_ = ptr_shape[3] * sizeof(float);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tmp_buf_size = RpnTmpBufSize(param->num_ratio_anchors_, param->num_scale_anchors_, param->inputs_height_[0],
|
||||
param->inputs_width_[0]);
|
||||
|
@ -617,40 +596,19 @@ int32_t ProposalInit(ProposalParam *param, const std::vector<mindspore::MSTensor
|
|||
return RET_OK;
|
||||
}
|
||||
|
||||
int32_t ProposalRun(std::vector<mindspore::MSTensor> *inputs, std::vector<mindspore::MSTensor> *outputs,
|
||||
ProposalParam *param) {
|
||||
if (inputs->size() < kNumInput2) {
|
||||
LOGE("inputs tensor size error.");
|
||||
return RET_ERROR;
|
||||
}
|
||||
if (outputs->size() != 1) {
|
||||
LOGE("outputs tensor size error.");
|
||||
return RET_ERROR;
|
||||
}
|
||||
int32_t ProposalRun(ProposalParam *param) {
|
||||
for (int i = 0; i < kNumInput2; i++) {
|
||||
auto input_data_type = inputs->at(i).DataType();
|
||||
if (input_data_type == DataType::kNumberTypeFloat32) {
|
||||
param->inputs_[i] = reinterpret_cast<float *>((*inputs)[i].MutableData());
|
||||
}
|
||||
}
|
||||
auto output_data_type = (*outputs)[0].DataType();
|
||||
if (output_data_type != DataType::kNumberTypeFloat32) {
|
||||
LOGE("outputs tensor data type error.");
|
||||
if (param->inputs_[i] == nullptr) {
|
||||
LOGE("inputs is nullptr.");
|
||||
return RET_ERROR;
|
||||
}
|
||||
|
||||
}
|
||||
Rpn(param->inputs_, param->num_ratio_anchors_, param->num_scale_anchors_, param->scales_, param->ratios_,
|
||||
param->ori_image_height_, param->ori_image_width_, param->inputs_height_, param->inputs_width_,
|
||||
param->inputs_channel_, param->inputs_stride_, param->max_roi_num_, param->min_size_, param->spatial_scale_,
|
||||
param->nms_thresh_, param->filter_thresh_, param->num_before_nms_, reinterpret_cast<char *>(param->rpn_tmp_buf_),
|
||||
reinterpret_cast<float *>(param->rpn_bounding_box_.data_), param->rpn_bounding_box_.stride_,
|
||||
¶m->rpn_bounding_box_.height_);
|
||||
|
||||
std::vector<int64_t> shape{static_cast<int64_t>(param->rpn_bounding_box_.height_), COORDI_NUM};
|
||||
(*outputs)[0].SetShape(shape);
|
||||
auto output_data = (*outputs)[0].MutableData();
|
||||
memcpy(output_data, param->rpn_bounding_box_.data_, param->rpn_bounding_box_.height_ * COORDI_NUM * sizeof(float));
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
|
|
|
@ -85,10 +85,8 @@ typedef struct {
|
|||
int32_t max_;
|
||||
} Stack;
|
||||
|
||||
int32_t ProposalInit(ProposalParam *param, const std::vector<mindspore::MSTensor> &inputs, uint32_t max_roi_num,
|
||||
uint32_t ori_image_height, uint32_t ori_image_width);
|
||||
int32_t ProposalRun(std::vector<mindspore::MSTensor> *inputs, std::vector<mindspore::MSTensor> *outputs,
|
||||
ProposalParam *param);
|
||||
int32_t ProposalInit(ProposalParam *param, uint32_t max_roi_num, uint32_t ori_image_height, uint32_t ori_image_width);
|
||||
int32_t ProposalRun(ProposalParam *param);
|
||||
void ProposalDeInit(ProposalParam *param);
|
||||
} // namespace proposal
|
||||
} // namespace mindspore
|
||||
|
|
|
@ -32,6 +32,10 @@ constexpr int kMaxSize = 1024;
|
|||
constexpr int kNumInput2 = 2;
|
||||
constexpr int kDecimal = 10;
|
||||
constexpr auto kMazRoiNum = "MaxROINum";
|
||||
constexpr int kNCHWDims = 4;
|
||||
constexpr int kNCHWFormatH = 2;
|
||||
constexpr int kNCHWFormatW = 3;
|
||||
constexpr int kNCHWFormatC = 1;
|
||||
bool IsValidUnsignedNum(const std::string &num_str) {
|
||||
return !num_str.empty() && std::all_of(num_str.begin(), num_str.end(), ::isdigit);
|
||||
}
|
||||
|
@ -95,7 +99,21 @@ int ProposalCPUKernel::Prepare() {
|
|||
}
|
||||
}
|
||||
|
||||
return ProposalInit(&proposal_param_, inputs_, max_roi_num_int, image_height_, image_weight_);
|
||||
for (size_t i = 0; i < inputs.size(); i++) {
|
||||
auto ptr_shape = inputs[i].Shape();
|
||||
if ((ptr_shape.size() == kNCHWDims)) {
|
||||
proposal_param_.inputs_height_[i] = ptr_shape[kNCHWFormatH];
|
||||
proposal_param_.inputs_width_[i] = ptr_shape[kNCHWFormatW];
|
||||
proposal_param_.inputs_channel_[i] = ptr_shape[kNCHWFormatC];
|
||||
if (i == 0) {
|
||||
proposal_param_.inputs_stride_ = ptr_shape[kNCHWFormatW] * sizeof(float);
|
||||
}
|
||||
} else {
|
||||
LOGE("proposal only support input shape size == 4.");
|
||||
return RET_ERROR;
|
||||
}
|
||||
}
|
||||
return ProposalInit(&proposal_param_, max_roi_num_int, image_height_, image_weight_);
|
||||
}
|
||||
|
||||
int ProposalCPUKernel::ReSize() {
|
||||
|
@ -106,7 +124,21 @@ int ProposalCPUKernel::ReSize() {
|
|||
return RET_OK;
|
||||
}
|
||||
|
||||
int ProposalCPUKernel::Execute() { return ProposalRun(&inputs_, &outputs_, &proposal_param_); }
|
||||
int ProposalCPUKernel::Execute() {
|
||||
for (int i = 0; i < kNumInput2; i++) {
|
||||
proposal_param_.inputs_[i] = reinterpret_cast<float *>(inputs_[i].MutableData());
|
||||
}
|
||||
if (ProposalRun(&proposal_param_) != RET_OK) {
|
||||
LOGE("ProposalRun error.");
|
||||
return RET_ERROR;
|
||||
}
|
||||
std::vector<int64_t> shape{static_cast<int64_t>(proposal_param_.rpn_bounding_box_.height_), COORDI_NUM};
|
||||
outputs_[0].SetShape(shape);
|
||||
auto output_data = outputs_[0].MutableData();
|
||||
memcpy(output_data, proposal_param_.rpn_bounding_box_.data_,
|
||||
proposal_param_.rpn_bounding_box_.height_ * COORDI_NUM * sizeof(float));
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
ProposalCPUKernel::~ProposalCPUKernel() { ProposalDeInit(&proposal_param_); }
|
||||
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
cmake_minimum_required(VERSION 3.14)
|
||||
project(micro_nnie_kernel)
|
||||
if(NOT DEFINED PKG_PATH)
|
||||
message(FATAL_ERROR "PKG_PATH not set")
|
||||
endif()
|
||||
message("PKG_PATH:${PKG_PATH}")
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../)
|
||||
set(THIRD_PATRY_PATH ${LITE_DIR}/providers/nnie/third_patry/)
|
||||
if(${MSLITE_REGISTRY_DEVICE} STREQUAL "Hi3516D")
|
||||
include_directories(${THIRD_PATRY_PATH}/hi3516_sdk/)
|
||||
link_directories(${THIRD_PATRY_PATH}/hi3516_sdk/lib)
|
||||
elseif(${MSLITE_REGISTRY_DEVICE} STREQUAL "Hi3519A")
|
||||
include_directories(${THIRD_PATRY_PATH}/hi3519_sdk/)
|
||||
link_directories(${THIRD_PATRY_PATH}/hi3519_sdk/lib)
|
||||
elseif(${MSLITE_REGISTRY_DEVICE} STREQUAL "Hi3559A")
|
||||
include_directories(${THIRD_PATRY_PATH}/hi3559_sdk/)
|
||||
link_directories(${THIRD_PATRY_PATH}/hi3559_sdk/lib)
|
||||
endif()
|
||||
|
||||
include_directories(${PKG_PATH}/tools/codegen/include)
|
||||
include_directories(${PKG_PATH}/runtime/)
|
||||
include_directories(${LITE_DIR}/providers/nnie)
|
||||
include_directories(${LITE_DIR}/providers/nnie_proposal)
|
||||
|
||||
set(SRC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/nnie_micro.cc
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/nnie_interfaces.cc
|
||||
${LITE_DIR}/providers/nnie/src/nnie_common.cc
|
||||
${LITE_DIR}/providers/nnie/src/nnie_memory.cc
|
||||
${LITE_DIR}/providers/nnie/src/nnie_print.cc
|
||||
${LITE_DIR}/providers/nnie_proposal/src/proposal.cc)
|
||||
|
||||
add_library(micro_nnie SHARED ${SRC})
|
||||
target_link_libraries(micro_nnie nnie mpi VoiceEngine upvqe dnvqe securec)
|
|
@ -0,0 +1,232 @@
|
|||
/**
|
||||
* Copyright 2022 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 "nnie/nnie_interfaces.h"
|
||||
#include <vector>
|
||||
#include "src/nnie_print.h"
|
||||
#include "include/hi_common.h"
|
||||
#include "include/hi_comm_vb.h"
|
||||
#include "include/mpi_sys.h"
|
||||
#include "include/mpi_vb.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace nnie {
|
||||
constexpr int kNNIEMaxNameLen = 128;
|
||||
|
||||
static int FillRoiPooling(NnieRunCfg *cfg, NnieTensors *inputs, int idx) {
|
||||
int *roi_shape = inputs->shape_[idx];
|
||||
if (roi_shape[1] != NNIE_COORDI_NUM) {
|
||||
LOGE("Roi shape err!");
|
||||
return HI_FAILURE;
|
||||
}
|
||||
|
||||
if (roi_shape[0] > (int64_t)(cfg->cfg_.max_roi_num_)) {
|
||||
LOGE("NNIE_RUNTIME_CONFIG_PATH: The maximum [max_roi_num] value set is less than the actual value: %d < %d.",
|
||||
cfg->cfg_.max_roi_num_, (int)(roi_shape[0]));
|
||||
return HI_FAILURE;
|
||||
}
|
||||
cfg->param_.rpn_bbox_.unShape.stWhc.u32Height = roi_shape[0];
|
||||
HI_U32 dst_stride = cfg->param_.rpn_bbox_.u32Stride;
|
||||
HI_S32 *proposal_result = NNIE_CONVERT_64BIT_ADDR(HI_S32, cfg->param_.rpn_bbox_.u64VirAddr);
|
||||
float *float_src_data = reinterpret_cast<float *>(inputs->data_[idx]);
|
||||
constexpr int kIndexLeft = 0;
|
||||
constexpr int kIndexRight = 1;
|
||||
constexpr int kIndexWidth = 2;
|
||||
constexpr int kIndexHeight = 3;
|
||||
for (size_t j = 0; j < cfg->param_.rpn_bbox_.unShape.stWhc.u32Height; j++) {
|
||||
proposal_result[dst_stride / sizeof(HI_U32) * j + kIndexLeft] = *(float_src_data++) * NNIE_QUANT_BASE;
|
||||
proposal_result[dst_stride / sizeof(HI_U32) * j + kIndexRight] = *(float_src_data++) * NNIE_QUANT_BASE;
|
||||
proposal_result[dst_stride / sizeof(HI_U32) * j + kIndexWidth] = *(float_src_data++) * NNIE_QUANT_BASE;
|
||||
proposal_result[dst_stride / sizeof(HI_U32) * j + kIndexHeight] = *(float_src_data++) * NNIE_QUANT_BASE;
|
||||
}
|
||||
NnieMemFlushCache(cfg->param_.rpn_bbox_.u64PhyAddr,
|
||||
NNIE_CONVERT_64BIT_ADDR(HI_VOID, cfg->param_.rpn_bbox_.u64VirAddr),
|
||||
dst_stride * cfg->param_.rpn_bbox_.unShape.stWhc.u32Height);
|
||||
|
||||
return HI_SUCCESS;
|
||||
}
|
||||
|
||||
int NnieInit(NnieHandle *h, NnieTensors *inputs) {
|
||||
NnieModel *model = &(h->cfg_.model_);
|
||||
|
||||
if (inputs->size_ <= 1) {
|
||||
LOGE("inputs size need greater than 1!");
|
||||
return HI_FAILURE;
|
||||
}
|
||||
if (NnieLoadModel(h->model_buf_, h->buf_size_, model) != HI_SUCCESS) {
|
||||
LOGE("NnieLoadModel failed!");
|
||||
return HI_FAILURE;
|
||||
}
|
||||
|
||||
std::vector<int64_t> input_shape;
|
||||
for (int i = 0; i < inputs->shape_len_[0]; i++) {
|
||||
input_shape.push_back(inputs->shape_[0][i]);
|
||||
}
|
||||
if (NnieCommCreate(&h->cfg_, input_shape) != HI_SUCCESS) {
|
||||
NnieCommDelete(&h->cfg_.param_, &h->cfg_.model_);
|
||||
return HI_FAILURE;
|
||||
}
|
||||
return HI_SUCCESS;
|
||||
}
|
||||
|
||||
static size_t GetFillIndex(char **input_name, size_t input_size, const HI_CHAR *name) {
|
||||
char prefix[kNNIEMaxNameLen];
|
||||
size_t i;
|
||||
for (i = 0; i < input_size; ++i) {
|
||||
char *post = strrchr(input_name[i], '_');
|
||||
if (post && (!strcmp(post + 1, "pre") || !strcmp(post + 1, "post"))) {
|
||||
HI_U32 prefix_len = (HI_U32)(post - input_name[i]);
|
||||
if (prefix_len >= kNNIEMaxNameLen) return input_size;
|
||||
strncpy(prefix, input_name[i], prefix_len);
|
||||
prefix[prefix_len] = '\0';
|
||||
if (strcmp(prefix, name) == 0) break;
|
||||
} else {
|
||||
if (strcmp(input_name[i], name) == 0) break;
|
||||
}
|
||||
}
|
||||
if (i == input_size) {
|
||||
for (i = 0; i < input_size; ++i) {
|
||||
char *post = strrchr(input_name[i], '_');
|
||||
if (post && (!strcmp(post + 1, "pre") || !strcmp(post + 1, "post"))) {
|
||||
HI_U32 prefix_len = (HI_U32)(post - input_name[i]);
|
||||
if (prefix_len >= kNNIEMaxNameLen) return input_size;
|
||||
strncpy(prefix, input_name[i], prefix_len);
|
||||
prefix[prefix_len] = '\0';
|
||||
if (strncmp(prefix, name, prefix_len) == 0) break;
|
||||
} else {
|
||||
if (strncmp(input_name[i], name, strlen(input_name[i])) == 0) break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
int NnieFillData(NnieHandle *h, NnieTensors *inputs) {
|
||||
SVP_NNIE_MODEL_S *model = h->cfg_.param_.model_;
|
||||
unsigned int seg_id = h->cfg_.run_idx_.seg_idx_;
|
||||
bool run_box = false;
|
||||
size_t i, j;
|
||||
if (model->astSeg[seg_id].enNetType == SVP_NNIE_NET_TYPE_ROI) {
|
||||
run_box = true;
|
||||
for (i = 0; i < static_cast<size_t>(inputs->size_); i++) {
|
||||
if (!strcmp(inputs->name_[i], "proposal")) {
|
||||
if (FillRoiPooling(&h->cfg_, inputs, i)) {
|
||||
return HI_FAILURE;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i == static_cast<size_t>(inputs->size_)) {
|
||||
LOGE("Can't find proposal out!");
|
||||
return HI_FAILURE;
|
||||
}
|
||||
} else if (inputs->size_ != model->astSeg[seg_id].u16SrcNum) {
|
||||
LOGE("Input Size Err!");
|
||||
return HI_FAILURE;
|
||||
}
|
||||
|
||||
for (i = 0; i < model->astSeg[seg_id].u16SrcNum; i++) {
|
||||
if (h->cfg_.param_.mem_cfg_.seg_[seg_id].src_node_[i]) {
|
||||
continue;
|
||||
}
|
||||
j = GetFillIndex(inputs->name_, inputs->size_, model->astSeg[seg_id].astSrcNode[i].szName);
|
||||
if (j == static_cast<size_t>(inputs->size_)) {
|
||||
if (run_box && !strcmp(inputs->name_[i], "proposal")) {
|
||||
continue;
|
||||
} else {
|
||||
j = i;
|
||||
LOGW("input tensor name(%s) can't match wk node name(%s).", inputs->name_[i],
|
||||
model->astSeg[seg_id].astSrcNode[i].szName);
|
||||
}
|
||||
}
|
||||
|
||||
auto input_data_type = inputs->dtype_[j];
|
||||
SVP_BLOB_TYPE_E src_type = h->cfg_.param_.seg_data_[seg_id].src_[i].enType;
|
||||
if (SVP_BLOB_TYPE_U8 <= src_type && src_type <= SVP_BLOB_TYPE_YVU422SP) {
|
||||
if (!(input_data_type == NnieDataType::NNIE_INT8 || input_data_type == NnieDataType::NNIE_UINT8)) {
|
||||
LOGE("Nnie input node type error!");
|
||||
return HI_FAILURE;
|
||||
}
|
||||
} else {
|
||||
if (input_data_type != NnieDataType::NNIE_FLOAT32) {
|
||||
LOGE("Nnie input node type error!");
|
||||
return HI_FAILURE;
|
||||
}
|
||||
}
|
||||
HI_U32 input_size = 1;
|
||||
for (int n = 0; n < inputs->shape_len_[j]; n++) {
|
||||
input_size *= inputs->shape_[j][n];
|
||||
}
|
||||
if (NnieCommFillData(&h->cfg_, inputs->data_[j], input_size, i) != HI_SUCCESS) {
|
||||
LOGE("FillData failed!");
|
||||
return HI_FAILURE;
|
||||
}
|
||||
}
|
||||
return HI_SUCCESS;
|
||||
}
|
||||
|
||||
int NnieRun(NnieHandle *h, NnieTensors *outputs) {
|
||||
SVP_NNIE_MODEL_S *model = h->cfg_.param_.model_;
|
||||
unsigned int seg_id = h->cfg_.run_idx_.seg_idx_;
|
||||
bool run_box = false;
|
||||
int i, j;
|
||||
if (model->astSeg[seg_id].enNetType == SVP_NNIE_NET_TYPE_ROI) {
|
||||
run_box = true;
|
||||
}
|
||||
|
||||
if (NnieCommRun(&h->cfg_, run_box)) {
|
||||
LOGE("Nnie Run Fail!");
|
||||
return HI_FAILURE;
|
||||
}
|
||||
|
||||
// Get output data
|
||||
if (outputs->size_ != model->astSeg[seg_id].u16DstNum) {
|
||||
LOGE("seg%d: %d output tensors are required, but there are %d outputs.", seg_id, model->astSeg[seg_id].u16DstNum,
|
||||
outputs->size_);
|
||||
return HI_FAILURE;
|
||||
}
|
||||
for (i = 0; i < model->astSeg[seg_id].u16DstNum; i++) {
|
||||
if (h->cfg_.param_.mem_cfg_.seg_[seg_id].dst_node_[i]) {
|
||||
continue;
|
||||
}
|
||||
j = GetFillIndex(outputs->name_, outputs->size_, model->astSeg[seg_id].astDstNode[i].szName);
|
||||
if (j == outputs->size_) {
|
||||
j = i;
|
||||
LOGW("output tensor name(%s) can't match wk node name(%s).", outputs->name_[j],
|
||||
model->astSeg[seg_id].astDstNode[i].szName);
|
||||
}
|
||||
if (outputs->dtype_[j] == NNIE_FLOAT32) {
|
||||
HI_U32 output_size = 1;
|
||||
for (int n = 0; n < outputs->shape_len_[j]; n++) {
|
||||
output_size *= outputs->shape_[j][n];
|
||||
}
|
||||
if (NnieCommGetOutputData(&h->cfg_, reinterpret_cast<float *>(outputs->data_[j]), output_size, i) != HI_SUCCESS) {
|
||||
return HI_FAILURE;
|
||||
}
|
||||
} else {
|
||||
LOGE("Unsupported DataType!");
|
||||
return HI_FAILURE;
|
||||
}
|
||||
}
|
||||
return HI_SUCCESS;
|
||||
}
|
||||
|
||||
void NnieClose(NnieHandle *h) {
|
||||
NnieCommDelete(&h->cfg_.param_, &h->cfg_.model_);
|
||||
h->load_model_ = 0;
|
||||
}
|
||||
} // namespace nnie
|
||||
} // namespace mindspore
|
|
@ -0,0 +1,48 @@
|
|||
/**
|
||||
* Copyright 2022 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_CONVERTER_MICRO_PROVIDERS_NNIE_NNIE_INTERFACES_H_
|
||||
#define MINDSPORE_LITE_TOOLS_CONVERTER_MICRO_PROVIDERS_NNIE_NNIE_INTERFACES_H_
|
||||
|
||||
#include "src/nnie_common.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace nnie {
|
||||
typedef struct {
|
||||
int load_model_;
|
||||
int roi_used_;
|
||||
char *model_buf_;
|
||||
int buf_size_;
|
||||
NnieRunCfg cfg_;
|
||||
} NnieHandle;
|
||||
|
||||
typedef enum { NNIE_INT8, NNIE_UINT8, NNIE_FLOAT32 } NnieDataType;
|
||||
|
||||
typedef struct {
|
||||
void *data_[SVP_NNIE_MAX_INPUT_NUM];
|
||||
char *name_[SVP_NNIE_MAX_INPUT_NUM];
|
||||
int *shape_[SVP_NNIE_MAX_INPUT_NUM];
|
||||
int shape_len_[SVP_NNIE_MAX_INPUT_NUM];
|
||||
NnieDataType dtype_[SVP_NNIE_MAX_INPUT_NUM];
|
||||
int size_;
|
||||
} NnieTensors;
|
||||
|
||||
int NnieInit(NnieHandle *h, NnieTensors *inputs);
|
||||
int NnieFillData(NnieHandle *h, NnieTensors *inputs);
|
||||
int NnieRun(NnieHandle *h, NnieTensors *outputs);
|
||||
void NnieClose(NnieHandle *h);
|
||||
} // namespace nnie
|
||||
} // namespace mindspore
|
||||
#endif // MINDSPORE_LITE_TOOLS_CONVERTER_MICRO_PROVIDERS_NNIE_NNIE_INTERFACES_H_
|
|
@ -0,0 +1,220 @@
|
|||
/**
|
||||
* Copyright 2022 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 "nnie/nnie_micro.h"
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <algorithm>
|
||||
#include <numeric>
|
||||
#include <map>
|
||||
#include <functional>
|
||||
#include "nnie/nnie_interfaces.h"
|
||||
#include "src/proposal.h"
|
||||
#include "include/ir/dtype/type_id.h"
|
||||
#include "include/c_api/status_c.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace {
|
||||
using nnie::NnieDataType;
|
||||
using nnie::NnieTensors;
|
||||
constexpr auto ENV_TIME_STEP = "TIME_STEP";
|
||||
constexpr auto ENV_MAX_ROI_NUM = "MAX_ROI_NUM";
|
||||
constexpr int kNumInput2 = 2;
|
||||
constexpr int kDefaultROINum = 300;
|
||||
constexpr int kNCHWDims = 4;
|
||||
constexpr int kNCHWFormatH = 2;
|
||||
constexpr int kNCHWFormatW = 3;
|
||||
constexpr int kNCHWFormatC = 1;
|
||||
static std::map<int, size_t> data_type_size_map = {{mindspore::kNumberTypeInt8, sizeof(int8_t)},
|
||||
{mindspore::kNumberTypeUInt8, sizeof(uint8_t)},
|
||||
{mindspore::kNumberTypeFloat32, sizeof(float)}};
|
||||
|
||||
int MakeTensorList(TensorC *tensors, int tensor_num, NnieTensors *tensor_list) {
|
||||
if (tensor_num > SVP_NNIE_MAX_INPUT_NUM) {
|
||||
printf("tensors' number is larger than 16\n");
|
||||
return kMSStatusLiteError;
|
||||
}
|
||||
tensor_list->size_ = tensor_num;
|
||||
for (int i = 0; i < tensor_num; ++i) {
|
||||
tensor_list->data_[i] = tensors[i].data_;
|
||||
tensor_list->shape_[i] = tensors[i].shape_;
|
||||
tensor_list->shape_len_[i] = tensors[i].shape_size_;
|
||||
tensor_list->name_[i] = tensors[i].name_;
|
||||
switch (tensors[i].data_type_) {
|
||||
case mindspore::kNumberTypeInt8:
|
||||
tensor_list->dtype_[i] = NnieDataType::NNIE_INT8;
|
||||
break;
|
||||
case mindspore::kNumberTypeUInt8:
|
||||
tensor_list->dtype_[i] = NnieDataType::NNIE_UINT8;
|
||||
break;
|
||||
case mindspore::kNumberTypeFloat32:
|
||||
tensor_list->dtype_[i] = NnieDataType::NNIE_FLOAT32;
|
||||
break;
|
||||
default:
|
||||
printf("The tensor's data type is unsupported, %d\n", tensors[i].data_type_);
|
||||
return kMSStatusLiteError;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool GetIntCustomAttr(const char *key, int *value, CustomParameter *param) {
|
||||
for (int i = 0; i < param->attr_num; ++i) {
|
||||
if (!strcmp(param->attr_name[i], key)) {
|
||||
*value = atoi(param->attr_data[i]);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static int GetIntEnv(const char *env_key, int default_data) {
|
||||
auto *env_data = std::getenv(env_key);
|
||||
int result = default_data;
|
||||
if (env_data != nullptr) {
|
||||
auto iter = std::find_if(env_data, env_data + strlen(env_data), [](char val) { return val < '0' || val > '9'; });
|
||||
if (iter != env_data) {
|
||||
*iter = '\0';
|
||||
result = atoi(env_data);
|
||||
} else {
|
||||
printf("%s ENV is invalid, now set to default value %d", env_key, default_data);
|
||||
}
|
||||
} else {
|
||||
printf("%s ENV is invalid, now set to default value %d", env_key, default_data);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
} // namespace
|
||||
namespace nnie {
|
||||
static int NnieKernel(TensorC *inputs, int input_num, TensorC *outputs, int output_num, CustomParameter *param) {
|
||||
int id;
|
||||
if (!GetIntCustomAttr("id", &id, param)) {
|
||||
printf("Not find the id attr!\n");
|
||||
return kMSStatusLiteError;
|
||||
}
|
||||
|
||||
static NnieHandle handle = {
|
||||
.load_model_ = 0,
|
||||
.roi_used_ = 0,
|
||||
};
|
||||
handle.model_buf_ = reinterpret_cast<char *>(inputs[input_num - 1].data_);
|
||||
if (data_type_size_map.find(inputs[input_num - 1].data_type_) == data_type_size_map.end()) {
|
||||
printf("Unsupported data type: %d\n", inputs[input_num - 1].data_type_);
|
||||
return kMSStatusLiteError;
|
||||
}
|
||||
size_t data_type_size = data_type_size_map.at(inputs[input_num - 1].data_type_);
|
||||
handle.buf_size_ =
|
||||
std::accumulate(inputs[input_num - 1].shape_, inputs[input_num - 1].shape_ + inputs[input_num - 1].shape_size_,
|
||||
data_type_size, std::multiplies<int>());
|
||||
handle.cfg_.run_idx_.seg_idx_ = id;
|
||||
NnieTensors input_list;
|
||||
if (MakeTensorList(inputs, input_num - 1, &input_list)) return kMSStatusLiteError;
|
||||
if (!handle.load_model_) {
|
||||
handle.cfg_.cfg_.max_roi_num_ = GetIntEnv(ENV_MAX_ROI_NUM, kDefaultROINum);
|
||||
handle.cfg_.cfg_.step_ = GetIntEnv(ENV_TIME_STEP, 1);
|
||||
if (NnieInit(&handle, &input_list) != HI_SUCCESS) return kMSStatusLiteError;
|
||||
handle.load_model_ = 1;
|
||||
}
|
||||
if (NnieFillData(&handle, &input_list) != HI_SUCCESS) return kMSStatusLiteError;
|
||||
NnieTensors output_list;
|
||||
if (MakeTensorList(outputs, output_num, &output_list)) return kMSStatusLiteError;
|
||||
if (NnieRun(&handle, &output_list) != HI_SUCCESS) return kMSStatusLiteError;
|
||||
return 0;
|
||||
}
|
||||
} // namespace nnie
|
||||
|
||||
namespace proposal {
|
||||
static int ProposalKernel(TensorC *inputs, int input_num, TensorC *outputs, int output_num, CustomParameter *param) {
|
||||
int ndims, image_height, image_width;
|
||||
if (input_num != kNumInput2) {
|
||||
printf("inputs tensor num error.\n");
|
||||
return kMSStatusLiteError;
|
||||
}
|
||||
if (output_num != 1) {
|
||||
LOGE("outputs tensor num error.");
|
||||
return kMSStatusLiteError;
|
||||
}
|
||||
if (!GetIntCustomAttr("proposal_id", &ndims, param)) {
|
||||
printf("Can't find the proposal_id attr!\n");
|
||||
return kMSStatusLiteError;
|
||||
}
|
||||
if (!GetIntCustomAttr("image_height", &image_height, param)) {
|
||||
printf("Can't find the image_height attr!\n");
|
||||
return kMSStatusLiteError;
|
||||
}
|
||||
if (!GetIntCustomAttr("image_width", &image_width, param)) {
|
||||
printf("Can't find the image_width attr!\n");
|
||||
return kMSStatusLiteError;
|
||||
}
|
||||
int max_roi_num_int = GetIntEnv(ENV_MAX_ROI_NUM, kDefaultROINum);
|
||||
ProposalParam pparam;
|
||||
memset(&pparam, 0, sizeof(ProposalParam));
|
||||
|
||||
std::vector<std::string> proposal_input{"rpn_cls_score", "rpn_bbox_pred"};
|
||||
TensorC *reorder_inputs[kNumInput2];
|
||||
for (size_t i = 0; i < proposal_input.size(); ++i) {
|
||||
for (int j = 0; j < input_num; ++j) {
|
||||
if (proposal_input[i] == inputs[j].name_) {
|
||||
reorder_inputs[i] = &inputs[j];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < input_num; i++) {
|
||||
auto ptr_shape = reorder_inputs[i]->shape_;
|
||||
if ((reorder_inputs[i]->shape_size_ == kNCHWDims)) {
|
||||
pparam.inputs_height_[i] = ptr_shape[kNCHWFormatH];
|
||||
pparam.inputs_width_[i] = ptr_shape[kNCHWFormatW];
|
||||
pparam.inputs_channel_[i] = ptr_shape[kNCHWFormatC];
|
||||
if (i == 0) {
|
||||
pparam.inputs_stride_ = ptr_shape[kNCHWFormatW] * sizeof(float);
|
||||
}
|
||||
} else {
|
||||
printf("proposal only support input shape size == 4.\n");
|
||||
return kMSStatusLiteError;
|
||||
}
|
||||
}
|
||||
if (ProposalInit(&pparam, max_roi_num_int, image_height, image_width)) {
|
||||
printf("proposal init failed!\n");
|
||||
return kMSStatusLiteError;
|
||||
}
|
||||
for (int i = 0; i < kNumInput2; i++) {
|
||||
pparam.inputs_[i] = reinterpret_cast<float *>(reorder_inputs[i]->data_);
|
||||
}
|
||||
pparam.rpn_bounding_box_.data_ = outputs[0].data_;
|
||||
if (ProposalRun(&pparam)) {
|
||||
printf("proposal run failed!\n");
|
||||
return kMSStatusLiteError;
|
||||
}
|
||||
|
||||
ProposalDeInit(&pparam);
|
||||
return 0;
|
||||
}
|
||||
} // namespace proposal
|
||||
} // namespace mindspore
|
||||
|
||||
int CustomKernel(TensorC *inputs, int input_num, TensorC *outputs, int output_num, CustomParameter *param) {
|
||||
if (!strcmp(param->type, "NNIE")) {
|
||||
return mindspore::nnie::NnieKernel(inputs, input_num, outputs, output_num, param);
|
||||
} else if (!strcmp(param->type, "Proposal")) {
|
||||
return mindspore::proposal::ProposalKernel(inputs, input_num, outputs, output_num, param);
|
||||
} else {
|
||||
printf("Unknown custom op type: %s\n", param->type);
|
||||
return kMSStatusLiteError;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
/**
|
||||
* Copyright 2022 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_CONVERTER_MICRO_PROVIDERS_NNIE_NNIE_MICRO_H_
|
||||
#define MINDSPORE_LITE_TOOLS_CONVERTER_MICRO_PROVIDERS_NNIE_NNIE_MICRO_H_
|
||||
|
||||
#include "nnacl/custom_parameter.h"
|
||||
#include "nnacl/tensor_c.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int CustomKernel(TensorC *inputs, int input_num, TensorC *outputs, int output_num, CustomParameter *param);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // MINDSPORE_LITE_TOOLS_CONVERTER_MICRO_PROVIDERS_NNIE_NNIE_MICRO_H_
|
Loading…
Reference in New Issue