!26459 [MSLITE][DEVELOP] fix bug of c++ api

Merge pull request !26459 from yangruoqi713/r1.5
This commit is contained in:
i-robot 2021-12-02 09:18:12 +00:00 committed by Gitee
commit 0544d0bdf9
2 changed files with 63 additions and 29 deletions

View File

@ -45,6 +45,14 @@ CreateTrainSessionProto *CreateTrainSessionCallbackHolder(CreateTrainSessionProt
Status ModelImpl::Build(const void *model_data, size_t data_size, ModelType model_type,
const std::shared_ptr<Context> &ms_context) {
if (model_data == nullptr) {
MS_LOG(ERROR) << "The input model buffer is nullptr.";
return kLiteNullptr;
}
if (data_size == 0) {
MS_LOG(ERROR) << "The input model buffer size is 0.";
return kLiteInputParamInvalid;
}
context_ = ms_context;
auto *lite_context = new (std::nothrow) lite::InnerContext();

View File

@ -62,8 +62,7 @@ class Buffer::Impl {
return false;
}
memcpy(MutableData(), data, data_len);
(void)memcpy(MutableData(), data, data_len);
return true;
}
@ -94,30 +93,41 @@ MSTensor *MSTensor::CreateTensor(const std::vector<char> &name, enum DataType ty
return nullptr;
}
if (data_len > 0 && data == nullptr) {
MS_LOG(ERROR) << "Mull data ptr of tensor.";
MS_LOG(ERROR) << "Null data ptr of tensor.";
return nullptr;
}
auto impl = Impl::CreateTensorImpl(CharToString(name), type, shape, nullptr, data_len);
if (data_len == 0 && data != nullptr) {
MS_LOG(ERROR) << "Data len doesn't match the data buffer size.";
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;
}
(void)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.";
if (new_data != nullptr) {
free(new_data);
}
return nullptr;
}
impl->set_own_data(true);
auto ms_tensor = new (std::nothrow) MSTensor(impl);
if (ms_tensor == nullptr) {
MS_LOG(ERROR) << "Allocate tensor impl failed.";
MS_LOG(ERROR) << "Allocate MSTensor failed.";
if (new_data != nullptr) {
free(new_data);
}
return nullptr;
}
if (data != nullptr) {
if (ms_tensor->MutableData() == nullptr) {
MS_LOG(ERROR) << "Allocate data failed.";
delete ms_tensor;
return nullptr;
}
::memcpy(ms_tensor->MutableData(), data, data_len);
}
impl->set_own_data(true);
return ms_tensor;
}
@ -191,27 +201,43 @@ MSTensor *MSTensor::Clone() const {
MS_LOG(ERROR) << "Illegal data size of tensor.";
return nullptr;
}
auto impl = Impl::CreateTensorImpl(this->Name(), this->DataType(), this->Shape(), nullptr, data_len);
if (impl == nullptr) {
MS_LOG(ERROR) << "Allocate tensor impl failed.";
if (data_len > 0 && impl_->Data() == nullptr) {
MS_LOG(ERROR) << "Null data ptr of tensor.";
return nullptr;
}
if (data_len == 0 && impl_->Data() != nullptr) {
MS_LOG(ERROR) << "Data len doesn't match the data buffer size.";
return nullptr;
}
void *new_data = nullptr;
if (impl_->Data() != nullptr) {
new_data = malloc(data_len);
if (new_data == nullptr) {
MS_LOG(ERROR) << "Allocate data failed.";
return nullptr;
}
(void)memcpy(new_data, impl_->MutableData(), data_len);
}
auto impl = Impl::CreateTensorImpl(this->Name(), this->DataType(), this->Shape(), new_data, data_len);
if (impl == nullptr) {
MS_LOG(ERROR) << "Allocate tensor impl failed.";
if (new_data != nullptr) {
free(new_data);
}
return nullptr;
}
impl->set_own_data(true);
auto ms_tensor = new (std::nothrow) MSTensor(impl);
if (ms_tensor == nullptr) {
MS_LOG(ERROR) << "Allocate tensor impl failed.";
MS_LOG(ERROR) << "Allocate MSTensor failed.";
if (new_data != nullptr) {
free(new_data);
}
return nullptr;
}
if (impl_->Data() != nullptr) {
if (ms_tensor->MutableData() == nullptr) {
MS_LOG(ERROR) << "Allocate data failed.";
delete ms_tensor;
return nullptr;
}
::memcpy(ms_tensor->MutableData(), impl_->MutableData(), data_len);
}
impl->set_own_data(true);
return ms_tensor;
}