From 7278225b8f316fd3733770d4ad0298964e0f8cf9 Mon Sep 17 00:00:00 2001 From: Hyeongseok Oh Date: Thu, 29 Aug 2024 13:44:40 +0900 Subject: [PATCH] [onert] Remove shape conversion function duplication (#13813) This commit remove duplicated shape conversion functions and change parameter to use permute type. ONE-DCO-1.0-Signed-off-by: Hyeongseok Oh Co-authored-by: Jiyoung Giuliana Yun --- runtime/onert/core/include/ir/Shape.h | 8 ++- .../backend/builtin/kernel/PermuteLayer.cc | 9 +-- runtime/onert/core/src/exec/ExecutorBase.cc | 2 - runtime/onert/core/src/exec/ShapeConverter.cc | 60 ------------------- runtime/onert/core/src/exec/ShapeConverter.h | 39 ------------ runtime/onert/core/src/ir/Shape.cc | 24 ++++---- 6 files changed, 22 insertions(+), 120 deletions(-) delete mode 100644 runtime/onert/core/src/exec/ShapeConverter.cc delete mode 100644 runtime/onert/core/src/exec/ShapeConverter.h diff --git a/runtime/onert/core/include/ir/Shape.h b/runtime/onert/core/include/ir/Shape.h index 744a6cb7c0c..bad1905e539 100644 --- a/runtime/onert/core/include/ir/Shape.h +++ b/runtime/onert/core/include/ir/Shape.h @@ -137,7 +137,13 @@ struct Shape inline bool operator==(const Shape &lhs, const Shape &rhs) { return lhs.dims() == rhs.dims(); } inline bool operator!=(const Shape &lhs, const Shape &rhs) { return lhs.dims() != rhs.dims(); } -Shape permuteShape(const Shape &shape, Layout frontend_layout, Layout backend_layout); +/** + * @brief Converts shape when its rank is 4 + * + * @return Return a shape based on permutation type. + * If rank is not 4, input shape is returned without conversion. + */ +Shape convertShape(const Shape &shape, const PermuteType &type); /** * @brief Find out if tha rank in this shape is "maybe" unspecified. diff --git a/runtime/onert/core/src/backend/builtin/kernel/PermuteLayer.cc b/runtime/onert/core/src/backend/builtin/kernel/PermuteLayer.cc index d22229934b9..7d06af10fbe 100644 --- a/runtime/onert/core/src/backend/builtin/kernel/PermuteLayer.cc +++ b/runtime/onert/core/src/backend/builtin/kernel/PermuteLayer.cc @@ -16,8 +16,6 @@ #include "PermuteLayer.h" -#include "../../../exec/ShapeConverter.h" - #include // from @ruy namespace onert @@ -203,14 +201,14 @@ void PermuteLayer::run() { auto dst_tensor = _dst_tensors.at(i); auto src_tensor = _src_tensors.at(i); + auto permute_type = _permute_types.at(i); if (src_tensor->is_dynamic() || dst_tensor->is_dynamic()) { // getting output shape auto src_shape = src_tensor->getShape(); // set output shape and output buffer - ir::Shape new_shape = - exec::convertShape(src_shape, src_tensor->layout(), dst_tensor->layout()); + ir::Shape new_shape = ir::convertShape(src_shape, permute_type); try { @@ -227,8 +225,7 @@ void PermuteLayer::run() throw; } } - assert(exec::convertShape(src_tensor->getShape(), src_tensor->layout(), dst_tensor->layout()) == - dst_tensor->getShape()); + assert(ir::convertShape(src_tensor->getShape(), permute_type) == dst_tensor->getShape()); } assert(_src_tensors.size() == _dst_tensors.size()); assert(_src_tensors.size() == _src_tensors_offsets.size()); diff --git a/runtime/onert/core/src/exec/ExecutorBase.cc b/runtime/onert/core/src/exec/ExecutorBase.cc index 2526e4e6e4f..14149fd10b5 100644 --- a/runtime/onert/core/src/exec/ExecutorBase.cc +++ b/runtime/onert/core/src/exec/ExecutorBase.cc @@ -16,8 +16,6 @@ #include "ExecutorBase.h" -#include "ShapeConverter.h" - #include "util/ConfigSource.h" #include diff --git a/runtime/onert/core/src/exec/ShapeConverter.cc b/runtime/onert/core/src/exec/ShapeConverter.cc deleted file mode 100644 index 707aef29b30..00000000000 --- a/runtime/onert/core/src/exec/ShapeConverter.cc +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (c) 2020 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 "ShapeConverter.h" - -namespace onert -{ -namespace exec -{ - -ir::Shape convertShape(const ir::Shape &shape, ir::Layout src_layout, ir::Layout dst_layout) -{ - if (shape.rank() != 4) - return shape; - - if (src_layout == dst_layout) - return shape; - - if (src_layout == ir::Layout::NCHW && dst_layout == ir::Layout::NHWC) - { - const ir::Shape &src_NCHW = shape; - ir::Shape dst_NHWC(4); - dst_NHWC.dim(0) = src_NCHW.dim(0); // N - dst_NHWC.dim(1) = src_NCHW.dim(2); // H - dst_NHWC.dim(2) = src_NCHW.dim(3); // W - dst_NHWC.dim(3) = src_NCHW.dim(1); // C - - return dst_NHWC; - } - - if (src_layout == ir::Layout::NHWC && dst_layout == ir::Layout::NCHW) - { - const ir::Shape &src_NHWC = shape; - ir::Shape dst_NCHW(4); - dst_NCHW.dim(0) = src_NHWC.dim(0); // N - dst_NCHW.dim(1) = src_NHWC.dim(3); // C - dst_NCHW.dim(2) = src_NHWC.dim(1); // H - dst_NCHW.dim(3) = src_NHWC.dim(2); // W - - return dst_NCHW; - } - - throw std::runtime_error("Should not reach here"); -} - -} // namespace exec -} // namespace onert diff --git a/runtime/onert/core/src/exec/ShapeConverter.h b/runtime/onert/core/src/exec/ShapeConverter.h deleted file mode 100644 index 7dc7e7536f1..00000000000 --- a/runtime/onert/core/src/exec/ShapeConverter.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2020 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 __ONERT_EXEC_SHAPE_CONVERTER_H__ -#define __ONERT_EXEC_SHAPE_CONVERTER_H__ - -#include -#include - -namespace onert -{ -namespace exec -{ - -/** - * @brief Converts shape when its rank is 4 - * - * @return ir::Shape Return a shape based on dst_layout. If rank is not 4, input shape is - * returned without conversion. - */ -ir::Shape convertShape(const ir::Shape &shape, ir::Layout src_layout, ir::Layout dst_layout); - -} // namespace exec -} // namespace onert - -#endif // __ONERT_EXEC_SHAPE_CONVERTER_H__ diff --git a/runtime/onert/core/src/ir/Shape.cc b/runtime/onert/core/src/ir/Shape.cc index 1961aea5da1..ad199f2d5dd 100644 --- a/runtime/onert/core/src/ir/Shape.cc +++ b/runtime/onert/core/src/ir/Shape.cc @@ -66,29 +66,29 @@ uint64_t Shape::num_elements() const std::multiplies()); } -Shape permuteShape(const Shape &shape, Layout from, Layout to) +Shape convertShape(const Shape &shape, const PermuteType &type) { assert(shape.rank() <= Shape::kMaxRank); Shape ret{shape}; - if (from == to) - return ret; - if (shape.rank() < 4) + + if (type == ir::PermuteType::COPY || shape.rank() < 4) return ret; + // Permutation changing layout beyond 4-D is not supported yet assert(shape.rank() <= 4); - if (from == Layout::NHWC && to == Layout::NCHW) + + if (type == ir::PermuteType::NHWC_TO_NCHW) { ret.dim(1) = shape.dim(3); ret.dim(2) = shape.dim(1); ret.dim(3) = shape.dim(2); + return ret; } - else if (from == Layout::NCHW && to == Layout::NHWC) - { - ret.dim(1) = shape.dim(2); - ret.dim(2) = shape.dim(3); - ret.dim(3) = shape.dim(1); - } - // Other cases(either `from` or `to` is UNKNOWN), just return the original shape + + assert(type == ir::PermuteType::NCHW_TO_NHWC); + ret.dim(1) = shape.dim(2); + ret.dim(2) = shape.dim(3); + ret.dim(3) = shape.dim(1); return ret; }