Add functions for Tanh in pure acl runtime (#1993)
author남궁석/동작제어Lab(SR)/Engineer/삼성전자 <sk.namkoong@samsung.com>
Fri, 20 Jul 2018 05:36:08 +0000 (14:36 +0900)
committer이춘석/동작제어Lab(SR)/Staff Engineer/삼성전자 <chunseok.lee@samsung.com>
Fri, 20 Jul 2018 05:36:08 +0000 (14:36 +0900)
Add appendTanh function in ActivationBuilder
Add visit function
Add tanh case in addOperation function

Signed-off-by: Seok NamKoong <sk.namkoong@samsung.com>
runtimes/pure_arm_compute/src/compilation.cc
runtimes/pure_arm_compute/src/model.cc

index 4b28821..e17ddb8 100644 (file)
@@ -235,6 +235,7 @@ private:
   void appendReLU(::arm_compute::ICLTensor *tensor);
   void appendReLU6(::arm_compute::ICLTensor *tensor);
   void appendReLU1(::arm_compute::ICLTensor *tensor);
+  void appendTanh(::arm_compute::ICLTensor *tensor);
 
 public:
   void append(FuseCode code, ::arm_compute::ICLTensor *tensor);
@@ -279,6 +280,18 @@ void ActivationBuilder::appendReLU6(::arm_compute::ICLTensor *ifm_alloc)
   _builder.append("ReLU6", std::move(fn));
 }
 
+void ActivationBuilder::appendTanh(::arm_compute::ICLTensor *ifm_alloc)
+{
+  const ::arm_compute::ActivationLayerInfo act_info{
+      ::arm_compute::ActivationLayerInfo::ActivationFunction::TANH, 1.0f, 1.0f};
+
+  auto fn = nnfw::make_unique<::arm_compute::CLActivationLayer>();
+
+  fn->configure(ifm_alloc, nullptr, act_info);
+
+  _builder.append("Tanh", std::move(fn));
+}
+
 void ActivationBuilder::append(FuseCode code, ::arm_compute::ICLTensor *ifm_alloc)
 {
   switch (code)
@@ -2525,7 +2538,41 @@ void Planner::visit(const ::internal::tflite::op::Tanh::Node &node)
 {
   VERBOSE(Tanh) << "Configure Tanh operation" << std::endl;
 
-  throw std::runtime_error{"Not supported operation"};
+  const ::internal::tflite::operand::Index ofm_index{node.param().ofm_index};
+  const ::internal::tflite::operand::Index ifm_index{node.param().ifm_index};
+
+  // Set shape constraints
+  _builder.addShapeConstr(ofm_index,
+                          asTensorInfo(_ctx.at(ofm_index).shape(), _ctx.at(ofm_index).type()));
+  _builder.addShapeConstr(ifm_index,
+                          asTensorInfo(_ctx.at(ifm_index).shape(), _ctx.at(ifm_index).type()));
+
+  struct Param
+  {
+    int ofm_index;
+    int ifm_index;
+  };
+
+  Param param;
+
+  param.ofm_index = ofm_index.asInt();
+  param.ifm_index = ifm_index.asInt();
+
+  auto stage = [param](const IAllocationContext &ctx, IExecutionBuilder &builder) {
+    auto ofm_alloc = ctx.at(::internal::tflite::operand::Index{param.ofm_index});
+    auto ifm_alloc = ctx.at(::internal::tflite::operand::Index{param.ifm_index});
+
+    const ::arm_compute::ActivationLayerInfo act_info{
+        ::arm_compute::ActivationLayerInfo::ActivationFunction::TANH, 1.0f, 1.0f};
+
+    auto fn = nnfw::make_unique<::arm_compute::CLActivationLayer>();
+
+    fn->configure(ifm_alloc, ofm_alloc, act_info);
+
+    builder.append("Tanh", std::move(fn));
+  };
+
+  _builder.addStage(stage);
 }
 
 class AllocationContext final : public IAllocationContext
index b5959cd..ee308a5 100644 (file)
@@ -368,6 +368,18 @@ int ANeuralNetworksModel_addOperation(ANeuralNetworksModel *model,
 
       break;
     }
+    case ANEURALNETWORKS_TANH:
+    {
+      using internal::tflite::op::Tanh::Param;
+      using internal::tflite::op::Tanh::Node;
+
+      // Add 'operations'
+      auto &operations = model->deref().operations();
+
+      operations.emplace_back<Node>(Param{inputCount, inputs, outputCount, outputs});
+
+      break;
+    }
     default:
       throw std::runtime_error{"Not supported operation"};
   };