add XNNPACK acceleration support sandbox/inki.dae/xnnpack_support
authorInki Dae <inki.dae@samsung.com>
Thu, 29 Dec 2022 03:47:40 +0000 (12:47 +0900)
committerInki Dae <inki.dae@samsung.com>
Wed, 1 Feb 2023 06:39:53 +0000 (15:39 +0900)
Change-Id: I4c635c7cf9c3e3a4715e46f81c114a4458aa6d6f
Signed-off-by: Inki Dae <inki.dae@samsung.com>
CMakeLists.txt
packaging/inference-engine-tflite.spec
src/inference_engine_tflite.cpp
src/inference_engine_tflite_private.h

index 8c520ef98d3512dc1c71d30066f87ede21ffbeb0..4236f654c21a55eea858ba6230b06c760048fe8b 100644 (file)
@@ -39,7 +39,6 @@ ADD_LIBRARY(${fw_name} SHARED ${SOURCES})
 
 TARGET_LINK_LIBRARIES(${fw_name} ${EXTRA_LDFLAGS})
 
-
 SET_TARGET_PROPERTIES(${fw_name}
      PROPERTIES
      CLEAN_DIRECT_OUTPUT 1
@@ -53,6 +52,11 @@ INSTALL(
         PATTERN "*.h"
         )
 
+IF (${USE_XNNPACK})
+    message("Use XNNPACK acceleration.")
+       ADD_DEFINITIONS(-DUSE_XNNPACK)
+ENDIF()
+
 SET(PC_NAME ${fw_name})
 SET(PC_REQUIRED ${pc_dependents})
 SET(PC_LDFLAGS -l${fw_name})
index 566eae8062a31f4dd01d0f722e8f18b403be5887..478bedf60101e9fadd6f2a3d84923cb4a62ba285 100644 (file)
@@ -14,11 +14,12 @@ BuildRequires: pkgconfig(inference-engine-interface-common)
 BuildRequires: coregl-devel
 BuildRequires: flatbuffers-devel
 BuildRequires: tensorflow2-lite-devel
+# if you want to use XNNPACK acceleration then set USE_XNNPACK to 1.
+%define build_options -DUSE_XNNPACK=1
 
 %description
 Tensorflow-Lite based implementation of inference-engine-interface
 
-
 %prep
 %setup -q
 
@@ -29,7 +30,7 @@ export CXXFLAGS="$CXXFLAGS -DTIZEN_DEBUG_ENABLE"
 export FFLAGS="$FFLAGS -DTIZEN_DEBUG_ENABLE"
 %endif
 
-%cmake .
+%cmake %{build_options} .
 
 make %{?jobs:-j%jobs}
 
index 183fc3e833da14b68ac5d4eea572dbf24434d020..4351462164c7021fb3b912be244788d62e4ed280 100644 (file)
@@ -34,6 +34,11 @@ namespace TFLiteImpl
        InferenceTFLite::InferenceTFLite(void) : mTargetTypes(INFERENCE_TARGET_NONE)
        {
                LOGI("ENTER");
+
+#if USE_XNNPACK
+               _use_xnnpack = true;
+#endif
+
                LOGI("LEAVE");
        }
 
@@ -116,48 +121,45 @@ namespace TFLiteImpl
 
                LOGI("Inferece targets are: [%d]", mTargetTypes);
 
+               TfLiteDelegate *delegate = NULL;
+
                if (mTargetTypes == INFERENCE_TARGET_GPU) {
                        TfLiteGpuDelegateOptionsV2 options = TfLiteGpuDelegateOptionsV2Default();
-                       TfLiteDelegate *delegate = TfLiteGpuDelegateV2Create(&options);
+
+                       delegate = TfLiteGpuDelegateV2Create(&options);
                        if (!delegate){
                                LOGE("Failed to GPU delegate");
                                return INFERENCE_ENGINE_ERROR_INVALID_OPERATION;
                        }
-
-                       if (mInterpreter->ModifyGraphWithDelegate(delegate) != kTfLiteOk)
-                       {
-                               LOGE("Failed to construct GPU delegate");
-                               return INFERENCE_ENGINE_ERROR_INVALID_OPERATION;
-                       }
-               } else {
+               } else if (_use_xnnpack) {
                        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)
-                       {
+                       if (env_tflite_num_threads) {
                                num_threads = atoi(env_tflite_num_threads);
-                               LOGI("@@@@@@ FORCE_TFLITE_NUM_THREADS(XNNPACK)=%d", num_threads);
+                               LOGI("FORCE_TFLITE_NUM_THREADS for NNPACK = %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);
+                       delegate = TfLiteXNNPackDelegateCreate(&xnnpack_options);
                        if (!delegate) {
-                               LOGE("ERR: %s(%d)", __FILE__, __LINE__);
-                       }
-
-                       if (mInterpreter->ModifyGraphWithDelegate(delegate) != kTfLiteOk) {
-                               LOGE("Failed to construct GPU delegate");
+                               LOGE("Fail to create XNNPACK Delegate.");
                                return INFERENCE_ENGINE_ERROR_INVALID_OPERATION;
                        }
                }
 
+               if (mInterpreter->ModifyGraphWithDelegate(delegate) != kTfLiteOk) {
+                       LOGE("Failed to construct a delegate");
+                       return INFERENCE_ENGINE_ERROR_INVALID_OPERATION;
+               }
+
                mInterpreter->SetNumThreads(MV_INFERENCE_TFLITE_MAX_THREAD_NUM);
                LOGI("mInterpreter->tensors_size() :[%zu]",
                         mInterpreter->tensors_size());
index 250f192437d7b60c353f4c57de5298c1a21f7a25..1defb318d98dd755c938c7d58ce888afccd3ea70 100644 (file)
@@ -105,6 +105,7 @@ namespace TFLiteImpl
                std::string mConfigFile;
                std::string mWeightFile;
                int mTargetTypes;
+               bool _use_xnnpack;
        };
 
 } /* InferenceEngineImpl */