From 39f05ee63a6d900bbd5e80b509dd99d869fbe617 Mon Sep 17 00:00:00 2001 From: icodo98 Date: Fri, 30 Aug 2024 08:26:12 +0900 Subject: [PATCH] [luci] Migrate helperPads to ShapeInferenceHelper This PR migrates heplerPads.h to CircleShapeInferenceHelper.h and CircleShapeInferenceHelper.cpp. ONE-DCO-1.0-Signed-off-by: JuYoung Lee rsb98759@gmail.com --- .../src/CircleShapeInferenceHelper.cpp | 56 ++++++++++++ .../service/src/CircleShapeInferenceHelper.h | 4 + compiler/luci/service/src/HelperPads.h | 88 ------------------- compiler/luci/service/src/Nodes/CirclePad.cpp | 4 +- 4 files changed, 62 insertions(+), 90 deletions(-) delete mode 100644 compiler/luci/service/src/HelperPads.h diff --git a/compiler/luci/service/src/CircleShapeInferenceHelper.cpp b/compiler/luci/service/src/CircleShapeInferenceHelper.cpp index 19029ee3bc0..790cba7dfb2 100644 --- a/compiler/luci/service/src/CircleShapeInferenceHelper.cpp +++ b/compiler/luci/service/src/CircleShapeInferenceHelper.cpp @@ -16,6 +16,10 @@ #include "CircleShapeInferenceHelper.h" +#include "Check.h" + +#include + #include using namespace luci::sinf; @@ -157,5 +161,57 @@ loco::TensorShape broadcast_shape(const loco::TensorShape &x, const loco::Tensor return output_shape; } +loco::TensorShape pad_shape(const loco::TensorShape &input_shape, const luci::CircleConst *paddings) +{ + const loco::DataType S32 = loco::DataType::S32; + const loco::DataType S64 = loco::DataType::S64; + + // TODO support other data type + LUCI_ASSERT(paddings->dtype() == S32 || paddings->dtype() == S64, "Support int 32/64 for now"); + LUCI_ASSERT(paddings->rank() == 2, "paddings should be rank 2"); + + int32_t n = paddings->dim(0).value(); + int32_t v = paddings->dim(1).value(); + + LUCI_ASSERT(v == 2, "paddings should be [n, 2]"); + LUCI_ASSERT(n == int32_t(input_shape.rank()), + "paddings [n, 2] should have same value of input rank"); + + loco::TensorShape output_shape; + + output_shape.rank(input_shape.rank()); + for (int32_t ni = 0; ni < n; ++ni) + { + if (not input_shape.dim(ni).known()) + { + output_shape.dim(ni).unset(); + continue; + } + int32_t idx = ni * 2; + int value = input_shape.dim(ni).value(); + if (paddings->dtype() == S32) + { + value += paddings->at(idx + 0); // left + value += paddings->at(idx + 1); // right + } + else + { + auto pl = paddings->at(idx + 0); + auto pr = paddings->at(idx + 1); + auto max = static_cast(std::numeric_limits::max()); + auto low = static_cast(std::numeric_limits::lowest()); + LUCI_ASSERT(pl <= max, "paddings is over 32 bit limit"); + LUCI_ASSERT(pl >= low, "paddings is over 32 bit limit"); + LUCI_ASSERT(pr <= max, "paddings is over 32 bit limit"); + LUCI_ASSERT(pr >= low, "paddings is over 32 bit limit"); + value += static_cast(pl); // left + value += static_cast(pr); // right + } + output_shape.dim(ni) = value; + } + + return output_shape; +} + } // namespace sinf } // namespace luci diff --git a/compiler/luci/service/src/CircleShapeInferenceHelper.h b/compiler/luci/service/src/CircleShapeInferenceHelper.h index 0d071c0505e..4961e9c40de 100644 --- a/compiler/luci/service/src/CircleShapeInferenceHelper.h +++ b/compiler/luci/service/src/CircleShapeInferenceHelper.h @@ -48,6 +48,10 @@ loco::TensorShape circle_shape(const luci::CircleNode *node); // Throw an exception if x and y are not broadcastable. loco::TensorShape broadcast_shape(const loco::TensorShape &x, const loco::TensorShape &y); +// Return shape of pad ops using paddings. +loco::TensorShape pad_shape(const loco::TensorShape &input_shape, + const luci::CircleConst *paddings); + /** * @brief Create a higher-rank TensorShape following NumPy broadcasting semantics * diff --git a/compiler/luci/service/src/HelperPads.h b/compiler/luci/service/src/HelperPads.h deleted file mode 100644 index b4c925b91be..00000000000 --- a/compiler/luci/service/src/HelperPads.h +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright (c) 2024 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 __LUCI_SERVICE_HELPER_PADS_H__ -#define __LUCI_SERVICE_HELPER_PADS_H__ - -#include "Check.h" - -#include -#include - -#include - -namespace luci -{ -namespace sinf -{ - -template -loco::TensorShape use_paddings(const CIRCLENODE *node, const luci::CircleConst *paddings) -{ - const loco::DataType S32 = loco::DataType::S32; - const loco::DataType S64 = loco::DataType::S64; - - auto input_shape = luci::shape_get(node->input()).template as(); - // TODO support other data type - LUCI_ASSERT(paddings->dtype() == S32 || paddings->dtype() == S64, "Support int 32/64 for now"); - LUCI_ASSERT(paddings->rank() == 2, "paddings should be rank 2"); - - int32_t n = paddings->dim(0).value(); - int32_t v = paddings->dim(1).value(); - - LUCI_ASSERT(v == 2, "paddings should be [n, 2]"); - LUCI_ASSERT(n == int32_t(input_shape.rank()), - "paddings [n, 2] should have same value of input rank"); - - loco::TensorShape output_shape; - - output_shape.rank(input_shape.rank()); - for (int32_t ni = 0; ni < n; ++ni) - { - if (not input_shape.dim(ni).known()) - { - output_shape.dim(ni).unset(); - continue; - } - int32_t idx = ni * 2; - int value = input_shape.dim(ni).value(); - if (paddings->dtype() == S32) - { - value += paddings->at(idx + 0); // left - value += paddings->at(idx + 1); // right - } - else - { - auto pl = paddings->at(idx + 0); - auto pr = paddings->at(idx + 1); - auto max = static_cast(std::numeric_limits::max()); - auto low = static_cast(std::numeric_limits::lowest()); - LUCI_ASSERT(pl <= max, "paddings is over 32 bit limit"); - LUCI_ASSERT(pl >= low, "paddings is over 32 bit limit"); - LUCI_ASSERT(pr <= max, "paddings is over 32 bit limit"); - LUCI_ASSERT(pr >= low, "paddings is over 32 bit limit"); - value += static_cast(pl); // left - value += static_cast(pr); // right - } - output_shape.dim(ni) = value; - } - - return output_shape; -} - -} // namespace sinf -} // namespace luci - -#endif // __LUCI_SERVICE_HELPER_PADS_H__ diff --git a/compiler/luci/service/src/Nodes/CirclePad.cpp b/compiler/luci/service/src/Nodes/CirclePad.cpp index 5212e59bdc6..c6abc9ed63d 100644 --- a/compiler/luci/service/src/Nodes/CirclePad.cpp +++ b/compiler/luci/service/src/Nodes/CirclePad.cpp @@ -18,7 +18,6 @@ #include "CircleCloneNode.h" #include "CircleShapeInferenceHelper.h" -#include "HelperPads.h" namespace luci { @@ -35,7 +34,8 @@ loco::TensorShape Algorithm::visit(const luci::CirclePad *node) { // TODO support non-const case auto paddings = loco::must_cast(node->paddings()); - return use_paddings(node, paddings); + auto input_shape = circle_shape(loco::must_cast(node->input())); + return pad_shape(input_shape, paddings); } } // namespace sinf