[neurun] Simplify code using Operand::asVector (#9038)
authorSergei Barannikov/AI Tools Lab /SRR/Engineer/Samsung Electronics <s.barannikov@samsung.com>
Thu, 21 Nov 2019 06:58:07 +0000 (09:58 +0300)
committer오형석/On-Device Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Thu, 21 Nov 2019 06:58:07 +0000 (15:58 +0900)
Simplify code using newly introduced `Operand::asVector`.

Signed-off-by: Sergei Barannikov <s.barannikov@samsung.com>
runtime/neurun/backend/acl_cl/KernelGenerator.cc
runtime/neurun/backend/acl_neon/KernelGenerator.cc

index b4112b1..d0bdacd 100644 (file)
@@ -533,7 +533,7 @@ void KernelGenerator::visit(const model::operation::ReduceSum &node)
 
   // Convert to ACL axes taking into account negative values and possible duplicates.
   std::set<std::uint32_t> acl_axes;
-  const auto input_rank = _ctx.at(input_index).shape().rank();
+  const int input_rank = _ctx.at(input_index).shape().rank();
   for (int axis : axes)
   {
     if (axis < 0)
@@ -1809,7 +1809,7 @@ void KernelGenerator::visit(const model::operation::Mean &node)
   // Convert to ACL axes taking into account negative values and possible duplicates.
   std::set<std::uint32_t> acl_axes;
   const int ifm_rank = _ctx.at(ifm_index).shape().rank();
-  for (auto axis : axes)
+  for (int axis : axes)
   {
     if (axis < 0)
       axis += ifm_rank;
@@ -1891,7 +1891,7 @@ void KernelGenerator::visit(const model::operation::ReduceMin &node)
 
   // Convert to ACL axes taking into account negative values and possible duplicates.
   std::set<std::uint32_t> acl_axes;
-  const auto ifm_rank = _ctx.at(ifm_index).shape().rank();
+  const int ifm_rank = _ctx.at(ifm_index).shape().rank();
   for (int axis : axes)
   {
     if (axis < 0)
index 4cace9c..89251d8 100644 (file)
@@ -433,64 +433,29 @@ void KernelGenerator::visit(const model::operation::Mean &node)
 {
   const auto ofm_index{node.getOutputs().at(0)};
   const auto ifm_index{node.getInputs().at(model::operation::Mean::Input::INPUT)};
-
-  const auto axis_index{node.param().axis_index};
+  const auto &axes{_ctx.at(node.param().axis_index).asVector<int>()};
   const auto keep_dims{node.param().keep_dims};
 
-  const auto ifm_shape = _ctx.at(ifm_index).shape();
-
   auto ofm_alloc = _tensor_builder->at(ofm_index).get();
   auto ifm_alloc = _tensor_builder->at(ifm_index).get();
-  std::set<uint32_t> axes;
+  const auto frontend_layout = _current_subg_layout;
+  const auto backend_layout = ifm_alloc->layout();
+
+  // Convert to ACL axes taking into account negative values and possible duplicates.
+  std::set<std::uint32_t> acl_axes;
+  const int ifm_rank = _ctx.at(ifm_index).shape().rank();
+  for (int axis : axes)
   {
-    const auto ifm_rank = ifm_shape.rank();
-    const auto frontend_layout = _current_subg_layout;
-    const auto backend_layout = ifm_alloc->layout();
-    const auto axis_shape = _ctx.at(axis_index).shape();
-    switch (axis_shape.rank())
-    {
-      case 0: // scalar
-      {
-        auto axis_value = _ctx.at(axis_index).asScalar<int32_t>();
-        if (axis_value < 0)
-        {
-          axis_value += ifm_rank;
-        }
-        axes.insert(::neurun::backend::acl_common::ToARMComputeAxis(ifm_rank, axis_value,
-                                                                    frontend_layout, backend_layout)
-                        .value());
-        break;
-      }
-      case 1: // vector
-      {
-        const auto axis_base = _ctx.at(axis_index).data().base();
-        const int axis_size = axis_shape.num_elements();
-
-        // If axis's data does not exist as constant values and can be gotten as input data, we have
-        // to find a way to infer output shape when sinking output.
-        assert(axis_base != nullptr);
-        for (int32_t n = 0; n < axis_size; ++n)
-        {
-          int32_t axis_value = *(reinterpret_cast<const int32_t *>(axis_base) + n);
-          if (axis_value < 0)
-          {
-            axis_value += ifm_rank;
-          }
-          axes.insert(::neurun::backend::acl_common::ToARMComputeAxis(
-                          ifm_rank, axis_value, frontend_layout, backend_layout)
-                          .value());
-        }
-        break;
-      }
-      default:
-        throw std::runtime_error("Not supported");
-    }
+    if (axis < 0)
+      axis += ifm_rank;
+    acl_axes.insert(
+        acl_common::ToARMComputeAxis(ifm_rank, axis, frontend_layout, backend_layout).value());
   }
 
   arm_compute::Coordinates fixed_axis;
-  for (auto a : axes)
+  for (const auto axis : acl_axes)
   {
-    fixed_axis.set(fixed_axis.num_dimensions(), a);
+    fixed_axis.set(fixed_axis.num_dimensions(), axis);
   }
 
   // NOTE NEReduceMean has a bug that does not support NHWC layout
@@ -1284,64 +1249,30 @@ void KernelGenerator::visit(const model::operation::ReduceMax &node)
 {
   const auto ofm_index{node.getOutputs().at(0)};
   const auto ifm_index{node.getInputs().at(model::operation::ReduceMax::Input::INPUT)};
-  const auto axis_index{node.param().axis_index};
+  const auto &axes{_ctx.at(node.param().axis_index).asVector<int>()};
 
-  auto ifm_shape = _ctx.at(ifm_index).shape();
-  auto ofm_shape = _ctx.at(ofm_index).shape();
-  auto axis_shape = _ctx.at(axis_index).shape();
-
-  const auto ifm_rank = ifm_shape.rank();
   auto ofm_alloc = _tensor_builder->at(ofm_index).get();
   auto ifm_alloc = _tensor_builder->at(ifm_index).get();
-  std::set<uint32_t> axes;
+  const auto frontend_layout = _current_subg_layout;
+  const auto backend_layout = ifm_alloc->layout();
+
+  // Convert to ACL axes taking into account negative values and possible duplicates.
+  std::set<std::uint32_t> acl_axes;
+  const int ifm_rank = _ctx.at(ifm_index).shape().rank();
+  for (int axis : axes)
   {
-    const auto frontend_layout = _current_subg_layout;
-    const auto backend_layout = ifm_alloc->layout();
-    switch (axis_shape.rank())
-    {
-      case 0: // scalar
-      {
-        auto axis_value = _ctx.at(axis_index).asScalar<int32_t>();
-        if (axis_value < 0)
-        {
-          axis_value += ifm_rank;
-        }
-        axes.insert(::neurun::backend::acl_common::ToARMComputeAxis(ifm_rank, axis_value,
-                                                                    frontend_layout, backend_layout)
-                        .value());
-        break;
-      }
-      case 1: // vector
-      {
-        const auto axis_base = _ctx.at(axis_index).data().base();
-        const int axis_size = axis_shape.num_elements();
-
-        // If axis's data does not exist as constant values and can be gotten as input data, we have
-        // to find a way to infer output shape when sinking output.
-        assert(axis_base != nullptr);
-        for (int32_t n = 0; n < axis_size; ++n)
-        {
-          int32_t axis_value = *(reinterpret_cast<const int32_t *>(axis_base) + n);
-          if (axis_value < 0)
-          {
-            axis_value += ifm_rank;
-          }
-          axes.insert(::neurun::backend::acl_common::ToARMComputeAxis(
-                          ifm_rank, axis_value, frontend_layout, backend_layout)
-                          .value());
-        }
-        break;
-      }
-      default:
-        throw std::runtime_error("Not supported");
-        break;
-    }
+    if (axis < 0)
+      axis += ifm_rank;
+    acl_axes.insert(
+        acl_common::ToARMComputeAxis(ifm_rank, axis, frontend_layout, backend_layout).value());
   }
+
   arm_compute::Coordinates reduce_axes;
-  for (const auto &a : axes)
+  for (const auto axis : acl_axes)
   {
-    reduce_axes.set(reduce_axes.num_dimensions(), a);
+    reduce_axes.set(reduce_axes.num_dimensions(), axis);
   }
+
   auto fn = nnfw::cpp14::make_unique<::arm_compute::NEReduceOperation>();
 
   fn->configure(ifm_alloc->handle(), reduce_axes, false, ofm_alloc->handle(),
@@ -1356,64 +1287,30 @@ void KernelGenerator::visit(const model::operation::ReduceMin &node)
 {
   const auto ofm_index{node.getOutputs().at(0)};
   const auto ifm_index{node.getInputs().at(model::operation::ReduceMin::Input::INPUT)};
-  const auto axis_index{node.param().axis_index};
-
-  auto ifm_shape = _ctx.at(ifm_index).shape();
-  auto ofm_shape = _ctx.at(ofm_index).shape();
-  auto axis_shape = _ctx.at(axis_index).shape();
+  const auto &axes{_ctx.at(node.param().axis_index).asVector<int>()};
 
-  const auto ifm_rank = ifm_shape.rank();
   auto ofm_alloc = _tensor_builder->at(ofm_index).get();
   auto ifm_alloc = _tensor_builder->at(ifm_index).get();
-  std::set<uint32_t> axes;
+  const auto frontend_layout = _current_subg_layout;
+  const auto backend_layout = ifm_alloc->layout();
+
+  // Convert to ACL axes taking into account negative values and possible duplicates.
+  std::set<std::uint32_t> acl_axes;
+  const int ifm_rank = _ctx.at(ifm_index).shape().rank();
+  for (int axis : axes)
   {
-    const auto frontend_layout = _current_subg_layout;
-    const auto backend_layout = ifm_alloc->layout();
-    switch (axis_shape.rank())
-    {
-      case 0: // scalar
-      {
-        auto axis_value = _ctx.at(axis_index).asScalar<int32_t>();
-        if (axis_value < 0)
-        {
-          axis_value += ifm_rank;
-        }
-        axes.insert(::neurun::backend::acl_common::ToARMComputeAxis(ifm_rank, axis_value,
-                                                                    frontend_layout, backend_layout)
-                        .value());
-        break;
-      }
-      case 1: // vector
-      {
-        const auto axis_base = _ctx.at(axis_index).data().base();
-        const int axis_size = axis_shape.num_elements();
-
-        // If axis's data does not exist as constant values and can be gotten as input data, we have
-        // to find a way to infer output shape when sinking output.
-        assert(axis_base != nullptr);
-        for (int32_t n = 0; n < axis_size; ++n)
-        {
-          int32_t axis_value = *(reinterpret_cast<const int32_t *>(axis_base) + n);
-          if (axis_value < 0)
-          {
-            axis_value += ifm_rank;
-          }
-          axes.insert(::neurun::backend::acl_common::ToARMComputeAxis(
-                          ifm_rank, axis_value, frontend_layout, backend_layout)
-                          .value());
-        }
-        break;
-      }
-      default:
-        throw std::runtime_error("Not supported");
-        break;
-    }
+    if (axis < 0)
+      axis += ifm_rank;
+    acl_axes.insert(
+        acl_common::ToARMComputeAxis(ifm_rank, axis, frontend_layout, backend_layout).value());
   }
+
   arm_compute::Coordinates reduce_axes;
-  for (const auto &a : axes)
+  for (const auto axis : acl_axes)
   {
-    reduce_axes.set(reduce_axes.num_dimensions(), a);
+    reduce_axes.set(reduce_axes.num_dimensions(), axis);
   }
+
   auto fn = nnfw::cpp14::make_unique<::arm_compute::NEReduceOperation>();
 
   fn->configure(ifm_alloc->handle(), reduce_axes, false, ofm_alloc->handle(),
@@ -1428,34 +1325,28 @@ void KernelGenerator::visit(const model::operation::ReduceSum &node)
 {
   const auto output_index{node.getOutputs().at(0)};
   const auto input_index{node.getInputs().at(model::operation::ReduceSum::Input::INPUT)};
-  const auto axis_index{node.param().axis_index};
-
-  const auto axis_base = _ctx.at(axis_index).data().base();
-  const auto axis_size = _ctx.at(axis_index).shape().num_elements();
-  const auto input_rank = _ctx.at(input_index).shape().rank();
+  const auto &axes{_ctx.at(node.param().axis_index).asVector<int>()};
 
   auto output_alloc = _tensor_builder->at(output_index).get();
   auto input_alloc = _tensor_builder->at(input_index).get();
   const auto frontend_layout = _current_subg_layout;
   const auto backend_layout = input_alloc->layout();
-  // The axis's data must exist as constant values
-  assert(axis_base != nullptr);
-  std::set<int32_t> axes;
-  for (size_t n = 0; n < axis_size; ++n)
+
+  // Convert to ACL axes taking into account negative values and possible duplicates.
+  std::set<std::uint32_t> acl_axes;
+  const int input_rank = _ctx.at(input_index).shape().rank();
+  for (int axis : axes)
   {
-    int32_t axis_value = *(reinterpret_cast<const int32_t *>(axis_base) + n);
-    if (axis_value < 0)
-    {
-      axis_value += input_rank;
-    }
-    axes.insert(::neurun::backend::acl_common::ToARMComputeAxis(input_rank, axis_value,
-                                                                frontend_layout, backend_layout)
-                    .value());
+    if (axis < 0)
+      axis += input_rank;
+    acl_axes.insert(
+        acl_common::ToARMComputeAxis(input_rank, axis, frontend_layout, backend_layout).value());
   }
+
   arm_compute::Coordinates fixed_axes;
-  for (const auto &a : axes)
+  for (const auto axis : acl_axes)
   {
-    fixed_axes.set(fixed_axes.num_dimensions(), a);
+    fixed_axes.set(fixed_axes.num_dimensions(), axis);
   }
 
   auto fn = nnfw::cpp14::make_unique<::arm_compute::NEReduceSum>();