-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(//core/lowering): New freeze model pass and new exception
elimination pass Signed-off-by: Naren Dasan <[email protected]> Signed-off-by: Naren Dasan <[email protected]>
- Loading branch information
1 parent
90c44b9
commit 4acc3fd
Showing
17 changed files
with
188 additions
and
46 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
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,86 @@ | ||
#include "torch/csrc/jit/passes/guard_elimination.h" | ||
#include "torch/csrc/jit/ir/alias_analysis.h" | ||
#include "torch/csrc/jit/jit_log.h" | ||
#include "torch/csrc/jit/passes/constant_propagation.h" | ||
#include "torch/csrc/jit/passes/peephole.h" | ||
#include "torch/csrc/jit/runtime/graph_executor.h" | ||
#include "torch/csrc/jit/passes/dead_code_elimination.h" | ||
|
||
#include "core/util/prelude.h" | ||
|
||
#include <vector> | ||
|
||
namespace trtorch { | ||
namespace core { | ||
namespace lowering { | ||
namespace passes { | ||
namespace { | ||
using namespace torch::jit; | ||
struct ExceptionOrPassPatternElimination { | ||
ExceptionOrPassPatternElimination(std::shared_ptr<Graph> graph) | ||
: graph_(std::move(graph)) {} | ||
|
||
void run() { | ||
LOG_GRAPH("Pre exeception or pass elimination: " << *graph_); | ||
findExceptionOrPassNodes(graph_->block()); | ||
torch::jit::EliminateDeadCode(graph_); | ||
LOG_GRAPH("Post exeception or pass elimination: " << *graph_); | ||
} | ||
|
||
private: | ||
bool isExceptionOrPassNode(Node* n) { | ||
/// Check if this Node hosts a pattern like so: | ||
/// = prim::If(%5958) | ||
/// block0(): | ||
/// = prim::RaiseException(%45) | ||
/// -> () | ||
/// block1(): | ||
/// -> () | ||
if (n->blocks().size() != 2) { | ||
return false; | ||
} | ||
auto arm1 = n->blocks()[0]; | ||
auto arm2 = n->blocks()[1]; | ||
if (arm1->outputs().size() != 0 || arm2->outputs().size() != 0) { | ||
// Make sure that the node doesn't actually produce any Value that are used by other nodes | ||
return false; | ||
} | ||
|
||
auto arm1_start = arm1->nodes().begin(); | ||
|
||
if ((*arm1_start)->kind() != prim::RaiseException && (*(++arm1_start))->kind() != prim::Return) { | ||
// Make sure that block0 is solely just the exception and the return | ||
return false; | ||
} | ||
|
||
if ((*(arm2->nodes().begin()))->kind() != prim::Return) { | ||
// Make sure that block1 is solely the return | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
void findExceptionOrPassNodes(Block* b) { | ||
for (auto it = b->nodes().begin(); it != b->nodes().end(); it++) { | ||
auto n = *it; | ||
if (n->kind() == prim::If && isExceptionOrPassNode(n)) { | ||
LOG_GRAPH("Found that node " << *n << " is an exception or pass node (EliminateChecks)"); | ||
it.destroyCurrent(); | ||
} | ||
} | ||
} | ||
|
||
std::shared_ptr<Graph> graph_; | ||
}; | ||
} // namespace | ||
|
||
void EliminateExceptionOrPassPattern(std::shared_ptr<Graph> graph) { | ||
ExceptionOrPassPatternElimination eppe(std::move(graph)); | ||
eppe.run(); | ||
} | ||
|
||
} // namespace passes | ||
} // namespace lowering | ||
} // namespace core | ||
} // namespace trtorch |
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
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
Oops, something went wrong.