tflite: mtk: mtk neuron stable delegate support QoS setting

add deadline and abort_time for QoS setting.

BUG=b:341603492
TEST=stable_delegate_test_suite

Change-Id: Id636a4a56311abc93f2bf958dc4088b3bc3f9a09
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/tflite/+/5828691
Commit-Queue: Tommy Chiang <ototot@google.com>
Tested-by: Pochun Lin <pochun.lin@mediatek.corp-partner.google.com>
Reviewed-by: Tommy Chiang <ototot@google.com>
Reviewed-by: Pochun Lin <pochun.lin@mediatek.corp-partner.google.com>
diff --git a/delegate/mtk_neuron/neuron_delegate.cc b/delegate/mtk_neuron/neuron_delegate.cc
index 18047e2..1153a88 100644
--- a/delegate/mtk_neuron/neuron_delegate.cc
+++ b/delegate/mtk_neuron/neuron_delegate.cc
@@ -60,6 +60,8 @@
       .compile_options = "",
       .accelerator_name = "",
       .operation_check_mode = kNoOperationCheck,
+      .inference_deadline = 0,
+      .inference_abort_time = 0,
   };
 
   // Return default options
diff --git a/delegate/mtk_neuron/neuron_delegate_external.cc b/delegate/mtk_neuron/neuron_delegate_external.cc
index 7b51fea..cd6ea03 100644
--- a/delegate/mtk_neuron/neuron_delegate_external.cc
+++ b/delegate/mtk_neuron/neuron_delegate_external.cc
@@ -157,6 +157,11 @@
       options.operation_check_mode =
           ConvertOperationCheckMode(neuron_settings->operation_check_mode());
     }
+
+    options.inference_deadline =
+        static_cast<uint16_t>(neuron_settings->inference_deadline_ms());
+    options.inference_abort_time =
+        static_cast<uint16_t>(neuron_settings->inference_abort_time_ms());
     options.allow_fp16 = neuron_settings->allow_fp16_precision_for_fp32();
     options.use_ahwb = neuron_settings->use_ahwb();
     options.use_cacheable_buffer = neuron_settings->use_cacheable_buffer();
diff --git a/delegate/mtk_neuron/neuron_delegate_kernel.cc b/delegate/mtk_neuron/neuron_delegate_kernel.cc
index 1d89652..5279803 100644
--- a/delegate/mtk_neuron/neuron_delegate_kernel.cc
+++ b/delegate/mtk_neuron/neuron_delegate_kernel.cc
@@ -1598,6 +1598,24 @@
   }
 #endif
 
+  if (options_.inference_deadline > 0 &&
+      neuronapi_->NeuronExecution_setDeadline != nullptr) {
+    RETURN_TFLITE_ERROR_IF_NEURON_ERROR(
+      context,
+      neuronapi_->NeuronExecution_setDeadline(execution,
+                                              options_.inference_deadline),
+      "set deadline");
+  }
+
+  if (options_.inference_abort_time > 0 &&
+      neuronapi_->NeuronExecution_setAbortTime != nullptr) {
+    RETURN_TFLITE_ERROR_IF_NEURON_ERROR(
+      context,
+      neuronapi_->NeuronExecution_setAbortTime(execution,
+                                               options_.inference_abort_time),
+      "set abort time");
+  }
+
   RETURN_TFLITE_ERROR_IF_NEURON_ERROR(
       context, neuronapi_->NeuronExecution_compute(execution),
       "running computation");
diff --git a/delegate/mtk_neuron/neuron_delegate_options.h b/delegate/mtk_neuron/neuron_delegate_options.h
index 7f138d7..b900604 100644
--- a/delegate/mtk_neuron/neuron_delegate_options.h
+++ b/delegate/mtk_neuron/neuron_delegate_options.h
@@ -87,6 +87,11 @@
   std::string accelerator_name;
   // Whether to use supported operation check or not.
   NeuronOperationCheckMode operation_check_mode;
+  // Execution deadline for the inference (in msec)
+  uint16_t inference_deadline;
+  // Execution abort time is the hint for the the maximum inference time for
+  // the execution (in msec).
+  uint16_t inference_abort_time;
   // Optional path to the platform-dependent Neuron configuration file.
   // Unused on ChromeOS.
   std::string neuron_config_path;
diff --git a/delegate/mtk_neuron/neuron_implementation.cc b/delegate/mtk_neuron/neuron_implementation.cc
index 7c55c82..006db1d 100644
--- a/delegate/mtk_neuron/neuron_implementation.cc
+++ b/delegate/mtk_neuron/neuron_implementation.cc
@@ -170,6 +170,10 @@
   LOAD_FUNCTION(libneuron_adapter, NeuronExecution_setLoopTimeout, neuron_api);
   LOAD_FUNCTION_OPTIONAL(libneuron_adapter, NeuronExecution_setCacheFlushHint,
                          neuron_api);
+  LOAD_FUNCTION_OPTIONAL(libneuron_adapter, NeuronExecution_setDeadline,
+                         neuron_api);
+  LOAD_FUNCTION_OPTIONAL(libneuron_adapter, NeuronExecution_setAbortTime,
+                         neuron_api);
   LOAD_FUNCTION(libneuron_adapter, NeuronCompilation_setOptimizationString,
                 neuron_api);
   LOAD_FUNCTION(libneuron_adapter, Neuron_getDeviceCount, neuron_api);
diff --git a/delegate/mtk_neuron/neuron_implementation.h b/delegate/mtk_neuron/neuron_implementation.h
index 87bf299..45ea155 100644
--- a/delegate/mtk_neuron/neuron_implementation.h
+++ b/delegate/mtk_neuron/neuron_implementation.h
@@ -245,6 +245,12 @@
   int (*NeuronExecution_setCacheFlushHint)(NeuronExecution* execution,
                                            uint8_t flushHint);
 
+  int (*NeuronExecution_setDeadline)(NeuronExecution* execution,
+                                     uint16_t deadline);
+
+  int (*NeuronExecution_setAbortTime)(NeuronExecution* execution,
+                                      uint16_t abortTime);
+
   int (*NeuronCompilation_setOptimizationString)(
       NeuronCompilation* compilation, const char* optimizationString);
 
diff --git a/delegate/mtk_neuron/neuron_types.h b/delegate/mtk_neuron/neuron_types.h
index 8e7bb29..ce93ed6 100644
--- a/delegate/mtk_neuron/neuron_types.h
+++ b/delegate/mtk_neuron/neuron_types.h
@@ -530,6 +530,12 @@
 typedef int (*NeuronExecution_setCacheFlushHint_fn)(NeuronExecution* execution,
                                                     uint8_t flushHint);
 
+typedef int (*NeuronExecution_setDeadline_fn)(NeuronExecution* execution,
+    uint16_t deadline);
+
+typedef int (*NeuronExecution_setAbortTime_fn)(NeuronExecution* execution,
+    uint16_t abortTime);
+
 typedef int (*NeuronCompilation_setOptimizationString_fn)(
     NeuronCompilation* compilation, const char* optimizationString);
 
diff --git a/delegate/mtk_neuron/neuron_version.h b/delegate/mtk_neuron/neuron_version.h
index b9fb761..1318051 100644
--- a/delegate/mtk_neuron/neuron_version.h
+++ b/delegate/mtk_neuron/neuron_version.h
@@ -25,7 +25,7 @@
 
 #define NEURON_DELEGATE_MAJOR_VERSION 1
 #define NEURON_DELEGATE_MINOR_VERSION 4
-#define NEURON_DELEGATE_PATCH_VERSION 2
+#define NEURON_DELEGATE_PATCH_VERSION 3
 
 #define NEURON_DELEGATE_STR_HELPER(x) #x
 #define NEURON_DELEGATE_STR(x) NEURON_DELEGATE_STR_HELPER(x)