Skip to content

Lowering pass for SiLU #352

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

Merged
merged 5 commits into from
Feb 22, 2021
Merged
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 core/lowering/lowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ void LowerGraph(std::shared_ptr<torch::jit::Graph>& g) {
passes::UnpackLogSoftmax(g);
passes::RemoveNOPs(g);
passes::AliasOperators(g);
passes::SiluToSigmoidMultipication(g);
torch::jit::EliminateDeadCode(g);
LOG_GRAPH(*g);
}
Expand Down
3 changes: 2 additions & 1 deletion core/lowering/passes/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ cc_library(
"unpack_addmm.cpp",
"unpack_batch_norm.cpp",
"unpack_log_softmax.cpp",
"op_aliasing.cpp"
"op_aliasing.cpp",
"silu_to_sigmoid_multiplication.cpp"
],
deps = [
"//core/util:prelude",
Expand Down
1 change: 1 addition & 0 deletions core/lowering/passes/passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ void UnpackAddMM(std::shared_ptr<torch::jit::Graph>& graph);
void UnpackBatchNorm(std::shared_ptr<torch::jit::Graph>& graph);
void UnpackLogSoftmax(std::shared_ptr<torch::jit::Graph>& graph);
void AliasOperators(std::shared_ptr<torch::jit::Graph>& graph);
void SiluToSigmoidMultipication(std::shared_ptr<torch::jit::Graph>& graph);

} // namespace passes
} // namespace lowering
Expand Down
31 changes: 31 additions & 0 deletions core/lowering/passes/silu_to_sigmoid_multiplication.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <torch/csrc/jit/passes/subgraph_rewrite.h>

#include "core/util/prelude.h"

namespace trtorch {
namespace core {
namespace lowering {
namespace passes {

void SiluToSigmoidMultipication(std::shared_ptr<torch::jit::Graph>& graph) {
std::string silu_pattern = R"IR(
graph(%x):
%1 : Tensor = aten::silu(%x)
return (%1))IR";
std::string sigmoid_multiplication_pattern = R"IR(
graph(%x):
%1 : Tensor = aten::sigmoid(%x)
%2 : Tensor = aten::mul(%x, %1)
return (%2))IR";
;

torch::jit::SubgraphRewriter map_silu_to_sigmoid_multiplication;
map_silu_to_sigmoid_multiplication.RegisterRewritePattern(silu_pattern, sigmoid_multiplication_pattern);
map_silu_to_sigmoid_multiplication.runOnGraph(graph);
LOG_GRAPH("Post map silu -> x * sigmoid(x): " << *graph);
}

} // namespace passes
} // namespace lowering
} // namespace core
} // namespace trtorch
4 changes: 4 additions & 0 deletions tests/core/lowering/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ lowering_test(
name = "test_operator_aliasing_pass",
)

lowering_test(
name = "test_silu_to_sigmoid_multiplication",
)

test_suite(
name = "lowering_tests",
tests = [
Expand Down
29 changes: 29 additions & 0 deletions tests/core/lowering/test_silu_to_sigmoid_multiplication.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <string>
#include "core/compiler.h"
#include "core/lowering/passes/passes.h"
#include "gtest/gtest.h"
#include "tests/util/util.h"
#include "torch/csrc/jit/ir/irparser.h"
#include "torch/csrc/jit/ir/subgraph_matcher.h"

TEST(LoweringPasses, RemoveSiluLowersCorrectly) {
std::string source_graph = R"IR(
graph(%x.1 : Tensor):
%2 : Tensor = aten::silu(%x.1)
return (%2))IR";
std::string target_graph = R"IR(
graph(%x.1):
%2 : Tensor = aten::sigmoid(%x.1)
%3 : Tensor = aten::mul(%x.1, %2)
return (%3))IR";

trtorch::core::util::logging::get_logger().set_reportable_log_level(trtorch::core::util::logging::LogLevel::kGRAPH);
auto sg = std::make_shared<torch::jit::Graph>();
torch::jit::parseIR(source_graph, &*sg);
trtorch::core::lowering::passes::SiluToSigmoidMultipication(sg);

auto tg = std::make_shared<torch::jit::Graph>();
torch::jit::parseIR(target_graph, &*tg);

ASSERT_TRUE(!torch::jit::findPatternMatches(*tg, *sg).empty());
}