Make ConcatLayers support NCHW frontend layout (#7063)
author장지섭/On-Device Lab(SR)/Engineer/삼성전자 <jiseob.jang@samsung.com>
Fri, 30 Aug 2019 09:41:08 +0000 (18:41 +0900)
committer오형석/On-Device Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Fri, 30 Aug 2019 09:41:08 +0000 (18:41 +0900)
This commit makes ConcatLayers support NCHW frontend layout.

Signed-off-by: jiseob.jang <jiseob.jang@samsung.com>
runtimes/neurun/backend/acl_cl/KernelGenerator.cc
runtimes/neurun/backend/acl_cl/kernel/ConcatLayer.cc
runtimes/neurun/backend/acl_neon/KernelGenerator.cc
runtimes/neurun/backend/acl_neon/kernel/ConcatLayer.cc
runtimes/neurun/backend/cpu/KernelGenerator.cc
runtimes/neurun/backend/cpu/kernel/OperationUtils.h

index 27a19ae..2b0d901 100644 (file)
@@ -409,7 +409,13 @@ void KernelGenerator::visit(const model::operation::ConcatNode &node)
 
   auto fn = nnfw::cpp14::make_unique<::neurun::backend::acl_cl::kernel::ConcatLayer>();
 
-  fn->configure(input_allocs, axis, output_alloc);
+  const auto rank = _ctx.at(ofm_index).shape().rank();
+  const auto frontend_layout = _current_subg_layout;
+  const auto backend_layout = output_alloc->ptr()->layout();
+  const auto fixed_axis =
+      acl_common::ToARMComputeAxis(rank, axis, frontend_layout, backend_layout).value();
+
+  fn->configure(input_allocs, fixed_axis, output_alloc);
 
   auto acl_fn = asAclFunction(std::move(fn));
 
index bc9a101..aa1fd9a 100644 (file)
@@ -18,8 +18,6 @@
 
 #include <arm_compute/runtime/CL/CLScheduler.h>
 
-#include <Swizzle.h>
-
 #include "util/feature/nchw/View.h"
 #include "util/logging.h"
 
@@ -144,12 +142,7 @@ void ConcatLayer::configure(
   // TODO Handle when axis is negative
   assert(axis >= 0);
 
-  // This map converts to be reversed
-  // NHWC -> CWHN
-  // NCHW -> WHCN
-  _axis =
-      ::neurun::backend::acl_common::ToARMComputeAxis(_output_alloc->ptr()->num_dimensions(), axis)
-          .value();
+  _axis = axis;
 
   _input_type = input_allocs[0]->ptr()->data_type();
 }
index 308a962..a3d627b 100644 (file)
@@ -495,7 +495,13 @@ void KernelGenerator::visit(const model::operation::ConcatNode &node)
 
   auto fn = nnfw::cpp14::make_unique<::neurun::backend::acl_neon::kernel::ConcatLayer>();
 
-  fn->configure(input_allocs, axis, output_alloc);
+  const auto rank = _ctx.at(ofm_index).shape().rank();
+  const auto frontend_layout = _current_subg_layout;
+  const auto backend_layout = output_alloc->layout();
+  const auto fixed_axis =
+      acl_common::ToARMComputeAxis(rank, axis, frontend_layout, backend_layout).value();
+
+  fn->configure(input_allocs, fixed_axis, output_alloc);
 
   auto acl_fn = asAclFunction(std::move(fn));
 
index 8fab277..f2d88fa 100644 (file)
@@ -16,9 +16,6 @@
 
 #include "ConcatLayer.h"
 
-#include <Swizzle.h>
-
-#include "util/feature/nchw/View.h"
 #include "util/logging.h"
 
 namespace
@@ -82,8 +79,6 @@ template <typename T> bool ConcatLayer::concatenate()
   {
     uint32_t axis_offset = 0;
 
-    util::feature::nchw::View<T> output_view{_output_alloc};
-
     for (auto input : _input_allocs)
     {
       for (uint32_t i = 0; i < input->info()->dimension(0); i++)
@@ -133,11 +128,7 @@ void ConcatLayer::configure(
   // TODO Handle when axis is negative
   assert(axis >= 0);
 
-  // This map converts to be reversed
-  // NHWC -> CWHN
-  // NCHW -> WHCN
-  _axis =
-      ::neurun::backend::acl_common::ToARMComputeAxis(output_alloc->num_dimensions(), axis).value();
+  _axis = axis;
 
   _input_type = input_allocs[0]->data_type();
 }
index f809daf..b7ce8a2 100644 (file)
@@ -223,7 +223,9 @@ void KernelGenerator::visit(const model::operation::ConcatNode &node)
 {
   const auto ofm_index{node.getOutputs().at(0)};
 
-  const auto axis = node.param().axis;
+  const auto rank = _ctx.at(ofm_index).shape().rank();
+  const auto axis =
+      ::neurun::backend::cpu::kernel::getAxis(rank, node.param().axis, _current_subg_layout);
 
   const auto ofm_backend_shape =
       ::neurun::backend::cpu::kernel::getShape(_ctx.at(ofm_index), _current_subg_layout);
index 93a8f41..dc5bab9 100644 (file)
@@ -101,6 +101,25 @@ inline nnfw::cker::Shape convertShapeToCkerShape(const Shape &shape)
   return nnfw::cker::GetShape(raw_shape);
 }
 
+inline int32_t getAxis(uint32_t rank, int32_t axis, ::neurun::model::Layout frontend_layout)
+{
+  auto ret = axis;
+
+  if (axis < 0)
+  {
+    ret += rank;
+  }
+
+  // NCHW -> NHWC
+  if (frontend_layout == ::neurun::model::Layout::NCHW)
+  {
+    int32_t permutation[4] = {0, 3, 1, 2};
+    ret = permutation[ret];
+  }
+
+  return ret;
+}
+
 void QuantizeMultiplier(double double_multiplier, int32_t *quantized_multiplier, int *shift);
 
 void GetQuantizedConvolutionMultiplier(const Shape &inputShape, const Shape &filterShape,