[newrt] Let the user select backend per operation (#1929)
author이한종/동작제어Lab(SR)/Engineer/삼성전자 <hanjoung.lee@samsung.com>
Wed, 11 Jul 2018 09:24:25 +0000 (18:24 +0900)
committer오형석/동작제어Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Wed, 11 Jul 2018 09:24:25 +0000 (18:24 +0900)
By default, arm_compute backend will be used for all operations.

Resolve #1878

Signed-off-by: Hanjoung Lee <hanjoung.lee@samsung.com>
runtimes/new_runtime/src/compilation.cc
runtimes/new_runtime/src/internal/op/Op.lst [new file with mode: 0644]

index 9984d3e..494aa80 100644 (file)
@@ -14,6 +14,7 @@
 #include "internal/Padding.h"
 #include "internal/IInitializerGenerator.h"
 #include "internal/IStageGenerator.h"
+#include "util/EnvVar.h"
 
 #include "compilation.h"
 #include "model.h"
@@ -76,17 +77,16 @@ class BackendResolver
 public:
   BackendResolver(::internal::BackendManager &backend_manager)
   {
-    auto acl_gen = backend_manager.get("arm_compute");
-    // auto cpu_gen = backend_manager.get("cpu"); // Unused for now
-
-    // TODO Set generator map according to environment variable
-    _gen_map[typeid(::internal::tflite::op::Conv2D::implicit::Node)] = acl_gen;
-    _gen_map[typeid(::internal::tflite::op::MaxPool2D::implicit::Node)] = acl_gen;
-    _gen_map[typeid(::internal::tflite::op::AvgPool2D::implicit::Node)] = acl_gen;
-    _gen_map[typeid(::internal::tflite::op::Concat::Node)] = acl_gen;
-    _gen_map[typeid(::internal::tflite::op::FullyConnected::Node)] = acl_gen;
-    _gen_map[typeid(::internal::tflite::op::Reshape::Node)] = acl_gen;
-    _gen_map[typeid(::internal::tflite::op::Softmax::Node)] = acl_gen;
+#define OP(InternalName, NnApiName)                                                            \
+  {                                                                                            \
+    const auto &backend_str =                                                                  \
+        ::nnfw::util::EnvVar{std::string("OP_BACKEND_") + #NnApiName}.asString("arm_compute"); \
+    auto backend = backend_manager.get(backend_str);                                           \
+    _gen_map[typeid(::internal::tflite::op::InternalName::Node)] = backend;                    \
+  }
+
+#include "internal/op/Op.lst"
+#undef OP
   }
 
   std::shared_ptr<::internal::IInitializerGenerator>
diff --git a/runtimes/new_runtime/src/internal/op/Op.lst b/runtimes/new_runtime/src/internal/op/Op.lst
new file mode 100644 (file)
index 0000000..b468067
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef OP
+#error  Define OP before including this file
+#endif
+
+// NOTE The relation between "Internal Name" and "NN API Name" is "1 : N".
+
+// Internal Name       | NN API Name
+OP(Conv2D::implicit    , CONV_2D)
+OP(AvgPool2D::implicit , AVERAGE_POOL_2D)
+OP(MaxPool2D::implicit , MAX_POOL_2D)
+OP(Concat              , CONCATENATION)
+OP(FullyConnected      , FULLY_CONNECTED)
+OP(Reshape             , RESHAPE)
+OP(Softmax             , SOFTMAX)