diff --git a/mindspore/lite/include/lite_utils.h b/mindspore/lite/include/lite_utils.h index d7ebedbc12e..0298e753371 100644 --- a/mindspore/lite/include/lite_utils.h +++ b/mindspore/lite/include/lite_utils.h @@ -99,8 +99,12 @@ class String { char *buffer_; }; -String operator+(const String &str1, const char *str2); -String operator+(const char *str1, const String &str2); +String operator+(const String &lhs, const char *rhs); +String operator+(const char *lhs, const String &rhs); + +bool operator==(const String &lhs, const String &rhs); +bool operator==(const String &lhs, const char *rhs); +bool operator==(const char *lhs, const String &rhs); String to_string(int32_t value); String to_string(float value); diff --git a/mindspore/lite/micro/cmake/file_list.cmake b/mindspore/lite/micro/cmake/file_list.cmake index fdb2f965a0b..789cdddbbea 100644 --- a/mindspore/lite/micro/cmake/file_list.cmake +++ b/mindspore/lite/micro/cmake/file_list.cmake @@ -28,6 +28,7 @@ set(CODER_GENERATOR_SRC ${MICRO_DIR}/coder/generator/component/const_blocks/debug_utils.cc ${MICRO_DIR}/coder/generator/component/const_blocks/msession.cc ${MICRO_DIR}/coder/generator/component/const_blocks/mtensor.cc + ${MICRO_DIR}/coder/generator/component/const_blocks/mstring.cc ${MICRO_DIR}/coder/generator/component/const_blocks/license.cc ${MICRO_DIR}/coder/generator/component/const_blocks/load_input.cc ${MICRO_DIR}/coder/generator/component/const_blocks/benchmark.cc diff --git a/mindspore/lite/micro/cmake/package_wrapper.cmake b/mindspore/lite/micro/cmake/package_wrapper.cmake index 094af3886e3..54bc68e226e 100644 --- a/mindspore/lite/micro/cmake/package_wrapper.cmake +++ b/mindspore/lite/micro/cmake/package_wrapper.cmake @@ -22,7 +22,6 @@ set(WRAPPER_SRC ${WRAPPER_DIR}/int8/resize_int8_wrapper.c ${WRAPPER_DIR}/int8/slice_int8_wrapper.c ${WRAPPER_DIR}/int8/batchnorm_int8_wrapper.c - ${LITE_DIR}/src/common/string.cc ) list(APPEND FILE_SET ${WRAPPER_SRC} ${RUNTIME_SRC}) diff --git a/mindspore/lite/micro/coder/generator/component/common_component.cc b/mindspore/lite/micro/coder/generator/component/common_component.cc index 85bae959bbc..436f13dbbf8 100644 --- a/mindspore/lite/micro/coder/generator/component/common_component.cc +++ b/mindspore/lite/micro/coder/generator/component/common_component.cc @@ -28,7 +28,7 @@ namespace mindspore::lite::micro { void CodeSessionCompileGraph(std::ofstream &ofs, const std::unique_ptr &ctx) { auto array_tostring = [&ofs](const std::vector &array, const std::string &name) { size_t num = array.size(); - ofs << " Vector " << name << ";\n"; + ofs << " Vector " << name << ";\n"; ofs << " " << name << ".resize(" << num << ");\n"; for (size_t i = 0; i < num; ++i) { ofs << " " << name << "[" << i << "] = " << array[i] << ";\n"; @@ -63,6 +63,25 @@ void CodeSessionCompileGraph(std::ofstream &ofs, const std::unique_ptrCompileGraph(nullptr);\n" + " if (ret != lite::RET_OK) {\n" + " return nullptr;\n" + " }\n"; + if (target != kARM32M) { + ofs << " Init(const_cast(net_buf), size);\n"; + } + ofs << " return session;\n" + "}\n" + "} // namespace mindspore\n\n"; +} + void CodeCopyOutputsState(std::ofstream &ofs) { ofs << "int CopyOutputsData(void **outputs, int num);\n\n"; } void CodeCopyOutputsImplement(std::ofstream &ofs, const std::unique_ptr &ctx) { diff --git a/mindspore/lite/micro/coder/generator/component/common_component.h b/mindspore/lite/micro/coder/generator/component/common_component.h index 5162ab331ac..b14fc148abf 100644 --- a/mindspore/lite/micro/coder/generator/component/common_component.h +++ b/mindspore/lite/micro/coder/generator/component/common_component.h @@ -24,9 +24,11 @@ #include #include "src/tensor.h" #include "coder/context.h" +#include "coder/config.h" namespace mindspore::lite::micro { void CodeSessionCompileGraph(std::ofstream &ofs, const std::unique_ptr &ctx); +void CodeCreateSessionImplement(std::ofstream &ofs, Target target); void CodeCopyOutputsState(std::ofstream &ofs); void CodeCopyOutputsImplement(std::ofstream &ofs, const std::unique_ptr &ctx); diff --git a/mindspore/lite/micro/coder/generator/component/const_blocks/benchmark.cc b/mindspore/lite/micro/coder/generator/component/const_blocks/benchmark.cc index 0a13a05956f..6f9cf8c3d63 100644 --- a/mindspore/lite/micro/coder/generator/component/const_blocks/benchmark.cc +++ b/mindspore/lite/micro/coder/generator/component/const_blocks/benchmark.cc @@ -19,7 +19,6 @@ namespace mindspore::lite::micro { const char *benchmark_source = R"RAW( - /** * Copyright 2021 Huawei Technologies Co., Ltd * @@ -66,19 +65,20 @@ void PrintData(void *data, size_t data_number) { } auto casted_data = static_cast(data); for (size_t i = 0; i < 10 && i < data_number; i++) { - std::cout << std::to_string(casted_data[i]) << ", "; + printf("%s,", std::to_string(casted_data[i]).c_str()); } - std::cout << std::endl; + printf("\n"); } void TensorToString(tensor::MSTensor *tensor) { - std::cout << ", DataType: " << tensor->data_type(); - std::cout << ", Size: " << tensor->Size(); - std::cout << ", Shape:"; + printf("name: %s, ", tensor->tensor_name().c_str()); + printf(", DataType: %d", tensor->data_type()); + printf(", Size: %lu", tensor->Size()); + printf(", Shape: "); for (auto &dim : tensor->shape()) { - std::cout << " " << dim; + printf("%d ", dim); } - std::cout << ", Data:" << std::endl; + printf(", Data: \n"); switch (tensor->data_type()) { case kNumberTypeFloat32: { PrintData(tensor->MutableData(), tensor->ElementsNum()); @@ -106,11 +106,11 @@ void TensorToString(tensor::MSTensor *tensor) { int main(int argc, const char **argv) { if (argc < 2) { - std::cout << "input command is invalid\n" << std::endl; + printf("input command is invalid\n"); usage(); return lite::RET_ERROR; } - std::cout << "start run benchmark" << std::endl; + printf("=======run benchmark======\n"); const char *model_buffer = nullptr; int model_size = 0; @@ -146,14 +146,18 @@ int main(int argc, const char **argv) { return lite::RET_ERROR; } - std::cout << "run benchmark success" << std::endl; + Vector outputs_name = session->GetOutputTensorNames(); + for (const auto &name : outputs_name) { + auto output = session->GetOutputByTensorName(name); + TensorToString(output); + } + printf("========run success=======\n"); delete session; for (size_t i = 0; i < inputs_num; ++i) { free(inputs_binbuf[i]); } return lite::RET_OK; } - )RAW"; } // namespace mindspore::lite::micro diff --git a/mindspore/lite/micro/coder/generator/component/const_blocks/cmake_lists.cc b/mindspore/lite/micro/coder/generator/component/const_blocks/cmake_lists.cc index 21080094c28..4ab8e74618a 100644 --- a/mindspore/lite/micro/coder/generator/component/const_blocks/cmake_lists.cc +++ b/mindspore/lite/micro/coder/generator/component/const_blocks/cmake_lists.cc @@ -19,7 +19,6 @@ namespace mindspore::lite::micro { const char *bench_cmake_lists_txt = R"RAW( - cmake_minimum_required(VERSION 3.14) project(benchmark) @@ -76,11 +75,9 @@ set(SRC_FILES ) add_executable(benchmark ${SRC_FILES}) target_link_libraries(benchmark net -lm -pthread) - )RAW"; const char *src_cmake_lists_txt = R"RAW( - cmake_minimum_required(VERSION 3.14) project(net) @@ -163,7 +160,6 @@ function(create_library) endfunction(create_library) string(CONCAT library_name "lib" net ".a") create_library() - )RAW"; } // namespace mindspore::lite::micro diff --git a/mindspore/lite/micro/coder/generator/component/const_blocks/msession.cc b/mindspore/lite/micro/coder/generator/component/const_blocks/msession.cc index f158b179ee7..9daa07054c7 100644 --- a/mindspore/lite/micro/coder/generator/component/const_blocks/msession.cc +++ b/mindspore/lite/micro/coder/generator/component/const_blocks/msession.cc @@ -166,7 +166,14 @@ Vector LiteSession::GetOutputTensorNames() const { return output_names; } -mindspore::tensor::MSTensor *LiteSession::GetOutputByTensorName(const String &tensor_name) const { return nullptr; } +mindspore::tensor::MSTensor *LiteSession::GetOutputByTensorName(const String &tensor_name) const { + for (const auto &output : outputs_) { + if (output->tensor_name() == tensor_name) { + return output; + } + } + return nullptr; +} } // namespace lite @@ -178,21 +185,6 @@ session::LiteSession *session::LiteSession::CreateSession(const lite::Context *c session->InitRuntimeBuffer(); return session; } - -session::LiteSession *session::LiteSession::CreateSession(const char *net_buf, size_t size, - const lite::Context *context) { - session::LiteSession *session = CreateSession(context); - if (session == nullptr) { - return nullptr; - } - int ret = session->CompileGraph(nullptr); - if (ret != lite::RET_OK) { - return nullptr; - } - Init(const_cast(net_buf), size); - return session; -} -} // namespace mindspore )RAW"; } // namespace mindspore::lite::micro diff --git a/mindspore/lite/src/common/string.cc b/mindspore/lite/micro/coder/generator/component/const_blocks/mstring.cc similarity index 86% rename from mindspore/lite/src/common/string.cc rename to mindspore/lite/micro/coder/generator/component/const_blocks/mstring.cc index 881a3a4b170..9e2c459cfcc 100644 --- a/mindspore/lite/src/common/string.cc +++ b/mindspore/lite/micro/coder/generator/component/const_blocks/mstring.cc @@ -1,3 +1,25 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "coder/generator/component/const_blocks/mstring.h" + +namespace mindspore::lite::micro { + +const char *string_source = R"RAW( + /** * Copyright 2021 Huawei Technologies Co., Ltd * @@ -273,18 +295,22 @@ int String::compare(const char *str) const { return strcmp(buffer_, str); } String String::substr(size_t pos, size_t count) const { return String(*this, pos, count); } -String operator+(const String &str1, const char *str2) { - String str = str1; - str += str2; +String operator+(const String &lhs, const char *rhs) { + String str = lhs; + str += rhs; return str; } -String operator+(const char *str1, const String &str2) { - String str = str2; - str += str1; +String operator+(const char *lhs, const String &rhs) { + String str = rhs; + str += lhs; return str; } +bool operator==(const String &lhs, const String &rhs) { return lhs.compare(rhs) == 0; } +bool operator==(const String &lhs, const char *rhs) { return lhs.compare(rhs) == 0; } +bool operator==(const char *lhs, const String &rhs) { return rhs.compare(lhs) == 0; } + String to_String(int32_t value) { char tmp[sizeof(int32_t) * 4]; snprintf(tmp, sizeof(int32_t) * 4, "%d", value); @@ -298,3 +324,6 @@ String to_String(float value) { } } // namespace mindspore #endif // NOT_USE_STL +)RAW"; + +} // namespace mindspore::lite::micro diff --git a/mindspore/lite/micro/coder/generator/component/const_blocks/mstring.h b/mindspore/lite/micro/coder/generator/component/const_blocks/mstring.h new file mode 100644 index 00000000000..bf7f0d9ba1f --- /dev/null +++ b/mindspore/lite/micro/coder/generator/component/const_blocks/mstring.h @@ -0,0 +1,26 @@ +/** + * 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_GENERATOR_CONST_BLOCK_MSTRING_H_ +#define MINDSPORE_LITE_MICRO_GENERATOR_CONST_BLOCK_MSTRING_H_ + +namespace mindspore::lite::micro { + +extern const char *string_source; + +} // namespace mindspore::lite::micro + +#endif // MINDSPORE_LITE_MICRO_GENERATOR_CONST_BLOCK_MSTRING_H_ diff --git a/mindspore/lite/micro/coder/generator/generator.cc b/mindspore/lite/micro/coder/generator/generator.cc index 1f124a22cd7..0ee720cfd75 100644 --- a/mindspore/lite/micro/coder/generator/generator.cc +++ b/mindspore/lite/micro/coder/generator/generator.cc @@ -26,6 +26,7 @@ #include "coder/generator/component/const_blocks/load_input.h" #include "coder/generator/component/const_blocks/msession.h" #include "coder/generator/component/const_blocks/mtensor.h" +#include "coder/generator/component/const_blocks/mstring.h" #include "coder/generator/component/const_blocks/benchmark.h" #include "coder/generator/component/const_blocks/license.h" #include "coder/log.h" @@ -89,7 +90,8 @@ int Generator::CodeStaticContent() { {net_src_file_path_ + "CMakeLists.txt", src_cmake_lists_txt}, {net_src_file_path_ + "session.h", session_header}, {net_src_file_path_ + "tensor.h", tensor_header}, - {net_src_file_path_ + "tensor.cc", tensor_source}}; + {net_src_file_path_ + "tensor.cc", tensor_source}, + {net_src_file_path_ + "string.cc", string_source}}; if (config_->debug_mode()) { const_blocks.emplace_back(std::make_pair(net_src_file_path_ + "debug_utils.h", debug_utils_h)); const_blocks.emplace_back(std::make_pair(net_src_file_path_ + "debug_utils.c", debug_utils_c)); @@ -113,6 +115,7 @@ int Generator::CodeSessionImplement() { ofs << "#include \n\n"; CodeSessionCompileGraph(ofs, ctx_); ofs << session_source; + CodeCreateSessionImplement(ofs, config_->target()); return RET_OK; } diff --git a/mindspore/lite/micro/coder/operator_library/CMakeLists.txt b/mindspore/lite/micro/coder/operator_library/CMakeLists.txt index 984890590a8..ff0b529c16e 100644 --- a/mindspore/lite/micro/coder/operator_library/CMakeLists.txt +++ b/mindspore/lite/micro/coder/operator_library/CMakeLists.txt @@ -60,5 +60,4 @@ endif() # generate static library add_library(ops STATIC ${OP_FILES}) -target_compile_definitions(ops PRIVATE NOT_USE_STL) install(TARGETS ops ARCHIVE DESTINATION ${LIB_PATH}) diff --git a/mindspore/lite/micro/example/mnist_stm32f746/mnist/benchmark/load_input.c b/mindspore/lite/micro/example/mnist_stm32f746/mnist/benchmark/load_input.c old mode 100755 new mode 100644 diff --git a/mindspore/lite/micro/example/mnist_stm32f746/mnist/benchmark/load_input.h b/mindspore/lite/micro/example/mnist_stm32f746/mnist/benchmark/load_input.h old mode 100755 new mode 100644