-
Notifications
You must be signed in to change notification settings - Fork 158
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] Refactor FuseAddWithTConvPass #13745
Merged
seanshpark
merged 3 commits into
Samsung:master
from
jiwaszki:jiwaszki/fix_order_fuse_add_tconv_pass
Aug 28, 2024
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright (c) 2021 Samsung Electronics Co., Ltd. All Rights Reserved | ||
* Copyright (c) 2021-2024 Samsung Electronics Co., Ltd. All Rights Reserved | ||
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. ditto |
||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
|
@@ -16,9 +16,196 @@ | |
|
||
#include "luci/Pass/FuseAddWithTConvPass.h" | ||
|
||
#include "helpers/CreateCircleConst.h" | ||
|
||
#include <luci/IR/CircleNodes.h> | ||
#include <luci/test/TestIOGraph.h> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
TEST(FuseAddWithTConvPassTest, name) | ||
#define ADD_VAL 5.0f | ||
namespace | ||
{ | ||
|
||
using namespace luci::test; | ||
|
||
/** | ||
* Graph for this test | ||
* | ||
* BEFORE (without extra_successor) | ||
* | ||
* | | ||
* [CircleConst] [CircleTransposeConv] | ||
* \ | | ||
* [CircleAdd w/ Relu] | ||
* | | ||
* | ||
* BEFORE (with extra_successor) | ||
* | ||
* | | ||
* [CircleConst] [CircleTransposeConv] | ||
* \ | | | ||
* [CircleAdd w/ Relu] [extra FC] | ||
* | | | ||
* | ||
* AFTER (if pass was successful) | ||
* | ||
* | | ||
* [CircleConst as bias] | | ||
* \ | | ||
* [CircleTransposeConv] | ||
* | | ||
* ([CircleRelu/Relu]) | ||
* | | ||
* | ||
*/ | ||
class TConvAddGraphlet | ||
{ | ||
public: | ||
void init(loco::Graph *g, luci::FusedActFunc tconv_activation, bool use_bias, | ||
bool extra_successor) | ||
{ | ||
_tconv = g->nodes()->create<luci::CircleTransposeConv>(); | ||
|
||
std::vector<float> input_sizes_val = {1, 4, 4, 1}; | ||
_tconv_i = luci::create_const_node(g, loco::DataType::FLOAT32, {4}, input_sizes_val); | ||
_tconv->inputSizes(_tconv_i); | ||
|
||
std::vector<float> filter_val(18); | ||
for (uint32_t i = 0; i < 18; i++) | ||
filter_val.at(i) = i; | ||
|
||
_tconv_f = luci::create_const_node(g, loco::DataType::FLOAT32, {1, 3, 3, 2}, filter_val); | ||
_tconv->filter(_tconv_f); | ||
|
||
if (use_bias) | ||
{ | ||
std::vector<float> bias_val(1, 3.0f); | ||
_tconv_b = luci::create_const_node(g, loco::DataType::FLOAT32, {1}, bias_val); | ||
} | ||
else | ||
{ | ||
// Create CircleOutputExclude -- no bias | ||
_tconv_b = g->nodes()->create<luci::CircleOutputExclude>(); | ||
} | ||
_tconv->bias(_tconv_b); | ||
|
||
_tconv->padding(luci::Padding::VALID); | ||
auto _stride = _tconv->stride(); | ||
_stride->w(1); | ||
_stride->h(1); | ||
_tconv->fusedActivationFunction(tconv_activation); | ||
_tconv->dtype(loco::DataType::FLOAT32); | ||
_tconv->shape({1, 4, 4, 1}); | ||
_tconv->name("tconv"); | ||
|
||
if (extra_successor) | ||
{ | ||
_extra_succ = g->nodes()->create<luci::CircleFullyConnected>(); | ||
// Set previous TConv as input to bump number of successors for it: | ||
_extra_succ->input(_tconv); | ||
std::vector<float> weights_val(8); | ||
_extra_f = luci::create_const_node(g, loco::DataType::FLOAT32, {1, 8}, weights_val); | ||
_extra_succ->weights(_extra_f); | ||
_extra_succ->bias(nullptr); | ||
_extra_succ->fusedActivationFunction(luci::FusedActFunc::NONE); | ||
_extra_succ->dtype(loco::DataType::FLOAT32); | ||
_extra_succ->shape({1, 4, 4, 1}); | ||
_extra_succ->name("extra_fc"); | ||
} | ||
|
||
std::vector<float> add_values(1, ADD_VAL); | ||
_add_c = luci::create_const_node(g, loco::DataType::FLOAT32, {1}, add_values); | ||
_add_c->name("const_c"); | ||
|
||
_add = g->nodes()->create<luci::CircleAdd>(); | ||
_add->x(_tconv); | ||
_add->y(_add_c); | ||
_add->fusedActivationFunction(luci::FusedActFunc::RELU); | ||
_add->dtype(loco::DataType::FLOAT32); | ||
_add->shape({1, 4, 4, 1}); | ||
|
||
_add->name("add"); | ||
} | ||
|
||
protected: | ||
luci::CircleTransposeConv *_tconv = nullptr; | ||
luci::CircleConst *_tconv_i = nullptr; | ||
luci::CircleConst *_tconv_f = nullptr; | ||
luci::CircleNode *_tconv_b = nullptr; | ||
luci::CircleAdd *_add = nullptr; | ||
luci::CircleConst *_add_c = nullptr; | ||
luci::CircleFullyConnected *_extra_succ = nullptr; | ||
luci::CircleConst *_extra_f = nullptr; | ||
}; | ||
|
||
class FuseAddWithTConvTestGraph : public TestIOGraph, public TConvAddGraphlet | ||
{ | ||
public: | ||
void init(luci::FusedActFunc tconv_activation, bool use_bias, bool extra_successor) | ||
{ | ||
TestIOGraph::init({1, 2, 2, 2}, {1, 4, 4, 1}); | ||
TConvAddGraphlet::init(g(), tconv_activation, use_bias, extra_successor); | ||
|
||
_tconv->outBackprop(input()); | ||
|
||
output()->from(_add); | ||
} | ||
}; | ||
|
||
class FuseAddWithTConvPassTest : public ::testing::Test | ||
{ | ||
public: | ||
FuseAddWithTConvTestGraph g; | ||
luci::FuseAddWithTConvPass pass; | ||
}; | ||
|
||
} // namespace | ||
|
||
TEST_F(FuseAddWithTConvPassTest, tconv_add_fuse) | ||
{ | ||
g.init(luci::FusedActFunc::NONE, false /* use_bias */, false /* extra_successor */); | ||
|
||
EXPECT_EQ(true, pass.run(g.g())); | ||
|
||
auto relu = dynamic_cast<luci::CircleRelu *>(g.output()->from()); | ||
EXPECT_NE(nullptr, relu); | ||
EXPECT_STREQ(relu->name().c_str(), "const_c/Relu"); | ||
|
||
auto tconv = dynamic_cast<luci::CircleTransposeConv *>(relu->features()); | ||
EXPECT_NE(nullptr, tconv); | ||
|
||
auto bias = loco::must_cast<luci::CircleConst *>(tconv->bias()); | ||
EXPECT_NE(nullptr, bias); | ||
|
||
for (uint32_t i = 0; i < bias->size<loco::DataType::FLOAT32>(); i++) | ||
{ | ||
EXPECT_EQ(ADD_VAL, bias->at<loco::DataType::FLOAT32>(i)); | ||
} | ||
} | ||
|
||
TEST_F(FuseAddWithTConvPassTest, tconv_with_bias_NEG) | ||
{ | ||
g.init(luci::FusedActFunc::NONE, true /* use_bias */, false /* extra_successor */); | ||
|
||
EXPECT_EQ(false, pass.run(g.g())); | ||
} | ||
|
||
TEST_F(FuseAddWithTConvPassTest, tconv_with_activation_NEG) | ||
{ | ||
g.init(luci::FusedActFunc::RELU, false /* use_bias */, false /* extra_successor */); | ||
|
||
EXPECT_EQ(false, pass.run(g.g())); | ||
} | ||
|
||
TEST_F(FuseAddWithTConvPassTest, tconv_with_extra_successor_NEG) | ||
{ | ||
g.init(luci::FusedActFunc::NONE, false /* use_bias */, true /* extra_successor */); | ||
|
||
EXPECT_EQ(false, pass.run(g.g())); | ||
} | ||
|
||
TEST_F(FuseAddWithTConvPassTest, name) | ||
{ | ||
luci::FuseAddWithTConvPass pass; | ||
auto const name = pass.name(); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
we don't need to do this change
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.
I wasn't sure about it as well... now I have guidance to follow. Will do!