[neurun] Enable ReLU, RSQRT ops (#4642)
author김수진/On-Device Lab(SR)/Engineer/삼성전자 <sjsujin.kim@samsung.com>
Tue, 12 Mar 2019 07:42:11 +0000 (16:42 +0900)
committer이춘석/On-Device Lab(SR)/Staff Engineer/삼성전자 <chunseok.lee@samsung.com>
Tue, 12 Mar 2019 07:42:11 +0000 (16:42 +0900)
This commit enables `ReLU`, `RSQRT` for `acl_ac`.

Signed-off-by: sjsujinkim <sjsujin.kim@samsung.com>
runtimes/neurun/src/backend/acl_cl/StageGenerator.cc
runtimes/neurun/src/backend/acl_cl/StageGenerator.h
runtimes/neurun/src/frontend/wrapper/OperationFactory.cc
runtimes/neurun/src/model/operation/Node.Include.h
runtimes/neurun/src/model/operation/Op.lst
runtimes/neurun/src/model/operation/RSQRTNode.cc [new file with mode: 0644]
runtimes/neurun/src/model/operation/RSQRTNode.h [new file with mode: 0644]
runtimes/neurun/src/model/operation/ReLUNode.cc [new file with mode: 0644]
runtimes/neurun/src/model/operation/ReLUNode.h [new file with mode: 0644]
tests/nnapi/nnapi_gtest.skip.armv7l-linux.neurun
tests/scripts/neurun_frameworktest_list.armv7l.acl_cl.txt

index d828e96..e8bcbd3 100644 (file)
@@ -1665,6 +1665,77 @@ void StageGenerator::visit(const model::operation::NotEqualNode &node)
   });
 }
 
+void StageGenerator::visit(const model::operation::RSQRTNode &node)
+{
+  const auto output_index{node.getOutputs().at(0)};
+  const auto input_index{node.getInputs().at(model::operation::LogisticNode::Input::INPUT)};
+
+  // Construct operation parameters
+  struct Param
+  {
+    model::operand::Index ofm_index;
+    model::operand::Index ifm_index;
+  };
+
+  Param param;
+
+  param.ofm_index = output_index;
+  param.ifm_index = input_index;
+
+  auto tensors = _tensor_builder;
+
+  returnStage([tensors, param](IExecutionBuilder &builder) {
+    auto ofm_alloc = tensors->at(param.ofm_index).get();
+    auto ifm_alloc = tensors->at(param.ifm_index).get();
+
+    const ::arm_compute::ActivationLayerInfoEx act_info{
+        ::arm_compute::ActivationLayerInfoEx::ActivationFunction::RSQRT};
+
+    auto fn = make_layer<::arm_compute::CLActivationLayerEx>();
+
+    fn->configure(ifm_alloc->handle(), ofm_alloc->handle(), act_info);
+
+    auto acl_fn = make_cl_function(std::move(fn));
+
+    builder.append(std::move(acl_fn));
+  });
+}
+
+void StageGenerator::visit(const model::operation::ReLUNode &node)
+{
+  const auto output_index{node.getOutputs().at(0)};
+  const auto input_index{node.getInputs().at(model::operation::ReLUNode::Input::INPUT)};
+
+  struct Param
+  {
+    model::operand::Index output_index;
+    model::operand::Index input_index;
+  };
+
+  Param param;
+
+  param.output_index = output_index;
+  param.input_index = input_index;
+
+  auto tensors = _tensor_builder;
+
+  returnStage([tensors, param](IExecutionBuilder &builder) {
+    auto output_alloc = tensors->at(param.output_index).get();
+    auto input_alloc = tensors->at(param.input_index).get();
+
+    auto fn = make_layer<arm_compute::CLActivationLayer>();
+
+    const ::arm_compute::ActivationLayerInfo act_info{
+        ::arm_compute::ActivationLayerInfo::ActivationFunction::RELU};
+
+    fn->configure(input_alloc->handle(), output_alloc->handle(), act_info);
+
+    auto acl_fn = make_cl_function(std::move(fn));
+
+    builder.append(std::move(acl_fn));
+  });
+}
+
 } // namespace acl_cl
 } // namespace backend
 } // namespace neurun
index 850694a..686293c 100644 (file)
@@ -60,6 +60,8 @@ public:
   virtual void visit(const model::operation::ReduceMaxNode &) override;
   virtual void visit(const model::operation::NotEqualNode &) override;
   virtual void visit(const model::operation::LogicalAndNode &) override;
+  virtual void visit(const model::operation::RSQRTNode &) override;
+  virtual void visit(const model::operation::ReLUNode &) override;
 
 private:
   const neurun::model::operand::Set &_ctx;
index 980d859..8275e07 100644 (file)
@@ -629,6 +629,32 @@ OperationFactory::OperationFactory()
 
     return new operation::LogicalAndNode{inputs, outputs};
   };
+
+  _map[ANEURALNETWORKS_RSQRT_EX] = [](const OperationFactory::Param &init_param) {
+    assert(init_param.input_count == 1 && init_param.output_count == 1);
+
+    operand::IndexSet outputs{init_param.outputs[0]};
+
+    // Each input should be interpreted as follows:
+    //
+    //  0 -> Input Tensor Index
+    operand::IndexSet inputs{init_param.inputs[0]};
+
+    return new operation::RSQRTNode{inputs, outputs};
+  };
+
+  _map[ANEURALNETWORKS_RELU] = [](const OperationFactory::Param &init_param) {
+    assert(init_param.input_count == 1 && init_param.output_count == 1);
+
+    operand::IndexSet outputs{init_param.outputs[0]};
+
+    // Each input should be interpreted as follows:
+    //
+    //  0 -> Input Tensor Index
+    operand::IndexSet inputs{init_param.inputs[0]};
+
+    return new operation::ReLUNode{inputs, outputs};
+  };
 }
 
 neurun::model::operation::Node *OperationFactory::create(ANeuralNetworksOperationType type,
index 56f4499..7289c9d 100644 (file)
@@ -40,3 +40,5 @@
 #include "ReduceMaxNode.h"
 #include "NotEqualNode.h"
 #include "LogicalAndNode.h"
+#include "RSQRTNode.h"
+#include "ReLUNode.h"
index 12dea54..a3ea626 100644 (file)
@@ -44,4 +44,6 @@ OP(ExpNode                 , true    , EXP_EX)
 OP(ReduceMaxNode           , true    , TENSORFLOW_MAX_EX)
 OP(NotEqualNode            , true    , NOT_EQUAL_EX)
 OP(LogicalAndNode          , true    , LOGICAL_AND_EX)
+OP(RSQRTNode               , true    , RSQRT_EX)
+OP(ReLUNode                , true    , RELU)
 OP(PermuteNode             , false   , NOT_AVAILABLE)
diff --git a/runtimes/neurun/src/model/operation/RSQRTNode.cc b/runtimes/neurun/src/model/operation/RSQRTNode.cc
new file mode 100644 (file)
index 0000000..77ad3c9
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2019 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.
+ */
+
+#include "RSQRTNode.h"
+
+#include <cassert>
+
+#include "NodeVisitor.h"
+
+namespace neurun
+{
+namespace model
+{
+namespace operation
+{
+
+void RSQRTNode::accept(NodeVisitor &&v) const { v.visit(*this); }
+
+RSQRTNode::RSQRTNode(const operand::IndexSet &inputs, const operand::IndexSet &outputs)
+    : model::operation::Node{OperandConstraint::createExact(1u), inputs, outputs}
+{
+}
+
+} // namespace operation
+} // namespace model
+} // namespace neurun
diff --git a/runtimes/neurun/src/model/operation/RSQRTNode.h b/runtimes/neurun/src/model/operation/RSQRTNode.h
new file mode 100644 (file)
index 0000000..a75e591
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2019 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 __NEURUN_MODEL_OPERATION_RSQRT_NODE_H__
+#define __NEURUN_MODEL_OPERATION_RSQRT_NODE_H__
+
+#include "model/operation/Node.h"
+
+namespace neurun
+{
+namespace model
+{
+namespace operation
+{
+
+class RSQRTNode : public model::operation::Node
+{
+public:
+  enum Input
+  {
+    INPUT = 0
+  };
+
+public:
+  RSQRTNode(const operand::IndexSet &inputs, const operand::IndexSet &outputs);
+
+public:
+  virtual void accept(NodeVisitor &&) const override;
+  virtual std::string getName() const override { return "RSQRT"; }
+};
+
+} // namespace operation
+} // namespace model
+} // namespace neurun
+
+#endif // __NEURUN_MODEL_OPERATION_RSQRT_NODE_H__
diff --git a/runtimes/neurun/src/model/operation/ReLUNode.cc b/runtimes/neurun/src/model/operation/ReLUNode.cc
new file mode 100644 (file)
index 0000000..692f648
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2019 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.
+ */
+
+#include "ReLUNode.h"
+
+#include <cassert>
+
+#include "NodeVisitor.h"
+
+namespace neurun
+{
+namespace model
+{
+namespace operation
+{
+
+void ReLUNode::accept(NodeVisitor &&v) const { v.visit(*this); }
+
+ReLUNode::ReLUNode(const operand::IndexSet &inputs, const operand::IndexSet &outputs)
+    : model::operation::Node{OperandConstraint::createExact(1u), inputs, outputs}
+{
+}
+
+} // namespace operation
+} // namespace model
+} // namespace neurun
diff --git a/runtimes/neurun/src/model/operation/ReLUNode.h b/runtimes/neurun/src/model/operation/ReLUNode.h
new file mode 100644 (file)
index 0000000..ce45855
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2019 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 __NEURUN_MODEL_OPERATION_RELU_NODE_H__
+#define __NEURUN_MODEL_OPERATION_RELU_NODE_H__
+
+#include "model/operation/Node.h"
+
+namespace neurun
+{
+namespace model
+{
+namespace operation
+{
+
+class ReLUNode : public model::operation::Node
+{
+public:
+  enum Input
+  {
+    INPUT = 0
+  };
+
+public:
+  ReLUNode(const operand::IndexSet &inputs, const operand::IndexSet &outputs);
+
+public:
+  virtual void accept(NodeVisitor &&) const override;
+  virtual std::string getName() const override { return "ReLU"; }
+};
+
+} // namespace operation
+} // namespace model
+} // namespace neurun
+
+#endif // __NEURUN_MODEL_OPERATION_RELU_NODE_H__
index 5904043..7d91c12 100644 (file)
@@ -32,10 +32,8 @@ GeneratedTests.prelu_ex*
 GeneratedTests.reduce_min*
 GeneratedTests.relu1*
 GeneratedTests.relu6*
-GeneratedTests.relu*
 GeneratedTests.resize_bilinear*
 GeneratedTests.rnn*
-GeneratedTests.rsqrt*
 GeneratedTests.mean*
 GeneratedTests.pad*
 GeneratedTests.space_to_depth*
index 71542eb..ec63189 100644 (file)
@@ -14,7 +14,9 @@ mul/broadcast
 not_equal
 softmax
 reduce_max
+relu
 reshape
+rsqrt
 strided_slice
 sub/broadcast
 tanh