Enable Burst mode by default, when the following requirements are satisfied:
 - User selected a list of NNAPI accelerators.
 - The max device feature level of the selected accelerators is NNAPI feature level 5 or higher.

PiperOrigin-RevId: 374440900
Change-Id: I6aaa3bdbf24e0c77e8287477be35caf831801ab5
diff --git a/tensorflow/lite/delegates/nnapi/nnapi_delegate.cc b/tensorflow/lite/delegates/nnapi/nnapi_delegate.cc
index b57bb69..873aa53 100644
--- a/tensorflow/lite/delegates/nnapi/nnapi_delegate.cc
+++ b/tensorflow/lite/delegates/nnapi/nnapi_delegate.cc
@@ -3801,8 +3801,15 @@
                                   "completing NNAPI compilation", nnapi_errno);
   nn_compilation_.reset(compilation);
 
+  bool should_use_burst_mode = delegate_options.use_burst_computation;
+  // Override should_use_burst_mode to true if the selected NNAPI devices are of
+  // NNAPI feature level 5 or higher.
+  if (!nnapi_devices_.empty() &&
+      target_sdk_version_ >= kNNAPIRuntimeFeatureLevel5) {
+    should_use_burst_mode = true;
+  }
   // Create burst object to be reused across a sequence of executions
-  if (delegate_options.use_burst_computation &&
+  if (should_use_burst_mode &&
       nnapi_->android_sdk_version >= kMinSdkVersionForNNAPI12 &&
       nnapi_->ANeuralNetworksBurst_create) {
     ANeuralNetworksBurst* burst = nullptr;
diff --git a/tensorflow/lite/delegates/nnapi/nnapi_delegate.h b/tensorflow/lite/delegates/nnapi/nnapi_delegate.h
index 73d8819..fe899a6 100644
--- a/tensorflow/lite/delegates/nnapi/nnapi_delegate.h
+++ b/tensorflow/lite/delegates/nnapi/nnapi_delegate.h
@@ -126,11 +126,14 @@
     // dynamic dimensions of the model.
     bool allow_dynamic_dimensions = false;
 
-    // Use NNAPI Burst mode if supported.
+    // Force using NNAPI Burst mode if supported.
     // Burst mode allows accelerators to efficiently manage resources, which
     // would significantly reduce overhead especially if the same delegate
     // instance is to be used for multiple inferences.
-    // Default: Disabled.
+    // If NNAPI devices are specified and are of NNAPI feature level 5 or
+    // higher, NNAPI delegate will automatically enable burst mode for better
+    // performance.
+    // Default: Disabled for devices with NNAPI feature level 4 or lower.
     bool use_burst_computation = false;
   };