Skip to content

Support aten::hardswish using unpack_hardswish pass #500

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 2 commits into from
Jun 24, 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 @@ -25,6 +25,7 @@ void LowerBlock(torch::jit::Block* b) {
}

void LowerGraph(std::shared_ptr<torch::jit::Graph>& g) {
passes::UnpackHardSwish(g);
torch::jit::EliminateRedundantGuards(g);
torch::jit::RemoveListMutation(g);
torch::jit::RemoveTensorMutation(g);
Expand Down
1 change: 1 addition & 0 deletions core/lowering/passes/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ cc_library(
"unpack_addmm.cpp",
"unpack_batch_norm.cpp",
"unpack_log_softmax.cpp",
"unpack_hardswish.cpp"
],
hdrs = [
"passes.h",
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 @@ -21,6 +21,7 @@ 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);
void UnpackHardSwish(std::shared_ptr<torch::jit::Graph>& graph);

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

#include "core/util/prelude.h"

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

void UnpackHardSwish(std::shared_ptr<torch::jit::Graph>& graph) {
std::string hardswish_pattern = R"IR(
graph(%input):
%result = aten::hardswish(%input)
return (%result))IR";

std::string hardswish_pattern_inplace = R"IR(
graph(%input):
%result = aten::hardswish_(%input)
return (%result))IR";

std::string new_pattern = R"IR(
graph(%input):
%1 : Scalar = prim::Constant[value=3.]()
%2 : Scalar = prim::Constant[value=1.]()
%3 = aten::add(%input, %1, %2)
%4 : Scalar = prim::Constant[value=0.]()
%5 : Scalar = prim::Constant[value=6.]()
%6 = aten::hardtanh(%3, %4, %5)
%7 = aten::div(%6, %5)
%8 = aten::mul(%input, %7)
return (%8))IR";

torch::jit::SubgraphRewriter rewriter;
rewriter.RegisterRewritePattern(hardswish_pattern, new_pattern);
rewriter.RegisterRewritePattern(hardswish_pattern_inplace, new_pattern);
rewriter.runOnGraph(graph);

LOG_GRAPH("Post unpack hardswish: " << *graph);
}

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

lowering_test(
name = "test_unpack_hardswish",
)

test_suite(
name = "lowering_tests",
tests = [
Expand All @@ -44,5 +48,6 @@ test_suite(
":test_remove_detach_pass",
":test_remove_dropout_pass",
":test_remove_to",
":test_unpack_hardswish"
],
)
87 changes: 87 additions & 0 deletions tests/core/lowering/test_unpack_hardswish.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#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, UnpackHardSwish) {
std::string source_graph = R"IR(
graph(%input):
%result = aten::hardswish(%input)
return (%result))IR";

std::string target_graph = R"IR(
graph(%input):
%1 : Scalar = prim::Constant[value=3.]()
%2 : Scalar = prim::Constant[value=1.]()
%3 = aten::add(%input, %1, %2)
%4 : Scalar = prim::Constant[value=0.]()
%5 : Scalar = prim::Constant[value=6.]()
%6 = aten::hardtanh(%3, %4, %5)
%7 = aten::div(%6, %5)
%8 = aten::mul(%input, %7)
return (%8))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);

auto in = at::rand({10, 100}, {at::kCUDA});
auto sg_params = trtorch::core::conversion::get_named_params(sg->inputs(), {});
auto sg_results = trtorch::tests::util::RunGraph(sg, sg_params, {in});

trtorch::core::lowering::passes::UnpackHardSwish(sg);

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

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

in = at::clone(in);
auto tg_params = trtorch::core::conversion::get_named_params(tg->inputs(), {});
auto tg_results = trtorch::tests::util::RunGraph(tg, tg_params, {in});

ASSERT_TRUE(trtorch::tests::util::almostEqual(sg_results[0], tg_results[0], 2e-6));
}

TEST(LoweringPasses, UnpackHardInplaceSwish) {
std::string source_graph = R"IR(
graph(%input):
%result = aten::hardswish_(%input)
return (%result))IR";

std::string target_graph = R"IR(
graph(%input):
%1 : Scalar = prim::Constant[value=3.]()
%2 : Scalar = prim::Constant[value=1.]()
%3 = aten::add(%input, %1, %2)
%4 : Scalar = prim::Constant[value=0.]()
%5 : Scalar = prim::Constant[value=6.]()
%6 = aten::hardtanh(%3, %4, %5)
%7 = aten::div(%6, %5)
%8 = aten::mul(%input, %7)
return (%8))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);

auto in = at::rand({10, 100}, {at::kCUDA});
auto sg_params = trtorch::core::conversion::get_named_params(sg->inputs(), {});
auto sg_results = trtorch::tests::util::RunGraph(sg, sg_params, {in});

trtorch::core::lowering::passes::UnpackHardSwish(sg);

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

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

in = at::clone(in);
auto tg_params = trtorch::core::conversion::get_named_params(tg->inputs(), {});
auto tg_results = trtorch::tests::util::RunGraph(tg, tg_params, {in});

ASSERT_TRUE(trtorch::tests::util::almostEqual(sg_results[0], tg_results[0], 2e-6));
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also do a functional test here, to make sure the numeric results of the graph with just aten::hardswish and the unpacked graph are the same?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can probably copy the test structure we use in converter tests to do this