Skip to content
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

[DRAFT][compiler] Introduce FuseGRUPass #13602

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/circle2circle-dredd-recipe-test/test.lst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Add(Net_Conv_Mul_003 PASS fuse_mul_with_conv)
Add(Net_Conv_PReluGraph_000 PASS fuse_prelu)
Add(Net_Conv_QuantDequant_000 PASS remove_quantdequant)
Add(Net_Conv_Relu6_000 PASS fuse_activation_function)
Add(Net_Decomposed_GRU_000 PASS fuse_gru)
Add(Net_Duplicate_Weights_000 PASS remove_duplicate_const)
Add(Net_DwConv_BN_000 PASS fuse_batchnorm_with_dwconv)
Add(Net_DwConv_BN_001 PASS fuse_batchnorm_with_dwconv)
Expand Down
6 changes: 6 additions & 0 deletions compiler/circle2circle/src/Circle2Circle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ int entry(int argc, char **argv)
"This will fuse BatchNorm operators of pre-activations to Convolution operator");
add_switch(arser, "--fuse_prelu", "This will fuse operators to PReLU operator");
add_switch(arser, "--fuse_gelu", "This will fuse operators to GeLU operator");
add_switch(arser, "--fuse_gru", "This will fuse operators to GRU operator");
add_switch(arser, "--fuse_rsqrt", "This will fuse operators to Rsqrt operator");
add_switch(arser, "--remove_duplicate_const", "This will remove all duplicate constant nodes");
add_switch(arser, "--remove_fakequant", "This will remove FakeQuant operators");
Expand Down Expand Up @@ -334,6 +335,8 @@ int entry(int argc, char **argv)
options->enable(Algorithms::FusePRelu);
if (arser.get<bool>("--fuse_gelu"))
options->enable(Algorithms::FuseGelu);
if (arser.get<bool>("--fuse_gru"))
options->enable(Algorithms::FuseGRU);
if (arser.get<bool>("--fuse_rsqrt"))
options->enable(Algorithms::FuseRsqrt);
if (arser.get<bool>("--fuse_transpose_with_mean"))
Expand Down Expand Up @@ -538,6 +541,9 @@ int entry(int argc, char **argv)
}
}

// call luci optimizations for module after optimizations for graph
optimizer.optimize(module.get());

// Export to output Circle file
luci::CircleExporter exporter;

Expand Down
7 changes: 7 additions & 0 deletions compiler/luci/lang/include/luci/IR/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ class Module final
*/
loco::Graph *graph(void) const;

/**
* @brief remove graph at index
*
* @note graph(0) is interpreted as a main graph and cannot be deleted
*/
void removeGraphByIndex(size_t idx);

/**
* @brief provide graph with an index
*
Expand Down
8 changes: 8 additions & 0 deletions compiler/luci/lang/src/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ loco::Graph *Module::graph(void) const
return graph.get();
}

void Module::removeGraphByIndex(size_t idx)
{
if (idx >= _graphs.size() or idx == 0)
throw std::invalid_argument("Module: Invalid graph index to be deleted");

_graphs.erase(_graphs.begin() + idx);
}

loco::Graph *Module::graph(size_t idx) const
{
auto &graph = _graphs.at(idx);
Expand Down
34 changes: 34 additions & 0 deletions compiler/luci/lang/src/Module.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,33 @@ TEST(ModuleTest, add)
ASSERT_EQ(g_ptr, m->graph(0));
}

TEST(ModuleTest, remove)
{
auto m = luci::make_module();
auto g1 = loco::make_graph();
auto g2 = loco::make_graph();
auto g3 = loco::make_graph();
auto g1_ptr = g1.get();
auto g2_ptr = g2.get();
auto g3_ptr = g3.get();

m->add(std::move(g1));
m->add(std::move(g2));
m->add(std::move(g3));

ASSERT_EQ(3, m->size());
ASSERT_EQ(g1_ptr, m->graph());
ASSERT_EQ(g1_ptr, m->graph(0));
ASSERT_EQ(g2_ptr, m->graph(1));
ASSERT_EQ(g3_ptr, m->graph(2));

// Let's delete graph at second position
m->removeGraphByIndex(1);
ASSERT_EQ(2, m->size());
ASSERT_EQ(g1_ptr, m->graph(0));
ASSERT_EQ(g3_ptr, m->graph(1));
}

TEST(ModuleTest, add_more)
{
auto m = luci::make_module();
Expand Down Expand Up @@ -65,6 +92,13 @@ TEST(ModuleTest, add_nullptr_NEG)
EXPECT_THROW(m->add(nullptr), std::invalid_argument);
}

TEST(ModuleTest, remove_index_overflow_NEG)
{
auto m = luci::make_module();

EXPECT_THROW(m->removeGraphByIndex(10), std::invalid_argument);
}

TEST(ModuleTest, graph_index_overflow_NEG)
{
auto m = luci::make_module();
Expand Down
1 change: 1 addition & 0 deletions compiler/luci/pass/include/luci/CircleOptimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class CircleOptimizer final
FuseActivationFunction,
FusePRelu,
FuseGelu,
FuseGRU,
FuseRsqrt,
ShuffleWeightTo16x1Float32,
RemoveRedundantTranspose,
Expand Down
45 changes: 45 additions & 0 deletions compiler/luci/pass/include/luci/Pass/EliminateDeadSubgraphPass.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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_ELIMINATE_DEAD_SUBGRAPH_PASS_H__
#define __LUCI_ELIMINATE_DEAD_SUBGRAPH_PASS_H__

#include <logo/Pass.h>
#include <luci/ModulePass.h>
#include <luci/IR/Module.h>

namespace luci
{

/**
* @brief Class to eliminate dead subgraph
*
*/
struct EliminateDeadSubgraphPass final : public luci::Pass
{
const char *name(void) const final { return "luci::EliminateDeadSubgraphPass"; }

bool run(luci::Module *m);
bool run(loco::Graph *)
{
// Do nothing
return false;
}
};

} // namespace luci

#endif // __LUCI_ELIMINATE_DEAD_SUBGRAPH_PASS_H__
39 changes: 39 additions & 0 deletions compiler/luci/pass/include/luci/Pass/FuseGRUPass.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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_FUSE_GRU_PASS_H__
#define __LUCI_FUSE_GRU_PASS_H__

#include <logo/Pass.h>

namespace luci
{

/**
* @brief Class to fuse certain pattern of subgraph into CircleGRU
*
* For detailed subgraph pattern to be fused, please check its implementation.
*/
struct FuseGRUPass final : public logo::Pass
{
const char *name(void) const final { return "luci::FuseGRUPass"; }

bool run(loco::Graph *g) final;
};

} // namespace luci

#endif // __LUCI_FUSE_GRU_PASS_H__
8 changes: 8 additions & 0 deletions compiler/luci/pass/src/CircleOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include "luci/Pass/FusePreActivationBatchNormPass.h"
#include "luci/Pass/FusePReluPass.h"
#include "luci/Pass/FuseGeluPass.h"
#include "luci/Pass/FuseGRUPass.h"
#include "luci/Pass/FuseRsqrtPass.h"
#include "luci/Pass/FuseSliceWithTConvPass.h"
#include "luci/Pass/FuseHorizontalFullyConnectedPass.h"
Expand Down Expand Up @@ -100,6 +101,7 @@

#include "luci/Pass/CircleShapeInferencePass.h"
#include "luci/Pass/CircleTypeInferencePass.h"
#include "luci/Pass/EliminateDeadSubgraphPass.h"

// logo passes
#include <logo/RemoveDeadNodeWithQueryPass.h>
Expand Down Expand Up @@ -245,6 +247,8 @@ void CircleOptimizer::optimize(luci::Module *m) const
phase.emplace_back(std::make_unique<FuseBCQPass>());
}

phase.emplace_back(std::make_unique<luci::EliminateDeadSubgraphPass>());

ModuleProgressReporter prog(m, logo::PhaseStrategy::Restart);
PhaseRunner<logo::PhaseStrategy::Restart> phase_runner{m};
phase_runner.attach(&prog);
Expand Down Expand Up @@ -370,6 +374,10 @@ void CircleOptimizer::optimize(loco::Graph *g) const
{
phase.emplace_back(std::make_unique<FuseGeluPass>());
}
if (_options->query(Options::Algorithm::FuseGRU))
{
phase.emplace_back(std::make_unique<FuseGRUPass>());
}
if (_options->query(Options::Algorithm::FuseRsqrt))
{
phase.emplace_back(std::make_unique<FuseRsqrtPass>());
Expand Down
142 changes: 142 additions & 0 deletions compiler/luci/pass/src/EliminateDeadSubgraphPass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* 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/EliminateDeadSubgraphPass.h"

#include <luci/IR/CircleNodes.h>

#include <unordered_set>
#include <deque>

namespace luci
{

namespace
{

// Go through the current graph and check all other graphs reachable from it and save it.
// Note: The main idea for finding achievable graphs is that we can reach other graphs only
// from some operations (see the list below) and we check the graph indexes from these operations.
void checkGraph(loco::Graph *current_graph, std::deque<size_t> &reachable_graphs_indexes_q)
{
assert(current_graph != nullptr);

// 1 - Obtain all active nodes in current graph
// 2 - Go through all active nodes and check its types
// 3 - If it is possible to reach another graph from the current operation (see the list below),
// then add the graph numbers to our queue

// 1 - Obtain all active nodes in current graph
// Let's enumerate nodes required to compute output nodes
auto active_nodes = loco::active_nodes(loco::output_nodes(current_graph));

// 2 - Go through all active nodes and check its types
// Nodes from we can obtain different subgraph:
// While, If, ...
// TODO: check all nodes which can be used to reach different subgraph
for (auto &node : active_nodes)
{
auto *circle_node = loco::must_cast<luci::CircleNode *>(node);

switch (circle_node->opcode())
{
case CircleOpcode::WHILE:
{
auto *while_node = loco::must_cast<luci::CircleWhile *>(circle_node);
// Get body and cond graph indexes
int32_t body_graph_index = while_node->body_branch();
int32_t cond_graph_index = while_node->cond_branch();
assert(body_graph_index >= 0);
assert(cond_graph_index >= 0);
// Add indexes into queue
reachable_graphs_indexes_q.push_back(static_cast<size_t>(body_graph_index));
reachable_graphs_indexes_q.push_back(static_cast<size_t>(cond_graph_index));
}
break;
case CircleOpcode::IF:
{
auto *if_node = loco::must_cast<luci::CircleIf *>(circle_node);
// Get then and else graph indexes
int32_t else_index = if_node->else_branch();
int32_t then_index = if_node->then_branch();
assert(else_index >= 0);
assert(then_index >= 0);
// Add indexes into queue
reachable_graphs_indexes_q.push_back(static_cast<size_t>(else_index));
reachable_graphs_indexes_q.push_back(static_cast<size_t>(then_index));
}
break;
default:
continue;
}
}
}

} // namespace

/**
* Eliminate dead subgraph.
* Note: dead means inaccessible from the main (with index zero) graph
**/
bool EliminateDeadSubgraphPass::run(luci::Module *m)
{
bool changed = false;

// Nothing check
if (m->size() == 1 or m->size() == 0)
return false;

std::unordered_set<size_t> reachable_indexes;

// Queue with reachable graphs indexes
std::deque<size_t> reachable_graphs_indexes_q;
// Insert main graph - with index zero
reachable_graphs_indexes_q.push_back(0);

while (reachable_graphs_indexes_q.empty() == false)
{
// Get first index from queue and remove it from queue
auto current_graph_index = reachable_graphs_indexes_q.front();
reachable_graphs_indexes_q.pop_front();

// If already check this graph - continue
if (reachable_indexes.find(current_graph_index) != reachable_indexes.end())
continue;

// Add current index to reachable set
reachable_indexes.insert(current_graph_index);

// Check current graph and add all graph indexes which can be reached from current graph
loco::Graph *graph = m->graph(current_graph_index);
assert(graph != nullptr);
checkGraph(graph, reachable_graphs_indexes_q);
}

assert(!reachable_indexes.empty());
// Let's remove all indexes which can not be reached from main graph
for (size_t i = 0; i < m->size(); ++i)
{
if (reachable_indexes.find(i) != reachable_indexes.end())
continue;

m->removeGraphByIndex(i);
changed = true;
}

return changed;
}

} // namespace luci
Loading
Loading