From 6f44811cd7185f8dba84680eff26efc900246bf6 Mon Sep 17 00:00:00 2001 From: yangjie159 Date: Tue, 16 Mar 2021 19:24:45 +0800 Subject: [PATCH] uniform interface, modify c to cpp --- .../component/const_blocks/benchmark.cc | 115 ++++++++++++ .../component/const_blocks/benchmark.h | 26 +++ .../component/const_blocks/mtensor.cc | 172 ++++++++++++++++++ .../component/const_blocks/mtensor.h | 27 +++ 4 files changed, 340 insertions(+) create mode 100644 mindspore/lite/micro/coder/generator/component/const_blocks/benchmark.cc create mode 100644 mindspore/lite/micro/coder/generator/component/const_blocks/benchmark.h create mode 100644 mindspore/lite/micro/coder/generator/component/const_blocks/mtensor.cc create mode 100644 mindspore/lite/micro/coder/generator/component/const_blocks/mtensor.h diff --git a/mindspore/lite/micro/coder/generator/component/const_blocks/benchmark.cc b/mindspore/lite/micro/coder/generator/component/const_blocks/benchmark.cc new file mode 100644 index 0000000000..b7df0dfc30 --- /dev/null +++ b/mindspore/lite/micro/coder/generator/component/const_blocks/benchmark.cc @@ -0,0 +1,115 @@ +/** + * 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/benchmark.h" + +namespace mindspore::lite::micro { + +const char *benchmark_source = R"RAW( +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include "include/lite_session.h" +#include "include/ms_tensor.h" +#include "include/errorcode.h" + +#include "read_file.h" + +using namespace mindspore; + +void usage() { + printf( + "-- mindspore benchmark params usage:\n" + "args[0]: executable file\n" + "args[1]: inputs binary file\n" + "args[2]: model weight binary file\n" + "args[3]: loop count for performance test\n" + "args[4]: runtime thread num\n" + "args[5]: runtime thread bind mode\n\n"); +} + +int main(int argc, const char **argv) { + if (argc < 2) { + std::cout << "input command is invalid\n" << std::endl; + usage(); + return lite::RET_ERROR; + } + std::cout << "start run benchmark" << std::endl; + + const char *model_buffer = nullptr; + int model_size = 0; + // read .net file by ReadBinaryFile; + if (argc >= 3) { + model_buffer = static_cast(ReadInputData(argv[2], &model_size)); + } + session::LiteSession *session = mindspore::session::LiteSession::CreateSession(model_buffer, model_size, nullptr); + if (session == nullptr) { + std::cerr << "create lite session failed" << std::endl; + return lite::RET_ERROR; + } + + // set model inputs tensor data + std::vector inputs = session->GetInputs(); + size_t inputs_num = inputs.size(); + void *inputs_binbuf[inputs_num]; + int inputs_size[inputs_num]; + for (size_t i = 0; i < inputs_num; ++i) { + inputs_size[i] = inputs[i]->Size(); + } + int ret = ReadInputsFile(argv[1], inputs_binbuf, inputs_size, inputs_num); + if (ret != lite::RET_OK) { + return lite::RET_ERROR; + } + for (size_t i = 0; i < inputs_num; ++i) { + inputs[i]->set_data(inputs_binbuf[i]); + } + + ret = session->RunGraph(); + if (ret != lite::RET_OK) { + return lite::RET_ERROR; + } + + auto outputs = session->GetOutputs(); + std::cout << outputs.size() << std::endl; + + std::cout << "run benchmark success" << std::endl; + 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/benchmark.h b/mindspore/lite/micro/coder/generator/component/const_blocks/benchmark.h new file mode 100644 index 0000000000..c91ecaded9 --- /dev/null +++ b/mindspore/lite/micro/coder/generator/component/const_blocks/benchmark.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_BENCHMARK_H_ +#define MINDSPORE_LITE_MICRO_GENERATOR_CONST_BLOCK_BENCHMARK_H_ + +namespace mindspore::lite::micro { + +extern const char *benchmark_source; + +} // namespace mindspore::lite::micro + +#endif // MINDSPORE_LITE_MICRO_GENERATOR_CONST_BLOCK_BENCHMARK_H_ diff --git a/mindspore/lite/micro/coder/generator/component/const_blocks/mtensor.cc b/mindspore/lite/micro/coder/generator/component/const_blocks/mtensor.cc new file mode 100644 index 0000000000..247ae8cf6e --- /dev/null +++ b/mindspore/lite/micro/coder/generator/component/const_blocks/mtensor.cc @@ -0,0 +1,172 @@ +/** + * 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/mtensor.h" + +namespace mindspore::lite::micro { + +const char *tensor_header = R"RAW( +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MINDSPORE_LITE_MICRO_LIBRARY_SOURCE_TENSOR_H_ +#define MINDSPORE_LITE_MICRO_LIBRARY_SOURCE_TENSOR_H_ + +#include "include/ms_tensor.h" +#include +#include + +namespace mindspore { +namespace lite { +struct QuantArg { + double scale; + int32_t zeroPoint; + float var_corr{1}; + float mean_corr{0}; + bool inited; + std::vector clusters{}; + int bitNum; + int roundType; + int multiplier; + int dstDtype; +}; + +class MTensor : public mindspore::tensor::MSTensor { + public: + MTensor() = default; + MTensor(std::string name, enum TypeId type, std::vector shape, void *data) + : tensor_name_(std::move(name)), data_type_(type), shape_(std::move(shape)), data_(data) {} + ~MTensor() override = default; + + TypeId data_type() const override { return data_type_; } + std::vector shape() const override { return shape_; } + int DimensionSize(size_t index) const override; + int ElementsNum() const override; + size_t Size() const override; + void *MutableData() override { return data_; }; + std::string tensor_name() const override { return tensor_name_; } + void set_tensor_name(const std::string name) override { tensor_name_ = name; } + void set_data(void *data) override { data_ = data; } + + private: + std::string tensor_name_; + TypeId data_type_; + std::vector shape_; + void *data_ = nullptr; + std::vector quant_params_; +}; + +} // namespace lite +} // namespace mindspore + +#endif // MINDSPORE_LITE_MICRO_LIBRARY_SOURCE_TENSOR_H_ + +)RAW"; + +const char *tensor_source = R"RAW( +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "tensor.h" + +namespace mindspore { +namespace lite { +size_t DataTypeSize(const TypeId type) { + switch (type) { + case kNumberTypeFloat64: + return sizeof(double); + case kNumberTypeFloat: + case kNumberTypeFloat32: + return sizeof(float); + case kNumberTypeInt8: + return sizeof(int8_t); + case kNumberTypeUInt8: + return sizeof(uint8_t); + case kNumberTypeFloat16: + case kNumberTypeInt16: + return sizeof(int16_t); + case kNumberTypeInt32: + return sizeof(int32_t); + case kNumberTypeInt64: + return sizeof(int64_t); + case kNumberTypeUInt16: + return sizeof(uint16_t); + case kNumberTypeUInt32: + return sizeof(uint32_t); + case kNumberTypeUInt64: + return sizeof(uint64_t); + case kNumberTypeBool: + return sizeof(bool); + case kObjectTypeString: + return sizeof(char); + case kObjectTypeTensorType: + default: + return 0; + } +} + +int MTensor::DimensionSize(const size_t index) const { + int dim_size = -1; + if (index < shape_.size()) { + dim_size = shape_[index]; + } + return dim_size; +} + +int MTensor::ElementsNum() const { + int elements = 1; + for (int i : shape_) { + elements *= i; + } + return elements; +} + +size_t MTensor::Size() const { + size_t element_size = DataTypeSize(data_type_); + return element_size * ElementsNum(); +} +} // namespace lite +} // namespace mindspore + +)RAW"; + +} // namespace mindspore::lite::micro diff --git a/mindspore/lite/micro/coder/generator/component/const_blocks/mtensor.h b/mindspore/lite/micro/coder/generator/component/const_blocks/mtensor.h new file mode 100644 index 0000000000..b2d8349c56 --- /dev/null +++ b/mindspore/lite/micro/coder/generator/component/const_blocks/mtensor.h @@ -0,0 +1,27 @@ +/** + * 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_MTENSOR_H_ +#define MINDSPORE_LITE_MICRO_GENERATOR_CONST_BLOCK_MTENSOR_H_ + +namespace mindspore::lite::micro { + +extern const char *tensor_header; +extern const char *tensor_source; + +} // namespace mindspore::lite::micro + +#endif // MINDSPORE_LITE_MICRO_GENERATOR_CONST_BLOCK_MTENSOR_H_