Browse Source

fix memory leak in sub_graph_kernel

tags/v1.1.0
hangangqiang 5 years ago
parent
commit
d10cf7c826
6 changed files with 32 additions and 19 deletions
  1. +10
    -7
      mindspore/lite/src/lite_session.cc
  2. +1
    -1
      mindspore/lite/src/lite_session.h
  3. +6
    -1
      mindspore/lite/src/runtime/kernel/opencl/subgraph_opencl_kernel.cc
  4. +11
    -8
      mindspore/lite/src/sub_graph_kernel.h
  5. +2
    -0
      mindspore/lite/src/tensor.h
  6. +2
    -2
      mindspore/lite/test/ut/src/infer_test.cc

+ 10
- 7
mindspore/lite/src/lite_session.cc View File

@@ -287,7 +287,7 @@ int LiteSession::CompileGraph(Model *model) {
is_running_.store(false);
return ret;
}
ret = executor->Prepare(this->kernels_);
ret = executor_->Prepare(this->kernels_);
if (ret != RET_OK) {
MS_LOG(ERROR) << "Prepare executor failed: " << ret;
is_running_.store(false);
@@ -325,9 +325,12 @@ int LiteSession::RunGraph(const KernelCallBack &before, const KernelCallBack &af
STATUS ret;
MS_ASSERT(this->context_);
if (before == nullptr && after == nullptr) {
ret = executor->Run(this->inputs_, this->outputs_, this->kernels_, this->context_->allocator.get());
ret = executor_->Run(this->inputs_, this->outputs_, this->kernels_, this->context_->allocator.get());
} else {
ret = executor->Run(this->inputs_, this->outputs_, this->kernels_, this->context_->allocator.get(), before, after);
ret = executor_->Run(this->inputs_, this->outputs_, this->kernels_, this->context_->allocator.get(), before, after);
}
if (ret != RET_OK) {
MS_LOG(ERROR) << "RunGraph failed : " << ret;
}
is_running_.store(false);
return ret;
@@ -376,8 +379,8 @@ int LiteSession::Init(const Context *context) {
}
}
#endif
executor = new (std::nothrow) Executor();
if (nullptr == executor) {
executor_ = new (std::nothrow) Executor();
if (nullptr == executor_) {
MS_LOG(ERROR) << "New Executor failed";
is_running_.store(false);
return RET_ERROR;
@@ -425,8 +428,8 @@ LiteSession::~LiteSession() {
delete kernel;
}
delete this->context_;
delete this->executor;
this->executor = nullptr;
delete this->executor_;
this->executor_ = nullptr;
is_running_.store(false);
}



+ 1
- 1
mindspore/lite/src/lite_session.h View File

@@ -110,7 +110,7 @@ class LiteSession : public session::LiteSession {
std::vector<std::string> output_tensor_names_;
// graph output tensor name -- output tensor
std::unordered_map<std::string, mindspore::tensor::MSTensor *> output_tensor_map_;
Executor *executor = nullptr;
Executor *executor_ = nullptr;
std::atomic<bool> is_running_ = false;
#if SUPPORT_GPU
opencl::OpenCLRuntimeWrapper ocl_runtime_wrap_;


+ 6
- 1
mindspore/lite/src/runtime/kernel/opencl/subgraph_opencl_kernel.cc View File

@@ -285,6 +285,7 @@ int SubGraphOpenCLKernel::UnInit() {
nodes_.clear();
in_convert_ops_.clear();
out_convert_ops_.clear();
delete this->executor_;
return RET_OK;
}

@@ -305,7 +306,11 @@ int SubGraphOpenCLKernel::Run() {
allocator_->UnmapBuffer(tensor->data_c());
}

executor_->Run(in_tensors_, out_tensors_, nodes_, allocator_);
auto ret = executor_->Run(in_tensors_, out_tensors_, nodes_, allocator_);
if (RET_OK != ret) {
MS_LOG(ERROR) << "Run opencl executor failed: " << ret;
return ret;
}
ocl_runtime_->SyncCommandQueue();

return RET_OK;


+ 11
- 8
mindspore/lite/src/sub_graph_kernel.h View File

@@ -35,7 +35,12 @@ class SubGraphKernel : public LiteKernel {
subgraph_type_ = kCpuFP32SubGraph;
}

~SubGraphKernel() override { delete (executor_); }
~SubGraphKernel() override {
for (auto *node : nodes_) {
delete node;
}
nodes_.clear();
}

// called while compiling graph. Call node->Prepare() by default.
int Prepare() override;
@@ -71,7 +76,7 @@ class CpuSubGraph : public SubGraphKernel {
this->executor_ = new mindspore::lite::Executor;
}

~CpuSubGraph() override = default;
~CpuSubGraph() override { delete this->executor_; }

int Prepare() override;
int Init() override { return SubGraphKernel::Init(); }
@@ -91,15 +96,14 @@ class CpuFp32SubGraph : public CpuSubGraph {
: CpuSubGraph(inputs, outputs, in_kernels, out_kernels, nodes, ctx) {
subgraph_type_ = kCpuFP32SubGraph;
this->name_ = "CpuFP32SubGraph";
this->executor_ = new mindspore::lite::Executor;
}

~CpuFp32SubGraph() override = default;
int Init() override { return mindspore::lite::RET_ERROR; }
int PreProcess() override;
int Run() override { return SubGraphKernel::Run(); }
int Run() override { return CpuSubGraph::Run(); }
int Run(const KernelCallBack &before, const KernelCallBack &after) override {
return SubGraphKernel::Run(before, after);
return CpuSubGraph::Run(before, after);
};
int PostProcess() override { return mindspore::lite::RET_OK; }
};
@@ -112,15 +116,14 @@ class CpuFp16SubGraph : public CpuSubGraph {
: CpuSubGraph(inputs, outputs, in_kernels, out_kernels, nodes, ctx) {
subgraph_type_ = kCpuFP16SubGraph;
this->name_ = "CpuFP16SubGraph";
this->executor_ = new mindspore::lite::Executor;
}

~CpuFp16SubGraph() override = default;
int Init() override { return mindspore::lite::RET_ERROR; }
int PreProcess() override;
int Run() override { return SubGraphKernel::Run(); }
int Run() override { return CpuSubGraph::Run(); }
int Run(const KernelCallBack &before, const KernelCallBack &after) override {
return SubGraphKernel::Run(before, after);
return CpuSubGraph::Run(before, after);
};
int PostProcess() override;
};


+ 2
- 0
mindspore/lite/src/tensor.h View File

@@ -89,6 +89,8 @@ class Tensor : public mindspore::tensor::MSTensor {

void set_allocator(mindspore::lite::Allocator *allocator) { allocator_ = allocator; }

mindspore::lite::Allocator *allocator() const { return this->allocator_; }

int MallocData(mindspore::lite::Allocator *allocator = nullptr);

int FreeData();


+ 2
- 2
mindspore/lite/test/ut/src/infer_test.cc View File

@@ -240,8 +240,8 @@ class SessionWithParallelExecutor : public lite::LiteSession {
public:
int Init(lite::InnerContext *context) {
lite::LiteSession::Init(context);
delete this->executor;
this->executor = new mindspore::lite::ParallelExecutor();
delete this->executor_;
this->executor_ = new mindspore::lite::ParallelExecutor();
return 0;
}
};


Loading…
Cancel
Save