forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JIT][SR] Introduce prim::IfThenElse (pytorch#72587)
Summary: Pull Request resolved: pytorch#72587 This pattern frequently appears in a few graphs: ``` %result = prim::If(%condition) block0(): -> (%a) block1(): -> (%b) ``` This is slow, particularly in static runtime. Static runtime creates memory planners/block runners for each sub-block, which eats up a lot of memory and introduces a lot of extra overhead for this relatively simple operation. This diff introduces a new op that replaces nodes like the above with a single op meant to act like a ternary operator: ``` %result = prim::IfThenElse(%condition, %a, %b) ``` Test Plan: New unit tests Reviewed By: eellison Differential Revision: D34091789 fbshipit-source-id: eb6a8c460c39b4c019a1f4ab1f3f1e5b6edc400c
- Loading branch information
1 parent
b3a1923
commit 0f1b335
Showing
13 changed files
with
175 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include <gtest/gtest.h> | ||
|
||
#include <test/cpp/jit/test_utils.h> | ||
#include <torch/csrc/jit/ir/irparser.h> | ||
#include <torch/csrc/jit/passes/add_if_then_else.h> | ||
|
||
namespace torch { | ||
namespace jit { | ||
|
||
TEST(AddIfThenElseOpTest, AddIfThenElseOpSimple) { | ||
const auto src = R"IR( | ||
graph(%cond: bool, %a: Tensor, %b: Tensor): | ||
%result: Tensor = prim::If(%cond) | ||
block0(): | ||
-> (%a) | ||
block1(): | ||
-> (%b) | ||
return (%result) | ||
)IR"; | ||
|
||
auto graph = std::make_shared<Graph>(); | ||
parseIR(src, graph.get()); | ||
EXPECT_TRUE(AddIfThenElseOp(graph)); | ||
|
||
testing::FileCheck() | ||
.check_count("= prim::IfThenElse", 1, /*exactly*/ true) | ||
->check_count("= prim::If", 0, /*exactly*/ true) | ||
->run(*graph); | ||
} | ||
|
||
TEST(AddIfThenElseOpTest, NoIfThenElseOpMultipleOutputs) { | ||
const auto src = R"IR( | ||
graph(%cond: bool, %a: Tensor, %b: Tensor): | ||
%result1: Tensor, %result2: Tensor = prim::If(%cond) | ||
block0(): | ||
-> (%a, %b) | ||
block1(): | ||
-> (%b, %a) | ||
return (%result1, %result2) | ||
)IR"; | ||
|
||
auto graph = std::make_shared<Graph>(); | ||
parseIR(src, graph.get()); | ||
EXPECT_FALSE(AddIfThenElseOp(graph)); | ||
|
||
testing::FileCheck() | ||
.check_count("= prim::IfThenElse", 0, /*exactly*/ true) | ||
->check_count("= prim::If", 1, /*exactly*/ true) | ||
->run(*graph); | ||
} | ||
|
||
} // namespace jit | ||
} // namespace torch |
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,55 @@ | ||
#include <torch/csrc/jit/passes/add_if_then_else.h> | ||
#include <torch/csrc/jit/runtime/graph_iterator.h> | ||
|
||
namespace torch { | ||
namespace jit { | ||
|
||
namespace { | ||
|
||
bool hasNoNodes(Block* block) { | ||
auto nodes = block->nodes(); | ||
return nodes.begin() == nodes.end(); | ||
} | ||
|
||
bool hasTrivialSubBlocks(Node* node) { | ||
const auto blocks = node->blocks(); | ||
DCHECK_EQ(blocks.size(), 2); | ||
|
||
return hasNoNodes(blocks[0]) && hasNoNodes(blocks[1]); | ||
} | ||
|
||
} // namespace | ||
|
||
bool AddIfThenElseOp(std::shared_ptr<Graph>& graph) { | ||
std::vector<Node*> to_replace; | ||
DepthFirstGraphNodeIterator graph_it(graph); | ||
for (auto* node = graph_it.next(); node != nullptr; node = graph_it.next()) { | ||
if (node->kind() != prim::If) { | ||
continue; | ||
} | ||
if (node->outputs().size() != 1) { | ||
continue; | ||
} | ||
if (hasTrivialSubBlocks(node)) { | ||
to_replace.push_back(node); | ||
} | ||
} | ||
|
||
for (auto* node : to_replace) { | ||
auto* if_then_else_node = graph->create(prim::IfThenElse, 1); | ||
if_then_else_node->addInput(node->input()); | ||
auto blocks = node->blocks(); | ||
if_then_else_node->addInput(blocks[0]->return_node()->input()); | ||
if_then_else_node->addInput(blocks[1]->return_node()->input()); | ||
|
||
if_then_else_node->insertBefore(node); | ||
if_then_else_node->output()->copyMetadata(node->output()); | ||
|
||
node->output()->replaceAllUsesWith(if_then_else_node->output()); | ||
node->destroy(); | ||
} | ||
return !to_replace.empty(); | ||
} | ||
|
||
} // namespace jit | ||
} // namespace torch |
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,11 @@ | ||
#pragma once | ||
|
||
#include <torch/csrc/jit/ir/ir.h> | ||
|
||
namespace torch { | ||
namespace jit { | ||
|
||
TORCH_API bool AddIfThenElseOp(std::shared_ptr<Graph>& graph); | ||
|
||
} // namespace jit | ||
} // namespace torch |
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
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