Skip to content

Commit 78c4e98

Browse files
Hao Lufacebook-github-bot
authored andcommitted
[StaticRuntime] Fusion pass for ClipRanges/GatherRanges/LengthsToOffsets
Differential Revision: D25388512 fbshipit-source-id: 078d34cf0fb79d82050cfd00b2dba0bbb8bb3f2c
1 parent 1d7ff0f commit 78c4e98

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

torch/csrc/jit/runtime/static/impl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
namespace torch {
1515
namespace jit {
16-
1716
namespace {
17+
1818
void OptimizeGraph(std::shared_ptr<torch::jit::Graph>& graph) {
1919
Inline(*graph);
2020
ConstantPropagation(graph);

torch/csrc/jit/runtime/static/passes.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,30 @@ void ConcatBatchMatMulBatchGather(std::shared_ptr<torch::jit::Graph>& graph) {
7171
fuse.runOnGraph(graph);
7272
}
7373

74+
void ClipRangesGatherRangesLengthsToOffsets(
75+
std::shared_ptr<torch::jit::Graph>& graph) {
76+
// TODO:: check restrictions for inputs; outputs not used elsewhere
77+
std::string pattern = R"IR(
78+
graph(%a, %b, %c, %d):
79+
%y0 : Tensor = fb::clip_ranges(%b, %c)
80+
%y1 : Tensor, %y2 : Tensor = fb::gather_ranges(%a, %y0)
81+
%y3 : Tensor = fb::lengths_to_offsets(%y2, %d)
82+
return (%y3, %y1))IR";
83+
std::string fused_pattern = R"IR(
84+
graph(%a, %b, %c, %d):
85+
%y0 : Tensor, %y1 : Tensor = fb::clip_ranges_gather_lengths_to_offsets(%a, %b, %c, %d)
86+
return (%y1, %y0))IR";
87+
SubgraphRewriter fuse;
88+
fuse.RegisterRewritePattern(pattern, fused_pattern);
89+
fuse.runOnGraph(graph);
90+
}
91+
7492
void FuseInferenceOpsForSparseNN(std::shared_ptr<torch::jit::Graph>& graph) {
7593
#ifdef FBCODE_CAFFE2
7694
ConcatAddMulReplaceNaNClip(graph);
7795
CastedBatchOneHotLengths(graph);
7896
ConcatBatchMatMulBatchGather(graph);
97+
ClipRangesGatherRangesLengthsToOffsets(graph);
7998
#endif
8099
}
81100

0 commit comments

Comments
 (0)