fix bugs of micro, and add dump tensor in benchmark

pull/13490/head
yangjie159 5 years ago
parent 1ad9f9de21
commit 8087fc88e9

@ -99,8 +99,12 @@ class String {
char *buffer_; char *buffer_;
}; };
String operator+(const String &str1, const char *str2); String operator+(const String &lhs, const char *rhs);
String operator+(const char *str1, const String &str2); 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(int32_t value);
String to_string(float value); String to_string(float value);

@ -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/debug_utils.cc
${MICRO_DIR}/coder/generator/component/const_blocks/msession.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/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/license.cc
${MICRO_DIR}/coder/generator/component/const_blocks/load_input.cc ${MICRO_DIR}/coder/generator/component/const_blocks/load_input.cc
${MICRO_DIR}/coder/generator/component/const_blocks/benchmark.cc ${MICRO_DIR}/coder/generator/component/const_blocks/benchmark.cc

@ -22,7 +22,6 @@ set(WRAPPER_SRC
${WRAPPER_DIR}/int8/resize_int8_wrapper.c ${WRAPPER_DIR}/int8/resize_int8_wrapper.c
${WRAPPER_DIR}/int8/slice_int8_wrapper.c ${WRAPPER_DIR}/int8/slice_int8_wrapper.c
${WRAPPER_DIR}/int8/batchnorm_int8_wrapper.c ${WRAPPER_DIR}/int8/batchnorm_int8_wrapper.c
${LITE_DIR}/src/common/string.cc
) )
list(APPEND FILE_SET ${WRAPPER_SRC} ${RUNTIME_SRC}) list(APPEND FILE_SET ${WRAPPER_SRC} ${RUNTIME_SRC})

@ -28,7 +28,7 @@ namespace mindspore::lite::micro {
void CodeSessionCompileGraph(std::ofstream &ofs, const std::unique_ptr<CoderContext> &ctx) { void CodeSessionCompileGraph(std::ofstream &ofs, const std::unique_ptr<CoderContext> &ctx) {
auto array_tostring = [&ofs](const std::vector<int> &array, const std::string &name) { auto array_tostring = [&ofs](const std::vector<int> &array, const std::string &name) {
size_t num = array.size(); size_t num = array.size();
ofs << " Vector<int> " << name << ";\n"; ofs << " Vector<int32_t> " << name << ";\n";
ofs << " " << name << ".resize(" << num << ");\n"; ofs << " " << name << ".resize(" << num << ");\n";
for (size_t i = 0; i < num; ++i) { for (size_t i = 0; i < num; ++i) {
ofs << " " << name << "[" << i << "] = " << array[i] << ";\n"; ofs << " " << name << "[" << i << "] = " << array[i] << ";\n";
@ -63,6 +63,25 @@ void CodeSessionCompileGraph(std::ofstream &ofs, const std::unique_ptr<CoderCont
ofs << "}\n\n"; ofs << "}\n\n";
} }
void CodeCreateSessionImplement(std::ofstream &ofs, Target target) {
ofs << "session::LiteSession *session::LiteSession::CreateSession(const char *net_buf, size_t size,\n"
" const lite::Context *context) {\n"
" session::LiteSession *session = CreateSession(context);\n"
" if (session == nullptr) {\n"
" return nullptr;\n"
" }\n"
" int ret = session->CompileGraph(nullptr);\n"
" if (ret != lite::RET_OK) {\n"
" return nullptr;\n"
" }\n";
if (target != kARM32M) {
ofs << " Init(const_cast<char *>(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 CodeCopyOutputsState(std::ofstream &ofs) { ofs << "int CopyOutputsData(void **outputs, int num);\n\n"; }
void CodeCopyOutputsImplement(std::ofstream &ofs, const std::unique_ptr<CoderContext> &ctx) { void CodeCopyOutputsImplement(std::ofstream &ofs, const std::unique_ptr<CoderContext> &ctx) {

@ -24,9 +24,11 @@
#include <fstream> #include <fstream>
#include "src/tensor.h" #include "src/tensor.h"
#include "coder/context.h" #include "coder/context.h"
#include "coder/config.h"
namespace mindspore::lite::micro { namespace mindspore::lite::micro {
void CodeSessionCompileGraph(std::ofstream &ofs, const std::unique_ptr<CoderContext> &ctx); void CodeSessionCompileGraph(std::ofstream &ofs, const std::unique_ptr<CoderContext> &ctx);
void CodeCreateSessionImplement(std::ofstream &ofs, Target target);
void CodeCopyOutputsState(std::ofstream &ofs); void CodeCopyOutputsState(std::ofstream &ofs);
void CodeCopyOutputsImplement(std::ofstream &ofs, const std::unique_ptr<CoderContext> &ctx); void CodeCopyOutputsImplement(std::ofstream &ofs, const std::unique_ptr<CoderContext> &ctx);

@ -19,7 +19,6 @@
namespace mindspore::lite::micro { namespace mindspore::lite::micro {
const char *benchmark_source = R"RAW( const char *benchmark_source = R"RAW(
/** /**
* Copyright 2021 Huawei Technologies Co., Ltd * Copyright 2021 Huawei Technologies Co., Ltd
* *
@ -66,19 +65,20 @@ void PrintData(void *data, size_t data_number) {
} }
auto casted_data = static_cast<T *>(data); auto casted_data = static_cast<T *>(data);
for (size_t i = 0; i < 10 && i < data_number; i++) { 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) { void TensorToString(tensor::MSTensor *tensor) {
std::cout << ", DataType: " << tensor->data_type(); printf("name: %s, ", tensor->tensor_name().c_str());
std::cout << ", Size: " << tensor->Size(); printf(", DataType: %d", tensor->data_type());
std::cout << ", Shape:"; printf(", Size: %lu", tensor->Size());
printf(", Shape: ");
for (auto &dim : tensor->shape()) { for (auto &dim : tensor->shape()) {
std::cout << " " << dim; printf("%d ", dim);
} }
std::cout << ", Data:" << std::endl; printf(", Data: \n");
switch (tensor->data_type()) { switch (tensor->data_type()) {
case kNumberTypeFloat32: { case kNumberTypeFloat32: {
PrintData<float>(tensor->MutableData(), tensor->ElementsNum()); PrintData<float>(tensor->MutableData(), tensor->ElementsNum());
@ -106,11 +106,11 @@ void TensorToString(tensor::MSTensor *tensor) {
int main(int argc, const char **argv) { int main(int argc, const char **argv) {
if (argc < 2) { if (argc < 2) {
std::cout << "input command is invalid\n" << std::endl; printf("input command is invalid\n");
usage(); usage();
return lite::RET_ERROR; return lite::RET_ERROR;
} }
std::cout << "start run benchmark" << std::endl; printf("=======run benchmark======\n");
const char *model_buffer = nullptr; const char *model_buffer = nullptr;
int model_size = 0; int model_size = 0;
@ -146,14 +146,18 @@ int main(int argc, const char **argv) {
return lite::RET_ERROR; return lite::RET_ERROR;
} }
std::cout << "run benchmark success" << std::endl; Vector<String> outputs_name = session->GetOutputTensorNames();
for (const auto &name : outputs_name) {
auto output = session->GetOutputByTensorName(name);
TensorToString(output);
}
printf("========run success=======\n");
delete session; delete session;
for (size_t i = 0; i < inputs_num; ++i) { for (size_t i = 0; i < inputs_num; ++i) {
free(inputs_binbuf[i]); free(inputs_binbuf[i]);
} }
return lite::RET_OK; return lite::RET_OK;
} }
)RAW"; )RAW";
} // namespace mindspore::lite::micro } // namespace mindspore::lite::micro

@ -19,7 +19,6 @@
namespace mindspore::lite::micro { namespace mindspore::lite::micro {
const char *bench_cmake_lists_txt = R"RAW( const char *bench_cmake_lists_txt = R"RAW(
cmake_minimum_required(VERSION 3.14) cmake_minimum_required(VERSION 3.14)
project(benchmark) project(benchmark)
@ -76,11 +75,9 @@ set(SRC_FILES
) )
add_executable(benchmark ${SRC_FILES}) add_executable(benchmark ${SRC_FILES})
target_link_libraries(benchmark net -lm -pthread) target_link_libraries(benchmark net -lm -pthread)
)RAW"; )RAW";
const char *src_cmake_lists_txt = R"RAW( const char *src_cmake_lists_txt = R"RAW(
cmake_minimum_required(VERSION 3.14) cmake_minimum_required(VERSION 3.14)
project(net) project(net)
@ -163,7 +160,6 @@ function(create_library)
endfunction(create_library) endfunction(create_library)
string(CONCAT library_name "lib" net ".a") string(CONCAT library_name "lib" net ".a")
create_library() create_library()
)RAW"; )RAW";
} // namespace mindspore::lite::micro } // namespace mindspore::lite::micro

@ -166,7 +166,14 @@ Vector<String> LiteSession::GetOutputTensorNames() const {
return output_names; 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 } // namespace lite
@ -178,21 +185,6 @@ session::LiteSession *session::LiteSession::CreateSession(const lite::Context *c
session->InitRuntimeBuffer(); session->InitRuntimeBuffer();
return session; 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<char *>(net_buf), size);
return session;
}
} // namespace mindspore
)RAW"; )RAW";
} // namespace mindspore::lite::micro } // namespace mindspore::lite::micro

@ -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 * 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 String::substr(size_t pos, size_t count) const { return String(*this, pos, count); }
String operator+(const String &str1, const char *str2) { String operator+(const String &lhs, const char *rhs) {
String str = str1; String str = lhs;
str += str2; str += rhs;
return str; return str;
} }
String operator+(const char *str1, const String &str2) { String operator+(const char *lhs, const String &rhs) {
String str = str2; String str = rhs;
str += str1; str += lhs;
return str; 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) { String to_String(int32_t value) {
char tmp[sizeof(int32_t) * 4]; char tmp[sizeof(int32_t) * 4];
snprintf(tmp, sizeof(int32_t) * 4, "%d", value); snprintf(tmp, sizeof(int32_t) * 4, "%d", value);
@ -298,3 +324,6 @@ String to_String(float value) {
} }
} // namespace mindspore } // namespace mindspore
#endif // NOT_USE_STL #endif // NOT_USE_STL
)RAW";
} // namespace mindspore::lite::micro

@ -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_

@ -26,6 +26,7 @@
#include "coder/generator/component/const_blocks/load_input.h" #include "coder/generator/component/const_blocks/load_input.h"
#include "coder/generator/component/const_blocks/msession.h" #include "coder/generator/component/const_blocks/msession.h"
#include "coder/generator/component/const_blocks/mtensor.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/benchmark.h"
#include "coder/generator/component/const_blocks/license.h" #include "coder/generator/component/const_blocks/license.h"
#include "coder/log.h" #include "coder/log.h"
@ -90,7 +91,8 @@ int Generator::CodeStaticContent() {
{net_src_file_path_ + "CMakeLists.txt", src_cmake_lists_txt}, {net_src_file_path_ + "CMakeLists.txt", src_cmake_lists_txt},
{net_src_file_path_ + "session.h", session_header}, {net_src_file_path_ + "session.h", session_header},
{net_src_file_path_ + "tensor.h", tensor_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()) { 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.h", debug_utils_h));
const_blocks.emplace_back(std::make_pair(net_src_file_path_ + "debug_utils.c", debug_utils_c)); const_blocks.emplace_back(std::make_pair(net_src_file_path_ + "debug_utils.c", debug_utils_c));
@ -114,6 +116,7 @@ int Generator::CodeSessionImplement() {
ofs << "#include <new>\n\n"; ofs << "#include <new>\n\n";
CodeSessionCompileGraph(ofs, ctx_); CodeSessionCompileGraph(ofs, ctx_);
ofs << session_source; ofs << session_source;
CodeCreateSessionImplement(ofs, config_->target());
return RET_OK; return RET_OK;
} }

@ -60,5 +60,4 @@ endif()
# generate static library # generate static library
add_library(ops STATIC ${OP_FILES}) add_library(ops STATIC ${OP_FILES})
target_compile_definitions(ops PRIVATE NOT_USE_STL)
install(TARGETS ops ARCHIVE DESTINATION ${LIB_PATH}) install(TARGETS ops ARCHIVE DESTINATION ${LIB_PATH})

Loading…
Cancel
Save