[neurun] Enable Cast op in neurun (#4496)
author김수진/On-Device Lab(SR)/Engineer/삼성전자 <sjsujin.kim@samsung.com>
Thu, 28 Feb 2019 06:54:15 +0000 (15:54 +0900)
committer오형석/On-Device Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Thu, 28 Feb 2019 06:54:15 +0000 (15:54 +0900)
* [neurun] Enable Cast op in neurun

This commit enables `Cast` op in `neurun` for `acl_cl`, that is from `PACL`.

* Initialize CLKernelLibraryEx

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

index cad9b89..5d80a4f 100644 (file)
  * limitations under the License.
  */
 
+// For CLKernelLibraryEx initialization
+#include "arm_compute/core/CL/CLHelpers.h"
+#include "arm_compute/core/CL/CLKernelLibrary.h"
+#include "arm_compute/core/CL/CLKernelLibraryEx.h"
+
 #include <arm_compute/runtime/CL/CLScheduler.h>
 
 #include "backend/acl_cl/Config.h"
@@ -25,7 +30,14 @@ namespace backend
 namespace acl_cl
 {
 
-void Config::initialize() { arm_compute::CLScheduler::get().default_init(); }
+void Config::initialize()
+{
+  arm_compute::CLScheduler::get().default_init();
+  // NOTE CLKernelLibraryEx must use the same context as CLScheduler
+  // It did not check whether another device is available.
+  arm_compute::CLKernelLibraryEx::get().init(
+      "./cl_kernels/", arm_compute::CLScheduler::get().context(), cl::Device::getDefault());
+}
 
 } // namespace acl_cl
 } // namespace backend
index f5f40a4..d84c5ad 100644 (file)
@@ -29,6 +29,7 @@
 #include <arm_compute/runtime/misc/functions/GenericFullyConnectedLayer.h>
 #include <arm_compute/runtime/CL/functions/CLStridedSlice.h>
 #include <arm_compute/runtime/CL/functions/CLPixelWiseMultiplication.h>
+#include <arm_compute/runtime/CL/functions/CLCast.h>
 
 #include "kernel/ConcatLayer.h"
 
@@ -181,6 +182,44 @@ StageGenerator::StageGenerator(const neurun::model::operand::Set &ctx,
   // DO NOTHING
 }
 
+void StageGenerator::visit(const model::operation::CastNode &node)
+{
+  const auto ofm_index{node.getOutputs().at(0)};
+  const auto ifm_index{node.getInputs().at(model::operation::CastNode::Input::INPUT)};
+
+  // Construct operation parameters
+  struct Param
+  {
+    model::operand::Index ofm_index;
+    model::operand::Index ifm_index;
+  };
+
+  Param param;
+
+  param.ofm_index = ofm_index;
+  param.ifm_index = ifm_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();
+
+    std::unique_ptr<::arm_compute::IFunction> fn;
+
+    auto l = make_layer<::arm_compute::CLCast>();
+
+    l->configure(ifm_alloc->handle(), ofm_alloc->handle());
+
+    fn = std::move(l);
+
+    auto acl_fn = make_cl_function(std::move(fn));
+
+    builder.append(std::move(acl_fn));
+
+  });
+}
+
 void StageGenerator::visit(const model::operation::Conv2DNode &node)
 {
   using model::operation::Conv2DNode;
index fd385fc..56f5348 100644 (file)
@@ -52,6 +52,11 @@ StageGenerator::StageGenerator(const neurun::model::operand::Set &operand_ctx,
   // DO NOTHING
 }
 
+void StageGenerator::visit(const model::operation::CastNode &)
+{
+  throw std::runtime_error("CastNode for cpu is not implemented yet.");
+}
+
 void StageGenerator::visit(const model::operation::Conv2DNode &node)
 {
   using model::operation::Conv2DNode;
index 592737d..532252e 100644 (file)
@@ -29,6 +29,17 @@ namespace neurun
 namespace compiler
 {
 
+void OperationValidator::visit(const model::operation::CastNode &node)
+{
+  const auto output_index{node.getOutputs().at(0)};
+  const auto input_index{node.getInputs().at(0)};
+
+  UNUSED_RELEASE(output_index);
+  UNUSED_RELEASE(input_index);
+
+  assert(_ctx.at(output_index).shape() == _ctx.at(input_index).shape());
+}
+
 void OperationValidator::visit(const model::operation::Conv2DNode &)
 {
   // DO NOTHING
index 495e58d..a3b5dd8 100644 (file)
@@ -293,7 +293,7 @@ int ANeuralNetworksModel_addOperationEx(ANeuralNetworksModel *model,
     return ANEURALNETWORKS_BAD_STATE;
   }
 
-  const ANeuralNetworksOperationTypeEx FIRST_OPERATION = ANEURALNETWORKS_GATHER_EX;
+  const ANeuralNetworksOperationTypeEx FIRST_OPERATION = ANEURALNETWORKS_CAST_EX;
   const ANeuralNetworksOperationTypeEx LAST_OPERATION = ANEURALNETWORKS_PRELU_EX;
   if ((type < FIRST_OPERATION) || (type > LAST_OPERATION))
   {
index 851c5dc..042e2b6 100644 (file)
@@ -28,6 +28,18 @@ OperationFactory::OperationFactory()
 {
   using namespace neurun::model;
 
+  _map[ANEURALNETWORKS_CAST_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::CastNode{inputs, outputs};
+  };
+
   _map[ANEURALNETWORKS_CONV_2D] = [](const OperationFactory::Param &init_param) {
     using neurun::model::operation::Conv2DNode;
 
index 55ef63c..f9735f6 100644 (file)
@@ -21,6 +21,7 @@
 
 #include "model/operation/Node.h"
 #include "NeuralNetworks.h"
+#include "NeuralNetworksEx.h"
 
 /**
  * @brief A class to create a neurun operation object from NN API input parameters
index 8f6e628..4b95cea 100644 (file)
@@ -209,8 +209,8 @@ bool ANeuralNetworksModel::addOperation(ANeuralNetworksOperationType type, uint3
   return true;
 }
 
-bool ANeuralNetworksModel::addOperationEx(ANeuralNetworksOperationTypeEx type, uint32_t,
-                                          const uint32_t *, uint32_t outputCount,
+bool ANeuralNetworksModel::addOperationEx(ANeuralNetworksOperationTypeEx type, uint32_t inputCount,
+                                          const uint32_t *inputs, uint32_t outputCount,
                                           const uint32_t *outputs) noexcept
 {
   try
@@ -220,8 +220,19 @@ bool ANeuralNetworksModel::addOperationEx(ANeuralNetworksOperationTypeEx type, u
       const neurun::model::operand::Index ind{outputs[i]};
       _model->operands().at(ind).usage(neurun::model::operand::Usage::OPERATION_OUTPUT);
     }
+
+    auto &factory = OperationFactory::instance();
+    OperationFactory::Param param{inputCount, inputs, outputCount, outputs};
+
     switch (type)
     {
+      case ANEURALNETWORKS_CAST_EX:
+      {
+        auto node = factory.create(type, param);
+        _model->addOperation(std::unique_ptr<neurun::model::operation::Node>{node});
+
+        break;
+      }
       default:
         throw std::runtime_error{"Not supported operation"};
     }
diff --git a/runtimes/neurun/src/model/operation/CastNode.cc b/runtimes/neurun/src/model/operation/CastNode.cc
new file mode 100644 (file)
index 0000000..d3fd822
--- /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 "CastNode.h"
+
+#include <cassert>
+
+#include "NodeVisitor.h"
+
+namespace neurun
+{
+namespace model
+{
+namespace operation
+{
+
+void CastNode::accept(NodeVisitor &&v) const { v.visit(*this); }
+
+CastNode::CastNode(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/CastNode.h b/runtimes/neurun/src/model/operation/CastNode.h
new file mode 100644 (file)
index 0000000..56277d2
--- /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_CAST_NODE_H__
+#define __NEURUN_MODEL_OPERATION_CAST_NODE_H__
+
+#include "model/operation/Node.h"
+
+namespace neurun
+{
+namespace model
+{
+namespace operation
+{
+
+class CastNode : public model::operation::Node
+{
+public:
+  enum Input
+  {
+    INPUT = 0
+  };
+
+public:
+  CastNode(const operand::IndexSet &inputs, const operand::IndexSet &outputs);
+
+public:
+  virtual void accept(NodeVisitor &&) const override;
+  virtual std::string getName() const override { return "Cast"; }
+};
+
+} // namespace operation
+} // namespace model
+} // namespace neurun
+
+#endif // __NEURUN_MODEL_OPERATION_CAST_NODE_H__
index f89c42c..ded7e3f 100644 (file)
@@ -31,3 +31,4 @@
 #include "MulNode.h"
 #include "TanhNode.h"
 #include "LogisticNode.h"
+#include "CastNode.h"
index 419f14a..f4298aa 100644 (file)
@@ -23,6 +23,7 @@
 // Internal Name           | NN API? | NN API Name
 OP(AddNode                 , true    , ADD)
 OP(SubNode                 , true    , SUB)
+OP(CastNode                , true    , CAST_EX)
 OP(Conv2DNode              , true    , CONV_2D)
 OP(DepthwiseConv2DNode     , true    , DEPTHWISECONV_2D)
 OP(AvgPool2DNode           , true    , AVERAGE_POOL_2D)
index 2c2e1d8..fa79b53 100644 (file)
@@ -51,7 +51,6 @@ GeneratedTests.div_*
 GeneratedTests.space_to_batch*
 GeneratedTests.squeeze*
 GeneratedTests.transpose*
-GeneratedTests.cast_ex*
 GeneratedTests.gather_ex*
 GeneratedTests.tensorflowmax_ex*
 GeneratedTests.reduce_sum_ex*