Apply backend layout for getTensorDesc() of ncnn (#8755)
author장지섭/On-Device Lab(SR)/Engineer/삼성전자 <jiseob.jang@samsung.com>
Wed, 6 Nov 2019 00:58:14 +0000 (09:58 +0900)
committer오형석/On-Device Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Wed, 6 Nov 2019 00:58:14 +0000 (09:58 +0900)
This commit applies backend layout for getTensorDesc() of ncnn.

Signed-off-by: jiseob.jang <jiseob.jang@samsung.com>
runtime/neurun/backend/srcn/KernelGenerator.cc
runtime/neurun/backend/srcn/kernel/AddLayer.cc
runtime/neurun/backend/srcn/kernel/AddLayer.h
runtime/neurun/backend/srcn/kernel/OperationUtils.cc
runtime/neurun/backend/srcn/kernel/OperationUtils.h

index b4a8895..6a305db 100644 (file)
@@ -71,15 +71,6 @@ void KernelGenerator::visit(const model::operation::InstanceNorm &node)
   const auto epsilon = node.param().epsilon;
   const auto activation = node.param().activation;
 
-  const auto ofm_backend_descr = ::neurun::backend::srcn::kernel::getTensorDescriptor(
-      _ctx.at(ofm_index), _current_subg_layout);
-  const auto ifm_backend_descr = ::neurun::backend::srcn::kernel::getTensorDescriptor(
-      _ctx.at(ifm_index), _current_subg_layout);
-  const auto gamma_backend_descr = ::neurun::backend::srcn::kernel::getTensorDescriptor(
-      _ctx.at(gamma_index), _current_subg_layout);
-  const auto beta_backend_descr = ::neurun::backend::srcn::kernel::getTensorDescriptor(
-      _ctx.at(beta_index), _current_subg_layout);
-
   auto ofm_alloc = _tensor_builder->at(ofm_index);
   auto ifm_alloc = _tensor_builder->at(ifm_index);
   auto gamma_alloc = _tensor_builder->at(gamma_index);
@@ -87,6 +78,15 @@ void KernelGenerator::visit(const model::operation::InstanceNorm &node)
 
   const auto backend_layout = ofm_alloc->layout();
 
+  const auto ofm_backend_descr = ::neurun::backend::srcn::kernel::getTensorDescriptor(
+      _ctx.at(ofm_index), _current_subg_layout, backend_layout);
+  const auto ifm_backend_descr = ::neurun::backend::srcn::kernel::getTensorDescriptor(
+      _ctx.at(ifm_index), _current_subg_layout, backend_layout);
+  const auto gamma_backend_descr = ::neurun::backend::srcn::kernel::getTensorDescriptor(
+      _ctx.at(gamma_index), _current_subg_layout, backend_layout);
+  const auto beta_backend_descr = ::neurun::backend::srcn::kernel::getTensorDescriptor(
+      _ctx.at(beta_index), _current_subg_layout, backend_layout);
+
   auto fn = nnfw::cpp14::make_unique<::neurun::backend::srcn::kernel::InstanceNormLayer>();
 
   fn->configure(ifm_alloc->buffer(), ifm_backend_descr, gamma_alloc->buffer(), gamma_backend_descr,
@@ -116,16 +116,18 @@ void KernelGenerator::visit(const model::operation::TransposeConv &node)
   const auto padding = neurun::util::calculatePadding(node.param().padding, ofm_shape, ifm_shape,
                                                       stride, ker_width, ker_height);
 
-  const auto ofm_backend_descr = ::neurun::backend::srcn::kernel::getTensorDescriptor(
-      _ctx.at(ofm_index), _current_subg_layout);
-  const auto ifm_backend_descr = ::neurun::backend::srcn::kernel::getTensorDescriptor(
-      _ctx.at(ifm_index), _current_subg_layout);
-  const auto ker_backend_descr = ::neurun::backend::srcn::kernel::getTensorDescriptor(
-      _ctx.at(ker_index), model::Layout::UNKNOWN);
-
   auto ofm_alloc = _tensor_builder->at(ofm_index);
   auto ifm_alloc = _tensor_builder->at(ifm_index);
   auto ker_alloc = _tensor_builder->at(ker_index);
+  const auto backend_layout = ofm_alloc->layout();
+  assert(backend_layout == ifm_alloc->layout());
+
+  const auto ofm_backend_descr = ::neurun::backend::srcn::kernel::getTensorDescriptor(
+      _ctx.at(ofm_index), _current_subg_layout, backend_layout);
+  const auto ifm_backend_descr = ::neurun::backend::srcn::kernel::getTensorDescriptor(
+      _ctx.at(ifm_index), _current_subg_layout, backend_layout);
+  const auto ker_backend_descr = ::neurun::backend::srcn::kernel::getTensorDescriptor(
+      _ctx.at(ker_index), _current_subg_layout, backend_layout);
 
   auto fn = nnfw::cpp14::make_unique<::neurun::backend::srcn::kernel::TransposeConvLayer>();
 
@@ -144,27 +146,26 @@ void KernelGenerator::visit(const model::operation::Add &node)
   const auto lhs_index{node.getInputs().at(model::operation::Add::Input::LHS)};
   const auto rhs_index{node.getInputs().at(model::operation::Add::Input::RHS)};
 
-  const auto ofm_backend_descr = ::neurun::backend::srcn::kernel::getTensorDescriptor(
-      _ctx.at(ofm_index), _current_subg_layout);
-  const auto lhs_backend_descr = ::neurun::backend::srcn::kernel::getTensorDescriptor(
-      _ctx.at(lhs_index), _current_subg_layout);
-  const auto rhs_backend_descr = ::neurun::backend::srcn::kernel::getTensorDescriptor(
-      _ctx.at(rhs_index), _current_subg_layout);
-
   const auto activation = node.param().activation;
 
   auto ofm_alloc = _tensor_builder->at(ofm_index).get();
   auto lhs_alloc = _tensor_builder->at(lhs_index).get();
   auto rhs_alloc = _tensor_builder->at(rhs_index).get();
 
-  const auto frontend_layout = _current_subg_layout;
   const auto backend_layout = ofm_alloc->layout();
+  assert(backend_layout == lhs_alloc->layout() && backend_layout == rhs_alloc->layout());
+
+  const auto ofm_backend_descr = ::neurun::backend::srcn::kernel::getTensorDescriptor(
+      _ctx.at(ofm_index), _current_subg_layout, backend_layout);
+  const auto lhs_backend_descr = ::neurun::backend::srcn::kernel::getTensorDescriptor(
+      _ctx.at(lhs_index), _current_subg_layout, backend_layout);
+  const auto rhs_backend_descr = ::neurun::backend::srcn::kernel::getTensorDescriptor(
+      _ctx.at(rhs_index), _current_subg_layout, backend_layout);
 
   auto fn = nnfw::cpp14::make_unique<::neurun::backend::srcn::kernel::AddLayer>();
 
   fn->configure(lhs_alloc->buffer(), lhs_backend_descr, rhs_alloc->buffer(), rhs_backend_descr,
-                activation, ofm_alloc->buffer(), ofm_backend_descr, frontend_layout,
-                backend_layout);
+                activation, ofm_alloc->buffer(), ofm_backend_descr, backend_layout);
 
   _execution_builder->append(std::move(fn));
 }
index ecf3a26..6b51507 100644 (file)
@@ -92,7 +92,7 @@ void AddLayer::addQuant8()
 void AddLayer::configure(uint8_t *lhsData, const TensorDescriptor &lhsDescr, uint8_t *rhsData,
                          const TensorDescriptor &rhsDescr, const model::Activation activation,
                          uint8_t *outputData, const TensorDescriptor &outputDescr,
-                         const model::Layout frontendLayout, const model::Layout backendLayout)
+                         const model::Layout backendLayout)
 {
   _lhsData.u8 = lhsData;
   _lhsDescr = lhsDescr;
@@ -102,7 +102,6 @@ void AddLayer::configure(uint8_t *lhsData, const TensorDescriptor &lhsDescr, uin
   _activation = activation;
   _outputData.u8 = outputData;
   _outputDescr = outputDescr;
-  _frontendLayout = frontendLayout;
   _backendLayout = backendLayout;
 }
 
index d7d2e88..9d6edb9 100644 (file)
@@ -46,7 +46,7 @@ public:
   void configure(uint8_t *lhsData, const TensorDescriptor &lhsDescr, uint8_t *rhsData,
                  const TensorDescriptor &rhsDescr, const model::Activation activation,
                  uint8_t *outputData, const TensorDescriptor &outputDescr,
-                 const model::Layout frontendLayout, const model::Layout backendLayout);
+                 const model::Layout backendLayout);
 
   void run();
   void runSync()
@@ -69,7 +69,6 @@ private:
 
   OperandType _inputType{OperandType::FLOAT32};
 
-  model::Layout _frontendLayout{model::Layout::UNKNOWN};
   model::Layout _backendLayout{model::Layout::UNKNOWN};
 };
 
index 930ce16..cd12846 100644 (file)
@@ -61,12 +61,24 @@ Coordinates convertCoordinates(const Coordinates &from_coordinates, FilterLayout
 }
 
 TensorDescriptor getTensorDescriptor(const ::neurun::model::Operand &o,
-                                     ::neurun::model::Layout frontend_layout)
+                                     ::neurun::model::Layout frontend_layout,
+                                     ::neurun::model::Layout backend_layout)
 {
   TensorDescriptor descriptor;
 
   auto dims = o.shape().dims();
-  if (frontend_layout == ::neurun::model::Layout::NCHW && o.shape().rank() == 4)
+  if (frontend_layout == ::neurun::model::Layout::NHWC &&
+      backend_layout == ::neurun::model::Layout::NCHW && o.shape().rank() == 4)
+  {
+    // NHWC -> NCHW
+    uint32_t permutation[4] = {0, 3, 1, 2};
+    for (int i = 0; i < o.shape().rank(); ++i)
+    {
+      dims.at(i) = o.shape().dim(permutation[i]);
+    }
+  }
+  else if (frontend_layout == ::neurun::model::Layout::NCHW &&
+           backend_layout == ::neurun::model::Layout::NHWC && o.shape().rank() == 4)
   {
     // NCHW -> NHWC
     uint32_t permutation[4] = {0, 2, 3, 1};
index 148adaa..ce676cd 100644 (file)
@@ -67,7 +67,8 @@ Coordinates convertCoordinates(const Coordinates &from_coordinates, FilterLayout
                                FilterLayout to_layout);
 
 TensorDescriptor getTensorDescriptor(const ::neurun::model::Operand &o,
-                                     ::neurun::model::Layout frontend_layout);
+                                     ::neurun::model::Layout frontend_layout,
+                                     ::neurun::model::Layout backend_layout);
 
 } // namespace kernel
 } // namespace srcn