未验证 提交 293d2d38 编写于 作者: Y Yuan Shuai 提交者: GitHub

[LITE][OPENCL] Add ReleaseResource for OpenCL when Predictor dead (#3358)

* [LITE][OPENCL] Add ReleaseResource for OpenCL when Predictor dead. test=develop

* fix void for decontrust. test=develop

* fix miscs. test=develop

* fix miscs. test=develop

* fix miscs. test=develop

* fix miscs. test=develop

* [LITE][OPENCL] fix Hang of mobilenetv1_test and kernel test. test=develop

* [LITE][OPENCL] Fix miscs. test is ok. test=develop
上级 8ae48e71
......@@ -43,6 +43,16 @@ class LITE_API Predictor {
public:
// Create an empty predictor.
Predictor() { scope_ = std::make_shared<Scope>(); }
~Predictor() {
#ifdef LITE_WITH_OPENCL
CLRuntime::Global()->ReleaseResources();
#endif
scope_.reset();
exec_scope_ = nullptr;
program_.reset();
input_names_.clear();
output_names_.clear();
}
// Create a predictor with the weight variable scope set.
explicit Predictor(const std::shared_ptr<lite::Scope>& root_scope)
: scope_(root_scope) {}
......
......@@ -107,6 +107,8 @@ class LightPredictorImpl : public lite_api::PaddlePredictor {
public:
LightPredictorImpl() = default;
~LightPredictorImpl();
std::unique_ptr<lite_api::Tensor> GetInput(int i) override;
std::unique_ptr<const lite_api::Tensor> GetOutput(int i) const override;
......
......@@ -21,6 +21,13 @@
namespace paddle {
namespace lite {
LightPredictorImpl::~LightPredictorImpl() {
raw_predictor_.reset();
#ifdef LITE_WITH_OPENCL
CLRuntime::Global()->ReleaseResources();
#endif
}
void LightPredictorImpl::Init(const lite_api::MobileConfig& config) {
// LightPredictor Only support NaiveBuffer backend in publish lib
if (config.lite_model_file().empty()) {
......
......@@ -29,30 +29,38 @@ CLRuntime* CLRuntime::Global() {
}
CLRuntime::~CLRuntime() {
LOG(INFO) << "CLRuntime::~CLRuntime()";
// Note: do ReleaseResources() in predictor
command_queue_&& clReleaseCommandQueue(command_queue_->get());
command_queue_.reset();
context_&& clReleaseContext(context_->get());
context_.reset();
device_.reset();
platform_.reset();
initialized_ = false;
}
void CLRuntime::ReleaseResources() {
// if (is_resources_released_) {
// return;
// }
if (command_queue_ != nullptr) {
command_queue_->flush();
command_queue_->finish();
}
for (size_t kidx = 0; kidx < kernels_.size(); ++kidx) {
clReleaseKernel(kernels_[kidx]->get());
kernels_[kidx].reset();
}
kernels_.clear();
kernel_offset_.clear();
for (auto& p : programs_) {
clReleaseProgram(p.second->get());
}
programs_.clear();
// For controlling the destruction order
command_queue_&& clReleaseCommandQueue(command_queue_->get());
command_queue_.reset();
context_&& clReleaseContext(context_->get());
context_.reset();
device_.reset();
platform_.reset();
LOG(INFO) << "release resources finished.";
is_resources_released_ = true;
}
bool CLRuntime::Init() {
......
......@@ -33,6 +33,8 @@ class CLRuntime {
public:
static CLRuntime* Global();
void ReleaseResources();
bool Init();
cl::Platform& platform();
......@@ -116,6 +118,8 @@ class CLRuntime {
bool initialized_{false};
bool is_init_success_{false};
bool is_resources_released_{false};
};
} // namespace lite
......
......@@ -126,7 +126,7 @@ class Buffer {
const size_t img_h,
void* host_ptr = nullptr) {
if (target != target_ || cl_image2d_width_ < img_w ||
cl_image2d_height_ < img_h) {
cl_image2d_height_ < img_h || host_ptr != nullptr) {
CHECK_EQ(own_data_, true) << "Can not reset unowned buffer.";
Free();
data_ = TargetWrapperCL::MallocImage<T>(img_w, img_h, host_ptr);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册