From 592d786f3077ee97fbe4bdc67b521c02100a1bf9 Mon Sep 17 00:00:00 2001 From: "A. Unique TensorFlower" Date: Fri, 20 Sep 2019 11:29:28 -0700 Subject: [PATCH] Switch TfLite benchmark from gl_delegate to delegate that combines OpenCL and OpenGL. PiperOrigin-RevId: 270314075 --- tensorflow/lite/tools/benchmark/BUILD | 2 +- .../benchmark_performance_options.cc | 24 +------------ .../tools/benchmark/benchmark_tflite_model.cc | 34 +++---------------- tensorflow/lite/tools/evaluation/BUILD | 2 +- tensorflow/lite/tools/evaluation/utils.cc | 17 ++++------ tensorflow/lite/tools/evaluation/utils.h | 4 +-- 6 files changed, 17 insertions(+), 66 deletions(-) diff --git a/tensorflow/lite/tools/benchmark/BUILD b/tensorflow/lite/tools/benchmark/BUILD index d15782577ca..bd19284e514 100644 --- a/tensorflow/lite/tools/benchmark/BUILD +++ b/tensorflow/lite/tools/benchmark/BUILD @@ -138,7 +138,7 @@ cc_library( "//tensorflow/lite/tools:command_line_flags", ] + select({ "//tensorflow:android": [ - "//tensorflow/lite/delegates/gpu:gl_delegate", + "//tensorflow/lite/delegates/gpu:delegate", ], "//conditions:default": [], }), diff --git a/tensorflow/lite/tools/benchmark/benchmark_performance_options.cc b/tensorflow/lite/tools/benchmark/benchmark_performance_options.cc index 57c5d8ca2e2..bdceeb3ac62 100644 --- a/tensorflow/lite/tools/benchmark/benchmark_performance_options.cc +++ b/tensorflow/lite/tools/benchmark/benchmark_performance_options.cc @@ -48,29 +48,7 @@ void MultiRunStatsRecorder::OnBenchmarkStart(const BenchmarkParams& params) { const bool allow_precision_loss = params.Get("gpu_precision_loss_allowed"); const std::string precision_tag = allow_precision_loss ? "fp16" : "fp32"; - - const int32_t gl_obj_type = params.Get("gpu_gl_object_type"); - std::string gl_type; - switch (gl_obj_type) { - case TFLITE_GL_OBJECT_TYPE_FASTEST: - gl_type = "fastest"; - break; - case TFLITE_GL_OBJECT_TYPE_TEXTURE: - gl_type = "texture"; - break; - case TFLITE_GL_OBJECT_TYPE_BUFFER: - gl_type = "buffer"; - break; - default: - gl_type = "unknown"; - break; - } - - if (allow_precision_loss && gl_obj_type == TFLITE_GL_OBJECT_TYPE_FASTEST) { - current_run_name_ = "gpu(fp16, fastest)-default"; - return; - } - current_run_name_ = "gpu(" + precision_tag + ", " + gl_type + ")"; + current_run_name_ = "gpu(" + precision_tag + ")"; #else current_run_name_ = "gpu(fp16, fastest)-default"; #endif diff --git a/tensorflow/lite/tools/benchmark/benchmark_tflite_model.cc b/tensorflow/lite/tools/benchmark/benchmark_tflite_model.cc index ccd8db95484..0f2ea24fb8a 100644 --- a/tensorflow/lite/tools/benchmark/benchmark_tflite_model.cc +++ b/tensorflow/lite/tools/benchmark/benchmark_tflite_model.cc @@ -24,7 +24,7 @@ limitations under the License. #include #if defined(__ANDROID__) -#include "tensorflow/lite/delegates/gpu/gl_delegate.h" +#include "tensorflow/lite/delegates/gpu/delegate.h" #include "tensorflow/lite/nnapi/nnapi_util.h" #endif @@ -218,9 +218,6 @@ BenchmarkParams BenchmarkTfLiteModel::DefaultParams() { #if defined(__ANDROID__) default_params.AddParam("gpu_precision_loss_allowed", BenchmarkParam::Create(true)); - default_params.AddParam( - "gpu_gl_object_type", - BenchmarkParam::Create(TFLITE_GL_OBJECT_TYPE_FASTEST)); #endif default_params.AddParam("allow_fp16", BenchmarkParam::Create(false)); default_params.AddParam("require_full_delegation", @@ -588,37 +585,16 @@ TfLiteStatus BenchmarkTfLiteModel::Init() { return kTfLiteOk; } -#if defined(__ANDROID__) -bool IsValidGLObjectTypeInGPU(int32_t type) { - if (type < TFLITE_GL_OBJECT_TYPE_FASTEST || - type > TFLITE_GL_OBJECT_TYPE_BUFFER) { - TFLITE_LOG(WARN) << "The specified GL object type in GPU is invalid: " - << type; - return false; - } - return true; -} -#endif - BenchmarkTfLiteModel::TfLiteDelegatePtrMap BenchmarkTfLiteModel::GetDelegates() const { TfLiteDelegatePtrMap delegates; if (params_.Get("use_gpu")) { #if defined(__ANDROID__) - TfLiteGpuDelegateOptions gpu_opts = TfLiteGpuDelegateOptionsDefault(); - gpu_opts.metadata = - model_ ? TfLiteGpuDelegateGetModelMetadata(model_->GetModel()) - : nullptr; - gpu_opts.compile_options.precision_loss_allowed = + TfLiteGpuDelegateOptionsV2 gpu_opts = TfLiteGpuDelegateOptionsV2Default(); + gpu_opts.inference_preference = + TFLITE_GPU_INFERENCE_PREFERENCE_SUSTAINED_SPEED; + gpu_opts.is_precision_loss_allowed = params_.Get("gpu_precision_loss_allowed") ? 1 : 0; - int32_t gl_obj_type = params_.Get("gpu_gl_object_type"); - // We overwrite the gl object type to the recommended value if the specified - // isn't valid. - if (!IsValidGLObjectTypeInGPU(gl_obj_type)) { - gl_obj_type = TFLITE_GL_OBJECT_TYPE_FASTEST; - } - gpu_opts.compile_options.preferred_gl_object_type = gl_obj_type; - gpu_opts.compile_options.dynamic_batch_enabled = 0; Interpreter::TfLiteDelegatePtr delegate = evaluation::CreateGPUDelegate(model_.get(), &gpu_opts); #else diff --git a/tensorflow/lite/tools/evaluation/BUILD b/tensorflow/lite/tools/evaluation/BUILD index fe8c7117e09..402a8afeeaa 100644 --- a/tensorflow/lite/tools/evaluation/BUILD +++ b/tensorflow/lite/tools/evaluation/BUILD @@ -45,7 +45,7 @@ cc_library( "//tensorflow/lite/delegates/nnapi:nnapi_delegate", ] + select({ "//tensorflow:android": [ - "//tensorflow/lite/delegates/gpu:gl_delegate", + "//tensorflow/lite/delegates/gpu:delegate", ], "//conditions:default": [], }), diff --git a/tensorflow/lite/tools/evaluation/utils.cc b/tensorflow/lite/tools/evaluation/utils.cc index 0de9212cf8c..d670059c0d1 100644 --- a/tensorflow/lite/tools/evaluation/utils.cc +++ b/tensorflow/lite/tools/evaluation/utils.cc @@ -99,22 +99,19 @@ Interpreter::TfLiteDelegatePtr CreateNNAPIDelegate( #if defined(__ANDROID__) Interpreter::TfLiteDelegatePtr CreateGPUDelegate( - tflite::FlatBufferModel* model, TfLiteGpuDelegateOptions* options) { - return Interpreter::TfLiteDelegatePtr(TfLiteGpuDelegateCreate(options), - &TfLiteGpuDelegateDelete); + tflite::FlatBufferModel* model, TfLiteGpuDelegateOptionsV2* options) { + return Interpreter::TfLiteDelegatePtr(TfLiteGpuDelegateV2Create(options), + &TfLiteGpuDelegateV2Delete); } #endif // defined(__ANDROID__) Interpreter::TfLiteDelegatePtr CreateGPUDelegate( tflite::FlatBufferModel* model) { #if defined(__ANDROID__) - TfLiteGpuDelegateOptions options = TfLiteGpuDelegateOptionsDefault(); - options.metadata = - model ? TfLiteGpuDelegateGetModelMetadata(model->GetModel()) : nullptr; - options.compile_options.precision_loss_allowed = 1; - options.compile_options.preferred_gl_object_type = - TFLITE_GL_OBJECT_TYPE_FASTEST; - options.compile_options.dynamic_batch_enabled = 0; + TfLiteGpuDelegateOptionsV2 options = TfLiteGpuDelegateOptionsV2Default(); + options.is_precision_loss_allowed = 1; + options.inference_preference = + TFLITE_GPU_INFERENCE_PREFERENCE_SUSTAINED_SPEED; return CreateGPUDelegate(model, &options); #else diff --git a/tensorflow/lite/tools/evaluation/utils.h b/tensorflow/lite/tools/evaluation/utils.h index 877a6493fbb..9c80df8e788 100644 --- a/tensorflow/lite/tools/evaluation/utils.h +++ b/tensorflow/lite/tools/evaluation/utils.h @@ -20,7 +20,7 @@ limitations under the License. #include #if defined(__ANDROID__) -#include "tensorflow/lite/delegates/gpu/gl_delegate.h" +#include "tensorflow/lite/delegates/gpu/delegate.h" #endif #include "tensorflow/lite/context.h" @@ -45,7 +45,7 @@ Interpreter::TfLiteDelegatePtr CreateNNAPIDelegate( Interpreter::TfLiteDelegatePtr CreateGPUDelegate(FlatBufferModel* model); #if defined(__ANDROID__) Interpreter::TfLiteDelegatePtr CreateGPUDelegate( - FlatBufferModel* model, TfLiteGpuDelegateOptions* options); + FlatBufferModel* model, TfLiteGpuDelegateOptionsV2* options); #endif } // namespace evaluation