Skip to content

Commit 04b27d8

Browse files
committed
refactor(removeNOPs): Make node removal generic to reduce code
Signed-off-by: Naren Dasan <[email protected]> Signed-off-by: Naren Dasan <[email protected]>
1 parent 3d6b1d0 commit 04b27d8

File tree

1 file changed

+5
-15
lines changed

1 file changed

+5
-15
lines changed

core/lowering/passes/remove_nops.cpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,21 @@ struct NOPRemoval {
2020
NOPRemoval(std::shared_ptr<Graph> graph) : graph_(std::move(graph)) {}
2121

2222
void run() {
23-
removeTo(graph_->block());
24-
removeDetach(graph_->block());
23+
removeNode(graph_->block(), "aten::to");
24+
removeNode(graph_->block(), "aten::detach");
2525
torch::jit::EliminateDeadCode(graph_);
2626
LOG_DEBUG(
2727
"RemoveNOPs - Note: Removing remaining aten::to operators (in addition to other ops that have no meaning in TRT), if type casts need to be preserved, add a pass before this pass is run");
2828
LOG_GRAPH("Post aten::to removal: " << *graph_);
2929
}
3030

3131
private:
32-
void removeTo(Block* b) {
33-
for (auto it = b->nodes().begin(); it != b->nodes().end(); it++) {
34-
auto n = *it;
35-
if (n->kind() == c10::Symbol::fromQualString("aten::to")) {
36-
LOG_GRAPH("Found that node " << *n << " is an to node (RemoveNOPs)" << std::endl);
37-
n->outputs()[0]->replaceAllUsesWith(n->inputs()[0]);
38-
it.destroyCurrent();
39-
}
40-
}
41-
}
4232

43-
void removeDetach(Block* b) {
33+
void removeNode(Block* b, std::string op) {
4434
for (auto it = b->nodes().begin(); it != b->nodes().end(); it++) {
4535
auto n = *it;
46-
if (n->kind() == c10::Symbol::fromQualString("aten::detach")) {
47-
LOG_GRAPH("Found that node " << *n << " is an detach node (RemoveNOPs)" << std::endl);
36+
if (n->kind() == c10::Symbol::fromQualString(op)) {
37+
LOG_GRAPH("Found that node " << *n << " is an " << op << " node (RemoveNOPs)" << std::endl);
4838
n->outputs()[0]->replaceAllUsesWith(n->inputs()[0]);
4939
it.destroyCurrent();
5040
}

0 commit comments

Comments
 (0)