-
Notifications
You must be signed in to change notification settings - Fork 159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[luci/pass] Introduce CanonicalizePass with Pad #13503
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* 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_CANONICALIZE_PASS_H__ | ||
#define __LUCI_CANONICALIZE_PASS_H__ | ||
|
||
#include <logo/Pass.h> | ||
|
||
namespace luci | ||
{ | ||
|
||
/** | ||
* @brief Class to canoncalize CircleNodes | ||
* | ||
*/ | ||
struct CanonicalizePass final : public logo::Pass | ||
{ | ||
const char *name(void) const final { return "luci::CanonicalizePass"; } | ||
|
||
bool run(loco::Graph *g) final; | ||
}; | ||
|
||
} // namespace luci | ||
|
||
#endif // __LUCI_CANONICALIZE_PASS_H__ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
#include "luci/Pass/CanonicalizePass.h" | ||
|
||
#include <luci/IR/CircleNodes.h> | ||
#include <luci/Profile/CircleNodeOrigin.h> | ||
|
||
#include <loco/IR/DataType.h> | ||
|
||
#include <limits> | ||
|
||
#define CHECK_OR_FALSE(condition) \ | ||
if (not(condition)) \ | ||
return false; | ||
|
||
namespace | ||
{ | ||
|
||
/** | ||
* Convert S64 CircleConst paddings to S32 | ||
*/ | ||
bool paddings_to_s32(luci::CirclePad *pad) | ||
{ | ||
// check conditions | ||
auto paddings = dynamic_cast<luci::CircleConst *>(pad->paddings()); | ||
CHECK_OR_FALSE(paddings); | ||
CHECK_OR_FALSE(paddings->dtype() == loco::DataType::S64); | ||
|
||
// TODO relocate to helpers/CreateCircleConst.h when necessary | ||
auto num_elements = paddings->size<loco::DataType::S64>(); | ||
auto hval = static_cast<int64_t>(std::numeric_limits<int32_t>::max()); | ||
auto lval = static_cast<int64_t>(std::numeric_limits<int32_t>::lowest()); | ||
for (uint32_t i = 0; i < num_elements; i++) | ||
{ | ||
auto v64 = paddings->at<loco::DataType::S64>(i); | ||
CHECK_OR_FALSE(v64 <= hval); | ||
CHECK_OR_FALSE(v64 >= lval); | ||
} | ||
|
||
auto paddings_s32 = pad->graph()->nodes()->create<luci::CircleConst>(); | ||
paddings_s32->name(paddings->name() + "_S32"); | ||
paddings_s32->dtype(loco::DataType::S32); | ||
paddings_s32->rank(paddings->rank()); | ||
for (uint32_t i = 0; i < paddings->rank(); i++) | ||
paddings_s32->dim(i).set(paddings->dim(i).value()); | ||
paddings_s32->shape_status(luci::ShapeStatus::VALID); | ||
luci::add_origin(paddings_s32, luci::get_origin(paddings)); | ||
|
||
paddings_s32->size<loco::DataType::S32>(num_elements); | ||
for (uint32_t i = 0; i < num_elements; i++) | ||
{ | ||
auto v64 = paddings->at<loco::DataType::S64>(i); | ||
paddings_s32->at<loco::DataType::S32>(i) = static_cast<int32_t>(v64); | ||
} | ||
|
||
// replace paddings with S32 dtype | ||
pad->paddings(paddings_s32); | ||
|
||
return true; | ||
} | ||
|
||
} // namespace | ||
|
||
namespace luci | ||
{ | ||
|
||
/** | ||
* Canonicalize circle nodes | ||
*/ | ||
bool CanonicalizePass::run(loco::Graph *g) | ||
{ | ||
bool changed = false; | ||
for (auto node : loco::active_nodes(loco::output_nodes(g))) | ||
{ | ||
if (auto pad = dynamic_cast<luci::CirclePad *>(node)) | ||
{ | ||
if (paddings_to_s32(pad)) | ||
changed = true; | ||
} | ||
|
||
// TODO add more canonicalization | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Putting all canonicalization tasks in a single pass seems to make this pass to have too many responsibilities. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, its' about canonicalization. |
||
} | ||
|
||
return changed; | ||
} | ||
|
||
} // namespace luci |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
#include "luci/Pass/CanonicalizePass.h" | ||
|
||
#include <luci/IR/CircleNodes.h> | ||
|
||
#include <luci/test/TestIOGraph.h> | ||
|
||
#include <vector> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
TEST(CanonicalizePassTest, name) | ||
{ | ||
luci::CanonicalizePass pass; | ||
auto const name = pass.name(); | ||
ASSERT_NE(nullptr, name); | ||
} | ||
|
||
namespace | ||
{ | ||
|
||
using namespace luci::test; | ||
|
||
struct PadGraphlet | ||
{ | ||
PadGraphlet() = default; | ||
|
||
void init(loco::Graph *g) | ||
{ | ||
_pad = g->nodes()->create<luci::CirclePad>(); | ||
_paddings_s32 = g->nodes()->create<luci::CircleConst>(); | ||
_paddings_s64 = g->nodes()->create<luci::CircleConst>(); | ||
|
||
_pad->name("pad"); | ||
_paddings_s32->name("paddings_s32"); | ||
_paddings_s64->name("paddings_s64"); | ||
|
||
_paddings_s64->dtype(loco::DataType::S64); | ||
_paddings_s64->rank(2); | ||
_paddings_s64->dim(0).set(4); | ||
_paddings_s64->dim(1).set(2); | ||
_paddings_s64->shape_status(luci::ShapeStatus::VALID); | ||
|
||
_paddings_s32->dtype(loco::DataType::S32); | ||
_paddings_s32->rank(2); | ||
_paddings_s32->dim(0).set(4); | ||
_paddings_s32->dim(1).set(2); | ||
_paddings_s32->shape_status(luci::ShapeStatus::VALID); | ||
|
||
std::vector<int64_t> ps = {0, 0, 1, 1, 1, 1, 0, 0}; | ||
|
||
uint32_t num_elements = static_cast<uint32_t>(ps.size()); | ||
_paddings_s64->size<loco::DataType::S64>(num_elements); | ||
for (uint32_t i = 0; i < num_elements; i++) | ||
_paddings_s64->at<loco::DataType::S64>(i) = ps[i]; | ||
|
||
_paddings_s32->size<loco::DataType::S32>(num_elements); | ||
for (uint32_t i = 0; i < num_elements; i++) | ||
_paddings_s32->at<loco::DataType::S32>(i) = static_cast<int32_t>(ps[i]); | ||
} | ||
|
||
luci::CirclePad *_pad = nullptr; | ||
luci::CircleConst *_paddings_s32 = nullptr; | ||
luci::CircleConst *_paddings_s64 = nullptr; | ||
}; | ||
|
||
class CanonicalizePadTestGraph : public TestIOGraph, public PadGraphlet | ||
{ | ||
public: | ||
CanonicalizePadTestGraph() = default; | ||
|
||
void init(void) | ||
{ | ||
TestIOGraph::init({1, 3, 3, 2}, {1, 5, 5, 2}); | ||
PadGraphlet::init(g()); | ||
|
||
_pad->input(input()); | ||
_pad->paddings(_paddings_s64); | ||
|
||
output()->from(_pad); | ||
} | ||
}; | ||
|
||
} // namespace | ||
|
||
TEST(CanonicalizePassPadTest, paddings_64_to_32) | ||
{ | ||
CanonicalizePadTestGraph g; | ||
luci::CanonicalizePass pass; | ||
|
||
g.init(); | ||
|
||
luci::CircleConst *paddings = dynamic_cast<luci::CircleConst *>(g._pad->paddings()); | ||
EXPECT_NE(nullptr, paddings); | ||
EXPECT_EQ(paddings->dtype(), loco::DataType::S64); | ||
|
||
EXPECT_TRUE(pass.run(g.g())); | ||
|
||
paddings = dynamic_cast<luci::CircleConst *>(g._pad->paddings()); | ||
EXPECT_NE(nullptr, paddings); | ||
EXPECT_EQ(paddings->dtype(), loco::DataType::S32); | ||
} | ||
|
||
TEST(CanonicalizePassPadTest, paddings_32_NEG) | ||
{ | ||
CanonicalizePadTestGraph g; | ||
luci::CanonicalizePass pass; | ||
|
||
g.init(); | ||
g._pad->paddings(g._paddings_s32); | ||
|
||
luci::CircleConst *paddings = dynamic_cast<luci::CircleConst *>(g._pad->paddings()); | ||
EXPECT_NE(nullptr, paddings); | ||
EXPECT_EQ(paddings->dtype(), loco::DataType::S32); | ||
|
||
EXPECT_FALSE(pass.run(g.g())); | ||
|
||
paddings = dynamic_cast<luci::CircleConst *>(g._pad->paddings()); | ||
EXPECT_NE(nullptr, paddings); | ||
EXPECT_EQ(paddings->dtype(), loco::DataType::S32); | ||
} | ||
|
||
TEST(CanonicalizePassPadTest, paddings_32_over_NEG) | ||
{ | ||
CanonicalizePadTestGraph g; | ||
luci::CanonicalizePass pass; | ||
|
||
g.init(); | ||
g._paddings_s64->at<loco::DataType::S64>(2) = | ||
static_cast<int64_t>(std::numeric_limits<int32_t>::max()) + 100; | ||
|
||
EXPECT_FALSE(pass.run(g.g())); | ||
|
||
luci::CircleConst *paddings = dynamic_cast<luci::CircleConst *>(g._pad->paddings()); | ||
EXPECT_NE(nullptr, paddings); | ||
EXPECT_EQ(paddings->dtype(), loco::DataType::S64); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This pass name seems too general. This name more fits a stage, i.e., a sequence of passes (PTAL https://github.com/Samsung/ONE/pull/13503/files#r1690670613).
How about renaming to CanonicalizePaddingDtypePass, or simply ConvertS64PaddingToS32Pass ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Named this pass to do some multiple things as canonicalization.
This pass will run always without option.