| @@ -326,6 +326,11 @@ LiteSession::~LiteSession() { | |||||
| } | } | ||||
| } | } | ||||
| input_vec_.clear(); | input_vec_.clear(); | ||||
| #if SUPPORT_GPU | |||||
| if (context_->device_ctx_.type == DT_GPU) { | |||||
| lite::opencl::OpenCLRuntime::DeleteInstance(); | |||||
| } | |||||
| #endif | |||||
| delete this->context_; | delete this->context_; | ||||
| delete this->executor; | delete this->executor; | ||||
| this->executor = nullptr; | this->executor = nullptr; | ||||
| @@ -24,7 +24,7 @@ | |||||
| namespace mindspore::lite::opencl { | namespace mindspore::lite::opencl { | ||||
| OpenCLAllocator::OpenCLAllocator() {} | OpenCLAllocator::OpenCLAllocator() {} | ||||
| OpenCLAllocator::~OpenCLAllocator() {} | |||||
| OpenCLAllocator::~OpenCLAllocator() { Clear(); } | |||||
| void OpenCLAllocator::SetContext(const AllocatorContext &ctx) { | void OpenCLAllocator::SetContext(const AllocatorContext &ctx) { | ||||
| lock_flag_ = ctx.lockFlag; | lock_flag_ = ctx.lockFlag; | ||||
| @@ -40,12 +40,29 @@ static std::mutex g_mtx; | |||||
| static std::mutex g_init_mtx; | static std::mutex g_init_mtx; | ||||
| bool OpenCLRuntime::init_done_ = false; | bool OpenCLRuntime::init_done_ = false; | ||||
| OpenCLRuntime *OpenCLRuntime::ocl_runtime_instance_ = nullptr; | |||||
| size_t OpenCLRuntime::instance_count_ = 0; | |||||
| OpenCLRuntime *OpenCLRuntime::GetInstance() { | OpenCLRuntime *OpenCLRuntime::GetInstance() { | ||||
| std::unique_lock<std::mutex> lck(g_mtx); | std::unique_lock<std::mutex> lck(g_mtx); | ||||
| static OpenCLRuntime ocl_runtime; | 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<std::mutex> 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"; } | OpenCLRuntime::OpenCLRuntime() { default_build_opts_ = " -cl-mad-enable -cl-fast-relaxed-math -Werror"; } | ||||
| @@ -207,16 +224,25 @@ int OpenCLRuntime::Init() { | |||||
| return RET_OK; | return RET_OK; | ||||
| } | } | ||||
| OpenCLRuntime::~OpenCLRuntime() { | |||||
| init_done_ = false; | |||||
| int OpenCLRuntime::Uninit() { | |||||
| program_map_.clear(); | program_map_.clear(); | ||||
| delete allocator_; | delete allocator_; | ||||
| delete default_command_queue_; | delete default_command_queue_; | ||||
| delete context_; | delete context_; | ||||
| delete device_; | delete device_; | ||||
| allocator_ = nullptr; | |||||
| default_command_queue_ = nullptr; | |||||
| context_ = nullptr; | |||||
| device_ = nullptr; | |||||
| #ifdef USE_OPENCL_WRAPPER | |||||
| OpenCLWrapper::GetInstance()->UnLoadOpenCLLibrary(); | OpenCLWrapper::GetInstance()->UnLoadOpenCLLibrary(); | ||||
| #endif | |||||
| init_done_ = false; | |||||
| return RET_OK; | |||||
| } | } | ||||
| OpenCLRuntime::~OpenCLRuntime() { Uninit(); } | |||||
| cl::Context *OpenCLRuntime::Context() { return context_; } | cl::Context *OpenCLRuntime::Context() { return context_; } | ||||
| cl::Device *OpenCLRuntime::Device() { return device_; } | cl::Device *OpenCLRuntime::Device() { return device_; } | ||||
| @@ -47,6 +47,7 @@ class OpenCLRuntime { | |||||
| OpenCLRuntime &operator=(const OpenCLRuntime &) = delete; | OpenCLRuntime &operator=(const OpenCLRuntime &) = delete; | ||||
| int Init(); | int Init(); | ||||
| int Uninit(); | |||||
| cl::Context *Context(); | cl::Context *Context(); | ||||
| cl::Device *Device(); | cl::Device *Device(); | ||||
| @@ -143,6 +144,8 @@ class OpenCLRuntime { | |||||
| private: | private: | ||||
| static bool init_done_; | static bool init_done_; | ||||
| static size_t instance_count_; | |||||
| static OpenCLRuntime *ocl_runtime_instance_; | |||||
| cl::CommandQueue *default_command_queue_{nullptr}; | cl::CommandQueue *default_command_queue_{nullptr}; | ||||
| cl::Context *context_{nullptr}; | cl::Context *context_{nullptr}; | ||||
| cl::Device *device_{nullptr}; | cl::Device *device_{nullptr}; | ||||
| @@ -377,6 +377,7 @@ TEST_F(TestActivationOpenCL, SigmoidFp32_dim4) { | |||||
| delete input_tensor; | delete input_tensor; | ||||
| delete output_tensor; | delete output_tensor; | ||||
| delete sub_graph; | delete sub_graph; | ||||
| lite::opencl::OpenCLRuntime::DeleteInstance(); | |||||
| } | } | ||||
| TEST_F(TestActivationOpenCL, LeakyReluFp32_dim4) { | TEST_F(TestActivationOpenCL, LeakyReluFp32_dim4) { | ||||
| @@ -480,5 +481,6 @@ TEST_F(TestActivationOpenCL, LeakyReluFp32_dim4) { | |||||
| delete input_tensor; | delete input_tensor; | ||||
| delete output_tensor; | delete output_tensor; | ||||
| delete sub_graph; | delete sub_graph; | ||||
| lite::opencl::OpenCLRuntime::DeleteInstance(); | |||||
| } | } | ||||
| } // namespace mindspore | } // namespace mindspore | ||||
| @@ -202,6 +202,7 @@ void TestCase(const std::vector<int> &shape_a, const std::vector<int> &shape_b) | |||||
| for (auto tensor : outputs) { | for (auto tensor : outputs) { | ||||
| delete tensor; | delete tensor; | ||||
| } | } | ||||
| lite::opencl::OpenCLRuntime::DeleteInstance(); | |||||
| } | } | ||||
| class TestArithmeticOpenCL : public mindspore::CommonTest { | class TestArithmeticOpenCL : public mindspore::CommonTest { | ||||
| @@ -142,6 +142,7 @@ TEST_F(TestAvgPoolingOpenCL, AvgPoolFp32) { | |||||
| delete pooling_kernel; | delete pooling_kernel; | ||||
| delete pGraph; | delete pGraph; | ||||
| delete param; | delete param; | ||||
| lite::opencl::OpenCLRuntime::DeleteInstance(); | |||||
| } | } | ||||
| } // namespace mindspore | } // namespace mindspore | ||||
| @@ -158,6 +158,7 @@ TEST_F(TestBatchnormOpenCLfp16, Batchnormfp16input_dim4) { | |||||
| delete param; | delete param; | ||||
| delete batchnorm_kernel; | delete batchnorm_kernel; | ||||
| delete sub_graph; | delete sub_graph; | ||||
| lite::opencl::OpenCLRuntime::DeleteInstance(); | |||||
| } | } | ||||
| TEST_F(TestBatchnormOpenCLfp32, Batchnormfp32input_dim4) { | TEST_F(TestBatchnormOpenCLfp32, Batchnormfp32input_dim4) { | ||||
| MS_LOG(INFO) << "begin test"; | MS_LOG(INFO) << "begin test"; | ||||
| @@ -277,5 +278,6 @@ TEST_F(TestBatchnormOpenCLfp32, Batchnormfp32input_dim4) { | |||||
| delete param; | delete param; | ||||
| delete batchnorm_kernel; | delete batchnorm_kernel; | ||||
| delete sub_graph; | delete sub_graph; | ||||
| lite::opencl::OpenCLRuntime::DeleteInstance(); | |||||
| } | } | ||||
| } // namespace mindspore | } // namespace mindspore | ||||
| @@ -198,5 +198,6 @@ TEST_F(TestBiasAddOpenCL, BiasAddFp32_dim4) { | |||||
| delete sub_graph; | delete sub_graph; | ||||
| delete param; | delete param; | ||||
| delete biasadd_kernel; | delete biasadd_kernel; | ||||
| lite::opencl::OpenCLRuntime::DeleteInstance(); | |||||
| } | } | ||||
| } // namespace mindspore | } // namespace mindspore | ||||
| @@ -218,6 +218,7 @@ TEST_F(TestConcatOpenCLfp16, ConcatFp16_2input_dim4_axis3) { | |||||
| delete param; | delete param; | ||||
| delete concat_kernel; | delete concat_kernel; | ||||
| delete sub_graph; | delete sub_graph; | ||||
| lite::opencl::OpenCLRuntime::DeleteInstance(); | |||||
| } | } | ||||
| TEST_F(TestConcatOpenCLfp32, ConcatFp32_2input_dim4_axis3) { | TEST_F(TestConcatOpenCLfp32, ConcatFp32_2input_dim4_axis3) { | ||||
| @@ -338,5 +339,6 @@ TEST_F(TestConcatOpenCLfp32, ConcatFp32_2input_dim4_axis3) { | |||||
| delete param; | delete param; | ||||
| delete concat_kernel; | delete concat_kernel; | ||||
| delete sub_graph; | delete sub_graph; | ||||
| lite::opencl::OpenCLRuntime::DeleteInstance(); | |||||
| } | } | ||||
| } // namespace mindspore | } // namespace mindspore | ||||
| @@ -134,6 +134,7 @@ void RunTestCaseConv2dTranspose(const std::vector<int> &shape, void *input_data, | |||||
| inputs[0]->SetData(nullptr); | inputs[0]->SetData(nullptr); | ||||
| outputs[0]->SetData(nullptr); | outputs[0]->SetData(nullptr); | ||||
| lite::opencl::OpenCLRuntime::DeleteInstance(); | |||||
| } | } | ||||
| void RunTestCaseConv2dTranspose(const std::vector<int> shape, const std::vector<std::string> file_path, bool fp16) { | void RunTestCaseConv2dTranspose(const std::vector<int> shape, const std::vector<std::string> file_path, bool fp16) { | ||||
| @@ -158,6 +158,7 @@ void TEST_MAIN(schema::Format input_format, schema::Format output_format, const | |||||
| bias_tensor.SetData(nullptr); | bias_tensor.SetData(nullptr); | ||||
| delete param; | delete param; | ||||
| delete sub_graph; | delete sub_graph; | ||||
| lite::opencl::OpenCLRuntime::DeleteInstance(); | |||||
| } | } | ||||
| TEST_F(TestConvolutionOpenCL, in1x224x224x3_out1x112x112x32_k33_s22_p0101_fp32) { | TEST_F(TestConvolutionOpenCL, in1x224x224x3_out1x112x112x32_k33_s22_p0101_fp32) { | ||||
| @@ -162,6 +162,7 @@ void DepthWiseTestMain(ConvParameter *conv_param, T2 *input_data, T1 *weight_dat | |||||
| inputs[1]->SetData(nullptr); | inputs[1]->SetData(nullptr); | ||||
| inputs[2]->SetData(nullptr); | inputs[2]->SetData(nullptr); | ||||
| delete[] packed_input; | delete[] packed_input; | ||||
| lite::opencl::OpenCLRuntime::DeleteInstance(); | |||||
| return; | return; | ||||
| } | } | ||||
| @@ -587,5 +588,6 @@ TEST_F(TestConvolutionDwOpenCL, ProfilingMobilenetv2Fp32) { | |||||
| } | } | ||||
| delete [] input_data; | delete [] input_data; | ||||
| delete [] weight_data; | delete [] weight_data; | ||||
| lite::opencl::OpenCLRuntime::DeleteInstance(); | |||||
| } | } | ||||
| } // namespace mindspore | } // namespace mindspore | ||||
| @@ -111,6 +111,7 @@ void RunTestCaseMatMul(const std::vector<int> shape, const std::vector<std::stri | |||||
| tensor_x->SetData(nullptr); | tensor_x->SetData(nullptr); | ||||
| tensor_out->SetData(nullptr); | tensor_out->SetData(nullptr); | ||||
| MS_LOG(INFO) << "TestMatMulFp32 passed"; | MS_LOG(INFO) << "TestMatMulFp32 passed"; | ||||
| lite::opencl::OpenCLRuntime::DeleteInstance(); | |||||
| } | } | ||||
| TEST_F(TestMatMulOpenCL, MatMulFp32) { | TEST_F(TestMatMulOpenCL, MatMulFp32) { | ||||
| @@ -118,6 +118,7 @@ TEST_F(TestMaxPoolingOpenCL, MaxPool_1_32_512_96) { | |||||
| } | } | ||||
| delete pooling_kernel; | delete pooling_kernel; | ||||
| delete pGraph; | delete pGraph; | ||||
| lite::opencl::OpenCLRuntime::DeleteInstance(); | |||||
| } | } | ||||
| } // namespace mindspore | } // namespace mindspore | ||||
| @@ -183,5 +183,6 @@ TEST_F(TestPReluOpenCL, PReluFp32_dim4) { | |||||
| delete param; | delete param; | ||||
| delete prelu_kernel; | delete prelu_kernel; | ||||
| delete sub_graph; | delete sub_graph; | ||||
| lite::opencl::OpenCLRuntime::DeleteInstance(); | |||||
| } | } | ||||
| } // namespace mindspore | } // namespace mindspore | ||||
| @@ -106,5 +106,6 @@ TEST_F(TestReshapeOpenCL, ReshapeFp32) { | |||||
| outputs[0]->SetData(nullptr); | outputs[0]->SetData(nullptr); | ||||
| MS_LOG(INFO) << "Test ReshapeFp32 passed"; | MS_LOG(INFO) << "Test ReshapeFp32 passed"; | ||||
| lite::opencl::OpenCLRuntime::DeleteInstance(); | |||||
| } | } | ||||
| } // namespace mindspore | } // namespace mindspore | ||||
| @@ -148,6 +148,7 @@ TEST_F(TestSliceOpenCLfp32, Slicefp32input_dim4) { | |||||
| } | } | ||||
| delete slice_kernel; | delete slice_kernel; | ||||
| delete sub_graph; | delete sub_graph; | ||||
| lite::opencl::OpenCLRuntime::DeleteInstance(); | |||||
| } | } | ||||
| TEST_F(TestSliceOpenCLfp16, Slicefp16input_dim4) { | TEST_F(TestSliceOpenCLfp16, Slicefp16input_dim4) { | ||||
| MS_LOG(INFO) << "begin test"; | MS_LOG(INFO) << "begin test"; | ||||
| @@ -258,5 +259,6 @@ TEST_F(TestSliceOpenCLfp16, Slicefp16input_dim4) { | |||||
| } | } | ||||
| delete slice_kernel; | delete slice_kernel; | ||||
| delete sub_graph; | delete sub_graph; | ||||
| lite::opencl::OpenCLRuntime::DeleteInstance(); | |||||
| } | } | ||||
| } // namespace mindspore | } // namespace mindspore | ||||
| @@ -92,6 +92,7 @@ void RunTestCase(std::vector<int> input_shape, std::vector<int> output_shape, st | |||||
| } | } | ||||
| delete kernel; | delete kernel; | ||||
| delete pGraph; | delete pGraph; | ||||
| lite::opencl::OpenCLRuntime::DeleteInstance(); | |||||
| } | } | ||||
| TEST_F(TestSoftmaxOpenCL, Softmax_1) { | TEST_F(TestSoftmaxOpenCL, Softmax_1) { | ||||
| @@ -103,5 +103,6 @@ TEST_F(TestToFormatOpenCL, ToFormatNHWC2NCHW) { | |||||
| // compare | // compare | ||||
| CompareOutputData(output_data, correct_data, h * w * c, 0.00001); | CompareOutputData(output_data, correct_data, h * w * c, 0.00001); | ||||
| MS_LOG(INFO) << "Test TransposeFp32 passed"; | MS_LOG(INFO) << "Test TransposeFp32 passed"; | ||||
| lite::opencl::OpenCLRuntime::DeleteInstance(); | |||||
| } | } | ||||
| } // namespace mindspore | } // namespace mindspore | ||||
| @@ -108,5 +108,6 @@ TEST_F(TestTransposeOpenCL, TransposeFp32) { | |||||
| outputs[0]->SetData(nullptr); | outputs[0]->SetData(nullptr); | ||||
| MS_LOG(INFO) << "Test TransposeFp32 passed"; | MS_LOG(INFO) << "Test TransposeFp32 passed"; | ||||
| lite::opencl::OpenCLRuntime::DeleteInstance(); | |||||
| } | } | ||||
| } // namespace mindspore | } // namespace mindspore | ||||