mindspore lite: add mindir model support for cloud infer

This commit is contained in:
liu lili 2022-05-07 12:13:00 +08:00
parent acd260b23f
commit d85f45c43d
30 changed files with 1789 additions and 56 deletions

View File

@ -846,6 +846,11 @@ else()
COMPONENT ${RUNTIME_COMPONENT_NAME})
install(FILES ${TOP_DIR}/mindspore/lite/build/src/${MINDSPORE_LITE_LIB_NAME}.a DESTINATION ${RUNTIME_LIB_DIR}
COMPONENT ${RUNTIME_COMPONENT_NAME})
if(MSLITE_ENABLE_CLOUD_FUSION_INFERENCE)
install(FILES ${glog_LIBPATH}/libglog.so.0.4.0 DESTINATION ${RUNTIME_LIB_DIR} RENAME libglog.so.0
COMPONENT ${RUNTIME_COMPONENT_NAME})
install(TARGETS mindspore_core DESTINATION ${RUNTIME_LIB_DIR} COMPONENT ${RUNTIME_COMPONENT_NAME})
endif()
if(ENABLE_MODEL_OBF)
install(FILES ${TOP_DIR}/mindspore/lite/tools/obfuscator/bin/linux-x64/msobfuscator
DESTINATION ${OBFUSCATOR_ROOT_DIR} PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ

View File

@ -57,6 +57,7 @@ option(MSLITE_ENABLE_GRAPH_KERNEL "enable graph kernel" off)
option(MSLITE_ENABLE_CONVERT_PYTORCH_MODEL "enable to convert pytorch model" off)
option(MSLITE_ENABLE_KERNEL_EXECUTOR "enable kernel executor" off)
option(MSLITE_ENABLE_GITEE_MIRROR "enable download third_party from gitee mirror" off)
option(MSLITE_ENABLE_CLOUD_FUSION_INFERENCE "enable cloud and device fusion inference architecture" off)
#Option that can be configured through manually
option(ENABLE_VERBOSE "" off)
@ -198,6 +199,10 @@ if(MSLITE_ENABLE_GITEE_MIRROR)
set(ENABLE_GITEE ON)
endif()
if(DEFINED ENV{MSLITE_ENABLE_CLOUD_FUSION_INFERENCE})
set(MSLITE_ENABLE_CLOUD_FUSION_INFERENCE $ENV{MSLITE_ENABLE_CLOUD_FUSION_INFERENCE})
endif()
if(MACHINE_LINUX_ARM64)
add_compile_definitions(MACHINE_LINUX_ARM64)
add_compile_definitions(LINUX_RUNTIME)
@ -331,6 +336,10 @@ if(MSLITE_ENABLE_RUNTIME_CONVERT)
set(MSLITE_ENABLE_CONVERTER on)
endif()
if(MSLITE_ENABLE_CLOUD_FUSION_INFERENCE)
set(MSLITE_ENABLE_RUNTIME_GLOG on)
endif()
if(MSLITE_ENABLE_TRAIN)
set(SUPPORT_TRAIN on)
if(NOT MSLITE_MINDDATA_IMPLEMENT STREQUAL "off" OR NOT PLATFORM_ARM)
@ -411,6 +420,7 @@ message(STATUS "\tMSLITE_ENABLE_SHARING_MODEL_WEIGHT = \t${MSLITE_ENABLE
message(STATUS "\tMSLITE_ENABLE_EXPERIMENTAL_KERNEL = \t${MSLITE_ENABLE_EXPERIMENTAL_KERNEL}")
message(STATUS "\tMSLITE_ENABLE_GRAPH_KERNEL = \t${MSLITE_ENABLE_GRAPH_KERNEL}")
message(STATUS "\tMSLITE_ENABLE_KERNEL_EXECUTOR = \t${MSLITE_ENABLE_KERNEL_EXECUTOR}")
message(STATUS "\tMSLITE_ENABLE_CLOUD_FUSION_INFERENCE = \t${MSLITE_ENABLE_CLOUD_FUSION_INFERENCE}")
if((MSLITE_ENABLE_CONVERTER OR MSLITE_ENABLE_TESTCASES) AND (
NOT MSLITE_ENABLE_MINDRT

View File

@ -16,14 +16,24 @@
#ifndef MINDSPORE_LITE_INCLUDE_MODEL_H_
#define MINDSPORE_LITE_INCLUDE_MODEL_H_
#ifdef ENABLE_CLOUD_FUSION_INFERENCE
#include <any>
#endif
#include "include/lite_utils.h"
namespace mindspore::lite {
typedef enum { ModelType_MSLite, ModelType_MindIR } LiteModelType;
struct MS_API Model {
struct Node {
String name_;
String op_type_;
int node_type_;
const void *primitive_ = nullptr;
#ifdef ENABLE_CLOUD_FUSION_INFERENCE
std::any base_operators_ = nullptr;
#endif
Uint32Vector input_indices_;
Uint32Vector output_indices_;
int quant_type_;
@ -45,6 +55,7 @@ struct MS_API Model {
TensorPtrVector all_tensors_;
NodePtrVector all_nodes_;
char *buf = nullptr;
size_t buf_size_ = 0;
SubGraphPtrVector sub_graphs_;
#ifdef ENABLE_MODEL_OBF
using NodeStatVector = Vector<uint32_t>;
@ -55,6 +66,7 @@ struct MS_API Model {
bool model_obfuscated_ = false;
PrimVector deobf_prims_;
#endif
LiteModelType model_type_ = mindspore::lite::ModelType_MSLite;
/// \brief Static method to create a Model pointer.
static Model *Import(const char *model_buf, size_t size);

View File

@ -140,6 +140,44 @@ set(LITE_SRC
${CMAKE_CURRENT_SOURCE_DIR}/control_flow/control_subgraph_creator.cc
)
set(MODEL_LOADER_FRAMEWORK_SRC
${MODEL_LOADER_FRAMEWORK_SRC}
${CMAKE_CURRENT_SOURCE_DIR}/model_loader/model_loader.cc
)
if(MSLITE_ENABLE_CLOUD_FUSION_INFERENCE)
add_compile_definitions(ENABLE_CLOUD_FUSION_INFERENCE)
string(REPLACE "-Werror" "" CMAKE_C_FLAGS ${CMAKE_C_FLAGS})
string(REPLACE "-Werror" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-error=return-type")
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=return-type")
set(MINDIR_MODEL_SRC
${MINDIR_MODEL_SRC}
${CMAKE_CURRENT_SOURCE_DIR}/model_loader/mindir_model/mindir_model.cc
${CMAKE_CURRENT_SOURCE_DIR}/model_loader/mindir_model/mindir_model_util.cc
${CMAKE_CURRENT_SOURCE_DIR}/model_loader/mindir_model/mindir_model_convertor.cc
${CMAKE_CURRENT_SOURCE_DIR}/model_loader/mindir_model/mindir_model_loader.cc
${CMAKE_CURRENT_SOURCE_DIR}/model_loader/mindir_model/kernel_mod_util.cc
)
set(MINDIR_KERNEL_SRC
${MINDIR_KERNEL_SRC}
${CMAKE_CURRENT_SOURCE_DIR}/model_loader/mindir_model/inner_kernel.cc)
set(LITE_SRC
${LITE_SRC}
${CMAKE_CURRENT_SOURCE_DIR}/model_loader/mindir_model/less_test_kernel_mod.cc)
endif()
set(LITE_SRC
${LITE_SRC}
${MODEL_LOADER_FRAMEWORK_SRC}
${MINDIR_MODEL_SRC}
${MINDIR_KERNEL_SRC}
)
if(MSLITE_ENABLE_MODEL_ENCRYPTION)
set(LITE_SRC
${LITE_SRC}
@ -422,8 +460,13 @@ if(MSVC)
set_target_properties(mindspore-lite_static PROPERTIES PREFIX lib)
endif()
if(MSLITE_ENABLE_CLOUD_FUSION_INFERENCE)
target_link_libraries(mindspore-lite cpu_kernel_mid nnacl_mid cpu_ops_mid mindspore_core mindspore::protobuf)
target_link_libraries(mindspore-lite_static cpu_kernel_mid nnacl_mid cpu_ops_mid mindspore_core mindspore::protobuf)
else()
target_link_libraries(mindspore-lite cpu_kernel_mid nnacl_mid cpu_ops_mid)
target_link_libraries(mindspore-lite_static cpu_kernel_mid nnacl_mid cpu_ops_mid)
endif()
if(SUPPORT_TRAIN)
target_link_libraries(mindspore-lite train_cpu_kernel_mid)

View File

@ -14,8 +14,8 @@
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_SRC_LITE_KERNEL_H_
#define MINDSPORE_LITE_SRC_LITE_KERNEL_H_
#ifndef MINDSPORE_LITE_SRC_KERNEL_EXEC_H_
#define MINDSPORE_LITE_SRC_KERNEL_EXEC_H_
#include <string>
#include <vector>
#include <memory>
@ -36,6 +36,9 @@
#include "src/cxx_api/tensor/tensor_impl.h"
#include "src/lite_kernel.h"
#include "include/api/delegate.h"
#include "model_loader/abstract_kernel.h"
using mindspore::infer::Abstractkernel;
namespace mindspore::kernel {
enum KERNEL_ARCH { kCPU, kGPU, kAPU, kNPU, kCustom, kDelegate, kKernelArch_MIN = kCPU, kKernelArch_MAX = kAPU };
@ -138,7 +141,7 @@ class KernelExec {
virtual int Train() {
MS_ASSERT(kernel_ != nullptr);
if (desc_.provider == kBuiltin) {
return std::static_pointer_cast<LiteKernel>(kernel_)->Train();
return std::static_pointer_cast<Abstractkernel>(kernel_)->Train();
}
return mindspore::lite::RET_OK;
}
@ -146,7 +149,7 @@ class KernelExec {
virtual bool IsTrain() const {
MS_ASSERT(kernel_ != nullptr);
if (desc_.provider == kBuiltin) {
return std::static_pointer_cast<LiteKernel>(kernel_)->IsTrain();
return std::static_pointer_cast<Abstractkernel>(kernel_)->IsTrain();
}
return false;
}
@ -154,7 +157,7 @@ class KernelExec {
virtual int Eval() {
MS_ASSERT(kernel_ != nullptr);
if (desc_.provider == kBuiltin) {
return std::static_pointer_cast<LiteKernel>(kernel_)->Eval();
return std::static_pointer_cast<Abstractkernel>(kernel_)->Eval();
}
return mindspore::lite::RET_OK;
}
@ -162,7 +165,7 @@ class KernelExec {
virtual bool IsEval() const {
MS_ASSERT(kernel_ != nullptr);
if (desc_.provider == kBuiltin) {
return std::static_pointer_cast<LiteKernel>(kernel_)->IsEval();
return std::static_pointer_cast<Abstractkernel>(kernel_)->IsEval();
}
return false;
}
@ -170,14 +173,14 @@ class KernelExec {
virtual void SetTrainable(bool trainable = true) {
MS_ASSERT(kernel_ != nullptr);
if (desc_.provider == kBuiltin) {
std::static_pointer_cast<LiteKernel>(kernel_)->SetTrainable(trainable);
std::static_pointer_cast<Abstractkernel>(kernel_)->SetTrainable(trainable);
}
}
virtual bool IsTrainable() const {
MS_ASSERT(kernel_ != nullptr);
if (desc_.provider == kBuiltin) {
return std::static_pointer_cast<LiteKernel>(kernel_)->IsTrainable();
return std::static_pointer_cast<Abstractkernel>(kernel_)->IsTrainable();
}
return false;
}
@ -206,7 +209,7 @@ class KernelExec {
void set_in_tensors(const std::vector<lite::Tensor *> &in_tensors) {
MS_ASSERT(kernel_ != nullptr);
if (desc_.provider == kBuiltin) {
std::static_pointer_cast<LiteKernel>(kernel_)->set_in_tensors(in_tensors);
std::static_pointer_cast<Abstractkernel>(kernel_)->set_in_tensors(in_tensors);
} else {
std::vector<MSTensor> tensors_in;
std::transform(in_tensors.begin(), in_tensors.end(), std::back_inserter(tensors_in), [](lite::Tensor *tensor) {
@ -220,7 +223,7 @@ class KernelExec {
void set_in_tensor(lite::Tensor *in_tensor, size_t index) {
MS_ASSERT(kernel_ != nullptr);
if (desc_.provider == kBuiltin) {
std::static_pointer_cast<LiteKernel>(kernel_)->set_in_tensor(in_tensor, index);
std::static_pointer_cast<Abstractkernel>(kernel_)->set_in_tensor(in_tensor, index);
} else {
MS_ASSERT(index < kernel_->inputs().size());
auto impl = std::make_shared<mindspore::LiteTensorImpl>(in_tensor);
@ -232,7 +235,7 @@ class KernelExec {
void set_out_tensors(const std::vector<lite::Tensor *> &out_tensors) {
MS_ASSERT(kernel_ != nullptr);
if (desc_.provider == kBuiltin) {
std::static_pointer_cast<LiteKernel>(kernel_)->set_out_tensors(out_tensors);
std::static_pointer_cast<Abstractkernel>(kernel_)->set_out_tensors(out_tensors);
} else {
std::vector<MSTensor> tensors_out;
std::transform(out_tensors.begin(), out_tensors.end(), std::back_inserter(tensors_out), [](lite::Tensor *tensor) {
@ -246,7 +249,7 @@ class KernelExec {
virtual void set_out_tensor(lite::Tensor *out_tensor, size_t index) {
MS_ASSERT(kernel_ != nullptr);
if (desc_.provider == kBuiltin) {
std::static_pointer_cast<LiteKernel>(kernel_)->set_out_tensor(out_tensor, index);
std::static_pointer_cast<Abstractkernel>(kernel_)->set_out_tensor(out_tensor, index);
} else {
MS_ASSERT(index < kernel_->outputs().size());
auto impl = std::make_shared<mindspore::LiteTensorImpl>(out_tensor);
@ -258,7 +261,7 @@ class KernelExec {
const std::vector<lite::Tensor *> &in_tensors() const {
MS_ASSERT(kernel_ != nullptr);
if (desc_.provider == kBuiltin) {
return std::static_pointer_cast<LiteKernel>(kernel_)->in_tensors();
return std::static_pointer_cast<Abstractkernel>(kernel_)->in_tensors();
} else {
auto &ms_tensors = kernel_->inputs();
mutable_in_tensors_.resize(ms_tensors.size());
@ -278,7 +281,7 @@ class KernelExec {
const std::vector<lite::Tensor *> &out_tensors() const {
MS_ASSERT(kernel_ != nullptr);
if (desc_.provider == kBuiltin) {
return std::static_pointer_cast<LiteKernel>(kernel_)->out_tensors();
return std::static_pointer_cast<Abstractkernel>(kernel_)->out_tensors();
} else {
auto &ms_tensors = kernel_->outputs();
mutable_out_tensors_.resize(ms_tensors.size());
@ -384,9 +387,8 @@ typedef LiteKernel *(*KernelCreator)(const std::vector<lite::Tensor *> &inputs,
const lite::Context *ctx, const KernelKey &desc);
template <class T>
kernel::LiteKernel *LiteKernelCreator(const std::vector<lite::Tensor *> &inputs,
const std::vector<lite::Tensor *> &outputs, OpParameter *parameter,
const lite::Context *ctx, const kernel::KernelKey &desc) {
LiteKernel *LiteKernelCreator(const std::vector<lite::Tensor *> &inputs, const std::vector<lite::Tensor *> &outputs,
OpParameter *parameter, const lite::Context *ctx, const kernel::KernelKey &desc) {
if (parameter == nullptr) {
MS_LOG(ERROR) << "parameter is nullptr.";
return nullptr;
@ -401,4 +403,4 @@ kernel::LiteKernel *LiteKernelCreator(const std::vector<lite::Tensor *> &inputs,
}
} // namespace mindspore::kernel
#endif // MINDSPORE_LITE_SRC_LITE_KERNEL_H_
#endif // MINDSPORE_LITE_SRC_KERNEL_EXEC_H_

View File

@ -14,8 +14,8 @@
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_SRC_INNER_KERNEL_H_
#define MINDSPORE_LITE_SRC_INNER_KERNEL_H_
#ifndef MINDSPORE_LITE_SRC_LITE_KERNEL_H_
#define MINDSPORE_LITE_SRC_LITE_KERNEL_H_
#include <string>
#include <vector>
#include <memory>
@ -32,9 +32,12 @@
#include "include/api/context.h"
#include "include/api/kernel.h"
#include "src/thread_cost_model.h"
#include "model_loader/abstract_kernel.h"
using mindspore::infer::Abstractkernel;
namespace mindspore::kernel {
class LiteKernel : public Kernel {
class LiteKernel : public Abstractkernel {
public:
LiteKernel() = default;
@ -129,9 +132,9 @@ class LiteKernel : public Kernel {
return outputs_;
}
void set_in_tensors(const std::vector<lite::Tensor *> &in_tensors) { this->in_tensors_ = in_tensors; }
void set_in_tensors(const std::vector<lite::Tensor *> &in_tensors) override { this->in_tensors_ = in_tensors; }
virtual void set_in_tensor(lite::Tensor *in_tensor, size_t index) {
void set_in_tensor(lite::Tensor *in_tensor, size_t index) override {
if (index >= in_tensors_.size()) {
MS_LOG(ERROR) << "index: " << index << " larger than in_tensors size: " << in_tensors_.size();
return;
@ -139,9 +142,9 @@ class LiteKernel : public Kernel {
this->in_tensors_[index] = in_tensor;
}
void set_out_tensors(const std::vector<lite::Tensor *> &out_tensors) { this->out_tensors_ = out_tensors; }
void set_out_tensors(const std::vector<lite::Tensor *> &out_tensors) override { this->out_tensors_ = out_tensors; }
virtual void set_out_tensor(lite::Tensor *out_tensor, size_t index) {
void set_out_tensor(lite::Tensor *out_tensor, size_t index) override {
if (index >= out_tensors_.size()) {
MS_LOG(ERROR) << "index: " << index << " larger than out_tensors size: " << out_tensors_.size();
return;
@ -149,29 +152,29 @@ class LiteKernel : public Kernel {
this->out_tensors_[index] = out_tensor;
}
const std::vector<lite::Tensor *> &in_tensors() const { return in_tensors_; }
const std::vector<lite::Tensor *> &in_tensors() const override { return in_tensors_; }
const std::vector<lite::Tensor *> &out_tensors() const { return out_tensors_; }
const std::vector<lite::Tensor *> &out_tensors() const override { return out_tensors_; }
virtual int Train() {
int Train() override {
this->train_mode_ = true;
return mindspore::lite::RET_OK;
}
virtual bool IsTrain() const { return this->train_mode_; }
bool IsTrain() const override { return this->train_mode_; }
virtual int Eval() {
int Eval() override {
this->train_mode_ = false;
return mindspore::lite::RET_OK;
}
virtual int SetupVirtualBatch(int virtual_batch_multiplier, int param) { return mindspore::lite::RET_OK; }
virtual bool IsEval() const { return !this->train_mode_; }
bool IsEval() const override { return !this->train_mode_; }
virtual void SetTrainable(bool trainable = true) { this->trainable_ = trainable; }
void SetTrainable(bool trainable = true) override { this->trainable_ = trainable; }
virtual bool IsTrainable() const { return this->trainable_; }
bool IsTrainable() const override { return this->trainable_; }
TypeId registry_data_type(void) const { return registry_data_type_; }
@ -206,4 +209,4 @@ class LiteKernel : public Kernel {
};
} // namespace mindspore::kernel
#endif // MINDSPORE_LITE_SRC_INNER_KERNEL_H_
#endif // MINDSPORE_LITE_SRC_LITE_KERNEL_H_

View File

@ -28,6 +28,7 @@
#include "src/common/graph_util.h"
#include "src/common/file_utils.h"
#include "src/tensor.h"
#include "model_loader/model_loader.h"
namespace mindspore::lite {
namespace {
@ -501,7 +502,18 @@ bool LiteModel::CheckQuantAllInit(
Model *ImportFromPath(const char *model_path) { return LiteImportFromPath(model_path); }
Model *ImportFromBuffer(const char *model_buf, size_t size, bool take_buf) {
Model *ImportFromBuffer(const char *model_buf, size_t size, bool take_buf, mindspore::ModelType model_type) {
auto model_loader = mindspore::infer::ModelLoaderRegistry::GetInstance()->GetModelLoader(model_type);
if (model_loader != nullptr) {
MS_LOG(INFO) << "import model from model loader";
// return nullptr;
auto model = model_loader->ImportModel(model_buf, size, true);
if (model != nullptr) {
return model;
}
}
MS_LOG(INFO) << "import model from lite model";
auto *model = new (std::nothrow) LiteModel();
if (model == nullptr) {
MS_LOG(ERROR) << "new model fail!";

View File

@ -33,6 +33,7 @@
#ifdef ENABLE_MODEL_OBF
#include "tools/obfuscator/include/deobfuscator.h"
#endif
#include "include/api/types.h"
namespace mindspore {
namespace lite {
@ -319,7 +320,8 @@ class LiteModel : public Model {
const std::string model_path_;
};
Model *ImportFromBuffer(const char *model_buf, size_t size, bool take_buf);
Model *ImportFromBuffer(const char *model_buf, size_t size, bool take_buf,
mindspore::ModelType model_type = mindspore::ModelType::kMindIR_Lite);
LiteModel *LiteImportFromPath(const char *model_path);
Model *ImportFromPath(const char *model_path);
} // namespace lite

View File

@ -53,6 +53,10 @@
#include "src/delegate/tensorrt/tensorrt_delegate.h"
#endif
#include "src/runtime/runtime_convert.h"
#include "model_loader/model_loader.h"
using AbstractBaseModel = mindspore::infer::AbstractBaseModel;
namespace mindspore {
#ifdef USE_GLOG
extern "C" {
@ -527,7 +531,12 @@ int LiteSession::CompileGraph(Model *model) {
return ret;
}
if (model->model_type_ != ModelType_MSLite) {
ret = reinterpret_cast<AbstractBaseModel *>(model)->ConvertTensors(&this->tensors_);
} else {
// TODO(liulili): Convert to abstract base model interface
ret = ConvertTensors(model);
}
if (ret != RET_OK) {
MS_LOG(ERROR) << "ConvertTensors failed: " << ret;
is_running_.store(false);
@ -1227,10 +1236,19 @@ int LiteSession::PreCheck(Model *model) {
MS_LOG(ERROR) << "The input model buf is nullptr.";
return RET_PARAM_INVALID;
}
if (model->model_type_ != ModelType_MSLite) {
// abstract base model
if (!reinterpret_cast<AbstractBaseModel *>(model)->ModelVerify()) {
MS_LOG(ERROR) << "wrong model input, please check";
return RET_ERROR;
}
} else {
// TODO(liulili): old routine, convert to abstract base model
if (!reinterpret_cast<LiteModel *>(model)->ModelVerify()) {
MS_LOG(ERROR) << "wrong model input, please check";
return RET_ERROR;
}
}
#ifndef ENABLE_FP16
if (context_->GetDeviceInfo(DT_CPU).cpu_device_info_.enable_float16_) {
@ -1612,7 +1630,11 @@ mindspore::ModelType lite::LiteSession::LoadModelByBuff(const char *model_buf, c
#ifdef RUNTIME_CONVERT
*lite_buf = RuntimeConvert(model_buf, buf_size, size, ms_context);
#else
MS_LOG(ERROR) << "Please enable runtime convert.";
MS_LOG(WARNING) << "Please enable runtime convert.";
#endif
#ifdef ENABLE_CLOUD_FUSION_INFERENCE
*size = buf_size;
*lite_buf = const_cast<char *>(model_buf);
#endif
return mindspore::ModelType::kMindIR;
}
@ -1651,10 +1673,7 @@ const char *lite::LiteSession::LoadModelByPath(const std::string &file, mindspor
if (buf_model_type == mindspore::ModelType::kUnknownType || lite_buf == nullptr) {
return nullptr;
}
if (buf_model_type == mindspore::ModelType::kMindIR) {
delete[] model_buf;
model_buf = nullptr;
}
return lite_buf;
}
@ -1667,7 +1686,19 @@ int lite::LiteSession::LoadModelAndCompileByBuf(const char *model_buf, mindspore
MS_LOG(ERROR) << "Invalid model_buf";
return RET_ERROR;
}
auto *model = lite::ImportFromBuffer(lite_buf, lite_buf_size, true);
mindspore::lite::Model *model = nullptr;
// if (model_type == kMindIR_Lite) {
model = lite::ImportFromBuffer(lite_buf, lite_buf_size, true, model_type);
// } else {
// auto *model_loader = ModelLoaderRegistry::GetInstance()->GetModelLoader(model_type);
// if (model_loader == nullptr) {
// MS_LOG(ERROR) << "Invalid model type";
// return RET_ERROR;
// }
// model_loader->ImportModel(model_buf, buf_size, true);
// }
// auto *model = lite::ImportFromBuffer(lite_buf, lite_buf_size, true);
if (model == nullptr) {
MS_LOG(ERROR) << "Import model failed";
return RET_ERROR;
@ -1697,7 +1728,7 @@ int lite::LiteSession::LoadModelAndCompileByBuf(const char *model_buf, mindspore
MS_LOG(ERROR) << "Invalid model_buf";
return RET_ERROR;
}
auto *model = lite::ImportFromBuffer(lite_buf, lite_buf_size, true);
auto *model = lite::ImportFromBuffer(lite_buf, lite_buf_size, true, model_type);
if (model == nullptr) {
MS_LOG(ERROR) << "Import model failed";
return RET_ERROR;
@ -1724,7 +1755,7 @@ int lite::LiteSession::LoadModelAndCompileByPath(const std::string &model_path,
MS_LOG(ERROR) << "Read model file failed";
return RET_ERROR;
}
auto *model = lite::ImportFromBuffer(model_buf, model_size, true);
auto *model = lite::ImportFromBuffer(model_buf, model_size, true, model_type);
if (model == nullptr) {
MS_LOG(ERROR) << "Import model failed";
return RET_ERROR;
@ -1749,7 +1780,7 @@ int lite::LiteSession::LoadModelAndCompileByPath(const std::string &model_path,
MS_LOG(ERROR) << "Read model file failed";
return RET_ERROR;
}
auto *model = lite::ImportFromBuffer(model_buf, model_size, true);
auto *model = lite::ImportFromBuffer(model_buf, model_size, true, model_type);
if (model == nullptr) {
MS_LOG(ERROR) << "Import model failed";
delete[] model_buf;

View File

@ -0,0 +1,43 @@
/**
* Copyright 2021-2022 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_SRC_MODEL_LOADER_ABSTRACT_BASE_MODEL_H_
#define MINDSPORE_LITE_SRC_MODEL_LOADER_ABSTRACT_BASE_MODEL_H_
#include <string>
#include <vector>
#include "include/model.h"
#include "src/tensor.h"
#include "src/kernel_exec.h"
using Model = mindspore::lite::Model;
namespace mindspore::infer {
class AbstractBaseModel : public Model {
public:
virtual bool ModelVerify() const = 0;
// virtual SchemaTensorWrapper *GetSchemaTensor(const size_t &tensor_index) const = 0;
virtual int ConvertTensors(std::vector<mindspore::lite::Tensor *> *lite_tensors) = 0;
virtual std::string GetModelPath() const = 0;
virtual mindspore::kernel::KernelExec *FindBackendKernel(const std::vector<mindspore::lite::Tensor *> &in_tensors,
const std::vector<mindspore::lite::Tensor *> &out_tensors,
const Model::Node *node, lite::Context *context,
TypeId prefer_data_type) = 0;
};
} // namespace mindspore::infer
#endif // MINDSPORE_LITE_SRC_MODEL_LOADER_ABSTRACT_BASE_MODEL_H_

View File

@ -0,0 +1,57 @@
/**
* Copyright 2021-2022 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_SRC_MODEL_LOADER_ABSTRACT_KERNEL_H_
#define MINDSPORE_LITE_SRC_MODEL_LOADER_ABSTRACT_KERNEL_H_
#include <vector>
#include "include/api/kernel.h"
using mindspore::kernel::Kernel;
namespace mindspore::infer {
class Abstractkernel : public Kernel {
public:
// virtual OpParameter *op_parameter() const = 0;
virtual int Train() = 0;
virtual bool IsTrain() const = 0;
virtual int Eval() = 0;
virtual bool IsEval() const = 0;
virtual void SetTrainable(bool trainable = true) = 0;
virtual bool IsTrainable() const = 0;
virtual void set_in_tensors(const std::vector<lite::Tensor *> &in_tensors) = 0;
virtual void set_in_tensor(lite::Tensor *in_tensor, size_t index) = 0;
virtual void set_out_tensors(const std::vector<lite::Tensor *> &out_tensors) = 0;
virtual void set_out_tensor(lite::Tensor *out_tensor, size_t index) = 0;
virtual const std::vector<lite::Tensor *> &in_tensors() const = 0;
virtual const std::vector<lite::Tensor *> &out_tensors() const = 0;
};
} // namespace mindspore::infer
#endif // MINDSPORE_LITE_SRC_MODEL_LOADER_ABSTRACT_KERNEL_H_

View File

@ -0,0 +1,20 @@
/**
* Copyright 2021-2022 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_SRC_MODEL_LOADER_ABSTRACT_TENSOR_H_
#define MINDSPORE_LITE_SRC_MODEL_LOADER_ABSTRACT_TENSOR_H_
#endif // MINDSPORE_LITE_SRC_MODEL_LOADER_ABSTRACT_TENSOR_H_

View File

@ -0,0 +1,101 @@
/**
* Copyright 2021-2022 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 <vector>
#include <memory>
#include "model_loader/mindir_model/inner_kernel.h"
#include "abstract/abstract_value.h"
namespace mindspore::kernel {
int InnerKernel::Prepare() {
auto inputs = LiteTensorToKernelTensorPtrVec(this->in_tensors_);
auto outputs = LiteTensorToKernelTensorPtrVec(this->out_tensors_);
return this->kernel_mod_->Init(this->base_operator_, inputs, outputs) ? mindspore::lite::RET_OK
: mindspore::lite::RET_ERROR;
}
int InnerKernel::Execute() {
auto inputs = LiteTensorToAddressPtrVec(this->in_tensors_);
auto outputs = LiteTensorToAddressPtrVec(this->out_tensors_);
std::vector<AddressPtr> workspace;
return this->kernel_mod_->Launch(inputs, workspace, outputs, nullptr) ? mindspore::lite::RET_OK
: mindspore::lite::RET_ERROR;
}
int InnerKernel::ReSize() {
// TODO(liulili): use InitOp instead
auto inputs = LiteTensorToKernelTensorPtrVec(this->in_tensors_);
auto outputs = LiteTensorToKernelTensorPtrVec(this->out_tensors_);
return this->kernel_mod_->Init(this->base_operator_, inputs, outputs) ? mindspore::lite::RET_OK
: mindspore::lite::RET_ERROR;
}
std::vector<KernelTensorPtr> InnerKernel::LiteTensorToKernelTensorPtrVec(
const std::vector<lite::Tensor *> &lite_tensors) {
std::vector<KernelTensorPtr> ret_vec;
for (auto lite_tensor : lite_tensors) {
auto kernel_tensor_ptr = LiteTensorToKernelTensorPtr(lite_tensor);
ret_vec.push_back(kernel_tensor_ptr);
}
return ret_vec;
}
KernelTensorPtr InnerKernel::LiteTensorToKernelTensorPtr(lite::Tensor *lite_tensor) {
KernelTensorPtr kernel_tensor_ptr = std::make_shared<mindspore::kernel::KernelTensor>();
auto address_ptr = LiteTensorToAddressPtr(lite_tensor);
kernel_tensor_ptr->SetData(address_ptr);
kernel_tensor_ptr->SetFormat(lite_tensor->format());
auto kernel_tensor_abstract_ptr = std::make_shared<mindspore::abstract::AbstractScalar>();
auto type_ptr = mindspore::TypeIdToType(lite_tensor->data_type());
kernel_tensor_abstract_ptr->set_type(type_ptr);
auto lite_shape = lite_tensor->shape();
std::vector<int64_t> shape;
for (size_t i = 0; i < lite_shape.size(); i++) {
shape.push_back(lite_shape[i]);
}
kernel_tensor_abstract_ptr->set_shape(std::make_shared<abstract::Shape>(shape));
kernel_tensor_ptr->SetAbstract(kernel_tensor_abstract_ptr);
return kernel_tensor_ptr;
}
std::vector<AddressPtr> InnerKernel::LiteTensorToAddressPtrVec(const std::vector<lite::Tensor *> &lite_tensors) {
std::vector<AddressPtr> ret_vec;
for (auto lite_tensor : lite_tensors) {
auto address_ptr = LiteTensorToAddressPtr(lite_tensor);
ret_vec.push_back(address_ptr);
}
return ret_vec;
}
AddressPtr InnerKernel::LiteTensorToAddressPtr(lite::Tensor *lite_tensor) {
AddressPtr address_ptr = std::make_shared<mindspore::kernel::Address>();
address_ptr->addr = lite_tensor->data();
address_ptr->size = lite_tensor->Size();
return address_ptr;
}
} // namespace mindspore::kernel

View File

@ -0,0 +1,107 @@
/**
* Copyright 2021-2022 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_SRC_MODEL_LOADER_MINDIR_MODEL_INNER_KERNEL_H_
#define MINDSPORE_LITE_SRC_MODEL_LOADER_MINDIR_MODEL_INNER_KERNEL_H_
#include <utility>
#include <vector>
#include <memory>
#include "src/tensor.h"
#include "include/errorcode.h"
#include "include/api/kernel.h"
#include "src/inner_context.h"
// #include "include/api/context.h"
#include "kernel/kernel.h"
#include "model_loader/abstract_kernel.h"
using mindspore::infer::Abstractkernel;
namespace mindspore::kernel {
class InnerKernel : public Abstractkernel {
public:
InnerKernel() = default;
InnerKernel(std::shared_ptr<mindspore::kernel::KernelMod> kernel_mod,
mindspore::kernel::BaseOperatorPtr base_operator, std::vector<lite::Tensor *> in_tensors,
std::vector<lite::Tensor *> out_tensors, const lite::Context *ctx)
: kernel_mod_(kernel_mod),
base_operator_(base_operator),
in_tensors_(std::move(in_tensors)),
out_tensors_(std::move(out_tensors)),
ms_context_(ctx) {}
virtual ~InnerKernel() {}
int Prepare() override;
int Execute() override;
int ReSize() override;
int Train() override { return mindspore::lite::RET_OK; }
bool IsTrain() const override { return true; }
int Eval() override { return mindspore::lite::RET_OK; }
bool IsEval() const override { return true; }
void SetTrainable(bool trainable = true) override {}
bool IsTrainable() const override { return true; }
void set_in_tensors(const std::vector<lite::Tensor *> &in_tensors) override { this->in_tensors_ = in_tensors; }
void set_in_tensor(lite::Tensor *in_tensor, size_t index) override {
if (index >= in_tensors_.size()) {
MS_LOG(ERROR) << "index: " << index << " larger than in_tensors size: " << in_tensors_.size();
return;
}
this->in_tensors_[index] = in_tensor;
}
void set_out_tensors(const std::vector<lite::Tensor *> &out_tensors) override { this->out_tensors_ = out_tensors; }
void set_out_tensor(lite::Tensor *out_tensor, size_t index) override {
if (index >= out_tensors_.size()) {
MS_LOG(ERROR) << "index: " << index << " larger than out_tensors size: " << out_tensors_.size();
return;
}
this->out_tensors_[index] = out_tensor;
}
const std::vector<lite::Tensor *> &in_tensors() const override { return in_tensors_; }
const std::vector<lite::Tensor *> &out_tensors() const override { return out_tensors_; }
private:
std::vector<KernelTensorPtr> LiteTensorToKernelTensorPtrVec(const std::vector<lite::Tensor *> &lite_tensors);
KernelTensorPtr LiteTensorToKernelTensorPtr(lite::Tensor *lite_tensor);
std::vector<AddressPtr> LiteTensorToAddressPtrVec(const std::vector<lite::Tensor *> &lite_tensors);
AddressPtr LiteTensorToAddressPtr(lite::Tensor *lite_tensor);
private:
std::shared_ptr<mindspore::kernel::KernelMod> kernel_mod_ = nullptr;
BaseOperatorPtr base_operator_ = nullptr;
std::vector<lite::Tensor *> in_tensors_;
std::vector<lite::Tensor *> out_tensors_;
const mindspore::lite::Context *ms_context_ = nullptr;
};
} // namespace mindspore::kernel
#endif // MINDSPORE_LITE_SRC_MODEL_LOADER_MINDIR_MODEL_INNER_KERNEL_H_

View File

@ -0,0 +1,44 @@
/**
* Copyright 2021-2022 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 <vector>
#include <memory>
#include "model_loader/mindir_model/kernel_mod_util.h"
#include "kernel/kernel.h"
#include "plugin/factory/ms_factory.h"
#include "plugin/device/cpu/kernel/cpu_kernel_mod.h"
namespace mindspore::kernel {
std::shared_ptr<mindspore::kernel::InnerKernel> KernelModUtil::GetInnerKernel(
const std::vector<mindspore::lite::Tensor *> &in_tensors, const std::vector<mindspore::lite::Tensor *> &out_tensors,
const mindspore::lite::Model::Node *node, lite::Context *context) {
auto op_type = node->op_type_;
std::shared_ptr<kernel::KernelMod> kernel_mod = nullptr;
if (kernel::Factory<kernel::CpuKernelMod>::Instance().IsRegistered(op_type)) {
kernel_mod = kernel::Factory<kernel::CpuKernelMod>::Instance().Create(op_type);
}
if (kernel_mod == nullptr) {
return nullptr;
}
// auto base_operator = reinterpret_cast<std::shared_ptr<ops::BaseOperator> *>(const_cast<void *>(node->primitive_));
// auto base_operator = reinterpret_cast<ops::BaseOperator *>(const_cast<void *>(node->primitive_));
auto base_operator = std::any_cast<std::shared_ptr<ops::BaseOperator>>(node->base_operators_);
// std::shared_ptr<ops::BaseOperator> base_operator_shared_ptr(base_operator);
return std::make_shared<kernel::InnerKernel>(kernel_mod, base_operator, in_tensors, out_tensors, context);
}
} // namespace mindspore::kernel

View File

@ -0,0 +1,37 @@
/**
* Copyright 2021-2022 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_SRC_MODEL_LOADER_MINDIR_MODEL_KERNEL_MOD_UTIL_H_
#define MINDSPORE_LITE_SRC_MODEL_LOADER_MINDIR_MODEL_KERNEL_MOD_UTIL_H_
#include <vector>
#include <memory>
#include "model_loader/mindir_model/inner_kernel.h"
#include "src/tensor.h"
#include "include/model.h"
#include "include/context.h"
namespace mindspore::kernel {
class KernelModUtil {
public:
static std::shared_ptr<mindspore::kernel::InnerKernel> GetInnerKernel(
const std::vector<mindspore::lite::Tensor *> &in_tensors, const std::vector<mindspore::lite::Tensor *> &out_tensors,
const mindspore::lite::Model::Node *node, lite::Context *context);
};
} // namespace mindspore::kernel
#endif // MINDSPORE_LITE_SRC_MODEL_LOADER_MINDIR_MODEL_KERNEL_MOD_UTIL_H_

View File

@ -0,0 +1,42 @@
/**
* Copyright 2021-2022 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 <vector>
#include <memory>
#include "model_loader/mindir_model/less_test_kernel_mod.h"
#include "plugin/factory/ms_factory.h"
namespace mindspore::kernel {
// bool LessTestKernelMod::Launch(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &workspace,
// const std::vector<AddressPtr> &outputs) {
// return true;
// }
bool LessTestKernelMod::Launch(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &workspace,
const std::vector<AddressPtr> &outputs, void *stream_ptr) {
return true;
}
bool LessTestKernelMod::Init(const BaseOperatorPtr &base_operator, const std::vector<KernelTensorPtr> &inputs,
const std::vector<KernelTensorPtr> &outputs) {
return true;
}
MS_KERNEL_FACTORY_REG_BY_CREATOR(CpuKernelMod, Less,
[]() { return std::make_shared<LessTestKernelMod>(prim::kPrimLess->name()); });
} // namespace mindspore::kernel

View File

@ -0,0 +1,44 @@
/**
* Copyright 2021-2022 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_SRC_MODEL_LOADER_MINDIR_MODEL_LESS_TEST_KERNEL_MOD_H_
#define MINDSPORE_LITE_SRC_MODEL_LOADER_MINDIR_MODEL_LESS_TEST_KERNEL_MOD_H_
#include <vector>
#include <string>
#include "plugin/device/cpu/kernel/cpu_kernel_mod.h"
namespace mindspore::kernel {
class LessTestKernelMod : public CpuKernelMod {
public:
LessTestKernelMod() = default;
~LessTestKernelMod() override = default;
explicit LessTestKernelMod(const std::string name) { kernel_name_ = name; }
// virtual bool Launch(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &workspace,
// const std::vector<AddressPtr> &outputs);
virtual bool Launch(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &workspace,
const std::vector<AddressPtr> &outputs, void *stream_ptr);
virtual bool Init(const BaseOperatorPtr &base_operator, const std::vector<KernelTensorPtr> &inputs,
const std::vector<KernelTensorPtr> &outputs);
};
} // namespace mindspore::kernel
#endif // MINDSPORE_LITE_SRC_MODEL_LOADER_MINDIR_MODEL_LESS_TEST_KERNEL_MOD_H_

View File

@ -0,0 +1,207 @@
/**
* Copyright 2021-2022 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 <vector>
#include <memory>
#include <string>
#include <fstream>
#include "model_loader/mindir_model/mindir_model.h"
#include "utils/ms_utils_secure.h"
#include "model_loader/mindir_model/mindir_model_util.h"
#include "model_loader/mindir_model/kernel_mod_util.h"
#include "src/kernel_exec.h"
#include "model_loader/mindir_model/inner_kernel.h"
namespace mindspore::infer::mindir {
#define IS_LITTLE_ENDIAN (uint8_t)1U
bool MindirModel::ModelVerify() const { return true; }
int MindirModel::ConvertTensors(std::vector<mindspore::lite::Tensor *> *lite_tensors) {
if (lite_tensors == nullptr) {
MS_LOG(ERROR) << "lite tensors is null.";
return mindspore::lite::RET_NULL_PTR;
}
uint32_t tensor_count = this->all_mindir_tensors_.size();
auto model_input_indices = this->input_indices_;
auto model_output_indices = this->output_indices_;
for (uint32_t i = 0; i < tensor_count; ++i) {
// auto *src_tensor = reinterpret_cast<const mind_ir::TensorProto *>(this->all_mindir_tensors_[i]);
auto src_tensor = this->all_mindir_tensors_[i];
// if (src_tensor == nullptr) {
// MS_LOG(ERROR) << i << "th tensor in model is nullptr";
// return RET_NULL_PTR;
// }
auto *dst_tensor = ConvertTensor(src_tensor);
if (dst_tensor == nullptr) {
MS_LOG(ERROR) << "Convert new " << i << "th tensor failed!";
return mindspore::lite::RET_NULL_PTR;
}
if (mindspore::lite::IsContain(model_input_indices, i)) {
dst_tensor->set_category(mindspore::lite::Category::GRAPH_INPUT);
}
if (mindspore::lite::IsContain(model_output_indices, i)) {
// a tensor is as both input and output, would be treated as an input.
if (!dst_tensor->IsGraphInput()) {
dst_tensor->set_category(mindspore::lite::Category::GRAPH_OUTPUT);
}
}
auto ret = CheckTensorValid(dst_tensor);
if (ret != RET_OK) {
MS_LOG(ERROR) << "Check " << i << "th tensor failed";
delete dst_tensor;
return ret;
}
lite_tensors->emplace_back(dst_tensor);
}
return mindspore::lite::RET_OK;
}
std::string MindirModel::GetModelPath() const { return this->model_path_; }
mindspore::kernel::KernelExec *MindirModel::FindBackendKernel(const std::vector<mindspore::lite::Tensor *> &in_tensors,
const std::vector<mindspore::lite::Tensor *> &out_tensors,
const Model::Node *node, lite::Context *context,
TypeId prefer_data_type) {
std::shared_ptr<kernel::InnerKernel> inner_kernel =
mindspore::kernel::KernelModUtil::GetInnerKernel(in_tensors, out_tensors, node, context);
kernel::KernelExec *kernel_exec = new kernel::KernelExec(inner_kernel);
auto desc = kernel_exec->desc();
desc.data_type = in_tensors.front()->data_type();
kernel_exec->set_desc(desc);
return kernel_exec;
}
mindspore::lite::Tensor *MindirModel::ConvertTensor(TensorProtoWrap mindir_tensor_wrap) {
auto mindir_tensor = mindir_tensor_wrap.tensor_proto();
auto data_type = MindirModelUtil::ProtoTypeToTypeId(mindir_tensor.data_type());
std::vector<int> shape;
for (int i = 0; i < mindir_tensor.dims_size(); i++) {
shape.push_back(mindir_tensor.dims(i));
}
auto format = Format::NCHW;
mindspore::lite::NodeType node_type;
if (mindir_tensor.has_raw_data() || mindir_tensor.has_external_data()) {
node_type = mindspore::lite::NodeType_ValueNode;
} else {
node_type = mindspore::lite::NodeType_CNode;
}
auto category = TensorCategory(node_type, mindir_tensor.dims_size(), data_type, mindir_tensor.raw_data().size());
auto *lite_tensor = new mindspore::lite::Tensor(data_type, shape, format, category);
lite_tensor->set_tensor_name(mindir_tensor_wrap.name());
if (this->LoadTensorData(lite_tensor, mindir_tensor) != RET_OK) {
MS_LOG(WARNING) << "MindirModel: Convert tensor failed, load tensor data failed, tensor data will be empty.";
}
return lite_tensor;
}
int MindirModel::LoadTensorData(mindspore::lite::Tensor *lite_tensor, const mind_ir::TensorProto &mindir_tensor) {
if (mindir_tensor.has_raw_data()) {
return memcpy_s(lite_tensor->MutableData(), lite_tensor->Size(), mindir_tensor.raw_data().data(),
mindir_tensor.raw_data().size());
}
if (mindir_tensor.has_external_data()) {
std::string file = this->GetModelPath() + "/" + mindir_tensor.external_data().location();
// Read file
std::basic_ifstream<char> fid(file, std::ios::in | std::ios::binary);
if (!fid) {
MS_LOG(ERROR) << "Open file '" << file << "' failed, please check the correct of the file.";
return RET_OK;
}
fid.seekg(0, std::ios_base::end);
size_t file_size = static_cast<size_t>(fid.tellg());
fid.clear();
fid.seekg(0);
auto plain_data = std::make_unique<char[]>(file_size);
constexpr uint8_t is_little_endian = 1;
constexpr int byte_order_index = 0;
fid.read(plain_data.get(), file_size);
fid.close();
// if byte order is not same return false
if ((plain_data[byte_order_index] == is_little_endian) != common::IsLittleByteOrder()) {
MS_LOG(ERROR) << "The byte order of export MindIr device and load MindIr device is not same!";
return mindspore::lite::RET_ERROR;
}
const uint8_t *data = reinterpret_cast<const uint8_t *>(plain_data.get());
auto ret =
common::huge_memcpy(reinterpret_cast<uint8_t *>(lite_tensor->MutableData()), lite_tensor->Size(),
data + mindir_tensor.external_data().offset(), mindir_tensor.external_data().length());
if (ret != 0) {
MS_LOG(ERROR) << "Build parameter occur memcpy_s error.";
return mindspore::lite::RET_OK;
}
return mindspore::lite::RET_OK;
}
return mindspore::lite::RET_NOT_SUPPORT;
}
int MindirModel::CheckTensorValid(lite::Tensor *dst_tensor) {
MS_ASSERT(dst_tensor != nullptr);
if (dst_tensor->data_type() == kObjectTypeTensorType) {
return mindspore::lite::RET_OK;
}
if (dst_tensor->IsGraphInput() || dst_tensor->IsGraphOutput()) {
return mindspore::lite::RET_OK;
}
if (dst_tensor->IsConst() == false && dst_tensor->data() != nullptr) {
return mindspore::lite::RET_ERROR;
}
return mindspore::lite::RET_OK;
}
void MindirModel::Free() {
if (this->buf != nullptr) {
delete[](this->buf);
this->buf = nullptr;
}
auto nodes_size = this->all_nodes_.size();
for (size_t i = 0; i < nodes_size; ++i) {
auto node = this->all_nodes_[i];
// auto *primitive_shared_ptr =
// reinterpret_cast<std::shared_ptr<ops::BaseOperator> *>(const_cast<void *>(node->primitive_));
auto *primitive_ptr = reinterpret_cast<ops::BaseOperator *>(const_cast<void *>(node->primitive_));
// delete primitive_shared_ptr;
delete primitive_ptr;
node->primitive_ = nullptr;
}
}
void MindirModel::Destroy() {
Free();
this->all_mindir_tensors_.clear();
auto nodes_size = this->all_nodes_.size();
for (size_t i = 0; i < nodes_size; ++i) {
auto node = this->all_nodes_[i];
MS_ASSERT(node != nullptr);
delete node;
}
this->all_nodes_.clear();
auto sub_graph_size = this->sub_graphs_.size();
for (size_t i = 0; i < sub_graph_size; ++i) {
auto sub_graph = this->sub_graphs_[i];
delete sub_graph;
}
}
} // namespace mindspore::infer::mindir

View File

@ -0,0 +1,76 @@
/**
* Copyright 2021-2022 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_SRC_MODEL_LOADER_MINDIR_MODEL_MINDIR_MODEL_H_
#define MINDSPORE_LITE_SRC_MODEL_LOADER_MINDIR_MODEL_MINDIR_MODEL_H_
#include <vector>
#include <string>
#include "model_loader/abstract_base_model.h"
#include "src/schema_tensor_wrapper.h"
#include "proto/mind_ir.pb.h"
namespace mindspore::infer::mindir {
class TensorProtoWrap {
public:
TensorProtoWrap(std::string name, const mind_ir::TensorProto &tensor_proto)
: name_(name), tensor_proto_(tensor_proto) {}
~TensorProtoWrap() = default;
const mind_ir::TensorProto &tensor_proto() { return tensor_proto_; }
std::string name() { return name_; }
private:
std::string name_;
mind_ir::TensorProto tensor_proto_;
};
class MindirModel : public AbstractBaseModel {
public:
MindirModel() {}
~MindirModel() { Destroy(); }
bool ModelVerify() const override;
// virtual SchemaTensorWrapper *GetSchemaTensor(const size_t &tensor_index) const override;
int ConvertTensors(std::vector<mindspore::lite::Tensor *> *lite_tensors) override;
std::string GetModelPath() const override;
virtual mindspore::kernel::KernelExec *FindBackendKernel(const std::vector<mindspore::lite::Tensor *> &in_tensors,
const std::vector<mindspore::lite::Tensor *> &out_tensors,
const Model::Node *node, lite::Context *context,
TypeId prefer_data_type);
void Free() override;
void Destroy() override;
void SetModelPath(const std::string &model_path) { this->model_path_ = model_path; }
private:
mindspore::lite::Tensor *ConvertTensor(TensorProtoWrap mindir_tensor);
int LoadTensorData(mindspore::lite::Tensor *lite_tensor, const mind_ir::TensorProto &mindir_tensor);
int CheckTensorValid(lite::Tensor *dst_tensor);
public:
std::vector<TensorProtoWrap> all_mindir_tensors_;
mind_ir::ModelProto mindir_model_proto_;
private:
std::string model_path_;
};
} // namespace mindspore::infer::mindir
#endif // MINDSPORE_LITE_SRC_MODEL_LOADER_MINDIR_MODEL_MINDIR_MODEL_H_

View File

@ -0,0 +1,26 @@
/**
* Copyright 2021-2022 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 <memory>
#include "model_loader/mindir_model/mindir_model_convertor.h"
#include "model_loader/mindir_model/mindir_model_util.h"
namespace mindspore::infer::mindir {
mindspore::lite::LiteModel *MindirModelConvertor::ConvertLiteModel(const std::shared_ptr<MindirModel> &mindir_model) {
return nullptr;
}
} // namespace mindspore::infer::mindir

View File

@ -0,0 +1,32 @@
/**
* Copyright 2021-2022 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_SRC_MODEL_LOADER_MINDIR_MODEL_MINDIR_MODEL_CONVERTOR_H_
#define MINDSPORE_LITE_SRC_MODEL_LOADER_MINDIR_MODEL_MINDIR_MODEL_CONVERTOR_H_
#include <memory>
#include "src/lite_model.h"
#include "model_loader/mindir_model/mindir_model.h"
namespace mindspore::infer::mindir {
class MindirModelConvertor {
public:
static mindspore::lite::LiteModel *ConvertLiteModel(const std::shared_ptr<MindirModel> &mindir_model);
};
} // namespace mindspore::infer::mindir
#endif // MINDSPORE_LITE_SRC_MODEL_LOADER_MINDIR_MODEL_MINDIR_MODEL_CONVERTOR_H_

View File

@ -0,0 +1,305 @@
/**
* Copyright 2021-2022 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 <memory>
#include <string>
#include "model_loader/mindir_model/mindir_model_loader.h"
#include "model_loader/mindir_model/mindir_model_util.h"
#include "src/kernel_registry.h"
#include "ops/primitive_c.h"
namespace mindspore::infer::mindir {
const char kNodeTypeConstant[] = "Constant";
AbstractBaseModel *MindirModelLoader::ImportModel(const char *model_buf, size_t size, bool take_buf) {
this->model_ = new MindirModel();
this->model_->model_type_ = mindspore::lite::ModelType_MindIR;
MS_CHECK_TRUE_MSG(this->model_ != nullptr, nullptr,
"MindirModelLoader: Import model failed: new mindir model failed.");
auto ret = this->InitModelBuffer(this->model_, model_buf, size, take_buf);
MS_CHECK_TRUE_MSG(ret == RET_OK, nullptr,
"MindirModelLoader: Import model failed: init model buffer error with " << ret);
// mind_ir::ModelProto model_proto;
MS_CHECK_TRUE_MSG(this->model_->mindir_model_proto_.ParseFromArray(this->model_->buf, static_cast<int32_t>(size)),
nullptr, "MindirModelLoader: Import model failed, please check the correctness of the file.");
MS_LOG(ERROR) << "model_proto: " << this->model_->mindir_model_proto_.DebugString();
if (!this->ConvertModel(this->model_->mindir_model_proto_)) {
MS_LOG(ERROR)
<< "MindirModelLoader: Import model failed, convert model error, please check the correctness of the file.";
delete this->model_;
this->model_ = nullptr;
return nullptr;
}
return this->model_;
}
bool MindirModelLoader::ConvertModel(const mind_ir::ModelProto &model_proto) {
this->model_->name_ = "";
if (model_proto.has_model_version()) {
this->model_->version_ = model_proto.model_version();
}
MS_CHECK_TRUE_MSG(
ConvertPrimitives(model_proto), false,
"MindirModelLoader: Import model failed, convert primitives error, please check the correctness of the file.");
this->tensor_count_ = 0;
this->node_count_ = 0;
if (model_proto.has_graph()) {
this->model_->name_ = model_proto.graph().name();
// root graph, do not pass sub graph
if (model_proto.functions_size() > 0) {
MS_CHECK_TRUE_MSG(
ConvertGraph(model_proto.graph(), nullptr, true), false,
"MindirModelLoader: Import model failed, convert root graph error, please check the correctness of the file.");
} else {
// no subgraph, add graph to subgraph
auto *sub_graph = new Model::SubGraph();
sub_graph->name_ = model_proto.graph().name();
MS_CHECK_TRUE_MSG(
ConvertGraph(model_proto.graph(), sub_graph, true), false,
"MindirModelLoader: Import model failed, convert root graph error, please check the correctness of the file.");
this->model_->sub_graphs_.push_back(sub_graph);
}
}
for (int i = 0; i < model_proto.functions_size(); i++) {
auto sub_graph_proto = model_proto.functions(i);
auto *sub_graph = new Model::SubGraph();
if (sub_graph == nullptr) {
MS_LOG(ERROR) << "MindirModelLoader: Import model failed, new sub graph failed.";
return mindspore::lite::RET_ERROR;
}
// MS_CHECK_FALSE_MSG(sub_graph == nullptr, mindspore::lite::RET_ERROR,
// "MindirModelLoader: Import model failed, new sub graph failed.");
sub_graph->name_ = sub_graph_proto.name();
MS_CHECK_TRUE_MSG(
ConvertGraph(sub_graph_proto, sub_graph), false,
"MindirModelLoader: Import model failed, convert sub graph error, please check the correctness of the file.");
this->model_->sub_graphs_.push_back(sub_graph);
}
MS_LOG(INFO) << "MindirModelLoader: Import model successful.";
return true;
}
bool MindirModelLoader::ConvertPrimitives(const mind_ir::ModelProto &model_proto) {
static auto op_primc_fns = ops::OpPrimCRegister::GetInstance().GetPrimCMap();
for (int i = 0; i < model_proto.primitives_size(); i++) {
auto primitive_proto = model_proto.primitives(i);
auto op_type = primitive_proto.op_type();
std::shared_ptr<mindspore::Primitive> prim;
auto it = op_primc_fns.find(op_type);
if (it == op_primc_fns.end()) {
MS_LOG(WARNING) << "MindirModelLoader: Convert primitives failed, unsupported op primitive type: " << op_type;
continue;
}
prim = it->second();
prim->set_instance_name(op_type);
for (int j = 0; j < primitive_proto.attribute_size(); j++) {
auto attr_proto = primitive_proto.attribute(j);
auto value_ptr = MindirModelUtil::MakeValueFromAttribute(attr_proto);
MS_CHECK_TRUE_MSG(value_ptr != nullptr, false,
"MindirModelLoader: convert primitives failed, parse prim: "
<< prim->ToString() << " attributes error: " << attr_proto.DebugString());
(void)prim->AddAttr(attr_proto.name(), value_ptr);
// TODO(lliulili): solve follow special case
/*
const std::string prim_name = prim->name();
if (!IsLite()) {
CHeckAndConvertUtils::ConvertAttrValueInLoad(prim_name, attr_name, &value);
}
if (prim_name == "HistogramFixedWidth" && attr_name == "dtype" && value->isa<StringImm>()) {
auto str_dtype = GetValue<std::string>(value);
if (str_dtype == "int32") {
int64_t index = 3;
(void)prim->AddAttr(attr_name, MakeValue<int64_t>(index));
break;
}
MS_EXCEPTION(NotSupportError) << "The primitive[HistogramFixedWidth] not supported. only support
attribute[dtype] is 'int32', but got" << value->ToString();
}
*/
}
static auto operator_fns = ops::OperatorRegister::GetInstance().GetOperatorMap();
auto op_it = operator_fns.find(op_type);
if (op_it == operator_fns.end()) {
MS_LOG(WARNING) << "MindirModelLoader: Convert primitives failed, unsupported op operator type: " << op_type;
continue;
}
auto base_operator = op_it->second(prim);
MS_CHECK_TRUE_MSG(this->all_operators_.count(primitive_proto.name()) <= 0, false,
"MindirModelLoader: There is a duplication primitive instance name: " << primitive_proto.name());
this->all_operators_[primitive_proto.name()] = base_operator;
}
return true;
}
bool MindirModelLoader::ConvertGraph(const mind_ir::GraphProto &graph_proto, Model::SubGraph *sub_graph,
bool is_main_graph) {
MS_CHECK_TRUE_MSG(
ConvertTensors(graph_proto, sub_graph, is_main_graph), false,
"MindirModelLoader: Convert Graph failed, convert tensors error, please check the correctness of the file.");
MS_CHECK_TRUE_MSG(
ConvertNodes(graph_proto, sub_graph, is_main_graph), false,
"MindirModelLoader: Convert Graph failed, convert nodes error, please check the correctness of the file.");
return true;
}
bool MindirModelLoader::ConvertTensors(const mind_ir::GraphProto &graph_proto, Model::SubGraph *sub_graph,
bool is_main_graph) {
for (int i = 0; i < graph_proto.input_size(); i++) {
const mind_ir::TensorProto &tensor_proto = graph_proto.input(i).tensor(0);
TensorProtoWrap tensor_wrap(graph_proto.input(i).name(), tensor_proto);
this->model_->all_mindir_tensors_.push_back(tensor_wrap);
this->tensor_index_map_[graph_proto.input(i).name()] = this->tensor_count_;
if (sub_graph != nullptr) {
sub_graph->input_indices_.push_back(this->tensor_count_);
sub_graph->tensor_indices_.push_back(this->tensor_count_);
}
if (is_main_graph) {
this->model_->input_indices_.push_back(this->tensor_count_);
}
this->tensor_count_++;
}
for (int i = 0; i < graph_proto.output_size(); i++) {
const mind_ir::TensorProto &tensor_proto = graph_proto.output(i).tensor(0);
TensorProtoWrap tensor_wrap(graph_proto.output(i).name(), tensor_proto);
this->model_->all_mindir_tensors_.push_back(tensor_wrap);
this->tensor_index_map_[graph_proto.output(i).name()] = this->tensor_count_;
if (sub_graph != nullptr) {
sub_graph->output_indices_.push_back(this->tensor_count_);
sub_graph->tensor_indices_.push_back(this->tensor_count_);
}
if (is_main_graph) {
this->model_->output_indices_.push_back(this->tensor_count_);
}
this->tensor_count_++;
}
for (int i = 0; i < graph_proto.parameter_size(); i++) {
const mind_ir::TensorProto &tensor_proto = graph_proto.parameter(i);
TensorProtoWrap tensor_wrap(tensor_proto.name(), tensor_proto);
this->model_->all_mindir_tensors_.push_back(tensor_wrap);
this->tensor_index_map_[tensor_proto.name()] = this->tensor_count_;
if (sub_graph != nullptr) {
sub_graph->tensor_indices_.push_back(this->tensor_count_);
}
this->tensor_count_++;
}
return true;
}
bool MindirModelLoader::ConvertNodes(const mind_ir::GraphProto &graph_proto, Model::SubGraph *sub_graph,
bool is_main_graph) {
for (int i = 0; i < graph_proto.node_size(); i++) {
auto node_proto = graph_proto.node(i);
if (node_proto.op_type() == kNodeTypeConstant) {
// Constant node, convert to tensor
for (int j = 0; j < node_proto.attribute_size(); j++) {
auto attribute_proto = node_proto.attribute(j);
if (attribute_proto.type() == mind_ir::AttributeProto_AttributeType_TENSORS) {
const mind_ir::TensorProto &tensor_proto = attribute_proto.tensors(0);
TensorProtoWrap tensor_wrap(node_proto.name(), tensor_proto);
this->model_->all_mindir_tensors_.push_back(tensor_wrap);
this->tensor_index_map_[node_proto.name()] = this->tensor_count_;
if (sub_graph != nullptr) {
sub_graph->tensor_indices_.push_back(this->tensor_count_);
}
this->tensor_count_++;
}
}
continue;
}
auto *node = new Model::Node();
if (node == nullptr) {
MS_LOG(ERROR) << "MindirModelLoader: Convert nodes failed, new node failed.";
return false;
}
node->name_ = node_proto.name();
node->base_operators_ = this->MakePrimitiveC(node_proto.op_type());
auto base_operator = std::any_cast<std::shared_ptr<ops::BaseOperator>>(node->base_operators_);
node->op_type_ = base_operator->GetPrim()->instance_name();
// solve input
for (int j = 0; j < node_proto.input_size(); j++) {
std::string input_name = node_proto.input(j);
auto it = this->tensor_index_map_.find(input_name);
if (it == this->tensor_index_map_.end()) {
MS_LOG(WARNING) << "MindirModelLoader: Convert nodes failed, cannot find input index with " << input_name;
continue;
}
node->input_indices_.push_back(it->second);
}
// solve output
for (int j = 0; j < node_proto.output_size(); j++) {
std::string output_name = node_proto.output(j);
auto it = this->tensor_index_map_.find(output_name);
if (it == this->tensor_index_map_.end()) {
MS_LOG(WARNING) << "MindirModelLoader: Convert nodes failed, cannot find output index with " << output_name;
continue;
}
node->output_indices_.push_back(it->second);
}
this->model_->all_nodes_.push_back(node);
if (sub_graph != nullptr) {
sub_graph->node_indices_.push_back(this->node_count_);
}
this->node_count_++;
}
return true;
}
std::any MindirModelLoader::MakePrimitiveC(const std::string &node_type) {
const std::string kOperatorTypeFlag = std::string("REF::");
const size_t kOpTypeFlagSize = kOperatorTypeFlag.length();
if (node_type.size() > kOpTypeFlagSize && node_type.substr(0, kOpTypeFlagSize) == kOperatorTypeFlag) {
auto it = this->all_operators_.find(node_type.substr(kOpTypeFlagSize));
if (it == this->all_operators_.end()) {
MS_LOG(ERROR) << "MindirModelLoader: make primitiveC failed, can't find the primitive ref:" << node_type;
return nullptr;
}
return it->second;
}
// node_type is not ref: pointer
auto op_primc_fns = ops::OpPrimCRegister::GetInstance().GetPrimCMap();
if (op_primc_fns.find(node_type) != op_primc_fns.end()) {
// registered primitive
auto prim = (op_primc_fns[node_type]());
prim->set_instance_name(node_type);
static auto operator_fns = ops::OperatorRegister::GetInstance().GetOperatorMap();
auto op_it = operator_fns.find(node_type);
if (op_it == operator_fns.end()) {
MS_LOG(WARNING) << "MindirModelLoader: Make primitiveC failed, unsupported op operator type: " << node_type;
return nullptr;
}
return op_it->second(prim);
} else {
// S-Prim-xxx or S-Prim-hyper_map[xxx] and custom node type, now not support
MS_LOG(ERROR) << "MindirModelLoader: make primitiveC failed, not support node type: " << node_type;
return nullptr;
}
}
static std::shared_ptr<ModelLoader> MindirModelLoaderCreator() { return std::make_shared<MindirModelLoader>(); }
REG_MODEL_LOADER(kMindIR, MindirModelLoaderCreator);
} // namespace mindspore::infer::mindir

View File

@ -0,0 +1,57 @@
/**
* Copyright 2021-2022 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_SRC_MODEL_LOADER_MINDIR_MODEL_MINDIR_MODEL_LOADER_H_
#define MINDSPORE_LITE_SRC_MODEL_LOADER_MINDIR_MODEL_MINDIR_MODEL_LOADER_H_
#include <memory>
#include <string>
#include "model_loader/mindir_model/mindir_model.h"
#include "model_loader/model_loader.h"
#include "proto/mind_ir.pb.h"
#include "ops/base_operator.h"
namespace mindspore::infer::mindir {
class MindirModelLoader : public ModelLoader {
public:
MindirModelLoader() {}
~MindirModelLoader() = default;
AbstractBaseModel *ImportModel(const char *model_buf, size_t size, bool take_buf) override;
private:
// int InitModelBuffer(const char *model_buf, size_t size, bool take_buf);
bool ConvertModel(const mind_ir::ModelProto &model_proto);
bool ConvertPrimitives(const mind_ir::ModelProto &model_proto);
bool ConvertGraph(const mind_ir::GraphProto &graph_proto, Model::SubGraph *sub_graph = nullptr,
bool is_main_graph = false);
bool ConvertNodes(const mind_ir::GraphProto &graph_proto, Model::SubGraph *sub_graph = nullptr,
bool is_main_graph = false);
bool ConvertTensors(const mind_ir::GraphProto &graph_proto, Model::SubGraph *sub_graph = nullptr,
bool is_main_graph = false);
std::any MakePrimitiveC(const std::string &node_type);
private:
MindirModel *model_;
mindspore::HashMap<std::string, std::shared_ptr<mindspore::ops::BaseOperator>> all_operators_;
mindspore::HashMap<std::string, int32_t> tensor_index_map_;
int tensor_count_;
int node_count_;
};
} // namespace mindspore::infer::mindir
#endif // MINDSPORE_LITE_SRC_MODEL_LOADER_MINDIR_MODEL_MINDIR_MODEL_LOADER_H_

View File

@ -0,0 +1,194 @@
/**
* Copyright 2021-2022 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 <vector>
#include <memory>
#include <string>
#include "model_loader/mindir_model/mindir_model_util.h"
#include "ir/tensor.h"
#include "ir/value.h"
#include "include/errorcode.h"
#include "nnacl/op_base.h"
namespace mindspore::infer::mindir {
static mindspore::HashMap<int, TypeId> kDefaultValueSwitchMap{
{mind_ir::TensorProto_DataType_BOOL, kNumberTypeBool},
{mind_ir::TensorProto_DataType_INT8, kNumberTypeInt8},
{mind_ir::TensorProto_DataType_INT16, kNumberTypeInt16},
{mind_ir::TensorProto_DataType_INT32, kNumberTypeInt32},
{mind_ir::TensorProto_DataType_INT64, kNumberTypeInt64},
{mind_ir::TensorProto_DataType_UINT8, kNumberTypeUInt8},
{mind_ir::TensorProto_DataType_UINT16, kNumberTypeUInt16},
{mind_ir::TensorProto_DataType_UINT32, kNumberTypeUInt32},
{mind_ir::TensorProto_DataType_UINT64, kNumberTypeUInt64},
{mind_ir::TensorProto_DataType_FLOAT16, kNumberTypeFloat16},
{mind_ir::TensorProto_DataType_FLOAT, kNumberTypeFloat32},
{mind_ir::TensorProto_DataType_FLOAT64, kNumberTypeFloat64},
{mind_ir::TensorProto_DataType_DOUBLE, kNumberTypeFloat64},
{mind_ir::TensorProto_DataType_STRING, kObjectTypeString},
{mind_ir::TensorProto_DataType_COMPLEX64, kNumberTypeComplex64},
{mind_ir::TensorProto_DataType_COMPLEX128, kNumberTypeComplex128}};
mindspore::ValuePtr MindirModelUtil::MakeValueFromAttribute(const mind_ir::AttributeProto &attr_proto) {
switch (attr_proto.type()) {
case mind_ir::AttributeProto_AttributeType_TENSORS: {
// embed tensor attribute
return MindirModelUtil::MakeValueFromTensorOrTypeAttribute(attr_proto);
}
case mind_ir::AttributeProto_AttributeType_TUPLE:
case mind_ir::AttributeProto_AttributeType_LIST: {
// list attribute
return MindirModelUtil::MakeValueFromListAttribute(attr_proto);
}
default: {
// base scalar attribute
return MindirModelUtil::MakeValueFromScalarAttribute(attr_proto);
}
}
}
mindspore::ValuePtr MindirModelUtil::MakeValueFromTensorOrTypeAttribute(const mind_ir::AttributeProto &attr_proto) {
auto tensor_proto = attr_proto.tensors(0);
if (tensor_proto.has_raw_data()) {
// For real tensor
return MindirModelUtil::MakeValueFromTensorAttribute(tensor_proto);
} else {
// for data type
const int attr_tensor_type = tensor_proto.data_type();
auto iter = kDefaultValueSwitchMap.find(attr_tensor_type);
MS_CHECK_TRUE_MSG(iter == kDefaultValueSwitchMap.end(), nullptr,
"MindirModelUtil: Generate value ptr failed, cannot find attr tensor type " << attr_tensor_type);
return TypeIdToType(iter->second);
}
}
mindspore::ValuePtr MindirModelUtil::MakeValueFromTensorAttribute(const mind_ir::TensorProto &tensor_proto,
bool need_load_data) {
ShapeVector shape;
auto attr_tensor_type = tensor_proto.data_type();
for (int i = 0; i < tensor_proto.dims_size(); i++) {
shape.push_back(tensor_proto.dims(i));
}
tensor::TensorPtr tensor = std::make_shared<tensor::Tensor>(kDefaultValueSwitchMap[attr_tensor_type], shape);
MS_EXCEPTION_IF_NULL(tensor);
const std::string &tensor_buf = tensor_proto.raw_data();
if (tensor_proto.has_raw_data()) {
auto *tensor_data_buf = reinterpret_cast<uint8_t *>(tensor->data_c());
auto ret = memcpy_s(tensor_data_buf, tensor->data().nbytes(), tensor_buf.data(), tensor_buf.size());
MS_CHECK_TRUE_MSG(
ret != mindspore::lite::RET_OK, nullptr,
"MindirModelUtil: Generate tensor ptr from tensor proto failed, failed to get tensor from tensor proto.");
} else {
MS_CHECK_TRUE_MSG(
need_load_data, nullptr,
"MindirModelUtil: Generate tensor ptr from tensor proto failed, failed to get tensor from tensor proto.");
}
return tensor;
}
mindspore::ValuePtr MindirModelUtil::MakeValueFromListAttribute(const mind_ir::AttributeProto &attr_proto) {
std::vector<mindspore::ValuePtr> vec;
for (int i = 0; i < attr_proto.values_size(); i++) {
mind_ir::AttributeProto elem_attr_proto = attr_proto.values(i);
mindspore::ValuePtr value_ptr = MindirModelUtil::MakeValueFromAttribute(elem_attr_proto);
vec.emplace_back(value_ptr);
}
auto type = attr_proto.type();
mindspore::ValuePtr value_sequence;
switch (type) {
case mind_ir::AttributeProto_AttributeType_TUPLE: {
return std::make_shared<mindspore::ValueTuple>(vec);
}
case mind_ir::AttributeProto_AttributeType_LIST: {
return std::make_shared<mindspore::ValueList>(vec);
}
default: {
MS_LOG(ERROR)
<< "MindirModelUtil: Obtain value in sequence form failed, the attribute type should be tuple or list";
return nullptr;
}
}
}
mindspore::ValuePtr MindirModelUtil::MakeValueFromScalarAttribute(const mind_ir::AttributeProto &attr_proto) {
auto attr_proto_type = static_cast<int>(attr_proto.type());
switch (attr_proto_type) {
case mind_ir::AttributeProto_AttributeType_STRING: {
auto value = static_cast<std::string>(attr_proto.s());
return MakeValue<std::string>(value);
}
case mind_ir::AttributeProto_AttributeType_INT8: {
auto value = static_cast<int8_t>(attr_proto.i());
return MakeValue<int8_t>(value);
}
case mind_ir::AttributeProto_AttributeType_INT16: {
auto value = static_cast<int16_t>(attr_proto.i());
return MakeValue<int16_t>(value);
}
case mind_ir::AttributeProto_AttributeType_INT32: {
auto value = static_cast<int32_t>(attr_proto.i());
return MakeValue<int32_t>(value);
}
case mind_ir::AttributeProto_AttributeType_INT64: {
auto value = static_cast<int64_t>(attr_proto.i());
return MakeValue<int64_t>(value);
}
case mind_ir::AttributeProto_AttributeType_UINT8: {
auto value = static_cast<uint8_t>(attr_proto.i());
return MakeValue<uint8_t>(value);
}
case mind_ir::AttributeProto_AttributeType_UINT16: {
auto value = static_cast<uint16_t>(attr_proto.i());
return MakeValue<uint16_t>(value);
}
case mind_ir::AttributeProto_AttributeType_UINT32: {
auto value = static_cast<uint32_t>(attr_proto.i());
return MakeValue<uint32_t>(value);
}
case mind_ir::AttributeProto_AttributeType_UINT64: {
auto value = static_cast<uint64_t>(attr_proto.i());
return MakeValue<uint64_t>(value);
}
case mind_ir::AttributeProto_AttributeType_FLOAT: {
auto value = static_cast<float>(attr_proto.f());
return MakeValue<float>(value);
}
case mind_ir::AttributeProto_AttributeType_DOUBLE: {
auto value = static_cast<double>(attr_proto.d());
return MakeValue<double>(value);
}
case mind_ir::AttributeProto_AttributeType_BOOL: {
auto value = static_cast<int32_t>(attr_proto.i());
return MakeValue<bool>(value);
}
default: {
MS_LOG(ERROR) << "MindirModelUtil: Obtain cnode attr in single scalar form failed, attr type " << attr_proto_type
<< " is xinot supported ";
return nullptr;
}
}
}
mindspore::TypeId MindirModelUtil::ProtoTypeToTypeId(int32_t proto_type) {
auto it = kDefaultValueSwitchMap.find(proto_type);
if (it == kDefaultValueSwitchMap.end()) {
return kTypeUnknown;
}
return it->second;
}
} // namespace mindspore::infer::mindir

View File

@ -0,0 +1,39 @@
/**
* Copyright 2021-2022 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_SRC_MODEL_LOADER_MINDIR_MODEL_MINDIR_MODEL_UTIL_H_
#define MINDSPORE_LITE_SRC_MODEL_LOADER_MINDIR_MODEL_MINDIR_MODEL_UTIL_H_
#include "ir/anf.h"
#include "mindapi/base/type_id.h"
#include "proto/mind_ir.pb.h"
namespace mindspore::infer::mindir {
class MindirModelUtil {
public:
static mindspore::ValuePtr MakeValueFromAttribute(const mind_ir::AttributeProto &attr_proto);
static mindspore::ValuePtr MakeValueFromTensorOrTypeAttribute(const mind_ir::AttributeProto &attr_proto);
static mindspore::ValuePtr MakeValueFromTensorAttribute(const mind_ir::TensorProto &attr_tensor,
bool need_load_data = false);
static mindspore::ValuePtr MakeValueFromListAttribute(const mind_ir::AttributeProto &attr_proto);
static mindspore::ValuePtr MakeValueFromScalarAttribute(const mind_ir::AttributeProto &attr_proto);
static mindspore::TypeId ProtoTypeToTypeId(int32_t proto_type);
};
} // namespace mindspore::infer::mindir
#endif // MINDSPORE_LITE_SRC_MODEL_LOADER_MINDIR_MODEL_MINDIR_MODEL_UTIL_H_

View File

@ -0,0 +1,54 @@
/**
* Copyright 2021-2022 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 "model_loader/model_loader.h"
namespace mindspore::infer {
constexpr size_t kMaxModelBufferSize = static_cast<size_t>(1024) * 1024 * 1024 * 2;
int ModelLoader::InitModelBuffer(AbstractBaseModel *model, const char *model_buf, size_t size, bool take_buf) {
if (model_buf == nullptr || size == 0) {
MS_LOG(ERROR) << "Input model buffer is nullptr.";
return mindspore::lite::RET_INPUT_PARAM_INVALID;
}
MS_ASSERT(model != nullptr);
if (take_buf) {
model->buf = const_cast<char *>(model_buf);
} else {
if (size > kMaxModelBufferSize) {
MS_LOG(ERROR) << "Input model buffer size invalid, require (0, 2GB].";
return mindspore::lite::RET_ERROR;
}
model->buf = new char[size];
if (model->buf == nullptr) {
MS_LOG(ERROR) << "new inner model buf fail!";
return mindspore::lite::RET_NULL_PTR;
}
memcpy(model->buf, model_buf, size);
}
model->buf_size_ = size;
return mindspore::lite::RET_OK;
}
ModelLoaderRegistry::ModelLoaderRegistry() {}
ModelLoaderRegistry::~ModelLoaderRegistry() {}
ModelLoaderRegistry *ModelLoaderRegistry::GetInstance() {
static ModelLoaderRegistry instance;
return &instance;
}
} // namespace mindspore::infer

View File

@ -0,0 +1,71 @@
/**
* Copyright 2021-2022 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_SRC_MODEL_LOADER_MODEL_LOADER_H_
#define MINDSPORE_LITE_SRC_MODEL_LOADER_MODEL_LOADER_H_
#include <memory>
#include "model_loader/abstract_base_model.h"
namespace mindspore::infer {
class ModelLoader {
public:
virtual AbstractBaseModel *ImportModel(const char *model_buf, size_t size, bool take_buf) = 0;
protected:
virtual int InitModelBuffer(AbstractBaseModel *model, const char *model_buf, size_t size, bool take_buf);
};
class ModelLoaderRegistry {
public:
ModelLoaderRegistry();
virtual ~ModelLoaderRegistry();
static ModelLoaderRegistry *GetInstance();
void RegModelLoader(mindspore::ModelType model_type, std::function<std::shared_ptr<ModelLoader>()> creator) {
model_loader_map_[model_type] = creator;
}
std::shared_ptr<ModelLoader> GetModelLoader(mindspore::ModelType model_type) {
auto it = model_loader_map_.find(model_type);
if (it == model_loader_map_.end()) {
return nullptr;
}
return it->second();
}
private:
mindspore::HashMap<mindspore::ModelType, std::function<std::shared_ptr<ModelLoader>()>> model_loader_map_;
};
class ModelLoaderRegistrar {
public:
ModelLoaderRegistrar(const mindspore::ModelType &model_type, std::function<std::shared_ptr<ModelLoader>()> creator) {
ModelLoaderRegistry::GetInstance()->RegModelLoader(model_type, creator);
}
~ModelLoaderRegistrar() = default;
};
// #define REG_MODEL_LOADER(model_type, model_loader)
// static ModelLoaderRegistrar g_##model_type##model_loader##ModelLoader(model_type, new model_loader());
#define REG_MODEL_LOADER(model_type, model_loader_creator) \
static ModelLoaderRegistrar g_##model_type##model_loader##ModelLoader(model_type, model_loader_creator);
} // namespace mindspore::infer
#endif // MINDSPORE_LITE_SRC_MODEL_LOADER_MODEL_LOADER_H_

View File

@ -51,6 +51,9 @@
#include "src/runtime/gpu/opencl/opencl_runtime.h"
#endif
#include "include/registry/register_kernel_interface.h"
#include "model_loader/abstract_base_model.h"
using AbstractBaseModel = mindspore::infer::AbstractBaseModel;
namespace mindspore::lite {
namespace {
@ -251,7 +254,13 @@ int Scheduler::SchedulePreProcess() {
schema_version_ = reinterpret_cast<LiteModel *>(src_model_)->GetSchemaVersion();
this->graph_output_node_indexes_ = GetGraphOutputNodes(src_model_);
if (src_model_->model_type_ != ModelType_MSLite) {
// TODO(liulili): call abstract model infer interface
*is_infershape_ = RET_OK;
} else {
*is_infershape_ = InferSubGraphShape(kMainSubGraphIndex);
}
if (*is_infershape_ != RET_OK && *is_infershape_ != RET_INFER_INVALID) {
MS_LOG(ERROR) << "op infer shape failed.";
return *is_infershape_;
@ -1410,12 +1419,26 @@ kernel::KernelExec *Scheduler::ScheduleNodeToKernel(const lite::Model::Node *src
ResetByExecutionPlan(src_node->name_, &prefer_data_type);
auto *kernel = this->FindBackendKernel(inputs, outputs, src_node, prefer_data_type);
mindspore::kernel::KernelExec *kernel = nullptr;
if (src_model_->model_type_ != mindspore::lite::ModelType_MSLite) {
auto abstract_model_ptr = reinterpret_cast<AbstractBaseModel *>(src_model_);
if (abstract_model_ptr == nullptr) {
MS_LOG(ERROR) << "src model is not abstract base model return nullptr.";
return nullptr;
}
kernel = abstract_model_ptr->FindBackendKernel(inputs, outputs, src_node, context_, prefer_data_type);
if (kernel == nullptr) {
MS_LOG(ERROR) << "FindBackendKernel return nullptr, name: " << src_node->name_;
return nullptr;
}
} else {
kernel = this->FindBackendKernel(inputs, outputs, src_node, prefer_data_type);
if (kernel == nullptr) {
MS_LOG(ERROR) << "FindBackendKernel return nullptr, name: " << src_node->name_
<< ", type: " << GetPrimitiveTypeName(src_node->primitive_, schema_version_);
return nullptr;
}
}
op_parameters_[src_node->output_indices_.at(0)] = nullptr;
auto ret = kernel::KernelExecUtil::SetKernelTensorDataType(kernel);
if (ret != RET_OK) {
@ -1478,7 +1501,7 @@ int Scheduler::ScheduleSubGraphToKernels(size_t subgraph_index, std::vector<kern
MS_ASSERT(primitive != nullptr);
kernel::KernelExec *kernel = nullptr;
if (IsPartialNode(primitive, schema_version_)) {
if (src_model_->model_type_ == ModelType_MSLite && IsPartialNode(primitive, schema_version_)) {
if (IsControlFlowPattern(*node)) {
kernel = ScheduleNodeToKernel(node, prefer_data_type);
auto partial_subgraph_index = GetPartialGraphIndex(primitive, schema_version_);

View File

@ -160,6 +160,40 @@ set(LITE_SRC ${API_SRC}
${SRC_DIR}/control_flow/control_flow_scheduler.cc
${SRC_DIR}/control_flow/control_subgraph_creator.cc
)
set(MODEL_LOADER_FRAMEWORK_SRC
${MODEL_LOADER_FRAMEWORK_SRC}
${SRC_DIR}/model_loader/model_loader.cc
)
if(MSLITE_ENABLE_CLOUD_FUSION_INFERENCE)
add_compile_definitions(ENABLE_CLOUD_FUSION_INFERENCE)
string(REPLACE "-Werror" "" CMAKE_C_FLAGS ${CMAKE_C_FLAGS})
string(REPLACE "-Werror" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
set(MINDIR_MODEL_SRC
${MINDIR_MODEL_SRC}
${SRC_DIR}/model_loader/mindir_model/mindir_model.cc
${SRC_DIR}/model_loader/mindir_model/mindir_model_util.cc
${SRC_DIR}/model_loader/mindir_model/mindir_model_convertor.cc
${SRC_DIR}/model_loader/mindir_model/mindir_model_loader.cc
${SRC_DIR}/model_loader/mindir_model/kernel_mod_util.cc
)
set(MINDIR_KERNEL_SRC
${MINDIR_KERNEL_SRC}
${SRC_DIR}/model_loader/mindir_model/inner_kernel.cc)
endif()
set(LITE_SRC
${LITE_SRC}
${MODEL_LOADER_FRAMEWORK_SRC}
${MINDIR_MODEL_SRC}
${MINDIR_KERNEL_SRC}
)
if(MSLITE_ENABLE_BFC_MEMORY)
set(LITE_SRC
${LITE_SRC}