!27772 [MSLITE][GPU] opengl use old benchmark bugfix and remove opengl Dependent library

Merge pull request !27772 from Greatpan/master_gl
This commit is contained in:
i-robot 2021-12-17 02:32:30 +00:00 committed by Gitee
commit ae6ac1a52e
14 changed files with 187 additions and 47 deletions

View File

@ -278,6 +278,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<char> &precision_mode);
std::vector<char> GetPrecisionModeChar() const;

View File

@ -35,6 +35,8 @@ typedef struct GpuDeviceInfo {
int rank_id_ = 0;
int group_size_ = 0;
bool enable_gl_texture_ = false; /**<enable sharing OpenGL texture with OpenCL */
void *gl_context_ = nullptr;
void *gl_display_ = nullptr;
} GpuDeviceInfo;
/// \brief NpuDeviceInfo defined for NPU's configuration information.

View File

@ -356,11 +356,6 @@ if(MSLITE_GPU_BACKEND STREQUAL opencl)
add_subdirectory(runtime/kernel/opencl)
target_link_libraries(mindspore-lite opencl_kernel_mid)
target_link_libraries(mindspore-lite_static opencl_kernel_mid)
if(MSLITE_ENABLE_SHARING_MEM_WITH_OPENGL)
list(APPEND opengl_lib GLESv3 EGL)
target_link_libraries(mindspore-lite ${opengl_lib})
target_link_libraries(mindspore-lite_static ${opengl_lib})
endif()
elseif(MSLITE_GPU_BACKEND STREQUAL cuda)
add_subdirectory(runtime/kernel/cuda)
target_link_libraries(mindspore-lite cuda_kernel_mid)

View File

@ -26,7 +26,9 @@ namespace mindspore {
constexpr auto kModelOptionCpuEnableFP16 = "mindspore.option.cpu.enable_fp16";
constexpr auto kModelOptionGPUEnableFP16 = "mindspore.option.gpu.enable_fp16";
#ifdef ENABLE_OPENGL_TEXTURE
constexpr auto kModelOptionGPUEnableEnableGLTexture = "mindspore.option.gpu.enable_gl_texture_";
constexpr auto kModelOptionGPUEnableGLTexture = "mindspore.option.gpu.enable_gl_texture_";
constexpr auto kModelOptionGPUGLContext = "mindspore.option.gpu.gl_context_";
constexpr auto kModelOptionGPUGLDisplay = "mindspore.option.gpu.gl_display_";
#endif
constexpr auto kModelOptionGPUDeviceID = "mindspore.option.gpu.device_id";
constexpr auto kModelOptionGPURankID = "mindspore.option.gpu.rank_id";
@ -238,16 +240,6 @@ void GPUDeviceInfo::SetEnableFP16(bool is_fp16) {
data_->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<bool>(data_, kModelOptionGPUEnableEnableGLTexture);
return GetValue<bool>(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<void *>(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<void *>(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

View File

@ -43,11 +43,12 @@ Status ContextUtils::AddCpuDevice(const std::shared_ptr<Allocator> &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> &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> &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<GPUDeviceInfo>();
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<KirinNPUDeviceInfo>();
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());

View File

@ -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> &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> &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) {

View File

@ -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";

View File

@ -176,16 +176,18 @@ int OpenCLRuntime::InitQueue(std::vector<cl::Platform> *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<cl::Device>{*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,

View File

@ -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<EGLContext *>(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<EGLDisplay *>(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

View File

@ -144,6 +144,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:

View File

@ -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> &context) {
int Benchmark::InitContext(const std::shared_ptr<Context> &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> &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) {
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();

View File

@ -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> &context);
int InitContext(const std::shared_ptr<Context> &context);
int CompareOutput() override;

View File

@ -325,7 +325,7 @@ void BenchmarkUnifiedApi::UpdateDistributionName(const std::shared_ptr<mindspore
return;
}
void BenchmarkUnifiedApi::InitMSContext(const std::shared_ptr<mindspore::Context> &context) {
int BenchmarkUnifiedApi::InitMSContext(const std::shared_ptr<mindspore::Context> &context) {
context->SetThreadNum(flags_->num_threads_);
context->SetEnableParallel(flags_->enable_parallel_);
context->SetThreadAffinity(flags_->cpu_bind_mode_);
@ -337,6 +337,24 @@ void BenchmarkUnifiedApi::InitMSContext(const std::shared_ptr<mindspore::Context
#ifdef ENABLE_OPENGL_TEXTURE
gpu_device_info->SetEnableGLTexture(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);
@ -358,6 +376,8 @@ void BenchmarkUnifiedApi::InitMSContext(const std::shared_ptr<mindspore::Context
std::shared_ptr<CPUDeviceInfo> device_info = std::make_shared<CPUDeviceInfo>();
device_info->SetEnableFP16(flags_->enable_fp16_);
device_list.push_back(device_info);
return RET_OK;
}
int BenchmarkUnifiedApi::CompareOutput() {
@ -793,7 +813,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_);
@ -835,7 +860,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;

View File

@ -74,7 +74,7 @@ class MS_API BenchmarkUnifiedApi : public BenchmarkBase {
int ReadInputFile() override;
void InitMSContext(const std::shared_ptr<Context> &context);
int InitMSContext(const std::shared_ptr<Context> &context);
int GetDataTypeByTensorName(const std::string &tensor_name) override;