From c8e043597b6464fe4aaa61db23c309b99a4dac7c Mon Sep 17 00:00:00 2001 From: greatpanc Date: Thu, 16 Dec 2021 14:57:06 +0800 Subject: [PATCH] opengl old benchmark bugfix and remove opengl Dependent library --- include/api/context.h | 20 +++++ mindspore/lite/include/context.h | 2 + mindspore/lite/src/CMakeLists.txt | 5 -- mindspore/lite/src/cxx_api/context.cc | 74 ++++++++++++++++--- mindspore/lite/src/cxx_api/converters.cc | 22 ++++-- mindspore/lite/src/cxx_api/converters.h | 5 +- mindspore/lite/src/lite_session.cc | 2 + .../src/runtime/gpu/opencl/opencl_runtime.cc | 21 +++--- .../src/runtime/gpu/opencl/opencl_runtime.h | 11 +++ mindspore/lite/src/scheduler.h | 2 + mindspore/lite/tools/benchmark/benchmark.cc | 35 +++++++-- mindspore/lite/tools/benchmark/benchmark.h | 2 +- .../tools/benchmark/benchmark_unified_api.cc | 31 +++++++- .../tools/benchmark/benchmark_unified_api.h | 2 +- 14 files changed, 187 insertions(+), 47 deletions(-) diff --git a/include/api/context.h b/include/api/context.h index 552dd4f8710..556ab98ff7a 100644 --- a/include/api/context.h +++ b/include/api/context.h @@ -277,6 +277,26 @@ class MS_API GPUDeviceInfo : public DeviceInfoContext { /// \return Whether enable sharing mem with OpenGL. bool GetEnableGLTexture() const; + /// \brief Set current OpenGL context + /// + /// \param[in] gl_context Current OpenGL context. + void SetGLContext(void *gl_context); + + /// \brief Get current OpenGL context + /// + /// \return the OpenCL context by OpenGL used. + void *GetGLContext() const; + + /// \brief Set current OpenGL display + /// + /// \param[in] gl_display Current OpenGL display. + void SetGLDisplay(void *gl_display); + + /// \brief Get current OpenGL display + /// + /// \return the OpenCL display by OpenGL used. + void *GetGLDisplay() const; + private: void SetPrecisionMode(const std::vector &precision_mode); std::vector GetPrecisionModeChar() const; diff --git a/mindspore/lite/include/context.h b/mindspore/lite/include/context.h index 3382a1404f8..a481315e2d3 100644 --- a/mindspore/lite/include/context.h +++ b/mindspore/lite/include/context.h @@ -35,6 +35,8 @@ typedef struct GpuDeviceInfo { int rank_id_ = 0; int group_size_ = 0; bool enable_gl_texture_ = false; /**params[kModelOptionGPUEnableFP16] = is_fp16; } -#ifdef ENABLE_OPENGL_TEXTURE -void GPUDeviceInfo::SetEnableGLTexture(bool is_enable_gl_texture) { - if (data_ == nullptr) { - MS_LOG(ERROR) << "Invalid context."; - return; - } - data_->params[kModelOptionGPUEnableEnableGLTexture] = is_enable_gl_texture; -} -#endif - bool GPUDeviceInfo::GetEnableFP16() const { if (data_ == nullptr) { MS_LOG(ERROR) << "Invalid context."; @@ -257,12 +249,70 @@ bool GPUDeviceInfo::GetEnableFP16() const { } #ifdef ENABLE_OPENGL_TEXTURE +void GPUDeviceInfo::SetEnableGLTexture(bool is_enable_gl_texture) { + if (data_ == nullptr) { + MS_LOG(ERROR) << "Invalid context."; + return; + } + data_->params[kModelOptionGPUEnableGLTexture] = is_enable_gl_texture; +} + bool GPUDeviceInfo::GetEnableGLTexture() const { if (data_ == nullptr) { MS_LOG(ERROR) << "Invalid context."; return false; } - return GetValue(data_, kModelOptionGPUEnableEnableGLTexture); + return GetValue(data_, kModelOptionGPUEnableGLTexture); +} + +void GPUDeviceInfo::SetGLContext(void *gl_context) { + if (data_ == nullptr) { + MS_LOG(ERROR) << "Invalid context."; + return; + } + data_->params[kModelOptionGPUGLContext] = gl_context; +} + +void *GPUDeviceInfo::GetGLContext() const { + if (data_ == nullptr) { + MS_LOG(ERROR) << "Invalid context."; + return nullptr; + } + return GetValue(data_, kModelOptionGPUGLContext); +} + +void GPUDeviceInfo::SetGLDisplay(void *gl_display) { + if (data_ == nullptr) { + MS_LOG(ERROR) << "Invalid context."; + return; + } + data_->params[kModelOptionGPUGLDisplay] = gl_display; +} + +void *GPUDeviceInfo::GetGLDisplay() const { + if (data_ == nullptr) { + MS_LOG(ERROR) << "Invalid context."; + return nullptr; + } + return GetValue(data_, kModelOptionGPUGLDisplay); +} +#else +void GPUDeviceInfo::SetEnableGLTexture(bool is_enable_gl_texture) { MS_LOG(ERROR) << "Unsupported Feature."; } +bool GPUDeviceInfo::GetEnableGLTexture() const { + MS_LOG(ERROR) << "Unsupported Feature."; + return false; +} + +void GPUDeviceInfo::SetGLContext(void *gl_context) { MS_LOG(ERROR) << "Unsupported Feature."; } +void *GPUDeviceInfo::GetGLContext() const { + MS_LOG(ERROR) << "Unsupported Feature."; + return nullptr; +} + +void GPUDeviceInfo::SetGLDisplay(void *gl_display) { MS_LOG(ERROR) << "Unsupported Feature."; } +void *GPUDeviceInfo::GetGLDisplay() const { + MS_LOG(ERROR) << "Unsupported Feature."; + return nullptr; } #endif diff --git a/mindspore/lite/src/cxx_api/converters.cc b/mindspore/lite/src/cxx_api/converters.cc index 332d9bfd8a8..32c397c5069 100644 --- a/mindspore/lite/src/cxx_api/converters.cc +++ b/mindspore/lite/src/cxx_api/converters.cc @@ -43,11 +43,12 @@ Status ContextUtils::AddCpuDevice(const std::shared_ptr &allocator, i } Status ContextUtils::AddGpuDevice(bool enable_fp16, uint32_t device_id, int rank_id, int group_size, - bool enable_gl_texture, const std::string &provider, - const std::string &provider_device, const std::shared_ptr &allocator, - lite::InnerContext *inner_context) { + bool enable_gl_texture, void *gl_context, void *gl_display, + const std::string &provider, const std::string &provider_device, + const std::shared_ptr &allocator, lite::InnerContext *inner_context) { lite::DeviceInfo device_info = {0}; - device_info.gpu_device_info_ = {enable_fp16, device_id, rank_id, group_size, enable_gl_texture}; + device_info.gpu_device_info_ = {enable_fp16, device_id, rank_id, group_size, + enable_gl_texture, gl_context, gl_display}; inner_context->device_list_.push_back({lite::DT_GPU, device_info, provider, provider_device, allocator}); return kSuccess; } @@ -95,12 +96,17 @@ lite::InnerContext *ContextUtils::Convert(Context *context) { } else if (device->GetDeviceType() == kGPU) { auto gpu_context = device->Cast(); bool enable_gl_texture = false; + void *gl_context = nullptr; + void *gl_display = nullptr; #ifdef ENABLE_OPENGL_TEXTURE enable_gl_texture = gpu_context->GetEnableGLTexture(); + gl_context = gpu_context->GetGLContext(); + gl_display = gpu_context->GetGLDisplay(); #endif - ret = AddGpuDevice(gpu_context->GetEnableFP16(), gpu_context->GetDeviceID(), gpu_context->GetRankID(), - gpu_context->GetGroupSize(), enable_gl_texture, gpu_context->GetProvider(), - gpu_context->GetProviderDevice(), gpu_context->GetAllocator(), inner_context.get()); + ret = + AddGpuDevice(gpu_context->GetEnableFP16(), gpu_context->GetDeviceID(), gpu_context->GetRankID(), + gpu_context->GetGroupSize(), enable_gl_texture, gl_context, gl_display, gpu_context->GetProvider(), + gpu_context->GetProviderDevice(), gpu_context->GetAllocator(), inner_context.get()); } else if (device->GetDeviceType() == kKirinNPU) { auto npu_context = device->Cast(); ret = AddNpuDevice(npu_context->GetFrequency(), inner_context.get()); @@ -140,7 +146,7 @@ lite::InnerContext *ContextUtils::Convert(const ContextC *context_c) { ret = AddCpuDevice(device_info_c->allocator, context_c->affinity_mode, device_info_c->enable_fp16, device_info_c->provider, device_info_c->provider_device, inner_context.get()); } else if (device_info_c->device_type == kMSDeviceTypeGPU) { - ret = AddGpuDevice(device_info_c->enable_fp16, 0, 0, 0, false, device_info_c->provider, + ret = AddGpuDevice(device_info_c->enable_fp16, 0, 0, 0, false, nullptr, nullptr, device_info_c->provider, device_info_c->provider_device, device_info_c->allocator, inner_context.get()); } else if (device_info_c->device_type == kMSDeviceTypeKirinNPU) { ret = AddNpuDevice(device_info_c->frequency, inner_context.get()); diff --git a/mindspore/lite/src/cxx_api/converters.h b/mindspore/lite/src/cxx_api/converters.h index ccec6d886e9..632caed5b40 100644 --- a/mindspore/lite/src/cxx_api/converters.h +++ b/mindspore/lite/src/cxx_api/converters.h @@ -39,8 +39,9 @@ class ContextUtils { const std::string &provider, const std::string &provider_device, lite::InnerContext *inner_context); static Status AddGpuDevice(bool enable_fp16, uint32_t device_id, int rank_id, int group_size, bool enable_gl_texture, - const std::string &provider, const std::string &provider_device, - const std::shared_ptr &allocator, lite::InnerContext *inner_context); + void *gl_context, void *gl_display, const std::string &provider, + const std::string &provider_device, const std::shared_ptr &allocator, + lite::InnerContext *inner_context); static Status AddNpuDevice(int frequency, lite::InnerContext *inner_context); static Status AddAscend310Device(lite::InnerContext *inner_context, DeviceInfoContext *device); static bool IsAffinityModeValid(int affinity_mode) { diff --git a/mindspore/lite/src/lite_session.cc b/mindspore/lite/src/lite_session.cc index f1667146732..8dc14beba4f 100644 --- a/mindspore/lite/src/lite_session.cc +++ b/mindspore/lite/src/lite_session.cc @@ -1382,6 +1382,8 @@ int LiteSession::InitGPURuntime() { #ifdef ENABLE_OPENGL_TEXTURE MS_LOG(INFO) << " InitGLQueue"; opencl_runtime->SetGLTextureEnable(gpu_device_info.enable_gl_texture_); + opencl_runtime->SetGLContext(gpu_device_info.gl_context_); + opencl_runtime->SetGLDisplay(gpu_device_info.gl_display_); if (opencl_runtime->InitGLQueue() != RET_OK) { MS_LOG(ERROR) << "Init OpenCL Runtime failed, the device unspport OpenGL sharing context or OpenGL Context is not Init"; diff --git a/mindspore/lite/src/runtime/gpu/opencl/opencl_runtime.cc b/mindspore/lite/src/runtime/gpu/opencl/opencl_runtime.cc index 10fd6959d98..3403f434d92 100644 --- a/mindspore/lite/src/runtime/gpu/opencl/opencl_runtime.cc +++ b/mindspore/lite/src/runtime/gpu/opencl/opencl_runtime.cc @@ -176,16 +176,18 @@ int OpenCLRuntime::InitQueue(std::vector *platforms) { if (this->GetGLTextureEnable()) { // create context from glcontext MS_LOG(INFO) << "Create special opencl context to share with OpenGL"; - if (eglGetCurrentContext() == EGL_NO_CONTEXT) { - MS_LOG(ERROR) << "eglGetCurrentContext error, find GL context failed"; + + if (!CheckGLContext()) { + MS_LOG(ERROR) << "GL Context error"; return RET_ERROR; } - if (eglGetCurrentDisplay() == EGL_NO_DISPLAY) { - MS_LOG(ERROR) << "eglGetDisplay error"; + if (!CheckGLDisplay()) { + MS_LOG(ERROR) << "GL Display error"; return RET_ERROR; } - cl_context_properties context_prop[] = {CL_GL_CONTEXT_KHR, (cl_context_properties)eglGetCurrentContext(), - CL_EGL_DISPLAY_KHR, (cl_context_properties)eglGetCurrentDisplay(), 0}; + + cl_context_properties context_prop[] = {CL_GL_CONTEXT_KHR, (cl_context_properties)*GetGLContext(), + CL_EGL_DISPLAY_KHR, (cl_context_properties)*GetGLDisplay(), 0}; context_ = new (std::nothrow) cl::Context(std::vector{*device_}, context_prop, nullptr, nullptr, &ret); if (context_ == nullptr || ret != CL_SUCCESS) { MS_LOG(ERROR) @@ -390,10 +392,6 @@ GpuInfo OpenCLRuntime::GetGpuInfo() { return gpu_info_; } bool OpenCLRuntime::GetFp16Enable() const { return fp16_enable_; } -#ifdef ENABLE_OPENGL_TEXTURE -bool OpenCLRuntime::GetGLTextureEnable() const { return enable_gl_texture_; } -#endif - // if support fp16, set fp16 will success. bool OpenCLRuntime::SetFp16Enable(bool enable) { fp16_enable_ = enable && support_fp16_; @@ -405,6 +403,9 @@ bool OpenCLRuntime::SetGLTextureEnable(bool enable) { enable_gl_texture_ = enable; return enable_gl_texture_ == enable; } + +bool OpenCLRuntime::GetGLTextureEnable() const { return enable_gl_texture_; } + #endif int OpenCLRuntime::BuildKernel(const cl::Kernel &kernel, const std::string &program_name, diff --git a/mindspore/lite/src/runtime/gpu/opencl/opencl_runtime.h b/mindspore/lite/src/runtime/gpu/opencl/opencl_runtime.h index 10bf93167e6..57842d5f697 100644 --- a/mindspore/lite/src/runtime/gpu/opencl/opencl_runtime.h +++ b/mindspore/lite/src/runtime/gpu/opencl/opencl_runtime.h @@ -71,6 +71,15 @@ class OpenCLRuntime { #ifdef ENABLE_OPENGL_TEXTURE bool GetGLTextureEnable() const; bool SetGLTextureEnable(bool enable); + + void SetGLContext(void *gl_context) { gl_context_ = reinterpret_cast(gl_context); } + EGLContext *GetGLContext() const { return gl_context_; } + bool CheckGLContext() const { return (GetGLContext() != nullptr && *GetGLContext() != EGL_NO_CONTEXT); } + + void SetGLDisplay(void *gl_display) { gl_display_ = reinterpret_cast(gl_display); } + EGLDisplay *GetGLDisplay() const { return gl_display_; } + bool CheckGLDisplay() const { return (GetGLDisplay() != nullptr && *GetGLDisplay() != EGL_NO_DISPLAY); } + int InitGLQueue() { return InitQueue(nullptr); } #endif bool GetSVMEnable() const { return svm_enable_; } @@ -197,6 +206,8 @@ class OpenCLRuntime { void *handle_{nullptr}; #ifdef ENABLE_OPENGL_TEXTURE bool enable_gl_texture_{false}; + EGLContext *gl_context_{nullptr}; + EGLDisplay *gl_display_{nullptr}; #endif TuningMode tuning_mode_{TuningMode::DEFAULT}; #if MS_OPENCL_PROFILE diff --git a/mindspore/lite/src/scheduler.h b/mindspore/lite/src/scheduler.h index 98f285e1d1c..c45f0aa6095 100644 --- a/mindspore/lite/src/scheduler.h +++ b/mindspore/lite/src/scheduler.h @@ -141,6 +141,8 @@ class Scheduler { #ifdef ENABLE_OPENGL_TEXTURE bool GetEnableGLTexture() { return context_->GetGpuInfo().enable_gl_texture_; } + void *GetGLContext() { return context_->GetGpuInfo().gl_context_; } + void *GetGLDisplay() { return context_->GetGpuInfo().gl_display_; } #endif protected: diff --git a/mindspore/lite/tools/benchmark/benchmark.cc b/mindspore/lite/tools/benchmark/benchmark.cc index 957cebb7ba9..512b6036cd7 100644 --- a/mindspore/lite/tools/benchmark/benchmark.cc +++ b/mindspore/lite/tools/benchmark/benchmark.cc @@ -52,7 +52,8 @@ int Benchmark::LoadInput() { } return RET_OK; } -#else +#endif + if (flags_->in_data_file_.empty()) { auto status = GenerateInputData(); if (status != RET_OK) { @@ -68,7 +69,6 @@ int Benchmark::LoadInput() { return status; } } -#endif return RET_OK; } @@ -294,7 +294,7 @@ int Benchmark::GetDataTypeByTensorName(const std::string &tensor_name) { } } -void Benchmark::InitContext(const std::shared_ptr &context) { +int Benchmark::InitContext(const std::shared_ptr &context) { auto &cpu_device_ctx = context->device_list_[0]; if (flags_->cpu_bind_mode_ == MID_CPU || flags_->cpu_bind_mode_ == HIGHER_CPU) { cpu_device_ctx.device_info_.cpu_device_info_.cpu_bind_mode_ = CpuBindMode(flags_->cpu_bind_mode_); @@ -309,6 +309,24 @@ void Benchmark::InitContext(const std::shared_ptr &context) { #ifdef ENABLE_OPENGL_TEXTURE if (flags_->enable_gl_texture_) { gpu_device_ctx.device_info_.gpu_device_info_.enable_gl_texture_ = true; + + EGLContext *gl_context = new (std::nothrow) EGLContext(); + if (gl_context == nullptr) { + MS_LOG(ERROR) << "new EGLContext failed"; + return RET_ERROR; + } else { + *gl_context = eglGetCurrentContext(); + } + gpu_device_ctx.device_info_.gpu_device_info_.gl_context_ = gl_context; + + EGLDisplay *gl_display = new (std::nothrow) EGLDisplay(); + if (gl_display == nullptr) { + MS_LOG(ERROR) << "new EGLDisplay failed"; + return RET_ERROR; + } else { + *gl_display = eglGetCurrentDisplay(); + } + gpu_device_ctx.device_info_.gpu_device_info_.gl_display_ = gl_display; } #endif context->device_list_.push_back(gpu_device_ctx); @@ -322,6 +340,8 @@ void Benchmark::InitContext(const std::shared_ptr &context) { context->thread_num_ = flags_->num_threads_; context->enable_parallel_ = flags_->enable_parallel_; + + return RET_OK; } int Benchmark::CompareOutput() { @@ -656,14 +676,19 @@ int Benchmark::RunBenchmark() { std::cerr << "New context failed while running " << model_name.c_str() << std::endl; return RET_ERROR; } - (void)InitContext(context); + auto ret = InitContext(context); + if (ret != RET_OK) { + MS_LOG(ERROR) << "InitContext failed while running ", model_name.c_str(); + std::cout << "InitContext failed while running ", model_name.c_str(); + return ret; + } session_ = session::LiteSession::CreateSession(context.get()); if (session_ == nullptr) { MS_LOG(ERROR) << "CreateSession failed while running ", model_name.c_str(); std::cout << "CreateSession failed while running ", model_name.c_str(); return RET_ERROR; } - auto ret = session_->CompileGraph(model.get()); + ret = session_->CompileGraph(model.get()); if (ret != RET_OK) { MS_LOG(ERROR) << "CompileGraph failed while running ", model_name.c_str(); std::cout << "CompileGraph failed while running ", model_name.c_str(); diff --git a/mindspore/lite/tools/benchmark/benchmark.h b/mindspore/lite/tools/benchmark/benchmark.h index 8843e3b27c5..5746ceb2c21 100644 --- a/mindspore/lite/tools/benchmark/benchmark.h +++ b/mindspore/lite/tools/benchmark/benchmark.h @@ -69,7 +69,7 @@ class MS_API Benchmark : public BenchmarkBase { int GetDataTypeByTensorName(const std::string &tensor_name) override; - void InitContext(const std::shared_ptr &context); + int InitContext(const std::shared_ptr &context); int CompareOutput() override; diff --git a/mindspore/lite/tools/benchmark/benchmark_unified_api.cc b/mindspore/lite/tools/benchmark/benchmark_unified_api.cc index a82a77c83c1..dd72b886d43 100644 --- a/mindspore/lite/tools/benchmark/benchmark_unified_api.cc +++ b/mindspore/lite/tools/benchmark/benchmark_unified_api.cc @@ -309,7 +309,7 @@ void BenchmarkUnifiedApi::UpdateDistributionName(const std::shared_ptr &context) { +int BenchmarkUnifiedApi::InitMSContext(const std::shared_ptr &context) { context->SetThreadNum(flags_->num_threads_); context->SetEnableParallel(flags_->enable_parallel_); context->SetThreadAffinity(flags_->cpu_bind_mode_); @@ -321,6 +321,24 @@ void BenchmarkUnifiedApi::InitMSContext(const std::shared_ptrSetEnableGLTexture(flags_->enable_gl_texture_); + + EGLContext *gl_context = new (std::nothrow) EGLContext(); + if (gl_context == nullptr) { + MS_LOG(ERROR) << "new EGLContext failed"; + return RET_ERROR; + } else { + *gl_context = eglGetCurrentContext(); + } + gpu_device_info->SetGLContext(gl_context); + + EGLDisplay *gl_display = new (std::nothrow) EGLDisplay(); + if (gl_display == nullptr) { + MS_LOG(ERROR) << "new EGLDisplay failed"; + return RET_ERROR; + } else { + *gl_display = eglGetCurrentDisplay(); + } + gpu_device_info->SetGLDisplay(gl_display); #endif device_list.push_back(gpu_device_info); @@ -342,6 +360,8 @@ void BenchmarkUnifiedApi::InitMSContext(const std::shared_ptr device_info = std::make_shared(); device_info->SetEnableFP16(flags_->enable_fp16_); device_list.push_back(device_info); + + return RET_OK; } int BenchmarkUnifiedApi::CompareOutput() { @@ -777,7 +797,12 @@ int BenchmarkUnifiedApi::RunBenchmark() { return RET_ERROR; } - (void)InitMSContext(context); + auto status = InitMSContext(context); + if (status != RET_OK) { + MS_LOG(ERROR) << "InitMSContext failed while running ", model_name.c_str(); + std::cout << "InitMSContext failed while running ", model_name.c_str(); + return RET_ERROR; + } (void)UpdateDistributionName(context, &flags_->model_file_); (void)UpdateDistributionName(context, &flags_->benchmark_data_file_); @@ -819,7 +844,7 @@ int BenchmarkUnifiedApi::RunBenchmark() { // Load input MS_LOG(INFO) << "start generate input data"; - auto status = LoadInput(); + status = LoadInput(); if (status != RET_OK) { MS_LOG(ERROR) << "Generate input data error"; return status; diff --git a/mindspore/lite/tools/benchmark/benchmark_unified_api.h b/mindspore/lite/tools/benchmark/benchmark_unified_api.h index 78ae5784959..731a5a4254a 100644 --- a/mindspore/lite/tools/benchmark/benchmark_unified_api.h +++ b/mindspore/lite/tools/benchmark/benchmark_unified_api.h @@ -74,7 +74,7 @@ class MS_API BenchmarkUnifiedApi : public BenchmarkBase { int ReadInputFile() override; - void InitMSContext(const std::shared_ptr &context); + int InitMSContext(const std::shared_ptr &context); int GetDataTypeByTensorName(const std::string &tensor_name) override;