Add Graph debug mode support
authorInki Dae <inki.dae@samsung.com>
Thu, 7 Jan 2021 05:22:40 +0000 (14:22 +0900)
committerInki Dae <inki.dae@samsung.com>
Tue, 2 Feb 2021 06:50:33 +0000 (15:50 +0900)
Added Graph debug mode support, which adds debug layer to all layers
of a given model in runtime so that we can check the computation result
to each layer while in inference.

To invoke graph debug mode, set ARMNN_GRAPH_DEBUG flag to 1 in
spec file.

Change-Id: Ia9cd25728903784ef25fd09c637c34a5cd5a7de4
Signed-off-by: Inki Dae <inki.dae@samsung.com>
CMakeLists.txt
packaging/inference-engine-armnn.spec
src/inference_engine_armnn.cpp

index 9ce42a5391f6d633919d938ced73457d32315834..56f697b23ea4c7c3b61debd73e3a47de16e5f532 100644 (file)
@@ -36,6 +36,11 @@ SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -g")
 ADD_DEFINITIONS("-DPREFIX=\"${CMAKE_INSTALL_PREFIX}\"")
 ADD_DEFINITIONS("-DTIZEN_DEBUG")
 
+IF (${ARMNN_GRAPH_DEBUG})
+    MESSAGE("GRAPH DEBUG MODE enabled.")
+    ADD_DEFINITIONS(-DGRAPH_DEBUG)
+ENDIF()
+
 SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -Wl,--rpath=${LIB_INSTALL_DIR}")
 
 aux_source_directory(src SOURCES)
index 228506e1b127ec436380e9714a34917a844b7ea1..7b1adc50cb01535a32cbba8778488cc6d6864624 100644 (file)
@@ -14,6 +14,8 @@ BuildRequires: pkgconfig(dlog)
 BuildRequires: pkgconfig(inference-engine-interface-common)
 BuildRequires: pkgconfig(armnn)
 
+%define build_options -DARMNN_GRAPH_DEBUG=0
+
 %description
 ARM Neural Network Runtime based implementation of inference-engine-interface
 
@@ -27,7 +29,7 @@ export CXXFLAGS="$CXXFLAGS -DTIZEN_DEBUG_ENABLE"
 export FFLAGS="$FFLAGS -DTIZEN_DEBUG_ENABLE"
 %endif
 
-%cmake .
+%cmake %{build_options} .
 
 make %{?jobs:-j%jobs}
 
index 76e603e01b2d471d0c883c8b5725591a8e0c01b3..9f397f58ca3cc48ca8f4c7e3043e22d2e24dc685 100644 (file)
@@ -24,6 +24,7 @@
 #include <queue>
 
 #include <armnn/ArmNN.hpp>
+#include <armnn/backends/ITensorHandle.hpp>
 #include <armnnTfLiteParser/ITfLiteParser.hpp>
 
 namespace InferenceEngineImpl
@@ -229,6 +230,15 @@ namespace ARMNNImpl
                return ret;
        }
 
+       static const void GraphDebugCallback(armnn::LayerGuid guid, unsigned int slotIndex, armnn::ITensorHandle* tensor)
+       {
+               std::cout << "input tensor shape : " << tensor->GetShape() << ", ";
+               std::cout << "layerGuid : " << guid << ", ";
+               std::cout << "outputSlot : " << slotIndex << std::endl;
+
+               // TODO.
+       }
+
        int InferenceARMNN::Load(std::vector<std::string> model_paths,
                                                         inference_model_format_e model_format)
        {
@@ -252,9 +262,18 @@ namespace ARMNNImpl
                // In default, add CpuRef as fallback.
                mAccelType.push_back(armnn::Compute::CpuRef);
 
+               bool graph_debug = false;
+
+#if GRAPH_DEBUG
+               graph_debug = true;
+#endif
+               // First parameter is reduceFp32ToFp16, and second one is debug mode.
+               // In default, reduceFp32ToFp16 is false.
+               armnn::OptimizerOptions optimizerOptions(false, graph_debug);
+
                // Optimize the network for a specific runtime compute device, e.g. CpuAcc, GpuAcc
                armnn::IOptimizedNetworkPtr optimizedNet = armnn::Optimize(
-                               *mNetwork, mAccelType, sRuntime->GetDeviceSpec());
+                               *mNetwork, mAccelType, sRuntime->GetDeviceSpec(), optimizerOptions);
                if (!optimizedNet) {
                        LOGE("Fail to optimize network.");
                        return INFERENCE_ENGINE_ERROR_INVALID_OPERATION;
@@ -270,6 +289,14 @@ namespace ARMNNImpl
 
                LOGI("Loaded the Network.");
 
+#if GRAPH_DEBUG
+               // Add debug layer to all layers of a given model, and call user given
+               // callback to debug each layer, GraphDebugCallback, while in inference.
+               // Ps. if you want to use default callback then set NULL instead of
+               // user given callback.
+               sRuntime->RegisterDebugCallback(mNetworkIdentifier, GraphDebugCallback);
+#endif
+
                LOGI("LEAVE");
 
                return ret;