sync c api

This commit is contained in:
sunsuodong 2022-01-16 22:17:47 -08:00
parent d25a40240a
commit 59ce15b41c
38 changed files with 369 additions and 382 deletions

View File

@ -222,6 +222,8 @@ if(PLATFORM_ARM64)
COMPONENT ${RUNTIME_COMPONENT_NAME})
install(DIRECTORY ${TOP_DIR}/include/api/ DESTINATION ${RUNTIME_INC_DIR}/api
COMPONENT ${RUNTIME_COMPONENT_NAME} FILES_MATCHING PATTERN "*.h" PATTERN "ops*" EXCLUDE)
install(DIRECTORY ${TOP_DIR}/include/c_api/ DESTINATION ${RUNTIME_INC_DIR}/c_api
COMPONENT ${RUNTIME_COMPONENT_NAME} FILES_MATCHING PATTERN "*.h")
__install_micro_wrapper()
if(MSLITE_ENABLE_TOOLS)
install(TARGETS ${BENCHMARK_NAME} RUNTIME DESTINATION ${BENCHMARK_ROOT_DIR} COMPONENT ${RUNTIME_COMPONENT_NAME})
@ -297,6 +299,8 @@ elseif(PLATFORM_ARM32)
COMPONENT ${RUNTIME_COMPONENT_NAME})
install(DIRECTORY ${TOP_DIR}/include/api/ DESTINATION ${RUNTIME_INC_DIR}/api
COMPONENT ${RUNTIME_COMPONENT_NAME} FILES_MATCHING PATTERN "*.h" PATTERN "ops*" EXCLUDE)
install(DIRECTORY ${TOP_DIR}/include/c_api/ DESTINATION ${RUNTIME_INC_DIR}/c_api
COMPONENT ${RUNTIME_COMPONENT_NAME} FILES_MATCHING PATTERN "*.h")
__install_micro_wrapper()
if(MSLITE_ENABLE_TOOLS AND NOT TARGET_OHOS_LITE)
install(TARGETS ${BENCHMARK_NAME} RUNTIME DESTINATION ${BENCHMARK_ROOT_DIR} COMPONENT ${RUNTIME_COMPONENT_NAME})
@ -362,6 +366,8 @@ elseif(WIN32)
COMPONENT ${RUNTIME_COMPONENT_NAME})
install(DIRECTORY ${TOP_DIR}/include/api/ DESTINATION ${RUNTIME_INC_DIR}/api
COMPONENT ${RUNTIME_COMPONENT_NAME} FILES_MATCHING PATTERN "*.h" PATTERN "ops*" EXCLUDE)
install(DIRECTORY ${TOP_DIR}/include/c_api/ DESTINATION ${RUNTIME_INC_DIR}/c_api
COMPONENT ${RUNTIME_COMPONENT_NAME} FILES_MATCHING PATTERN "*.h")
if(MSVC)
install(FILES ${TOP_DIR}/build/mindspore/src/${MINDSPORE_LITE_LIB_NAME}.lib DESTINATION ${RUNTIME_LIB_DIR}
COMPONENT ${RUNTIME_COMPONENT_NAME})
@ -404,6 +410,8 @@ else()
COMPONENT ${RUNTIME_COMPONENT_NAME})
install(DIRECTORY ${TOP_DIR}/include/api/ DESTINATION ${RUNTIME_INC_DIR}/api
COMPONENT ${RUNTIME_COMPONENT_NAME} FILES_MATCHING PATTERN "*.h" PATTERN "ops*" EXCLUDE)
install(DIRECTORY ${TOP_DIR}/include/c_api/ DESTINATION ${RUNTIME_INC_DIR}/c_api
COMPONENT ${RUNTIME_COMPONENT_NAME} FILES_MATCHING PATTERN "*.h")
install(FILES ${TOP_DIR}/mindspore/lite/build/src/${MINDSPORE_LITE_LIB_NAME}.so DESTINATION ${RUNTIME_LIB_DIR}
COMPONENT ${RUNTIME_COMPONENT_NAME})
install(FILES ${TOP_DIR}/mindspore/lite/build/src/${MINDSPORE_LITE_LIB_NAME}.a DESTINATION ${RUNTIME_LIB_DIR}

View File

@ -41,6 +41,7 @@ class DeviceInfoContext;
/// \brief Context is used to store environment variables during execution.
class MS_API Context {
public:
struct Data;
Context();
~Context() = default;
@ -104,7 +105,6 @@ class MS_API Context {
std::vector<std::shared_ptr<DeviceInfoContext>> &MutableDeviceInfo();
private:
struct Data;
std::shared_ptr<Data> data_;
};

View File

@ -32,12 +32,14 @@ typedef enum {
} SchemaVersion;
using KernelIter = std::vector<kernel::Kernel *>::iterator;
template <class T>
class MS_API DelegateModel {
public:
/// \brief Constructor of MindSpore Lite DelegateModel.
DelegateModel(std::vector<kernel::Kernel *> *kernels, const std::vector<MSTensor> &inputs,
const std::vector<MSTensor> &outputs,
const std::map<kernel::Kernel *, const schema::Primitive *> &primitives, SchemaVersion version)
const std::vector<MSTensor> &outputs, const std::map<kernel::Kernel *, const T *> &primitives,
SchemaVersion version)
: kernels_(kernels), inputs_(inputs), outputs_(outputs), primitives_(primitives), version_(version) {}
/// \brief Destructor of MindSpore Lite DelegateModel.
@ -47,18 +49,24 @@ class MS_API DelegateModel {
///
/// \param[in] a kernel in DelegateModel kernels vector.
///
/// \return The schema::Primitive of The kernel.
const schema::Primitive *GetPrimitive(kernel::Kernel *kernel) const;
/// \return The Primitive of The kernel.
const T *GetPrimitive(kernel::Kernel *kernel) const {
if (primitives_.find(kernel) != primitives_.end()) {
return primitives_.at(kernel);
} else {
return nullptr;
}
}
/// \brief Get the begin iterator of the DelegateModel kernels vector.
///
/// \return The begin iterator of the DelegateModel kernels vector.
KernelIter BeginKernelIterator();
KernelIter BeginKernelIterator() { return kernels_->begin(); }
/// \brief Get the end iterator of the DelegateModel kernels vector.
///
/// \return The end iterator of the DelegateModel kernels vector.
KernelIter EndKernelIterator();
KernelIter EndKernelIterator() { return kernels_->end(); }
/// \brief Replace the continuous kernel supported by the delegate with a delegate graph kernel.
///
@ -66,7 +74,15 @@ class MS_API DelegateModel {
/// \param[in] end Define the end iterator of continuous kernel supported by the delegate.
///
/// \return The next iterator after graph_kernel, point to the next kernel that is not visited.
KernelIter Replace(KernelIter from, KernelIter end, kernel::Kernel *graph_kernel);
KernelIter Replace(KernelIter from, KernelIter end, kernel::Kernel *graph_kernel) {
size_t insert_index = from - BeginKernelIterator();
if (insert_index >= kernels_->size()) {
return BeginKernelIterator();
}
kernels_->erase(from, end);
kernels_->insert(BeginKernelIterator() + insert_index, graph_kernel);
return BeginKernelIterator() + insert_index + 1;
}
/// \brief Get the input tensors of DelegateModel.
///
@ -87,7 +103,7 @@ class MS_API DelegateModel {
std::vector<kernel::Kernel *> *kernels_;
const std::vector<mindspore::MSTensor> &inputs_;
const std::vector<mindspore::MSTensor> &outputs_;
const std::map<kernel::Kernel *, const schema::Primitive *> &primitives_;
const std::map<kernel::Kernel *, const T *> &primitives_;
SchemaVersion version_;
};
@ -111,7 +127,7 @@ class MS_API Delegate {
/// \note Build willed be called in Model::Build.
///
/// \param[in] model Define the delegate model to be built.
virtual Status Build(DelegateModel *model) = 0;
virtual Status Build(DelegateModel<schema::Primitive> *model) = 0;
};
} // namespace mindspore
#endif // MINDSPORE_INCLUDE_API_DELEGATE_H

View File

@ -61,9 +61,14 @@ file(GLOB CXX_API_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/cxx_api/tensor/*.cc
)
file(GLOB C_API_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/c_api/*.cc
)
set(API_SRC
${CORE_DIR}/utils/status.cc
${CXX_API_SRCS}
${C_API_SRCS}
)
file(GLOB CXX_API_TRAIN_SRCS
@ -160,11 +165,6 @@ set(LITE_SRC
${KERNEL_REG_SRC}
)
set(LITE_SRC
${LITE_SRC}
${CMAKE_CURRENT_SOURCE_DIR}/delegate/delegate.cc
)
if(MSLITE_GPU_BACKEND STREQUAL opencl)
file(GLOB_RECURSE OPENCL_RUNTIME_SRC
${CMAKE_CURRENT_SOURCE_DIR}/runtime/gpu/opencl/*.cc

View File

@ -13,15 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "include/api/context.h"
#include "src/cxx_api/context.h"
#include <string>
#include <memory>
#ifndef SUPPORT_NNIE
#include <any>
#else
#include <experimental/any>
#endif
#include "include/api/types.h"
#include "include/api/data_type.h"
#include "src/runtime/inner_allocator.h"
@ -47,24 +41,6 @@ constexpr auto KModelOptionAscend310FusionSwitchCfgPath = "mindspore.option.asce
constexpr auto kModelOptionAscend310DynamicBatchSize = "mindspore.option.ascend310.dynamic_batch_size";
constexpr auto kModelOptionAscend310BufferOptimize = "mindspore.option.ascend310.buffer_optimize";
struct Context::Data {
std::vector<std::shared_ptr<DeviceInfoContext>> device_info_list;
int32_t thread_num = 2;
bool enable_parallel_ = false;
std::vector<int32_t> affinity_core_list_;
int affinity_mode_ = 0;
std::shared_ptr<Delegate> delegate = nullptr;
};
struct DeviceInfoContext::Data {
#ifndef SUPPORT_NNIE
std::map<std::string, std::any> params;
#else
std::map<std::string, std::experimental::any> params;
#endif
std::shared_ptr<Allocator> allocator = nullptr;
};
Context::Context() : data_(std::shared_ptr<Data>(new (std::nothrow) Data())) {}
template <class T, typename U = std::remove_cv_t<std::remove_reference_t<T>>>

View File

@ -0,0 +1,51 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_SRC_CXX_API_CONTEXT_H_
#define MINDSPORE_LITE_SRC_CXX_API_CONTEXT_H_
#include <cstdint>
#include <map>
#include <vector>
#include <string>
#include <memory>
#ifndef SUPPORT_NNIE
#include <any>
#else
#include <experimental/any>
#endif
#include "include/api/context.h"
namespace mindspore {
struct Context::Data {
std::vector<std::shared_ptr<DeviceInfoContext>> device_info_list;
int32_t thread_num = 2;
bool enable_parallel_ = false;
std::vector<int32_t> affinity_core_list_;
int affinity_mode_ = 0;
std::shared_ptr<Delegate> delegate = nullptr;
};
struct DeviceInfoContext::Data {
#ifndef SUPPORT_NNIE
std::map<std::string, std::any> params;
#else
std::map<std::string, std::experimental::any> params;
#endif
std::shared_ptr<Allocator> allocator = nullptr;
};
} // namespace mindspore
#endif // MINDSPORE_LITE_SRC_CXX_API_CONTEXT_H_

View File

@ -14,114 +14,135 @@
* limitations under the License.
*/
#include "src/cxx_api/converters.h"
#include <cstddef>
#include <string>
#include <vector>
#include <memory>
#include "include/context.h"
#include "include/api/context.h"
#include "src/runtime/inner_allocator.h"
#include "src/common/log_adapter.h"
#include "nnacl/op_base.h"
namespace mindspore {
constexpr static int kMaxNumOfDevices = 3;
Status AddCpuDevice(Context *a_context, lite::InnerContext *l_context, DeviceInfoContext *device) {
auto cpu_context = device->Cast<CPUDeviceInfo>();
l_context->allocator = cpu_context->GetAllocator();
if (l_context->allocator == nullptr) {
l_context->allocator = Allocator::Create();
if (l_context->allocator == nullptr) {
MS_LOG(ERROR) << "Create Allocator failed.";
return kLiteNullptr;
}
MS_LOG(DEBUG) << "Set new allocator.";
cpu_context->SetAllocator(l_context->allocator);
}
void ContextUtils::SetContextAttr(int32_t thread_num, bool enable_parallel,
const std::vector<int32_t> &affinity_core_list,
const std::shared_ptr<Delegate> &delegate, lite::InnerContext *inner_context) {
inner_context->thread_num_ = thread_num;
inner_context->enable_parallel_ = enable_parallel;
inner_context->affinity_core_list_ = affinity_core_list;
inner_context->delegate = delegate;
}
if (!IsAffinityModeValid(a_context->GetThreadAffinityMode())) {
MS_LOG(ERROR)
<< "Invalid affinity mode, only supports 0: no affinities, 1: big cores first, 2: little cores first.";
Status ContextUtils::AddCpuDevice(const std::shared_ptr<Allocator> &allocator, int affinity_mode, bool enable_fp16,
const std::string &provider, const std::string &provider_device,
lite::InnerContext *inner_context) {
inner_context->allocator = allocator;
if (!IsAffinityModeValid(affinity_mode)) {
MS_LOG(ERROR) << "Invalid affinity mode, only supports 0:no affinities, 1:big cores first, 2:little cores first.";
return kLiteInputParamInvalid;
}
lite::CpuBindMode mode = A2L_ConvertAffinityMode(a_context->GetThreadAffinityMode());
lite::DeviceInfo cpu_info = {0};
cpu_info.cpu_device_info_ = {cpu_context->GetEnableFP16(), mode};
l_context->device_list_.push_back({lite::DT_CPU, cpu_info, cpu_context->GetProvider(),
cpu_context->GetProviderDevice(), cpu_context->GetAllocator()});
return kSuccess;
}
Status AddGpuDevice(Context *a_context, lite::InnerContext *l_context, DeviceInfoContext *device) {
lite::DeviceInfo device_info = {0};
auto gpu_context = device->Cast<GPUDeviceInfo>();
device_info.gpu_device_info_ = {gpu_context->GetEnableFP16(), gpu_context->GetDeviceID()};
l_context->device_list_.push_back({lite::DT_GPU, device_info, gpu_context->GetProvider(),
gpu_context->GetProviderDevice(), gpu_context->GetAllocator()});
device_info.cpu_device_info_ = {enable_fp16, static_cast<lite::CpuBindMode>(affinity_mode)};
inner_context->device_list_.push_back({lite::DT_CPU, device_info, provider, provider_device, allocator});
return kSuccess;
}
Status AddNpuDevice(Context *a_context, lite::InnerContext *l_context, DeviceInfoContext *device) {
Status ContextUtils::AddGpuDevice(bool enable_fp16, uint32_t device_id, const std::string &provider,
const std::string &provider_device, const std::shared_ptr<Allocator> &allocator,
lite::InnerContext *inner_context) {
lite::DeviceInfo device_info = {0};
auto npu_context = device->Cast<KirinNPUDeviceInfo>();
device_info.npu_device_info_ = {npu_context->GetFrequency()};
l_context->device_list_.push_back({lite::DT_NPU, device_info});
device_info.gpu_device_info_ = {enable_fp16, device_id};
inner_context->device_list_.push_back({lite::DT_GPU, device_info, provider, provider_device, allocator});
return kSuccess;
}
Status AddAscend310Device(Context *a_context, lite::InnerContext *l_context, DeviceInfoContext *device) {
Status ContextUtils::AddNpuDevice(int frequency, lite::InnerContext *inner_context) {
lite::DeviceInfo device_info = {0};
device_info.npu_device_info_ = {frequency};
inner_context->device_list_.push_back({lite::DT_NPU, device_info});
return kSuccess;
}
Status ContextUtils::AddAscend310Device(lite::InnerContext *inner_context, DeviceInfoContext *device) {
lite::DeviceInfo device_info = {0};
auto ascend310_context = device->Cast<Ascend310DeviceInfo>();
device_info.ascend310_device_info_ = {ascend310_context->GetDeviceID()};
l_context->device_list_.push_back({lite::DT_ASCEND310, device_info});
inner_context->device_list_.push_back({lite::DT_ASCEND310, device_info});
return kSuccess;
}
Status A2L_ConvertContext(Context *a_context, lite::InnerContext *l_context) {
if ((a_context == nullptr) || (l_context == nullptr)) {
lite::InnerContext *ContextUtils::Convert(Context *context) {
auto inner_context = std::make_unique<lite::InnerContext>();
if ((context == nullptr) || (inner_context == nullptr)) {
MS_LOG(ERROR) << "Invalid context pointers.";
return kLiteNullptr;
return nullptr;
}
auto device_list = a_context->MutableDeviceInfo();
if (device_list.size() == 0) {
MS_LOG(ERROR) << "Invalid device list.";
return kLiteInputParamInvalid;
auto device_list = context->MutableDeviceInfo();
if (device_list.size() == 0 || device_list.size() > kMaxNumOfDevices) {
MS_LOG(ERROR) << "Device num, support min: 1, max: " << kMaxNumOfDevices;
return nullptr;
}
if (device_list.size() > kMaxNumOfDevices) {
MS_LOG(ERROR) << "Device support Max: " << kMaxNumOfDevices;
return kLiteInputParamInvalid;
}
l_context->thread_num_ = a_context->GetThreadNum();
l_context->enable_parallel_ = a_context->GetEnableParallel();
l_context->affinity_core_list_ = a_context->GetThreadAffinityCoreList();
l_context->device_list_.clear();
Status error_code;
SetContextAttr(context->GetThreadNum(), context->GetEnableParallel(), context->GetThreadAffinityCoreList(),
context->GetDelegate(), inner_context.get());
inner_context->device_list_.clear();
Status ret = kLiteError;
for (auto &device : device_list) {
MS_CHECK_TRUE_RET(device != nullptr, kLiteNullptr);
MS_CHECK_TRUE_RET(device != nullptr, nullptr);
if (device->GetDeviceType() == kCPU) {
error_code = AddCpuDevice(a_context, l_context, device.get());
auto cpu_context = device->Cast<CPUDeviceInfo>();
if (cpu_context->GetAllocator() == nullptr) {
cpu_context->SetAllocator(Allocator::Create());
}
ret = AddCpuDevice(cpu_context->GetAllocator(), context->GetThreadAffinityMode(), cpu_context->GetEnableFP16(),
cpu_context->GetProvider(), cpu_context->GetProviderDevice(), inner_context.get());
} else if (device->GetDeviceType() == kGPU) {
error_code = AddGpuDevice(a_context, l_context, device.get());
auto gpu_context = device->Cast<GPUDeviceInfo>();
ret = AddGpuDevice(gpu_context->GetEnableFP16(), gpu_context->GetDeviceID(), gpu_context->GetProvider(),
gpu_context->GetProviderDevice(), gpu_context->GetAllocator(), inner_context.get());
} else if (device->GetDeviceType() == kKirinNPU) {
error_code = AddNpuDevice(a_context, l_context, device.get());
auto npu_context = device->Cast<KirinNPUDeviceInfo>();
ret = AddNpuDevice(npu_context->GetFrequency(), inner_context.get());
} else if (device->GetDeviceType() == kAscend310) {
error_code = AddAscend310Device(a_context, l_context, device.get());
} else {
MS_LOG(ERROR) << "Invalid device.";
return kLiteInputParamInvalid;
ret = AddAscend310Device(inner_context.get(), device.get());
}
if (error_code != kSuccess) {
if (ret != kSuccess) {
MS_LOG(ERROR) << "Add device failed!";
return error_code;
return nullptr;
}
}
return inner_context.release();
}
l_context->delegate = a_context->GetDelegate();
return kSuccess;
lite::InnerContext *ContextUtils::Convert(const ContextC *context_c) {
auto inner_context = std::make_unique<lite::InnerContext>();
if ((context_c == nullptr) || (inner_context == nullptr)) {
MS_LOG(ERROR) << "Invalid context pointers.";
return nullptr;
}
auto device_list = context_c->device_info_list;
if (device_list.size() == 0 || device_list.size() > kMaxNumOfDevices) {
MS_LOG(ERROR) << "Device num, support min: 1, max: " << kMaxNumOfDevices;
return nullptr;
}
SetContextAttr(context_c->thread_num, context_c->enable_parallel, context_c->affinity_core_list, context_c->delegate,
inner_context.get());
inner_context->device_list_.clear();
Status ret = kLiteError;
for (auto &device_info_c : device_list) {
MS_CHECK_TRUE_RET(device_info_c != nullptr, nullptr);
if (device_info_c->device_type == kMSDeviceTypeCPU) {
if (device_info_c->allocator == nullptr) {
device_info_c->allocator = Allocator::Create();
}
ret = AddCpuDevice(device_info_c->allocator, context_c->affinity_mode, device_info_c->enable_fp16,
device_info_c->provider, device_info_c->provider_device, inner_context.get());
} else if (device_info_c->device_type == kMSDeviceTypeGPU) {
ret = AddGpuDevice(device_info_c->enable_fp16, 0, device_info_c->provider, device_info_c->provider_device,
device_info_c->allocator, inner_context.get());
} else if (device_info_c->device_type == kMSDeviceTypeKirinNPU) {
ret = AddNpuDevice(device_info_c->frequency, inner_context.get());
}
if (ret != kSuccess) {
MS_LOG(ERROR) << "Add device failed!";
return nullptr;
}
}
return inner_context.release();
}
} // namespace mindspore

View File

@ -13,25 +13,40 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_LITE_SRC_CXX_API_CONVERTERS_H_
#define MINDSPORE_LITE_SRC_CXX_API_CONVERTERS_H_
#include <limits.h>
#include <vector>
#include <string>
#include <memory>
#include "include/api/context.h"
#include "include/api/status.h"
#include "include/api/types.h"
#include "include/lite_types.h"
#include "include/api/cfg.h"
#include "include/train/train_cfg.h"
#include "src/inner_context.h"
#include "src/c_api/context_c.h"
namespace mindspore {
class ContextUtils {
public:
static lite::InnerContext *Convert(Context *context);
static lite::InnerContext *Convert(const ContextC *context_c);
namespace lite {
struct Context;
class TrainCfg;
} // namespace lite
class Context;
class TrainCfg;
private:
static void SetContextAttr(int32_t thread_num, bool enable_parallel, const std::vector<int32_t> &affinity_core_list,
const std::shared_ptr<Delegate> &delegate, lite::InnerContext *inner_context);
static Status AddCpuDevice(const std::shared_ptr<Allocator> &allocator, int affinity_mode, bool enable_fp16,
const std::string &provider, const std::string &provider_device,
lite::InnerContext *inner_context);
static Status AddGpuDevice(bool enable_fp16, uint32_t device_id, const std::string &provider,
const std::string &provider_device, const std::shared_ptr<Allocator> &allocator,
lite::InnerContext *inner_context);
static Status AddNpuDevice(int frequency, lite::InnerContext *inner_context);
static Status AddAscend310Device(lite::InnerContext *inner_context, DeviceInfoContext *device);
static bool IsAffinityModeValid(int affinity_mode) {
return affinity_mode >= lite::NO_BIND && affinity_mode <= lite::MID_CPU;
}
};
inline lite::QuantizationType A2L_ConvertQT(mindspore::QuantizationType qt) {
if (qt == kNoQuant) {
@ -43,25 +58,6 @@ inline lite::QuantizationType A2L_ConvertQT(mindspore::QuantizationType qt) {
return lite::QT_DEFAULT;
}
inline lite::CpuBindMode A2L_ConvertAffinityMode(int affinity_mode) {
switch (affinity_mode) {
case 0:
return lite::NO_BIND;
case 1:
return lite::HIGHER_CPU;
case 2:
return lite::MID_CPU;
default:
return lite::NO_BIND;
}
}
inline bool IsAffinityModeValid(int affinity_mode) {
return affinity_mode >= lite::NO_BIND && affinity_mode <= lite::MID_CPU;
}
Status A2L_ConvertContext(Context *a_context, lite::InnerContext *l_context);
Status A2L_ConvertConfig(const TrainCfg *a_train_cfg, lite::TrainCfg *l_train_cfg);
} // namespace mindspore

View File

@ -54,21 +54,13 @@ Status ModelImpl::Build(const void *model_data, size_t data_size, ModelType mode
return kLiteInputParamInvalid;
}
context_ = ms_context;
auto *lite_context = new (std::nothrow) lite::InnerContext();
MS_CHECK_TRUE_MSG(lite_context != nullptr, kLiteNullptr, "inner context failed");
auto status = A2L_ConvertContext(ms_context.get(), lite_context);
if (status != kSuccess) {
return status;
}
auto session = std::shared_ptr<session::LiteSession>(CreateLiteSession(lite_context));
auto session = std::shared_ptr<lite::LiteSession>(CreateLiteSession(ContextUtils::Convert(ms_context.get())));
if (session == nullptr) {
MS_LOG(ERROR) << "Allocate session failed.";
return kLiteNullptr;
}
auto ret = lite::LiteSession::CreateSessionByBuf(static_cast<const char *>(model_data), data_size, session.get());
auto ret = session->LoadModelAndCompileByBuf(static_cast<const char *>(model_data), data_size);
if (ret != RET_OK) {
MS_LOG(ERROR) << "Init session failed";
return kLiteError;
@ -81,20 +73,13 @@ Status ModelImpl::Build(const void *model_data, size_t data_size, ModelType mode
Status ModelImpl::Build(const std::string &model_path, ModelType model_type,
const std::shared_ptr<Context> &ms_context) {
auto *lite_context = new (std::nothrow) lite::InnerContext();
MS_CHECK_TRUE_MSG(lite_context != nullptr, kLiteNullptr, "inner context failed");
auto status = A2L_ConvertContext(ms_context.get(), lite_context);
if (status != kSuccess) {
return status;
}
auto session = std::shared_ptr<session::LiteSession>(CreateLiteSession(lite_context));
auto session = std::shared_ptr<lite::LiteSession>(CreateLiteSession(ContextUtils::Convert(ms_context.get())));
if (session == nullptr) {
MS_LOG(ERROR) << "Allocate session failed.";
return kLiteNullptr;
}
auto ret = lite::LiteSession::CreateSessionByPath(model_path, session.get());
auto ret = session->LoadModelAndCompileByPath(model_path);
if (ret != RET_OK) {
MS_LOG(ERROR) << "Init session failed";
return kLiteError;
@ -117,17 +102,15 @@ Status ModelImpl::Build() {
return kLiteNullptr;
}
auto *lite_context = new (std::nothrow) lite::InnerContext();
MS_CHECK_TRUE_MSG(lite_context != nullptr, kLiteNullptr, "inner context failed");
auto status = A2L_ConvertContext(context_.get(), lite_context);
if (status != kSuccess) {
auto *inner_context = ContextUtils::Convert(context_.get());
if (inner_context == nullptr) {
MS_LOG(ERROR) << "Failed to convert Context to Lite Context";
return status;
return kLiteNullptr;
}
auto create_callback = CreateTrainSessionCallbackHolder();
if (create_callback != nullptr) {
auto session = create_callback(graph_->graph_data_, cfg_, lite_context);
auto session = create_callback(graph_->graph_data_, cfg_, inner_context);
if (session != nullptr) {
session_ = session;
MS_LOG(DEBUG) << "Build model success.";
@ -137,11 +120,12 @@ Status ModelImpl::Build() {
auto model = graph_->graph_data_->lite_model();
if (model == nullptr || model->buf == nullptr) {
delete inner_context;
MS_LOG(ERROR) << "Lite model has been freed.";
return kLiteError;
}
auto session = std::shared_ptr<session::LiteSession>(CreateLiteSession(lite_context));
auto session = std::shared_ptr<lite::LiteSession>(CreateLiteSession(inner_context));
if (session == nullptr) {
MS_LOG(ERROR) << "Allocate session failed.";
return kLiteNullptr;
@ -560,7 +544,7 @@ Status ModelImpl::Resize(const std::vector<MSTensor> &inputs, const std::vector<
return static_cast<StatusCode>(ret);
}
session::LiteSession *ModelImpl::CreateLiteSession(lite::InnerContext *context) {
lite::LiteSession *ModelImpl::CreateLiteSession(lite::InnerContext *context) {
auto session = new (std::nothrow) lite::LiteSession();
if (session == nullptr) {
MS_LOG(ERROR) << "create session failed";

View File

@ -30,6 +30,7 @@
#include "include/lite_session.h"
#include "src/cxx_api/graph/graph_data.h"
#include "src/inner_context.h"
#include "src/lite_session.h"
template <class T>
void clearVectorOfPointers(std::vector<T> *v) {
@ -43,9 +44,9 @@ void clearVectorOfPointers(std::vector<T> *v) {
namespace mindspore {
typedef std::shared_ptr<session::LiteSession>(CreateTrainSessionProto)(std::shared_ptr<Graph::GraphData> graph_data,
std::shared_ptr<TrainCfg> cfg,
lite::InnerContext *context);
typedef std::shared_ptr<lite::LiteSession>(CreateTrainSessionProto)(std::shared_ptr<Graph::GraphData> graph_data,
std::shared_ptr<TrainCfg> cfg,
lite::InnerContext *context);
CreateTrainSessionProto *CreateTrainSessionCallbackHolder(CreateTrainSessionProto *proto = nullptr);
namespace session {
@ -67,7 +68,7 @@ class ModelImpl {
Status Predict(const std::vector<MSTensor> &inputs, std::vector<MSTensor> *outputs, const MSKernelCallBack &before,
const MSKernelCallBack &after);
session::LiteSession *CreateLiteSession(lite::InnerContext *context);
lite::LiteSession *CreateLiteSession(lite::InnerContext *context);
Status LoadConfig(const std::string &config_path);
std::vector<MSTensor> GetInputs();
@ -103,7 +104,7 @@ class ModelImpl {
friend class Model;
friend class Serialization;
std::shared_ptr<Graph> graph_ = nullptr;
std::shared_ptr<session::LiteSession> session_ = nullptr;
std::shared_ptr<lite::LiteSession> session_ = nullptr;
std::shared_ptr<Context> context_ = nullptr;
std::shared_ptr<TrainCfg> cfg_ = nullptr;
std::vector<Metrics *> metrics_;

View File

@ -40,8 +40,8 @@
#include "src/train/train_session.h"
namespace mindspore {
std::shared_ptr<session::LiteSession> CreateTrainSession(std::shared_ptr<Graph::GraphData> graph_data,
std::shared_ptr<TrainCfg> cfg, lite::InnerContext *context) {
std::shared_ptr<lite::LiteSession> CreateTrainSession(std::shared_ptr<Graph::GraphData> graph_data,
std::shared_ptr<TrainCfg> cfg, lite::InnerContext *context) {
MS_CHECK_TRUE_MSG(graph_data != nullptr, nullptr, "graph data cannot be nullptr");
bool is_train_session = graph_data->IsTrainModel();
if (is_train_session) {
@ -50,7 +50,7 @@ std::shared_ptr<session::LiteSession> CreateTrainSession(std::shared_ptr<Graph::
MS_LOG(ERROR) << "Lite model has been freed.";
return nullptr;
}
std::shared_ptr<session::LiteSession> shared_session;
std::shared_ptr<lite::LiteSession> shared_session;
auto session = new (std::nothrow) lite::TrainSession();
if (session == nullptr) {
MS_LOG(ERROR) << "create session failed";

View File

@ -1,40 +0,0 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "include/api/delegate.h"
namespace mindspore {
const schema::Primitive *DelegateModel::GetPrimitive(kernel::Kernel *kernel) const {
if (primitives_.find(kernel) != primitives_.end()) {
return primitives_.at(kernel);
} else {
return nullptr;
}
}
KernelIter DelegateModel::BeginKernelIterator() { return kernels_->begin(); }
KernelIter DelegateModel::EndKernelIterator() { return kernels_->end(); }
KernelIter DelegateModel::Replace(KernelIter from, KernelIter end, kernel::Kernel *graph_kernel) {
size_t insert_index = from - BeginKernelIterator();
if (insert_index >= kernels_->size()) {
return BeginKernelIterator();
}
kernels_->erase(from, end);
kernels_->insert(BeginKernelIterator() + insert_index, graph_kernel);
return BeginKernelIterator() + insert_index + 1;
}
} // namespace mindspore

View File

@ -94,8 +94,8 @@ std::vector<mindspore::MSTensor> GetGraphOutTensors(const std::vector<T *> &ops)
}
template <typename T>
std::vector<mindspore::MSTensor> GraphInTensors(const std::vector<T *> &ops, DelegateModel *model, KernelIter from,
KernelIter end) {
std::vector<mindspore::MSTensor> GraphInTensors(const std::vector<T *> &ops, DelegateModel<schema::Primitive> *model,
KernelIter from, KernelIter end) {
auto in_tensors = GetGraphInTensors(ops, nullptr);
std::vector<mindspore::MSTensor> all_in_tensors;
for (auto op : ops) {
@ -121,8 +121,8 @@ std::vector<mindspore::MSTensor> GraphInTensors(const std::vector<T *> &ops, Del
}
template <typename T>
std::vector<mindspore::MSTensor> GraphOutTensors(const std::vector<T *> &ops, DelegateModel *model, KernelIter from,
KernelIter end) {
std::vector<mindspore::MSTensor> GraphOutTensors(const std::vector<T *> &ops, DelegateModel<schema::Primitive> *model,
KernelIter from, KernelIter end) {
auto out_tensors = GetGraphOutTensors(ops);
std::vector<mindspore::MSTensor> all_out_tensors;
for (auto op : ops) {

View File

@ -197,7 +197,7 @@ Status NPUDelegate::Init() {
return mindspore::kSuccess;
}
Status NPUDelegate::Build(DelegateModel *model) {
Status NPUDelegate::Build(DelegateModel<schema::Primitive> *model) {
KernelIter from, end;
std::vector<NPUOp *> npu_ops;
int graph_index = 0;
@ -286,7 +286,8 @@ NPUOp *NPUDelegate::GetOP(kernel::Kernel *kernel, const schema::Primitive *primi
return npu_op;
}
std::vector<mindspore::MSTensor> GraphOutTensors(const std::vector<NPUOp *> &ops, DelegateModel *model, KernelIter from,
std::vector<mindspore::MSTensor> GraphOutTensors(const std::vector<NPUOp *> &ops,
DelegateModel<schema::Primitive> *model, KernelIter from,
KernelIter end) {
auto out_tensors = lite::GetGraphOutTensors(ops);
std::vector<mindspore::MSTensor> all_out_tensors;
@ -313,8 +314,8 @@ std::vector<mindspore::MSTensor> GraphOutTensors(const std::vector<NPUOp *> &ops
return out_tensors;
}
kernel::Kernel *NPUDelegate::CreateNPUGraph(const std::vector<NPUOp *> &ops, DelegateModel *model, KernelIter from,
KernelIter end) {
kernel::Kernel *NPUDelegate::CreateNPUGraph(const std::vector<NPUOp *> &ops, DelegateModel<schema::Primitive> *model,
KernelIter from, KernelIter end) {
auto in_tensors = lite::GetGraphInTensors(ops, nullptr);
auto out_tensors = GraphOutTensors(ops, model, from, end);
auto graph_kernel = new (std::nothrow) NPUGraph(ops, npu_manager_, in_tensors, out_tensors);

View File

@ -34,13 +34,13 @@ class NPUDelegate : public Delegate {
Status Init() override;
Status Build(DelegateModel *model) override;
Status Build(DelegateModel<schema::Primitive> *model) override;
protected:
NPUOp *GetOP(kernel::Kernel *kernel, const schema::Primitive *primitive);
kernel::Kernel *CreateNPUGraph(const std::vector<NPUOp *> &ops, DelegateModel *model, KernelIter from,
KernelIter end);
kernel::Kernel *CreateNPUGraph(const std::vector<NPUOp *> &ops, DelegateModel<schema::Primitive> *model,
KernelIter from, KernelIter end);
Status AddPasses();

View File

@ -119,7 +119,7 @@ Status TensorRTDelegate::Init() {
return mindspore::kSuccess;
}
Status TensorRTDelegate::Build(DelegateModel *model) {
Status TensorRTDelegate::Build(DelegateModel<schema::Primitive> *model) {
lite::SetCudaDevice(device_info_);
KernelIter from, end;
std::vector<TensorRTOp *> tensorrt_ops;
@ -180,8 +180,9 @@ TensorRTOp *TensorRTDelegate::FindTensorRTOp(kernel::Kernel *kernel, const schem
}
}
TensorRTSubGraph *TensorRTDelegate::CreateTensorRTGraph(const std::vector<TensorRTOp *> &ops, DelegateModel *model,
KernelIter from, KernelIter end) {
TensorRTSubGraph *TensorRTDelegate::CreateTensorRTGraph(const std::vector<TensorRTOp *> &ops,
DelegateModel<schema::Primitive> *model, KernelIter from,
KernelIter end) {
auto in_tensors = GraphInTensors<TensorRTOp>(ops, model, from, end);
auto out_tensors = GraphOutTensors<TensorRTOp>(ops, model, from, end);
auto *tensorrt_graph = new (std::nothrow)

View File

@ -39,13 +39,13 @@ class TensorRTDelegate : public Delegate {
Status Init() override;
Status Build(DelegateModel *model) override;
Status Build(DelegateModel<schema::Primitive> *model) override;
private:
TensorRTOp *FindTensorRTOp(kernel::Kernel *kernel, const schema::Primitive *primitive);
TensorRTSubGraph *CreateTensorRTGraph(const std::vector<TensorRTOp *> &ops, DelegateModel *model, KernelIter from,
KernelIter end);
TensorRTSubGraph *CreateTensorRTGraph(const std::vector<TensorRTOp *> &ops, DelegateModel<schema::Primitive> *model,
KernelIter from, KernelIter end);
std::map<schema::PrimitiveType, TensorRTGetOp> op_func_lists_;

View File

@ -161,8 +161,8 @@ int InnerContext::Init() {
device_ctx.device_info_.npu_device_info_.frequency_ != hiai::AiModelDescription_Frequency_MEDIUM &&
device_ctx.device_info_.npu_device_info_.frequency_ != hiai::AiModelDescription_Frequency_HIGH &&
device_ctx.device_info_.npu_device_info_.frequency_ != hiai::AiModelDescription_Frequency_EXTREME) {
MS_LOG(INFO) << "NPU frequency set to 3, original value "
<< device_ctx.device_info_.npu_device_info_.frequency_;
MS_LOG(WARNING) << "NPU frequency set to 3, original value "
<< device_ctx.device_info_.npu_device_info_.frequency_;
device_ctx.device_info_.npu_device_info_.frequency_ = hiai::AiModelDescription_Frequency_HIGH;
}
}
@ -187,7 +187,7 @@ int InnerContext::IsValid() const {
return RET_NOT_SUPPORT;
}
if (this->device_list_.size() > kMaxInnerContextDeviceNums) {
MS_LOG(ERROR) << "Not support device list more than 2.";
MS_LOG(ERROR) << "Not support device list more than " << kMaxInnerContextDeviceNums;
return RET_NOT_SUPPORT;
}
if (thread_num_ < 1) {

View File

@ -100,7 +100,7 @@ int LiteModel::ConvertAttrToTensors() {
void LiteModel::Free() {
if (this->buf != nullptr) {
free(this->buf);
delete[](this->buf);
this->buf = nullptr;
}
auto nodes_size = this->all_nodes_.size();
@ -442,7 +442,7 @@ Model *ImportFromBuffer(const char *model_buf, size_t size, bool take_buf) {
delete (model);
return nullptr;
}
model->buf = reinterpret_cast<char *>(malloc(size));
model->buf = new char[size];
if (model->buf == nullptr) {
MS_LOG(ERROR) << "new inner model buf fail!";
delete (model);

View File

@ -1070,7 +1070,7 @@ session::LiteSession *session::LiteSession::CreateSession(const char *model_buf,
MS_LOG(ERROR) << "Create session failed";
return nullptr;
}
auto ret = lite::LiteSession::CreateSessionByBuf(model_buf, size, session);
auto ret = reinterpret_cast<lite::LiteSession *>(session)->LoadModelAndCompileByBuf(model_buf, size);
if (ret != RET_OK) {
MS_LOG(ERROR) << "Init session failed";
delete session;
@ -1085,7 +1085,7 @@ session::LiteSession *lite::LiteSession::CreateSession(const std::string &model_
MS_LOG(ERROR) << "Create session failed";
return nullptr;
}
auto ret = lite::LiteSession::CreateSessionByPath(model_path, session);
auto ret = reinterpret_cast<lite::LiteSession *>(session)->LoadModelAndCompileByPath(model_path);
if (ret != RET_OK) {
MS_LOG(ERROR) << "Init session failed";
delete session;
@ -1094,13 +1094,13 @@ session::LiteSession *lite::LiteSession::CreateSession(const std::string &model_
return session;
}
int lite::LiteSession::CreateSessionByBuf(const char *model_buf, size_t size, session::LiteSession *session) {
auto *model = lite::ImportFromBuffer(model_buf, size, true);
int lite::LiteSession::LoadModelAndCompileByBuf(const char *model_buf, size_t buf_size) {
auto *model = lite::ImportFromBuffer(model_buf, buf_size, true);
if (model == nullptr) {
MS_LOG(ERROR) << "Import model failed";
return RET_ERROR;
}
auto ret = session->CompileGraph(model);
auto ret = CompileGraph(model);
if (ret != lite::RET_OK) {
MS_LOG(ERROR) << "Compile model failed";
model->buf = nullptr;
@ -1108,11 +1108,11 @@ int lite::LiteSession::CreateSessionByBuf(const char *model_buf, size_t size, se
return RET_ERROR;
}
model->buf = nullptr;
(reinterpret_cast<lite::LiteSession *>(session))->set_model(model);
set_model(model);
return RET_OK;
}
int lite::LiteSession::CreateSessionByPath(const std::string &model_path, session::LiteSession *session) {
int lite::LiteSession::LoadModelAndCompileByPath(const std::string &model_path) {
size_t model_size;
auto model_buf = lite::ReadFile(model_path.c_str(), &model_size);
if (model_buf == nullptr) {
@ -1125,12 +1125,12 @@ int lite::LiteSession::CreateSessionByPath(const std::string &model_path, sessio
return RET_ERROR;
}
(reinterpret_cast<lite::LiteModel *>(model))->set_keep_model_buf(true);
auto ret = session->CompileGraph(model);
auto ret = CompileGraph(model);
if (ret != lite::RET_OK) {
MS_LOG(ERROR) << "Compile model failed";
return RET_ERROR;
}
(reinterpret_cast<lite::LiteSession *>(session))->set_model(model);
set_model(model);
return RET_OK;
}

View File

@ -52,8 +52,9 @@ class LiteSession : public session::LiteSession {
static session::LiteSession *CreateSession(const std::string &model_path, const lite::Context *context);
static int CreateSessionByBuf(const char *model_buf, size_t size, session::LiteSession *session);
static int CreateSessionByPath(const std::string &model_path, session::LiteSession *session);
int LoadModelAndCompileByBuf(const char *model_buf, size_t buf_size);
int LoadModelAndCompileByPath(const std::string &model_path);
virtual int Init(InnerContext *context);

View File

@ -390,8 +390,8 @@ int Scheduler::ReplaceDelegateKernels(std::vector<kernel::LiteKernel *> *dst_ker
ms_inputs_ = LiteTensorsToMSTensors(inputs_);
ms_outputs_ = LiteTensorsToMSTensors(outputs_);
auto schema_version = static_cast<SchemaVersion>(schema_version_);
DelegateModel *model =
new (std::nothrow) DelegateModel(&kernels, ms_inputs_, ms_outputs_, primitives_, schema_version);
DelegateModel<schema::Primitive> *model =
new (std::nothrow) DelegateModel<schema::Primitive>(&kernels, ms_inputs_, ms_outputs_, primitives_, schema_version);
if (model == nullptr) {
MS_LOG(ERROR) << "New delegate model failed.";
return RET_NULL_PTR;

View File

@ -29,6 +29,8 @@ file(GLOB_RECURSE TEST_UT_SRC
${TEST_DIR}/ut/src/runtime/kernel/arm/fp32/*.cc
${TEST_DIR}/ut/src/runtime/kernel/arm/int8/*.cc
${TEST_DIR}/ut/src/runtime/kernel/arm/string/*.cc
${TEST_DIR}/ut/src/api/context_c_test.cc
${TEST_DIR}/ut/src/api/tensor_c_test.cc
)
if(MSLITE_ENABLE_TRAIN)
@ -69,6 +71,7 @@ if(MSLITE_ENABLE_CONVERTER)
${LITE_DIR}/tools/benchmark/run_benchmark.cc
${LITE_DIR}/tools/benchmark/benchmark_base.cc
${LITE_DIR}/tools/benchmark/benchmark_unified_api.cc
${LITE_DIR}/tools/benchmark/benchmark_c_api.cc
${LITE_DIR}/tools/benchmark/benchmark.cc
)
endif()

View File

@ -1 +1 @@
751952
782672

View File

@ -99,3 +99,7 @@ echo 'runtime pass'
echo 'Runtime config file test'
./lite-test --gtest_filter="MixDataTypeTest.Config1"
echo 'run c api ut test'
./lite-test --gtest_filter="TensorCTest.*"
./lite-test --gtest_filter="ContextCTest.*"

View File

@ -62,10 +62,10 @@ class CustomDelegate : public Delegate {
Status Init() override { return mindspore::kSuccess; }
Status Build(DelegateModel *model) override;
Status Build(DelegateModel<schema::Primitive> *model) override;
};
Status CustomDelegate::Build(DelegateModel *model) {
Status CustomDelegate::Build(DelegateModel<schema::Primitive> *model) {
auto graph_kernel = new (std::nothrow) CustomSubgraph(model->inputs(), model->outputs());
if (graph_kernel == nullptr) {
return mindspore::kLiteNullptr;

View File

@ -375,13 +375,11 @@ TEST_F(MultipleDeviceTest, NewApi2) {
context.MutableDeviceInfo().push_back(std::make_shared<mindspore::CPUDeviceInfo>());
context.MutableDeviceInfo().push_back(std::make_shared<mindspore::GPUDeviceInfo>());
mindspore::lite::InnerContext inner_context;
mindspore::A2L_ConvertContext(&context, &inner_context);
ASSERT_EQ(inner_context.device_list_.size(), 3);
ASSERT_EQ(inner_context.device_list_.at(0).device_type_, mindspore::lite::DT_NPU);
ASSERT_EQ(inner_context.device_list_.at(1).device_type_, mindspore::lite::DT_CPU);
ASSERT_EQ(inner_context.device_list_.at(2).device_type_, mindspore::lite::DT_GPU);
auto inner_context = std::shared_ptr<mindspore::lite::InnerContext>(mindspore::ContextUtils::Convert(&context));
ASSERT_EQ(inner_context->device_list_.size(), 3);
ASSERT_EQ(inner_context->device_list_.at(0).device_type_, mindspore::lite::DT_NPU);
ASSERT_EQ(inner_context->device_list_.at(1).device_type_, mindspore::lite::DT_CPU);
ASSERT_EQ(inner_context->device_list_.at(2).device_type_, mindspore::lite::DT_GPU);
}
TEST_F(MultipleDeviceTest, NewApi3) {
@ -389,12 +387,10 @@ TEST_F(MultipleDeviceTest, NewApi3) {
context.MutableDeviceInfo().push_back(std::make_shared<mindspore::CPUDeviceInfo>());
context.MutableDeviceInfo().push_back(std::make_shared<mindspore::KirinNPUDeviceInfo>());
mindspore::lite::InnerContext inner_context;
mindspore::A2L_ConvertContext(&context, &inner_context);
ASSERT_EQ(inner_context.device_list_.size(), 2);
ASSERT_EQ(inner_context.device_list_.at(0).device_type_, mindspore::lite::DT_CPU);
ASSERT_EQ(inner_context.device_list_.at(1).device_type_, mindspore::lite::DT_NPU);
auto inner_context = std::shared_ptr<mindspore::lite::InnerContext>(mindspore::ContextUtils::Convert(&context));
ASSERT_EQ(inner_context->device_list_.size(), 2);
ASSERT_EQ(inner_context->device_list_.at(0).device_type_, mindspore::lite::DT_CPU);
ASSERT_EQ(inner_context->device_list_.at(1).device_type_, mindspore::lite::DT_NPU);
}
TEST_F(MultipleDeviceTest, NewApi4) {
@ -402,12 +398,10 @@ TEST_F(MultipleDeviceTest, NewApi4) {
context.MutableDeviceInfo().push_back(std::make_shared<mindspore::GPUDeviceInfo>());
context.MutableDeviceInfo().push_back(std::make_shared<mindspore::CPUDeviceInfo>());
mindspore::lite::InnerContext inner_context;
mindspore::A2L_ConvertContext(&context, &inner_context);
ASSERT_EQ(inner_context.device_list_.size(), 2);
ASSERT_EQ(inner_context.device_list_.at(0).device_type_, mindspore::lite::DT_GPU);
ASSERT_EQ(inner_context.device_list_.at(1).device_type_, mindspore::lite::DT_CPU);
auto inner_context = std::shared_ptr<mindspore::lite::InnerContext>(mindspore::ContextUtils::Convert(&context));
ASSERT_EQ(inner_context->device_list_.size(), 2);
ASSERT_EQ(inner_context->device_list_.at(0).device_type_, mindspore::lite::DT_GPU);
ASSERT_EQ(inner_context->device_list_.at(1).device_type_, mindspore::lite::DT_CPU);
}
TEST_F(MultipleDeviceTest, NewApi5) {

View File

@ -14,6 +14,7 @@ add_executable(benchmark
${CMAKE_CURRENT_SOURCE_DIR}/benchmark_base.cc
${CMAKE_CURRENT_SOURCE_DIR}/benchmark.cc
${CMAKE_CURRENT_SOURCE_DIR}/benchmark_unified_api.cc
${CMAKE_CURRENT_SOURCE_DIR}/benchmark_c_api.cc
${COMMON_SRC})
add_dependencies(benchmark fbs_src)

View File

@ -113,43 +113,13 @@ int Benchmark::ReadInputFile() {
return RET_OK;
}
int Benchmark::ReadTensorData(std::ifstream &in_file_stream, const std::string &tensor_name,
const std::vector<size_t> &dims) {
std::string line;
getline(in_file_stream, line);
std::stringstream line_stream(line);
if (this->benchmark_data_.find(tensor_name) != this->benchmark_data_.end()) {
return RET_OK;
}
tensor::MSTensor *tensor = session_->GetOutputByTensorName(tensor_name);
if (tensor == nullptr) {
MS_LOG(ERROR) << "Get tensor failed, tensor name: " << tensor_name;
return RET_ERROR;
}
std::vector<float> data;
std::vector<std::string> strings_data;
size_t shape_size = std::accumulate(dims.begin(), dims.end(), 1, std::multiplies<size_t>());
if (tensor->data_type() == kObjectTypeString) {
strings_data.push_back(line);
for (size_t i = 1; i < shape_size; i++) {
getline(in_file_stream, line);
strings_data.push_back(line);
}
int Benchmark::GetDataTypeByTensorName(const std::string &tensor_name) {
auto tensor = session_->GetOutputByTensorName(tensor_name);
if (tensor != nullptr) {
return tensor->data_type();
} else {
for (size_t i = 0; i < shape_size; i++) {
float tmp_data;
line_stream >> tmp_data;
data.push_back(tmp_data);
}
return kTypeUnknown;
}
auto *check_tensor = new (std::nothrow) CheckTensor(dims, data, strings_data);
if (check_tensor == nullptr) {
MS_LOG(ERROR) << "New CheckTensor failed, tensor name: " << tensor_name;
return RET_ERROR;
}
this->benchmark_tensor_names_.push_back(tensor_name);
this->benchmark_data_.insert(std::make_pair(tensor_name, check_tensor));
return RET_OK;
}
void Benchmark::InitContext(const std::shared_ptr<Context> &context) {
@ -503,13 +473,6 @@ int Benchmark::RunBenchmark() {
}
if (!flags_->benchmark_data_file_.empty()) {
status = MarkAccuracy();
for (auto &data : benchmark_data_) {
data.second->shape.clear();
data.second->data.clear();
delete data.second;
data.second = nullptr;
}
benchmark_data_.clear();
if (status != 0) {
MS_LOG(ERROR) << "Run MarkAccuracy error: " << status;
std::cout << "Run MarkAccuracy error: " << status << std::endl;
@ -742,7 +705,7 @@ std::string GenerateOutputFileName(tensor::MSTensor *tensor, const std::string &
if (kTypeIdMap.find(tensor->data_type()) != kTypeIdMap.end()) {
file_name += kTypeIdMap.at(tensor->data_type());
}
auto tensor_format = static_cast<schema::Format>(static_cast<lite::Tensor *>(tensor)->format());
auto tensor_format = static_cast<lite::Tensor *>(tensor)->format();
if (kTensorFormatMap.find(tensor_format) != kTensorFormatMap.end()) {
file_name += "_" + kTensorFormatMap.at(tensor_format) + ".bin";
}

View File

@ -53,8 +53,7 @@ class MS_API Benchmark : public BenchmarkBase {
int ReadInputFile() override;
int ReadTensorData(std::ifstream &in_file_stream, const std::string &tensor_name,
const std::vector<size_t> &dims) override;
int GetDataTypeByTensorName(const std::string &tensor_name) override;
void InitContext(const std::shared_ptr<Context> &context);

View File

@ -64,12 +64,12 @@ const std::unordered_map<int, std::string> kTypeIdMap{
{kNumberTypeInt32, "Int32"}, {kNumberTypeUInt8, "UInt8"}, {kNumberTypeUInt16, "UInt16"},
{kNumberTypeUInt, "UInt32"}, {kNumberTypeUInt32, "UInt32"}, {kObjectTypeString, "String"},
{kNumberTypeBool, "Bool"}, {kObjectTypeTensorType, "Tensor"}};
const std::unordered_map<schema::Format, std::string> kTensorFormatMap{
{schema::Format_NCHW, "NCHW"}, {schema::Format_NHWC, "NHWC"}, {schema::Format_NHWC4, "NHWC4"},
{schema::Format_HWKC, "HWKC"}, {schema::Format_HWCK, "HWCK"}, {schema::Format_KCHW, "KCHW"},
{schema::Format_CKHW, "CKHW"}, {schema::Format_KHWC, "KHWC"}, {schema::Format_CHWK, "CHWK"},
{schema::Format_HW, "HW"}, {schema::Format_HW4, "HW4"}, {schema::Format_NC, "NC"},
{schema::Format_NC4, "NC4"}, {schema::Format_NC4HW4, "NC4HW4"}, {schema::Format_NCDHW, "NCDHW"}};
const std::unordered_map<mindspore::Format, std::string> kTensorFormatMap{
{mindspore::NCHW, "NCHW"}, {mindspore::NHWC, "NHWC"}, {mindspore::NHWC4, "NHWC4"}, {mindspore::HWKC, "HWKC"},
{mindspore::HWCK, "HWCK"}, {mindspore::KCHW, "KCHW"}, {mindspore::CKHW, "CKHW"}, {mindspore::KHWC, "KHWC"},
{mindspore::CHWK, "CHWK"}, {mindspore::HW, "HW"}, {mindspore::HW4, "HW4"}, {mindspore::NC, "NC"},
{mindspore::NC4, "NC4"}, {mindspore::NC4HW4, "NC4HW4"}, {mindspore::NCDHW, "NCDHW"}};
int BenchmarkBase::GenerateRandomData(size_t size, void *data, int data_type) {
MS_ASSERT(data != nullptr);
@ -173,6 +173,40 @@ int BenchmarkBase::ReadCalibData() {
return RET_OK;
}
int BenchmarkBase::ReadTensorData(std::ifstream &in_file_stream, const std::string &tensor_name,
const std::vector<size_t> &dims) {
std::string line;
getline(in_file_stream, line);
std::stringstream line_stream(line);
if (this->benchmark_data_.find(tensor_name) != this->benchmark_data_.end()) {
return RET_OK;
}
std::vector<float> data;
std::vector<std::string> strings_data;
size_t shape_size = std::accumulate(dims.begin(), dims.end(), 1, std::multiplies<size_t>());
if (GetDataTypeByTensorName(tensor_name) == static_cast<int>(kObjectTypeString)) {
strings_data.push_back(line);
for (size_t i = 1; i < shape_size; i++) {
getline(in_file_stream, line);
strings_data.push_back(line);
}
} else {
for (size_t i = 0; i < shape_size; i++) {
float tmp_data;
line_stream >> tmp_data;
data.push_back(tmp_data);
}
}
auto *check_tensor = new (std::nothrow) CheckTensor(dims, data, strings_data);
if (check_tensor == nullptr) {
MS_LOG(ERROR) << "New CheckTensor failed, tensor name: " << tensor_name;
return RET_ERROR;
}
this->benchmark_tensor_names_.push_back(tensor_name);
this->benchmark_data_.insert(std::make_pair(tensor_name, check_tensor));
return RET_OK;
}
int BenchmarkBase::CompareStringData(const std::string &name, const std::vector<std::string> &calib_strings,
const std::vector<std::string> &output_strings) {
size_t compare_num = std::min(calib_strings.size(), output_strings.size());
@ -574,8 +608,11 @@ int BenchmarkBase::PrintPerfResult(const std::vector<std::string> &title,
#endif
BenchmarkBase::~BenchmarkBase() {
for (const auto &iter : this->benchmark_data_) {
delete (iter.second);
for (auto &iter : this->benchmark_data_) {
iter.second->shape.clear();
iter.second->data.clear();
delete iter.second;
iter.second = nullptr;
}
this->benchmark_data_.clear();
}

View File

@ -31,6 +31,7 @@
#include <utility>
#include <nlohmann/json.hpp>
#include "include/model.h"
#include "include/api/format.h"
#include "tools/common/flag_parser.h"
#include "src/common/file_utils.h"
#include "src/common/utils.h"
@ -38,6 +39,12 @@
#include "schema/model_generated.h"
namespace mindspore::lite {
#define BENCHMARK_LOG_ERROR(str) \
do { \
MS_LOG(ERROR) << str; \
std::cerr << str << std::endl; \
} while (0);
enum MS_API InDataType { kImage = 0, kBinary = 1 };
enum MS_API AiModelDescription_Frequency {
@ -60,7 +67,7 @@ constexpr const char *DELIM_COMMA = ",";
constexpr const char *DELIM_SLASH = "/";
extern const std::unordered_map<int, std::string> kTypeIdMap;
extern const std::unordered_map<schema::Format, std::string> kTensorFormatMap;
extern const std::unordered_map<mindspore::Format, std::string> kTensorFormatMap;
//
namespace dump {
@ -184,8 +191,9 @@ class MS_API BenchmarkBase {
int ReadCalibData();
virtual int ReadTensorData(std::ifstream &in_file_stream, const std::string &tensor_name,
const std::vector<size_t> &dims) = 0;
int ReadTensorData(std::ifstream &in_file_stream, const std::string &tensor_name, const std::vector<size_t> &dims);
virtual int GetDataTypeByTensorName(const std::string &tensor_name) = 0;
virtual int CompareOutput() = 0;

View File

@ -120,43 +120,8 @@ int BenchmarkUnifiedApi::ReadInputFile() {
return RET_OK;
}
int BenchmarkUnifiedApi::ReadTensorData(std::ifstream &in_file_stream, const std::string &tensor_name,
const std::vector<size_t> &dims) {
std::string line;
getline(in_file_stream, line);
std::stringstream line_stream(line);
if (this->benchmark_data_.find(tensor_name) != this->benchmark_data_.end()) {
return RET_OK;
}
mindspore::MSTensor tensor = ms_model_.GetOutputByTensorName(tensor_name);
if (tensor == nullptr) {
MS_LOG(ERROR) << "Get tensor failed, tensor name: " << tensor_name;
return RET_ERROR;
}
std::vector<float> data;
std::vector<std::string> strings_data;
size_t shape_size = std::accumulate(dims.begin(), dims.end(), 1, std::multiplies<size_t>());
if (static_cast<int>(tensor.DataType()) == kObjectTypeString) {
strings_data.push_back(line);
for (size_t i = 1; i < shape_size; i++) {
getline(in_file_stream, line);
strings_data.push_back(line);
}
} else {
for (size_t i = 0; i < shape_size; i++) {
float tmp_data;
line_stream >> tmp_data;
data.push_back(tmp_data);
}
}
auto *check_tensor = new (std::nothrow) CheckTensor(dims, data, strings_data);
if (check_tensor == nullptr) {
MS_LOG(ERROR) << "New CheckTensor failed, tensor name: " << tensor_name;
return RET_ERROR;
}
this->benchmark_tensor_names_.push_back(tensor_name);
this->benchmark_data_.insert(std::make_pair(tensor_name, check_tensor));
return RET_OK;
int BenchmarkUnifiedApi::GetDataTypeByTensorName(const std::string &tensor_name) {
return static_cast<int>(ms_model_.GetOutputByTensorName(tensor_name).DataType());
}
void BenchmarkUnifiedApi::InitMSContext(const std::shared_ptr<mindspore::Context> &context) {
@ -508,13 +473,6 @@ int BenchmarkUnifiedApi::RunBenchmark() {
}
if (!flags_->benchmark_data_file_.empty()) {
status = MarkAccuracy();
for (auto &data : benchmark_data_) {
data.second->shape.clear();
data.second->data.clear();
delete data.second;
data.second = nullptr;
}
benchmark_data_.clear();
if (status != 0) {
MS_LOG(ERROR) << "Run MarkAccuracy error: " << status;
std::cout << "Run MarkAccuracy error: " << status << std::endl;
@ -746,6 +704,11 @@ std::string GenerateOutputFileName(mindspore::MSTensor *tensor, const std::strin
file_name += kTypeIdMap.at(static_cast<int>(tensor->DataType()));
}
auto tensor_format = tensor->format();
if (kTensorFormatMap.find(tensor_format) != kTensorFormatMap.end()) {
file_name += "_" + kTensorFormatMap.at(tensor_format) + ".bin";
}
file_name += +".bin";
return file_name;
}

View File

@ -58,11 +58,10 @@ class MS_API BenchmarkUnifiedApi : public BenchmarkBase {
int ReadInputFile() override;
int ReadTensorData(std::ifstream &in_file_stream, const std::string &tensor_name,
const std::vector<size_t> &dims) override;
void InitMSContext(const std::shared_ptr<Context> &context);
int GetDataTypeByTensorName(const std::string &tensor_name) override;
int CompareOutput() override;
int InitTimeProfilingCallbackParameter() override;

View File

@ -16,6 +16,7 @@
#include "tools/benchmark/run_benchmark.h"
#include "include/version.h"
#include "src/common/log_adapter.h"
int main(int argc, const char **argv) {
MS_LOG(INFO) << mindspore::lite::Version();

View File

@ -27,7 +27,6 @@
#include "tools/converter/anf_transform.h"
#include "tools/converter/converter_context.h"
#include "tools/common/graph_util.h"
#include "load_mindir/load_model.h"
namespace mindspore {
namespace lite {

View File

@ -144,6 +144,7 @@ getCommonFile() {
while IFS='' read -r line; do cxx_api_files+=("$line"); done < <(ls mindspore/lite/src/cxx_api/model/*.cc)
while IFS='' read -r line; do cxx_api_files+=("$line"); done < <(ls mindspore/lite/src/cxx_api/tensor/*.cc)
while IFS='' read -r line; do cxx_api_files+=("$line"); done < <(ls mindspore/lite/src/cxx_api/*.cc)
while IFS='' read -r line; do cxx_api_files+=("$line"); done < <(ls mindspore/lite/src/c_api/*.cc)
mindrt_files=()
while IFS='' read -r line; do mindrt_files+=("$line"); done < <(ls mindspore/core/mindrt/src/*.cc)
while IFS='' read -r line; do mindrt_files+=("$line"); done < <(ls mindspore/core/mindrt/src/async/*.cc)
@ -303,7 +304,6 @@ chmod 444 ${GPU_MAPPING_OUTPUT_FILE}
# support for npu
npu_files=()
while IFS='' read -r line; do npu_files+=("$line"); done < <(ls mindspore/lite/src/delegate/delegate.cc)
while IFS='' read -r line; do npu_files+=("$line"); done < <(ls mindspore/lite/src/delegate/npu/*.cc)
while IFS='' read -r line; do npu_files+=("$line"); done < <(ls mindspore/lite/src/delegate/npu/op/*.cc)
while IFS='' read -r line; do npu_files+=("$line"); done < <(ls mindspore/lite/src/delegate/npu/pass/*.cc)

View File

@ -88,9 +88,9 @@ FMT_FILE_LIST='__format_files_list__'
if [[ "X${mode}" == "Xall" ]]; then
find mindspore/{ccsrc,core,lite} -type f -name "*" | grep -E "(\.h$|\.cc$|\.c$)" > "${FMT_FILE_LIST}" || true
elif [[ "X${mode}" == "Xchanged" ]]; then
git diff --name-only | grep "mindspore/ccsrc\|mindspore/core\|mindspore/lite" | grep -E "(\.h$|\.cc$|\.c$)" > "${FMT_FILE_LIST}" || true
git diff --name-only | grep "mindspore/ccsrc\|mindspore/core\|mindspore/lite\|include" | grep -E "(\.h$|\.cc$|\.c$)" > "${FMT_FILE_LIST}" || true
else # "X${mode}" == "Xlastcommit"
git diff --name-only HEAD~ HEAD | grep "mindspore/ccsrc\|mindspore/core\|mindspore/lite" | grep -E "(\.h$|\.cc$|\.c$)" > "${FMT_FILE_LIST}" || true
git diff --name-only HEAD~ HEAD | grep "mindspore/ccsrc\|mindspore/core\|mindspore/lite\|include" | grep -E "(\.h$|\.cc$|\.c$)" > "${FMT_FILE_LIST}" || true
fi
while read line; do