-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement fast approximation of Gelu as lowering pass to improv…
…e performance Signed-off-by: Dheeraj Peri <peri.dheeraj@gmail.com> Signed-off-by: Dheeraj Peri <peri.dheeraj@gmail.com> Signed-off-by: Dheeraj Peri <peri.dheeraj@gmail.com> chore: refactor converters Signed-off-by: Dheeraj Peri <peri.dheeraj@gmail.com> chore: Upload reduce_gelu.cpp Signed-off-by: Dheeraj Peri <peri.dheeraj@gmail.com> chore: Add files Signed-off-by: Dheeraj Peri <peri.dheeraj@gmail.com>
- Loading branch information
Showing
8 changed files
with
99 additions
and
32 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
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,44 @@ | ||
#include <torch/csrc/jit/passes/subgraph_rewrite.h> | ||
#include "core/util/prelude.h" | ||
|
||
namespace torch_tensorrt { | ||
namespace core { | ||
namespace lowering { | ||
namespace passes { | ||
|
||
void ReduceGelu(std::shared_ptr<torch::jit::Graph>& graph) { | ||
std::string gelu_pattern = R"IR( | ||
graph(%x): | ||
%out : Tensor = aten::gelu(%x) | ||
return (%out))IR"; | ||
|
||
std::string gelu_reduce_pattern = R"IR( | ||
graph(%x.1 : Tensor): | ||
%6 : float = prim::Constant[value=0.044714999999999998]() | ||
%5 : float = prim::Constant[value=0.79788456080000003]() | ||
%4 : float = prim::Constant[value=1.]() | ||
%3 : float = prim::Constant[value=0.5]() | ||
%2 : int = prim::Constant[value=1]() | ||
%7 : Tensor = aten::mul(%x.1, %3) | ||
%8 : Tensor = aten::mul(%x.1, %5) | ||
%9 : Tensor = aten::mul(%x.1, %6) | ||
%10 : Tensor = aten::mul(%9, %x.1) | ||
%11 : Tensor = aten::add(%10, %4, %2) | ||
%12 : Tensor = aten::mul(%8, %11) | ||
%13 : Tensor = aten::tanh(%12) | ||
%14 : Tensor = aten::add(%13, %4, %2) | ||
%15 : Tensor = aten::mul(%7, %14) | ||
return (%15))IR"; | ||
|
||
// replace aten::gelu with pointwise operations | ||
torch::jit::SubgraphRewriter map_gelu_to_pointwise_ops; | ||
map_gelu_to_pointwise_ops.RegisterRewritePattern(gelu_pattern, gelu_reduce_pattern); | ||
map_gelu_to_pointwise_ops.runOnGraph(graph); | ||
|
||
LOG_GRAPH("Post lowering of [aten::gelu] -> " << *graph); | ||
} | ||
|
||
} // namespace passes | ||
} // namespace lowering | ||
} // namespace core | ||
} // namespace torch_tensorrt |
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,42 @@ | ||
#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, ReduceGeluCorrectly) { | ||
std::string source_graph = R"IR( | ||
graph(%x): | ||
%out : Tensor = aten::gelu(%x) | ||
return (%out))IR"; | ||
std::string target_graph = R"IR( | ||
graph(%x.1 : Tensor): | ||
%6 : float = prim::Constant[value=0.044714999999999998]() | ||
%5 : float = prim::Constant[value=0.79788456080000003]() | ||
%4 : float = prim::Constant[value=1.]() | ||
%3 : float = prim::Constant[value=0.5]() | ||
%2 : int = prim::Constant[value=1]() | ||
%7 : Tensor = aten::mul(%x.1, %3) | ||
%8 : Tensor = aten::mul(%x.1, %5) | ||
%9 : Tensor = aten::mul(%x.1, %6) | ||
%10 : Tensor = aten::mul(%9, %x.1) | ||
%11 : Tensor = aten::add(%10, %4, %2) | ||
%12 : Tensor = aten::mul(%8, %11) | ||
%13 : Tensor = aten::tanh(%12) | ||
%14 : Tensor = aten::add(%13, %4, %2) | ||
%15 : Tensor = aten::mul(%7, %14) | ||
return (%15))IR"; | ||
|
||
torch_tensorrt::core::util::logging::get_logger().set_reportable_log_level( | ||
torch_tensorrt::core::util::logging::LogLevel::kGRAPH); | ||
auto sg = std::make_shared<torch::jit::Graph>(); | ||
torch::jit::parseIR(source_graph, &*sg); | ||
torch_tensorrt::core::lowering::passes::ReduceGelu(sg); | ||
|
||
auto tg = std::make_shared<torch::jit::Graph>(); | ||
torch::jit::parseIR(target_graph, &*tg); | ||
|
||
ASSERT_TRUE(!torch::jit::findPatternMatches(*tg, *sg).empty()); | ||
} |