forked from mindspore-Ecosystem/mindspore
!20835 [LITE] fix resource leak
Merge pull request !20835 from zhaozhenlong/lite/issue/enterprise_defect_check
This commit is contained in:
commit
e187cfc889
|
@ -52,6 +52,6 @@ uint16_t Float32ToShort(float src_value) {
|
|||
res -= (127 - 15) << 13;
|
||||
|
||||
// sign
|
||||
res |= (src_value_bits.u & 0x400000000) >> 16;
|
||||
res |= (src_value_bits.u & 0x80000000) >> 16;
|
||||
return res;
|
||||
}
|
||||
|
|
|
@ -959,11 +959,14 @@ session::LiteSession *session::LiteSession::CreateSession(const char *model_buf,
|
|||
auto *model = lite::ImportFromBuffer(model_buf, size, true);
|
||||
if (model == nullptr) {
|
||||
MS_LOG(ERROR) << "Import model failed";
|
||||
delete session;
|
||||
return nullptr;
|
||||
}
|
||||
auto ret = session->CompileGraph(model);
|
||||
if (ret != lite::RET_OK) {
|
||||
MS_LOG(ERROR) << "Compile model failed";
|
||||
delete model;
|
||||
delete session;
|
||||
return nullptr;
|
||||
}
|
||||
model->buf = nullptr;
|
||||
|
|
|
@ -32,6 +32,7 @@ schema::Tensor *AttrToTensor(void *data, int data_size, bool is_array, TypeId ty
|
|||
auto dst_data = dst_tensor->MutableData();
|
||||
if (dst_data == nullptr) {
|
||||
MS_LOG(ERROR) << "Data from tensor is nullptr";
|
||||
delete dst_tensor;
|
||||
return nullptr;
|
||||
}
|
||||
std::vector<uint8_t> uint8_data;
|
||||
|
|
|
@ -28,6 +28,7 @@ using mindspore::schema::PrimitiveType_AddFusion;
|
|||
namespace mindspore::kernel {
|
||||
namespace {
|
||||
constexpr int kBaseShift = 20;
|
||||
constexpr size_t kMaxShapeSize = 10;
|
||||
} // namespace
|
||||
|
||||
QuantizedAddCPUKernel::~QuantizedAddCPUKernel() {
|
||||
|
@ -99,18 +100,30 @@ int QuantizedAddCPUKernel::ReSize() {
|
|||
arith_para_->in_elements_num1_ = in_tensors_[1]->ElementsNum();
|
||||
arith_para_->out_elements_num_ = out_tensors_[0]->ElementsNum();
|
||||
|
||||
if (input0->shape().size() > kMaxShapeSize) {
|
||||
MS_LOG(ERROR) << "input0->shape().size() " << input0->shape().size() << " > max shape size " << kMaxShapeSize;
|
||||
return RET_ERROR;
|
||||
}
|
||||
for (size_t i = 0; i < in_tensors_[0]->shape().size(); i++) {
|
||||
if (arith_para_->in_shape0_[i] == -1) {
|
||||
memcpy(arith_para_->in_shape0_, input0->shape().data(), input0->shape().size() * sizeof(int));
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (input1->shape().size() > kMaxShapeSize) {
|
||||
MS_LOG(ERROR) << "input1->shape().size() " << input1->shape().size() << " > max shape size " << kMaxShapeSize;
|
||||
return RET_ERROR;
|
||||
}
|
||||
for (size_t i = 0; i < in_tensors_[1]->shape().size(); i++) {
|
||||
if (arith_para_->in_shape1_[i] == -1) {
|
||||
memcpy(arith_para_->in_shape1_, input1->shape().data(), input1->shape().size() * sizeof(int));
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (output->shape().size() > kMaxShapeSize) {
|
||||
MS_LOG(ERROR) << "output->shape().size() " << output->shape().size() << " > max shape size " << kMaxShapeSize;
|
||||
return RET_ERROR;
|
||||
}
|
||||
for (size_t i = 0; i < out_tensors_[0]->shape().size(); i++) {
|
||||
if (arith_para_->out_shape_[i] == -1) {
|
||||
memcpy(arith_para_->out_shape_, output->shape().data(), output->shape().size() * sizeof(int));
|
||||
|
@ -145,8 +158,8 @@ int QuantizedAddCPUKernel::ReSize() {
|
|||
|
||||
int AddInt8Run(void *cdata, int task_id, float lhs_scale, float rhs_scale) {
|
||||
auto add = reinterpret_cast<QuantizedAddCPUKernel *>(cdata);
|
||||
add->DoExecute(task_id);
|
||||
return RET_OK;
|
||||
auto ret = add->DoExecute(task_id);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int QuantizedAddCPUKernel::BroadcastRun(int task_id) {
|
||||
|
@ -227,9 +240,9 @@ int QuantizedAddCPUKernel::Run() {
|
|||
input1_data_ = static_cast<int8_t *>(in_tensors_.at(1)->data_c());
|
||||
output_data_ = static_cast<int8_t *>(out_tensors_.at(0)->data_c());
|
||||
|
||||
ParallelLaunch(this->ms_context_, AddInt8Run, this, thread_count_);
|
||||
auto ret = ParallelLaunch(this->ms_context_, AddInt8Run, this, thread_count_);
|
||||
|
||||
return RET_OK;
|
||||
return ret;
|
||||
}
|
||||
|
||||
REG_KERNEL(kCPU, kNumberTypeInt8, PrimitiveType_AddFusion, LiteKernelCreator<QuantizedAddCPUKernel>)
|
||||
|
|
|
@ -122,7 +122,11 @@ int ConcatInt8CPUKernel::Run() {
|
|||
|
||||
int ConcatInt8Run(void *cdata, int task_id, float lhs_scale, float rhs_scale) {
|
||||
auto concat = reinterpret_cast<ConcatInt8CPUKernel *>(cdata);
|
||||
concat->DoExecute(task_id);
|
||||
auto ret = concat->DoExecute(task_id);
|
||||
if (ret != RET_OK) {
|
||||
MS_LOG(ERROR) << "ConcatInt8Run task_id " << task_id << " failed.";
|
||||
return ret;
|
||||
}
|
||||
return lite::RET_OK;
|
||||
}
|
||||
|
||||
|
|
|
@ -371,9 +371,13 @@ int Convolution1x1Int8CPUKernel::InitParam() {
|
|||
int Convolution1x1Int8CPUKernel::ReSize() {
|
||||
FreeResizeBuf();
|
||||
|
||||
ConvolutionBaseCPUKernel::Init();
|
||||
int error_code = ConvolutionBaseCPUKernel::Init();
|
||||
if (error_code != RET_OK) {
|
||||
MS_LOG(ERROR) << "Convolution base init failed.";
|
||||
return error_code;
|
||||
}
|
||||
|
||||
int error_code = InitParam();
|
||||
error_code = InitParam();
|
||||
if (error_code != RET_OK) {
|
||||
MS_LOG(ERROR) << "Convolution base init failed.";
|
||||
return error_code;
|
||||
|
|
|
@ -115,7 +115,11 @@ int ConvolutionDepthwise3x3Int8CPUKernel::Init() {
|
|||
}
|
||||
|
||||
int ConvolutionDepthwise3x3Int8CPUKernel::ReSize() {
|
||||
ConvolutionBaseCPUKernel::Init();
|
||||
auto ret = ConvolutionBaseCPUKernel::Init();
|
||||
if (ret != RET_OK) {
|
||||
MS_LOG(ERROR) << "ConvolutionBaseCPUKernel Init failed.";
|
||||
return ret;
|
||||
}
|
||||
InitSlidingParamConvDw(sliding_, conv_param_, conv_param_->input_channel_);
|
||||
conv_param_->thread_num_ = MSMIN(thread_count_, conv_param_->output_h_);
|
||||
return RET_OK;
|
||||
|
|
|
@ -100,10 +100,7 @@ int ConvolutionDepthwiseInt8CPUKernel::Init() {
|
|||
return ReSize();
|
||||
}
|
||||
|
||||
int ConvolutionDepthwiseInt8CPUKernel::ReSize() {
|
||||
ConvolutionBaseCPUKernel::Init();
|
||||
return RET_OK;
|
||||
}
|
||||
int ConvolutionDepthwiseInt8CPUKernel::ReSize() { return ConvolutionBaseCPUKernel::Init(); }
|
||||
|
||||
int ConvolutionDepthwiseInt8CPUKernel::Execute(int task_id) {
|
||||
auto buffer = row_buffer_ + conv_param_->output_w_ * conv_param_->output_channel_ * task_id;
|
||||
|
|
|
@ -142,7 +142,11 @@ int ConvolutionDepthwiseSWInt8CPUKernel::ReinitFreeBefore() {
|
|||
}
|
||||
|
||||
int ConvolutionDepthwiseSWInt8CPUKernel::ReinitQuantParam() {
|
||||
ReinitFreeBefore(); // remalloc quant param buffer
|
||||
auto ret = ReinitFreeBefore(); // remalloc quant param buffer
|
||||
if (ret != RET_OK) {
|
||||
MS_LOG(ERROR) << "ReinitFreeBefore failed.";
|
||||
return ret;
|
||||
}
|
||||
|
||||
auto input_tensor = in_tensors_.at(kInputIndex);
|
||||
auto channel = conv_param_->input_channel_;
|
||||
|
|
|
@ -57,7 +57,11 @@ int CropInt8CPUKernel::Run() {
|
|||
|
||||
int CropInt8Run(void *cdata, int task_id, float lhs_scale, float rhs_scale) {
|
||||
auto crop = reinterpret_cast<CropInt8CPUKernel *>(cdata);
|
||||
crop->DoExecute(task_id);
|
||||
auto ret = crop->DoExecute(task_id);
|
||||
if (ret != RET_OK) {
|
||||
MS_LOG(ERROR) << "CropInt8Run task id " << task_id << " run failed.";
|
||||
return ret;
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
|
|
|
@ -39,6 +39,10 @@ int DeconvolutionDepthwiseInt8CPUKernel::InitWeightBias() {
|
|||
// o, h, w, i -> o/8, h, w, i, 8; o equals to group, i equals to 1
|
||||
auto weight_tensor = in_tensors_.at(kWeightIndex);
|
||||
auto origin_weight = reinterpret_cast<int8_t *>(weight_tensor->MutableData());
|
||||
if (origin_weight == nullptr) {
|
||||
MS_LOG(ERROR) << "origin_weight nullptr";
|
||||
return RET_ERROR;
|
||||
}
|
||||
int OC4 = UP_DIV(weight_tensor->Batch(), C4NUM);
|
||||
int pack_weight_size = C4NUM * OC4 * weight_tensor->Height() * weight_tensor->Width();
|
||||
packed_weight_ = reinterpret_cast<int16_t *>(malloc(pack_weight_size * sizeof(int16_t)));
|
||||
|
|
|
@ -56,8 +56,12 @@ void DeConvInt8CPUKernel::FreeTmpBuffer() {
|
|||
int DeConvInt8CPUKernel::ReSize() {
|
||||
FreeTmpBuffer();
|
||||
|
||||
ConvolutionBaseCPUKernel::Init();
|
||||
int error_code = InitParam();
|
||||
int error_code = ConvolutionBaseCPUKernel::Init();
|
||||
if (error_code != RET_OK) {
|
||||
MS_LOG(ERROR) << "deconv int8 convolution base init failed.";
|
||||
return error_code;
|
||||
}
|
||||
error_code = InitParam();
|
||||
if (error_code != RET_OK) {
|
||||
MS_LOG(ERROR) << "deconv int8 InitParam error!";
|
||||
return error_code;
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
#include "src/kernel_registry.h"
|
||||
|
||||
using mindspore::lite::KernelRegistrar;
|
||||
using mindspore::lite::RET_ERROR;
|
||||
using mindspore::lite::RET_OK;
|
||||
using mindspore::schema::PrimitiveType_FullConnection;
|
||||
|
||||
|
|
|
@ -73,7 +73,11 @@ int HswishInt8CPUKernel::DoActivation(int task_id) {
|
|||
int stride = UP_DIV(length, thread_count_);
|
||||
int count = MSMIN(stride, length - stride * task_id);
|
||||
|
||||
HSwishInt8(input_addr + stride * task_id, count, output_addr + stride * task_id, &quant_arg_);
|
||||
auto ret = HSwishInt8(input_addr + stride * task_id, count, output_addr + stride * task_id, &quant_arg_);
|
||||
if (ret != RET_OK) {
|
||||
MS_LOG(ERROR) << "DoActivation hswish int8 task id " << task_id << " failed.";
|
||||
return ret;
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
|
|
|
@ -50,7 +50,11 @@ int L2NormInt8CPUKernel::Init() {
|
|||
|
||||
int L2NormInt8Run(void *cdata, int task_id, float lhs_scale, float rhs_scale) {
|
||||
auto kernel = reinterpret_cast<L2NormInt8CPUKernel *>(cdata);
|
||||
kernel->DoExecute(task_id);
|
||||
auto ret = kernel->DoExecute(task_id);
|
||||
if (ret != RET_OK) {
|
||||
MS_LOG(ERROR) << "L2NormInt8Run task id " << task_id << " failed.";
|
||||
return ret;
|
||||
}
|
||||
return lite::RET_OK;
|
||||
}
|
||||
|
||||
|
|
|
@ -117,13 +117,21 @@ int LayerNormInt8CPUKernel::ReSize() {
|
|||
}
|
||||
|
||||
int LayerNormInt8CPUKernel::DoExecute(int task_id) {
|
||||
LayerNormInt8(src_ptr_, gamma_ptr_, beta_ptr_, dst_ptr_, param_, quant_param_, task_id);
|
||||
auto ret = LayerNormInt8(src_ptr_, gamma_ptr_, beta_ptr_, dst_ptr_, param_, quant_param_, task_id);
|
||||
if (ret != RET_OK) {
|
||||
MS_LOG(ERROR) << "DoExecute task id " << task_id << " failed.";
|
||||
return ret;
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int LayerNormInt8Run(void *cdata, int task_id, float lhs_scale, float rhs_scale) {
|
||||
auto kernel = reinterpret_cast<LayerNormInt8CPUKernel *>(cdata);
|
||||
kernel->DoExecute(task_id);
|
||||
auto ret = kernel->DoExecute(task_id);
|
||||
if (ret != RET_OK) {
|
||||
MS_LOG(ERROR) << "LayerNormInt8Run task_id " << task_id << " failed.";
|
||||
return ret;
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
|
|
|
@ -32,7 +32,11 @@ int LeakyReluInt8Run(void *cdata, int task_id, float lhs_scale, float rhs_scale)
|
|||
return RET_ERROR;
|
||||
}
|
||||
auto relu = reinterpret_cast<LeakyReluInt8CPUKernel *>(cdata);
|
||||
relu->DoExecute(task_id);
|
||||
auto ret = relu->DoExecute(task_id);
|
||||
if (ret != RET_OK) {
|
||||
MS_LOG(ERROR) << "LeakyReluInt8Run task_id " << task_id << " failed.";
|
||||
return ret;
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
} // namespace
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
#include "src/kernel_registry.h"
|
||||
|
||||
using mindspore::lite::KernelRegistrar;
|
||||
using mindspore::lite::RET_ERROR;
|
||||
using mindspore::lite::RET_OK;
|
||||
using mindspore::schema::PrimitiveType_MatMul;
|
||||
|
||||
|
|
|
@ -155,7 +155,7 @@ int MulInt8CPUKernel::Run() {
|
|||
if (fast_hw_broadcast_) {
|
||||
elements_num_ = out_tensors_.front()->Batch() * out_tensors_.front()->Height() * out_tensors_.front()->Width();
|
||||
count_unit_ = thread_count_ > 1 ? UP_DIV(elements_num_, thread_count_) : elements_num_;
|
||||
return ParallelLaunch(this->ms_context_, FastHWBroadcatMulInt8Run, this, thread_count_);
|
||||
return ParallelLaunch(this->ms_context_, FastHWBroadcastMulInt8Run, this, thread_count_);
|
||||
}
|
||||
|
||||
elements_num_ = out_tensors_.at(0)->ElementsNum();
|
||||
|
@ -185,15 +185,23 @@ int MulInt8CPUKernel::Run() {
|
|||
return ret;
|
||||
}
|
||||
|
||||
int FastHWBroadcatMulInt8Run(void *cdata, int task_id, float lhs_scale, float rhs_scale) {
|
||||
int FastHWBroadcastMulInt8Run(void *cdata, int task_id, float lhs_scale, float rhs_scale) {
|
||||
auto mul = reinterpret_cast<MulInt8CPUKernel *>(cdata);
|
||||
mul->FastDoExecute(task_id);
|
||||
auto ret = mul->FastDoExecute(task_id);
|
||||
if (ret != RET_OK) {
|
||||
MS_LOG(ERROR) << "FastHWBroadcastMulInt8Run task_id " << task_id << " failed.";
|
||||
return ret;
|
||||
}
|
||||
return lite::RET_OK;
|
||||
}
|
||||
|
||||
int MulInt8Run(void *cdata, int task_id, float lhs_scale, float rhs_scale) {
|
||||
auto mul = reinterpret_cast<MulInt8CPUKernel *>(cdata);
|
||||
mul->DoExecute(task_id);
|
||||
auto ret = mul->DoExecute(task_id);
|
||||
if (ret != RET_OK) {
|
||||
MS_LOG(ERROR) << "MulInt8Run task_id " << task_id << " failed.";
|
||||
return ret;
|
||||
}
|
||||
return lite::RET_OK;
|
||||
}
|
||||
|
||||
|
|
|
@ -57,7 +57,7 @@ class MulInt8CPUKernel : public InnerKernel {
|
|||
};
|
||||
|
||||
int MulInt8Run(void *cdata, int task_id, float lhs_scale, float rhs_scale);
|
||||
int FastHWBroadcatMulInt8Run(void *cdata, int task_id, float lhs_scale, float rhs_scale);
|
||||
int FastHWBroadcastMulInt8Run(void *cdata, int task_id, float lhs_scale, float rhs_scale);
|
||||
} // namespace mindspore::kernel
|
||||
|
||||
#endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_INT8_MUL_INT8_H_
|
||||
|
|
|
@ -63,7 +63,11 @@ int ReshapeInt8CPUKernel::Run() {
|
|||
|
||||
int ReshapeInt8Run(void *cdata, int task_id, float lhs_scale, float rhs_scale) {
|
||||
auto reshape = reinterpret_cast<ReshapeInt8CPUKernel *>(cdata);
|
||||
reshape->DoExecute(task_id);
|
||||
auto ret = reshape->DoExecute(task_id);
|
||||
if (ret != RET_OK) {
|
||||
MS_LOG(ERROR) << "Reshapeint8Run task_id " << task_id << " failed.";
|
||||
return ret;
|
||||
}
|
||||
return lite::RET_OK;
|
||||
}
|
||||
|
||||
|
|
|
@ -73,7 +73,11 @@ int SigmoidInt8CPUKernel::DoActivation(int task_id) {
|
|||
int stride = UP_DIV(length, op_parameter_->thread_num_);
|
||||
int count = MSMIN(stride, length - stride * task_id);
|
||||
|
||||
SigmoidInt8(input_addr + stride * task_id, count, output_addr + stride * task_id, table_list_);
|
||||
auto ret = SigmoidInt8(input_addr + stride * task_id, count, output_addr + stride * task_id, table_list_);
|
||||
if (ret != RET_OK) {
|
||||
MS_LOG(ERROR) << "DoActivation task_id " << task_id << " failed.";
|
||||
return ret;
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
|
|
|
@ -97,7 +97,11 @@ int SqueezeInt8CPUKernel::Run() {
|
|||
|
||||
int SqueezeInt8Run(void *cdata, int task_id, float lhs_scale, float rhs_scale) {
|
||||
auto Squeeze = reinterpret_cast<SqueezeInt8CPUKernel *>(cdata);
|
||||
Squeeze->DoExecute(task_id);
|
||||
auto ret = Squeeze->DoExecute(task_id);
|
||||
if (ret != RET_OK) {
|
||||
MS_LOG(ERROR) << "SqueezeInt8Run task_id " << task_id << " failed.";
|
||||
return ret;
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
|
|
|
@ -23,6 +23,9 @@ using mindspore::lite::RET_OP_EXECUTE_FAILURE;
|
|||
using mindspore::schema::PrimitiveType_Transpose;
|
||||
|
||||
namespace mindspore::kernel {
|
||||
namespace {
|
||||
constexpr size_t kMaxShapeSize = 20;
|
||||
} // namespace
|
||||
int TransposeInt8CPUKernel::Init() {
|
||||
if (!InferShapeDone()) {
|
||||
return RET_OK;
|
||||
|
@ -109,7 +112,15 @@ int TransposeInt8CPUKernel::Run() {
|
|||
NHNCTransposeFunc_(in_ptr_, out_ptr_, nhnc_param_[0], nhnc_param_[1], nhnc_param_[2]);
|
||||
return RET_OK;
|
||||
}
|
||||
if (in_dims.size() > kMaxShapeSize) {
|
||||
MS_LOG(ERROR) << "in_dims size > " << kMaxShapeSize << " cannot copy data.";
|
||||
return RET_ERROR;
|
||||
}
|
||||
memcpy(in_shape_, in_dims.data(), in_dims.size() * sizeof(int));
|
||||
if (out_dims.size() > kMaxShapeSize) {
|
||||
MS_LOG(ERROR) << "out_dims size > " << kMaxShapeSize << " cannot copy data.";
|
||||
return RET_ERROR;
|
||||
}
|
||||
memcpy(out_shape_, out_dims.data(), out_dims.size() * sizeof(int));
|
||||
|
||||
if (out_tensor->shape().size() > DIMENSION_6D) {
|
||||
|
|
|
@ -69,6 +69,7 @@ Tensor *Tensor::CopyTensor(const Tensor &src_tensor, bool copy_data, AllocatorPt
|
|||
auto ret = CopyTensorData(src_tensor, result);
|
||||
if (ret != RET_OK) {
|
||||
MS_LOG(ERROR) << "CopyTensorData error";
|
||||
delete result;
|
||||
return nullptr;
|
||||
}
|
||||
result->own_data_ = src_tensor.own_data_;
|
||||
|
|
|
@ -52,7 +52,12 @@ STATUS Optimizer::Run(schema::MetaGraphT *graphDefT) {
|
|||
// each node should go through all node pass not each node pass go through all node
|
||||
for (auto &opDef : graphDefT->nodes) {
|
||||
for (auto pass : this->node_passes_) {
|
||||
status = pass->Run(new (std::nothrow) GraphNode(graphDefT, opDef.get()));
|
||||
auto graph_node = new (std::nothrow) GraphNode(graphDefT, opDef.get());
|
||||
if (graph_node == nullptr) {
|
||||
return RET_ERROR;
|
||||
}
|
||||
status = pass->Run(graph_node);
|
||||
delete graph_node;
|
||||
if (status != RET_OK && status != RET_NO_CHANGE && status != RET_INFER_INVALID) {
|
||||
MS_LOG(ERROR) << "Run NodePass failed";
|
||||
return status;
|
||||
|
|
|
@ -749,12 +749,17 @@ SessionModel CreateSessionByFuncGraph(const FuncGraphPtr &func_graph, const conv
|
|||
auto session = session::LiteSession::CreateSession(&ctx);
|
||||
if (session == nullptr) {
|
||||
MS_LOG(ERROR) << "create session failed.";
|
||||
model->Free();
|
||||
delete meta_graph;
|
||||
return sm;
|
||||
}
|
||||
|
||||
status = session->CompileGraph(model);
|
||||
if (status != RET_OK) {
|
||||
MS_LOG(ERROR) << "CompileGraph error";
|
||||
model->Free();
|
||||
delete meta_graph;
|
||||
delete session;
|
||||
return sm;
|
||||
}
|
||||
model->Free();
|
||||
|
|
Loading…
Reference in New Issue