From 6fe6de81468d1cbf1cd7bfeae00a25b175b15376 Mon Sep 17 00:00:00 2001 From: chengyuanwang Date: Mon, 8 Feb 2021 11:54:15 +0800 Subject: [PATCH] unified the macro defined in header files && feature, push micro generator to master --- mindspore/lite/CMakeLists.txt | 4 +- mindspore/lite/micro/cmake/file_list.cmake | 44 ++- mindspore/lite/micro/coder/CMakeLists.txt | 2 - .../lite/micro/coder/allocator/allocator.cc | 23 +- .../lite/micro/coder/allocator/allocator.h | 2 +- mindspore/lite/micro/coder/coder.cc | 4 +- mindspore/lite/micro/coder/coder.h | 8 +- mindspore/lite/micro/coder/coder_config.h | 9 +- .../coder/{coder_context.cc => context.cc} | 2 +- .../coder/{coder_context.h => context.h} | 29 +- mindspore/lite/micro/coder/debug.cc | 95 ------ .../component/benchmark_component.cc | 191 ++++++++++++ .../generator/component/benchmark_component.h | 52 ++++ .../generator/component/cmake_component.cc | 55 ++++ .../component/cmake_component.h} | 28 +- .../generator/component/common_component.cc | 209 +++++++++++++ .../generator/component/common_component.h | 51 +++ .../const_blocks/cmake_lists.h} | 0 .../const_blocks/debug_utils.h} | 42 ++- .../{ => component}/const_blocks/license.h | 0 .../const_blocks/load_input.h} | 0 .../const_blocks/micro_tensor.h | 0 .../generator/component/weight_component.cc | 150 +++++++++ .../generator/component/weight_component.h | 41 +++ .../lite/micro/coder/generator/generator.cc | 271 +++------------- .../lite/micro/coder/generator/generator.h | 26 +- .../inference/inference_generator.cc | 273 ++-------------- .../generator/inference/inference_generator.h | 14 +- .../coder/generator/utils/generator_utils.cc | 172 ---------- .../coder/generator/utils/generator_utils.h | 47 --- .../micro/coder/{coder_graph.cc => graph.cc} | 2 +- .../micro/coder/{coder_graph.h => graph.h} | 7 +- mindspore/lite/micro/coder/log.h | 23 +- .../opcoders/cmsis-nn/int8/add_int8_coder.cc | 6 +- .../opcoders/cmsis-nn/int8/add_int8_coder.h | 2 +- .../cmsis-nn/int8/conv2d_base_coder.cc | 2 +- .../cmsis-nn/int8/conv2d_base_coder.h | 2 +- .../cmsis-nn/int8/conv2d_int8_coder.cc | 8 +- .../cmsis-nn/int8/conv2d_int8_coder.h | 2 +- .../cmsis-nn/int8/dwconv_int8_coder.cc | 8 +- .../cmsis-nn/int8/dwconv_int8_coder.h | 2 +- .../int8/fullconnection_int8_coder.cc | 6 +- .../opcoders/cmsis-nn/int8/mul_int8_coder.cc | 6 +- .../opcoders/cmsis-nn/int8/mul_int8_coder.h | 2 +- .../cmsis-nn/int8/pooling_int8_coder.cc | 6 +- .../cmsis-nn/int8/pooling_int8_coder.h | 2 +- .../cmsis-nn/int8/reshape_int8_coder.cc | 4 +- .../cmsis-nn/int8/reshape_int8_coder.h | 2 +- .../cmsis-nn/int8/softmax_int8_coder.cc | 6 +- .../cmsis-nn/int8/softmax_int8_coder.h | 2 +- .../micro/coder/opcoders/file_collector.h | 2 +- .../nnacl/fp32/activation_fp32_coder.h | 6 +- .../opcoders/nnacl/fp32/addn_fp32_coder.h | 2 +- .../nnacl/fp32/arithmetic_fp32_coder.cc | 6 +- .../nnacl/fp32/arithmetic_fp32_coder.h | 10 +- .../nnacl/fp32/arithmetic_self_fp32_coder.cc | 6 +- .../nnacl/fp32/arithmetic_self_fp32_coder.h | 8 +- .../nnacl/fp32/assign_add_fp32_coder.h | 2 +- .../nnacl/fp32/batchnorm_fp32_coder.cc | 6 +- .../nnacl/fp32/batchnorm_fp32_coder.h | 8 +- .../opcoders/nnacl/fp32/concat_fp32_coder.cc | 6 +- .../opcoders/nnacl/fp32/concat_fp32_coder.h | 2 +- .../fp32/convolution_depthwise_fp32_coder.cc | 78 +++++ .../fp32/convolution_depthwise_fp32_coder.h | 42 +++ .../nnacl/fp32/convolution_fp32_coder.cc | 179 +++++++++++ .../nnacl/fp32/convolution_fp32_coder.h | 62 ++++ .../fp32/convolution_winograd_fp32_coder.cc | 247 +++++++++++++++ .../fp32/convolution_winograd_fp32_coder.h | 75 +++++ .../nnacl/fp32/expand_dims_fp32_coder.cc | 6 +- .../nnacl/fp32/expand_dims_fp32_coder.h | 2 +- .../opcoders/nnacl/fp32/gather_fp32_coder.cc | 8 +- .../opcoders/nnacl/fp32/gather_fp32_coder.h | 2 +- .../nnacl/fp32/nchw2nhwc_fp32_coder.cc | 6 +- .../nnacl/fp32/nchw2nhwc_fp32_coder.h | 2 +- .../nnacl/fp32/nhwc2nchw_fp32_coder.cc | 6 +- .../nnacl/fp32/nhwc2nchw_fp32_coder.h | 2 +- .../opcoders/nnacl/fp32/pad_fp32_coder.h | 2 +- .../opcoders/nnacl/fp32/pooling_fp32_coder.cc | 8 +- .../opcoders/nnacl/fp32/pooling_fp32_coder.h | 6 +- .../opcoders/nnacl/fp32/power_fp32_coder.cc | 6 +- .../opcoders/nnacl/fp32/power_fp32_coder.h | 2 +- .../opcoders/nnacl/fp32/reduce_fp32_coder.cc | 8 +- .../opcoders/nnacl/fp32/reduce_fp32_coder.h | 4 +- .../opcoders/nnacl/fp32/reshape_fp32_coder.cc | 6 +- .../opcoders/nnacl/fp32/reshape_fp32_coder.h | 2 +- .../opcoders/nnacl/fp32/scale_fp32_coder.cc | 8 +- .../opcoders/nnacl/fp32/scale_fp32_coder.h | 2 +- .../opcoders/nnacl/fp32/slice_fp32_coder.cc | 6 +- .../opcoders/nnacl/fp32/slice_fp32_coder.h | 2 +- .../opcoders/nnacl/fp32/softmax_fp32_coder.cc | 6 +- .../nnacl/fp32/squeeze_dims_fp32_coder.cc | 6 +- .../nnacl/fp32/squeeze_dims_fp32_coder.h | 2 +- .../opcoders/nnacl/fp32/tile_fp32_coder.cc | 6 +- .../opcoders/nnacl/fp32/tile_fp32_coder.h | 2 +- .../nnacl/fp32/transpose_fp32_coder.h | 8 +- .../opcoders/nnacl/int8/concat_int8_coder.cc | 8 +- .../opcoders/nnacl/int8/pooling_int8_coder.cc | 8 +- .../opcoders/nnacl/int8/pooling_int8_coder.h | 2 +- .../opcoders/nnacl/int8/reduce_int8_coder.cc | 8 +- .../opcoders/nnacl/int8/reduce_int8_coder.h | 8 +- .../opcoders/nnacl/int8/reshape_int8_coder.cc | 8 +- .../opcoders/nnacl/int8/reshape_int8_coder.h | 2 +- .../opcoders/nnacl/int8/softmax_int8_coder.cc | 8 +- .../opcoders/nnacl/int8/softmax_int8_coder.h | 2 +- .../lite/micro/coder/opcoders/op_coder.cc | 25 +- .../lite/micro/coder/opcoders/op_coder.h | 31 +- .../micro/coder/opcoders/op_coder_builder.cc | 6 +- .../micro/coder/opcoders/op_coder_register.h | 6 +- .../nnacl_serializer/nnacl_fp32_serializer.h | 6 +- .../nnacl_serializer/nnacl_int8_serializer.h | 6 +- .../nnacl_serializer/nnacl_stream_utils.h | 6 +- .../coder/opcoders/serializers/serializer.h | 1 - .../coder/{session_coder.cc => session.cc} | 158 ++++------ .../coder/{session_coder.h => session.h} | 14 +- .../lite/micro/coder/utils/coder_utils.cc | 163 +++++++++- .../lite/micro/coder/utils/coder_utils.h | 19 +- mindspore/lite/micro/coder/utils/dir_utils.h | 6 +- .../lite/micro/coder/utils/print_utils.cc | 294 ------------------ mindspore/lite/micro/coder/utils/type_cast.cc | 117 +++++++ .../utils/{print_utils.h => type_cast.h} | 33 +- mindspore/lite/micro/test/CMakeLists.txt | 1 + mindspore/lite/micro/test/code_gen_test.cc | 2 +- 122 files changed, 2202 insertions(+), 1563 deletions(-) rename mindspore/lite/micro/coder/{coder_context.cc => context.cc} (97%) rename mindspore/lite/micro/coder/{coder_context.h => context.h} (77%) delete mode 100644 mindspore/lite/micro/coder/debug.cc create mode 100644 mindspore/lite/micro/coder/generator/component/benchmark_component.cc create mode 100644 mindspore/lite/micro/coder/generator/component/benchmark_component.h create mode 100644 mindspore/lite/micro/coder/generator/component/cmake_component.cc rename mindspore/lite/micro/coder/{debug.h => generator/component/cmake_component.h} (55%) create mode 100644 mindspore/lite/micro/coder/generator/component/common_component.cc create mode 100644 mindspore/lite/micro/coder/generator/component/common_component.h rename mindspore/lite/micro/coder/generator/{const_blocks/cmake_lists_code.h => component/const_blocks/cmake_lists.h} (100%) rename mindspore/lite/micro/coder/generator/{const_blocks/bench_debug_utils.h => component/const_blocks/debug_utils.h} (89%) rename mindspore/lite/micro/coder/generator/{ => component}/const_blocks/license.h (100%) rename mindspore/lite/micro/coder/generator/{const_blocks/bench_load_input.h => component/const_blocks/load_input.h} (100%) rename mindspore/lite/micro/coder/generator/{ => component}/const_blocks/micro_tensor.h (100%) create mode 100644 mindspore/lite/micro/coder/generator/component/weight_component.cc create mode 100644 mindspore/lite/micro/coder/generator/component/weight_component.h delete mode 100644 mindspore/lite/micro/coder/generator/utils/generator_utils.cc delete mode 100644 mindspore/lite/micro/coder/generator/utils/generator_utils.h rename mindspore/lite/micro/coder/{coder_graph.cc => graph.cc} (98%) rename mindspore/lite/micro/coder/{coder_graph.h => graph.h} (94%) create mode 100644 mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_depthwise_fp32_coder.cc create mode 100644 mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_depthwise_fp32_coder.h create mode 100644 mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_fp32_coder.cc create mode 100644 mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_fp32_coder.h create mode 100644 mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_winograd_fp32_coder.cc create mode 100644 mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_winograd_fp32_coder.h rename mindspore/lite/micro/coder/{session_coder.cc => session.cc} (77%) rename mindspore/lite/micro/coder/{session_coder.h => session.h} (83%) delete mode 100644 mindspore/lite/micro/coder/utils/print_utils.cc create mode 100644 mindspore/lite/micro/coder/utils/type_cast.cc rename mindspore/lite/micro/coder/utils/{print_utils.h => type_cast.h} (78%) diff --git a/mindspore/lite/CMakeLists.txt b/mindspore/lite/CMakeLists.txt index 61e529d96a..fe5cca68b2 100644 --- a/mindspore/lite/CMakeLists.txt +++ b/mindspore/lite/CMakeLists.txt @@ -203,9 +203,7 @@ if(ENABLE_CONVERTER) include(${TOP_DIR}/cmake/external_libs/eigen.cmake) include(${TOP_DIR}/cmake/external_libs/protobuf.cmake) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tools/converter) - if(NOT WIN32) - add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/micro/coder) - endif() + add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/micro/coder) endif() if(PLATFORM_ARM32 OR PLATFORM_ARM64) diff --git a/mindspore/lite/micro/cmake/file_list.cmake b/mindspore/lite/micro/cmake/file_list.cmake index fe0dfda9c6..8cda5edf91 100644 --- a/mindspore/lite/micro/cmake/file_list.cmake +++ b/mindspore/lite/micro/cmake/file_list.cmake @@ -1,13 +1,12 @@ #### classify all .h .c .cc files to FILE_SET set(CODER_SRC ${MICRO_DIR}/coder/coder.cc - ${MICRO_DIR}/coder/coder_context.cc - ${MICRO_DIR}/coder/coder_graph.cc - ${MICRO_DIR}/coder/debug.cc - ${MICRO_DIR}/coder/session_coder.cc + ${MICRO_DIR}/coder/context.cc + ${MICRO_DIR}/coder/graph.cc + ${MICRO_DIR}/coder/session.cc ) -set(CODER_ALLOC_SRC +set(CODER_ALLOCATOR_SRC ${MICRO_DIR}/coder/allocator/allocator.cc ${MICRO_DIR}/coder/allocator/memory_manager.cc ) @@ -15,7 +14,10 @@ set(CODER_ALLOC_SRC set(CODER_GENERATOR_SRC ${MICRO_DIR}/coder/generator/generator.cc ${MICRO_DIR}/coder/generator/inference/inference_generator.cc - ${MICRO_DIR}/coder/generator/utils/generator_utils.cc + ${MICRO_DIR}/coder/generator/component/benchmark_component.cc + ${MICRO_DIR}/coder/generator/component/common_component.cc + ${MICRO_DIR}/coder/generator/component/weight_component.cc + ${MICRO_DIR}/coder/generator/component/cmake_component.cc ) set(CODER_OPCODERS_SRC @@ -51,6 +53,9 @@ set(CODER_OPCODERS_SRC ${MICRO_DIR}/coder/opcoders/nnacl/fp32/assign_add_fp32_coder.cc ${MICRO_DIR}/coder/opcoders/nnacl/fp32/batchnorm_fp32_coder.cc ${MICRO_DIR}/coder/opcoders/nnacl/fp32/concat_fp32_coder.cc + ${MICRO_DIR}/coder/opcoders/nnacl/fp32/convolution_fp32_coder.cc + ${MICRO_DIR}/coder/opcoders/nnacl/fp32/convolution_winograd_fp32_coder.cc + ${MICRO_DIR}/coder/opcoders/nnacl/fp32/convolution_depthwise_fp32_coder.cc ${MICRO_DIR}/coder/opcoders/nnacl/fp32/expand_dims_fp32_coder.cc ${MICRO_DIR}/coder/opcoders/nnacl/fp32/gather_fp32_coder.cc ${MICRO_DIR}/coder/opcoders/nnacl/fp32/nchw2nhwc_fp32_coder.cc @@ -77,19 +82,10 @@ set(CODER_OPCODERS_SRC set(CODER_UTILS_SRC ${MICRO_DIR}/coder/utils/coder_utils.cc ${MICRO_DIR}/coder/utils/dir_utils.cc - ${MICRO_DIR}/coder/utils/print_utils.cc - ) - -set(PRIMITIVE_OP_SRC - ${LITE_DIR}/src/ops/batch_norm.cc - ${LITE_DIR}/src/ops/primitive_c.cc - ${LITE_DIR}/src/ops/slice.cc - ${LITE_DIR}/src/ops/while.cc + ${MICRO_DIR}/coder/utils/type_cast.cc ) set(LITE_SRC - ${PRIMITIVE_OP_SRC} - ${LITE_DIR}/tools/common/flag_parser.cc ${LITE_DIR}/src/common/file_utils.cc ${LITE_DIR}/src/common/graph_util.cc ${LITE_DIR}/src/common/string_util.cc @@ -98,11 +94,25 @@ set(LITE_SRC ${LITE_DIR}/src/tensorlist.cc ${LITE_DIR}/src/tensor.cc ${LITE_DIR}/src/common/log_adapter.cc + ### src/ops for parameter and infer shape + ${LITE_DIR}/src/ops/batch_norm.cc + ${LITE_DIR}/src/ops/conv2d.cc + ${LITE_DIR}/src/ops/primitive_c.cc + ${LITE_DIR}/src/ops/slice.cc + ${LITE_DIR}/src/ops/while.cc + ### populate operator parameter + ${LITE_DIR}/src/ops/populate/conv2d_populate.cc + ### nnacl + ${LITE_DIR}/nnacl/base/minimal_filtering_generator.c + ${LITE_DIR}/nnacl/fp32/winograd_utils.c + ${LITE_DIR}/nnacl/fp32/pack_fp32.c ${LITE_DIR}/nnacl/int8/quantize.c ${LITE_DIR}/nnacl/int8/pack_int8.c ${LITE_DIR}/nnacl/int8/matmul_int8.c ${LITE_DIR}/nnacl/int8/fixed_point.c + ### tools + ${LITE_DIR}/tools/common/flag_parser.cc ) list(APPEND FILE_SET ${CODER_SRC} ${CODER_UTILS_SRC} ${CODER_OPCODERS_SRC} ${CODER_GENERATOR_SRC} - ${CODER_ALLOC_SRC} ${LITE_SRC}) + ${CODER_ALLOCATOR_SRC} ${LITE_SRC}) diff --git a/mindspore/lite/micro/coder/CMakeLists.txt b/mindspore/lite/micro/coder/CMakeLists.txt index 351152b007..446960ebc3 100644 --- a/mindspore/lite/micro/coder/CMakeLists.txt +++ b/mindspore/lite/micro/coder/CMakeLists.txt @@ -13,9 +13,7 @@ include_directories(${LITE_DIR}) include_directories(${TOP_DIR}/mindspore/core/) #include coder -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/utils) include(${MICRO_DIR}/cmake/file_list.cmake) add_executable(codegen main.cc ${FILE_SET}) add_dependencies(codegen fbs_src) diff --git a/mindspore/lite/micro/coder/allocator/allocator.cc b/mindspore/lite/micro/coder/allocator/allocator.cc index ebfdb89977..71c4f5d72f 100644 --- a/mindspore/lite/micro/coder/allocator/allocator.cc +++ b/mindspore/lite/micro/coder/allocator/allocator.cc @@ -19,25 +19,20 @@ #include #include "coder/allocator/memory_manager.h" #include "coder/opcoders/op_coder.h" -#include "coder/coder_config.h" namespace mindspore::lite::micro { -const std::map> types_map = { - {std::type_index(typeid(float *)), {kNumberTypeFloat32, sizeof(float)}}, - {std::type_index(typeid(int *)), {kNumberTypeInt32, sizeof(int)}}, - {std::type_index(typeid(int32_t *)), {kNumberTypeInt32, sizeof(int32_t)}}, - {std::type_index(typeid(int16_t *)), {kNumberTypeInt16, sizeof(int16_t)}}, - {std::type_index(typeid(int8_t *)), {kNumberTypeInt8, sizeof(int8_t)}}, -}; - void *MemoryAllocator::MallocWeightTensor(TypeId type_id, size_t size, MallocType type) { - auto item = types_map.find(std::type_index(typeid(type_id))); - MS_CHECK_TRUE_RET_NULL(item != types_map.end(), "unsupported type idnex"); - TypeId typei = item->second.first; - size_t type_size = item->second.second; + static const std::map size_map = {{kNumberTypeFloat32, sizeof(float)}, + {kNumberTypeInt32, sizeof(int)}, + {kNumberTypeInt32, sizeof(int32_t)}, + {kNumberTypeInt16, sizeof(int16_t)}, + {kNumberTypeInt8, sizeof(int8_t)}}; + auto item = size_map.find(type_id); + MS_CHECK_TRUE_RET_NULL(item != size_map.end(), "unsupported type idnex"); + size_t type_size = item->second; std::vector shape = {1, static_cast(size / type_size)}; auto cate = type == kOfflinePackWeight ? Tensor::Category::CONST_TENSOR : Tensor::Category::VAR; - Tensor *weight = new (std::nothrow) lite::Tensor(typei, shape, schema::Format_NHWC, cate); + Tensor *weight = new (std::nothrow) lite::Tensor(type_id, shape, schema::Format_NHWC, cate); MS_CHECK_PTR_RET_NULL(weight); std::string runtime_addr = net_weight_addr_ + std::to_string(weight_index_++); malloc_weights_addr_.insert(std::make_pair(weight, runtime_addr)); diff --git a/mindspore/lite/micro/coder/allocator/allocator.h b/mindspore/lite/micro/coder/allocator/allocator.h index 6488790a8d..f4f6614ca0 100644 --- a/mindspore/lite/micro/coder/allocator/allocator.h +++ b/mindspore/lite/micro/coder/allocator/allocator.h @@ -23,7 +23,7 @@ #include #include "coder/allocator/memory_manager.h" #include "coder/log.h" -#include "coder/utils/print_utils.h" +#include "coder/utils/type_cast.h" #include "src/tensor.h" #include "src/common/log_adapter.h" diff --git a/mindspore/lite/micro/coder/coder.cc b/mindspore/lite/micro/coder/coder.cc index 79e860dd9d..bb3be2ebe1 100644 --- a/mindspore/lite/micro/coder/coder.cc +++ b/mindspore/lite/micro/coder/coder.cc @@ -21,8 +21,8 @@ #include #include "schema/inner/model_generated.h" #include "tools/common/flag_parser.h" -#include "coder/session_coder.h" -#include "coder/coder_context.h" +#include "coder/session.h" +#include "coder/context.h" #include "utils/dir_utils.h" #include "securec/include/securec.h" #include "src/common/file_utils.h" diff --git a/mindspore/lite/micro/coder/coder.h b/mindspore/lite/micro/coder/coder.h index aad8139d07..3078efb3ac 100644 --- a/mindspore/lite/micro/coder/coder.h +++ b/mindspore/lite/micro/coder/coder.h @@ -14,11 +14,11 @@ * limitations under the License. */ -#ifndef MICRO_CODER_CODER_H_ -#define MICRO_CODER_CODER_H_ +#ifndef MINDSPORE_LITE_MICRO_CODER_CODER_H_ +#define MINDSPORE_LITE_MICRO_CODER_CODER_H_ #include #include -#include "coder/session_coder.h" +#include "coder/session.h" namespace mindspore::lite::micro { @@ -41,4 +41,4 @@ class Coder final { int RunCoder(int argc, const char **argv); } // namespace mindspore::lite::micro -#endif // MICRO_CODER_CODER_H_ +#endif // MINDSPORE_LITE_MICRO_CODER_CODER_H_ diff --git a/mindspore/lite/micro/coder/coder_config.h b/mindspore/lite/micro/coder/coder_config.h index 94cbd4984d..5fe9fb2454 100644 --- a/mindspore/lite/micro/coder/coder_config.h +++ b/mindspore/lite/micro/coder/coder_config.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef MICRO_CODER_CONFIG_H -#define MICRO_CODER_CONFIG_H +#ifndef MINDSPORE_LITE_MICRO_CODER_CONFIG_H +#define MINDSPORE_LITE_MICRO_CODER_CONFIG_H #include @@ -36,11 +36,13 @@ class Configurator { void set_code_path(const std::string &code_path) { code_path_ = code_path; } std::string code_path() const { return code_path_; } + void set_subgraph_(const std::string &subgraph) { sub_graph_ = subgraph; } + std::string sub_graph() { return sub_graph_; } + void set_target(Target target) { target_ = target; } Target target() const { return target_; } void set_code_mode(CodeMode code_mode) { code_mode_ = code_mode; } - CodeMode code_mode() const { return code_mode_; } void set_debug_mode(bool debug) { debug_mode_ = debug; } @@ -56,6 +58,7 @@ class Configurator { bool is_weight_file_{false}; std::string module_name_; std::string code_path_; + std::string sub_graph_; Target target_{kTargetUnknown}; CodeMode code_mode_{Code_Unknown}; bool debug_mode_{false}; diff --git a/mindspore/lite/micro/coder/coder_context.cc b/mindspore/lite/micro/coder/context.cc similarity index 97% rename from mindspore/lite/micro/coder/coder_context.cc rename to mindspore/lite/micro/coder/context.cc index e3a1066ae8..065879d1cb 100644 --- a/mindspore/lite/micro/coder/coder_context.cc +++ b/mindspore/lite/micro/coder/context.cc @@ -14,7 +14,7 @@ * limitations under the License. */ -#include "micro/coder/coder_context.h" +#include "micro/coder/context.h" #include "micro/coder/coder_config.h" #include "micro/coder/allocator/allocator.h" diff --git a/mindspore/lite/micro/coder/coder_context.h b/mindspore/lite/micro/coder/context.h similarity index 77% rename from mindspore/lite/micro/coder/coder_context.h rename to mindspore/lite/micro/coder/context.h index 0328a82ae5..77c93047be 100644 --- a/mindspore/lite/micro/coder/coder_context.h +++ b/mindspore/lite/micro/coder/context.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef MICRO_CODER_CODER_CONTEXT_H_ -#define MICRO_CODER_CODER_CONTEXT_H_ +#ifndef MINDSPORE_LITE_MICRO_CODER_CODER_CONTEXT_H_ +#define MINDSPORE_LITE_MICRO_CODER_CODER_CONTEXT_H_ #include #include #include @@ -24,8 +24,6 @@ #include #include #include "src/tensor.h" -#include "coder/utils/coder_utils.h" -#include "coder/utils/print_utils.h" namespace mindspore::lite::micro { class CoderContext { public: @@ -36,7 +34,13 @@ class CoderContext { std::vector init_contents() const { return initialContent_; } void set_code_blocks(const std::vector &code_block) { code_blocks_ = code_block; } - std::vector code_blocks() { return code_blocks_; } + std::vector code_blocks() const { return code_blocks_; } + + void set_inference_blocks(const std::vector &inference_blocks) { inference_blocks_ = inference_blocks; } + std::vector inference_blocks() const { return inference_blocks_; } + + void set_train_blocks(const std::vector &train_blocks) { train_blocks_ = train_blocks; } + std::vector train_blocks() const { return train_blocks_; } void set_tensor_map(const std::map &tensor_map) { tensors_map_.insert(tensor_map.begin(), tensor_map.end()); @@ -64,16 +68,13 @@ class CoderContext { void AppendInitCode(const std::string &codeBlock); std::set c_files() const { return c_files_; } - - void set_c_files(const std::set files) { c_files_.insert(files.begin(), files.end()); } + void set_c_files(const std::set &files) { c_files_.insert(files.begin(), files.end()); } std::set h_files() const { return h_files_; } - - void set_h_files(const std::set files) { h_files_.insert(files.begin(), files.end()); } + void set_h_files(const std::set &files) { h_files_.insert(files.begin(), files.end()); } std::set asm_files() const { return asm_files_; } - - void set_asm_files(const std::set files) { asm_files_.insert(files.begin(), files.end()); } + void set_asm_files(const std::set &files) { asm_files_.insert(files.begin(), files.end()); } private: std::vector graph_inputs_; @@ -101,9 +102,11 @@ class CoderContext { std::set asm_files_; // operator header files std::set h_files_; - // net.c's content, include the inference and prediction implementation + // net.c's content, include the Inference and Training implementation std::vector code_blocks_; + std::vector train_blocks_; + std::vector inference_blocks_; }; } // namespace mindspore::lite::micro -#endif // MICRO_CODER_CODER_CONTEXT_H_ +#endif // MINDSPORE_LITE_MICRO_CODER_CONTEXT_H_ diff --git a/mindspore/lite/micro/coder/debug.cc b/mindspore/lite/micro/coder/debug.cc deleted file mode 100644 index 91ef02ca2c..0000000000 --- a/mindspore/lite/micro/coder/debug.cc +++ /dev/null @@ -1,95 +0,0 @@ -/** - * 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 "micro/coder/debug.h" -#include -#include -#include -#include -#include "include/errorcode.h" -#include "micro/coder/utils/print_utils.h" -#include "micro/coder/coder_context.h" - -namespace mindspore::lite::micro { -void MicroDebug::DumpTensorData(Tensor *tensor, const std::string &tensor_addr, std::string *code_block_str, - bool is_input) { - *code_block_str += "\t\t\t{\n\t\t\t\tMicroTensor tensor;\n"; - - std::string format_str = "\t\t\t\ttensor.format = " + std::to_string(tensor->format()) + ";\n"; - std::string type_str = "\t\t\t\ttensor.type = " + GetMicroTensorDataType(tensor->data_type()) + ";\n"; - std::string ndim_str = "\t\t\t\ttensor.ndim = " + std::to_string(static_cast(tensor->shape().size())) + ";\n"; - - *code_block_str += "\t\t\t\tint dim[] = {"; - for (size_t i = 0; i < tensor->shape().size(); ++i) { - *code_block_str += std::to_string(tensor->shape().at(i)) + ", "; - } - *code_block_str += "};\n"; - *code_block_str += "\t\t\t\ttensor.dim = dim;\n"; - std::string data_str = "\t\t\t\ttensor.data = (void *)(" + tensor_addr + ");\n"; - std::string in_or_out = (is_input == 1 ? "input" : "output"); - std::string fprint_str = "\t\t\t\tfprintf(output_file, \"" + in_or_out + " Tensor:" + tensor_addr + "\\n\");\n"; - std::string print_str = "\t\t\t\tPrintTensor(&tensor,output_file," + std::to_string(is_input) + ");\n\t\t\t}\n"; - - *code_block_str += ndim_str; - *code_block_str += type_str; - *code_block_str += format_str; - *code_block_str += data_str; - *code_block_str += fprint_str; - *code_block_str += print_str; -} - -int MicroDebug::DumpNodeData(const std::unique_ptr &op_coder, - const std::map &tensor_addrs, std::string *code_block_str) { - auto config = Configurator::GetInstance(); - if (!config->debug_mode()) { - return RET_OK; - } - std::string node_name = op_coder->ID(); - std::string file_str = "\n\t\t{\n\t\t\tFILE *output_file = fopen( \"./" + node_name + - ".ir\", \"w\");\n\t\t\tfprintf(output_file, \"Node:" + op_coder->ID() + "\\n\");\n"; - - *code_block_str += file_str; - auto runtime_tensor_iterator = [&op_coder, tensor_addrs, &code_block_str](const std::vector &tensors, - bool dump_data) { - for (const auto &tensor : tensors) { - if (tensor->data_c() != nullptr) { - continue; - } - auto find_item = - std::find_if(tensor_addrs.begin(), tensor_addrs.end(), - [tensor](const std::pair &item) { return item.first == tensor; }); - if (find_item != tensor_addrs.end()) { - DumpTensorData(tensor, find_item->second, code_block_str, dump_data); - } - } - return RET_OK; - }; - int status = runtime_tensor_iterator(op_coder->input_tensors(), true); - if (status != RET_OK) { - MS_LOG(ERROR) << "dump runtime input tensor failed!"; - return status; - } - status = runtime_tensor_iterator(op_coder->output_tensors(), false); - if (status != RET_OK) { - MS_LOG(ERROR) << "dump runtime input tensor failed!"; - return status; - } - std::string end_file_str = "\t\t\tfclose(output_file);\n\t\t}\n"; - *code_block_str += end_file_str; - return RET_OK; -} - -} // namespace mindspore::lite::micro diff --git a/mindspore/lite/micro/coder/generator/component/benchmark_component.cc b/mindspore/lite/micro/coder/generator/component/benchmark_component.cc new file mode 100644 index 0000000000..3b42e2a2bf --- /dev/null +++ b/mindspore/lite/micro/coder/generator/component/benchmark_component.cc @@ -0,0 +1,191 @@ +/** + * 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/benchmark_component.h" +#include +#include "coder/generator/component/const_blocks/license.h" +#include "coder/log.h" +#include "include/errorcode.h" +#include "nnacl/op_base.h" + +namespace mindspore::lite::micro { +constexpr int kWarmUp = 3; +void CodeBenchmarkHeader(std::ofstream &ofs, const std::string &header) { + ofs << g_hwLicense; + ofs << "#include \n" + "#include \n" + "#include \n" + "#include \n" + "#include \"microtensor.h\"\n" + "#include \"load_input.h\"\n" + "#include \"debug_utils.h\"\n"; + ofs << "#include \"" << header << "\"\n"; +} + +void CodeBenchmarkUsage(std::ofstream &ofs) { + ofs << "void usage() {\n" + " printf(\n" + " \"-- mindspore micro params usage:\\n\"\n" + " \"args[0]: executable file\\n\"\n" + " \"args[1]: inputs binary file\\n\"\n" + " \"args[2]: model weight binary file\\n\"\n" + " \"args[3]: loop count for performance test\\n\"\n" + " \"args[4]: runtime thread num\\n\"\n" + " \"args[5]: runtime thread bind mode\\n\\n\");\n" + "}\n\n"; +} + +void CodeBenchmarkWarmup(std::ofstream &ofs, const std::string &module_name) { + ofs << "// the default number of warm-ups is 3\n" + << "void " << module_name << "_WarmUp() {\n" + << " for (int i = 0; i < " << kWarmUp << "; ++i) {\n" + << " " << module_name << "_Inference();\n" + << " }\n" + << "}\n"; +} + +void CodeBenchmarkSetInputs(std::ofstream &ofs, const std::string &module_name, + const std::unique_ptr &ctx) { + ofs << "int main(int argc, char **argv) {\n" + " if (argc < 2) {\n" + " MICRO_ERROR(\"input command is invalid\\n\");\n" + " usage();\n" + " return RET_ERROR;\n" + " }\n"; + std::vector inputs = ctx->graph_inputs(); + size_t inputs_num = inputs.size(); + ofs << " // input shape: "; + std::for_each(inputs.begin(), inputs.end(), [&](Tensor *t) { + ofs << "[ "; + for (int i : t->shape()) { + ofs << i << ", "; + } + ofs << "], "; + }); + ofs << "\n"; + ofs << " void *inputs_binbuf[" << inputs_num << "];\n"; + ofs << " int inputs_size[" << inputs_num << "] = {"; + for (size_t i = 0; i < inputs_num; ++i) { + Tensor *input = inputs[i]; + ofs << input->Size() << ", "; + } + ofs << "};\n"; + ofs << " int ret = ReadInputsFile(argv[1], inputs_binbuf, inputs_size, " << inputs_num + << ");\n" + " if (ret != RET_OK) {\n" + " MICRO_ERROR(\"read inputs file failed\");\n" + " return RET_ERROR;\n" + " }\n"; + ofs << " ret = " << module_name << "_SetInputs((const void **)inputs_binbuf, " << inputs_num + << ");\n" + " if (ret != RET_OK) {\n" + " MICRO_ERROR(\"set inputs failed\");\n" + " return RET_ERROR;\n" + " }\n"; +} + +void CodeBenchmarkSetBuffer(std::ofstream &ofs, const std::string &module_name) { + ofs << " int total_buffer_size = " << module_name << "_GetBufferSize();\n"; + ofs << " void *buffer = malloc(total_buffer_size);\n"; + ofs << " if (buffer == NULL ){\n" + " MICRO_ERROR(\"malloc memory buffer failed\");\n" + " return RET_ERROR;\n" + " }\n"; + ofs << " ret = " << module_name + << "_SetBuffer(buffer);\n" + " if (ret != RET_OK) {\n" + " MICRO_ERROR(\"set inputs failed\");\n" + " return RET_ERROR;" + " }\n"; +} + +void CodeBenchmarkInitWeight(std::ofstream &ofs, const std::string &module_name) { + ofs << " int weight_size = 0;\n" + " void *weight_buffer = ReadInputData(argv[2], &weight_size); \n" + " if(" + << module_name + << "_Init(weight_buffer, weight_size) != RET_OK) {\n" + " MICRO_ERROR(\"model init failed\");\n" + " " + << module_name + << "_FreeResource();\n" + " return RET_ERROR;\n" + " }\n" + " free(weight_buffer);\n" + " weight_buffer = NULL;\n"; +} + +void CodeBenchmarkConfigThread(std::ofstream &ofs) { + ofs << " int thread_num = 4;\n" + " BindMode bind_mode = NO_BIND_MODE;\n" + " if (argc >= 6) {\n" + " thread_num = atoi(argv[4]);\n" + " bind_mode = atoi(argv[5]);\n" + " }\n" + " ret = ConfigThreadPool(THREAD_POOL_DEFAULT, thread_num, bind_mode);\n" + " if (ret != 0) {\n" + " MICRO_ERROR(\"create thread pool failed\");\n" + " }\n"; +} + +void CodeBenchmarkInference(std::ofstream &ofs, const std::string &module_name) { + ofs << " if (argc >= 4) {\n" + << " " << module_name << "_WarmUp();\n" + << " uint64_t timeAvg = 0;\n" + << " int loop_count = atoi(argv[3]);\n" + << " printf(\"======Inference Start======\\n\");\n" + << " printf(\"cycles: %d\", loop_count);\n" + << " for (int i = 0; i < loop_count; i++) {\n" + << " uint64_t runBegin = GetTimeUs();\n" + << " " << module_name << "_Inference();\n" + << " uint64_t runEnd = GetTimeUs();\n" + << " uint64_t time = runEnd - runBegin;\n" + << " timeAvg += time;\n" + << " }\n" + << " float cunCost = (float)timeAvg / 1000.0f;\n" + << " printf(\"=======Inference End=======\\n\");\n" + " printf(\"total time:\\t %.5fms, per time: \\t %.5fms\\n\", cunCost, cunCost/loop_count);\n" + << " }\n"; + ofs << " " << module_name << "_Inference();\n"; +} + +void CodeBenchmarkPrintOutputs(std::ofstream &ofs, const std::string &module_name) { + ofs << " // print model outputs \n"; + ofs << " const MicroTensorList *outs = " << module_name << "_GetOutputs();\n"; + ofs << " for (int i = 0; i < outs->num; ++i) {\n" + " MicroTensor *tensor = outs->tensor + i;\n" + " PrintTensorData(tensor);\n" + " }\n"; + ofs << " printf(\"" << module_name << " inference success.\\n\");\n"; + ofs << " free(buffer);\n"; +} + +/** + * 1. free malloc memory buffer + * 2. set input and buffer to NULL, and free packed weight memory + * 3. free input binary memory + */ +void CodeBenchmarkFreeResourse(std::ofstream &ofs, const std::string &module_name, size_t inputs_num) { + ofs << " free(buffer);\n"; + ofs << " " << module_name << "_FreeResource();\n"; + ofs << " for (int i = 0; i < " << inputs_num << "; ++i) {\n"; + ofs << " free(inputs_binbuf[i]);\n" + " }\n" + " return RET_OK;" + "}\n\n"; +} + +} // namespace mindspore::lite::micro diff --git a/mindspore/lite/micro/coder/generator/component/benchmark_component.h b/mindspore/lite/micro/coder/generator/component/benchmark_component.h new file mode 100644 index 0000000000..e73e916c2d --- /dev/null +++ b/mindspore/lite/micro/coder/generator/component/benchmark_component.h @@ -0,0 +1,52 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MINDSPORE_LITE_MICRO_CODER_GENERATOR_BENCHMARK_COMPONENT_H_ +#define MINDSPORE_LITE_MICRO_CODER_GENERATOR_BENCHMARK_COMPONENT_H_ + +#include +#include +#include +#include +#include +#include "src/tensor.h" +#include "coder/context.h" + +namespace mindspore::lite::micro { +void CodeBenchmarkHeader(std::ofstream &ofs, const std::string &header); + +void CodeBenchmarkUsage(std::ofstream &ofs); + +void CodeBenchmarkWarmup(std::ofstream &ofs, const std::string &module_name); + +void CodeBenchmarkSetInputs(std::ofstream &ofs, const std::string &module_name, + const std::unique_ptr &ctx); + +void CodeBenchmarkSetBuffer(std::ofstream &ofs, const std::string &module_name); + +void CodeBenchmarkInitWeight(std::ofstream &ofs, const std::string &module_name); + +void CodeBenchmarkConfigThread(std::ofstream &ofs); + +void CodeBenchmarkInference(std::ofstream &ofs, const std::string &module_name); + +void CodeBenchmarkPrintOutputs(std::ofstream &ofs, const std::string &module_name); + +void CodeBenchmarkFreeResourse(std::ofstream &ofs, const std::string &module_name, size_t inputs_num); + +} // namespace mindspore::lite::micro + +#endif // MINDSPORE_LITE_MICRO_CODER_GENERATOR_BENCHMARK_COMPONENT_H_ diff --git a/mindspore/lite/micro/coder/generator/component/cmake_component.cc b/mindspore/lite/micro/coder/generator/component/cmake_component.cc new file mode 100644 index 0000000000..58cd691d8c --- /dev/null +++ b/mindspore/lite/micro/coder/generator/component/cmake_component.cc @@ -0,0 +1,55 @@ +/** + * 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/cmake_component.h" +#include +#include + +namespace mindspore::lite::micro { + +void CodeCMakeNetLibrary(std::ofstream &ofs, const std::string &module_name, const std::unique_ptr &ctx, + Target target) { + ofs << "include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include/)\n"; + if (target == kARM32M) { + ofs << "include_directories(${OP_HEADER_PATH}/cmsis)\n" + << "include_directories(${OP_HEADER_PATH}/cmsis/CMSIS/NN/Include)\n" + << "include_directories(${OP_HEADER_PATH}/cmsis/CMSIS/DSP/Include)\n" + << "include_directories(${OP_HEADER_PATH}/cmsis/CMSIS/Core/Include)\n"; + } + ofs << "set(OP_SRC\n"; + for (const std::string &c_file : ctx->c_files()) { + ofs << " " << c_file << ".o\n"; + } + ofs << " " << module_name << "_weight.c.o\n" + << " " << module_name << ".c.o\n" + << ")\n"; + + std::set kernel_cmake_asm_set_files = ctx->asm_files(); + if (!kernel_cmake_asm_set_files.empty()) { + ofs << "set(ASSEMBLY_SRC\n"; + for (const std::string &asm_file : kernel_cmake_asm_set_files) { + ofs << " " << asm_file << ".o\n"; + } + ofs << ")\n" + << "set_property(SOURCE ${ASSEMBLY_SRC} PROPERTY LANGUAGE C)\n" + << "list(APPEND OP_SRC ${ASSEMBLY_SRC})\n"; + } + + ofs << "file(GLOB NET_SRC ${CMAKE_CURRENT_SOURCE_DIR}/*.c)\n" + << "add_library(${PROJ_NAME} STATIC ${NET_SRC})\n"; +} + +} // namespace mindspore::lite::micro diff --git a/mindspore/lite/micro/coder/debug.h b/mindspore/lite/micro/coder/generator/component/cmake_component.h similarity index 55% rename from mindspore/lite/micro/coder/debug.h rename to mindspore/lite/micro/coder/generator/component/cmake_component.h index 1b613c2df5..7d3be32662 100644 --- a/mindspore/lite/micro/coder/debug.h +++ b/mindspore/lite/micro/coder/generator/component/cmake_component.h @@ -14,30 +14,22 @@ * limitations under the License. */ -#ifndef MINDSPORE_MICRO_LITE_CODER_DEBUG_H_ -#define MINDSPORE_MICRO_LITE_CODER_DEBUG_H_ +#ifndef MINDSPORE_LITE_MICRO_CODER_GENERATOR_CMAKE_COMPONENT_H_ +#define MINDSPORE_LITE_MICRO_CODER_GENERATOR_CMAKE_COMPONENT_H_ -#include -#include -#include #include +#include +#include #include +#include #include "src/tensor.h" -#include "micro/coder/opcoders/op_coder.h" +#include "coder/coder_config.h" +#include "coder/context.h" namespace mindspore::lite::micro { +void CodeCMakeNetLibrary(std::ofstream &ofs, const std::string &module_name, const std::unique_ptr &ctx, + Target target); -class MicroDebug { - public: - MicroDebug() = default; - ~MicroDebug() = default; - - static void DumpTensorData(Tensor *tensor, const std::string &tensor_addr, std::string *code_block_str, - bool is_input); - - static int DumpNodeData(const std::unique_ptr &op_coder, - const std::map &tensor_addrs, std::string *code_block_str); -}; } // namespace mindspore::lite::micro -#endif // MINDSPORE_LITE_MICRO_CODER_DEBUG_H_ +#endif // MINDSPORE_LITE_MICRO_CODER_GENERATOR_CMAKE_COMPONENT_H_ diff --git a/mindspore/lite/micro/coder/generator/component/common_component.cc b/mindspore/lite/micro/coder/generator/component/common_component.cc new file mode 100644 index 0000000000..fabf32e71f --- /dev/null +++ b/mindspore/lite/micro/coder/generator/component/common_component.cc @@ -0,0 +1,209 @@ +/** + * 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/common_component.h" +#include +#include "coder/generator/component/const_blocks/license.h" +#include "coder/utils/type_cast.h" +#include "coder/log.h" +#include "include/errorcode.h" +#include "nnacl/op_base.h" + +namespace mindspore::lite::micro { +void CodeSourceFileInclude(std::ofstream &ofs, const std::string &weight_file, const std::string &header) { + ofs << g_hwLicense << "#include \"microtensor.h\"\n" + << "#include \"" << weight_file << "\"\n" + << "#include \"" << header << "\"\n"; +} + +void CodeInputAndOutputState(std::ofstream &ofs, const std::string &module_name) { + ofs << "/**\n" + << " * set input tensors\n" + << " * @param inputs, the input data ptr's array of the model, the tensors' count of input may be greater than " + "one.\n" + << " * @param num, the input data's number of the model.\n" + << " **/\n" + << "int " << module_name << "_SetInputs(const void **inputs, int num);\n\n"; + + ofs << "/**\n" + << " * get output tensor of the model \n" + << " **/\n" + << "const MicroTensorList *" << module_name << "_GetOutputs();\n\n"; +} + +void PrintMicroTensors(std::ofstream &ofs, std::vector tensors, const std::string &name, + const std::map &tensors_map) { + for (size_t i = 0; i < tensors.size(); ++i) { + Tensor *tensor = tensors[i]; + auto item = tensors_map.find(tensor); + if (item == tensors_map.end()) { + MS_LOG(ERROR) << "nonexistent tensor"; + break; + } + ofs << " static int dim[] = {"; + for (size_t j = 0; j < tensor->shape().size(); ++j) { + ofs << tensor->shape()[j] << ", "; + } + ofs << "};\n" + << " " << name << "[" << i << "].ndim = " << tensor->shape().size() << ";\n" + << " " << name << "[" << i << "].dim = dim;\n" + << " " << name << "[" << i << "].type = " << EnumMicroTensorDataType(tensor->data_type()) << ";\n" + << " " << name << "[" << i << "].format = " << std::to_string(tensor->format()) << ";\n" + << " " << name << "[" << i << "].data =" << item->second << ";\n"; + } +} + +void CodeInputAndOutputImplement(std::ofstream &ofs, const std::string &module_name, + const std::unique_ptr &ctx) { + // input tensors + ofs << "\n// input tensors\n"; + std::vector inputs = ctx->graph_inputs(); + for (size_t i = 0; i < inputs.size(); ++i) { + ofs << "static const unsigned char *" << ctx->input_name() + std::to_string(i) << " = 0;\n"; + } + size_t size = inputs.size(); + ofs << "int " << module_name << "_SetInputs(const void **inputs, int num) {\n" + << " if (inputs == NULL) {\n" + " return RET_ERROR;\n" + " }\n" + << " if (num !=" << size << ") {\n" + << " return RET_ERROR;\n" + " }\n"; + for (size_t i = 0; i < size; ++i) { + ofs << "\t" << ctx->input_name() + std::to_string(i) << " = inputs[" << i << "];\n"; + } + ofs << " return RET_OK;\n}\n"; + + // output tensors + ofs << "\n// output tensors\n"; + std::vector outputs = ctx->graph_outputs(); + size_t output_num = outputs.size(); + std::string output_name = ctx->output_name(); + + ofs << "const MicroTensorList* " << module_name << "_GetOutputs() {\n" + << " static MicroTensor " << output_name << "[" << output_num << "] ;\n"; + + PrintMicroTensors(ofs, outputs, output_name, ctx->tensors_map()); + ofs << " static MicroTensorList " << module_name << "_TensorArray;\n" + << " " << module_name << "_TensorArray.num = " << output_num << ";\n" + << " " << module_name << "_TensorArray.tensor = &" << output_name << "[0];\n" + << " return &" << module_name << "_TensorArray; \n}\n"; +} + +void CodeGraphQuantArgsState(std::ofstream &ofs, const std::string &module_name) { + ofs << "/**\n" + << " * get input and output QuantArgs of the model \n" + << " **/\n" + << "GraphQuantArgs " << module_name << "_GetInOutQuantArgs();\n\n"; +} + +void CodeGraphQuantArgsImplement(std::ofstream &ofs, const std::string &module_name, + const std::unique_ptr &ctx) { + std::vector graph_inputs = ctx->graph_inputs(); + Tensor *in_tensor = graph_inputs.at(kInputIndex); + MS_CHECK_PTR_IF_NULL(in_tensor); + std::vector graph_outputs = ctx->graph_outputs(); + Tensor *out_tensor = graph_outputs.at(kOutputIndex); + MS_CHECK_PTR_IF_NULL(out_tensor); + std::vector in_quant_args = in_tensor->quant_params(); + std::vector out_quant_args = out_tensor->quant_params(); + if (graph_inputs.empty() || graph_outputs.empty() || in_quant_args.empty() || out_quant_args.empty()) { + MS_LOG(ERROR) << "code model quant args failed"; + return; + } + ofs << "GraphQuantArgs " << module_name << "_GetInOutQuantArgs() {\n" + << "\t\tGraphQuantArgs quan_args = { " << in_quant_args.at(0).scale << ", " << out_quant_args.at(0).scale << ", " + << in_quant_args.at(0).zeroPoint << ", " << out_quant_args.at(0).zeroPoint << "};\n" + << "\t\treturn quan_args;\n" + << "}\n"; +} + +void CodeInitWeightState(std::ofstream &ofs, const std::string &module_name) { + ofs << "/**\n" + << " * @param weight_buffer, the address of the weight binary file\n" + << " * @param weight_size, the size of the model file in bytes\n" + << " **/\n" + << "int " << module_name << "_Init(void *weight_buffer, int weight_size);\n\n"; +} + +void CodeManageResourceState(std::ofstream &ofs, const std::string &module_name) { + ofs << "/**\n" + << " * get the memory space size of the inference.\n" + << " **/\n" + << "int " << module_name << "_GetBufferSize();\n"; + + ofs << "/**\n" + << " * set the memory space for the inference\n" + << " **/\n" + << "int " << module_name << "_SetBuffer(void *buffer);\n\n"; + + ofs << "/**\n" + << " * free the memory of packed weights, and set the membuf buffer and input address to NULL\n" + << " **/\n" + << "void " << module_name << "_FreeResource();\n"; +} + +void CodeInitResourceImplement(std::ofstream &ofs, const std::string &module_name, + const std::unique_ptr &ctx) { + ofs << "int " << module_name << "deconv_GetBufferSize() {\n" + << " return " << ctx->total_buffer_size() << ";\n" + << "}\n"; + ofs << "int " << module_name << "_SetBuffer( void *buffer) {\n"; + ofs << " if (buffer == NULL) {\n" + " MICRO_ERROR(\"memory buffer is NULL\");\n" + " return RET_ERROR;\n" + " }\n"; + ofs << " " << ctx->buffer_name() + << " = buffer;\n" + " return RET_OK;\n" + "}\n"; +} + +void CodeFreeResourceImplement(std::ofstream &ofs, const std::string &module_name, + const std::unique_ptr &ctx) { + ofs << "void " << module_name << "_FreeResource() {\n"; + ofs << " " << ctx->buffer_name() << "= NULL;\n"; + std::vector inputs = ctx->graph_inputs(); + size_t size = inputs.size(); + for (size_t i = 0; i < size; ++i) { + ofs << " " << ctx->input_name() + std::to_string(i) << " = NULL;\n"; + } + ofs << " void *allocated[] = {"; + size_t num = 0; + for (const auto &item : ctx->tensors_map()) { + Tensor *tensor = item.first; + std::string name = item.second; + if (tensor->data_c() != nullptr && tensor->category() != Tensor::Category::CONST_TENSOR) { + ofs << name << ", "; + num++; + } + } + ofs << " };\n"; + ofs << " for (int i = 0; i < " << num << "; ++i) {\n" + << " free(allocated[i]);\n" + << " allocated[i] = NULL;\n" + << " }\n"; + ofs << "}\n"; +} + +void CodeInferenceState(std::ofstream &ofs, const std::string &module_name) { + ofs << "/**\n" + << " * net inference function\n" + << " **/\n" + << "void " << module_name << "_Inference();\n\n"; +} + +} // namespace mindspore::lite::micro diff --git a/mindspore/lite/micro/coder/generator/component/common_component.h b/mindspore/lite/micro/coder/generator/component/common_component.h new file mode 100644 index 0000000000..2c5d0ecda2 --- /dev/null +++ b/mindspore/lite/micro/coder/generator/component/common_component.h @@ -0,0 +1,51 @@ +/** + * 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_CODER_GENERATOR_COMMON_COMPONENT_H_ +#define MINDSPORE_LITE_MICRO_CODER_GENERATOR_COMMON_COMPONENT_H_ + +#include +#include +#include +#include +#include +#include "src/tensor.h" +#include "coder/context.h" + +namespace mindspore::lite::micro { +void CodeSourceFileInclude(std::ofstream &ofs, const std::string &weight_file, const std::string &header); + +void CodeInputAndOutputState(std::ofstream &ofs, const std::string &module_name); +void CodeInputAndOutputImplement(std::ofstream &ofs, const std::string &module_name, + const std::unique_ptr &ctx); + +void CodeGraphQuantArgsState(std::ofstream &ofs, const std::string &module_name); +void CodeGraphQuantArgsImplement(std::ofstream &ofs, const std::string &module_name, + const std::unique_ptr &ctx); + +void CodeInitWeightState(std::ofstream &ofs, const std::string &module_name); + +void CodeManageResourceState(std::ofstream &ofs, const std::string &module_name); +void CodeInitResourceImplement(std::ofstream &ofs, const std::string &module_name, + const std::unique_ptr &ctx); + +void CodeFreeResourceImplement(std::ofstream &ofs, const std::string &module_name, + const std::unique_ptr &ctx); + +void CodeInferenceState(std::ofstream &ofs, const std::string &module_name); +} // namespace mindspore::lite::micro + +#endif // MINDSPORE_LITE_MICRO_CODER_GENERATOR_COMMON_COMPONENT_H_ diff --git a/mindspore/lite/micro/coder/generator/const_blocks/cmake_lists_code.h b/mindspore/lite/micro/coder/generator/component/const_blocks/cmake_lists.h similarity index 100% rename from mindspore/lite/micro/coder/generator/const_blocks/cmake_lists_code.h rename to mindspore/lite/micro/coder/generator/component/const_blocks/cmake_lists.h diff --git a/mindspore/lite/micro/coder/generator/const_blocks/bench_debug_utils.h b/mindspore/lite/micro/coder/generator/component/const_blocks/debug_utils.h similarity index 89% rename from mindspore/lite/micro/coder/generator/const_blocks/bench_debug_utils.h rename to mindspore/lite/micro/coder/generator/component/const_blocks/debug_utils.h index 11f0d8f551..995b80b972 100644 --- a/mindspore/lite/micro/coder/generator/const_blocks/bench_debug_utils.h +++ b/mindspore/lite/micro/coder/generator/component/const_blocks/debug_utils.h @@ -42,11 +42,11 @@ static const char debug_utils_h[] = "#include \n" "#include \"microtensor.h\"\n" "\n" - "void PrintTensor(MicroTensor *tensor, FILE *output_file, int isInput);\n" + "void PrintTensor(MicroTensor *tensor, FILE *output_file, const char *is_input);\n" "\n" "void PrintTensorData(MicroTensor *tensor);\n" "\n" - "uint64_t GetTimeUs(void);\n" + "uint64_t GetTimeUs();\n" "\n" "#endif // MINDSPORE_LITE_MICRO_MICRODEBUGUTIL_H_\n"; @@ -238,31 +238,27 @@ static const char debug_utils_c[] = " fprintf(file, \"\\n\");\n" "}\n" "\n" - "void PrintTensor(MicroTensor *tensor, FILE *output_file, int isInput) {\n" + "void PrintTensor(MicroTensor *tensor, FILE *output_file, const char *is_input) {\n" " if (output_file != NULL) {\n" - " const char *tips = NULL;\n" - " if (isInput) {\n" - " tips = \"input\";\n" - " } else {\n" - " tips = \"output\";\n" - " }\n" - " fprintf(output_file, \"%s \", tips);\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\", tips, type, format, tensorSize);\n" - " fprintf(output_file, \"%s Data:\\n\", tips);\n" - " PrintDataToFile(tensor->data, tensorSize, tensor->type, output_file);\n" - " (void)fflush(output_file);\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(void) {\n" + "uint64_t GetTimeUs() {\n" " const int USEC = 1000000;\n" " const int MSEC = 1000;\n" " struct timespec ts = {0, 0};\n" diff --git a/mindspore/lite/micro/coder/generator/const_blocks/license.h b/mindspore/lite/micro/coder/generator/component/const_blocks/license.h similarity index 100% rename from mindspore/lite/micro/coder/generator/const_blocks/license.h rename to mindspore/lite/micro/coder/generator/component/const_blocks/license.h diff --git a/mindspore/lite/micro/coder/generator/const_blocks/bench_load_input.h b/mindspore/lite/micro/coder/generator/component/const_blocks/load_input.h similarity index 100% rename from mindspore/lite/micro/coder/generator/const_blocks/bench_load_input.h rename to mindspore/lite/micro/coder/generator/component/const_blocks/load_input.h diff --git a/mindspore/lite/micro/coder/generator/const_blocks/micro_tensor.h b/mindspore/lite/micro/coder/generator/component/const_blocks/micro_tensor.h similarity index 100% rename from mindspore/lite/micro/coder/generator/const_blocks/micro_tensor.h rename to mindspore/lite/micro/coder/generator/component/const_blocks/micro_tensor.h diff --git a/mindspore/lite/micro/coder/generator/component/weight_component.cc b/mindspore/lite/micro/coder/generator/component/weight_component.cc new file mode 100644 index 0000000000..6475e5f13c --- /dev/null +++ b/mindspore/lite/micro/coder/generator/component/weight_component.cc @@ -0,0 +1,150 @@ +/** + * 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/weight_component.h" +#include +#include +#include +#include "coder/generator/component/const_blocks/license.h" +#include "coder/utils/coder_utils.h" + +namespace mindspore::lite::micro { +void CodeWeightFileHeader(std::ofstream &ofs, const std::unique_ptr &ctx) { + ofs << g_hwLicense; + // include all operator header + for (const auto &h_file : ctx->h_files()) { + ofs << "#include \"" << h_file << "\"\n"; + } + ofs << "#include \n" + << "#include \n" + << "#include \"microtensor.h\"\n\n" + << "extern unsigned char *" << ctx->buffer_name() << ";\n"; +} + +void CodeModelParamsState(std::ofstream &ofs, const std::map &weights) { + for (auto &item : weights) { + std::string name = item.first; + Tensor *tensor = item.second; + if (tensor->category() == Tensor::Category::CONST_TENSOR) { + if (tensor->data_c() == nullptr) { + continue; + } + ofs << "extern const " << GetTensorDataType(tensor->data_type()) << name << "[];\n"; + } + } +} + +void CodeModelParamsData(std::ofstream &ofs, const std::map &weights) { + for (auto &item : weights) { + std::string name = item.first; + Tensor *tensor = item.second; + if (tensor->category() == Tensor::Category::CONST_TENSOR) { + if (tensor->data_c() == nullptr) { + continue; + } + ofs << "const " << GetTensorDataType(tensor->data_type()) << name << "[] = "; + PrintTensorData(tensor, ofs); + } + } +} + +void CodeModelParamsForNet(std::ofstream &hofs, std::ofstream &cofs, const std::unique_ptr &ctx) { + // reverse key and value of tensors_map + std::map address_map; + for (const auto &item : ctx->tensors_map()) { + address_map.insert(std::make_pair(item.second, item.first)); + } + for (auto &item : address_map) { + std::string name = item.first; + Tensor *tensor = item.second; + if (tensor->data_c() == nullptr) { + continue; + } + if (tensor->category() == Tensor::Category::CONST_TENSOR) { + hofs << "extern " << GetTensorDataType(tensor->data_type()) << name << " = [];\n"; + cofs << GetTensorDataType(tensor->data_type()) << name << " = [" << tensor->ElementsNum() << "];\n"; + } else if (tensor->category() == Tensor::Category::VAR) { + hofs << "extern " << GetTensorDataType(tensor->data_type()) << " *" << name << ";\n"; + cofs << GetTensorDataType(tensor->data_type()) << "*" << name << " = NULL;\n"; + } + } + cofs << "\n"; +} + +void CodeWeightInitFunc(std::ofstream &ofs, const std::string &module_name, const std::unique_ptr &ctx) { + ofs << "int " << module_name << "_Init(void *weight_buffer, int weight_size) {\n" + << " if (weight_buffer == NULL) {\n" + " MICRO_ERROR(\"weight buffer is NULL\");\n" + << " return RET_ERROR;\n" + << " }\n"; + + ofs << " struct ModelParameter {\n" + << " void *addr;\n" + << " size_t size;\n" + << " size_t offset;\n" + << " };\n"; + size_t params_num = 0; + size_t offset = 0; + std::string params; + std::string origins; + for (const auto &item : ctx->saved_weights()) { + std::string name = item.first; + Tensor *tensor = item.second; + if (tensor->category() != Tensor::Category::CONST_TENSOR) { + continue; + } + auto iter = ctx->tensors_map().find(tensor); + if (iter != ctx->tensors_map().end()) { + origins += " {" + name + ", " + std::to_string(tensor->Size()) + ", " + std::to_string(offset) + "},\n"; + params_num++; + } else { + TypeId data_type = tensor->data_type(); + params += + " " + GetTensorDataType(data_type) + "*" + name + " = (weight_buffer + " + std::to_string(offset) + ");\n"; + } + } + ofs << " struct ModelParameter model_params[] = {\n" << origins << " };\n"; + ofs << params << "\n"; + + ofs << "\n"; + ofs << " for(int i = 0; i < " << params_num << "; ++i) {\n" + << " if (model_params[i].offset + model_params[i].size > weight_size) {\n" + " MICRO_ERROR(\"buffer is invalid, size: %d, offset: %lu\", weight_size, model_params[i].offset);\n" + " return RET_ERROR;\n" + " }\n" + << " memcpy(model_params[i].addr, (weight_buffer + model_params[i].offset), model_params[i].size);\n" + << " }\n"; + for (const auto &block : ctx->init_contents()) { + ofs << "{\n" << block << "}\n"; + } + ofs << " return RET_OK;\n"; + ofs << "}\n\n"; +} + +void SaveDataToNet(const std::map &saved_weights, const std::string &net_file) { + std::ofstream net(net_file, std::ios::out | std::ios::trunc | std::ios::binary); + MS_CHECK_TRUE_WITHOUT_RET(net.is_open(), "net file open failed!"); + for (auto &item : saved_weights) { + std::string name = item.first; + Tensor *tensor = item.second; + if (tensor->category() == Tensor::Category::CONST_TENSOR && tensor->data_c() != nullptr) { + net.write(reinterpret_cast(tensor->data_c()), tensor->Size()); + } + } + net.close(); +} + +} // namespace mindspore::lite::micro diff --git a/mindspore/lite/micro/coder/generator/component/weight_component.h b/mindspore/lite/micro/coder/generator/component/weight_component.h new file mode 100644 index 0000000000..815a1dd215 --- /dev/null +++ b/mindspore/lite/micro/coder/generator/component/weight_component.h @@ -0,0 +1,41 @@ +/** + * 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_CODER_GENERATOR_WEIGHT_COMPONENT_H_ +#define MINDSPORE_LITE_MICRO_CODER_GENERATOR_WEIGHT_COMPONENT_H_ + +#include +#include +#include +#include +#include +#include "src/tensor.h" +#include "coder/coder_config.h" +#include "coder/context.h" + +namespace mindspore::lite::micro { + +void CodeWeightFileHeader(std::ofstream &ofs, const std::unique_ptr &ctx); +void CodeModelParamsState(std::ofstream &ofs, const std::map &weights); +void CodeModelParamsData(std::ofstream &ofs, const std::map &weights); + +void SaveDataToNet(const std::map &saved_weights, const std::string &net_file); +void CodeModelParamsForNet(std::ofstream &hofs, std::ofstream &cofs, const std::unique_ptr &ctx); +void CodeWeightInitFunc(std::ofstream &ofs, const std::string &module_name, const std::unique_ptr &ctx); + +} // namespace mindspore::lite::micro + +#endif // MINDSPORE_LITE_MICRO_CODER_GENERATOR_WEIGHT_COMPONENT_H_ diff --git a/mindspore/lite/micro/coder/generator/generator.cc b/mindspore/lite/micro/coder/generator/generator.cc index 03eb7f27b7..0be8854b26 100644 --- a/mindspore/lite/micro/coder/generator/generator.cc +++ b/mindspore/lite/micro/coder/generator/generator.cc @@ -18,15 +18,27 @@ #include #include #include -#include "coder/generator/utils/generator_utils.h" -#include "coder/generator/const_blocks/cmake_lists_code.h" -#include "coder/generator/const_blocks/bench_debug_utils.h" -#include "coder/generator/const_blocks/bench_load_input.h" -#include "coder/generator/const_blocks/micro_tensor.h" -#include "coder/generator/const_blocks/license.h" +#include "coder/generator/component/cmake_component.h" +#include "coder/generator/component/weight_component.h" +#include "coder/generator/component/const_blocks/micro_tensor.h" +#include "coder/generator/component/const_blocks/cmake_lists.h" +#include "coder/generator/component/const_blocks/debug_utils.h" +#include "coder/generator/component/const_blocks/load_input.h" +#include "coder/generator/component/const_blocks/license.h" #include "micro/coder/log.h" namespace mindspore::lite::micro { +int WriteContentToFile(const std::string &file, const std::string &content) { + std::ofstream of(file); + if (of.bad()) { + MS_LOG(ERROR) << "open file error " << file; + return RET_ERROR; + } + MS_LOG(INFO) << "write " << file; + of << content; + of.close(); + return RET_OK; +} Generator::Generator(std::unique_ptr ctx) { ctx_ = std::move(ctx); @@ -46,104 +58,6 @@ Generator::Generator(std::unique_ptr ctx) { Generator::~Generator() { (void)umask(origin_umask_); } -int Generator::CodeGraphInOutQuanArgs(std::ofstream &ofs) { - std::vector graph_inputs = ctx_->graph_inputs(); - if (graph_inputs.empty()) { - MS_LOG(ERROR) << "this graph has no input tensor"; - return RET_ERROR; - } - Tensor *in_tensor = graph_inputs.at(kInputIndex); - MS_CHECK_PTR(in_tensor); - std::vector graph_outputs = ctx_->graph_outputs(); - if (graph_outputs.empty()) { - MS_LOG(ERROR) << "this graph has no output tensor"; - return RET_ERROR; - } - Tensor *out_tensor = graph_outputs.at(kOutputIndex); - MS_CHECK_PTR(out_tensor); - std::vector in_quant_args = in_tensor->quant_params(); - std::vector out_quant_args = out_tensor->quant_params(); - if (in_quant_args.empty() || out_quant_args.empty()) { - MS_LOG(WARNING) << "in_quant_args or out_quant_args is empty"; - return RET_OK; - } - ofs << "GraphQuantArgs " << config_->module_name() << "_GetInOutQuantArgs() {\n" - << "\t\t" - << "GraphQuantArgs quan_args = { " << in_quant_args.at(0).scale << ", " << out_quant_args.at(0).scale << ", " - << in_quant_args.at(0).zeroPoint << ", " << out_quant_args.at(0).zeroPoint << "};\n" - << "\t\t" - << "return quan_args;\n" - << "}\n"; - return RET_OK; -} - -int Generator::CodeNetFileInputOutput(std::ofstream &ofs) { - // input tensors - ofs << "\n// set input tensors\n"; - std::vector inputs = ctx_->graph_inputs(); - for (size_t i = 0; i < inputs.size(); ++i) { - ofs << "\nstatic const unsigned char *" << ctx_->input_name() + std::to_string(i) << " = 0;\n"; - } - size_t size = inputs.size(); - ofs << "int " << config_->module_name() << "_SetInputs(const void **inputs, int num) {\n" - << "if (inputs == NULL) {\n" - "\treturn RET_ERROR;\n" - "\t}\n" - << "\tif (num !=" << size << ") { return RET_ERROR;}\n"; - for (size_t i = 0; i < size; ++i) { - ofs << "\t" << ctx_->input_name() + std::to_string(i) << " = inputs[" << i << "];\n"; - } - ofs << "\treturn RET_OK;\n}\n"; - - // output tensors - ofs << "\n// output tensors\n"; - std::vector outputs = ctx_->graph_outputs(); - size_t output_num = outputs.size(); - std::string output_name = ctx_->output_name(); - - ofs << "const MicroTensorList* " << config_->module_name() << "_GetOutputs() {\n" - << " static MicroTensor " << output_name << "[" << output_num << "] ;\n"; - - if (PrintMicroTensors(ofs, outputs, output_name, ctx_->tensors_map()) != RET_OK) { - return RET_ERROR; - } - ofs << " static MicroTensorList " << config_->module_name() << "_TensorArray;\n" - << " " << config_->module_name() << "_TensorArray.num = " << output_num << ";\n" - << " " << config_->module_name() << "_TensorArray.tensor = &" << output_name << "[0];\n" - << " return &" << config_->module_name() << "_TensorArray; \n}\n"; - return RET_OK; -} - -void Generator::CodeNetFileMembuffer(std::ofstream &ofs) { - // memory buffer - ofs << "\n// Get MemBuffer Size\n" - << "unsigned int " << config_->module_name() << "_GetBufferSize() {\n" - << "\t return " << ctx_->total_buffer_size() << "; \n}\n"; - - ofs << "\n// set Membuffer address\n"; - ofs << "int " << config_->module_name() << "_SetBuffer( void *buffer) { \n"; - ofs << "\tif (buffer == NULL) {\n" - "\t\tMICRO_ERROR(\"memory buffer is NULL\");\n" - "\t\treturn RET_ERROR;\n" - "\t}\n"; - ofs << "\t" << ctx_->buffer_name() - << "= buffer; \n" - "\treturn RET_OK;"; - ofs << "}\n"; -} - -void Generator::CodeNetFileInclude(std::ofstream &ofs) { - ofs << g_hwLicense; - // need copy head file of microtensor ro dst'dirs - ofs << "#include \"microtensor.h\"\n"; - // copy debug head files to cmake include files - ofs << "#include \"" << net_weight_hfile_ << "\"\n" - << "#include \"" << net_inc_hfile_ << "\"\n"; - if (config_->debug_mode()) { - ofs << "#include \"../benchmark/debug_utils.h\"\n"; - } -} - void Generator::CodeNetRunFunc(std::ofstream &ofs) { // generate net predict code ofs << "void " << config_->module_name() << "_Inference() {\n"; @@ -158,69 +72,30 @@ void Generator::CodeNetRunFunc(std::ofstream &ofs) { ofs << "}\n"; } -int Generator::CodeTestCMakeFile() { +int Generator::CodeBenchmarkCMakeFile() { std::string net_main_cmake_file_path = net_main_file_path_; std::string test_cmake_file = net_main_cmake_file_path + "benchmark.cmake"; - std::ofstream of(test_cmake_file); - if (of.bad()) { - MS_LOG(ERROR) << "open file error " << test_cmake_file; - return RET_ERROR; - } - + std::ofstream ofs(test_cmake_file); + MS_CHECK_TRUE(!ofs.bad(), "filed to open file"); MS_LOG(INFO) << "write " << test_cmake_file; - of << "include_directories(${CMAKE_CURRENT_SOURCE_DIR})\n"; - of << "include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include/)\n"; - of << "set(SRC_FILES\n"; - of << "\t\t" << config_->module_name() + "_benchmark.c\n"; - of << "\t\tload_input.c\n"; - of << "\t\tdebug_utils.c\n"; - of << ")\n"; - of.close(); - return RET_OK; -} - -int Generator::CodeCMakeExecutableFile(std::ofstream &ofs) const { + ofs << "include_directories(${CMAKE_CURRENT_SOURCE_DIR})\n"; ofs << "include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include/)\n"; - if (config_->target() == kARM32M) { - IncludeCmsisDirectories(ofs); - } - ofs << "set(OP_SRC\n"; - for (const std::string &c_file : ctx_->c_files()) { - ofs << " " << c_file << ".o\n"; - } - ofs << " " << config_->module_name() << "_weight.c.o\n"; - ofs << " " << config_->module_name() << ".c.o\n"; + ofs << "set(SRC_FILES\n"; + ofs << "\t\t" << config_->module_name() + "_benchmark.c\n"; + ofs << "\t\tload_input.c\n"; + ofs << "\t\tdebug_utils.c\n"; ofs << ")\n"; - - std::set kernel_cmake_asm_set_files = ctx_->asm_files(); - if (!kernel_cmake_asm_set_files.empty()) { - ofs << "set(ASSEMBLY_SRC\n"; - for (const std::string &asm_file : kernel_cmake_asm_set_files) { - ofs << " " << asm_file << ".o\n"; - } - ofs << ")\n"; - ofs << "set_property(SOURCE ${ASSEMBLY_SRC} PROPERTY LANGUAGE C)\n"; - ofs << "list(APPEND OP_SRC ${ASSEMBLY_SRC})\n"; - } - - ofs << "file(GLOB NET_SRC ${CMAKE_CURRENT_SOURCE_DIR}/*.c)\n"; - ofs << "add_library(${PROJ_NAME} STATIC ${NET_SRC})\n"; + ofs.close(); return RET_OK; } -int Generator::CodeCMakeFile() { +int Generator::CodeSourceCMakeFile() { std::string src_cmake_file = net_src_file_path_ + cmake_file_name_; - std::ofstream of(src_cmake_file); - if (of.bad()) { - MS_LOG(ERROR) << "open file error " << src_cmake_file; - return RET_ERROR; - } - MS_LOG(INFO) << "write " << src_cmake_file.c_str(); - if (CodeCMakeExecutableFile(of) != RET_OK) { - of.close(); - return RET_ERROR; - } - of.close(); + std::ofstream ofs(src_cmake_file); + MS_CHECK_TRUE(!ofs.bad(), "filed to open file"); + MS_LOG(INFO) << "write " << src_cmake_file; + CodeCMakeNetLibrary(ofs, config_->module_name(), ctx_, config_->target()); + ofs.close(); return RET_OK; } @@ -243,86 +118,32 @@ int Generator::CodeStaticContent() { return RET_OK; } -void Generator::CodeWeightInitFunc(const std::map &address_map, std::ofstream &ofs) { - ofs << "int " << config_->module_name() << "_Init(void *weight_buffer, int weight_size) {\n" - << "\tif (weight_buffer == NULL) {\n" - "\t\tMICRO_ERROR(\"weight buffer is NULL\");\n" - << "\t\treturn RET_ERROR;\n" - << "\t}\n"; - CodeReadModelParams(ctx_->saved_weights(), ctx_->tensors_map(), ofs); - for (const auto &block : ctx_->init_contents()) { - ofs << "{\n" << block << "\n}\n"; - } - ofs << "return RET_OK;"; - ofs << "}\n\n"; -} - -void Generator::CodeFreeResource(const std::map &address_map, std::ofstream &ofs) const { - ofs << "\tvoid *allocated[] = {"; - size_t num = 0; - for (const auto &item : address_map) { - std::string name = item.first; - Tensor *tensor = item.second; - if (tensor->data_c() != nullptr && tensor->category() != Tensor::Category::CONST_TENSOR) { - ofs << name << ", "; - num++; - } - } - ofs << "\t};\n"; - - ofs << "\tfor (int i = 0; i < " << num << "; ++i) {\n"; - ofs << "\t\tfree(allocated[i]);\n"; - ofs << "\t\tallocated[i] = NULL;\n"; - ofs << "\t}\n"; -} - int Generator::CodeWeightFile() { // weight header file std::string hfile = net_src_file_path_ + net_weight_hfile_; std::ofstream hofs(hfile); - if (hofs.bad()) { - MS_LOG(ERROR) << "open file error" << hfile; - return RET_ERROR; - } - hofs << g_hwLicense; - for (const auto &h_file : ctx_->h_files()) { - hofs << "#include \"" << h_file << "\"\n"; - } - hofs << "#include \n"; - hofs << "#include \n"; - hofs << "#include \"microtensor.h\"\n\n"; - hofs << "extern unsigned char *" << ctx_->buffer_name() << ";\n"; + MS_CHECK_TRUE(!hofs.bad(), "filed to open file"); + MS_LOG(INFO) << "write " << hfile; + CodeWeightFileHeader(hofs, ctx_); // weight source file std::string cfile = net_src_file_path_ + config_->module_name() + "_weight.c"; std::ofstream cofs(cfile); - if (cofs.bad()) { - MS_LOG(ERROR) << "open file error" << cfile; - return RET_ERROR; - } + MS_CHECK_TRUE(!cofs.bad(), "filed to open file"); + MS_LOG(INFO) << "write " << cfile; cofs << g_hwLicense; cofs << "#include \"" << net_weight_hfile_ << "\"\n\n"; cofs << "unsigned char * " << ctx_->buffer_name() << " = 0 ; \n"; - // reverse key and value of tensors_map - std::map address_map; - for (const auto &item : ctx_->tensors_map()) { - address_map.insert(std::make_pair(item.second, item.first)); - } - if (config_->is_weight_file()) { std::string net_file = net_src_file_path_ + config_->module_name() + ".net"; - if (SaveDataToNet(ctx_->saved_weights(), net_file) != RET_OK) { - hofs.close(); - cofs.close(); - return RET_ERROR; - } - CodeModelParamsDefine(address_map, hofs, cofs); - CodeWeightInitFunc(address_map, cofs); + SaveDataToNet(ctx_->saved_weights(), net_file); + CodeModelParamsForNet(hofs, cofs, ctx_); + CodeWeightInitFunc(cofs, config_->module_name(), ctx_); } else { - CodeModelParamsDefineAndData(ctx_->saved_weights(), hofs, cofs); + CodeModelParamsState(hofs, ctx_->saved_weights()); + CodeModelParamsData(cofs, ctx_->saved_weights()); } - hofs.close(); cofs.close(); return RET_OK; @@ -332,9 +153,9 @@ int Generator::GenerateCode() { MS_CHECK_RET_CODE(CodeNetHFile(), "code net h file failed."); MS_CHECK_RET_CODE(CodeNetCFile(), "code net c file failed."); MS_CHECK_RET_CODE(CodeWeightFile(), "code weight file failed."); - MS_CHECK_RET_CODE(CodeCMakeFile(), "code net cmake file failed."); - MS_CHECK_RET_CODE(CodeTestFile(), "code test file failed."); - MS_CHECK_RET_CODE(CodeTestCMakeFile(), "code test cmake file failed."); + MS_CHECK_RET_CODE(CodeSourceCMakeFile(), "code net cmake file failed."); + MS_CHECK_RET_CODE(CodeBenchmarkFile(), "code benchmark file failed."); + MS_CHECK_RET_CODE(CodeBenchmarkCMakeFile(), "code benchmark cmake file failed."); MS_CHECK_RET_CODE(CodeStaticContent(), "code static content failed."); return RET_OK; } diff --git a/mindspore/lite/micro/coder/generator/generator.h b/mindspore/lite/micro/coder/generator/generator.h index 7b10ad79a3..5543b3dccf 100644 --- a/mindspore/lite/micro/coder/generator/generator.h +++ b/mindspore/lite/micro/coder/generator/generator.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef MINDSPORE_MICRO_CODER_GENERATOR_H_ -#define MINDSPORE_MICRO_CODER_GENERATOR_H_ +#ifndef MINDSPORE_LITE_MICRO_CODER_GENERATOR_H_ +#define MINDSPORE_LITE_MICRO_CODER_GENERATOR_H_ #include #include @@ -28,13 +28,12 @@ #include #include "include/errorcode.h" #include "src/tensor.h" +#include "coder/log.h" #include "coder/coder_config.h" -#include "coder/coder_context.h" -#include "coder/utils/print_utils.h" +#include "coder/context.h" +#include "coder/utils/type_cast.h" namespace mindspore::lite::micro { -constexpr int kWarmUp = 3; - class Generator { public: explicit Generator(std::unique_ptr ctx); @@ -43,18 +42,12 @@ class Generator { int GenerateCode(); protected: - virtual int CodeTestFile() = 0; + virtual int CodeBenchmarkFile() = 0; virtual int CodeNetHFile() = 0; virtual int CodeNetCFile() = 0; - virtual int CodeCMakeFile(); virtual int CodeWeightFile(); - void CodeNetFileInclude(std::ofstream &ofs); - int CodeNetFileInputOutput(std::ofstream &ofs); - void CodeNetFileMembuffer(std::ofstream &ofs); void CodeNetRunFunc(std::ofstream &ofs); - int CodeGraphInOutQuanArgs(std::ofstream &ofs); - void CodeFreeResource(const std::map &address_map, std::ofstream &ofs) const; Configurator *config_{nullptr}; std::unique_ptr ctx_{nullptr}; @@ -70,10 +63,9 @@ class Generator { std::string net_main_file_path_; private: - int CodeTestCMakeFile(); + int CodeBenchmarkCMakeFile(); + int CodeSourceCMakeFile(); int CodeStaticContent(); - int CodeCMakeExecutableFile(std::ofstream &ofs) const; - void CodeWeightInitFunc(const std::map &address_map, std::ofstream &ofs); std::string cmake_file_name_{"net.cmake"}; // the user's generated file's permission @@ -84,4 +76,4 @@ class Generator { } // namespace mindspore::lite::micro -#endif // MINDSPORE_MICRO_CODER_GENERATOR_H_ +#endif // MINDSPORE_LITE_MICRO_CODER_GENERATOR_H_ diff --git a/mindspore/lite/micro/coder/generator/inference/inference_generator.cc b/mindspore/lite/micro/coder/generator/inference/inference_generator.cc index b5aa0b41e2..33acd925cf 100644 --- a/mindspore/lite/micro/coder/generator/inference/inference_generator.cc +++ b/mindspore/lite/micro/coder/generator/inference/inference_generator.cc @@ -16,284 +16,75 @@ #include "coder/generator/inference/inference_generator.h" #include -#include -#include #include -#include "coder/generator/const_blocks/license.h" +#include "coder/generator/component/common_component.h" +#include "coder/generator/component/benchmark_component.h" +#include "coder/generator/component/const_blocks/license.h" namespace mindspore::lite::micro { - int InferenceGenerator::CodeNetHFile() { std::string net_include_file = net_inc_file_path_ + net_inc_hfile_; std::ofstream ofs(net_include_file); - if (ofs.bad()) { - MS_LOG(ERROR) << "open file error " << net_include_file.c_str(); - return RET_ERROR; - } + MS_CHECK_TRUE(!ofs.bad(), "filed to open file"); + MS_LOG(INFO) << "write " << net_include_file; ofs << g_hwLicense; if (config_->code_mode() == CodeMode::Code_Android) { ofs << "#include \"src/runtime/thread_pool.h\"\n"; } ofs << "#include \"microtensor.h\"\n\n"; - - ofs << "/**\n" - << " * set input tensors\n" - << " * @param inputs, the input data ptr's array of the model, the tensors' count of input may be greater than " - "one.\n" - << " * @param num, the input data's number of the model.\n" - << " **/\n" - << "int " << config_->module_name() << "_SetInputs(const void **inputs, int num);\n\n"; - - ofs << "/**\n" - << " * get output tensor of the model \n" - << " **/\n" - << "const MicroTensorList *" << config_->module_name() << "_GetOutputs();\n\n"; - + CodeInputAndOutputState(ofs, config_->module_name()); if (is_get_quant_args_) { - std::vector graph_inputs = ctx_->graph_inputs(); - if (graph_inputs.empty()) { - MS_LOG(ERROR) << "this graph has no input tensor"; - ofs.close(); - return RET_ERROR; - } - size_t total_input_size = std::accumulate( - graph_inputs.begin(), graph_inputs.end(), 0UL, - [](size_t total_input_size, const Tensor *const tensor) { return total_input_size += tensor->Size(); }); - ofs << "/**\n"; - ofs << " * get input sizes of the model \n"; - ofs << " **/\n"; - ofs << "inline int " << config_->module_name() << "_GetInputSizes() {\n" - << "\t\t" - << "return " << total_input_size << ";\n" - << "}\n\n"; - - ofs << "/**\n"; - ofs << " * get input and output QuantArgs of the model \n"; - ofs << " **/\n"; - ofs << "GraphQuantArgs " << config_->module_name() << "_GetInOutQuantArgs();\n\n"; + CodeGraphQuantArgsState(ofs, config_->module_name()); } - if (config_->is_weight_file()) { - ofs << "/**\n" - << " * @param weightBuffer, the ptr of the model's parameters\n" - << " * @param weightSize, the size of the model's parameters\n" - << " **/\n" - << "int " << config_->module_name() << "_Init(void *weightBuffer, int weightSize);\n\n"; + CodeInitWeightState(ofs, config_->module_name()); } - - ofs << "/**\n" - << " * free the memory of packed weights and model's workspace buffer, input address\n" - << " **/\n" - << "void " << config_->module_name() << "_FreeResource();\n"; - - ofs << "/**\n" - << " * get the memory space size of the inference.\n" - << " **/\n" - << "unsigned int " << config_->module_name() << "_GetBufferSize();\n"; - - ofs << "/**\n" - << " * set the memory space for the inference\n" - << " **/\n" - << "int " << config_->module_name() << "_SetBuffer(void *buffer);\n\n"; - - ofs << "/**\n" - << " * net inference function\n" - << " **/\n" - << "void " << config_->module_name() << "_Inference();\n\n"; + CodeManageResourceState(ofs, config_->module_name()); + CodeInferenceState(ofs, config_->module_name()); return RET_OK; } int InferenceGenerator::CodeNetCFile() { std::string net_impl_file = net_src_file_path_ + net_src_cfile_; std::ofstream ofs(net_impl_file); - if (ofs.bad()) { - MS_LOG(ERROR) << "open file error" << net_impl_file.c_str(); - return RET_ERROR; - } - MS_LOG(DEBUG) << "write " << net_impl_file.c_str(); - CodeNetFileInclude(ofs); - CodeNetFileMembuffer(ofs); + MS_CHECK_TRUE(!ofs.bad(), "filed to open file"); + MS_LOG(INFO) << "write " << net_impl_file; + CodeSourceFileInclude(ofs, net_weight_hfile_, net_inc_hfile_); + CodeInputAndOutputImplement(ofs, config_->module_name(), ctx_); + CodeInitResourceImplement(ofs, config_->module_name(), ctx_); + CodeFreeResourceImplement(ofs, config_->module_name(), ctx_); if (is_get_quant_args_) { - if (CodeGraphInOutQuanArgs(ofs) != RET_OK) { - MS_LOG(ERROR) << "CodeGraphInOutQuanArgs failed"; - ofs.close(); - return RET_ERROR; - } - } - if (CodeNetFileInputOutput(ofs) != RET_OK) { - ofs.close(); - return RET_ERROR; + CodeGraphQuantArgsImplement(ofs, config_->module_name(), ctx_); } - ofs << "void " << config_->module_name() << "_FreeResource() {\n"; - ofs << "\t" << ctx_->buffer_name() << "= NULL;\n"; - std::vector inputs = ctx_->graph_inputs(); - size_t size = inputs.size(); - for (size_t i = 0; i < size; ++i) { - ofs << "\t" << ctx_->input_name() + std::to_string(i) << " = NULL;\n"; - } - std::map address_map; - for (const auto &item : ctx_->tensors_map()) { - address_map.insert(std::make_pair(item.second, item.first)); - } - if (config_->is_weight_file()) { - CodeFreeResource(address_map, ofs); - } - ofs << "}\n"; CodeNetRunFunc(ofs); ofs.close(); return RET_OK; } -void InferenceGenerator::CodeTestRelevantHeader(std::ofstream &code_test_ofs) { - code_test_ofs << g_hwLicense; - code_test_ofs << "#include \n" - "#include \n" - "#include \n" - "#include \n" - "#include \"microtensor.h\"\n" - "#include \"load_input.h\"\n" - "#include \"debug_utils.h\"\n"; - code_test_ofs << "#include \"" << net_inc_hfile_ << "\"\n"; - - code_test_ofs << "/**\n" - " * mindspore micro params usage:\n" - " * args[0]: executable file\n" - " * args[1]: inputs .bin file\n" - " * args[2]: model weight .net file\n" - " * args[3]: loop count for performance testing\n" - " * args[4]: runtime thread num\n" - " * args[5]: runtime thread bind mode\n" - " */\n"; - - code_test_ofs << "\n// Warm up. \n" - << "void " << config_->module_name() << "_WarmUp() {\n" - << "\tfor (int i = 0; i < " << kWarmUp << "; ++i) {\n" - << "\t\t" << config_->module_name() << "_Inference();\n" - << "\t}\n" - << "}\n"; -} - -void InferenceGenerator::CodeTestRelevantTile(std::ofstream &code_test_ofs) { - if (config_->code_mode() == Code_Android) { - code_test_ofs << " DestroyThreadPool(THREAD_POOL_DEFAULT);\n"; - } - code_test_ofs << " // print model outputs \n"; - code_test_ofs << " const MicroTensorList *outs = " << config_->module_name() << "_GetOutputs();\n"; - code_test_ofs << " for (int i = 0; i < outs->num; ++i) {\n" - " MicroTensor *tensor = outs->tensor + i;\n" - " PrintTensorData(tensor);\n" - " }\n"; - code_test_ofs << " printf(\"" << config_->module_name() << " inference End.\\n\");\n"; - code_test_ofs << " free(buffer);\n"; - code_test_ofs << " " << config_->module_name() << "_FreeResource();\n"; - std::vector inputs = ctx_->graph_inputs(); - size_t inputs_num = inputs.size(); - code_test_ofs << " // this code_block can be ignore \n"; - code_test_ofs << " for (int i = 0; i < " << inputs_num - << "; ++i) {\n" - " free(inputs_binbuf[i]);\n" - " }\n"; - code_test_ofs << " return 0;\n"; - code_test_ofs << "}\n"; -} - -int InferenceGenerator::CodeTestFile() { +int InferenceGenerator::CodeBenchmarkFile() { std::string net_main_impl_file = net_main_file_path_ + net_main_cfile_; std::ofstream ofs(net_main_impl_file); - if (ofs.bad()) { - MS_LOG(ERROR) << "open file error " << net_main_impl_file.c_str(); - return RET_ERROR; - } - MS_LOG(INFO) << "write " << net_main_impl_file.c_str(); - CodeTestRelevantHeader(ofs); - ofs << "int main(int argc, char **argv) {\n" - " if (argc < 2) { printf(\"There is not input and out file.\\n\"); }\n"; - ofs << " printf(\"" << config_->module_name() << " inference Start.\\n\");\n"; + MS_LOG(INFO) << "write " << net_main_impl_file; + MS_CHECK_TRUE(!ofs.bad(), "filed to open file"); std::vector inputs = ctx_->graph_inputs(); size_t inputs_num = inputs.size(); - for (size_t i = 0; i < inputs_num; ++i) { - Tensor *input = inputs[i]; - std::vector shape = input->shape(); - ofs << " // model's input_shape is [ "; - for (int sh : shape) { - ofs << sh << ", "; - } - ofs << "];\n"; - } - ofs << " void *inputs_binbuf[" << inputs_num << "];\n"; - ofs << " int inputs_size[" << inputs_num << "] = {"; - for (size_t i = 0; i < inputs_num; ++i) { - Tensor *input = inputs[i]; - ofs << input->Size() << ", "; - } - ofs << "};\n"; - ofs << " int ret = ReadInputsFile(argv[1], inputs_binbuf, inputs_size, " << inputs_num - << ");\n" - " if (ret != RET_OK) {\n" - " MICRO_ERROR(\"read inputs file failed\");\n" - " return RET_ERROR;\n" - " }\n"; - ofs << " ret = " << config_->module_name() << "_SetInputs((const void **)inputs_binbuf, " << inputs_num - << ");\n" - " if (ret != RET_OK) {\n" - " MICRO_ERROR(\"set inputs failed\");\n" - " return RET_ERROR;\n" - " }\n"; - ofs << " unsigned int total_buffer_size = " << config_->module_name() << "_GetBufferSize();\n"; - ofs << " void *buffer = malloc(total_buffer_size);\n"; - ofs << " if (buffer == NULL ){\n" - " MICRO_ERROR(\"malloc memory buffer failed\");\n" - " return RET_ERROR;\n" - " }\n"; - ofs << " ret = " << config_->module_name() - << "_SetBuffer(buffer);\n" - " if (ret != RET_OK) {\n" - " MICRO_ERROR(\"set inputs failed\");\n" - " return RET_ERROR;" - " }\n"; + CodeBenchmarkHeader(ofs, net_inc_hfile_); + CodeBenchmarkUsage(ofs); + CodeBenchmarkWarmup(ofs, config_->module_name()); + + CodeBenchmarkSetInputs(ofs, config_->module_name(), ctx_); + CodeBenchmarkSetBuffer(ofs, config_->module_name()); if (config_->is_weight_file()) { - ofs << " int weightSize = 0;\n"; - ofs << " void *weightBuffer = ReadInputData(argv[2], &weightSize); \n"; - ofs << " if(" << config_->module_name() << "_Init(weightBuffer, weightSize) != RET_OK) {\n"; - ofs << " printf(\"model init failed\");\n"; - ofs << " " << config_->module_name() << "_FreeResource();\n"; - ofs << " return RET_ERROR;\n"; - ofs << " }\n"; - ofs << " free(weightBuffer);\n"; - ofs << " weightBuffer = NULL;\n"; + CodeBenchmarkInitWeight(ofs, config_->module_name()); } - if (config_->code_mode() == CodeMode::Code_Android) { - ofs << " int thread_num = 4;\n" - " BindMode bind_mode = NO_BIND_MODE;\n" - " if (argc >= 6) {\n" - " thread_num = atoi(argv[4]);\n" - " bind_mode = atoi(argv[5]);\n" - " }\n" - " ret = ConfigThreadPool(THREAD_POOL_DEFAULT, thread_num, bind_mode);\n" - " if (ret != 0) {\n" - " printf(\"create thread pool failed\");\n" - " }\n"; + CodeBenchmarkConfigThread(ofs); } - ofs << " if (argc >= 4) {\n" - << " " << config_->module_name() << "_WarmUp();\n" - << " uint64_t timeAvg = 0;\n" - << " int loop_count = atoi(argv[3]);\n" - << " printf(\"\\n### begin to run %d\", loop_count);\n" - << " for (int i = 0; i < loop_count; i++) {\n" - << " uint64_t runBegin = GetTimeUs();\n" - << " " << config_->module_name() << "_Inference();\n" - << " uint64_t runEnd = GetTimeUs();\n" - << " uint64_t time = runEnd - runBegin;\n" - << " timeAvg += time;\n" - << " }\n" - << " float cunCost = (float)timeAvg / 1000.0f;\n" - << " printf(\"\\n###Run over, total time:\\t %5.5f ms.\\n\", cunCost);\n" - << " printf(\"\\n###Run over, predict per time:\\t %5.5f ms.\\n\", cunCost / loop_count);\n" - << " }\n"; - ofs << " " << config_->module_name() << "_Inference();\n"; - CodeTestRelevantTile(ofs); + CodeBenchmarkInference(ofs, config_->module_name()); + CodeBenchmarkPrintOutputs(ofs, config_->module_name()); + + CodeBenchmarkFreeResourse(ofs, config_->module_name(), inputs_num); ofs.close(); return RET_OK; } diff --git a/mindspore/lite/micro/coder/generator/inference/inference_generator.h b/mindspore/lite/micro/coder/generator/inference/inference_generator.h index 7da6650f3c..60f8092a93 100644 --- a/mindspore/lite/micro/coder/generator/inference/inference_generator.h +++ b/mindspore/lite/micro/coder/generator/inference/inference_generator.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef MINDSPORE_MICRO_CODER_ANDROID_GENERATOR_H_ -#define MINDSPORE_MICRO_CODER_ANDROID_GENERATOR_H_ +#ifndef MINDSPORE_LITE_MICRO_CODER_GENERATOR_INFERENCE_GENERATOR_H_ +#define MINDSPORE_LITE_MICRO_CODER_GENERATOR_INFERENCE_GENERATOR_H_ #include #include @@ -27,15 +27,11 @@ class InferenceGenerator : public Generator { explicit InferenceGenerator(std::unique_ptr ctx) : Generator(std::move(ctx)) {} ~InferenceGenerator() override = default; - protected: + private: int CodeNetHFile() override; int CodeNetCFile() override; - int CodeTestFile() override; - - private: - void CodeTestRelevantHeader(std::ofstream &code_test_ofs); - void CodeTestRelevantTile(std::ofstream &code_test_ofs); + int CodeBenchmarkFile() override; }; } // namespace mindspore::lite::micro -#endif // MINDSPORE_MICRO_CODER_ANDROID_GENERATOR_H_ +#endif // MINDSPORE_LITE_MICRO_CODER_GENERATOR_INFERENCE_GENERATOR_H_ diff --git a/mindspore/lite/micro/coder/generator/utils/generator_utils.cc b/mindspore/lite/micro/coder/generator/utils/generator_utils.cc deleted file mode 100644 index 561720d38a..0000000000 --- a/mindspore/lite/micro/coder/generator/utils/generator_utils.cc +++ /dev/null @@ -1,172 +0,0 @@ -/** - * 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/utils/generator_utils.h" -#include -#include -#include -#include -#include -#include "include/errorcode.h" -#include "coder/log.h" -#include "coder/utils/print_utils.h" -#include "src/common/file_utils.h" - -namespace mindspore::lite::micro { - -int WriteContentToFile(const std::string &file, const std::string &content) { - std::ofstream of(file); - if (of.bad()) { - MS_LOG(ERROR) << "open file error " << file.c_str(); - return RET_ERROR; - } - MS_LOG(INFO) << "write " << file.c_str(); - of << content; - of.close(); - return RET_OK; -} - -void CodeReadModelParams(const std::map &saved_weights, - const std::map &tensors_map, std::ofstream &ofs) { - ofs << "\n\tstruct ModelParameter {\n" - << "\t\tvoid *addr;\n" - << "\t\tsize_t size;\n" - << "\t\tsize_t offset;\n" - << "\t};\n"; - - size_t params_num = 0; - size_t offset = 0; - ofs << "\n\tstruct ModelParameter model_params[] = {\n"; - for (const auto &item : saved_weights) { - std::string name = item.first; - Tensor *tensor = item.second; - if (tensor->category() == Tensor::Category::CONST_TENSOR) { - auto iter = std::find_if(tensors_map.begin(), tensors_map.end(), - [&tensor](const std::pair &t) { return t.first == tensor; }); - if (iter != tensors_map.end()) { - ofs << "\t\t{" << name << ", " << tensor->Size() << ", " << offset << "},\n"; - params_num++; - } - offset += tensor->Size(); - } - } - ofs << "\t};\n"; - - offset = 0; - for (const auto &item : saved_weights) { - std::string name = item.first; - Tensor *tensor = item.second; - if (tensor->category() == Tensor::Category::CONST_TENSOR) { - auto iter = std::find_if(tensors_map.begin(), tensors_map.end(), - [&tensor](const std::pair &t) { return t.first == tensor; }); - if (iter == tensors_map.end()) { - TypeId data_type = tensor->data_type(); - ofs << "\t" << GetTensorDataType(data_type) << "*" << name << " = (weight_buffer + " << offset << ");\n"; - } - offset += tensor->Size(); - } - } - ofs << "\n"; - - ofs << "\tfor(int i = 0; i < " << params_num << "; ++i) {\n" - << "\t\tif (model_params[i].offset + model_params[i].size > weight_size) {\n" - "\t\t\tMICRO_ERROR(\"buffer is invalid, size: %d, offset: %lu\", weight_size, model_params[i].offset);\n" - "\t\t\treturn RET_ERROR;\n" - "\t\t}\n" - << "\t\tmemcpy(model_params[i].addr, (weight_buffer + model_params[i].offset), model_params[i].size);\n" - << "\t}\n"; -} - -int SaveDataToNet(const std::map &tensors_map, const std::string &net_file) { - std::ofstream out(net_file, std::ios::out | std::ios::trunc | std::ios::binary); - MS_CHECK_TRUE(out.is_open(), "net file open failed!"); - for (auto &item : tensors_map) { - std::string name = item.first; - Tensor *tensor = item.second; - if (tensor->category() == Tensor::Category::CONST_TENSOR) { - if (tensor->data_c() == nullptr) { - continue; - } - out.write(reinterpret_cast(tensor->data_c()), tensor->Size()); - } - } - out.close(); - return RET_OK; -} - -void CodeModelParamsDefine(const std::map &address_map, std::ofstream &hfile, - std::ofstream &cfile) { - for (auto &item : address_map) { - std::string name = item.first; - Tensor *tensor = item.second; - if (tensor->data_c() == nullptr) { - continue; - } - if (tensor->category() == Tensor::Category::CONST_TENSOR) { - PrintTensorForNet(tensor, cfile, hfile, name); - } else if (tensor->category() == Tensor::Category::VAR) { - hfile << "extern " << GetTensorDataType(tensor->data_type()) << " *" << name << ";\n"; - cfile << GetTensorDataType(tensor->data_type()) << "*" << name << " = NULL;\n"; - } - } - cfile << "\n"; -} - -void CodeModelParamsDefineAndData(const std::map &address_map, std::ofstream &hfile, - std::ofstream &cfile) { - for (auto &item : address_map) { - std::string name = item.first; - Tensor *tensor = item.second; - if (tensor->category() == Tensor::Category::CONST_TENSOR) { - if (tensor->data_c() == nullptr) { - continue; - } - PrintTensor(tensor, cfile, hfile, name); - } - } -} - -int PrintMicroTensors(std::ofstream &ofs, std::vector tensors, const std::string &name, - const std::map &tensors_map) { - for (size_t index = 0; index < tensors.size(); ++index) { - Tensor *tensor = tensors[index]; - auto item = tensors_map.find(tensor); - if (item == tensors_map.end()) { - MS_LOG(ERROR) << "nonexistent tensor"; - return RET_ERROR; - } - ofs << " static int dim[] = {"; - for (size_t i = 0; i < tensor->shape().size(); ++i) { - ofs << tensor->shape()[i] << ", "; - } - ofs << "};\n"; - ofs << " " << name << "[" << index << "].ndim = " << tensor->shape().size() << ";\n"; - ofs << " " << name << "[" << index << "].dim = dim;\n"; - ofs << " " << name << "[" << index << "].type = " << GetMicroTensorDataType(tensor->data_type()) << ";\n"; - ofs << " " << name << "[" << index << "].format = " << std::to_string(tensor->format()) << ";\n"; - ofs << " " << name << "[" << index << "].data =" << item->second << ";\n"; - } - return RET_OK; -} - -void IncludeCmsisDirectories(std::ofstream &ofs) { - ofs << "include_directories(${OP_HEADER_PATH}/cmsis)\n"; - ofs << "include_directories(${OP_HEADER_PATH}/cmsis/CMSIS/NN/Include)\n"; - ofs << "include_directories(${OP_HEADER_PATH}/cmsis/CMSIS/DSP/Include)\n"; - ofs << "include_directories(${OP_HEADER_PATH}/cmsis/CMSIS/Core/Include)\n"; -} - -} // namespace mindspore::lite::micro diff --git a/mindspore/lite/micro/coder/generator/utils/generator_utils.h b/mindspore/lite/micro/coder/generator/utils/generator_utils.h deleted file mode 100644 index 77c824dd76..0000000000 --- a/mindspore/lite/micro/coder/generator/utils/generator_utils.h +++ /dev/null @@ -1,47 +0,0 @@ -/** - * 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_MICRO_CODER_GENERATOR_GENERATOR_UTILS_H_ -#define MINDSPORE_MICRO_CODER_GENERATOR_GENERATOR_UTILS_H_ - -#include -#include -#include -#include "src/tensor.h" - -namespace mindspore::lite::micro { - -int WriteContentToFile(const std::string &file, const std::string &content); - -void CodeReadModelParams(const std::map &saved_weights, - const std::map &tensors_map, std::ofstream &ofs); - -int SaveDataToNet(const std::map &tensors_map, const std::string &net_file); - -void CodeModelParamsDefine(const std::map &address_map, std::ofstream &hfile, - std::ofstream &cfile); - -void CodeModelParamsDefineAndData(const std::map &address_map, std::ofstream &hfile, - std::ofstream &cfile); - -int PrintMicroTensors(std::ofstream &ofs, std::vector tensors, const std::string &name, - const std::map &tensors_map); - -void IncludeCmsisDirectories(std::ofstream &ofs); - -} // namespace mindspore::lite::micro - -#endif // MINDSPORE_MICRO_CODER_GENERATOR_GENERATOR_UTILS_H_ diff --git a/mindspore/lite/micro/coder/coder_graph.cc b/mindspore/lite/micro/coder/graph.cc similarity index 98% rename from mindspore/lite/micro/coder/coder_graph.cc rename to mindspore/lite/micro/coder/graph.cc index a799f9f826..f2cc821c4b 100644 --- a/mindspore/lite/micro/coder/coder_graph.cc +++ b/mindspore/lite/micro/coder/graph.cc @@ -14,7 +14,7 @@ * limitations under the License. */ -#include "micro/coder/coder_graph.h" +#include "micro/coder/graph.h" #include #include #include diff --git a/mindspore/lite/micro/coder/coder_graph.h b/mindspore/lite/micro/coder/graph.h similarity index 94% rename from mindspore/lite/micro/coder/coder_graph.h rename to mindspore/lite/micro/coder/graph.h index c30afad0db..e583624102 100644 --- a/mindspore/lite/micro/coder/coder_graph.h +++ b/mindspore/lite/micro/coder/graph.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef MICRO_CODER_GRAPH_NODE_H_ -#define MICRO_CODER_GRAPH_NODE_H_ +#ifndef MINDSPORE_LITE_MICRO_CODER_GRAPH_H_ +#define MINDSPORE_LITE_MICRO_CODER_GRAPH_H_ #include #include @@ -28,6 +28,7 @@ #include "schema/inner/model_generated.h" #include "src/common/graph_util.h" #include "src/tensor.h" + namespace mindspore::lite::micro { class CoderGraph { public: @@ -80,4 +81,4 @@ class CoderGraph { Model *model_{nullptr}; }; } // namespace mindspore::lite::micro -#endif // MICRO_CODER_GRAPH_NODE_H_ +#endif // MINDSPORE_LITE_MICRO_CODER_GRAPH_H_ diff --git a/mindspore/lite/micro/coder/log.h b/mindspore/lite/micro/coder/log.h index cc9ca9e2b0..db94f7ec63 100644 --- a/mindspore/lite/micro/coder/log.h +++ b/mindspore/lite/micro/coder/log.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef MICRO_LOG_H_ -#define MICRO_LOG_H_ +#ifndef MINDSPORE_LITE_MICRO_CODER_LOG_H_ +#define MINDSPORE_LITE_MICRO_CODER_LOG_H_ #include "src/common/log_adapter.h" #include "include/errorcode.h" @@ -86,6 +86,23 @@ } \ } while (0) +#define MS_CHECK_TRUE_WITH_EXE(code, msg, FUNC) \ + do { \ + if (!(code)) { \ + MS_LOG(ERROR) << msg; \ + FUNC; \ + return mindspore::lite::RET_ERROR; \ + } \ + } while (0) + +#define MS_CHECK_TRUE_WITHOUT_RET(code, msg) \ + do { \ + if (!(code)) { \ + MS_LOG(ERROR) << msg; \ + return; \ + } \ + } while (0) + #define MS_CHECK_TRUE_RET_NULL(code, msg) \ do { \ if (!(code)) { \ @@ -102,4 +119,4 @@ } \ } while (0) -#endif // MICRO_LOG_H_ +#endif // MINDSPORE_LITE_MICRO_CODER_LOG_H_ diff --git a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/add_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/add_int8_coder.cc index da34f00c61..e8683ff368 100644 --- a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/add_int8_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/add_int8_coder.cc @@ -14,14 +14,14 @@ * limitations under the License. */ -#include "micro/coder/opcoders/cmsis-nn/int8/add_int8_coder.h" +#include "coder/opcoders/cmsis-nn/int8/add_int8_coder.h" #include #include #include "micro/coder/opcoders/serializers/serializer.h" #include "nnacl/arithmetic.h" #include "nnacl/int8/quantize.h" -#include "micro/coder/opcoders/file_collector.h" -#include "micro/coder/log.h" +#include "coder/opcoders/file_collector.h" +#include "coder/log.h" using mindspore::schema::PrimitiveType_Add; diff --git a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/add_int8_coder.h b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/add_int8_coder.h index c68c6068da..5fc681725f 100644 --- a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/add_int8_coder.h +++ b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/add_int8_coder.h @@ -18,7 +18,7 @@ #define MINDSPORE_LITE_MICRO_CODER_OPCODERS_CMSIS_NN_ADD_INT8_CODER_H_ #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" namespace mindspore::lite::micro::cmsis { diff --git a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/conv2d_base_coder.cc b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/conv2d_base_coder.cc index 15b45ea1f1..f1e95c8ee2 100644 --- a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/conv2d_base_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/conv2d_base_coder.cc @@ -14,7 +14,7 @@ * limitations under the License. */ -#include "micro/coder/opcoders/cmsis-nn/int8/conv2d_base_coder.h" +#include "coder/opcoders/cmsis-nn/int8/conv2d_base_coder.h" #include "nnacl/int8/quantize.h" namespace mindspore::lite::micro::cmsis { diff --git a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/conv2d_base_coder.h b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/conv2d_base_coder.h index b5db5a89c8..588d50b32b 100644 --- a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/conv2d_base_coder.h +++ b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/conv2d_base_coder.h @@ -19,7 +19,7 @@ #include #include -#include "micro/coder/opcoders/base/conv2d_base_coder.h" +#include "coder/opcoders/base/conv2d_base_coder.h" #include "nnacl/conv_parameter.h" namespace mindspore::lite::micro::cmsis { diff --git a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/conv2d_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/conv2d_int8_coder.cc index 36cda8ce96..2e9ff730b9 100644 --- a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/conv2d_int8_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/conv2d_int8_coder.cc @@ -14,13 +14,13 @@ * limitations under the License. */ -#include "micro/coder/opcoders/cmsis-nn/int8/conv2d_int8_coder.h" +#include "coder/opcoders/cmsis-nn/int8/conv2d_int8_coder.h" #include #include #include -#include "micro/coder/opcoders/cmsis-nn/int8/dwconv_int8_coder.h" -#include "micro/coder/opcoders/serializers/serializer.h" -#include "micro/coder/opcoders/file_collector.h" +#include "coder/opcoders/cmsis-nn/int8/dwconv_int8_coder.h" +#include "coder/opcoders/serializers/serializer.h" +#include "coder/opcoders/file_collector.h" using mindspore::schema::PrimitiveType_Conv2D; diff --git a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/conv2d_int8_coder.h b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/conv2d_int8_coder.h index 500eb27858..8d6aa951c3 100644 --- a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/conv2d_int8_coder.h +++ b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/conv2d_int8_coder.h @@ -19,7 +19,7 @@ #include #include -#include "micro/coder/opcoders/cmsis-nn/int8/conv2d_base_coder.h" +#include "coder/opcoders/cmsis-nn/int8/conv2d_base_coder.h" #include "nnacl/conv_parameter.h" namespace mindspore::lite::micro::cmsis { diff --git a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/dwconv_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/dwconv_int8_coder.cc index e70ab1b808..04322376fc 100644 --- a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/dwconv_int8_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/dwconv_int8_coder.cc @@ -14,11 +14,11 @@ * limitations under the License. */ -#include "micro/coder/opcoders/cmsis-nn/int8/dwconv_int8_coder.h" +#include "coder/opcoders/cmsis-nn/int8/dwconv_int8_coder.h" #include -#include "micro/coder/opcoders/serializers/serializer.h" -#include "micro/coder/opcoders/file_collector.h" -#include "micro/coder/log.h" +#include "coder/opcoders/serializers/serializer.h" +#include "coder/opcoders/file_collector.h" +#include "coder/log.h" using mindspore::schema::PrimitiveType_DepthwiseConv2D; diff --git a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/dwconv_int8_coder.h b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/dwconv_int8_coder.h index fc2c9bc25e..7bfd7fa0d4 100644 --- a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/dwconv_int8_coder.h +++ b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/dwconv_int8_coder.h @@ -18,7 +18,7 @@ #define MINDSPORE_LITE_MICRO_CODER_CMSIS_NN_DWCONV_INT8_CODER_H_ #include -#include "micro/coder/opcoders/cmsis-nn/int8/conv2d_base_coder.h" +#include "coder/opcoders/cmsis-nn/int8/conv2d_base_coder.h" #include "src/runtime/kernel/arm/int8/convolution_depthwise_int8.h" namespace mindspore::lite::micro::cmsis { diff --git a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/fullconnection_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/fullconnection_int8_coder.cc index 0efaddd4e6..9b640eae2e 100644 --- a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/fullconnection_int8_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/fullconnection_int8_coder.cc @@ -14,9 +14,9 @@ * limitations under the License. */ -#include "micro/coder/opcoders/cmsis-nn/int8/fullconnection_int8_coder.h" -#include "micro/coder/opcoders/serializers/serializer.h" -#include "micro/coder/opcoders/file_collector.h" +#include "coder/opcoders/cmsis-nn/int8/fullconnection_int8_coder.h" +#include "coder/opcoders/serializers/serializer.h" +#include "coder/opcoders/file_collector.h" using mindspore::schema::PrimitiveType_FullConnection; diff --git a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/mul_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/mul_int8_coder.cc index 12823778b8..f999aa4f95 100644 --- a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/mul_int8_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/mul_int8_coder.cc @@ -14,11 +14,11 @@ * limitations under the License. */ -#include "micro/coder/opcoders/cmsis-nn/int8/mul_int8_coder.h" +#include "coder/opcoders/cmsis-nn/int8/mul_int8_coder.h" #include -#include "micro/coder/opcoders/serializers/serializer.h" +#include "coder/opcoders/serializers/serializer.h" #include "nnacl/int8/quantize.h" -#include "micro/coder/opcoders/file_collector.h" +#include "coder/opcoders/file_collector.h" using mindspore::schema::PrimitiveType_Mul; diff --git a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/mul_int8_coder.h b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/mul_int8_coder.h index 018be41e6e..5e9f8cd41e 100644 --- a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/mul_int8_coder.h +++ b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/mul_int8_coder.h @@ -18,7 +18,7 @@ #define MINDSPORE_LITE_MICRO_CODER_OPCODERS_CMSIS_NN_MUL_INT8_CODER_H_ #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" namespace mindspore::lite::micro::cmsis { class MulInt8Coder final : public OperatorCoder { diff --git a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/pooling_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/pooling_int8_coder.cc index 59559ee3a5..342841ab00 100644 --- a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/pooling_int8_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/pooling_int8_coder.cc @@ -16,9 +16,9 @@ #include #include -#include "micro/coder/opcoders/cmsis-nn/int8/pooling_int8_coder.h" -#include "micro/coder/opcoders/serializers/serializer.h" -#include "micro/coder/opcoders/file_collector.h" +#include "coder/opcoders/cmsis-nn/int8/pooling_int8_coder.h" +#include "coder/opcoders/serializers/serializer.h" +#include "coder/opcoders/file_collector.h" using mindspore::schema::PrimitiveType_Pooling; diff --git a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/pooling_int8_coder.h b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/pooling_int8_coder.h index 0ca4232cb2..f955cb0358 100644 --- a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/pooling_int8_coder.h +++ b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/pooling_int8_coder.h @@ -20,7 +20,7 @@ #include #include #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" #include "nnacl/int8/pooling_int8.h" namespace mindspore::lite::micro::cmsis { diff --git a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/reshape_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/reshape_int8_coder.cc index a4fe626e6f..2d1e427143 100644 --- a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/reshape_int8_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/reshape_int8_coder.cc @@ -14,10 +14,10 @@ * limitations under the License. */ -#include "micro/coder/opcoders/cmsis-nn/int8/reshape_int8_coder.h" +#include "coder/opcoders/cmsis-nn/int8/reshape_int8_coder.h" #include #include -#include "micro/coder/opcoders/serializers/serializer.h" +#include "coder/opcoders/serializers/serializer.h" using mindspore::schema::PrimitiveType_Reshape; diff --git a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/reshape_int8_coder.h b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/reshape_int8_coder.h index bec4748662..714daf2a9f 100644 --- a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/reshape_int8_coder.h +++ b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/reshape_int8_coder.h @@ -18,7 +18,7 @@ #define MINDSPORE_LITE_MICRO_CODER_OPCODERS_CMSIS_NN_RESHAPE_INT8_CODER_H_ #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" namespace mindspore::lite::micro::cmsis { class ReshapeInt8Coder final : public OperatorCoder { diff --git a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/softmax_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/softmax_int8_coder.cc index 0f3e6e3c4b..2083945a6d 100644 --- a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/softmax_int8_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/softmax_int8_coder.cc @@ -14,10 +14,10 @@ * limitations under the License. */ -#include "micro/coder/opcoders/cmsis-nn/int8/softmax_int8_coder.h" +#include "coder/opcoders/cmsis-nn/int8/softmax_int8_coder.h" #include -#include "micro/coder/opcoders/serializers/serializer.h" -#include "micro/coder/opcoders/file_collector.h" +#include "coder/opcoders/serializers/serializer.h" +#include "coder/opcoders/file_collector.h" using mindspore::schema::PrimitiveType_SoftMax; namespace mindspore::lite::micro::cmsis { diff --git a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/softmax_int8_coder.h b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/softmax_int8_coder.h index 6d3b62982e..c9e502c753 100644 --- a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/softmax_int8_coder.h +++ b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/softmax_int8_coder.h @@ -20,7 +20,7 @@ #include #include #include -#include "micro/coder/opcoders/base/softmax_base_coder.h" +#include "coder/opcoders/base/softmax_base_coder.h" namespace mindspore::lite::micro::cmsis { diff --git a/mindspore/lite/micro/coder/opcoders/file_collector.h b/mindspore/lite/micro/coder/opcoders/file_collector.h index ea50e7d9ad..03a036188a 100644 --- a/mindspore/lite/micro/coder/opcoders/file_collector.h +++ b/mindspore/lite/micro/coder/opcoders/file_collector.h @@ -19,7 +19,7 @@ #include #include -#include "micro/coder/coder_context.h" +#include "coder/context.h" namespace mindspore::lite::micro { void Collect(CoderContext *const ctx, const std::vector &headers, diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/activation_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/activation_fp32_coder.h index 06a6694fcb..ae38c3bca9 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/activation_fp32_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/activation_fp32_coder.h @@ -14,11 +14,11 @@ * limitations under the License. */ -#ifndef MICRO_CODER_OPCODERS_FP32_ACTIVATIONFP32_CODER_H_ -#define MICRO_CODER_OPCODERS_FP32_ACTIVATIONFP32_CODER_H_ +#ifndef MINDSPORE_LITE_MICRO_CODER_OPCODERS_FP32_ACTIVATIONFP32_CODER_H_ +#define MINDSPORE_LITE_MICRO_CODER_OPCODERS_FP32_ACTIVATIONFP32_CODER_H_ #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" namespace mindspore::lite::micro::nnacl { diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/addn_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/addn_fp32_coder.h index a6933909f6..6441d1c8cf 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/addn_fp32_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/addn_fp32_coder.h @@ -17,7 +17,7 @@ #ifndef MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_ADDN_FP32_CODER_H_ #define MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_ADDN_FP32_CODER_H_ #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" namespace mindspore::lite::micro::nnacl { class AddNFP32Coder final : public OperatorCoder { diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/arithmetic_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/arithmetic_fp32_coder.cc index a337d128da..1c70ebd6f4 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/arithmetic_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/arithmetic_fp32_coder.cc @@ -13,13 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/fp32/arithmetic_fp32_coder.h" +#include "coder/opcoders/nnacl/fp32/arithmetic_fp32_coder.h" #include #include #include -#include "micro/coder/opcoders/file_collector.h" +#include "coder/opcoders/file_collector.h" #include "nnacl/fp32/arithmetic_fp32.h" -#include "micro/coder/log.h" +#include "coder/log.h" namespace mindspore::lite::micro::nnacl { diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/arithmetic_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/arithmetic_fp32_coder.h index 7eae448f39..7c2ed43242 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/arithmetic_fp32_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/arithmetic_fp32_coder.h @@ -14,14 +14,14 @@ * limitations under the License. */ -#ifndef MICRO_CODER_OPCODERS_FP32_ARITHMETIC_FP32_CODER_H_ -#define MICRO_CODER_OPCODERS_FP32_ARITHMETIC_FP32_CODER_H_ +#ifndef MINDSPORE_LITE_MICRO_CODER_OPCODERS_FP32_ARITHMETIC_FP32_CODER_H_ +#define MINDSPORE_LITE_MICRO_CODER_OPCODERS_FP32_ARITHMETIC_FP32_CODER_H_ #include #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" #include "nnacl/fp32/arithmetic_fp32.h" -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" #define DEFAULT_ARITHMETIC_NDIMS 10 namespace mindspore::lite::micro::nnacl { @@ -106,4 +106,4 @@ class ArithmeticFP32Coder final : public OperatorCoder { LiteDataType data_type_{kDataTypeFloat}; }; } // namespace mindspore::lite::micro::nnacl -#endif // MICRO_CODER_OPCODERS_FP32_ARITHMETIC_FP32_CODER_H_ +#endif // MINDSPORE_LITE_MICRO_CODER_OPCODERS_FP32_ARITHMETIC_FP32_CODER_H_ diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/arithmetic_self_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/arithmetic_self_fp32_coder.cc index 596bcd30be..6c58e5e357 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/arithmetic_self_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/arithmetic_self_fp32_coder.cc @@ -14,12 +14,12 @@ * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/fp32/arithmetic_self_fp32_coder.h" +#include "coder/opcoders/nnacl/fp32/arithmetic_self_fp32_coder.h" #include #include #include "nnacl/fp32/arithmetic_fp32.h" -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" -#include "micro/coder/opcoders/file_collector.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" +#include "coder/opcoders/file_collector.h" namespace mindspore::lite::micro::nnacl { diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/arithmetic_self_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/arithmetic_self_fp32_coder.h index 2230232976..239173907e 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/arithmetic_self_fp32_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/arithmetic_self_fp32_coder.h @@ -14,12 +14,12 @@ * limitations under the License. */ -#ifndef MICRO_CODER_OPCODERS_FP32_ARITHMETIC_SELF_FP32_CODER_H_ -#define MICRO_CODER_OPCODERS_FP32_ARITHMETIC_SELF_FP32_CODER_H_ +#ifndef MINDSPORE_LITE_MICRO_CODER_OPCODERS_FP32_ARITHMETIC_SELF_FP32_CODER_H_ +#define MINDSPORE_LITE_MICRO_CODER_OPCODERS_FP32_ARITHMETIC_SELF_FP32_CODER_H_ #include #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" #include "nnacl/fp32/arithmetic_self_fp32.h" #include "nnacl/arithmetic_self_parameter.h" @@ -106,4 +106,4 @@ class ArithmeticSelfFP32Coder final : public OperatorCoder { std::string arithmetic_self_run_; }; } // namespace mindspore::lite::micro::nnacl -#endif // MICRO_CODER_OPCODERS_FP32_ARITHMETIC_SELF_FP32_CODER_H_ +#endif // MINDSPORE_LITE_MICRO_CODER_OPCODERS_FP32_ARITHMETIC_SELF_FP32_CODER_H_ diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/assign_add_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/assign_add_fp32_coder.h index fd064e5288..e817e7db2d 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/assign_add_fp32_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/assign_add_fp32_coder.h @@ -18,7 +18,7 @@ #define MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_ASSIGN_ADD_FP32_CODER_H_ #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" #include "nnacl/base/tile_base.h" namespace mindspore::lite::micro::nnacl { diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/batchnorm_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/batchnorm_fp32_coder.cc index d00b31a368..4ddf38d561 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/batchnorm_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/batchnorm_fp32_coder.cc @@ -13,14 +13,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/fp32/batchnorm_fp32_coder.h" +#include "coder/opcoders/nnacl/fp32/batchnorm_fp32_coder.h" #include #include #include "nnacl/fp32/batchnorm_fp32.h" #include "src/ops/batch_norm.h" #include "nnacl/op_base.h" -#include "micro/coder/opcoders/file_collector.h" -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" +#include "coder/opcoders/file_collector.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" using mindspore::schema::PrimitiveType_BatchNorm; diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/batchnorm_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/batchnorm_fp32_coder.h index bf0890f1ad..9b77799a04 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/batchnorm_fp32_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/batchnorm_fp32_coder.h @@ -14,11 +14,11 @@ * limitations under the License. */ -#ifndef MICRO_CODER_OPCODERS_FP32_BATCHNORM_FP32_CODER_H_ -#define MICRO_CODER_OPCODERS_FP32_BATCHNORM_FP32_CODER_H_ +#ifndef MINDSPORE_LITE_MICRO_CODER_OPCODERS_FP32_BATCHNORM_FP32_CODER_H_ +#define MINDSPORE_LITE_MICRO_CODER_OPCODERS_FP32_BATCHNORM_FP32_CODER_H_ #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" namespace mindspore::lite::micro::nnacl { @@ -40,4 +40,4 @@ class BatchnormFP32Coder final : public OperatorCoder { } // namespace mindspore::lite::micro::nnacl -#endif // MICRO_CODER_OPCODERS_FP32_CODER_H_ +#endif // MINDSPORE_LITE_MICRO_CODER_OPCODERS_FP32_CODER_H_ diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/concat_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/concat_fp32_coder.cc index be0b360dc4..ab6f62bf9a 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/concat_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/concat_fp32_coder.cc @@ -13,11 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/fp32/concat_fp32_coder.h" +#include "coder/opcoders/nnacl/fp32/concat_fp32_coder.h" #include #include -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" -#include "micro/coder/opcoders/file_collector.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" +#include "coder/opcoders/file_collector.h" using mindspore::schema::PrimitiveType_Concat; diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/concat_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/concat_fp32_coder.h index 494bc9e9fe..302bb0e5dc 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/concat_fp32_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/concat_fp32_coder.h @@ -18,7 +18,7 @@ #define MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_CONCAT_FP32_CODER_H_ #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" #include "nnacl/concat_parameter.h" namespace mindspore::lite::micro::nnacl { diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_depthwise_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_depthwise_fp32_coder.cc new file mode 100644 index 0000000000..a9dd2ea0ae --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_depthwise_fp32_coder.cc @@ -0,0 +1,78 @@ +/** + * Copyright 2020 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 "micro/coder/opcoders/nnacl/fp32/convolution_depthwise_fp32_coder.h" +#include +#include "micro/coder/log.h" +#include "micro/coder/opcoders/file_collector.h" +#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" + +using mindspore::schema::PrimitiveType_DepthwiseConv2D; +namespace mindspore::lite::micro::nnacl { +int ConvolutionDepthwiseFP32Coder::Prepare(CoderContext *const context) { + Conv2DBaseCoder::Init(); + MS_CHECK_RET_CODE(InitWeightBias(), "dwconvolution do init weightbais failed"); + conv_param_->thread_num_ = MSMIN(thread_num_, conv_param_->output_h_); + return RET_OK; +} + +int ConvolutionDepthwiseFP32Coder::InitWeightBias() { + // init weight: o, h, w, i; o == group, i == 1 + auto *origin_weight = reinterpret_cast(filter_tensor_->data_c()); + int channel = filter_tensor_->Batch(); + size_t pack_weight_size = filter_tensor_->Batch() * filter_tensor_->Height() * filter_tensor_->Width(); + size_t packed_weight_data_size = pack_weight_size * sizeof(float); + packed_weight_ = + reinterpret_cast(allocator_->Malloc(kNumberTypeFloat32, packed_weight_data_size, kOfflinePackWeight)); + MS_CHECK_PTR(packed_weight_); + MS_CHECK_RET_CODE(memset_s(packed_weight_, packed_weight_data_size, 0, packed_weight_data_size), + "memset packed weight failed!"); + PackNCHWToNHWCFp32(origin_weight, packed_weight_, 1, filter_tensor_->Height() * filter_tensor_->Width(), channel); + + auto channel_size = static_cast(channel); + auto bias_size = static_cast(channel_size * sizeof(float)); + bias_ = reinterpret_cast(allocator_->Malloc(kNumberTypeFloat32, bias_size, kOfflinePackWeight)); + MS_CHECK_PTR(bias_); + MS_CHECK_RET_CODE(memset_s(bias_, bias_size, 0, bias_size), "memset bias failed!"); + // init bias + if (input_tensors_.size() == kInputSize2) { + auto *ori_bias = reinterpret_cast(bias_tensor_->data_c()); + MS_CHECK_TRUE(bias_tensor_->ElementsNum() > 0, "invalid bias length"); + MS_CHECK_RET_CODE(memcpy_s(bias_, static_cast(bias_tensor_->ElementsNum() * sizeof(float)), ori_bias, + static_cast(bias_tensor_->ElementsNum() * sizeof(float))), + "memcpy_s bias failed!"); + } + return RET_OK; +} + +int ConvolutionDepthwiseFP32Coder::DoCode(CoderContext *const context) { + MS_CHECK_TRUE(conv_param_->input_channel_ == conv_param_->output_channel_, + "Only support input channel equals output channel."); + // generate code .h .c + Collect(context, {"nnacl/fp32/conv_depthwise.h"}, {"conv_depthwise.c"}); + + nnacl::NNaclFp32Serializer code; + // call the op function + code.CodeStruct("conv_parameter", *conv_param_); + int task_id = 0; + code.CodeFunction("ConvDw", output_tensor_, input_tensor_, packed_weight_, bias_, "&conv_parameter", task_id); + context->AppendCode(code.str()); + return RET_OK; +} + +REG_OPERATOR_CODER(kAllTargets, kNumberTypeFloat32, PrimitiveType_DepthwiseConv2D, + CPUOpCoderCreator) +} // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_depthwise_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_depthwise_fp32_coder.h new file mode 100644 index 0000000000..e16a0e3db3 --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_depthwise_fp32_coder.h @@ -0,0 +1,42 @@ +/** + * Copyright 2020 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_CODER_OPCODERS_FP32_CONVOLUTION_DEPTHWISE_FP32_CODER_H_ +#define MINDSPORE_LITE_MICRO_CODER_OPCODERS_FP32_CONVOLUTION_DEPTHWISE_FP32_CODER_H_ + +#include +#include "micro/coder/opcoders/base/conv2d_base_coder.h" +#include "src/runtime/kernel/arm/fp32/convolution_depthwise_fp32.h" + +namespace mindspore::lite::micro::nnacl { +class ConvolutionDepthwiseFP32Coder final : public Conv2DBaseCoder { + public: + ConvolutionDepthwiseFP32Coder(const std::vector &in_tensors, const std::vector &out_tensors, + const Model::Node *node, size_t node_index, Target target) + : Conv2DBaseCoder(in_tensors, out_tensors, node, node_index, target) {} + + ~ConvolutionDepthwiseFP32Coder() override = default; + int Prepare(CoderContext *const context) override; + + int DoCode(CoderContext *const context) override; + + private: + int InitWeightBias(); + float *packed_weight_{nullptr}; + float *bias_{nullptr}; +}; +} // namespace mindspore::lite::micro::nnacl +#endif // MINDSPORE_LITE_MICRO_CODER_OPCODERS_FP32_CONVOLUTION_DEPTHWISE_FP32_CODER_H_ diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_fp32_coder.cc new file mode 100644 index 0000000000..54a6e86efe --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_fp32_coder.cc @@ -0,0 +1,179 @@ +/** + * 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 "micro/coder/opcoders/nnacl/fp32/convolution_fp32_coder.h" +#include +#include +#include +#include "micro/coder/opcoders/nnacl/fp32/convolution_winograd_fp32_coder.h" +#include "nnacl/fp32/winograd_utils.h" +#include "src/ops/populate/populate_register.h" +#include "micro/coder/opcoders/file_collector.h" +#include "micro/coder/log.h" + +using mindspore::schema::PrimitiveType_Conv2D; +namespace mindspore::lite::micro::nnacl { +int ConvolutionFP32Coder::InitTmpBuffer() { + int in_channel = conv_param_->input_channel_; + int uint_size; + if (target_ == kARM32A) { + uint_size = conv_param_->kernel_h_ * conv_param_->kernel_w_ * in_channel * C4NUM * thread_num_; + } else { + uint_size = conv_param_->kernel_h_ * conv_param_->kernel_w_ * in_channel * C12NUM * thread_num_; + } + packed_input_size_ = uint_size * sizeof(float); + packed_input_ = reinterpret_cast(allocator_->Malloc(kNumberTypeFloat32, packed_input_size_, kWorkspace)); + col_major_input_size_ = uint_size * sizeof(float); + col_major_input_ = + reinterpret_cast(allocator_->Malloc(kNumberTypeFloat32, col_major_input_size_, kWorkspace)); + return RET_OK; +} + +int ConvolutionFP32Coder::Prepare(CoderContext *const context) { + int ret = Conv2DBaseCoder::Init(); + MS_CHECK_RET_CODE(ret, "Conv2DBaseCoder::Init() failed."); + ret = InitWeightBias(context); + MS_CHECK_RET_CODE(ret, "Init weight bias failed."); + return Resize(); +} + +int ConvolutionFP32Coder::Resize() { + int ret = Conv2DBaseCoder::CheckResizeValid(); + MS_CHECK_RET_CODE(ret, "Resize is invalid."); + ret = Conv2DBaseCoder::Init(); + MS_CHECK_RET_CODE(ret, "init failed."); + ret = InitTmpBuffer(); + MS_CHECK_RET_CODE(ret, "init tmp buffer failed."); + return RET_OK; +} + +int ConvolutionFP32Coder::InitWeightBias(CoderContext *const context) { + int kernel_h = filter_tensor_->Height(); + int kernel_w = filter_tensor_->Width(); + int in_channel = filter_tensor_->Channel(); + int out_channel = filter_tensor_->Batch(); + conv_param_->input_channel_ = in_channel; + conv_param_->output_channel_ = out_channel; + int kernel_plane = kernel_h * kernel_w; + const int oc_block = C8NUM; + int oc_block_num = UP_DIV(out_channel, C8NUM); + int pack_weight_size = oc_block_num * oc_block * in_channel * kernel_plane; + + auto origin_weight = reinterpret_cast(filter_tensor_->MutableData()); + MS_CHECK_PTR(origin_weight); + packed_weight_ = reinterpret_cast( + allocator_->Malloc(kNumberTypeFloat32, pack_weight_size * sizeof(float), kOnlinePackWeight)); + MS_CHECK_PTR(packed_weight_); + auto out_channel_size = static_cast(out_channel); + + NNaclFp32Serializer code; + code.CodeMallocExpression(packed_weight_, pack_weight_size * sizeof(float)); + code.CodeFunction("memset", packed_weight_, 0, pack_weight_size * sizeof(float)); + code.CodeFunction("RowMajor2Col8Major", filter_tensor_, packed_weight_, out_channel_size, in_channel * kernel_plane); + + auto bias_data_size = static_cast(oc_block_num * oc_block * sizeof(float)); + bias_data_ = reinterpret_cast(allocator_->Malloc(kNumberTypeFloat32, bias_data_size, kOnlinePackWeight)); + MS_CHECK_PTR(bias_data_); + if (input_tensors_.size() == kInputSize2) { + code.CodeMallocExpression(bias_data_, bias_data_size); + code.CodeFunction("memset", bias_data_, 0, bias_data_size); + code.CodeFunction("memcpy", bias_data_, bias_tensor_, out_channel_size * sizeof(float)); + } else { + return RET_ERROR; + } + context->AppendInitCode(code.str()); + return RET_OK; +} + +int ConvolutionFP32Coder::DoCode(CoderContext *const context) { + { + std::vector asmFiles; + if (target_ == kARM32A) { + asmFiles = {"MatmulFp32.S", + "MatmulFp32Opt.S", + "PreSum4x16Int8Peroc.S", + "PreSum4x16Int8Pert.S", + "IndirectGemmInt16to32_8x4.S", + "MatmulInt8.S"}; + } else if (target_ == kARM64) { + asmFiles = {"MatmulFp32.S", "MatmulFp32Opt.S", "PreSum4x16Int8Peroc.S", "MatVecMulFp32.S", + "PreSum4x16Int8Peroc.S", "PreSum4x16Int8Pert.S", "IndirectGemmInt16to32_8x4.S", "MatmulInt8.S"}; + } + Collect(context, + {"nnacl/kernel/fp32/conv_fp32_slim.h", "nnacl/fp32/matmul.h", "nnacl/conv_parameter.h", "nnacl/op_base.h"}, + {"common_func.c", "conv_fp32_slim.c", "matmul.c"}, asmFiles); + } + NNaclFp32Serializer code; + // call the op function + code.CodeFunction("memset", packed_input_, "0", packed_input_size_); + code.CodeFunction("memset", col_major_input_, "0", col_major_input_size_); + code.CodeStruct("conv_parameter", *conv_param_); + int task_id = 0; + code.CodeFunction("ConvFp32Slim", input_tensor_, packed_input_, packed_weight_, bias_data_, col_major_input_, + output_tensor_, task_id, "(ConvParameter *)&conv_parameter"); + + context->AppendCode(code.str()); + return RET_OK; +} + +std::unique_ptr CPUConvolutionFP32CoderCreator(const std::vector &in_tensors, + const std::vector &out_tensors, + const Model::Node *node, size_t node_index, + Target target) { + std::vector inputs = in_tensors; + std::vector outputs = out_tensors; + auto primitive = node->primitive_; + if (!primitive) { + return nullptr; + } + OpParameter *parameter = + PopulateRegistry::GetInstance()->GetParameterCreator((schema::PrimitiveType(primitive->Type())))(primitive); + if (parameter == nullptr) { + MS_LOG(ERROR) << "PopulateParameter return nullptr, type: " + << schema::EnumNamePrimitiveType((schema::PrimitiveType)(primitive->Type())); + return nullptr; + } + auto conv_param = reinterpret_cast(parameter); + bool use_winograd = false; + int out_unit = 0; + int kernel_h = conv_param->kernel_h_; + int kernel_w = conv_param->kernel_w_; + conv_param->input_h_ = inputs.at(kInputIndex)->Height(); + conv_param->input_w_ = inputs.at(kInputIndex)->Width(); + conv_param->input_channel_ = inputs.at(kInputIndex)->Channel(); + conv_param->output_h_ = outputs.at(kOutputIndex)->Height(); + conv_param->output_w_ = outputs.at(kOutputIndex)->Width(); + conv_param->output_channel_ = outputs.at(kOutputIndex)->Channel(); + conv_param->op_parameter_.thread_num_ = 1; + CheckIfUseWinograd(&use_winograd, &out_unit, conv_param); + free(parameter); + // weight de quant + std::unique_ptr coder; + if (kernel_h == 1 && kernel_w == 1) { + MS_LOG(DEBUG) << "create ConvolutionFP32Coder"; + coder = CPUOpCoderCreator(in_tensors, out_tensors, node, node_index, target); + } else if (use_winograd) { + MS_LOG(DEBUG) << "create Conv2DWinogradFP32Coder"; + coder = std::make_unique(in_tensors, out_tensors, node, node_index, target, out_unit); + } else { + MS_LOG(DEBUG) << "create ConvolutionFP32Coder"; + coder = CPUOpCoderCreator(in_tensors, out_tensors, node, node_index, target); + } + return coder; +} + +REG_OPERATOR_CODER(kAllTargets, kNumberTypeFloat32, PrimitiveType_Conv2D, CPUConvolutionFP32CoderCreator) +} // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_fp32_coder.h new file mode 100644 index 0000000000..eaabfb841e --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_fp32_coder.h @@ -0,0 +1,62 @@ +/** + * 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_CODER_OPCODERS_FP32_CONVOLUTION_FP32_CODER_H_ +#define MINDSPORE_LITE_MICRO_CODER_OPCODERS_FP32_CONVOLUTION_FP32_CODER_H_ + +#include +#include +#include "nnacl/conv_parameter.h" +#include "micro/coder/opcoders/base/conv2d_base_coder.h" +#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" + +namespace mindspore::lite::micro::nnacl { +class ConvolutionFP32Coder final : public Conv2DBaseCoder { + public: + ConvolutionFP32Coder(const std::vector &in_tensors, const std::vector &out_tensors, + const Model::Node *node, size_t node_index, Target target) + : Conv2DBaseCoder(in_tensors, out_tensors, node, node_index, target) {} + + int Prepare(CoderContext *const context) override; + + int DoCode(CoderContext *const context) override; + + ~ConvolutionFP32Coder() override = default; + + private: + int InitWeightBias(CoderContext *const context); + + int InitTmpBuffer(); + + int Resize(); + + float *packed_weight_{nullptr}; + + float *bias_data_{nullptr}; + + float *packed_input_{nullptr}; + + size_t packed_input_size_{0}; + + int thread_stride_{0}; + + int thread_count_{0}; + + float *col_major_input_{nullptr}; + size_t col_major_input_size_{0}; +}; +} // namespace mindspore::lite::micro::nnacl +#endif // MINDSPORE_LITE_MICRO_CODER_OPCODERS_FP32_CONVOLUTION_FP32_CODER_H_ diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_winograd_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_winograd_fp32_coder.cc new file mode 100644 index 0000000000..fbf6867252 --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_winograd_fp32_coder.cc @@ -0,0 +1,247 @@ +/** + * 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 "micro/coder/opcoders/nnacl/fp32/convolution_winograd_fp32_coder.h" +#include +#include "nnacl/base/minimal_filtering_generator.h" +#include "micro/coder/log.h" +#include "micro/coder/opcoders/file_collector.h" +#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" + +namespace mindspore::lite::micro::nnacl { +const std::array InputTransFuncList = { + "", "", "", "", "InputTransform4x4Unit", "", "InputTransform6x6Unit", "", "InputTransform8x8Unit"}; + +const std::array OutputTransFuncList4 = {"", "", "OutputTransform4x2Unit", "OutputTransform4x3Unit"}; + +const std::array OutputTransFuncReluList4 = {"", "", "OutputTransform4x2ReluUnit", + "OutputTransform4x3ReluUnit"}; + +const std::array OutputTransFuncRelu6List4 = {"", "", "OutputTransform4x2Relu6Unit", + "OutputTransform4x3Relu6Unit"}; +const std::array OutputTransFuncList6 = { + "", "", "OutputTransform6x2Unit", "OutputTransform6x3Unit", "OutputTransform6x4Unit", "OutputTransform6x5Unit"}; + +const std::array OutputTransFuncReluList6 = {"", + "", + "OutputTransform6x2ReluUnit", + "OutputTransform6x3ReluUnit", + "OutputTransform6x4ReluUnit", + "OutputTransform6x5ReluUnit"}; + +const std::array OutputTransFuncRelu6List6 = {"", + "", + "OutputTransform6x2Relu6Unit", + "OutputTransform6x3Relu6Unit", + "OutputTransform6x4Relu6Unit", + "OutputTransform6x5Relu6Unit"}; + +const std::array OutputTransFuncList8 = {"", + "", + "OutputTransform8x2Unit", + "OutputTransform8x3Unit", + "OutputTransform8x4Unit", + "OutputTransform8x5Unit", + "OutputTransform8x6Unit", + "OutputTransform8x7Unit"}; + +const std::array OutputTransFuncReluList8 = {"", + "", + "OutputTransform8x2ReluUnit", + "OutputTransform8x3ReluUnit", + "OutputTransform8x4ReluUnit", + "OutputTransform8x5ReluUnit", + "OutputTransform8x6ReluUnit", + "OutputTransform8x7ReluUnit"}; +const std::array OutputTransFuncRelu6List8 = {"", + "", + "OutputTransform8x2Relu6Unit", + "OutputTransform8x3Relu6Unit", + "OutputTransform8x4Relu6Unit", + "OutputTransform8x5Relu6Unit", + "OutputTransform8x6Relu6Unit", + "OutputTransform8x7Relu6Unit"}; + +int ConvolutionWinogradFP32Coder::WinogradFilterTransform(const float *weight_data, float *matrix_g, + const float *matrix_gt, int oc_block) { + MS_CHECK_TRUE(oc_block, "Divide by zero!"); + return WinogradWeightTransform(weight_data, trans_weight_, matrix_g, matrix_gt, oc_block, input_unit_, kernel_unit_, + conv_param_->input_channel_, conv_param_->output_channel_, true); +} + +int ConvolutionWinogradFP32Coder::InitTmpBuffer() { + int channel_out = conv_param_->output_channel_; + int oc8 = UP_DIV(channel_out, C8NUM); + int tile_num = C12NUM; + tile_buffer_size_ = thread_num_ * tile_num * input_unit_ * input_unit_ * conv_param_->input_channel_ * sizeof(float); + trans_input_ = reinterpret_cast(allocator_->Malloc(kNumberTypeFloat32, tile_buffer_size_, kWorkspace)); + gemm_out_size_ = thread_num_ * tile_num * input_unit_ * input_unit_ * oc8 * C8NUM * sizeof(float); + gemm_out_ = reinterpret_cast(allocator_->Malloc(kNumberTypeFloat32, gemm_out_size_, kWorkspace)); + tmp_data_size_ = thread_num_ * C4NUM * input_unit_ * input_unit_ * sizeof(float); + tmp_data_ = reinterpret_cast(allocator_->Malloc(kNumberTypeFloat32, tmp_data_size_, kWorkspace)); + col_buffer_size_ = thread_num_ * tile_num * conv_param_->input_channel_ * sizeof(float); + col_buffer_ = reinterpret_cast(allocator_->Malloc(kNumberTypeFloat32, col_buffer_size_, kWorkspace)); + return RET_OK; +} + +int ConvolutionWinogradFP32Coder::ReSize() { + // malloc tmp buffer + int ret = ConfigInputOutput(); + MS_CHECK_RET_CODE(ret, "ConfigInputOutput failed."); + ret = InitTmpBuffer(); + MS_CHECK_RET_CODE(ret, "Init tmp buffer failed."); + return RET_OK; +} + +int ConvolutionWinogradFP32Coder::Prepare(CoderContext *const context) { + int ret = Conv2DBaseCoder::Init(); + MS_CHECK_RET_CODE(ret, "convolution base coder init failed."); + kernel_unit_ = conv_param_->kernel_h_; + input_unit_ = output_unit_ + kernel_unit_ - 1; + conv_param_->input_unit_ = input_unit_; + conv_param_->output_unit_ = output_unit_; + ret = InitWeightBias(); + MS_CHECK_RET_CODE(ret, "Init weight bias failed."); + return ReSize(); +} // namespace micro + +int ConvolutionWinogradFP32Coder::InitWeightBias() { + int in_channel = filter_tensor_->Channel(); + int out_channel = filter_tensor_->Batch(); + MS_CHECK_TRUE(in_channel > 0, "invalid in channel size"); + MS_CHECK_TRUE(out_channel > 0, "invalid out channel size"); + conv_param_->input_channel_ = in_channel; + conv_param_->output_channel_ = out_channel; + + int oc4 = UP_DIV(out_channel, C4NUM); + const int oc_block = C8NUM; + int oc_block_num = UP_DIV(out_channel, C8NUM); + // init weight + int trans_matrix_data_size = input_unit_ * input_unit_ * in_channel * oc_block_num * oc_block; + trans_weight_ = reinterpret_cast( + allocator_->Malloc(kNumberTypeFloat32, trans_matrix_data_size * sizeof(float), kOfflinePackWeight)); + MS_CHECK_PTR(trans_weight_); + int ret = memset_s(trans_weight_, trans_matrix_data_size * sizeof(float), 0, trans_matrix_data_size * sizeof(float)); + MS_CHECK_RET_CODE(ret, "memset_s failed!"); + float matrix_g[64]; + float matrix_gt[64]; + float matrix_a[64]; + float matrix_at[64]; + float matrix_b[64]; + float matrix_bt[64]; + float coef = 1.0f; + if (input_unit_ == 8) { + coef = 0.5f; + } + CookToomFilter(matrix_a, matrix_at, matrix_b, matrix_bt, matrix_g, matrix_gt, coef, output_unit_, kernel_unit_); + + auto out_channel_size = static_cast(out_channel); + auto weight_data = reinterpret_cast(filter_tensor_->MutableData()); + ret = WinogradFilterTransform(weight_data, matrix_g, matrix_gt, oc_block); + MS_CHECK_RET_CODE(ret, "winograd filter transform failed!"); + // init bias + int new_bias_ele_num = oc4 * C4NUM; + auto new_bias_ele_size = static_cast(new_bias_ele_num * sizeof(float)); + new_bias_ = reinterpret_cast(allocator_->Malloc(kNumberTypeFloat32, new_bias_ele_size, kOfflinePackWeight)); + MS_CHECK_PTR(new_bias_); + ret = memset_s(new_bias_, new_bias_ele_size, 0, new_bias_ele_size); + MS_CHECK_RET_CODE(ret, "memset_s failed!"); + if (input_tensors_.size() == kInputSize2) { + auto ori_bias_addr = reinterpret_cast(bias_tensor_->data_c()); + MS_CHECK_RET_CODE(memcpy_s(new_bias_, static_cast(out_channel_size * sizeof(float)), ori_bias_addr, + static_cast(out_channel_size * sizeof(float))), + "memset_s failed!"); + } else { + return RET_ERROR; + } + return RET_OK; +} + +int ConvolutionWinogradFP32Coder::ConfigInputOutput() { + in_func_ = GetInputTransFunc(input_unit_); + MS_CHECK_TRUE(!in_func_.empty(), "Get input_trans_func failed."); + out_func_ = GetOutputTransFunc(input_unit_, output_unit_, conv_param_->act_type_); + MS_CHECK_TRUE(!out_func_.empty(), "Get output_trans_func_ failed."); + return RET_OK; +} + +std::string ConvolutionWinogradFP32Coder::GetInputTransFunc(int input_unit) { + return InputTransFuncList.at(input_unit); +} + +std::string ConvolutionWinogradFP32Coder::GetOutputTransFunc(int input_unit, int output_unit, ActType act_type) { + std::string res; + if (input_unit == 4 && output_unit < 4) { + if (act_type == ActType_Relu) { + return OutputTransFuncReluList4.at(output_unit); + } else if (act_type == ActType_Relu6) { + return OutputTransFuncRelu6List4.at(output_unit); + } else { + return OutputTransFuncList4.at(output_unit); + } + } else if (input_unit == 6 && output_unit < 6) { + if (act_type == ActType_Relu) { + return OutputTransFuncReluList6.at(output_unit); + } else if (act_type == ActType_Relu6) { + return OutputTransFuncRelu6List6.at(output_unit); + } else { + return OutputTransFuncList6.at(output_unit); + } + } else if (input_unit == 8 && output_unit < 8) { + if (act_type == ActType_Relu) { + return OutputTransFuncReluList8.at(output_unit); + } else if (act_type == ActType_Relu6) { + return OutputTransFuncRelu6List8.at(output_unit); + } else { + return OutputTransFuncList8.at(output_unit); + } + } else { + return res; + } +} + +int ConvolutionWinogradFP32Coder::DoCode(CoderContext *const context) { + std::vector asmFiles; + if (target_ == kARM32A) { + asmFiles = { + "MatmulFp32.S", "MatmulFp32Opt.S", "PreSum4x16Int8Peroc.S", "PreSum4x16Int8Pert.S", "IndirectGemmInt16to32_8x4.S", + "MatmulInt8.S"}; + } else if (target_ == kARM64) { + asmFiles = {"MatmulFp32.S", "MatmulFp32Opt.S", "PreSum4x16Int8Peroc.S", "MatVecMulFp32.S", + "PreSum4x16Int8Peroc.S", "PreSum4x16Int8Pert.S", "IndirectGemmInt16to32_8x4.S", "MatmulInt8.S"}; + } + Collect(context, {"nnacl/fp32/conv.h", "nnacl/common_func.h"}, + {"common_func.c", "conv_int8.c", "matmul_int8.c", "pack.c", "conv.c", "winograd_transform.c", + "common_func_fp32.c", "fixed_point.c", "winograd_utils.c", "minimal_filtering_generator.c"}, + asmFiles); + + NNaclFp32Serializer code; + // call the op function + code.CodeFunction("memset", trans_input_, "0", tile_buffer_size_); + code.CodeFunction("memset", gemm_out_, "0", gemm_out_size_); + code.CodeFunction("memset", tmp_data_, "0", tmp_data_size_); + code.CodeFunction("memset", col_buffer_, "0", col_buffer_size_); + code << "\t\tfloat *tmp_buffer_address_list[4] = {" << allocator_->GetRuntimeAddr(trans_input_) << ", " + << allocator_->GetRuntimeAddr(gemm_out_) << ", " << allocator_->GetRuntimeAddr(tmp_data_) << ", " + << allocator_->GetRuntimeAddr(col_buffer_) << "};\n"; + code.CodeStruct("conv_parameter", *conv_param_); + // code operator func + int task_id = 0; + code.CodeFunction("ConvWinogardFp32", input_tensor_, trans_weight_, new_bias_, output_tensor_, + "tmp_buffer_address_list", task_id, "&conv_parameter", in_func_, out_func_); + context->AppendCode(code.str()); + return RET_OK; +} +} // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_winograd_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_winograd_fp32_coder.h new file mode 100644 index 0000000000..8bce785ad0 --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_winograd_fp32_coder.h @@ -0,0 +1,75 @@ +/** + * 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_CODER_OPCODERS_FP32_CONVOLUTION_WINOGRAD_FP32_CODER_H_ +#define MINDSPORE_LITE_MICRO_CODER_OPCODERS_FP32_CONVOLUTION_WINOGRAD_FP32_CODER_H_ + +#include +#include +#include +#include "micro/coder/opcoders/base/conv2d_base_coder.h" +#include "nnacl/conv_parameter.h" + +namespace mindspore::lite::micro::nnacl { +class ConvolutionWinogradFP32Coder : public Conv2DBaseCoder { + public: + ConvolutionWinogradFP32Coder(const std::vector &in_tensors, const std::vector &out_tensors, + const Model::Node *node, size_t node_index, Target target, int output_unit) + : Conv2DBaseCoder(in_tensors, out_tensors, node, node_index, target), output_unit_(output_unit) {} + + int Prepare(CoderContext *const context) override; + + int DoCode(CoderContext *const context) override; + + ~ConvolutionWinogradFP32Coder() override = default; + + private: + int InitWeightBias(); + + int ConfigInputOutput(); + + int InitTmpBuffer(); + + int ReSize(); + + int WinogradFilterTransform(const float *weight_data, float *matrix_g, const float *matrix_gt, int oc_block); + + std::string GetInputTransFunc(int input_unit); + + std::string GetOutputTransFunc(int input_unit, int output_unit, ActType act_type); + + float *trans_weight_{nullptr}; + float *new_bias_{nullptr}; + + int kernel_unit_{0}; + int input_unit_{0}; + int output_unit_{0}; + + size_t tmp_data_size_{0}; + size_t tile_buffer_size_{0}; + size_t gemm_out_size_{0}; + size_t col_buffer_size_{0}; + + float *tmp_data_{nullptr}; + float *trans_input_{nullptr}; + float *gemm_out_{nullptr}; + float *col_buffer_{nullptr}; + + std::string in_func_; + std::string out_func_; +}; +} // namespace mindspore::lite::micro::nnacl +#endif // MINDSPORE_LITE_MICRO_CODER_OPCODERS_FP32_CONVOLUTION_WINOGRAD_FP32_CODER_H_ diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/expand_dims_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/expand_dims_fp32_coder.cc index e4bcf2cfd5..5e5ca04f04 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/expand_dims_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/expand_dims_fp32_coder.cc @@ -14,10 +14,10 @@ * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/fp32/expand_dims_fp32_coder.h" +#include "coder/opcoders/nnacl/fp32/expand_dims_fp32_coder.h" #include -#include "micro/coder/opcoders/file_collector.h" -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" +#include "coder/opcoders/file_collector.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" using mindspore::schema::PrimitiveType_ExpandDims; diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/expand_dims_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/expand_dims_fp32_coder.h index 727372351c..f91a48ea0f 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/expand_dims_fp32_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/expand_dims_fp32_coder.h @@ -18,7 +18,7 @@ #define MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_EXPANDDIMS_FP32_CODER_H_ #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" namespace mindspore::lite::micro::nnacl { class ExpandDimsFP32Coder final : public OperatorCoder { diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/gather_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/gather_fp32_coder.cc index 2157589ecf..d84d2c8c68 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/gather_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/gather_fp32_coder.cc @@ -14,12 +14,12 @@ * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/fp32/gather_fp32_coder.h" +#include "coder/opcoders/nnacl/fp32/gather_fp32_coder.h" #include #include "nnacl/gather_parameter.h" -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" -#include "micro/coder/log.h" -#include "micro/coder/opcoders/file_collector.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" +#include "coder/log.h" +#include "coder/opcoders/file_collector.h" using mindspore::schema::PrimitiveType_Gather; diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/gather_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/gather_fp32_coder.h index d3d142707d..616b14c255 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/gather_fp32_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/gather_fp32_coder.h @@ -18,7 +18,7 @@ #define MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_GATHER_FP32_CODER_H_ #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" #include "nnacl/base/tile_base.h" namespace mindspore::lite::micro::nnacl { diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/nchw2nhwc_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/nchw2nhwc_fp32_coder.cc index b8fe43e26b..6a1daf2ef8 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/nchw2nhwc_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/nchw2nhwc_fp32_coder.cc @@ -14,11 +14,11 @@ * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/fp32/nchw2nhwc_fp32_coder.h" +#include "coder/opcoders/nnacl/fp32/nchw2nhwc_fp32_coder.h" #include #include -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" -#include "micro/coder/opcoders/file_collector.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" +#include "coder/opcoders/file_collector.h" using mindspore::schema::PrimitiveType_Nchw2Nhwc; diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/nchw2nhwc_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/nchw2nhwc_fp32_coder.h index bd643556bd..193d371ba2 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/nchw2nhwc_fp32_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/nchw2nhwc_fp32_coder.h @@ -19,7 +19,7 @@ #include #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" #include "nnacl/base/tile_base.h" namespace mindspore::lite::micro::nnacl { diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/nhwc2nchw_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/nhwc2nchw_fp32_coder.cc index 0e3c61a9b2..e96a62e125 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/nhwc2nchw_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/nhwc2nchw_fp32_coder.cc @@ -14,10 +14,10 @@ * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/fp32/nhwc2nchw_fp32_coder.h" +#include "coder/opcoders/nnacl/fp32/nhwc2nchw_fp32_coder.h" #include -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" -#include "micro/coder/opcoders/file_collector.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" +#include "coder/opcoders/file_collector.h" using mindspore::schema::PrimitiveType_Nhwc2Nchw; namespace mindspore::lite::micro::nnacl { diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/nhwc2nchw_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/nhwc2nchw_fp32_coder.h index 20088d76b1..8d54450a06 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/nhwc2nchw_fp32_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/nhwc2nchw_fp32_coder.h @@ -18,7 +18,7 @@ #define MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_NHWC2NCHW_FP32_CODER_H_ #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" #include "nnacl/base/tile_base.h" namespace mindspore::lite::micro::nnacl { diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/pad_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/pad_fp32_coder.h index 6ebf8acef4..19aff13921 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/pad_fp32_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/pad_fp32_coder.h @@ -18,7 +18,7 @@ #define MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_PAD_FP32_CODER_H_ #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" #include "nnacl/fp32/pad_fp32.h" namespace mindspore::lite::micro::nnacl { diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/pooling_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/pooling_fp32_coder.cc index 09b73517c8..7ce3b2c343 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/pooling_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/pooling_fp32_coder.cc @@ -13,13 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/fp32/pooling_fp32_coder.h" +#include "coder/opcoders/nnacl/fp32/pooling_fp32_coder.h" #include #include #include "nnacl/fp32/pooling_fp32.h" -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" -#include "micro/coder/log.h" -#include "micro/coder/opcoders/file_collector.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" +#include "coder/log.h" +#include "coder/opcoders/file_collector.h" using mindspore::schema::PrimitiveType_Pooling; diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/pooling_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/pooling_fp32_coder.h index 89acfc2e95..30429acbdd 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/pooling_fp32_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/pooling_fp32_coder.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef MICRO_CODER_OPCODERS_FP32_POOLFP32_CODER_H_ -#define MICRO_CODER_OPCODERS_FP32_POOLFP32_CODER_H_ +#ifndef MINDSPORE_LITE_MICRO_CODER_OPCODERS_POOLFP32_CODER_H_ +#define MINDSPORE_LITE_MICRO_CODER_OPCODERS_POOLFP32_CODER_H_ #include #include "micro/coder/opcoders/op_coder.h" @@ -36,4 +36,4 @@ class PoolingFP32Coder final : public OperatorCoder { } // namespace mindspore::lite::micro::nnacl -#endif // MICRO_CODER_OPCODERS_FP32__CODER_H_ +#endif // MINDSPORE_LITE_MICRO_CODER_OPCODERS_FP32_CODER_H_ diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/power_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/power_fp32_coder.cc index 5f620b491b..46999a64f2 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/power_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/power_fp32_coder.cc @@ -14,11 +14,11 @@ * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/fp32/power_fp32_coder.h" +#include "coder/opcoders/nnacl/fp32/power_fp32_coder.h" #include #include -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" -#include "micro/coder/opcoders/file_collector.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" +#include "coder/opcoders/file_collector.h" using mindspore::schema::PrimitiveType_Power; diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/power_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/power_fp32_coder.h index 94534bc8fd..07ccb7f601 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/power_fp32_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/power_fp32_coder.h @@ -18,7 +18,7 @@ #define MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_POWER_FP32_CODER_H_ #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" #include "nnacl/power_parameter.h" namespace mindspore::lite::micro::nnacl { diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/reduce_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/reduce_fp32_coder.cc index 6d82e764dc..55c71f371c 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/reduce_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/reduce_fp32_coder.cc @@ -14,11 +14,11 @@ * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/fp32/reduce_fp32_coder.h" +#include "coder/opcoders/nnacl/fp32/reduce_fp32_coder.h" #include -#include "micro/coder/log.h" -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" -#include "micro/coder/opcoders/file_collector.h" +#include "coder/log.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" +#include "coder/opcoders/file_collector.h" using mindspore::schema::PrimitiveType_Reduce; diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/reduce_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/reduce_fp32_coder.h index a9ab9c3f2e..7fea26c0e4 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/reduce_fp32_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/reduce_fp32_coder.h @@ -19,8 +19,8 @@ #include #include -#include "micro/coder/opcoders/base/reduce_base_coder.h" -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/base/reduce_base_coder.h" +#include "coder/opcoders/op_coder.h" namespace mindspore::lite::micro::nnacl { class ReduceFP32Coder final : public ReduceBaseCoder { diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/reshape_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/reshape_fp32_coder.cc index 2fd64bc4c1..4b66480103 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/reshape_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/reshape_fp32_coder.cc @@ -14,10 +14,10 @@ * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/fp32/reshape_fp32_coder.h" +#include "coder/opcoders/nnacl/fp32/reshape_fp32_coder.h" #include -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" -#include "micro/coder/opcoders/file_collector.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" +#include "coder/opcoders/file_collector.h" using mindspore::schema::PrimitiveType_Reshape; namespace mindspore::lite::micro::nnacl { diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/reshape_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/reshape_fp32_coder.h index 7a57730f77..fe32e677be 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/reshape_fp32_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/reshape_fp32_coder.h @@ -18,7 +18,7 @@ #define MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_RESHAPE_FP32_CODER_H_ #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" namespace mindspore::lite::micro::nnacl { class ReshapeFP32Coder final : public OperatorCoder { diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/scale_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/scale_fp32_coder.cc index 197b32532c..4e901cb237 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/scale_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/scale_fp32_coder.cc @@ -13,11 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/fp32/scale_fp32_coder.h" +#include "coder/opcoders/nnacl/fp32/scale_fp32_coder.h" #include -#include "micro/coder/log.h" -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" -#include "micro/coder/opcoders/file_collector.h" +#include "coder/log.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" +#include "coder/opcoders/file_collector.h" using mindspore::schema::PrimitiveType_Scale; diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/scale_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/scale_fp32_coder.h index 5074f24bf6..19a31141e5 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/scale_fp32_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/scale_fp32_coder.h @@ -18,7 +18,7 @@ #define MICRO_CODER_OPCODERS_FP32_SCALEFP32_CODER_H_ #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" #include "nnacl/scale.h" namespace mindspore::lite::micro::nnacl { diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/slice_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/slice_fp32_coder.cc index 86fe2c4ed2..15d31e4b41 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/slice_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/slice_fp32_coder.cc @@ -14,12 +14,12 @@ * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/fp32/slice_fp32_coder.h" +#include "coder/opcoders/nnacl/fp32/slice_fp32_coder.h" #include #include "nnacl/slice_parameter.h" #include "src/ops/slice.h" -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" -#include "micro/coder/opcoders/file_collector.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" +#include "coder/opcoders/file_collector.h" using mindspore::schema::PrimitiveType_Slice; namespace mindspore::lite::micro::nnacl { diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/slice_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/slice_fp32_coder.h index a44802feb3..97acb64263 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/slice_fp32_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/slice_fp32_coder.h @@ -18,7 +18,7 @@ #define MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_SLICE_FP32_CODER_H_ #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" namespace mindspore::lite::micro::nnacl { class SliceFP32Coder final : public OperatorCoder { diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/softmax_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/softmax_fp32_coder.cc index edd4f21f03..b949590ebb 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/softmax_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/softmax_fp32_coder.cc @@ -14,11 +14,11 @@ * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/fp32/softmax_fp32_coder.h" +#include "coder/opcoders/nnacl/fp32/softmax_fp32_coder.h" #include -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" #include "schema/inner/ops_generated.h" -#include "micro/coder/opcoders/file_collector.h" +#include "coder/opcoders/file_collector.h" using mindspore::schema::PrimitiveType_SoftMax; diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/squeeze_dims_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/squeeze_dims_fp32_coder.cc index da977ea24f..2a52286a32 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/squeeze_dims_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/squeeze_dims_fp32_coder.cc @@ -14,10 +14,10 @@ * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/fp32/squeeze_dims_fp32_coder.h" +#include "coder/opcoders/nnacl/fp32/squeeze_dims_fp32_coder.h" #include -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" -#include "micro/coder/opcoders/file_collector.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" +#include "coder/opcoders/file_collector.h" using mindspore::schema::PrimitiveType_Squeeze; diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/squeeze_dims_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/squeeze_dims_fp32_coder.h index 0f97a0894b..05c5d6bf02 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/squeeze_dims_fp32_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/squeeze_dims_fp32_coder.h @@ -18,7 +18,7 @@ #define MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_SQUEEZE_FP32_CODER_H_ #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" namespace mindspore::lite::micro::nnacl { class SqueezeFP32Coder final : public OperatorCoder { diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/tile_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/tile_fp32_coder.cc index c373ea175a..0376e42bac 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/tile_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/tile_fp32_coder.cc @@ -14,11 +14,11 @@ * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/fp32/tile_fp32_coder.h" +#include "coder/opcoders/nnacl/fp32/tile_fp32_coder.h" #include #include -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" -#include "micro/coder/opcoders/file_collector.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" +#include "coder/opcoders/file_collector.h" using mindspore::schema::PrimitiveType_Tile; diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/tile_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/tile_fp32_coder.h index 12279ea64a..3b370fc3da 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/tile_fp32_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/tile_fp32_coder.h @@ -18,7 +18,7 @@ #define MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_TILE_FP32_CODER_H_ #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" #include "nnacl/base/tile_base.h" namespace mindspore::lite::micro::nnacl { diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/transpose_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/transpose_fp32_coder.h index 5bfb2bb1ab..e4a150bcc7 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/transpose_fp32_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/transpose_fp32_coder.h @@ -14,10 +14,10 @@ * limitations under the License. */ -#ifndef MICRO_LITE_MICRO_CODER_OPCODERS_NNACL_FP32_TRANSPOSE_FP32_CODER_H_ -#define MICRO_LITE_MICRO_CODER_OPCODERS_NNACL_FP32_TRANSPOSE_FP32_CODER_H_ +#ifndef MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_FP32_TRANSPOSE_FP32_CODER_H_ +#define MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_FP32_TRANSPOSE_FP32_CODER_H_ #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" #include "nnacl/transpose.h" namespace mindspore::lite::micro::nnacl { @@ -50,4 +50,4 @@ class TransposeFp32Coder final : public OperatorCoder { }; } // namespace mindspore::lite::micro::nnacl -#endif // MICRO_LITE_MICRO_CODER_OPCODERS_NNACL_FP32_TRANSPOSE_FP32_CODER_H_ +#endif // MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_FP32_TRANSPOSE_FP32_CODER_H_ diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/concat_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/int8/concat_int8_coder.cc index 2a69194753..99c217c990 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/int8/concat_int8_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/concat_int8_coder.cc @@ -14,14 +14,14 @@ * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/int8/concat_int8_coder.h" +#include "coder/opcoders/nnacl/int8/concat_int8_coder.h" #include #include "nnacl/int8/concat_int8.h" #include "nnacl/int8/arithmetic_int8.h" #include "nnacl/int8/quantize.h" -#include "micro/coder/opcoders/file_collector.h" -#include "micro/coder/log.h" -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" +#include "coder/opcoders/file_collector.h" +#include "coder/log.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" int MallocQuantArgForConcat(ConcatQuantArg *quant_arg, size_t input_num) { quant_arg->in_args_ = static_cast(malloc(sizeof(QuantArg) * input_num)); diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/pooling_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/int8/pooling_int8_coder.cc index 9bf408d1af..5b3a9cd1fe 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/int8/pooling_int8_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/pooling_int8_coder.cc @@ -13,14 +13,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/int8/pooling_int8_coder.h" +#include "coder/opcoders/nnacl/int8/pooling_int8_coder.h" #include #include #include #include "nnacl/int8/pooling_int8.h" -#include "micro/coder/log.h" -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" -#include "micro/coder/opcoders/file_collector.h" +#include "coder/log.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" +#include "coder/opcoders/file_collector.h" using std::string; diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/pooling_int8_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/int8/pooling_int8_coder.h index e34da97130..dd068c8468 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/int8/pooling_int8_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/pooling_int8_coder.h @@ -20,7 +20,7 @@ #include #include #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" namespace mindspore::lite::micro::nnacl { diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/reduce_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/int8/reduce_int8_coder.cc index 14b44b4a65..da7f81687e 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/int8/reduce_int8_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/reduce_int8_coder.cc @@ -13,12 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/int8/reduce_int8_coder.h" +#include "coder/opcoders/nnacl/int8/reduce_int8_coder.h" #include #include -#include "micro/coder/opcoders/file_collector.h" -#include "micro/coder/log.h" -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" +#include "coder/opcoders/file_collector.h" +#include "coder/log.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" using mindspore::schema::PrimitiveType_Reduce; namespace mindspore::lite::micro::nnacl { diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/reduce_int8_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/int8/reduce_int8_coder.h index a40c429563..3ab5919f2e 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/int8/reduce_int8_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/reduce_int8_coder.h @@ -14,15 +14,15 @@ * limitations under the License. */ -#ifndef MICRO_LITE_MICRO_CODER_OPCODERS_INT8_REDUCE_INT8_CODER_H_ -#define MICRO_LITE_MICRO_CODER_OPCODERS_INT8_REDUCE_INT8_CODER_H_ +#ifndef MINDSPORE_LITE_MICRO_CODER_OPCODERS_INT8_REDUCE_INT8_CODER_H_ +#define MINDSPORE_LITE_MICRO_CODER_OPCODERS_INT8_REDUCE_INT8_CODER_H_ #include #include #include "coder/opcoders/op_coder.h" #include "nnacl/int8/quantize.h" #include "nnacl/int8/reduce_int8.h" -#include "micro/coder/opcoders/base/reduce_base_coder.h" +#include "coder/opcoders/base/reduce_base_coder.h" namespace mindspore::lite::micro::nnacl { class ReduceInt8Coder final : public ReduceBaseCoder { public: @@ -52,4 +52,4 @@ class ReduceInt8Coder final : public ReduceBaseCoder { std::vector sum_square_multipliers_; }; } // namespace mindspore::lite::micro::nnacl -#endif // MICRO_LITE_MICRO_CODER_OPCODERS_INT8_REDUCE_INT8_CODER_H_ +#endif // MINDSPORE_LITE_MICRO_CODER_OPCODERS_INT8_REDUCE_INT8_CODER_H_ diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/reshape_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/int8/reshape_int8_coder.cc index 261bb797e2..0df7a07986 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/int8/reshape_int8_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/reshape_int8_coder.cc @@ -14,12 +14,12 @@ * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/int8/reshape_int8_coder.h" +#include "coder/opcoders/nnacl/int8/reshape_int8_coder.h" #include #include -#include "micro/coder/opcoders/file_collector.h" -#include "micro/coder/log.h" -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" +#include "coder/opcoders/file_collector.h" +#include "coder/log.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" using mindspore::schema::PrimitiveType_Reshape; diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/reshape_int8_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/int8/reshape_int8_coder.h index 22ffad642c..4053f4b02b 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/int8/reshape_int8_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/reshape_int8_coder.h @@ -18,7 +18,7 @@ #define MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_RESHAPE_INT8_CODER_H_ #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" namespace mindspore::lite::micro::nnacl { class ReshapeInt8Coder final : public OperatorCoder { diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/softmax_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/int8/softmax_int8_coder.cc index ff4a07740b..40a1bfc528 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/int8/softmax_int8_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/softmax_int8_coder.cc @@ -14,16 +14,16 @@ * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/int8/softmax_int8_coder.h" +#include "coder/opcoders/nnacl/int8/softmax_int8_coder.h" #include #include #include #include #include "schema/inner/ops_generated.h" #include "nnacl/softmax_parameter.h" -#include "micro/coder/log.h" -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" -#include "micro/coder/opcoders/file_collector.h" +#include "coder/log.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" +#include "coder/opcoders/file_collector.h" using mindspore::schema::PrimitiveType_SoftMax; diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/softmax_int8_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/int8/softmax_int8_coder.h index 41ef1c7de8..c8ce497469 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/int8/softmax_int8_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/softmax_int8_coder.h @@ -19,7 +19,7 @@ #include #include #include -#include "micro/coder/opcoders/base/softmax_base_coder.h" +#include "coder/opcoders/base/softmax_base_coder.h" namespace mindspore::lite::micro::nnacl { diff --git a/mindspore/lite/micro/coder/opcoders/op_coder.cc b/mindspore/lite/micro/coder/opcoders/op_coder.cc index 61075e3381..bc4820e400 100644 --- a/mindspore/lite/micro/coder/opcoders/op_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/op_coder.cc @@ -29,35 +29,18 @@ const std::vector OperatorCoder::input_tensors() const { return input_ const std::vector OperatorCoder::output_tensors() const { return output_tensors_; } -void OperatorCoder::set_input_tensor_indices(const std::vector *input_indices) { - input_tensor_indices_ = *input_indices; +void OperatorCoder::set_input_tensor_indices(const std::vector &input_indices) { + input_tensor_indices_ = input_indices; } -void OperatorCoder::set_output_tensor_indices(const std::vector *output_indices) { - output_tensor_indices_ = *output_indices; +void OperatorCoder::set_output_tensor_indices(const std::vector &output_indices) { + output_tensor_indices_ = output_indices; } const std::vector OperatorCoder::input_tensor_indices() const { return input_tensor_indices_; } const std::vector OperatorCoder::output_tensor_indices() const { return output_tensor_indices_; } -void OperatorCoder::AddInputNodeIndex(size_t input_node_index) { - if (!input_node_set_indices_.count(input_node_index)) { - input_node_set_indices_.insert(input_node_index); - input_node_indices_.push_back(input_node_index); - } -} - -void OperatorCoder::AddOutputNodeIndex(size_t output_node_index) { - if (!output_node_set_indices_.count(output_node_index)) { - output_node_set_indices_.insert(output_node_index); - output_node_indices_.push_back(output_node_index); - } -} - -const std::vector OperatorCoder::input_node_indices() const { return this->input_node_indices_; } -const std::vector OperatorCoder::output_node_indices() const { return this->output_node_indices_; } - void OperatorCoder::set_parameter(OpParameter *parameter) { this->parameter_ = parameter; } size_t OperatorCoder::node_index() const { return node_index_; } diff --git a/mindspore/lite/micro/coder/opcoders/op_coder.h b/mindspore/lite/micro/coder/opcoders/op_coder.h index 85cc6af090..ced4cb489a 100644 --- a/mindspore/lite/micro/coder/opcoders/op_coder.h +++ b/mindspore/lite/micro/coder/opcoders/op_coder.h @@ -20,8 +20,8 @@ #include #include #include -#include "coder/coder_context.h" -#include "coder/coder_graph.h" +#include "coder/context.h" +#include "coder/graph.h" #include "coder/allocator/allocator.h" #include "include/errorcode.h" #include "src/lite_kernel.h" @@ -50,25 +50,23 @@ class OperatorCoder { } std::string ID() const { return node_->name_; } - void set_input_tensor_indices(const std::vector *input_indices); - void set_output_tensor_indices(const std::vector *output_indices); + void set_input_tensor_indices(const std::vector &input_indices); + void set_output_tensor_indices(const std::vector &output_indices); const std::vector input_tensor_indices() const; const std::vector output_tensor_indices() const; - void AddInputNodeIndex(size_t input_node_index); - void AddOutputNodeIndex(size_t output_node_index); + const std::vector input_tensors() const; + const std::vector output_tensors() const; - const std::vector input_node_indices() const; - const std::vector output_node_indices() const; + void AddInputOp(OperatorCoder *op) { input_ops_.push_back(op); } + void AddOutputOp(OperatorCoder *op) { output_ops_.push_back(op); } + const std::vector input_ops() const { return input_ops_; } + const std::vector output_ops() const { return output_ops_; } size_t node_index() const; void set_parameter(OpParameter *parameter); - - const std::vector input_tensors() const; - const std::vector output_tensors() const; - const PrimitiveC *primitive() const { return node_->primitive_; } const Model::Node *node() const { return this->node_; } @@ -99,20 +97,15 @@ class OperatorCoder { MemoryAllocator *allocator_{nullptr}; std::string thread_num_s_{"1"}; - int thread_num_{1}; private: size_t node_index_{0}; std::vector input_tensor_indices_; std::vector output_tensor_indices_; - std::vector input_node_indices_; - std::vector output_node_indices_; - std::set input_node_set_indices_; - std::set output_node_set_indices_; - std::vector> input_nodes_; - std::vector> output_nodes_; + std::vector input_ops_; + std::vector output_ops_; std::vector initial_parameters_; }; diff --git a/mindspore/lite/micro/coder/opcoders/op_coder_builder.cc b/mindspore/lite/micro/coder/opcoders/op_coder_builder.cc index 8eeb91a251..2511da37e8 100644 --- a/mindspore/lite/micro/coder/opcoders/op_coder_builder.cc +++ b/mindspore/lite/micro/coder/opcoders/op_coder_builder.cc @@ -45,7 +45,7 @@ std::unique_ptr OpCoderBuilder::build() { MS_CHECK_PTR_RET_NULL(inputs_.at(kInputIndex)); MS_CHECK_PTR_RET_NULL(outputs_.at(kOutputIndex)); } - std::unique_ptr op_coder = creator_func(inputs_, outputs_, node_, node_index_, target_); + std::unique_ptr op_coder = creator_func(inputs_, outputs_, node_, node_index_++, target_); if (!op_coder) { MS_LOG(ERROR) << "coderFactor create a null op_coder: " << node_->name_ << " primitive type: " << mindspore::schema::EnumNamePrimitiveType(static_cast(primitive_type)) @@ -59,8 +59,8 @@ std::unique_ptr OpCoderBuilder::build() { << schema::EnumNamePrimitiveType((schema::PrimitiveType)(primitive_type)); return nullptr; } - op_coder->set_input_tensor_indices(&input_indices_); - op_coder->set_output_tensor_indices(&output_indices_); + op_coder->set_input_tensor_indices(input_indices_); + op_coder->set_output_tensor_indices(output_indices_); int thread_num = this->mode_ == CodeMode::Code_Android ? kMAX_THREAD_NUM_SUPPORT : 1; op_coder->set_thread_num(thread_num); parameter->thread_num_ = thread_num; diff --git a/mindspore/lite/micro/coder/opcoders/op_coder_register.h b/mindspore/lite/micro/coder/opcoders/op_coder_register.h index 3b98f23561..08942c2786 100644 --- a/mindspore/lite/micro/coder/opcoders/op_coder_register.h +++ b/mindspore/lite/micro/coder/opcoders/op_coder_register.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef MICRO_MICRO_CODER_OPCODERS_OP_CODER_REGISTER_H_ -#define MICRO_MICRO_CODER_OPCODERS_OP_CODER_REGISTER_H_ +#ifndef MINDSPORE_LITE_MICRO_CODER_OPCODERS_OP_CODER_REGISTER_H_ +#define MINDSPORE_LITE_MICRO_CODER_OPCODERS_OP_CODER_REGISTER_H_ #include #include @@ -82,4 +82,4 @@ class OpCoderRegister { static OpCoderRegister g_##target##data_type##operator_type##Creator(target, data_type, operator_type, creator_func); } // namespace mindspore::lite::micro -#endif // MICRO_MICRO_CODER_OPCODERS_OP_CODER_REGISTER_H_ +#endif // MINDSPORE_LITE_MICRO_CODER_OPCODERS_OP_CODER_REGISTER_H_ diff --git a/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h b/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h index a77fb29042..7128853050 100644 --- a/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h +++ b/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef MICRO_MICRO_CODER_OPCODERS_SERIALIZERS_NNACL_FP32_SERIALIZER_H_ -#define MICRO_MICRO_CODER_OPCODERS_SERIALIZERS_NNACL_FP32_SERIALIZER_H_ +#ifndef MINDSPORE_LITE_MICRO_CODER_OPCODERS_SERIALIZERS_NNACL_FP32_SERIALIZER_H_ +#define MINDSPORE_LITE_MICRO_CODER_OPCODERS_SERIALIZERS_NNACL_FP32_SERIALIZER_H_ #include #include #include "micro/coder/opcoders/serializers/serializer.h" @@ -48,4 +48,4 @@ class NNaclFp32Serializer : public Serializer { }; } // namespace mindspore::lite::micro::nnacl -#endif // MICRO_MICRO_CODER_OPCODERS_SERIALIZERS_NNACL_FP32_ERIALIZER_H_ +#endif // MINDSPORE_LITE_MICRO_CODER_OPCODERS_SERIALIZERS_NNACL_FP32_ERIALIZER_H_ diff --git a/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h b/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h index b8a04f8069..eef0961467 100644 --- a/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h +++ b/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h @@ -13,8 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef MICRO_MICRO_CODER_OPCODERS_SERIALIZERS_NNACL_INT8_SERIALIZER_H_ -#define MICRO_MICRO_CODER_OPCODERS_SERIALIZERS_NNACL_INT8_SERIALIZER_H_ +#ifndef MINDSPORE_LITE_MICRO_CODER_OPCODERS_SERIALIZERS_NNACL_INT8_SERIALIZER_H_ +#define MINDSPORE_LITE_MICRO_CODER_OPCODERS_SERIALIZERS_NNACL_INT8_SERIALIZER_H_ #include #include #include "nnacl/pooling_parameter.h" @@ -48,4 +48,4 @@ class NNaclInt8Serializer : public Serializer { }; } // namespace mindspore::lite::micro::nnacl -#endif // MICRO_MICRO_CODER_OPCODERS_SERIALIZERS_NNACL_INT8_SERIALIZER_H_ +#endif // MINDSPORE_LITE_MICRO_CODER_OPCODERS_SERIALIZERS_NNACL_INT8_SERIALIZER_H_ diff --git a/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_stream_utils.h b/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_stream_utils.h index 31a579df0a..d89549821b 100644 --- a/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_stream_utils.h +++ b/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_stream_utils.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef LITE_MICRO_CODER_OPCODERS_SERIALIZERS_NNACL_STREAM_UTILS_H_ -#define LITE_MICRO_CODER_OPCODERS_SERIALIZERS_NNACL_STREAM_UTILS_H_ +#ifndef MINDSPORE_LITE_MICRO_CODER_OPCODERS_SERIALIZERS_NNACL_STREAM_UTILS_H_ +#define MINDSPORE_LITE_MICRO_CODER_OPCODERS_SERIALIZERS_NNACL_STREAM_UTILS_H_ #include #include #include "nnacl/pooling_parameter.h" @@ -62,4 +62,4 @@ inline std::ostream &operator<<(std::ostream &code, DataOrder data_order) { return code; } } // namespace mindspore::lite::micro -#endif // LITE_MICRO_CODER_OPCODERS_SERIALIZERS_NNACL_STREAM_UTILS_H_ +#endif // MINDSPORE_LITE_MICRO_CODER_OPCODERS_SERIALIZERS_NNACL_STREAM_UTILS_H_ diff --git a/mindspore/lite/micro/coder/opcoders/serializers/serializer.h b/mindspore/lite/micro/coder/opcoders/serializers/serializer.h index acf1056f61..dd844da5f3 100644 --- a/mindspore/lite/micro/coder/opcoders/serializers/serializer.h +++ b/mindspore/lite/micro/coder/opcoders/serializers/serializer.h @@ -20,7 +20,6 @@ #include #include #include -#include "micro/coder/utils/print_utils.h" #include "micro/coder/allocator/allocator.h" #include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_stream_utils.h" diff --git a/mindspore/lite/micro/coder/session_coder.cc b/mindspore/lite/micro/coder/session.cc similarity index 77% rename from mindspore/lite/micro/coder/session_coder.cc rename to mindspore/lite/micro/coder/session.cc index 6ff0ba7f33..ab4a8617f0 100644 --- a/mindspore/lite/micro/coder/session_coder.cc +++ b/mindspore/lite/micro/coder/session.cc @@ -14,12 +14,13 @@ * limitations under the License. */ -#include "coder/session_coder.h" +#include "coder/session.h" +#include #include +#include #include #include "coder/allocator/allocator.h" -#include "coder/coder_context.h" -#include "coder/debug.h" +#include "coder/context.h" #include "coder/generator/generator.h" #include "coder/generator/inference/inference_generator.h" #include "coder/opcoders/op_coder_builder.h" @@ -77,19 +78,17 @@ int CoderSession::InferShape() { } void CoderSession::EndCode() { - coder_context_->set_tensor_map(allocator_->tensors_map()); - coder_context_->set_saved_weights(allocator_->saved_weights()); - coder_context_->set_total_buffer_size(allocator_->total_buffer_size()); - std::vector blocks; - for (size_t index = 0; index < coder_context_->code_blocks().size(); ++index) { - auto &curr_node = op_coders_.at(index); - std::string coder_block = coder_context_->code_blocks().at(index); - MicroDebug::DumpNodeData(curr_node, coder_context_->tensors_map(), &coder_block); - blocks.emplace_back(coder_block); + context_->set_tensor_map(allocator_->tensors_map()); + context_->set_saved_weights(allocator_->saved_weights()); + context_->set_total_buffer_size(allocator_->total_buffer_size()); + context_->set_graph_inputs(coder_graph_->input_tensors()); + context_->set_graph_outputs(coder_graph_->output_tensors()); + Configurator *config = Configurator::GetInstance(); + if (config->debug_mode()) { + std::vector blocks; + blocks = AddDumpDataInfo(context_->code_blocks(), op_coders_); + context_->set_code_blocks(blocks); } - coder_context_->set_code_blocks(blocks); - coder_context_->set_graph_inputs(coder_graph_->input_tensors()); - coder_context_->set_graph_outputs(coder_graph_->output_tensors()); } int CoderSession::Run() { @@ -97,36 +96,19 @@ int CoderSession::Run() { // 1. assign memory std::vector inputs = coder_graph_->input_tensors(); int ret = allocator_->Assign(inputs, op_coders_); - if (ret != RET_OK) { - MS_LOG(ERROR) << "assign memory failed"; - return RET_ERROR; - } - + MS_CHECK_RET_CODE(ret, "assign memory failed"); // 2. prepare, init model parameters for (const auto &op_coder : op_coders_) { - if (op_coder == nullptr) { - MS_LOG(ERROR) << "opcoder is nullptr"; - return RET_ERROR; - } - ret = op_coder->Prepare(coder_context_.get()); - if (ret != RET_OK) { - MS_LOG(ERROR) << "prepare coder " << op_coder->ID() << " failed"; - return RET_ERROR; - } + MS_CHECK_PTR(op_coder); + ret = op_coder->Prepare(context_.get()); + MS_CHECK_RET_CODE(ret, "prepare coder " << op_coder->ID() << " failed"); allocator_->enable_is_next(); } - // 3. docode, write operator code for (const auto &op_coder : op_coders_) { - if (op_coder == nullptr) { - MS_LOG(ERROR) << "opcoder is nullptr"; - return RET_ERROR; - } - ret = op_coder->DoCode(this->coder_context_.get()); - if (ret != RET_OK) { - MS_LOG(ERROR) << "do coder " << op_coder->ID() << " failed"; - return RET_ERROR; - } + MS_CHECK_PTR(op_coder); + ret = op_coder->DoCode(this->context_.get()); + MS_CHECK_RET_CODE(ret, "do coder " << op_coder->ID() << " failed"); } this->EndCode(); @@ -143,7 +125,7 @@ int CoderSession::GenerateCode() { case Code_Normal: case Code_Android: MS_LOG(INFO) << "generate code for Android"; - generator = std::make_shared(std::move(coder_context_)); + generator = std::make_shared(std::move(context_)); break; default: MS_LOG(ERROR) << "unsupported generator code mode, " << code_mode; @@ -165,11 +147,8 @@ int CoderSession::Init(const std::string &model_path) { MS_LOG(DEBUG) << "start reading model file"; size_t size = 0; char *graph_buf = ReadFile(model_path.c_str(), &size); - if (graph_buf == nullptr) { - MS_LOG(ERROR) << "the graphBuf is nullptr"; - return RET_ERROR; - } - // new a coder_context for session + MS_CHECK_PTR(graph_buf); + // new a context for session if (size >= UINT_MAX) { MS_LOG(ERROR) << "the size is invalid"; delete[] graph_buf; @@ -179,9 +158,8 @@ int CoderSession::Init(const std::string &model_path) { delete[] graph_buf; MS_CHECK_PTR(model); coder_graph_ = std::make_unique(model); - coder_context_ = std::make_unique(); - allocator_->RecordRuntimeAddrs(coder_context_->input_name(), coder_context_->buffer_name(), - coder_context_->weight_name()); + context_ = std::make_unique(); + allocator_->RecordRuntimeAddrs(context_->input_name(), context_->buffer_name(), context_->weight_name()); MS_LOG(INFO) << "CoderSession::Init done"; return RET_OK; } @@ -198,26 +176,30 @@ int CoderSession::Build() { return RET_OK; } -int CoderSession::InitNodesInputsAndOutputs() { - auto &op_coders = this->op_coders_; - for (const auto &op_coder : op_coders) { - for (const auto &search : op_coders) { - if (search.get() == op_coder.get()) { - continue; - } - for (const auto &tensor : op_coder->input_tensors()) { - std::vector outputs = search->output_tensors(); - auto iter = std::find(outputs.begin(), outputs.end(), tensor); - if (iter != outputs.end()) { - op_coder->AddInputNodeIndex(search->node_index()); - } +int CoderSession::InitOpcodersInputsAndOutputs() { + std::map input_node_map; + std::map output_node_map; + for (const auto &op_coder : op_coders_) { + std::vector inputs = op_coder->input_tensors(); + std::for_each(inputs.begin(), inputs.end(), + [&](Tensor *t) { input_node_map.insert(std::make_pair(t, op_coder.get())); }); + std::vector outputs = op_coder->input_tensors(); + std::for_each(outputs.begin(), outputs.end(), + [&](Tensor *t) { output_node_map.insert(std::make_pair(t, op_coder.get())); }); + } + for (const auto &op_coder : op_coders_) { + std::vector inputs = op_coder->input_tensors(); + for (const auto &tensor : inputs) { + auto item = output_node_map.find(tensor); + if (item != output_node_map.end()) { + op_coder->AddInputOp(item->second); } - for (const auto &tensor : op_coder->output_tensors()) { - auto inputs = search->input_tensors(); - auto iter = std::find(inputs.begin(), inputs.end(), tensor); - if (iter != inputs.end()) { - op_coder->AddOutputNodeIndex(search->node_index()); - } + } + std::vector outputs = op_coder->output_tensors(); + for (const auto &tensor : outputs) { + auto item = input_node_map.find(tensor); + if (item != input_node_map.end()) { + op_coder->AddOutputOp(item->second); } } } @@ -262,39 +244,39 @@ int CoderSession::ConvertTensors() { // deal with allTensors uint32_t tensorCount = model->all_tensors_.size(); for (uint32_t i = 0; i < tensorCount; ++i) { - auto *meta_tensor = model->all_tensors_.at(i); - MS_CHECK_PTR_WITH_EXE(meta_tensor, clear_tensors()); + schema::Tensor *origin_tensor = model->all_tensors_.at(i); + MS_CHECK_PTR_WITH_EXE(origin_tensor, clear_tensors()); // tensor dims std::vector shape; - if (meta_tensor->nodeType() == schema::NodeType_ValueNode) { - MS_CHECK_PTR_WITH_EXE(meta_tensor->dims(), clear_tensors()); - for (uint32_t j = 0; j < meta_tensor->dims()->size(); j++) { - MS_CHECK_PTR(meta_tensor->dims()->data()); - int dim = static_cast(meta_tensor->dims()->data()[j]); + if (origin_tensor->nodeType() == schema::NodeType_ValueNode) { + MS_CHECK_PTR_WITH_EXE(origin_tensor->dims(), clear_tensors()); + for (uint32_t j = 0; j < origin_tensor->dims()->size(); j++) { + MS_CHECK_PTR(origin_tensor->dims()->data()); + int dim = static_cast(origin_tensor->dims()->data()[j]); MS_CHECK_RET_CODE_WITH_EXE(check_dim(dim), "parse shape failed!", clear_tensors()); shape.push_back(dim); } } // tensor Datatype - int meta_data_type = static_cast(meta_tensor->dataType()); - auto dstTensor = new (std::nothrow) - lite::Tensor(TypeId(meta_data_type), shape, meta_tensor->format(), TensorCategory(meta_tensor)); + int origin_data_type = static_cast(origin_tensor->dataType()); + Tensor *dstTensor = new (std::nothrow) + lite::Tensor(TypeId(origin_data_type), shape, origin_tensor->format(), TensorCategory(origin_tensor)); MS_CHECK_PTR(dstTensor); - if (meta_tensor->nodeType() == schema::NodeType_ValueNode && meta_tensor->data() != nullptr && - meta_tensor->data()->size() > 0) { + if (origin_tensor->nodeType() == schema::NodeType_ValueNode && origin_tensor->data() != nullptr && + origin_tensor->data()->size() > 0) { if (shape.empty()) { shape.push_back(1); } // copy data, this is weight && bias - MS_CHECK_TRUE(meta_tensor->data()->size() > 0, "invalid meta_tensor data size"); - auto data_size = static_cast(meta_tensor->data()->size()); - MS_CHECK_RET_CODE(dstTensor->MallocData(), "dst tensor malloc data failed!"); + MS_CHECK_TRUE_WITH_EXE(origin_tensor->data()->size() > 0, "invalid meta_tensor data size.", delete dstTensor); + auto data_size = static_cast(origin_tensor->data()->size()); + MS_CHECK_RET_CODE_WITH_EXE(dstTensor->MallocData(), "dst tensor malloc data failed!", delete dstTensor); void *dst_data = dstTensor->data_c(); - MS_CHECK_RET_CODE(memcpy_s(dst_data, data_size, meta_tensor->data()->data(), data_size), - "memcpy_s copy data failed!"); + MS_CHECK_RET_CODE_WITH_EXE(memcpy_s(dst_data, data_size, origin_tensor->data()->data(), data_size), + "memcpy_s copy data failed!", delete dstTensor); dstTensor->set_data(dst_data); } - auto quant_params = meta_tensor->quantParams(); + auto quant_params = origin_tensor->quantParams(); if (quant_params != nullptr) { for (int j = 0; j < static_cast(quant_params->size()); j++) { QuantArg quant_arg{}; @@ -380,7 +362,7 @@ int CoderSession::CreateOpCoders() { op_coders_.push_back(std::move(op_coder)); builder.Reset(); } - InitNodesInputsAndOutputs(); + InitOpcodersInputsAndOutputs(); return RET_OK; } @@ -452,12 +434,10 @@ int CoderSession::InitGraphInOutTensors() { } int CoderSession::CompileGraph() { + MS_LOG(INFO) << "CompileGraph"; MS_CHECK_RET_CODE(ConvertTensors(), "ConvertTensors failed"); MS_CHECK_RET_CODE(InitGraphInOutTensors(), "InitGraphInOutTensors failed"); - // InferShape MS_CHECK_RET_CODE(InferShape(), "do infershape failed!"); - - // create all op_coders MS_CHECK_RET_CODE(CreateOpCoders(), "CreateOpCoders failed!"); MS_CHECK_RET_CODE(InitTensorsRef(), "InitTensorsRefcount failed!"); return RET_OK; diff --git a/mindspore/lite/micro/coder/session_coder.h b/mindspore/lite/micro/coder/session.h similarity index 83% rename from mindspore/lite/micro/coder/session_coder.h rename to mindspore/lite/micro/coder/session.h index fc46c9ed6b..d02a06b1bf 100644 --- a/mindspore/lite/micro/coder/session_coder.h +++ b/mindspore/lite/micro/coder/session.h @@ -13,16 +13,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef MICRO_CODER_SESSION_CODER_H_ -#define MICRO_CODER_SESSION_CODER_H_ +#ifndef MINDSPORE_LITE_MICRO_CODER_SESSION_H_ +#define MINDSPORE_LITE_MICRO_CODER_SESSION_H_ #include #include #include #include #include "schema/inner/model_generated.h" -#include "coder/coder_graph.h" -#include "coder/coder_context.h" +#include "coder/graph.h" +#include "coder/context.h" #include "coder/coder_config.h" #include "coder/allocator/allocator.h" #include "coder/opcoders/op_coder.h" @@ -43,7 +43,7 @@ class CoderSession { int GenerateCode(); private: - int InitNodesInputsAndOutputs(); + int InitOpcodersInputsAndOutputs(); int InitTensorsRef(); int ConvertTensors(); int CreateOpCoders(); @@ -53,7 +53,7 @@ class CoderSession { void EndCode(); std::unique_ptr coder_graph_{nullptr}; - std::unique_ptr coder_context_{nullptr}; + std::unique_ptr context_{nullptr}; MemoryAllocator *allocator_{nullptr}; std::vector> op_coders_; }; @@ -61,4 +61,4 @@ class CoderSession { std::shared_ptr CreateCoderSession(); } // namespace mindspore::lite::micro -#endif // MICRO_CODER_SESSION_CODER_H_ +#endif // MINDSPORE_LITE_MICRO_CODER_SESSION_H_ diff --git a/mindspore/lite/micro/coder/utils/coder_utils.cc b/mindspore/lite/micro/coder/utils/coder_utils.cc index adb4f8faa1..731d17ff5f 100644 --- a/mindspore/lite/micro/coder/utils/coder_utils.cc +++ b/mindspore/lite/micro/coder/utils/coder_utils.cc @@ -14,31 +14,160 @@ * limitations under the License. */ #include "coder/utils/coder_utils.h" +#include +#include #include +#include +#include +#include "coder/log.h" +#include "coder/utils/type_cast.h" +#include "coder/allocator/allocator.h" namespace mindspore::lite::micro { +template +void TensorDataToFile(const lite::Tensor *tensor, std::ofstream &ofs) { + const int NUM = 45; + T *data = reinterpret_cast(tensor->data_c()); + if (data == nullptr) { + MS_LOG(ERROR) << "data is nullptr"; + return; + } + ofs << "{\n"; + if (typeid(T) == typeid(float)) { + ofs.precision(kWeightPrecision); + } + int len = tensor->ElementsNum(); + for (int i = 0; i < len; ++i) { + ofs << data[i] << ", "; + if (i % NUM == NUM - 1) { + ofs << "\n"; + } + } + ofs << "\n};\n\n"; +} -std::string EnumNameDataType(TypeId type) { - switch (type) { - case kNumberTypeInt: - return "kNumberTypeInt"; +void PrintTensorData(const lite::Tensor *tensor, std::ofstream &ofs) { + TypeId type = tensor->data_type(); + switch (tensor->data_type()) { + case kNumberTypeFloat: + case kNumberTypeFloat32: + TensorDataToFile(tensor, ofs); + break; case kNumberTypeInt8: - return "kNumberTypeInt8"; - case kNumberTypeInt16: - return "kNumberTypeInt16"; + TensorDataToFile(tensor, ofs); + break; + case kNumberTypeInt: case kNumberTypeInt32: - return "kNumberTypeInt32"; - case kNumberTypeFloat32: - return "kNumberTypeFloat32"; - case kNumberTypeFloat16: - return "kNumberTypeFloat16"; - case kNumberTypeFloat64: - return "kNumberTypeFloat64"; - case kTypeUnknown: - return "kTypeUnknown"; + TensorDataToFile(tensor, ofs); + case kNumberTypeInt64: + TensorDataToFile(tensor, ofs); + break; + case kNumberTypeUInt8: + TensorDataToFile(tensor, ofs); + break; + case kNumberTypeUInt32: + TensorDataToFile(tensor, ofs); + break; default: - return "unsupported type, " + std::to_string(type); + MS_LOG(ERROR) << "unsupported data type: " << EnumNameDataType(type); + break; + } +} + +template +std::string ArrayToString(const std::vector &array) { + std::string result = "{"; + std::for_each(array.begin(), array.end(), [&result](const T &t) { result += std::to_string(t) + ", "; }); + return result + "}"; +} + +std::string TensorsToString(const std::vector &tensors, const std::string &is_input) { + MemoryAllocator *allocator = MemoryAllocator::GetInstance(); + std::string info; + for (const auto &tensor : tensors) { + if (tensor->category() == Tensor::Category::CONST_TENSOR) { + continue; + } + info += " {\n"; + info += " int dim[] = " + ArrayToString(tensor->shape()) + ";\n"; + info += " MicroTensor tensor = {"; + info += EnumMicroTensorDataType(tensor->data_type()) + ", "; + info += EnumMicroTensorFormat(tensor->format()) + ", "; + info += std::to_string(tensor->shape().size()) + ", dim, "; + info += allocator->GetRuntimeAddr(tensor) + "};\n"; + info += " fprintf(output_file, \"" + is_input + " Tensor: " + allocator->GetRuntimeAddr(tensor) + "\\n\");\n"; + info += " PrintTensor(&tensor, output_file, \"" + is_input + "\");\n"; + info += " }\n"; + } + return info; +} + +std::vector AddDumpDataInfo(const std::vector &blocks, + const std::vector> &opcoders) { + std::vector results; + if (blocks.size() != opcoders.size()) { + MS_LOG(ERROR) << "error, coder blocks size is not equal to opcoders size"; + return results; + } + size_t num = opcoders.size(); + for (size_t i = 0; i < num; ++i) { + auto &opcoder = opcoders.at(i); + std::string code = blocks.at(i); + std::string name = opcoder->ID(); + code += " {\n"; + code += " FILE *output_file = fopen(\"./" + name + ".ir\", \"w\");\n"; + code += " fprintf(output_file, \"Node:" + name + "\\n\");\n"; + code += TensorsToString(opcoder->input_tensors(), "input"); + code += TensorsToString(opcoder->output_tensors(), "output"); + code += " fclose(output_file);\n"; + code += " }\n"; + results.emplace_back(code); + } + return results; +} + +std::vector SplitString(std::string str, const std::string &pattern) { + std::vector results; + if (str.empty()) { + MS_LOG(ERROR) << "source string is empty"; + return results; + } + str += pattern; + while (!str.empty()) { + size_t size = str.size(); + size_t pos = str.find(pattern); + std::string sub_string = str.substr(0, pos); + results.push_back(sub_string); + str = str.substr(pos + 1, size); } + return results; } +std::set FindInferenceOpcoders(OperatorCoder *edge) { + std::set subgraph; + std::queue to_visit; + to_visit.push(edge); + while (!to_visit.empty()) { + size_t size = to_visit.size(); + for (size_t i = 0; i < size; ++i) { + OperatorCoder *curr = to_visit.front(); + to_visit.pop(); + if (subgraph.find(curr) != subgraph.end()) { + continue; + } + subgraph.insert(curr); + for (const auto &op : curr->input_ops()) { + to_visit.push(op); + } + } + } + auto item = subgraph.find(edge); + if (item == subgraph.end()) { + MS_LOG(ERROR) << "failed to find the edge in the subgraph"; + return subgraph; + } + // erase edge operator coder from subgraph + subgraph.erase(item); + return subgraph; +} } // namespace mindspore::lite::micro diff --git a/mindspore/lite/micro/coder/utils/coder_utils.h b/mindspore/lite/micro/coder/utils/coder_utils.h index 567f0fe8c4..2a8e058856 100644 --- a/mindspore/lite/micro/coder/utils/coder_utils.h +++ b/mindspore/lite/micro/coder/utils/coder_utils.h @@ -13,23 +13,30 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef MICRO_CODER_CODER_UTILS_CODER_UTILS_H_ -#define MICRO_CODER_CODER_UTILS_CODER_UTILS_H_ +#ifndef MINDSPORE_LITE_MICRO_CODER_UTILS_CODER_UTILS_H_ +#define MINDSPORE_LITE_MICRO_CODER_UTILS_CODER_UTILS_H_ +#include #include #include +#include #include #include "include/errorcode.h" #include "securec/include/securec.h" #include "src/tensor.h" +#include "coder/opcoders/op_coder.h" namespace mindspore::lite::micro { -constexpr int kSubSize = 2; -constexpr int kDefaultDims = 4; +constexpr int kWeightPrecision = 9; -std::string EnumNameDataType(TypeId type); +std::vector AddDumpDataInfo(const std::vector &blocks, + const std::vector> &opcoders); + +void PrintTensorData(const lite::Tensor *tensor, std::ofstream &ofs); + +std::set FindInferenceOpcoders(OperatorCoder *edge); } // namespace mindspore::lite::micro -#endif // MICRO_CODER_CODER_UTILS_CODER_UTILS_H_ +#endif // MINDSPORE_LITE_MICRO_CODER_UTILS_CODER_UTILS_H_ diff --git a/mindspore/lite/micro/coder/utils/dir_utils.h b/mindspore/lite/micro/coder/utils/dir_utils.h index c9f3fc6011..6a378d5a4e 100644 --- a/mindspore/lite/micro/coder/utils/dir_utils.h +++ b/mindspore/lite/micro/coder/utils/dir_utils.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef MINDSPORE_PREDICT_MICRO_CODER_CODER_UTILS_INIT_PROJ_DIRS_H_ -#define MINDSPORE_PREDICT_MICRO_CODER_CODER_UTILS_INIT_PROJ_DIRS_H_ +#ifndef MINDSPORE_LITE_MICRO_CODER_UTILS_DIRS_H_ +#define MINDSPORE_LITE_MICRO_CODER_UTILS_DIRS_H_ #include namespace mindspore::lite::micro { #if defined(_WIN32) || defined(_WIN64) @@ -31,4 +31,4 @@ bool DirExists(const std::string &dir_path); bool FileExists(const std::string &dir_path); } // namespace mindspore::lite::micro -#endif // MINDSPORE_PREDICT_MICRO_CODER_CODER_UTILS_INIT_PROJ_DIRS_H_ +#endif // MINDSPORE_LITE_MICRO_CODER_UTILS_DIRS_H_ diff --git a/mindspore/lite/micro/coder/utils/print_utils.cc b/mindspore/lite/micro/coder/utils/print_utils.cc deleted file mode 100644 index 58af23a35d..0000000000 --- a/mindspore/lite/micro/coder/utils/print_utils.cc +++ /dev/null @@ -1,294 +0,0 @@ -/** - * 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/utils/print_utils.h" - -namespace mindspore::lite::micro { - -std::string GetPrintFormat(const lite::Tensor *tensor) { - switch (tensor->data_type()) { - case kNumberTypeFloat: { - return "%f"; - } - case kNumberTypeInt8: { - return "%c"; - } - case kNumberTypeInt32: { - return "%d"; - } - case kNumberTypeUInt8: { - return "%d"; - } - case kNumberTypeInt16: { - return "%d"; - } - case kNumberTypeUInt32: { - return "%ld"; - } - case kNumberTypeInt64: { - return "%l64d"; - } - case kNumberTypeUInt16: { - return "%f"; - } - case kNumberTypeFloat16: { - MS_LOG(WARNING) << "unsupported data type: kNumberTypeFloat16"; - return "float "; - } - default: - MS_LOG(WARNING) << "unsupported data type: " << tensor->data_type(); - return "%d"; - } -} - -template -void PrintTensorData(const lite::Tensor *tensor, std::ofstream &of, const std::string &left = "\t") { - const int NUM = 20; - T *data = reinterpret_cast(tensor->data_c()); - of << "{\n" << left; - int len = tensor->ElementsNum(); - if (typeid(T) == typeid(float)) { - of.precision(kWeightPrecision); - for (int i = 0; i < len - 1; ++i) { - of << data[i] << ","; - if (i % NUM == NUM - 1) { - of << std::endl << left; - } - } - if (len > 0) { - of << data[len - 1]; - } - } else { - for (int i = 0; i < len - 1; ++i) { - of << std::to_string(data[i]) << ","; - if (i % NUM == NUM - 1) { - of << std::endl << left; - } - } - if (len > 0) { - of << std::to_string(data[len - 1]); - } - } - of << "\n" << left << "};\n\n"; -} - -void PrintTensor(const lite::Tensor *tensor, std::ofstream &weightOf, std::ofstream &hOf, - const std::string &tensorName) { - switch (tensor->data_type()) { - case kNumberTypeFloat: { - weightOf << "const float " << tensorName << "[] = "; - hOf << "extern const float " << tensorName << "[];\n"; - PrintTensorData(tensor, weightOf); - break; - } - case kNumberTypeFloat32: { - weightOf << "const float " << tensorName << "[] = "; - hOf << "extern const float " << tensorName << "[];\n"; - PrintTensorData(tensor, weightOf); - break; - } - case kNumberTypeInt8: { - weightOf << "const signed char " << tensorName << "[] = "; - hOf << "extern const signed char " << tensorName << "[];\n"; - PrintTensorData(tensor, weightOf); - break; - } - case kNumberTypeInt32: { - weightOf << "const int " << tensorName << "[] = "; - hOf << "extern const int " << tensorName << "[];\n"; - PrintTensorData(tensor, weightOf); - break; - } - case kNumberTypeUInt8: { - weightOf << "const unsigned char " << tensorName << "[] = "; - hOf << "extern const unsigned char " << tensorName << "[];\n"; - PrintTensorData(tensor, weightOf); - break; - } - case kNumberTypeInt16: { - weightOf << "const short " << tensorName << "[] = "; - hOf << "extern const short " << tensorName << "[];\n"; - PrintTensorData(tensor, weightOf); - break; - } - case kNumberTypeUInt32: { - weightOf << "const unsigned int " << tensorName << "[] = "; - hOf << "extern const unsigned int " << tensorName << "[];\n"; - PrintTensorData(tensor, weightOf); - break; - } - case kNumberTypeInt64: { - weightOf << "const long " << tensorName << "[] = "; - hOf << "extern const long " << tensorName << "[];\n"; - PrintTensorData(tensor, weightOf); - break; - } - case kNumberTypeUInt16: { - weightOf << "const unsigned short " << tensorName << "[] = "; - hOf << "extern const unsigned short " << tensorName << "[];\n"; - PrintTensorData(tensor, weightOf); - break; - } - case kNumberTypeFloat16: { - MS_LOG(WARNING) << "unsupported data type: kNumberTypeFloat16"; - break; - } - default: - MS_LOG(WARNING) << "unsupported data type: " << tensor->data_type(); - } -} - -void PrintTensorForNet(const lite::Tensor *tensor, std::ofstream &weightOf, std::ofstream &hOf, - const std::string &tensorName) { - MS_LOG(DEBUG) << "PrintTensorForNet tensor dtype: " << tensor->data_type(); - switch (tensor->data_type()) { - case kNumberTypeFloat: { - weightOf << "float " << tensorName << "[" << tensor->ElementsNum() << "]={0};\n"; - hOf << "extern float " << tensorName << "[];\n"; - break; - } - case kNumberTypeFloat32: { - weightOf << "float " << tensorName << "[" << tensor->ElementsNum() << "]={0};\n"; - hOf << "extern float " << tensorName << "[];\n"; - break; - } - - case kNumberTypeInt8: { - weightOf << "signed char " << tensorName << "[" << tensor->ElementsNum() << "]={0};\n"; - hOf << "extern signed char " << tensorName << "[];\n"; - break; - } - case kNumberTypeInt32: { - weightOf << "int " << tensorName << "[" << tensor->ElementsNum() << "]={0};\n"; - hOf << "extern int " << tensorName << "[];\n"; - break; - } - case kNumberTypeUInt8: { - weightOf << "unsigned char " << tensorName << "[" << tensor->ElementsNum() << "]={0};\n"; - hOf << "extern unsigned char " << tensorName << "[];\n"; - break; - } - case kNumberTypeInt16: { - weightOf << "short " << tensorName << "[" << tensor->ElementsNum() << "]={0};\n"; - hOf << "extern short " << tensorName << "[];\n"; - break; - } - case kNumberTypeUInt32: { - weightOf << "unsigned int " << tensorName << "[" << tensor->ElementsNum() << "]={0};\n"; - hOf << "extern unsigned int " << tensorName << "[];\n"; - break; - } - case kNumberTypeInt64: { - weightOf << "long " << tensorName << "[" << tensor->ElementsNum() << "]={0};\n"; - hOf << "extern long " << tensorName << "[];\n"; - break; - } - case kNumberTypeUInt16: { - weightOf << "unsigned short " << tensorName << "[" << tensor->ElementsNum() << "]={0};\n"; - hOf << "extern unsigned short " << tensorName << "[];\n"; - break; - } - case kNumberTypeFloat16: { - weightOf << "float " << tensorName << "[" << tensor->ElementsNum() << "]={0};\n"; - hOf << "extern float " << tensorName << "[];\n"; - break; - } - default: - MS_LOG(WARNING) << "Default DataType_DT not support. Tensor name: " << tensorName.c_str(); - } -} - -std::string GetTensorDataType(const TypeId typeId) { - switch (typeId) { - case kNumberTypeFloat32: { - return "float "; - } - case kNumberTypeFloat: { - return "float "; - } - case kNumberTypeInt8: { - return "char "; - } - case kNumberTypeInt: { - return "int "; - } - case kNumberTypeInt32: { - return "int "; - } - case kNumberTypeUInt8: { - return "unsigned char "; - } - case kNumberTypeInt16: { - return "short "; - } - case kNumberTypeUInt32: { - return "unsigned int "; - } - case kNumberTypeInt64: { - return "long "; - } - case kNumberTypeUInt16: { - return "unsigned short "; - } - case kNumberTypeFloat16: { - MS_LOG(WARNING) << "unsupported data type: kNumberTypeFloat16"; - return "float "; - } - default: - MS_LOG(WARNING) << "unsupported data type: " << typeId; - return "int"; - } -} - -std::string GetMicroTensorDataType(TypeId type) { - switch (type) { - case kNumberTypeFloat: - case kNumberTypeFloat32: { - return "DataType_DT_FLOAT"; - } - case kNumberTypeInt8: { - return "DataType_DT_INT8"; - } - case kNumberTypeInt: - case kNumberTypeInt32: { - return "DataType_DT_INT32"; - } - case kNumberTypeUInt8: { - return "DataType_DT_UINT8"; - } - case kNumberTypeInt16: { - return "DataType_DT_INT16"; - } - case kNumberTypeUInt32: { - return "DataType_DT_UINT32"; - } - case kNumberTypeInt64: { - return "DataType_DT_INT64"; - } - case kNumberTypeUInt16: { - return "DataType_DT_UINT16"; - } - case kNumberTypeFloat16: { - MS_LOG(WARNING) << "unsupported data type: kNumberTypeFloat16"; - return "DataType_DT_FLOAT16"; - } - default: - MS_LOG(WARNING) << "unsupported data type: " << type << ", reference: " << kNumberTypeInt; - return "DataType_DT_UNDEFINED"; - } -} - -} // namespace mindspore::lite::micro diff --git a/mindspore/lite/micro/coder/utils/type_cast.cc b/mindspore/lite/micro/coder/utils/type_cast.cc new file mode 100644 index 0000000000..7be96be52a --- /dev/null +++ b/mindspore/lite/micro/coder/utils/type_cast.cc @@ -0,0 +1,117 @@ +/** + * 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/utils/type_cast.h" +#include + +namespace mindspore::lite::micro { + +std::string EnumNameDataType(TypeId type) { + switch (type) { + case kNumberTypeInt: + return "kNumberTypeInt"; + case kNumberTypeInt8: + return "kNumberTypeInt8"; + case kNumberTypeInt16: + return "kNumberTypeInt16"; + case kNumberTypeInt32: + return "kNumberTypeInt32"; + case kNumberTypeFloat: + case kNumberTypeFloat32: + return "kNumberTypeFloat32"; + case kNumberTypeFloat16: + return "kNumberTypeFloat16"; + case kNumberTypeFloat64: + return "kNumberTypeFloat64"; + case kTypeUnknown: + return "kTypeUnknown"; + default: + return "unsupported"; + } +} + +std::string GetTensorDataType(TypeId type) { + switch (type) { + case kNumberTypeFloat: + case kNumberTypeFloat32: + return "float "; + case kNumberTypeInt8: + return "int8"; + case kNumberTypeInt: + case kNumberTypeInt32: + return "int32_t"; + case kNumberTypeUInt8: + return "uint8_t"; + case kNumberTypeUInt32: + return "uint32_t"; + case kNumberTypeInt64: + return "int64_t"; + default: + MS_LOG(ERROR) << "unsupported data type: " << EnumNameDataType(type); + return ""; + } +} + +std::string EnumMicroTensorFormat(schema::Format format) { + switch (format) { + case schema::Format_NHWC: + return "Format_NHWC"; + case schema::Format_NCHW: + return "Format_NCHW"; + case schema::Format_HWKC: + return "Format_HWKC"; + case schema::Format_HWCK: + return "Format_HWCK"; + case schema::Format_KCHW: + return "Format_KCHW"; + case schema::Format_CKHW: + return "Format_CKHW"; + case schema::Format_NC4HW4: + return "Format_NC4HW4"; + default: + MS_LOG(ERROR) << "unsupported format: " << schema::EnumNameFormat(format); + return "Format_NUM_OF_FORMAT"; + } +} + +std::string EnumMicroTensorDataType(TypeId type) { + switch (type) { + case kNumberTypeFloat: + case kNumberTypeFloat32: + return "DataType_DT_FLOAT"; + case kNumberTypeInt8: + return "DataType_DT_INT8"; + case kNumberTypeInt: + case kNumberTypeInt32: + return "DataType_DT_INT32"; + case kNumberTypeUInt8: + return "DataType_DT_UINT8"; + case kNumberTypeInt16: + return "DataType_DT_INT16"; + case kNumberTypeUInt32: + return "DataType_DT_UINT32"; + case kNumberTypeInt64: + return "DataType_DT_INT64"; + case kNumberTypeUInt16: + return "DataType_DT_UINT16"; + case kNumberTypeFloat16: + MS_LOG(WARNING) << "unsupported data type: kNumberTypeFloat16"; + return "DataType_DT_FLOAT16"; + default: + MS_LOG(WARNING) << "unsupported data type: " << type << ", reference: " << kNumberTypeInt; + return "DataType_DT_UNDEFINED"; + } +} +} // namespace mindspore::lite::micro diff --git a/mindspore/lite/micro/coder/utils/print_utils.h b/mindspore/lite/micro/coder/utils/type_cast.h similarity index 78% rename from mindspore/lite/micro/coder/utils/print_utils.h rename to mindspore/lite/micro/coder/utils/type_cast.h index 2c30f0007f..44b599fcb0 100644 --- a/mindspore/lite/micro/coder/utils/print_utils.h +++ b/mindspore/lite/micro/coder/utils/type_cast.h @@ -13,34 +13,28 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#ifndef MINDSPORE_LITE_MICRO_CODER_UTILS_TYPE_CAST_H_ +#define MINDSPORE_LITE_MICRO_CODER_UTILS_TYPE_CAST_H_ -#ifndef MINDSPORE_MICRO_PRINT_UTILS_H_ -#define MINDSPORE_MICRO_PRINT_UTILS_H_ - -#include -#include -#include #include -#include +#include +#include +#include #include +#include +#include "include/errorcode.h" +#include "securec/include/securec.h" #include "src/tensor.h" #include "nnacl/int8/quantize.h" namespace mindspore::lite::micro { +std::string EnumNameDataType(TypeId type); -constexpr int kWeightPrecision = 9; +std::string GetTensorDataType(TypeId type); -std::string GetPrintFormat(const lite::Tensor *tensor); +std::string EnumMicroTensorFormat(schema::Format format); -void PrintTensor(const lite::Tensor *tensor, std::ofstream &weightOf, std::ofstream &hOf, - const std::string &tensorName); - -void PrintTensorForNet(const lite::Tensor *tensor, std::ofstream &weightOf, std::ofstream &hOf, - const std::string &tensorName); - -std::string GetTensorDataType(const TypeId typeId); - -std::string GetMicroTensorDataType(TypeId type); +std::string EnumMicroTensorDataType(TypeId type); /** * @tparam T @@ -61,7 +55,6 @@ std::string GetVariableTypeName() { {std::type_index(typeid(int16_t *)), "int16_t *"}, {std::type_index(typeid(int8_t *)), "int8_t *"}, {std::type_index(typeid(float *)), "float *"}}; - auto item = types_name.find(std::type_index(typeid(T))); if (item != types_name.end()) { return item->second; @@ -71,4 +64,4 @@ std::string GetVariableTypeName() { } } // namespace mindspore::lite::micro -#endif // MINDSPORE_MICRO_PRINT_UTILS_H_ +#endif // MINDSPORE_LITE_MICRO_CODER_UTILS_TYPE_CAST_H_ diff --git a/mindspore/lite/micro/test/CMakeLists.txt b/mindspore/lite/micro/test/CMakeLists.txt index 2aa93c774e..9d2be7c223 100644 --- a/mindspore/lite/micro/test/CMakeLists.txt +++ b/mindspore/lite/micro/test/CMakeLists.txt @@ -2,6 +2,7 @@ string(REPLACE "/test" "" MICRO_DIR ${CMAKE_CURRENT_SOURCE_DIR}) string(REPLACE " -fvisibility=hidden " " -fvisibility=default " CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") string(REPLACE " -fvisibility=hidden " " -fvisibility=default " CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") set(LITE_DIR ${MICRO_DIR}/..) +set(NNACL_DIR ${LITE_DIR}/nnacl) set(3RD_DIR ${TOP_DIR}/third_party) set(BUILD_LITE "on") diff --git a/mindspore/lite/micro/test/code_gen_test.cc b/mindspore/lite/micro/test/code_gen_test.cc index 40704cf6c0..6d0859da08 100644 --- a/mindspore/lite/micro/test/code_gen_test.cc +++ b/mindspore/lite/micro/test/code_gen_test.cc @@ -19,7 +19,7 @@ namespace mindspore::lite::micro::test { TEST(GenerateCodeTest, mnist_x86) { - const char *argv[] = {"./codegen", "--modelPath=../example/mnist.ms", "--moduleName=mnist", "--codePath=.", + const char *argv[] = {"./codegen", "--modelPath=../example/mnist/mnist.ms", "--moduleName=mnist", "--codePath=.", "--isWeightFile"}; STATUS status = RunCoder(5, argv); ASSERT_EQ(status, RET_OK);