[MSLITE][Develop] new api to r1.3

This commit is contained in:
yangruoqi713 2021-07-10 17:16:22 +08:00
parent 8bdb5dad41
commit cc49daf074
12 changed files with 301 additions and 28 deletions

88
include/api/allocator.h Normal file
View File

@ -0,0 +1,88 @@
/**
* 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 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_INCLUDE_API_ALLOCATOR_H
#define MINDSPORE_INCLUDE_API_ALLOCATOR_H
#include <memory>
#include "include/api/types.h"
namespace mindspore {
/// \brief Allocator defined a memory pool for malloc memory and free memory dynamically.
class MS_API Allocator {
public:
/// \brief Destructor of MindSpore Allocator.
virtual ~Allocator() = default;
/// \brief Method to request memory.
///
/// \param[in] size Define the memory size to request.
virtual void *Malloc(size_t size) = 0;
/// \brief Method to free memory.
///
/// \param[in] ptr Define the pointer of a certain memory.
virtual void Free(void *ptr) = 0;
/// \brief Reference count of a certain memory.
///
/// \param[in] ptr Define the pointer of a certain memory.
///
/// \return Reference count of a certain memory currently.
virtual int RefCount(void *ptr) = 0;
/// \brief Set reference count of a certain memory.
///
/// \param[in] ptr Define the pointer of a certain memory.
/// \param[in] ref_count Define the reference count to set.
///
/// \return Reference count of a certain memory after setting.
virtual int SetRefCount(void *ptr, int ref_count) = 0;
/// \brief Decrease the reference count of a certain memory.
///
/// \param[in] ptr Define the pointer of a certain memory.
/// \param[in] ref_count Define the reference count to reduce.
///
/// \return Reference count of a certain memory after decreating.
virtual int DecRefCount(void *ptr, int ref_count) = 0;
/// \brief Increase the reference count of a certain memory.
///
/// \param[in] ptr Define the pointer of a certain memory.
/// \param[in] ref_count Define the reference count to increase.
///
/// \return Reference count of a certain memory after increasing.
virtual int IncRefCount(void *ptr, int ref_count) = 0;
/// \brief Static method to create an allocator.
///
/// \return Smart pointer of an allocator.
static std::shared_ptr<Allocator> Create();
/// \brief Prepare a certain memory.
///
/// \param[in] ptr Define the pointer of a certain memory to prepare.
///
/// \return Pointer of ready memory.
virtual void *Prepare(void *ptr) { return ptr; }
protected:
// memory aligned bytes
size_t aligned_size_ = 32;
};
} // namespace mindspore
#endif // MINDSPORE_INCLUDE_API_ALLOCATOR_H

View File

@ -36,6 +36,7 @@ enum DeviceType {
};
class Allocator;
class Delegate;
class DeviceInfoContext;
class MS_API Context {
@ -57,6 +58,9 @@ class MS_API Context {
void SetEnableParallel(bool is_parallel);
bool GetEnableParallel() const;
void SetDelegate(const std::shared_ptr<Delegate> &delegate);
std::shared_ptr<Delegate> GetDelegate() const;
std::vector<std::shared_ptr<DeviceInfoContext>> &MutableDeviceInfo();
private:

View File

@ -23,6 +23,7 @@ enum class DataType : int {
kObjectTypeList = 13,
kObjectTypeTuple = 14,
kObjectTypeTensorType = 17,
kNumberTypeBegin = 29,
kNumberTypeBool = 30,
kNumberTypeInt8 = 32,
kNumberTypeInt16 = 33,

View File

@ -23,6 +23,7 @@
#include <functional>
#include "include/api/data_type.h"
#include "include/api/dual_abi_helper.h"
#include "ir/format.h"
#ifdef _WIN32
#define MS_API __declspec(dllexport)
@ -41,6 +42,7 @@ enum ModelType : uint32_t {
kUnknownType = 0xFFFFFFFF
};
class Allocator;
class MS_API MSTensor {
public:
class Impl;
@ -76,6 +78,17 @@ class MS_API MSTensor {
MSTensor *Clone() const;
bool operator==(std::nullptr_t) const;
bool operator!=(std::nullptr_t) const;
bool operator==(const MSTensor &tensor) const;
void SetShape(const std::vector<int64_t> &shape);
void SetDataType(enum DataType data_type);
void SetTensorName(const std::string &name);
void SetAllocator(std::shared_ptr<Allocator> allocator);
std::shared_ptr<Allocator> allocator() const;
void SetFormat(mindspore::Format format);
mindspore::Format format() const;
void SetData(void *data);
const std::shared_ptr<Impl> impl() const { return impl_; }
private:
// api without std::string

View File

@ -70,6 +70,7 @@ Status Model::Build(const void *model_data, size_t data_size, ModelType model_ty
MS_LOG(ERROR) << "Unsupported Feature.";
return kMCFailed;
}
Status Model::Resize(const std::vector<MSTensor> &inputs, const std::vector<std::vector<int64_t>> &dims) {
if (impl_ == nullptr) {
MS_LOG(ERROR) << "Failed because this model has not been built.";

View File

@ -40,6 +40,7 @@ struct Context::Data {
bool enable_parallel_ = false;
std::vector<int32_t> affinity_core_list_;
int affinity_mode_ = 2;
std::shared_ptr<Delegate> delegate = nullptr;
};
struct DeviceInfoContext::Data {
@ -137,6 +138,22 @@ std::vector<int32_t> Context::GetThreadAffinityCoreList() const {
return data_->affinity_core_list_;
}
void Context::SetDelegate(const std::shared_ptr<Delegate> &delegate) {
if (data_ == nullptr) {
MS_LOG(ERROR) << "Invalid context.";
return;
}
data_->delegate = delegate;
}
std::shared_ptr<Delegate> Context::GetDelegate() const {
if (data_ == nullptr) {
MS_LOG(ERROR) << "Invalid context.";
return nullptr;
}
return data_->delegate;
}
std::vector<std::shared_ptr<DeviceInfoContext>> &Context::MutableDeviceInfo() {
static std::vector<std::shared_ptr<DeviceInfoContext>> empty;
if (data_ == nullptr) {

View File

@ -179,6 +179,7 @@ Status ModelImpl::RunGraph(const MSKernelCallBack &before, const MSKernelCallBac
mscall_param.node_type_ = call_param.node_type;
return before(inputs, outputs, mscall_param);
};
auto after_call_back = [&](const std::vector<mindspore::tensor::MSTensor *> &before_inputs,
const std::vector<mindspore::tensor::MSTensor *> &before_outputs,
const CallBackParam &call_param) {

View File

@ -14,6 +14,7 @@
* limitations under the License.
*/
#include "src/cxx_api/tensor/tensor_impl.h"
#include <cstddef>
#include <numeric>
#include <memory>
@ -21,12 +22,10 @@
#include <string>
#include <vector>
#include <functional>
#include "src/cxx_api/tensor/tensor_impl.h"
#include "src/cxx_api/tensor_utils.h"
#include "include/api/types.h"
#include "include/api/status.h"
#include "include/ms_tensor.h"
#include "src/common/string_util.h"
#include "src/tensor.h"
#include "src/common/log_adapter.h"
#include "ir/dtype/type_id.h"
@ -37,7 +36,12 @@ using mindspore::lite::RET_OK;
std::shared_ptr<MSTensor::Impl> MSTensor::Impl::CreateTensorImpl(const std::string &name, enum DataType type,
const std::vector<int64_t> &shape, const void *data,
size_t data_len) {
std::vector<int32_t> truncated_shape = TruncateShape(shape, static_cast<enum TypeId>(type), data_len, true);
std::vector<int32_t> truncated_shape;
if (data_len == 0) {
truncated_shape = TruncateShape(shape, static_cast<enum TypeId>(type), data_len, false);
} else {
truncated_shape = TruncateShape(shape, static_cast<enum TypeId>(type), data_len, true);
}
if (truncated_shape.empty() && !(shape.empty())) {
MS_LOG(ERROR) << "Invalid shape for creating tensor.";
return nullptr;

View File

@ -26,9 +26,9 @@
#include <functional>
#include "include/api/types.h"
#include "include/api/status.h"
#include "include/errorcode.h"
#include "include/lite_utils.h"
#include "include/ms_tensor.h"
#include "src/tensor.h"
#include "src/common/log_adapter.h"
namespace mindspore {
@ -38,7 +38,7 @@ class MSTensor::Impl {
public:
Impl() {}
virtual ~Impl() {
~Impl() {
if (lite_tensor_ == nullptr) {
return;
}
@ -72,7 +72,7 @@ class MSTensor::Impl {
return lite::MSTensorToStrings(lite_tensor);
}
virtual const std::string &Name() const {
const std::string &Name() const {
static std::string empty = "";
if (lite_tensor_ == nullptr) {
MS_LOG(ERROR) << "Invalid tensor.";
@ -81,7 +81,15 @@ class MSTensor::Impl {
return tensor_name_;
}
virtual enum DataType DataType() const {
void SetName(const std::string &name) {
if (lite_tensor_ == nullptr) {
MS_LOG(ERROR) << "Invalid tensor.";
return;
}
lite_tensor_->set_tensor_name(name);
}
enum DataType DataType() const {
if (lite_tensor_ == nullptr) {
MS_LOG(ERROR) << "Invalid tensor.";
return DataType::kTypeUnknown;
@ -89,6 +97,14 @@ class MSTensor::Impl {
return static_cast<enum DataType>(lite_tensor_->data_type());
}
void SetDataType(enum DataType data_type) {
if (lite_tensor_ == nullptr) {
MS_LOG(ERROR) << "Invalid tensor.";
return;
}
lite_tensor_->set_data_type(static_cast<enum TypeId>(data_type));
}
int64_t ElementNum() const {
if (lite_tensor_ == nullptr) {
MS_LOG(ERROR) << "Invalid tensor.";
@ -97,7 +113,7 @@ class MSTensor::Impl {
return static_cast<int64_t>(lite_tensor_->ElementsNum());
}
virtual const std::vector<int64_t> &Shape() {
const std::vector<int64_t> &Shape() {
static std::vector<int64_t> empty;
if (lite_tensor_ == nullptr) {
MS_LOG(ERROR) << "Invalid tensor.";
@ -109,7 +125,50 @@ class MSTensor::Impl {
return shape_;
}
virtual std::shared_ptr<const void> Data() const {
void SetShape(const std::vector<int64_t> &shape) {
if (lite_tensor_ == nullptr) {
MS_LOG(ERROR) << "Invalid tensor.";
return;
}
std::vector<int> tensor_shape;
tensor_shape.resize(shape.size());
std::transform(shape.begin(), shape.end(), tensor_shape.begin(), [](int64_t c) { return static_cast<int>(c); });
lite_tensor_->set_shape(tensor_shape);
}
std::shared_ptr<Allocator> allocator() const {
if (lite_tensor_ == nullptr) {
MS_LOG(ERROR) << "Invalid tensor.";
return nullptr;
}
return lite_tensor_->allocator();
}
void SetAllocator(std::shared_ptr<Allocator> allocator) {
if (lite_tensor_ == nullptr) {
MS_LOG(ERROR) << "Invalid tensor.";
return;
}
lite_tensor_->set_allocator(allocator);
}
mindspore::Format format() {
if (lite_tensor_ == nullptr) {
MS_LOG(ERROR) << "Invalid tensor.";
return mindspore::Format::NHWC;
}
return lite_tensor_->format();
}
void SetFormat(mindspore::Format format) {
if (lite_tensor_ == nullptr) {
MS_LOG(ERROR) << "Invalid tensor.";
return;
}
lite_tensor_->set_format(format);
}
std::shared_ptr<const void> Data() const {
if (lite_tensor_ == nullptr) {
MS_LOG(ERROR) << "Invalid tensor.";
return nullptr;
@ -123,14 +182,15 @@ class MSTensor::Impl {
return std::shared_ptr<const void>(lite_tensor_->data(), [](const void *) {});
}
virtual void *MutableData() {
void *MutableData() {
if (lite_tensor_ == nullptr) {
MS_LOG(ERROR) << "Invalid tensor.";
return nullptr;
}
return lite_tensor_->MutableData();
}
virtual size_t DataSize() const {
size_t DataSize() const {
if (lite_tensor_ == nullptr) {
MS_LOG(ERROR) << "Invalid tensor.";
return 0;
@ -138,7 +198,15 @@ class MSTensor::Impl {
return lite_tensor_->Size();
}
virtual bool IsDevice() const { return false; }
void SetData(void *data) {
if (lite_tensor_ == nullptr) {
MS_LOG(ERROR) << "Invalid tensor.";
return;
}
lite_tensor_->set_data(data);
}
bool IsDevice() const { return false; }
tensor::MSTensor *lite_tensor() const { return lite_tensor_; }

View File

@ -16,6 +16,7 @@
#include "src/cxx_api/tensor_utils.h"
#include "src/common/log_adapter.h"
#include "src/tensor.h"
namespace mindspore {
std::vector<int32_t> TruncateShape(const std::vector<int64_t> &shape, enum TypeId type, size_t data_len,

View File

@ -28,6 +28,7 @@
namespace mindspore {
std::vector<int32_t> TruncateShape(const std::vector<int64_t> &shape, enum TypeId type, size_t data_len,
bool verify_size);
Status LiteTensorToMSTensor(tensor::MSTensor *srcTensor, MSTensor *dstTensor);
std::vector<MSTensor> LiteTensorsToMSTensors(const std::vector<mindspore::tensor::MSTensor *> &srcTensors);

View File

@ -21,12 +21,13 @@
#include "include/api/status.h"
#include "include/api/dual_abi_helper.h"
#include "src/cxx_api/tensor/tensor_impl.h"
#include "src/common/string_util.h"
#include "src/tensor.h"
#include "src/common/log_adapter.h"
#include "include/version.h"
namespace mindspore {
namespace {
constexpr int64_t MAX_MALLOC_SIZE = static_cast<size_t>(2000) * 1024 * 1024;
}
class Buffer::Impl {
public:
Impl() : data_() { MS_LOG(ERROR) << "Unsupported feature."; }
@ -72,28 +73,37 @@ bool MSTensor::operator==(std::nullptr_t) const { return impl_ == nullptr; }
bool MSTensor::operator!=(std::nullptr_t) const { return impl_ != nullptr; }
bool MSTensor::operator==(const MSTensor &tensor) const { return impl_->lite_tensor() == tensor.impl_->lite_tensor(); }
MSTensor *MSTensor::CreateTensor(const std::vector<char> &name, enum DataType type, const std::vector<int64_t> &shape,
const void *data, size_t data_len) noexcept {
if (data_len < 0 || data_len > MAX_MALLOC_SIZE) {
MS_LOG(ERROR) << "data_len is error.";
return nullptr;
}
auto new_data = malloc(data_len);
if (new_data == nullptr) {
MS_LOG(ERROR) << "Allocate data failed.";
return nullptr;
void *new_data = nullptr;
if (data != nullptr) {
new_data = malloc(data_len);
if (new_data == nullptr) {
MS_LOG(ERROR) << "Allocate data failed.";
return nullptr;
}
::memcpy(new_data, data, data_len);
}
::memcpy(new_data, data, data_len);
auto impl = Impl::CreateTensorImpl(CharToString(name), type, shape, new_data, data_len);
if (impl == nullptr) {
MS_LOG(ERROR) << "Allocate tensor impl failed.";
free(new_data);
if (new_data != nullptr) {
free(new_data);
}
return nullptr;
}
auto ms_tensor = new (std::nothrow) MSTensor(impl);
if (ms_tensor == nullptr) {
MS_LOG(ERROR) << "Allocate tensor impl failed.";
free(new_data);
if (new_data != nullptr) {
free(new_data);
}
return nullptr;
}
impl->set_own_data(true);
@ -173,7 +183,7 @@ MSTensor *MSTensor::Clone() const {
std::vector<char> MSTensor::CharName() const {
if (impl_ == nullptr) {
MS_LOG(ERROR) << "Invalid tensor inpmlement.";
MS_LOG(ERROR) << "Invalid tensor implement.";
return std::vector<char>();
}
return StringToChar(impl_->Name());
@ -181,7 +191,7 @@ std::vector<char> MSTensor::CharName() const {
int64_t MSTensor::ElementNum() const {
if (impl_ == nullptr) {
MS_LOG(ERROR) << "Invalid tensor inpmlement.";
MS_LOG(ERROR) << "Invalid tensor implement.";
return -1;
}
return impl_->ElementNum();
@ -189,7 +199,7 @@ int64_t MSTensor::ElementNum() const {
enum DataType MSTensor::DataType() const {
if (impl_ == nullptr) {
MS_LOG(ERROR) << "Invalid tensor inpmlement.";
MS_LOG(ERROR) << "Invalid tensor implement.";
return DataType::kTypeUnknown;
}
return impl_->DataType();
@ -198,7 +208,7 @@ enum DataType MSTensor::DataType() const {
const std::vector<int64_t> &MSTensor::Shape() const {
static std::vector<int64_t> empty;
if (impl_ == nullptr) {
MS_LOG(ERROR) << "Invalid tensor inpmlement.";
MS_LOG(ERROR) << "Invalid tensor implement.";
return empty;
}
return impl_->Shape();
@ -206,7 +216,7 @@ const std::vector<int64_t> &MSTensor::Shape() const {
std::shared_ptr<const void> MSTensor::Data() const {
if (impl_ == nullptr) {
MS_LOG(ERROR) << "Invalid tensor inpmlement.";
MS_LOG(ERROR) << "Invalid tensor implement.";
return nullptr;
}
return impl_->Data();
@ -214,7 +224,7 @@ std::shared_ptr<const void> MSTensor::Data() const {
void *MSTensor::MutableData() {
if (impl_ == nullptr) {
MS_LOG(ERROR) << "Invalid tensor inpmlement.";
MS_LOG(ERROR) << "Invalid tensor implement.";
return nullptr;
}
return impl_->MutableData();
@ -222,7 +232,7 @@ void *MSTensor::MutableData() {
size_t MSTensor::DataSize() const {
if (impl_ == nullptr) {
MS_LOG(ERROR) << "Invalid tensor inpmlement.";
MS_LOG(ERROR) << "Invalid tensor implement.";
return 0;
}
return impl_->DataSize();
@ -239,6 +249,70 @@ void MSTensor::DestroyTensorPtr(MSTensor *tensor) noexcept {
}
}
void MSTensor::SetShape(const std::vector<int64_t> &shape) {
if (impl_ == nullptr) {
MS_LOG(ERROR) << "Invalid tensor implement.";
return;
}
impl_->SetShape(shape);
}
void MSTensor::SetDataType(enum DataType data_type) {
if (impl_ == nullptr) {
MS_LOG(ERROR) << "Invalid tensor implement.";
return;
}
impl_->SetDataType(data_type);
}
void MSTensor::SetTensorName(const std::string &name) {
if (impl_ == nullptr) {
MS_LOG(ERROR) << "Invalid tensor implement.";
return;
}
impl_->SetName(name);
}
void MSTensor::SetAllocator(std::shared_ptr<Allocator> allocator) {
if (impl_ == nullptr) {
MS_LOG(ERROR) << "Invalid tensor implement.";
return;
}
return impl_->SetAllocator(allocator);
}
std::shared_ptr<Allocator> MSTensor::allocator() const {
if (impl_ == nullptr) {
MS_LOG(ERROR) << "Invalid tensor implement.";
return nullptr;
}
return impl_->allocator();
}
void MSTensor::SetFormat(mindspore::Format format) {
if (impl_ == nullptr) {
MS_LOG(ERROR) << "Invalid tensor implement.";
return;
}
return impl_->SetFormat(format);
}
mindspore::Format MSTensor::format() const {
if (impl_ == nullptr) {
MS_LOG(ERROR) << "Invalid tensor implement.";
return mindspore::Format::NHWC;
}
return impl_->format();
}
void MSTensor::SetData(void *data) {
if (impl_ == nullptr) {
MS_LOG(ERROR) << "Invalid tensor implement.";
return;
}
return impl_->SetData(data);
}
Buffer::Buffer() : impl_(nullptr) { MS_LOG(ERROR) << "Unsupported feature."; }
Buffer::Buffer(const void *data, size_t data_len) : impl_(nullptr) { MS_LOG(ERROR) << "Unsupported feature."; }
Buffer::~Buffer() = default;