diff --git a/include/api/allocator.h b/include/api/allocator.h new file mode 100644 index 00000000000..e78cf770b33 --- /dev/null +++ b/include/api/allocator.h @@ -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 +#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 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 diff --git a/include/api/context.h b/include/api/context.h index 3f08de1c581..9ac356ad562 100644 --- a/include/api/context.h +++ b/include/api/context.h @@ -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); + std::shared_ptr GetDelegate() const; + std::vector> &MutableDeviceInfo(); private: diff --git a/include/api/data_type.h b/include/api/data_type.h index a39488a83d3..61eb1d51f2b 100644 --- a/include/api/data_type.h +++ b/include/api/data_type.h @@ -23,6 +23,7 @@ enum class DataType : int { kObjectTypeList = 13, kObjectTypeTuple = 14, kObjectTypeTensorType = 17, + kNumberTypeBegin = 29, kNumberTypeBool = 30, kNumberTypeInt8 = 32, kNumberTypeInt16 = 33, diff --git a/include/api/types.h b/include/api/types.h index 1cdb3986cf7..eb77ee0ea67 100644 --- a/include/api/types.h +++ b/include/api/types.h @@ -23,6 +23,7 @@ #include #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 &shape); + void SetDataType(enum DataType data_type); + void SetTensorName(const std::string &name); + void SetAllocator(std::shared_ptr allocator); + std::shared_ptr allocator() const; + void SetFormat(mindspore::Format format); + mindspore::Format format() const; + void SetData(void *data); + const std::shared_ptr impl() const { return impl_; } private: // api without std::string diff --git a/mindspore/ccsrc/cxx_api/model/model.cc b/mindspore/ccsrc/cxx_api/model/model.cc index a4a84ceabe5..f2d32f131cd 100644 --- a/mindspore/ccsrc/cxx_api/model/model.cc +++ b/mindspore/ccsrc/cxx_api/model/model.cc @@ -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 &inputs, const std::vector> &dims) { if (impl_ == nullptr) { MS_LOG(ERROR) << "Failed because this model has not been built."; diff --git a/mindspore/lite/src/cxx_api/context.cc b/mindspore/lite/src/cxx_api/context.cc index f664d9c6125..d7ac4674769 100644 --- a/mindspore/lite/src/cxx_api/context.cc +++ b/mindspore/lite/src/cxx_api/context.cc @@ -40,6 +40,7 @@ struct Context::Data { bool enable_parallel_ = false; std::vector affinity_core_list_; int affinity_mode_ = 2; + std::shared_ptr delegate = nullptr; }; struct DeviceInfoContext::Data { @@ -137,6 +138,22 @@ std::vector Context::GetThreadAffinityCoreList() const { return data_->affinity_core_list_; } +void Context::SetDelegate(const std::shared_ptr &delegate) { + if (data_ == nullptr) { + MS_LOG(ERROR) << "Invalid context."; + return; + } + data_->delegate = delegate; +} + +std::shared_ptr Context::GetDelegate() const { + if (data_ == nullptr) { + MS_LOG(ERROR) << "Invalid context."; + return nullptr; + } + return data_->delegate; +} + std::vector> &Context::MutableDeviceInfo() { static std::vector> empty; if (data_ == nullptr) { diff --git a/mindspore/lite/src/cxx_api/model/model_impl.cc b/mindspore/lite/src/cxx_api/model/model_impl.cc index bee5b6d90d2..7725b7bf32a 100644 --- a/mindspore/lite/src/cxx_api/model/model_impl.cc +++ b/mindspore/lite/src/cxx_api/model/model_impl.cc @@ -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 &before_inputs, const std::vector &before_outputs, const CallBackParam &call_param) { diff --git a/mindspore/lite/src/cxx_api/tensor/tensor_impl.cc b/mindspore/lite/src/cxx_api/tensor/tensor_impl.cc index b4e24123f49..a67335650b3 100644 --- a/mindspore/lite/src/cxx_api/tensor/tensor_impl.cc +++ b/mindspore/lite/src/cxx_api/tensor/tensor_impl.cc @@ -14,6 +14,7 @@ * limitations under the License. */ +#include "src/cxx_api/tensor/tensor_impl.h" #include #include #include @@ -21,12 +22,10 @@ #include #include #include -#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::CreateTensorImpl(const std::string &name, enum DataType type, const std::vector &shape, const void *data, size_t data_len) { - std::vector truncated_shape = TruncateShape(shape, static_cast(type), data_len, true); + std::vector truncated_shape; + if (data_len == 0) { + truncated_shape = TruncateShape(shape, static_cast(type), data_len, false); + } else { + truncated_shape = TruncateShape(shape, static_cast(type), data_len, true); + } if (truncated_shape.empty() && !(shape.empty())) { MS_LOG(ERROR) << "Invalid shape for creating tensor."; return nullptr; diff --git a/mindspore/lite/src/cxx_api/tensor/tensor_impl.h b/mindspore/lite/src/cxx_api/tensor/tensor_impl.h index b9b916ecdb8..40c6870f31f 100644 --- a/mindspore/lite/src/cxx_api/tensor/tensor_impl.h +++ b/mindspore/lite/src/cxx_api/tensor/tensor_impl.h @@ -26,9 +26,9 @@ #include #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(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(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(lite_tensor_->ElementsNum()); } - virtual const std::vector &Shape() { + const std::vector &Shape() { static std::vector empty; if (lite_tensor_ == nullptr) { MS_LOG(ERROR) << "Invalid tensor."; @@ -109,7 +125,50 @@ class MSTensor::Impl { return shape_; } - virtual std::shared_ptr Data() const { + void SetShape(const std::vector &shape) { + if (lite_tensor_ == nullptr) { + MS_LOG(ERROR) << "Invalid tensor."; + return; + } + std::vector tensor_shape; + tensor_shape.resize(shape.size()); + std::transform(shape.begin(), shape.end(), tensor_shape.begin(), [](int64_t c) { return static_cast(c); }); + lite_tensor_->set_shape(tensor_shape); + } + + std::shared_ptr allocator() const { + if (lite_tensor_ == nullptr) { + MS_LOG(ERROR) << "Invalid tensor."; + return nullptr; + } + return lite_tensor_->allocator(); + } + + void SetAllocator(std::shared_ptr 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 Data() const { if (lite_tensor_ == nullptr) { MS_LOG(ERROR) << "Invalid tensor."; return nullptr; @@ -123,14 +182,15 @@ class MSTensor::Impl { return std::shared_ptr(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_; } diff --git a/mindspore/lite/src/cxx_api/tensor_utils.cc b/mindspore/lite/src/cxx_api/tensor_utils.cc index c070e193450..b91ad13e78c 100644 --- a/mindspore/lite/src/cxx_api/tensor_utils.cc +++ b/mindspore/lite/src/cxx_api/tensor_utils.cc @@ -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 TruncateShape(const std::vector &shape, enum TypeId type, size_t data_len, diff --git a/mindspore/lite/src/cxx_api/tensor_utils.h b/mindspore/lite/src/cxx_api/tensor_utils.h index ea1afc188aa..532c315f13e 100644 --- a/mindspore/lite/src/cxx_api/tensor_utils.h +++ b/mindspore/lite/src/cxx_api/tensor_utils.h @@ -28,6 +28,7 @@ namespace mindspore { std::vector TruncateShape(const std::vector &shape, enum TypeId type, size_t data_len, bool verify_size); + Status LiteTensorToMSTensor(tensor::MSTensor *srcTensor, MSTensor *dstTensor); std::vector LiteTensorsToMSTensors(const std::vector &srcTensors); diff --git a/mindspore/lite/src/cxx_api/types.cc b/mindspore/lite/src/cxx_api/types.cc index e84d7742066..d12ad46d7f7 100644 --- a/mindspore/lite/src/cxx_api/types.cc +++ b/mindspore/lite/src/cxx_api/types.cc @@ -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(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 &name, enum DataType type, const std::vector &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 MSTensor::CharName() const { if (impl_ == nullptr) { - MS_LOG(ERROR) << "Invalid tensor inpmlement."; + MS_LOG(ERROR) << "Invalid tensor implement."; return std::vector(); } return StringToChar(impl_->Name()); @@ -181,7 +191,7 @@ std::vector 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 &MSTensor::Shape() const { static std::vector 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 &MSTensor::Shape() const { std::shared_ptr 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 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 &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) { + if (impl_ == nullptr) { + MS_LOG(ERROR) << "Invalid tensor implement."; + return; + } + return impl_->SetAllocator(allocator); +} + +std::shared_ptr 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;