From: 장지섭/On-Device Lab(SR)/Engineer/삼성전자 Date: Fri, 30 Aug 2019 09:41:08 +0000 (+0900) Subject: Make ConcatLayers support NCHW frontend layout (#7063) X-Git-Tag: accepted/tizen/unified/20190903.052428~25 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=dac7521b952fe9be6e054a735b5015287f202071;p=platform%2Fcore%2Fml%2Fnnfw.git Make ConcatLayers support NCHW frontend layout (#7063) This commit makes ConcatLayers support NCHW frontend layout. Signed-off-by: jiseob.jang --- diff --git a/runtimes/neurun/backend/acl_cl/KernelGenerator.cc b/runtimes/neurun/backend/acl_cl/KernelGenerator.cc index 27a19ae..2b0d901 100644 --- a/runtimes/neurun/backend/acl_cl/KernelGenerator.cc +++ b/runtimes/neurun/backend/acl_cl/KernelGenerator.cc @@ -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)); diff --git a/runtimes/neurun/backend/acl_cl/kernel/ConcatLayer.cc b/runtimes/neurun/backend/acl_cl/kernel/ConcatLayer.cc index bc9a101..aa1fd9a 100644 --- a/runtimes/neurun/backend/acl_cl/kernel/ConcatLayer.cc +++ b/runtimes/neurun/backend/acl_cl/kernel/ConcatLayer.cc @@ -18,8 +18,6 @@ #include -#include - #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(); } diff --git a/runtimes/neurun/backend/acl_neon/KernelGenerator.cc b/runtimes/neurun/backend/acl_neon/KernelGenerator.cc index 308a962..a3d627b 100644 --- a/runtimes/neurun/backend/acl_neon/KernelGenerator.cc +++ b/runtimes/neurun/backend/acl_neon/KernelGenerator.cc @@ -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)); diff --git a/runtimes/neurun/backend/acl_neon/kernel/ConcatLayer.cc b/runtimes/neurun/backend/acl_neon/kernel/ConcatLayer.cc index 8fab277..f2d88fa 100644 --- a/runtimes/neurun/backend/acl_neon/kernel/ConcatLayer.cc +++ b/runtimes/neurun/backend/acl_neon/kernel/ConcatLayer.cc @@ -16,9 +16,6 @@ #include "ConcatLayer.h" -#include - -#include "util/feature/nchw/View.h" #include "util/logging.h" namespace @@ -82,8 +79,6 @@ template bool ConcatLayer::concatenate() { uint32_t axis_offset = 0; - util::feature::nchw::View 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(); } diff --git a/runtimes/neurun/backend/cpu/KernelGenerator.cc b/runtimes/neurun/backend/cpu/KernelGenerator.cc index f809daf..b7ce8a2 100644 --- a/runtimes/neurun/backend/cpu/KernelGenerator.cc +++ b/runtimes/neurun/backend/cpu/KernelGenerator.cc @@ -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); diff --git a/runtimes/neurun/backend/cpu/kernel/OperationUtils.h b/runtimes/neurun/backend/cpu/kernel/OperationUtils.h index 93a8f41..dc5bab9 100644 --- a/runtimes/neurun/backend/cpu/kernel/OperationUtils.h +++ b/runtimes/neurun/backend/cpu/kernel/OperationUtils.h @@ -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,