Skip to content

Commit

Permalink
[luci] move template function to new head file
Browse files Browse the repository at this point in the history
move use_paddings funtion to new .h file

ONE-DCO-1.0-Signed-off-by: JuYoung Lee [email protected]
  • Loading branch information
icodo98 committed Aug 28, 2024
1 parent b4a37f2 commit 8aa6138
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 58 deletions.
57 changes: 1 addition & 56 deletions compiler/luci/service/src/CircleShapeInferenceHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include <loco/IR/NodeShape.h>
#include <loco/IR/TensorShape.h>
#include "Check.h"

#include <luci/IR/CircleNodes.h>

namespace luci
Expand Down Expand Up @@ -87,61 +87,6 @@ class TensorShapeExpander final
const loco::TensorShape _shape;
};

template <class CIRCLENODE>
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<loco::TensorShape>();

// 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<S32>(idx + 0); // left
value += paddings->at<S32>(idx + 1); // right
}
else
{
auto pl = paddings->at<S64>(idx + 0);
auto pr = paddings->at<S64>(idx + 1);
auto max = static_cast<int64_t>(std::numeric_limits<int32_t>::max());
auto low = static_cast<int64_t>(std::numeric_limits<int32_t>::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<int32_t>(pl); // left
value += static_cast<int32_t>(pr); // right
}
output_shape.dim(ni) = value;
}

return output_shape;
}

} // namespace sinf
} // namespace luci

Expand Down
86 changes: 86 additions & 0 deletions compiler/luci/service/src/CircleShapeInferenceHelperPads.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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_CIRCLE_SHAPE_INFERENCE_HELPER_PADS_H__
#define __LUCI_CIRCLE_SHAPE_INFERENCE_HELPER_PADS_H__

#include "Check.h"

#include <loco/IR/TensorShape.h>
#include <luci/IR/CircleNodes.h>
#include <limits>

namespace luci
{
namespace sinf
{

template <class CIRCLENODE>
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<loco::TensorShape>();
// 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<S32>(idx + 0); // left
value += paddings->at<S32>(idx + 1); // right
}
else
{
auto pl = paddings->at<S64>(idx + 0);
auto pr = paddings->at<S64>(idx + 1);
auto max = static_cast<int64_t>(std::numeric_limits<int32_t>::max());
auto low = static_cast<int64_t>(std::numeric_limits<int32_t>::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<int32_t>(pl); // left
value += static_cast<int32_t>(pr); // right
}
output_shape.dim(ni) = value;
}

return output_shape;
}
} // namespace sinf
} // namespace luci

#endif
5 changes: 3 additions & 2 deletions compiler/luci/service/src/Nodes/CirclePad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
* limitations under the License.
*/

#include "luci/Service/CircleShapeInference.h"

#include "CircleCloneNode.h"
#include "CircleShapeInferenceHelper.h"

#include "luci/Service/CircleShapeInference.h"
#include "CircleShapeInferenceHelperPads.h"

namespace luci
{
Expand Down

0 comments on commit 8aa6138

Please sign in to comment.