-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
on-going draft to fix S64 paddings in Pad. Signed-off-by: SaeHie Park <[email protected]>
- Loading branch information
1 parent
8a76a55
commit 9f4c80e
Showing
9 changed files
with
361 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
for (uint32_t i = 0; i < num_elements; i++) | ||
{ | ||
auto v64 = paddings->at<loco::DataType::S64>(i); | ||
auto hval = static_cast<int64_t>(std::numeric_limits<int32_t>::max()); | ||
auto lval = static_cast<int64_t>(std::numeric_limits<int32_t>::lowest()); | ||
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 | ||
} | ||
|
||
return changed; | ||
} | ||
|
||
} // namespace luci |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.