diff --git a/mindspore/lite/src/lite_session.cc b/mindspore/lite/src/lite_session.cc index 7c09737fad..7e6aa192a1 100644 --- a/mindspore/lite/src/lite_session.cc +++ b/mindspore/lite/src/lite_session.cc @@ -326,6 +326,11 @@ LiteSession::~LiteSession() { } } input_vec_.clear(); +#if SUPPORT_GPU + if (context_->device_ctx_.type == DT_GPU) { + lite::opencl::OpenCLRuntime::DeleteInstance(); + } +#endif delete this->context_; delete this->executor; this->executor = nullptr; diff --git a/mindspore/lite/src/runtime/opencl/opencl_allocator.cc b/mindspore/lite/src/runtime/opencl/opencl_allocator.cc index 404e9a372e..b9bae225dd 100644 --- a/mindspore/lite/src/runtime/opencl/opencl_allocator.cc +++ b/mindspore/lite/src/runtime/opencl/opencl_allocator.cc @@ -24,7 +24,7 @@ namespace mindspore::lite::opencl { OpenCLAllocator::OpenCLAllocator() {} -OpenCLAllocator::~OpenCLAllocator() {} +OpenCLAllocator::~OpenCLAllocator() { Clear(); } void OpenCLAllocator::SetContext(const AllocatorContext &ctx) { lock_flag_ = ctx.lockFlag; diff --git a/mindspore/lite/src/runtime/opencl/opencl_runtime.cc b/mindspore/lite/src/runtime/opencl/opencl_runtime.cc index 8d214d5a98..bd6ece20fd 100644 --- a/mindspore/lite/src/runtime/opencl/opencl_runtime.cc +++ b/mindspore/lite/src/runtime/opencl/opencl_runtime.cc @@ -40,12 +40,29 @@ static std::mutex g_mtx; static std::mutex g_init_mtx; bool OpenCLRuntime::init_done_ = false; +OpenCLRuntime *OpenCLRuntime::ocl_runtime_instance_ = nullptr; +size_t OpenCLRuntime::instance_count_ = 0; OpenCLRuntime *OpenCLRuntime::GetInstance() { std::unique_lock lck(g_mtx); static OpenCLRuntime ocl_runtime; - ocl_runtime.Init(); - return &ocl_runtime; + if (instance_count_ == 0) { + ocl_runtime_instance_ = &ocl_runtime; + ocl_runtime_instance_->Init(); + } + instance_count_++; + return ocl_runtime_instance_; +} + +void OpenCLRuntime::DeleteInstance() { + std::unique_lock lck(g_mtx); + if (instance_count_ == 0) { + MS_LOG(ERROR) << "No OpenCLRuntime instance could delete!"; + } + instance_count_--; + if (instance_count_ == 0) { + ocl_runtime_instance_->Uninit(); + } } OpenCLRuntime::OpenCLRuntime() { default_build_opts_ = " -cl-mad-enable -cl-fast-relaxed-math -Werror"; } @@ -207,16 +224,25 @@ int OpenCLRuntime::Init() { return RET_OK; } -OpenCLRuntime::~OpenCLRuntime() { - init_done_ = false; +int OpenCLRuntime::Uninit() { program_map_.clear(); delete allocator_; delete default_command_queue_; delete context_; delete device_; + allocator_ = nullptr; + default_command_queue_ = nullptr; + context_ = nullptr; + device_ = nullptr; +#ifdef USE_OPENCL_WRAPPER OpenCLWrapper::GetInstance()->UnLoadOpenCLLibrary(); +#endif + init_done_ = false; + return RET_OK; } +OpenCLRuntime::~OpenCLRuntime() { Uninit(); } + cl::Context *OpenCLRuntime::Context() { return context_; } cl::Device *OpenCLRuntime::Device() { return device_; } diff --git a/mindspore/lite/src/runtime/opencl/opencl_runtime.h b/mindspore/lite/src/runtime/opencl/opencl_runtime.h index f56f7140b2..993c6f8368 100644 --- a/mindspore/lite/src/runtime/opencl/opencl_runtime.h +++ b/mindspore/lite/src/runtime/opencl/opencl_runtime.h @@ -47,6 +47,7 @@ class OpenCLRuntime { OpenCLRuntime &operator=(const OpenCLRuntime &) = delete; int Init(); + int Uninit(); cl::Context *Context(); cl::Device *Device(); @@ -143,6 +144,8 @@ class OpenCLRuntime { private: static bool init_done_; + static size_t instance_count_; + static OpenCLRuntime *ocl_runtime_instance_; cl::CommandQueue *default_command_queue_{nullptr}; cl::Context *context_{nullptr}; cl::Device *device_{nullptr}; diff --git a/mindspore/lite/test/ut/src/runtime/kernel/opencl/activation_tests.cc b/mindspore/lite/test/ut/src/runtime/kernel/opencl/activation_tests.cc index d0bb7b5104..1298ec65ef 100644 --- a/mindspore/lite/test/ut/src/runtime/kernel/opencl/activation_tests.cc +++ b/mindspore/lite/test/ut/src/runtime/kernel/opencl/activation_tests.cc @@ -377,6 +377,7 @@ TEST_F(TestActivationOpenCL, SigmoidFp32_dim4) { delete input_tensor; delete output_tensor; delete sub_graph; + lite::opencl::OpenCLRuntime::DeleteInstance(); } TEST_F(TestActivationOpenCL, LeakyReluFp32_dim4) { @@ -480,5 +481,6 @@ TEST_F(TestActivationOpenCL, LeakyReluFp32_dim4) { delete input_tensor; delete output_tensor; delete sub_graph; + lite::opencl::OpenCLRuntime::DeleteInstance(); } } // namespace mindspore diff --git a/mindspore/lite/test/ut/src/runtime/kernel/opencl/arithmetic_tests.cc b/mindspore/lite/test/ut/src/runtime/kernel/opencl/arithmetic_tests.cc index c6cebe265d..a752594142 100644 --- a/mindspore/lite/test/ut/src/runtime/kernel/opencl/arithmetic_tests.cc +++ b/mindspore/lite/test/ut/src/runtime/kernel/opencl/arithmetic_tests.cc @@ -202,6 +202,7 @@ void TestCase(const std::vector &shape_a, const std::vector &shape_b) for (auto tensor : outputs) { delete tensor; } + lite::opencl::OpenCLRuntime::DeleteInstance(); } class TestArithmeticOpenCL : public mindspore::CommonTest { diff --git a/mindspore/lite/test/ut/src/runtime/kernel/opencl/avg_pooling_tests.cc b/mindspore/lite/test/ut/src/runtime/kernel/opencl/avg_pooling_tests.cc index 308c2e339b..1c8edc5b35 100644 --- a/mindspore/lite/test/ut/src/runtime/kernel/opencl/avg_pooling_tests.cc +++ b/mindspore/lite/test/ut/src/runtime/kernel/opencl/avg_pooling_tests.cc @@ -142,6 +142,7 @@ TEST_F(TestAvgPoolingOpenCL, AvgPoolFp32) { delete pooling_kernel; delete pGraph; delete param; + lite::opencl::OpenCLRuntime::DeleteInstance(); } } // namespace mindspore diff --git a/mindspore/lite/test/ut/src/runtime/kernel/opencl/batchnorm_tests.cc b/mindspore/lite/test/ut/src/runtime/kernel/opencl/batchnorm_tests.cc index 0bafff9140..ab95c9e57e 100644 --- a/mindspore/lite/test/ut/src/runtime/kernel/opencl/batchnorm_tests.cc +++ b/mindspore/lite/test/ut/src/runtime/kernel/opencl/batchnorm_tests.cc @@ -158,6 +158,7 @@ TEST_F(TestBatchnormOpenCLfp16, Batchnormfp16input_dim4) { delete param; delete batchnorm_kernel; delete sub_graph; + lite::opencl::OpenCLRuntime::DeleteInstance(); } TEST_F(TestBatchnormOpenCLfp32, Batchnormfp32input_dim4) { MS_LOG(INFO) << "begin test"; @@ -277,5 +278,6 @@ TEST_F(TestBatchnormOpenCLfp32, Batchnormfp32input_dim4) { delete param; delete batchnorm_kernel; delete sub_graph; + lite::opencl::OpenCLRuntime::DeleteInstance(); } } // namespace mindspore diff --git a/mindspore/lite/test/ut/src/runtime/kernel/opencl/biasadd_tests.cc b/mindspore/lite/test/ut/src/runtime/kernel/opencl/biasadd_tests.cc index cd939cbc47..57960e4094 100644 --- a/mindspore/lite/test/ut/src/runtime/kernel/opencl/biasadd_tests.cc +++ b/mindspore/lite/test/ut/src/runtime/kernel/opencl/biasadd_tests.cc @@ -198,5 +198,6 @@ TEST_F(TestBiasAddOpenCL, BiasAddFp32_dim4) { delete sub_graph; delete param; delete biasadd_kernel; + lite::opencl::OpenCLRuntime::DeleteInstance(); } } // namespace mindspore diff --git a/mindspore/lite/test/ut/src/runtime/kernel/opencl/concat_tests.cc b/mindspore/lite/test/ut/src/runtime/kernel/opencl/concat_tests.cc index b843b339a7..08f6223de2 100644 --- a/mindspore/lite/test/ut/src/runtime/kernel/opencl/concat_tests.cc +++ b/mindspore/lite/test/ut/src/runtime/kernel/opencl/concat_tests.cc @@ -218,6 +218,7 @@ TEST_F(TestConcatOpenCLfp16, ConcatFp16_2input_dim4_axis3) { delete param; delete concat_kernel; delete sub_graph; + lite::opencl::OpenCLRuntime::DeleteInstance(); } TEST_F(TestConcatOpenCLfp32, ConcatFp32_2input_dim4_axis3) { @@ -338,5 +339,6 @@ TEST_F(TestConcatOpenCLfp32, ConcatFp32_2input_dim4_axis3) { delete param; delete concat_kernel; delete sub_graph; + lite::opencl::OpenCLRuntime::DeleteInstance(); } } // namespace mindspore diff --git a/mindspore/lite/test/ut/src/runtime/kernel/opencl/conv2d_transpose_tests.cc b/mindspore/lite/test/ut/src/runtime/kernel/opencl/conv2d_transpose_tests.cc index a1e51f0b0a..1bdb6f8575 100644 --- a/mindspore/lite/test/ut/src/runtime/kernel/opencl/conv2d_transpose_tests.cc +++ b/mindspore/lite/test/ut/src/runtime/kernel/opencl/conv2d_transpose_tests.cc @@ -134,6 +134,7 @@ void RunTestCaseConv2dTranspose(const std::vector &shape, void *input_data, inputs[0]->SetData(nullptr); outputs[0]->SetData(nullptr); + lite::opencl::OpenCLRuntime::DeleteInstance(); } void RunTestCaseConv2dTranspose(const std::vector shape, const std::vector file_path, bool fp16) { diff --git a/mindspore/lite/test/ut/src/runtime/kernel/opencl/convolution_tests.cc b/mindspore/lite/test/ut/src/runtime/kernel/opencl/convolution_tests.cc index 440695b577..457021b8a7 100644 --- a/mindspore/lite/test/ut/src/runtime/kernel/opencl/convolution_tests.cc +++ b/mindspore/lite/test/ut/src/runtime/kernel/opencl/convolution_tests.cc @@ -158,6 +158,7 @@ void TEST_MAIN(schema::Format input_format, schema::Format output_format, const bias_tensor.SetData(nullptr); delete param; delete sub_graph; + lite::opencl::OpenCLRuntime::DeleteInstance(); } TEST_F(TestConvolutionOpenCL, in1x224x224x3_out1x112x112x32_k33_s22_p0101_fp32) { diff --git a/mindspore/lite/test/ut/src/runtime/kernel/opencl/depthwise_conv2d_tests.cc b/mindspore/lite/test/ut/src/runtime/kernel/opencl/depthwise_conv2d_tests.cc index 693f6b97c1..8e6e32cd21 100644 --- a/mindspore/lite/test/ut/src/runtime/kernel/opencl/depthwise_conv2d_tests.cc +++ b/mindspore/lite/test/ut/src/runtime/kernel/opencl/depthwise_conv2d_tests.cc @@ -162,6 +162,7 @@ void DepthWiseTestMain(ConvParameter *conv_param, T2 *input_data, T1 *weight_dat inputs[1]->SetData(nullptr); inputs[2]->SetData(nullptr); delete[] packed_input; + lite::opencl::OpenCLRuntime::DeleteInstance(); return; } @@ -587,5 +588,6 @@ TEST_F(TestConvolutionDwOpenCL, ProfilingMobilenetv2Fp32) { } delete [] input_data; delete [] weight_data; + lite::opencl::OpenCLRuntime::DeleteInstance(); } } // namespace mindspore diff --git a/mindspore/lite/test/ut/src/runtime/kernel/opencl/matmul_tests.cc b/mindspore/lite/test/ut/src/runtime/kernel/opencl/matmul_tests.cc index e16d66fceb..9540fca960 100644 --- a/mindspore/lite/test/ut/src/runtime/kernel/opencl/matmul_tests.cc +++ b/mindspore/lite/test/ut/src/runtime/kernel/opencl/matmul_tests.cc @@ -111,6 +111,7 @@ void RunTestCaseMatMul(const std::vector shape, const std::vectorSetData(nullptr); tensor_out->SetData(nullptr); MS_LOG(INFO) << "TestMatMulFp32 passed"; + lite::opencl::OpenCLRuntime::DeleteInstance(); } TEST_F(TestMatMulOpenCL, MatMulFp32) { diff --git a/mindspore/lite/test/ut/src/runtime/kernel/opencl/max_pooling_tests.cc b/mindspore/lite/test/ut/src/runtime/kernel/opencl/max_pooling_tests.cc index 6e04cf9639..fce2f582c2 100644 --- a/mindspore/lite/test/ut/src/runtime/kernel/opencl/max_pooling_tests.cc +++ b/mindspore/lite/test/ut/src/runtime/kernel/opencl/max_pooling_tests.cc @@ -118,6 +118,7 @@ TEST_F(TestMaxPoolingOpenCL, MaxPool_1_32_512_96) { } delete pooling_kernel; delete pGraph; + lite::opencl::OpenCLRuntime::DeleteInstance(); } } // namespace mindspore diff --git a/mindspore/lite/test/ut/src/runtime/kernel/opencl/prelu_tests.cc b/mindspore/lite/test/ut/src/runtime/kernel/opencl/prelu_tests.cc index 90505a818e..64fc23c43c 100644 --- a/mindspore/lite/test/ut/src/runtime/kernel/opencl/prelu_tests.cc +++ b/mindspore/lite/test/ut/src/runtime/kernel/opencl/prelu_tests.cc @@ -183,5 +183,6 @@ TEST_F(TestPReluOpenCL, PReluFp32_dim4) { delete param; delete prelu_kernel; delete sub_graph; + lite::opencl::OpenCLRuntime::DeleteInstance(); } } // namespace mindspore diff --git a/mindspore/lite/test/ut/src/runtime/kernel/opencl/reshape_tests.cc b/mindspore/lite/test/ut/src/runtime/kernel/opencl/reshape_tests.cc index b7e7d45533..d0e0b344f1 100644 --- a/mindspore/lite/test/ut/src/runtime/kernel/opencl/reshape_tests.cc +++ b/mindspore/lite/test/ut/src/runtime/kernel/opencl/reshape_tests.cc @@ -106,5 +106,6 @@ TEST_F(TestReshapeOpenCL, ReshapeFp32) { outputs[0]->SetData(nullptr); MS_LOG(INFO) << "Test ReshapeFp32 passed"; + lite::opencl::OpenCLRuntime::DeleteInstance(); } } // namespace mindspore diff --git a/mindspore/lite/test/ut/src/runtime/kernel/opencl/slice_tests.cc b/mindspore/lite/test/ut/src/runtime/kernel/opencl/slice_tests.cc index b74e50add2..33ff054d32 100644 --- a/mindspore/lite/test/ut/src/runtime/kernel/opencl/slice_tests.cc +++ b/mindspore/lite/test/ut/src/runtime/kernel/opencl/slice_tests.cc @@ -148,6 +148,7 @@ TEST_F(TestSliceOpenCLfp32, Slicefp32input_dim4) { } delete slice_kernel; delete sub_graph; + lite::opencl::OpenCLRuntime::DeleteInstance(); } TEST_F(TestSliceOpenCLfp16, Slicefp16input_dim4) { MS_LOG(INFO) << "begin test"; @@ -258,5 +259,6 @@ TEST_F(TestSliceOpenCLfp16, Slicefp16input_dim4) { } delete slice_kernel; delete sub_graph; + lite::opencl::OpenCLRuntime::DeleteInstance(); } } // namespace mindspore diff --git a/mindspore/lite/test/ut/src/runtime/kernel/opencl/softmax_tests.cc b/mindspore/lite/test/ut/src/runtime/kernel/opencl/softmax_tests.cc index 444d162251..2913d322d9 100644 --- a/mindspore/lite/test/ut/src/runtime/kernel/opencl/softmax_tests.cc +++ b/mindspore/lite/test/ut/src/runtime/kernel/opencl/softmax_tests.cc @@ -92,6 +92,7 @@ void RunTestCase(std::vector input_shape, std::vector output_shape, st } delete kernel; delete pGraph; + lite::opencl::OpenCLRuntime::DeleteInstance(); } TEST_F(TestSoftmaxOpenCL, Softmax_1) { diff --git a/mindspore/lite/test/ut/src/runtime/kernel/opencl/to_format_tests.cc b/mindspore/lite/test/ut/src/runtime/kernel/opencl/to_format_tests.cc index 54e744af25..bbe9d94fa6 100644 --- a/mindspore/lite/test/ut/src/runtime/kernel/opencl/to_format_tests.cc +++ b/mindspore/lite/test/ut/src/runtime/kernel/opencl/to_format_tests.cc @@ -103,5 +103,6 @@ TEST_F(TestToFormatOpenCL, ToFormatNHWC2NCHW) { // compare CompareOutputData(output_data, correct_data, h * w * c, 0.00001); MS_LOG(INFO) << "Test TransposeFp32 passed"; + lite::opencl::OpenCLRuntime::DeleteInstance(); } } // namespace mindspore diff --git a/mindspore/lite/test/ut/src/runtime/kernel/opencl/transpose_tests.cc b/mindspore/lite/test/ut/src/runtime/kernel/opencl/transpose_tests.cc index b3d6205882..0cd2fa8536 100644 --- a/mindspore/lite/test/ut/src/runtime/kernel/opencl/transpose_tests.cc +++ b/mindspore/lite/test/ut/src/runtime/kernel/opencl/transpose_tests.cc @@ -108,5 +108,6 @@ TEST_F(TestTransposeOpenCL, TransposeFp32) { outputs[0]->SetData(nullptr); MS_LOG(INFO) << "Test TransposeFp32 passed"; + lite::opencl::OpenCLRuntime::DeleteInstance(); } } // namespace mindspore