|
| 1 | +#include "torch/csrc/jit/passes/guard_elimination.h" |
| 2 | +#include "torch/csrc/jit/ir/alias_analysis.h" |
| 3 | +#include "torch/csrc/jit/jit_log.h" |
| 4 | +#include "torch/csrc/jit/passes/constant_propagation.h" |
| 5 | +#include "torch/csrc/jit/passes/peephole.h" |
| 6 | +#include "torch/csrc/jit/runtime/graph_executor.h" |
| 7 | +#include "torch/csrc/jit/passes/dead_code_elimination.h" |
| 8 | + |
| 9 | +#include "core/util/prelude.h" |
| 10 | + |
| 11 | +#include <vector> |
| 12 | + |
| 13 | +namespace trtorch { |
| 14 | +namespace core { |
| 15 | +namespace lowering { |
| 16 | +namespace passes { |
| 17 | +namespace { |
| 18 | +using namespace torch::jit; |
| 19 | +struct AddMMBranchFusion { |
| 20 | + AddMMBranchFusion(std::shared_ptr<Graph> graph) |
| 21 | + : graph_(std::move(graph)) {} |
| 22 | + |
| 23 | + void run() { |
| 24 | + findAddMMVariantsNodes(graph_->block()); |
| 25 | + torch::jit::EliminateDeadCode(graph_); |
| 26 | + LOG_GRAPH("Post aten::addmm branch fusion: " << *graph_); |
| 27 | + } |
| 28 | + |
| 29 | +private: |
| 30 | + bool isAddMMVariantsNode(Node* n) { |
| 31 | + /// Check if this Node hosts a pattern like so: |
| 32 | + /// %ret : Tensor = prim::If(%622) |
| 33 | + /// block0(): |
| 34 | + /// %ret.1 : Tensor = aten::addmm(%self.fc.bias, %x9.1, %3677, %3, %3) |
| 35 | + /// -> (%ret.1) |
| 36 | + /// block1(): |
| 37 | + /// %output.1 : Tensor = aten::matmul(%x9.1, %3677) |
| 38 | + /// %output0.1 : Tensor = aten::add_(%output.1, %self.fc.bias, %3) |
| 39 | + /// -> (%output0.1) |
| 40 | + |
| 41 | + if (n->blocks().size() != 2) { |
| 42 | + return false; |
| 43 | + } |
| 44 | + auto arm1 = n->blocks()[0]; |
| 45 | + auto arm2 = n->blocks()[1]; |
| 46 | + |
| 47 | + auto arm1_start = arm1->nodes().begin(); |
| 48 | + if ((*arm1_start)->kind().toQualString() != std::string("aten::addmm") |
| 49 | + && (*(++arm1_start))->kind() != prim::Return) { |
| 50 | + // Make sure that block0 is solely just the aten::addmm op and the return |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + auto arm2_start = arm2->nodes().begin(); |
| 55 | + if ((*arm2_start)->kind().toQualString() != std::string("aten::matmul") |
| 56 | + && (*(++arm2_start))->kind().toQualString() != std::string("aten::add_") |
| 57 | + && (*(++arm2_start))->kind() != prim::Return) { |
| 58 | + // Make sure that block1 is solely the return |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + return true; |
| 63 | + } |
| 64 | + |
| 65 | + void findAddMMVariantsNodes(Block* b) { |
| 66 | + for (auto it = b->nodes().begin(); it != b->nodes().end(); it++) { |
| 67 | + auto n = *it; |
| 68 | + if (n->kind() == prim::If && isAddMMVariantsNode(n)) { |
| 69 | + LOG_GRAPH("Found that node " << *n << " is an AddMM variants node (FuseAddMMBranches)" << std::endl); |
| 70 | + auto arm1 = n->blocks()[0]; |
| 71 | + auto arm1_start = arm1->nodes().begin(); |
| 72 | + |
| 73 | + auto input_values = (*arm1_start)->inputs(); |
| 74 | + |
| 75 | + auto new_addmm_node = b->owningGraph()->create(c10::Symbol::fromQualString("aten::addmm"), input_values, 1); |
| 76 | + n->replaceAllUsesWith(new_addmm_node); |
| 77 | + |
| 78 | + auto old_insert_point = b->owningGraph()->insertPoint(); |
| 79 | + b->owningGraph()->setInsertPoint(n); |
| 80 | + b->owningGraph()->insertNode(new_addmm_node); |
| 81 | + b->owningGraph()->setInsertPoint(old_insert_point); |
| 82 | + |
| 83 | + it.destroyCurrent(); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + std::shared_ptr<Graph> graph_; |
| 89 | +}; |
| 90 | +} // namespace |
| 91 | + |
| 92 | +void FuseAddMMBranches(std::shared_ptr<Graph> graph) { |
| 93 | + AddMMBranchFusion ammbf(std::move(graph)); |
| 94 | + ammbf.run(); |
| 95 | +} |
| 96 | + |
| 97 | +} // namespace passes |
| 98 | +} // namespace lowering |
| 99 | +} // namespace core |
| 100 | +} // namespace trtorch |
0 commit comments