add xnnpack support
authorInki Dae <inki.dae@samsung.com>
Wed, 14 Dec 2022 06:37:00 +0000 (15:37 +0900)
committerInki Dae <inki.dae@samsung.com>
Wed, 14 Dec 2022 06:37:00 +0000 (15:37 +0900)
Change-Id: I8684364b60049befc115a1ae52292c1aa6a875c3
Signed-off-by: Inki Dae <inki.dae@samsung.com>
src/inference_engine_tflite.cpp
src/inference_engine_tflite_private.h

index 8f22bd996a7272eff890f2e33c4240a6558cb583..183fc3e833da14b68ac5d4eea572dbf24434d020 100644 (file)
@@ -22,6 +22,7 @@
 #include <unistd.h>
 #include <time.h>
 #include <queue>
+#include <thread>
 
 // H/W
 #define MV_INFERENCE_TFLITE_MAX_THREAD_NUM -1
@@ -128,6 +129,33 @@ namespace TFLiteImpl
                                LOGE("Failed to construct GPU delegate");
                                return INFERENCE_ENGINE_ERROR_INVALID_OPERATION;
                        }
+               } else {
+                       LOGI("Use XNNPACK.");
+
+                       TfLiteDelegate *delegate = NULL;
+                       int num_threads = std::thread::hardware_concurrency();
+                       char *env_tflite_num_threads = getenv("FORCE_TFLITE_NUM_THREADS");
+                       if (env_tflite_num_threads)
+                       {
+                               num_threads = atoi(env_tflite_num_threads);
+                               LOGI("@@@@@@ FORCE_TFLITE_NUM_THREADS(XNNPACK)=%d", num_threads);
+                       }
+
+                       // IMPORTANT: initialize options with TfLiteXNNPackDelegateOptionsDefault() for
+                       // API-compatibility with future extensions of the TfLiteXNNPackDelegateOptions
+                       // structure.
+                       TfLiteXNNPackDelegateOptions xnnpack_options = TfLiteXNNPackDelegateOptionsDefault();
+                       xnnpack_options.num_threads = num_threads;
+
+                       delegate = TfLiteXNNPackDelegateCreate (&xnnpack_options);
+                       if (!delegate) {
+                               LOGE("ERR: %s(%d)", __FILE__, __LINE__);
+                       }
+
+                       if (mInterpreter->ModifyGraphWithDelegate(delegate) != kTfLiteOk) {
+                               LOGE("Failed to construct GPU delegate");
+                               return INFERENCE_ENGINE_ERROR_INVALID_OPERATION;
+                       }
                }
 
                mInterpreter->SetNumThreads(MV_INFERENCE_TFLITE_MAX_THREAD_NUM);
index d4915007a49a0cda4a869da8378a5e57930617a4..250f192437d7b60c353f4c57de5298c1a21f7a25 100644 (file)
@@ -20,6 +20,7 @@
 #include <inference_engine_common.h>
 
 #include "tensorflow2/lite/delegates/gpu/delegate.h"
+#include "tensorflow2/lite/delegates/xnnpack/xnnpack_delegate.h"
 #include "tensorflow2/lite/kernels/register.h"
 #include "tensorflow2/lite/model.h"
 #include "tensorflow2/lite/optional_debug_tools.h"