From 46d5764e7f59105826456bafc15aa6019bcd3e8d Mon Sep 17 00:00:00 2001 From: zhujingxuan Date: Tue, 9 Mar 2021 11:08:29 +0800 Subject: [PATCH] use raw string --- mindspore/lite/micro/CMakeLists.txt | 1 + mindspore/lite/micro/cmake/file_list.cmake | 8 +- mindspore/lite/micro/coder/CMakeLists.txt | 2 +- .../component/const_blocks/cmake_lists.cc | 141 +++++++++ .../component/const_blocks/cmake_lists.h | 103 +------ .../component/const_blocks/debug_utils.cc | 275 ++++++++++++++++++ .../component/const_blocks/debug_utils.h | 256 +--------------- .../component/const_blocks/license.cc | 40 +++ .../component/const_blocks/license.h | 18 +- .../component/const_blocks/load_input.cc | 147 ++++++++++ .../component/const_blocks/load_input.h | 126 +------- .../component/const_blocks/micro_tensor.cc | 111 +++++++ .../component/const_blocks/micro_tensor.h | 93 +----- .../component/const_blocks/thread_pool.cc | 99 +++++++ .../component/const_blocks/thread_pool.h | 77 +---- .../coder/operator_library/CMakeLists.txt | 22 +- 16 files changed, 862 insertions(+), 657 deletions(-) create mode 100644 mindspore/lite/micro/coder/generator/component/const_blocks/cmake_lists.cc create mode 100644 mindspore/lite/micro/coder/generator/component/const_blocks/debug_utils.cc create mode 100644 mindspore/lite/micro/coder/generator/component/const_blocks/license.cc create mode 100644 mindspore/lite/micro/coder/generator/component/const_blocks/load_input.cc create mode 100644 mindspore/lite/micro/coder/generator/component/const_blocks/micro_tensor.cc create mode 100644 mindspore/lite/micro/coder/generator/component/const_blocks/thread_pool.cc diff --git a/mindspore/lite/micro/CMakeLists.txt b/mindspore/lite/micro/CMakeLists.txt index 912acff2ce..25b9be4c91 100644 --- a/mindspore/lite/micro/CMakeLists.txt +++ b/mindspore/lite/micro/CMakeLists.txt @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.14) project(micro) option(BUILD_TESTCASES "if build testcase" on) +option(ENABLE_CONVERTER "turn it on to compile codegen" on) string(REPLACE "/mindspore/lite/micro" "" TOP_DIR ${CMAKE_CURRENT_SOURCE_DIR}) set(CMAKE_CXX_STANDARD 17) diff --git a/mindspore/lite/micro/cmake/file_list.cmake b/mindspore/lite/micro/cmake/file_list.cmake index 70d2818dee..ed24e68af6 100644 --- a/mindspore/lite/micro/cmake/file_list.cmake +++ b/mindspore/lite/micro/cmake/file_list.cmake @@ -25,6 +25,12 @@ set(CODER_GENERATOR_SRC ${MICRO_DIR}/coder/generator/component/cmake_component.cc ${MICRO_DIR}/coder/generator/component/train_component.cc ${MICRO_DIR}/coder/generator/component/parallel_component.cc + ${MICRO_DIR}/coder/generator/component/const_blocks/cmake_lists.cc + ${MICRO_DIR}/coder/generator/component/const_blocks/debug_utils.cc + ${MICRO_DIR}/coder/generator/component/const_blocks/license.cc + ${MICRO_DIR}/coder/generator/component/const_blocks/load_input.cc + ${MICRO_DIR}/coder/generator/component/const_blocks/micro_tensor.cc + ${MICRO_DIR}/coder/generator/component/const_blocks/thread_pool.cc ) set(MINDSPORE_CORE @@ -305,7 +311,6 @@ set(LITE_KERNEL_SRC if("${X86_64_SIMD}" STREQUAL "sse") set(SSE_SRC ${LITE_DIR}/nnacl/intrinsics/sse/sse_common.c - ${LITE_DIR}/nnacl/intrinsics/sse/PackNHWCToNCHWFp32.c ${LITE_DIR}/nnacl/intrinsics/sse/MatMul_Sse.c ) set_property(SOURCE ${SSE_SRC} PROPERTY LANGUAGE C) @@ -319,7 +324,6 @@ if("${X86_64_SIMD}" STREQUAL "avx") ${LITE_DIR}/nnacl/intrinsics/avx/common_utils.c ${LITE_DIR}/nnacl/intrinsics/sse/sse_common.c ${LITE_DIR}/nnacl/intrinsics/sse/MatMul_Sse.c - ${LITE_DIR}/nnacl/intrinsics/sse/PackNHWCToNCHWFp32.c ${LITE_DIR}/nnacl/assembly/avx/MatmulAvx.S ) set_property(SOURCE ${AVX_SRC} PROPERTY LANGUAGE C) diff --git a/mindspore/lite/micro/coder/CMakeLists.txt b/mindspore/lite/micro/coder/CMakeLists.txt index 70ef0b23b6..75ff29ae51 100644 --- a/mindspore/lite/micro/coder/CMakeLists.txt +++ b/mindspore/lite/micro/coder/CMakeLists.txt @@ -25,7 +25,7 @@ include(${MICRO_DIR}/cmake/file_list.cmake) include(${MICRO_DIR}/cmake/package_wrapper.cmake) add_subdirectory(operator_library) -if(NOT PLATFORM_ARM32 AND NOT PLATFORM_ARM64) +if(ENABLE_CONVERTER) add_executable(codegen main.cc ${FILE_SET}) add_dependencies(codegen fbs_src) add_dependencies(codegen fbs_inner_src) diff --git a/mindspore/lite/micro/coder/generator/component/const_blocks/cmake_lists.cc b/mindspore/lite/micro/coder/generator/component/const_blocks/cmake_lists.cc new file mode 100644 index 0000000000..595d186f32 --- /dev/null +++ b/mindspore/lite/micro/coder/generator/component/const_blocks/cmake_lists.cc @@ -0,0 +1,141 @@ +/** + * 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 "coder/generator/component/const_blocks/cmake_lists.h" + +namespace mindspore::lite::micro { + +const char *bench_cmake_lists_txt = R"RAW( +cmake_minimum_required(VERSION 3.14) +project(benchmark) + +message("project name: ${MODEL_LIB_PATH}") + +function(parse_lib_info lib_full_path lib_name lib_path) + string(FIND "${lib_full_path}" "/" POS REVERSE) + math(EXPR POS "${POS} + 1") + string(SUBSTRING ${lib_full_path} 0 ${POS} path) + set(${lib_path} ${path} PARENT_SCOPE) + string(SUBSTRING ${lib_full_path} "${POS}" "-1" name) + set(${lib_name} ${name} PARENT_SCOPE) +endfunction(parse_lib_info) + +parse_lib_info(${MODEL_LIB} MODEL_LIB_NAME MODEL_LIB_PATH) + +option(MICRO_BUILD_ARM64 "build android arm64" OFF) +option(MICRO_BUILD_ARM32A "build android arm32" OFF) + +if(MICRO_BUILD_ARM64 OR MICRO_BUILD_ARM32A) + add_compile_definitions(ENABLE_NEON) + add_compile_definitions(ENABLE_ARM) +endif() + +if(MICRO_BUILD_ARM64) + add_compile_definitions(ENABLE_ARM64) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=armv8.2-a+dotprod") +endif() + +if(MICRO_BUILD_ARM32A) + add_compile_definitions(ENABLE_ARM32) + add_definitions(-mfloat-abi=softfp -mfpu=neon) +endif() + +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include) + +set(CMAKE_C_FLAGS "${CMAKE_ENABLE_C99} ${CMAKE_C_FLAGS}") +if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug") + message("*******************${CMAKE_BUILD_TYPE}**********") + set(CMAKE_C_FLAGS "-DDebug -g -fPIC -fPIE -fvisibility=default ${CMAKE_C_FLAGS}") +else() + set(CMAKE_C_FLAGS "-fPIC -fPIE -O3 -fstack-protector-strong -fomit-frame-pointer ${CMAKE_C_FLAGS}") + set(CMAKE_C_FLAGS_Release "${CMAKE_C_FLAGS_Release} -O3 -ffunction-sections -fdata-sections") +endif() +link_directories(${MODEL_LIB_PATH}) +include(benchmark.cmake) +add_executable(benchmark ${SRC_FILES}) +target_link_libraries(benchmark ${MODEL_LIB_NAME} -lm -pthread) + +)RAW"; + +const char *src_cmake_lists_txt = R"RAW( +cmake_minimum_required(VERSION 3.14) +project(net) + +message("operator lib path: ${OP_LIB}") +message("operator header path: ${OP_HEADER_PATH}") + +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include) +include_directories(${OP_HEADER_PATH}) + +include(net.cmake) + +option(MICRO_BUILD_ARM64 "build android arm64" OFF) +option(MICRO_BUILD_ARM32A "build android arm32" OFF) + +if(MICRO_BUILD_ARM64 OR MICRO_BUILD_ARM32A) + add_compile_definitions(ENABLE_NEON) + add_compile_definitions(ENABLE_ARM) +endif() + +if(MICRO_BUILD_ARM64) + add_compile_definitions(ENABLE_ARM64) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=armv8.2-a+dotprod") +endif() + +if(MICRO_BUILD_ARM32A) + add_compile_definitions(ENABLE_ARM32) + add_definitions(-mfloat-abi=softfp -mfpu=neon) +endif() + +set(CMAKE_C_FLAGS "${CMAKE_ENABLE_C99} ${CMAKE_C_FLAGS}") +if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug") + set(CMAKE_C_FLAGS "-DDebug -g -fPIC -fPIE -fvisibility=default ${CMAKE_C_FLAGS}") +else() + set(CMAKE_C_FLAGS "-fPIC -fPIE -O3 -Werror -fstack-protector-strong -fomit-frame-pointer ${CMAKE_C_FLAGS}") + set(CMAKE_C_FLAGS_Release "${CMAKE_C_FLAGS_Release} -O3 -ffunction-sections -Werror -fdata-sections") + string(REPLACE "-g" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") +endif() + +function(create_library) + add_custom_command(TARGET net + POST_BUILD + COMMAND rm -rf tmp + COMMAND mkdir tmp + COMMAND cd tmp && ar -x ${OP_LIB} + COMMAND echo "raw static library ${library_name} size:" + COMMAND ls -lh ${library_name} + COMMAND mv ${library_name} ./tmp && cd tmp && ar -x ${library_name} + COMMENT "unzip raw static library ${library_name}" + ) + foreach(object_file ${OP_SRC}) + add_custom_command(TARGET net POST_BUILD COMMAND mv ./tmp/${object_file} .) + endforeach() + add_custom_command(TARGET net + POST_BUILD + COMMAND ar cr ${library_name} *.o + COMMAND ranlib ${library_name} + COMMAND echo "new static library ${library_name} size:" + COMMAND ls -lh ${library_name} + COMMAND rm -rf tmp && rm -rf *.o + COMMENT "generate specified static library ${library_name}" + ) +endfunction(create_library) +string(CONCAT library_name "lib" net ".a") +create_library() + +)RAW"; + +} // namespace mindspore::lite::micro diff --git a/mindspore/lite/micro/coder/generator/component/const_blocks/cmake_lists.h b/mindspore/lite/micro/coder/generator/component/const_blocks/cmake_lists.h index d9a07fffdc..f244e5e94b 100644 --- a/mindspore/lite/micro/coder/generator/component/const_blocks/cmake_lists.h +++ b/mindspore/lite/micro/coder/generator/component/const_blocks/cmake_lists.h @@ -14,101 +14,14 @@ * limitations under the License. */ -#ifndef MINDSPORE_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_CMAKE_LISTS_CODE_H_ -#define MICRO_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_CMAKE_LISTS_CODE_H_ +#ifndef MINDSPORE_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_CMAKE_LISTS_H_ +#define MINDSPORE_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_CMAKE_LISTS_H_ -const char *bench_cmake_lists_txt = - "cmake_minimum_required(VERSION 3.14)\n" - "project(benchmark)\n" - "\n" - "message(\"project name: ${MODEL_LIB_PATH}\")\n" - "message(\"architecture cmake file path: ${ARCH_CMAKE_PATH}\")\n" - "\n" - "function(parse_lib_info lib_full_path lib_name lib_path)\n" - " string(FIND \"${lib_full_path}\" \"/\" POS REVERSE)\n" - " math(EXPR POS \"${POS} + 1\")\n" - " string(SUBSTRING ${lib_full_path} 0 ${POS} path)\n" - " set(${lib_path} ${path} PARENT_SCOPE)\n" - " string(SUBSTRING ${lib_full_path} \"${POS}\" \"-1\" name)\n" - " set(${lib_name} ${name} PARENT_SCOPE)\n" - "endfunction(parse_lib_info)\n" - "\n" - "parse_lib_info(${MODEL_LIB} MODEL_LIB_NAME MODEL_LIB_PATH)\n" - "\n" - "if (\"${ARCH_CMAKE_PATH}\" STREQUAL \"\")\n" - " message(\"arch is x86_64\")\n" - "else ()\n" - " include(${ARCH_CMAKE_PATH})\n" - "endif ()\n" - "\n" - "include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include)\n" - "\n" - "set(CMAKE_C_FLAGS \"${CMAKE_ENABLE_C99} ${CMAKE_C_FLAGS}\")\n" - "if (\"${CMAKE_BUILD_TYPE}\" STREQUAL \"Debug\")\n" - " message(\"*******************${CMAKE_BUILD_TYPE}**********\")\n" - " set(CMAKE_C_FLAGS \"-DDebug -g -fPIC -fPIE -fvisibility=default ${CMAKE_C_FLAGS}\")\n" - "else ()\n" - " set(CMAKE_C_FLAGS \"-fPIC -fPIE -O3 -fstack-protector-strong -fomit-frame-pointer ${CMAKE_C_FLAGS}\")\n" - " set(CMAKE_C_FLAGS_Release \"${CMAKE_C_FLAGS_Release} -O3 -ffunction-sections -fdata-sections\")\n" - "endif ()\n" - "link_directories(${MODEL_LIB_PATH})\n" - "include(benchmark.cmake)\n" - "add_executable(benchmark ${SRC_FILES})\n" - "target_link_libraries(benchmark ${MODEL_LIB_NAME} -lm -pthread)\n"; +namespace mindspore::lite::micro { -const char *src_cmake_lists_txt = - "cmake_minimum_required(VERSION 3.14)\n" - "project(net)\n" - "\n" - "message(\"architecture cmake file path: ${ARCH_CMAKE_PATH}\")\n" - "message(\"operator lib path: ${OP_LIB}\")\n" - "message(\"operator header path: ${OP_HEADER_PATH}\")\n" - "\n" - "include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include)\n" - "include_directories(${OP_HEADER_PATH})\n" - "\n" - "include(net.cmake)\n" - "\n" - "if(\"${ARCH_CMAKE_PATH}\" STREQUAL \"\")\n" - " message(\"arch is x86_64\")\n" - "else()\n" - " include(${ARCH_CMAKE_PATH})\n" - "endif()\n" - "\n" - "set(CMAKE_C_FLAGS \"${CMAKE_ENABLE_C99} ${CMAKE_C_FLAGS}\")\n" - "if(\"${CMAKE_BUILD_TYPE}\" STREQUAL \"Debug\")\n" - " set(CMAKE_C_FLAGS \"-DDebug -g -fPIC -fPIE -fvisibility=default ${CMAKE_C_FLAGS}\")\n" - "else()\n" - " set(CMAKE_C_FLAGS \"-fPIC -fPIE -O3 -Werror -fstack-protector-strong -fomit-frame-pointer ${CMAKE_C_FLAGS}\")\n" - " set(CMAKE_C_FLAGS_Release \"${CMAKE_C_FLAGS_Release} -O3 -ffunction-sections -Werror -fdata-sections\")\n" - " string(REPLACE \"-g\" \"\" CMAKE_C_FLAGS \"${CMAKE_C_FLAGS}\")\n" - "endif()\n" - "\n" - "function(create_library)\n" - " add_custom_command(TARGET net\n" - " POST_BUILD\n" - " COMMAND rm -rf tmp\n" - " COMMAND mkdir tmp\n" - " COMMAND cd tmp && ar -x ${OP_LIB}\n" - " COMMAND echo \"raw static library ${library_name} size:\"\n" - " COMMAND ls -lh ${library_name}\n" - " COMMAND mv ${library_name} ./tmp && cd tmp && ar -x ${library_name}\n" - " COMMENT \"unzip raw static library ${library_name}\"\n" - " )\n" - " foreach (object_file ${OP_SRC})\n" - " add_custom_command(TARGET net POST_BUILD COMMAND mv ./tmp/${object_file} .)\n" - " endforeach ()\n" - " add_custom_command(TARGET net\n" - " POST_BUILD\n" - " COMMAND ar cr ${library_name} *.o\n" - " COMMAND ranlib ${library_name}\n" - " COMMAND echo \"new static library ${library_name} size:\"\n" - " COMMAND ls -lh ${library_name}\n" - " COMMAND rm -rf tmp && rm -rf *.o\n" - " COMMENT \"generate specified static library ${library_name}\"\n" - " )\n" - "endfunction(create_library)\n" - "string(CONCAT library_name \"lib\" net \".a\")\n" - "create_library()\n"; +extern const char *bench_cmake_lists_txt; +extern const char *src_cmake_lists_txt; -#endif // MINDSPORE_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_CMAKE_LISTS_CODE_H_ +} // namespace mindspore::lite::micro + +#endif // MINDSPORE_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_CMAKE_LISTS_H_ diff --git a/mindspore/lite/micro/coder/generator/component/const_blocks/debug_utils.cc b/mindspore/lite/micro/coder/generator/component/const_blocks/debug_utils.cc new file mode 100644 index 0000000000..2b3bab6557 --- /dev/null +++ b/mindspore/lite/micro/coder/generator/component/const_blocks/debug_utils.cc @@ -0,0 +1,275 @@ +/** + * 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 "coder/generator/component/const_blocks/debug_utils.h" + +namespace mindspore::lite::micro { + +const char *debug_utils_h = R"RAW( +/** + * 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_MICRO_MICRODEBUGUTIL_H_ +#define MINDSPORE_LITE_MICRO_MICRODEBUGUTIL_H_ + +#include +#include +#include +#include +#include "microtensor.h" + +void PrintTensor(MicroTensor *tensor, FILE *output_file, const char *is_input); + +void PrintTensorData(MicroTensor *tensor); + +uint64_t GetTimeUs(); + +#endif // MINDSPORE_LITE_MICRO_MICRODEBUGUTIL_H_ + +)RAW"; + +const char *debug_utils_c = R"RAW( +/** + * 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 "debug_utils.h" + +#define UP_DIV(x, y) (((x) + (y) - (1)) / (y)) + +static const unsigned int kPrintNums = 20; +static const unsigned int kLineSplitNum = 44; +static const unsigned int kLineNum = 45; +unsigned int GetTensorElementSize(const MicroTensor *tensor) { + unsigned int ans = 1; + if (tensor->format == Format_NC4HW4) { + for (unsigned int i = 0; i < tensor->ndim; ++i) { + unsigned int dim = tensor->dim[i]; + if (i == 1) { + dim = UP_DIV(dim, 4) * 4; + } + ans *= dim; + } + } else { + for (unsigned int i = 0; i < tensor->ndim; ++i) { + ans *= tensor->dim[i]; + } + } + return ans; +} + +static const char *const TypeNames[] = {"DT_FLOAT", "DT_FLOAT16", "DT_INT8", "DT_INT32", "DT_UINT8", "DT_INT16", + "", "", "DT_UINT32", "DT_INT64", "DT_UINT16", "", + "", "", "", "", "DT_UNDEFINED", ""}; + +const char *EnumNameFormat(enum Format e) { + switch (e) { + case Format_NCHW: + return "NCHW"; + case Format_NHWC: + return "NHWC"; + case Format_HWKC: + return "HWKC"; + case Format_HWCK: + return "HWCK"; + case Format_KCHW: + return "KCHW"; + case Format_CKHW: + return "CKHW"; + case Format_KHWC: + return "KHWC"; + case Format_CHWK: + return "CHWK"; + case Format_NC4HW4: + return "NC4HW4"; + case Format_NUM_OF_FORMAT: + return "NUM_OF_FORMAT"; + default: + return ""; + } +} + +void PrintTensorData(MicroTensor *tensor) { + void *data = tensor->data; + unsigned int elenums = GetTensorElementSize(tensor); + if (data == NULL || elenums == 0) { + MICRO_ERROR("print tensor data failed"); + return; + } + switch (tensor->type) { + case DataType_DT_FLOAT: { + float *addr = (float *)(data); + for (int i = 0; i < elenums && i < kPrintNums; ++i) { + printf("%f, ", addr[i]); + } + break; + } + case DataType_DT_INT32: { + int32_t *addr = (int32_t *)(data); + for (int i = 0; i < elenums && i < kPrintNums; ++i) { + printf("%d, ", addr[i]); + } + break; + } + case DataType_DT_INT8: { + int8_t *addr = (int8_t *)(data); + for (int i = 0; i < elenums && i < kPrintNums; ++i) { + printf("%d, ", addr[i]); + } + break; + } + case DataType_DT_UINT32: { + uint32_t *addr = (uint32_t *)(data); + for (int i = 0; i < elenums && i < kPrintNums; ++i) { + printf("%u, ", addr[i]); + } + break; + } + case DataType_DT_UINT8: { + uint8_t *addr = (uint8_t *)(data); + for (int i = 0; i < elenums && i < kPrintNums; ++i) { + printf("%u, ", addr[i]); + } + break; + } + default: + MICRO_ERROR("unsupported data type %d", tensor->type); + } + printf("\n"); +} + +void PrintDataToFile(const void *data, const size_t elenums, const enum DataType type, FILE *file) { + if (data == NULL || elenums == 0) { + MICRO_ERROR("print tensor data to file failed"); + return; + } + switch (type) { + case DataType_DT_FLOAT: { + float *addr = (float *)(data); + for (int i = 0; i < elenums; ++i) { + fprintf(file, "%0.15f, ", addr[i]); + if (i % kLineNum == kLineSplitNum) { + fprintf(file, "\n"); + } + } + break; + } + case DataType_DT_INT32: { + int32_t *addr = (int32_t *)(data); + for (int i = 0; i < elenums; ++i) { + fprintf(file, "%d, ", addr[i]); + if (i % kLineNum == kLineSplitNum) { + fprintf(file, "\n"); + } + } + break; + } + case DataType_DT_INT8: { + int8_t *addr = (int8_t *)(data); + for (int i = 0; i < elenums; ++i) { + fprintf(file, "%d, ", addr[i]); + if (i % kLineNum == kLineSplitNum) { + fprintf(file, "\n"); + } + } + break; + } + case DataType_DT_UINT32: { + uint32_t *addr = (uint32_t *)(data); + for (int i = 0; i < elenums; ++i) { + fprintf(file, "%u, ", addr[i]); + if (i % kLineNum == kLineSplitNum) { + fprintf(file, "\n"); + } + } + break; + } + case DataType_DT_UINT8: { + uint8_t *addr = (uint8_t *)(data); + for (int i = 0; i < elenums; ++i) { + fprintf(file, "%u, ", addr[i]); + if (i % kLineNum == kLineSplitNum) { + fprintf(file, "\n"); + } + } + break; + } + default: + MICRO_ERROR("unsupported data type %d", type); + } + fprintf(file, "\n"); +} + +void PrintTensor(MicroTensor *tensor, FILE *output_file, const char *is_input) { + if (output_file == NULL) { + MICRO_ERROR("output file is NULL"); + return; + } + fprintf(output_file, "%s ", is_input); + for (int i = 0; i < tensor->ndim; ++i) { + fprintf(output_file, "%u, ", tensor->dim[i]); + } + fprintf(output_file, "\n"); + + const char *type = TypeNames[tensor->type]; + const char *format = EnumNameFormat(tensor->format); + unsigned int tensorSize = GetTensorElementSize(tensor); + fprintf(output_file, "%s type:%s, format:%s, elementSize: %u\n", is_input, type, format, tensorSize); + fprintf(output_file, "%s Data:\n", is_input); + PrintDataToFile(tensor->data, tensorSize, tensor->type, output_file); + (void)fflush(output_file); +} + +uint64_t GetTimeUs() { + const int USEC = 1000000; + const int MSEC = 1000; + struct timespec ts = {0, 0}; + if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { + return 0; + } + uint64_t retval = (uint64_t)((ts.tv_sec * USEC) + (ts.tv_nsec / MSEC)); + return retval; +} + +)RAW"; + +} // namespace mindspore::lite::micro diff --git a/mindspore/lite/micro/coder/generator/component/const_blocks/debug_utils.h b/mindspore/lite/micro/coder/generator/component/const_blocks/debug_utils.h index 9af8eb6043..51f709af65 100644 --- a/mindspore/lite/micro/coder/generator/component/const_blocks/debug_utils.h +++ b/mindspore/lite/micro/coder/generator/component/const_blocks/debug_utils.h @@ -16,257 +16,11 @@ #ifndef MINDSPORE_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_DEBUG_UTILS_H_ #define MINDSPORE_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_DEBUG_UTILS_H_ -const char *debug_utils_h = - "/**\n" - " * Copyright 2021 Huawei Technologies Co., Ltd\n" - " *\n" - " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" - " * you may not use this file except in compliance with the License.\n" - " * You may obtain a copy of the License at\n" - " *\n" - " * http://www.apache.org/licenses/LICENSE-2.0\n" - " *\n" - " * Unless required by applicable law or agreed to in writing, software\n" - " * distributed under the License is distributed on an \"AS IS\" BASIS,\n" - " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" - " * See the License for the specific language governing permissions and\n" - " * limitations under the License.\n" - " */\n" - "\n" - "#ifndef MINDSPORE_LITE_MICRO_MICRODEBUGUTIL_H_\n" - "#define MINDSPORE_LITE_MICRO_MICRODEBUGUTIL_H_\n" - "\n" - "#include \n" - "#include \n" - "#include \n" - "#include \n" - "#include \"microtensor.h\"\n" - "\n" - "void PrintTensor(MicroTensor *tensor, FILE *output_file, const char *is_input);\n" - "\n" - "void PrintTensorData(MicroTensor *tensor);\n" - "\n" - "uint64_t GetTimeUs();\n" - "\n" - "#endif // MINDSPORE_LITE_MICRO_MICRODEBUGUTIL_H_\n"; +namespace mindspore::lite::micro { -const char *debug_utils_c = - "/**\n" - " * Copyright 2021 Huawei Technologies Co., Ltd\n" - " *\n" - " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" - " * you may not use this file except in compliance with the License.\n" - " * You may obtain a copy of the License at\n" - " *\n" - " * http://www.apache.org/licenses/LICENSE-2.0\n" - " *\n" - " * Unless required by applicable law or agreed to in writing, software\n" - " * distributed under the License is distributed on an \"AS IS\" BASIS,\n" - " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" - " * See the License for the specific language governing permissions and\n" - " * limitations under the License.\n" - " */\n" - "\n" - "#include \n" - "#include \"debug_utils.h\"\n" - "\n" - "#define UP_DIV(x, y) (((x) + (y) - (1)) / (y))\n" - "\n" - "static const unsigned int kPrintNums = 20;\n" - "static const unsigned int kLineSplitNum = 44;\n" - "static const unsigned int kLineNum = 45;\n" - "unsigned int GetTensorElementSize(const MicroTensor *tensor) {\n" - " unsigned int ans = 1;\n" - " if (tensor->format == Format_NC4HW4) {\n" - " for (unsigned int i = 0; i < tensor->ndim; ++i) {\n" - " unsigned int dim = tensor->dim[i];\n" - " if (i == 1) {\n" - " dim = UP_DIV(dim, 4) * 4;\n" - " }\n" - " ans *= dim;\n" - " }\n" - " } else {\n" - " for (unsigned int i = 0; i < tensor->ndim; ++i) {\n" - " ans *= tensor->dim[i];\n" - " }\n" - " }\n" - " return ans;\n" - "}\n" - "\n" - "static const char *const TypeNames[] = {\"DT_FLOAT\", \"DT_FLOAT16\", \"DT_INT8\", \"DT_INT32\", \"DT_UINT8\", " - " \"DT_INT16\",\n" - " \"\", \"\", \"DT_UINT32\", \"DT_INT64\", \"DT_UINT16\", " - " \"\",\n" - " \"\", \"\", \"\", \"\", " - "\"DT_UNDEFINED\", \"\"};\n" - "\n" - "const char *EnumNameFormat(enum Format e) {\n" - " switch (e) {\n" - " case Format_NCHW:\n" - " return \"NCHW\";\n" - " case Format_NHWC:\n" - " return \"NHWC\";\n" - " case Format_HWKC:\n" - " return \"HWKC\";\n" - " case Format_HWCK:\n" - " return \"HWCK\";\n" - " case Format_KCHW:\n" - " return \"KCHW\";\n" - " case Format_CKHW:\n" - " return \"CKHW\";\n" - " case Format_KHWC:\n" - " return \"KHWC\";\n" - " case Format_CHWK:\n" - " return \"CHWK\";\n" - " case Format_NC4HW4:\n" - " return \"NC4HW4\";\n" - " case Format_NUM_OF_FORMAT:\n" - " return \"NUM_OF_FORMAT\";\n" - " default:\n" - " return \"\";\n" - " }\n" - "}\n" - "\n" - "void PrintTensorData(MicroTensor *tensor) {\n" - " void *data = tensor->data;\n" - " unsigned int elenums = GetTensorElementSize(tensor);\n" - " if (data == NULL || elenums == 0) {\n" - " MICRO_ERROR(\"print tensor data failed\");\n" - " return;\n" - " }\n" - " switch (tensor->type) {\n" - " case DataType_DT_FLOAT: {\n" - " float *addr = (float *)(data);\n" - " for (int i = 0; i < elenums && i < kPrintNums; ++i) {\n" - " printf(\"%f, \", addr[i]);\n" - " }\n" - " break;\n" - " }\n" - " case DataType_DT_INT32: {\n" - " int32_t *addr = (int32_t *)(data);\n" - " for (int i = 0; i < elenums && i < kPrintNums; ++i) {\n" - " printf(\"%d, \", addr[i]);\n" - " }\n" - " break;\n" - " }\n" - " case DataType_DT_INT8: {\n" - " int8_t *addr = (int8_t *)(data);\n" - " for (int i = 0; i < elenums && i < kPrintNums; ++i) {\n" - " printf(\"%d, \", addr[i]);\n" - " }\n" - " break;\n" - " }\n" - " case DataType_DT_UINT32: {\n" - " uint32_t *addr = (uint32_t *)(data);\n" - " for (int i = 0; i < elenums && i < kPrintNums; ++i) {\n" - " printf(\"%u, \", addr[i]);\n" - " }\n" - " break;\n" - " }\n" - " case DataType_DT_UINT8: {\n" - " uint8_t *addr = (uint8_t *)(data);\n" - " for (int i = 0; i < elenums && i < kPrintNums; ++i) {\n" - " printf(\"%u, \", addr[i]);\n" - " }\n" - " break;\n" - " }\n" - " default:\n" - " MICRO_ERROR(\"unsupported data type %d\", tensor->type);\n" - " }\n" - " printf(\"\\n\");\n" - "}\n" - "\n" - "void PrintDataToFile(const void *data, const size_t elenums, const enum DataType type, FILE *file) {\n" - " if (data == NULL || elenums == 0) {\n" - " MICRO_ERROR(\"print tensor data to file failed\");\n" - " return;\n" - " }\n" - " switch (type) {\n" - " case DataType_DT_FLOAT: {\n" - " float *addr = (float *)(data);\n" - " for (int i = 0; i < elenums; ++i) {\n" - " fprintf(file, \"%0.15f, \", addr[i]);\n" - " if (i % kLineNum == kLineSplitNum) {\n" - " fprintf(file, \"\\n\");\n" - " }\n" - " }\n" - " break;\n" - " }\n" - " case DataType_DT_INT32: {\n" - " int32_t *addr = (int32_t *)(data);\n" - " for (int i = 0; i < elenums; ++i) {\n" - " fprintf(file, \"%d, \", addr[i]);\n" - " if (i % kLineNum == kLineSplitNum) {\n" - " fprintf(file, \"\\n\");\n" - " }\n" - " }\n" - " break;\n" - " }\n" - " case DataType_DT_INT8: {\n" - " int8_t *addr = (int8_t *)(data);\n" - " for (int i = 0; i < elenums; ++i) {\n" - " fprintf(file, \"%d, \", addr[i]);\n" - " if (i % kLineNum == kLineSplitNum) {\n" - " fprintf(file, \"\\n\");\n" - " }\n" - " }\n" - " break;\n" - " }\n" - " case DataType_DT_UINT32: {\n" - " uint32_t *addr = (uint32_t *)(data);\n" - " for (int i = 0; i < elenums; ++i) {\n" - " fprintf(file, \"%u, \", addr[i]);\n" - " if (i % kLineNum == kLineSplitNum) {\n" - " fprintf(file, \"\\n\");\n" - " }\n" - " }\n" - " break;\n" - " }\n" - " case DataType_DT_UINT8: {\n" - " uint8_t *addr = (uint8_t *)(data);\n" - " for (int i = 0; i < elenums; ++i) {\n" - " fprintf(file, \"%u, \", addr[i]);\n" - " if (i % kLineNum == kLineSplitNum) {\n" - " fprintf(file, \"\\n\");\n" - " }\n" - " }\n" - " break;\n" - " }\n" - " default:\n" - " MICRO_ERROR(\"unsupported data type %d\", type);\n" - " }\n" - " fprintf(file, \"\\n\");\n" - "}\n" - "\n" - "void PrintTensor(MicroTensor *tensor, FILE *output_file, const char *is_input) {\n" - " if (output_file == NULL) {\n" - " MICRO_ERROR(\"output file is NULL\");\n" - " return;\n" - " }\n" - " fprintf(output_file, \"%s \", is_input);\n" - " for (int i = 0; i < tensor->ndim; ++i) {\n" - " fprintf(output_file, \"%u, \", tensor->dim[i]);\n" - " }\n" - " fprintf(output_file, \"\\n\");\n" - "\n" - " const char *type = TypeNames[tensor->type];\n" - " const char *format = EnumNameFormat(tensor->format);\n" - " unsigned int tensorSize = GetTensorElementSize(tensor);\n" - " fprintf(output_file, \"%s type:%s, format:%s, elementSize: %u\\n\", is_input, type, format, tensorSize);\n" - " fprintf(output_file, \"%s Data:\\n\", is_input);\n" - " PrintDataToFile(tensor->data, tensorSize, tensor->type, output_file);\n" - " (void)fflush(output_file);\n" - "}\n" - "\n" - "uint64_t GetTimeUs() {\n" - " const int USEC = 1000000;\n" - " const int MSEC = 1000;\n" - " struct timespec ts = {0, 0};\n" - " if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {\n" - " return 0;\n" - " }\n" - " uint64_t retval = (uint64_t)((ts.tv_sec * USEC) + (ts.tv_nsec / MSEC));\n" - " return retval;\n" - "}\n"; +extern const char *debug_utils_h; +extern const char *debug_utils_c; + +} // namespace mindspore::lite::micro #endif // MINDSPORE_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_DEBUG_UTILS_H_ diff --git a/mindspore/lite/micro/coder/generator/component/const_blocks/license.cc b/mindspore/lite/micro/coder/generator/component/const_blocks/license.cc new file mode 100644 index 0000000000..4669149c54 --- /dev/null +++ b/mindspore/lite/micro/coder/generator/component/const_blocks/license.cc @@ -0,0 +1,40 @@ +/** + * 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 "coder/generator/component/const_blocks/license.h" + +namespace mindspore::lite::micro { + +const char *g_hwLicense = R"RAW( +/** + * 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. + */ + +)RAW"; + +} // namespace mindspore::lite::micro diff --git a/mindspore/lite/micro/coder/generator/component/const_blocks/license.h b/mindspore/lite/micro/coder/generator/component/const_blocks/license.h index 825f3b1721..4c49b251f3 100644 --- a/mindspore/lite/micro/coder/generator/component/const_blocks/license.h +++ b/mindspore/lite/micro/coder/generator/component/const_blocks/license.h @@ -19,22 +19,8 @@ namespace mindspore::lite::micro { -static const char *g_hwLicense = - "/**\n" - " * Copyright 2021 Huawei Technologies Co., Ltd\n" - " *\n" - " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" - " * you may not use this file except in compliance with the License.\n" - " * You may obtain a copy of the License at\n" - " *\n" - " * http://www.apache.org/licenses/LICENSE-2.0\n" - " *\n" - " * Unless required by applicable law or agreed to in writing, software\n" - " * distributed under the License is distributed on an \"AS IS\" BASIS,\n" - " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" - " * See the License for the specific language governing permissions and\n" - " * limitations under the License.\n" - " */\n\n"; +extern const char *g_hwLicense; + } // namespace mindspore::lite::micro #endif // MINDSPORE_LITE_MICRO_GENERATOR_CONST_BLOCK_LICENSE_INFOS_H_ diff --git a/mindspore/lite/micro/coder/generator/component/const_blocks/load_input.cc b/mindspore/lite/micro/coder/generator/component/const_blocks/load_input.cc new file mode 100644 index 0000000000..053906c01e --- /dev/null +++ b/mindspore/lite/micro/coder/generator/component/const_blocks/load_input.cc @@ -0,0 +1,147 @@ +/** + * 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 "coder/generator/component/const_blocks/load_input.h" + +namespace mindspore::lite::micro { + +const char *load_input_h = R"RAW( +/** + * 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 MICRO_EXAMPLE_LOAD_INPUT_LOAD_INPUT_H_ +#define MICRO_EXAMPLE_LOAD_INPUT_LOAD_INPUT_H_ +void *ReadInputData(const char *real_input_path, int *size); + +void SaveOutputData(char *final_name, unsigned char *output_data, unsigned int out_size); + +int ReadInputsFile(char *path, void **buffers, const int *inputs_size, int inputs_num); + +#endif // MICRO_EXAMPLE_LOAD_INPUT_LOAD_INPUT_H_ + +)RAW"; + +const char *load_input_c = R"RAW( +/** + * 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 "load_input.h" +#include +#include +#include + +void *ReadInputData(const char *real_input_path, int *size) { + if (real_input_path == NULL) { + return NULL; + } + if (strstr(real_input_path, ".bin") || strstr(real_input_path, ".net")) { + FILE *file; + file = fopen(real_input_path, "rb+"); + if (!file) { + printf("Can't find %s\n", real_input_path); + return NULL; + } + int curr_file_posi = ftell(file); + fseek(file, 0, SEEK_END); + *size = ftell(file); + unsigned char *buf = malloc((*size)); + (void)memset(buf, 0, (*size)); + fseek(file, curr_file_posi, SEEK_SET); + int read_size = (int)(fread(buf, 1, *size, file)); + if (read_size != (*size)) { + printf("read file failed, total file size: %d, read_size: %d\n", (*size), read_size); + fclose(file); + free(buf); + return NULL; + } + fclose(file); + return (void *)buf; + } else { + printf("input data file should be .bin , .net"); + return NULL; + } +} + +void SaveOutputData(char *final_name, unsigned char *output_data, unsigned int out_size) { + FILE *output_file; + output_file = fopen(final_name, "w"); + if (output_file == NULL) { + printf("fopen output file: %s failed\n", final_name); + return; + } + unsigned char str[out_size]; + for (unsigned int i = 0; i < out_size; ++i) { + str[i] = output_data[i]; + fprintf(output_file, "%d\t", str[i]); + } + fclose(output_file); +} + +int ReadInputsFile(char *path, void **buffers, const int *inputs_size, int inputs_num) { + char *inputs_path[inputs_num]; + char *delim = ","; + char *token; + int i = 0; + while ((token = strtok_r(path, delim, &path))) { + if (i >= inputs_num) { + printf("inputs num is error, need: %d\n", inputs_num); + return -1; + } + inputs_path[i] = token; + printf("input %d: %s\n", i, inputs_path[i]); + i++; + } + + for (i = 0; i < inputs_num; ++i) { + int size = 0; + buffers[i] = ReadInputData(inputs_path[i], &size); + if (size != inputs_size[i] || buffers[i] == NULL) { + printf("size mismatch, %s, input: %d, needed: %d\n", inputs_path[i], size, inputs_size[i]); + return -1; + } + } + return 0; +} + +)RAW"; + +} // namespace mindspore::lite::micro diff --git a/mindspore/lite/micro/coder/generator/component/const_blocks/load_input.h b/mindspore/lite/micro/coder/generator/component/const_blocks/load_input.h index 119b5da6ac..972436035a 100644 --- a/mindspore/lite/micro/coder/generator/component/const_blocks/load_input.h +++ b/mindspore/lite/micro/coder/generator/component/const_blocks/load_input.h @@ -16,126 +16,12 @@ #ifndef MINDSPORE_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_BENCH_LOAD_INPUT_H_ #define MINDSPORE_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_BENCH_LOAD_INPUT_H_ -const char *load_input_h = - "/**\n" - " * Copyright 2021 Huawei Technologies Co., Ltd\n" - " *\n" - " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" - " * you may not use this file except in compliance with the License.\n" - " * You may obtain a copy of the License at\n" - " *\n" - " * http://www.apache.org/licenses/LICENSE-2.0\n" - " *\n" - " * Unless required by applicable law or agreed to in writing, software\n" - " * distributed under the License is distributed on an \"AS IS\" BASIS,\n" - " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" - " * See the License for the specific language governing permissions and\n" - " * limitations under the License.\n" - " */\n" - "\n" - "#ifndef MICRO_EXAMPLE_LOAD_INPUT_LOAD_INPUT_H_\n" - "#define MICRO_EXAMPLE_LOAD_INPUT_LOAD_INPUT_H_\n" - "void *ReadInputData(const char *real_input_path, int *size);\n" - "\n" - "void SaveOutputData(char *final_name, unsigned char *output_data, unsigned int out_size);\n" - "\n" - "int ReadInputsFile(char *path, void **buffers, const int *inputs_size, int inputs_num);\n" - "\n" - "#endif // MICRO_EXAMPLE_LOAD_INPUT_LOAD_INPUT_H_\n"; -const char *load_input_c = - "/**\n" - " * Copyright 2021 Huawei Technologies Co., Ltd\n" - " *\n" - " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" - " * you may not use this file except in compliance with the License.\n" - " * You may obtain a copy of the License at\n" - " *\n" - " * http://www.apache.org/licenses/LICENSE-2.0\n" - " *\n" - " * Unless required by applicable law or agreed to in writing, software\n" - " * distributed under the License is distributed on an \"AS IS\" BASIS,\n" - " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" - " * See the License for the specific language governing permissions and\n" - " * limitations under the License.\n" - " */\n" - "\n" - "#include \"load_input.h\"\n" - "#include \n" - "#include \n" - "#include \n" - "\n" - "void *ReadInputData(const char *real_input_path, int *size) {\n" - " if (real_input_path == NULL) {\n" - " return NULL;\n" - " }\n" - " if (strstr(real_input_path, \".bin\") || strstr(real_input_path, \".net\")) {\n" - " FILE *file;\n" - " file = fopen(real_input_path, \"rb+\");\n" - " if (!file) {\n" - " printf(\"Can't find %s\\n\", real_input_path);\n" - " return NULL;\n" - " }\n" - " int curr_file_posi = ftell(file);\n" - " fseek(file, 0, SEEK_END);\n" - " *size = ftell(file);\n" - " unsigned char *buf = malloc((*size));\n" - " (void)memset(buf, 0, (*size));\n" - " fseek(file, curr_file_posi, SEEK_SET);\n" - " int read_size = (int)(fread(buf, 1, *size, file));\n" - " if (read_size != (*size)) {\n" - " printf(\"read file failed, total file size: %d, read_size: %d\\n\", (*size), read_size);\n" - " fclose(file);\n" - " free(buf);\n" - " return NULL;\n" - " }\n" - " fclose(file);\n" - " return (void *)buf;\n" - " } else {\n" - " printf(\"input data file should be .bin , .net\");\n" - " return NULL;\n" - " }\n" - "}\n" - "\n" - "void SaveOutputData(char *final_name, unsigned char *output_data, unsigned int out_size) {\n" - " FILE *output_file;\n" - " output_file = fopen(final_name, \"w\");\n" - " if (output_file == NULL) {\n" - " printf(\"fopen output file: %s failed\\n\", final_name);\n" - " return;\n" - " }\n" - " unsigned char str[out_size];\n" - " for (unsigned int i = 0; i < out_size; ++i) {\n" - " str[i] = output_data[i];\n" - " fprintf(output_file, \"%d\\t\", str[i]);\n" - " }\n" - " fclose(output_file);\n" - "}\n" - "\n" - "int ReadInputsFile(char *path, void **buffers, const int *inputs_size, int inputs_num) {\n" - " char *inputs_path[inputs_num];\n" - " char *delim = \",\";\n" - " char *token;\n" - " int i = 0;\n" - " while ((token = strtok_r(path, delim, &path))) {\n" - " if (i >= inputs_num) {\n" - " printf(\"inputs num is error, need: %d\\n\", inputs_num);\n" - " return -1;\n" - " }\n" - " inputs_path[i] = token;\n" - " printf(\"input %d: %s\\n\", i, inputs_path[i]);\n" - " i++;\n" - " }\n" - "\n" - " for (i = 0; i < inputs_num; ++i) {\n" - " int size = 0;\n" - " buffers[i] = ReadInputData(inputs_path[i], &size);\n" - " if (size != inputs_size[i] || buffers[i] == NULL) {\n" - " printf(\"size mismatch, %s, input: %d, needed: %d\\n\", inputs_path[i], size, inputs_size[i]);\n" - " return -1;\n" - " }\n" - " }\n" - " return 0;\n" - "}\n"; +namespace mindspore::lite::micro { + +extern const char *load_input_h; +extern const char *load_input_c; + +} // namespace mindspore::lite::micro #endif // MINDSPORE_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_BENCH_LOAD_INPUT_H_ diff --git a/mindspore/lite/micro/coder/generator/component/const_blocks/micro_tensor.cc b/mindspore/lite/micro/coder/generator/component/const_blocks/micro_tensor.cc new file mode 100644 index 0000000000..7fd44bbb2f --- /dev/null +++ b/mindspore/lite/micro/coder/generator/component/const_blocks/micro_tensor.cc @@ -0,0 +1,111 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "coder/generator/component/const_blocks/micro_tensor.h" + +namespace mindspore::lite::micro { + +const char *micro_tensor_h = R"RAW( +/** + * 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 MSMICRO_TENSOR_H +#define MSMICRO_TENSOR_H + +#include +#include +#include +#include +#include + +#define MICRO_INFO(content, args...) \ + { printf("[INFO] %s|%d: " #content "\r\n", __func__, __LINE__, ##args); } +#define MICRO_ERROR(content, args...) \ + { printf("[ERROR] %s|%d: " #content "\r\n", __func__, __LINE__, ##args); } + +enum STATUS { + RET_OK = 0, + RET_ERROR = 1, +}; + +enum DataType { + DataType_DT_FLOAT = 0, + DataType_DT_FLOAT16 = 1, + DataType_DT_INT8 = 2, + DataType_DT_INT32 = 3, + DataType_DT_UINT8 = 4, + DataType_DT_INT16 = 5, + DataType_DT_UINT32 = 8, + DataType_DT_INT64 = 9, + DataType_DT_UINT16 = 10, + DataType_DT_UNDEFINED = 16, + DataType_MIN = DataType_DT_FLOAT, + DataType_MAX = DataType_DT_UNDEFINED +}; + +enum Format { + Format_NCHW = 0, + Format_NHWC = 1, + Format_HWKC = 2, + Format_HWCK = 3, + Format_KCHW = 4, + Format_CKHW = 5, + Format_KHWC = 6, + Format_CHWK = 7, + Format_NC4HW4 = 100, + Format_NUM_OF_FORMAT = 101, + Format_MIN = Format_NCHW, + Format_MAX = Format_NUM_OF_FORMAT +}; + +typedef struct { + enum DataType type; + enum Format format; + int ndim; + int *dim; + void *data; +} MicroTensor; + +typedef struct { + int num; + MicroTensor *tensor; +} MicroTensorList; + +typedef struct { + float in_scale; + float out_scale; + int in_zero_point; + int out_zero_point; +} GraphQuantArgs; + +#endif // MSMICRO_TENSOR_H + +)RAW"; + +} // namespace mindspore::lite::micro diff --git a/mindspore/lite/micro/coder/generator/component/const_blocks/micro_tensor.h b/mindspore/lite/micro/coder/generator/component/const_blocks/micro_tensor.h index 79eb4484dc..838636cacc 100644 --- a/mindspore/lite/micro/coder/generator/component/const_blocks/micro_tensor.h +++ b/mindspore/lite/micro/coder/generator/component/const_blocks/micro_tensor.h @@ -16,91 +16,10 @@ #ifndef MINDSPORE_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_MICRO_TENSOR_H_ #define MINDSPORE_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_MICRO_TENSOR_H_ -const char *micro_tensor_h = - "/**\n" - " * Copyright 2021 Huawei Technologies Co., Ltd\n" - " *\n" - " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" - " * you may not use this file except in compliance with the License.\n" - " * You may obtain a copy of the License at\n" - " *\n" - " * http://www.apache.org/licenses/LICENSE-2.0\n" - " *\n" - " * Unless required by applicable law or agreed to in writing, software\n" - " * distributed under the License is distributed on an \"AS IS\" BASIS,\n" - " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" - " * See the License for the specific language governing permissions and\n" - " * limitations under the License.\n" - " */\n" - "\n" - "#ifndef MSMICRO_TENSOR_H\n" - "#define MSMICRO_TENSOR_H\n" - "\n" - "#include \n" - "#include \n" - "#include \n" - "#include \n" - "#include \n" - "\n" - "#define MICRO_INFO(content, args...) \\\n" - " { printf(\"[INFO] %s|%d: \" #content \"\\r\\n\", __func__, __LINE__, ##args); }\n" - "#define MICRO_ERROR(content, args...) \\\n" - " { printf(\"[ERROR] %s|%d: \" #content \"\\r\\n\", __func__, __LINE__, ##args); }\n" - "\n" - "enum STATUS {\n" - " RET_OK = 0,\n" - " RET_ERROR = 1,\n" - "};\n" - "\n" - "enum DataType {\n" - " DataType_DT_FLOAT = 0,\n" - " DataType_DT_FLOAT16 = 1,\n" - " DataType_DT_INT8 = 2,\n" - " DataType_DT_INT32 = 3,\n" - " DataType_DT_UINT8 = 4,\n" - " DataType_DT_INT16 = 5,\n" - " DataType_DT_UINT32 = 8,\n" - " DataType_DT_INT64 = 9,\n" - " DataType_DT_UINT16 = 10,\n" - " DataType_DT_UNDEFINED = 16,\n" - " DataType_MIN = DataType_DT_FLOAT,\n" - " DataType_MAX = DataType_DT_UNDEFINED\n" - "};\n" - "\n" - "enum Format {\n" - " Format_NCHW = 0,\n" - " Format_NHWC = 1,\n" - " Format_HWKC = 2,\n" - " Format_HWCK = 3,\n" - " Format_KCHW = 4,\n" - " Format_CKHW = 5,\n" - " Format_KHWC = 6,\n" - " Format_CHWK = 7,\n" - " Format_NC4HW4 = 100,\n" - " Format_NUM_OF_FORMAT = 101,\n" - " Format_MIN = Format_NCHW,\n" - " Format_MAX = Format_NUM_OF_FORMAT\n" - "};\n" - "\n" - "typedef struct {\n" - " enum DataType type;\n" - " enum Format format;\n" - " int ndim;\n" - " int *dim;\n" - " void *data;\n" - "} MicroTensor;\n" - "\n" - "typedef struct {\n" - " int num;\n" - " MicroTensor *tensor;\n" - "} MicroTensorList;\n" - "\n" - "typedef struct {\n" - " float in_scale;\n" - " float out_scale;\n" - " int in_zero_point;\n" - " int out_zero_point;\n" - "} GraphQuantArgs;\n" - "\n" - "#endif // MSMICRO_TENSOR_H\n"; +namespace mindspore::lite::micro { + +extern const char *micro_tensor_h; + +} // namespace mindspore::lite::micro + #endif // MINDSPORE_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_MICRO_TENSOR_H_ diff --git a/mindspore/lite/micro/coder/generator/component/const_blocks/thread_pool.cc b/mindspore/lite/micro/coder/generator/component/const_blocks/thread_pool.cc new file mode 100644 index 0000000000..a3227383d2 --- /dev/null +++ b/mindspore/lite/micro/coder/generator/component/const_blocks/thread_pool.cc @@ -0,0 +1,99 @@ +/** + * 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 "coder/generator/component/const_blocks/thread_pool.h" + +namespace mindspore::lite::micro { + +const char *thread_pool_h = R"RAW( +/** + * 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_SRC_RUNTIME_THREAD_POOL_H_ +#define MINDSPORE_LITE_SRC_RUNTIME_THREAD_POOL_H_ + +#include + +#define MAX_TASK_NUM (2) + +// brief BindMode defined for holding bind cpu strategy argument. +typedef enum { + NO_BIND_MODE = 0, /**< no bind */ + HIGHER_MODE = 1, /**< bind higher cpu first */ + MID_MODE = 2 /**< bind middle cpu first */ +} BindMode; + +struct ThreadPool; + +struct ThreadPool *CreateThreadPool(int thread_num, int mode); + +/** + * + * @param session_index, support multi session + * @param job + * @param content + * @param task_num + */ +int ParallelLaunch(struct ThreadPool *thread_pool, int (*job)(void *, int), void *content, int task_num); + +/** + * bind each thread to specified cpu core + * @param is_bind + * @param mode + */ +int BindThreads(struct ThreadPool *thread_pool, bool is_bind, int mode); + +/** + * activate the thread pool + * @param thread_pool_id + */ +void ActivateThreadPool(struct ThreadPool *thread_pool); + +/** + * deactivate the thread pool + * @param thread_pool_id + */ +void DeactivateThreadPool(struct ThreadPool *thread_pool); + +/** + * + * @return current thread num + */ +int GetCurrentThreadNum(struct ThreadPool *thread_pool); + +/** + * destroy thread pool, and release resource + */ +void DestroyThreadPool(struct ThreadPool *thread_pool); + +#endif // MINDSPORE_LITE_SRC_RUNTIME_THREAD_POOL_H_ + +)RAW"; + +} // namespace mindspore::lite::micro diff --git a/mindspore/lite/micro/coder/generator/component/const_blocks/thread_pool.h b/mindspore/lite/micro/coder/generator/component/const_blocks/thread_pool.h index 959eacdca4..69ec293639 100644 --- a/mindspore/lite/micro/coder/generator/component/const_blocks/thread_pool.h +++ b/mindspore/lite/micro/coder/generator/component/const_blocks/thread_pool.h @@ -19,81 +19,8 @@ namespace mindspore::lite::micro { -const char *thread_pool_h = - "/**\n" - " * Copyright 2021 Huawei Technologies Co., Ltd\n" - " *\n" - " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" - " * you may not use this file except in compliance with the License.\n" - " * You may obtain a copy of the License at\n" - " *\n" - " * http://www.apache.org/licenses/LICENSE-2.0\n" - " *\n" - " * Unless required by applicable law or agreed to in writing, software\n" - " * distributed under the License is distributed on an \"AS IS\" BASIS,\n" - " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" - " * See the License for the specific language governing permissions and\n" - " * limitations under the License.\n" - " */\n" - "\n" - "#ifndef MINDSPORE_LITE_SRC_RUNTIME_THREAD_POOL_H_\n" - "#define MINDSPORE_LITE_SRC_RUNTIME_THREAD_POOL_H_\n" - "\n" - "#include \n" - "\n" - "#define MAX_TASK_NUM (2)\n" - "\n" - "/// \\brief BindMode defined for holding bind cpu strategy argument.\n" - "typedef enum {\n" - " NO_BIND_MODE = 0, /**< no bind */\n" - " HIGHER_MODE = 1, /**< bind higher cpu first */\n" - " MID_MODE = 2 /**< bind middle cpu first */\n" - "} BindMode;\n" - "\n" - "struct ThreadPool;\n" - "\n" - "struct ThreadPool *CreateThreadPool(int thread_num, int mode);\n" - "\n" - "/**\n" - " *\n" - " * @param session_index, support multi session\n" - " * @param job\n" - " * @param content\n" - " * @param task_num\n" - " */\n" - "int ParallelLaunch(struct ThreadPool *thread_pool, int (*job)(void *, int), void *content, int task_num);\n" - "\n" - "/**\n" - " * bind each thread to specified cpu core\n" - " * @param is_bind\n" - " * @param mode\n" - " */\n" - "int BindThreads(struct ThreadPool *thread_pool, bool is_bind, int mode);\n" - "\n" - "/**\n" - " * activate the thread pool\n" - " * @param thread_pool_id\n" - " */\n" - "void ActivateThreadPool(struct ThreadPool *thread_pool);\n" - "\n" - "/**\n" - " * deactivate the thread pool\n" - " * @param thread_pool_id\n" - " */\n" - "void DeactivateThreadPool(struct ThreadPool *thread_pool);\n" - "\n" - "/**\n" - " *\n" - " * @return current thread num\n" - " */\n" - "int GetCurrentThreadNum(struct ThreadPool *thread_pool);\n" - "\n" - "/**\n" - " * destroy thread pool, and release resource\n" - " */\n" - "void DestroyThreadPool(struct ThreadPool *thread_pool);\n" - "\n" - "#endif // MINDSPORE_LITE_SRC_RUNTIME_THREAD_POOL_H_\n"; +extern const char *thread_pool_h; + } // namespace mindspore::lite::micro #endif // MINDSPORE_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_THREAD_POOL_H_ diff --git a/mindspore/lite/micro/coder/operator_library/CMakeLists.txt b/mindspore/lite/micro/coder/operator_library/CMakeLists.txt index fe7cb4b7a2..6ee3d59320 100644 --- a/mindspore/lite/micro/coder/operator_library/CMakeLists.txt +++ b/mindspore/lite/micro/coder/operator_library/CMakeLists.txt @@ -26,16 +26,18 @@ set(HEADER_PATH "${OPERATOR_LIBRARY_PATH}/include") message("===========>start to pack operators' head file") file(REMOVE_RECURSE ${OPERATOR_LIBRARY_PATH}) file(MAKE_DIRECTORY ${OPERATOR_LIBRARY_PATH}) -file(INSTALL ${LITE_DIR}/nnacl DESTINATION ${HEADER_PATH} FILES_MATCHING PATTERN "*.h") -file(INSTALL ${MICRO_DIR}/coder/operator_library/wrapper DESTINATION ${HEADER_PATH} FILES_MATCHING PATTERN "*.h") -file(INSTALL ${CMAKE_BINARY_DIR}/cmsis/CMSIS/Core/Include DESTINATION ${HEADER_PATH}/CMSIS/Core) -file(INSTALL ${CMAKE_BINARY_DIR}/cmsis/CMSIS/DSP/Include DESTINATION ${HEADER_PATH}/CMSIS/DSP) -file(INSTALL ${CMAKE_BINARY_DIR}/cmsis/CMSIS/NN/Include DESTINATION ${HEADER_PATH}/CMSIS/NN) -file(REMOVE_RECURSE ${HEADER_PATH}/nnacl/assembly) -file(REMOVE_RECURSE ${HEADER_PATH}/nnacl/fp16) -file(REMOVE_RECURSE ${HEADER_PATH}/nnacl/fp16_grad) -file(REMOVE_RECURSE ${HEADER_PATH}/nnacl/fp32_grad) -file(REMOVE_RECURSE ${HEADER_PATH}/nnacl/optimize) +file(GLOB NNACL_FILES GLOB ${LITE_DIR}/nnacl/*.h) +file(COPY ${NNACL_FILES} DESTINATION ${HEADER_PATH}/nnacl/) +file(COPY ${LITE_DIR}/nnacl/base + ${LITE_DIR}/nnacl/int8 + ${LITE_DIR}/nnacl/fp32 + ${LITE_DIR}/nnacl/intrinsics + DESTINATION ${HEADER_PATH}/nnacl FILES_MATCHING PATTERN "*.h") +file(COPY ${MICRO_DIR}/coder/operator_library/wrapper DESTINATION ${HEADER_PATH} FILES_MATCHING PATTERN "*.h") +file(COPY ${CMAKE_BINARY_DIR}/cmsis/CMSIS/Core/Include + ${CMAKE_BINARY_DIR}/cmsis/CMSIS/DSP/Include + ${CMAKE_BINARY_DIR}/cmsis/CMSIS/NN/Include + DESTINATION ${HEADER_PATH}/CMSIS/Core) if(PLATFORM_ARM64) set(MICRO_BUILD_ARM64 ON)