| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #include "inline_block.h" |
| 16 | #include "../pass_level1.h" |
| 17 | |
| 18 | #include <set> |
| 19 | |
| 20 | #include <torch/csrc/jit/passes/quantization/helper.h> |
| 21 | #include <torch/csrc/api/include/torch/version.h> |
| 22 | |
| 23 | namespace pnnx { |
| 24 | |
| 25 | static void inlineCallTo(torch::jit::Node* to_replace, torch::jit::Function* callee) |
| 26 | { |
| 27 | torch::jit::WithInsertPoint guard(to_replace); |
| 28 | |
| 29 | std::unordered_map<torch::jit::Value*, torch::jit::Value*> value_map; |
| 30 | #if TORCH_VERSION_MAJOR >= 2 || (TORCH_VERSION_MAJOR >= 1 && TORCH_VERSION_MINOR >= 11) |
| 31 | std::vector<torch::jit::Value*> new_outputs = torch::jit::insertGraph(*to_replace->owningGraph(), *(toGraphFunction(*callee).graph()), to_replace->inputs(), value_map); |
| 32 | #else |
| 33 | std::vector<torch::jit::Value*> new_outputs = torch::jit::insertGraph(*to_replace->owningGraph(), *(callee->graph()), to_replace->inputs(), value_map); |
| 34 | #endif |
| 35 | |
| 36 | const auto& old_outputs = to_replace->outputs(); |
| 37 | for (size_t i = 0; i < old_outputs.size(); ++i) |
| 38 | { |
| 39 | new_outputs[i]->copyMetadata(old_outputs[i]); |
| 40 | |
| 41 | old_outputs[i]->replaceAllUsesWith(new_outputs[i]); |
| 42 | } |
| 43 | |
| 44 | to_replace->destroy(); |
| 45 | } |
| 46 | |
| 47 | static void inlineCalls(torch::jit::Block* block, const std::vector<std::string>& module_operators, std::set<std::string>& inlined_modules, bool inside_module_op = false) |
| 48 | { |
| 49 | for (auto it = block->nodes().begin(), end = block->nodes().end(); it != end;) |
| 50 | { |
| 51 | torch::jit::Node* n = *it++; |
| 52 | if (n->kind() == c10::prim::CallFunction) |
| 53 | { |
| 54 | auto function_constant = n->input(0)->node(); |
| 55 | auto fun_type = function_constant->output()->type()->expect<torch::jit::FunctionType>(); |
| 56 | if (!fun_type->function()->isGraphFunction()) |
| 57 | continue; |
| 58 | |
| 59 | #if TORCH_VERSION_MAJOR >= 2 || (TORCH_VERSION_MAJOR >= 1 && TORCH_VERSION_MINOR >= 11) |
| 60 | inlineCalls(toGraphFunction(*(fun_type->function())).graph()->block(), module_operators, inlined_modules, inside_module_op); |
| 61 | #else |
| 62 | inlineCalls(fun_type->function()->graph()->block(), module_operators, inlined_modules, inside_module_op); |
| 63 | #endif |
| 64 | |
| 65 | n->removeInput(0); |
| 66 | |
| 67 | fprintf(stderr, "inline function %s\n", fun_type->function()->name().c_str()); |
| 68 | |
| 69 | pnnx::inlineCallTo(n, fun_type->function()); |
| 70 | } |
| 71 | else if (n->kind() == c10::prim::CallMethod) |
| 72 | { |
| 73 | auto class_type = n->input(0)->type()->cast<torch::jit::ClassType>(); |
| 74 | if (!class_type) |
| 75 | continue; |
| 76 | |
| 77 | const std::string& function_name = n->s(torch::jit::attr::name); |
| 78 | torch::jit::Function& function = class_type->getMethod(function_name); |
| 79 | if (!function.isGraphFunction()) |
| 80 | continue; |
| 81 | |
| 82 | std::string class_type_str = torch::jit::removeTorchMangle(class_type->str()); |
| 83 | |
| 84 | std::string class_type_str_no_torch_prefix = class_type_str.substr(10); |
| 85 | |
| 86 | if (!inside_module_op) |
| 87 | { |
| 88 | if (std::find(module_operators.begin(), module_operators.end(), class_type_str_no_torch_prefix) != module_operators.end()) |
| 89 | { |
| 90 | #if TORCH_VERSION_MAJOR >= 2 || (TORCH_VERSION_MAJOR >= 1 && TORCH_VERSION_MINOR >= 11) |
| 91 | inlineCalls(toGraphFunction(function).graph()->block(), module_operators, inlined_modules, true); |
| 92 | #else |
| 93 | inlineCalls(function.graph()->block(), module_operators, inlined_modules, true); |
| 94 | #endif |
| 95 | |
| 96 | continue; |
| 97 | } |
| 98 | |
| 99 | bool skip_inline = false; |
| 100 | for (const auto& ow : get_global_pnnx_fuse_module_passes()) |
| 101 | { |
| 102 | if (class_type_str == ow->match_type_str()) |
| 103 | { |
| 104 | skip_inline = true; |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | if (skip_inline) |
| 110 | continue; |
| 111 | } |
| 112 | |
| 113 | #if TORCH_VERSION_MAJOR >= 2 || (TORCH_VERSION_MAJOR >= 1 && TORCH_VERSION_MINOR >= 11) |
| 114 | inlineCalls(toGraphFunction(function).graph()->block(), module_operators, inlined_modules, inside_module_op); |
| 115 | #else |
| 116 | inlineCalls(function.graph()->block(), module_operators, inlined_modules, inside_module_op); |
| 117 | #endif |
| 118 | |
| 119 | inlined_modules.insert(class_type_str_no_torch_prefix); |
| 120 | |
| 121 | // fprintf(stderr, "inline %s\n", class_type_str_no_torch_prefix.c_str()); |
| 122 | // fprintf(stderr, "inline method %s %s %s\n", function.name().c_str(), class_type->str().c_str(), n->input(0)->node()->s(torch::jit::attr::name).c_str()); |
| 123 | |
| 124 | pnnx::inlineCallTo(n, &function); |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | for (auto b : n->blocks()) |
| 129 | { |
| 130 | inlineCalls(b, module_operators, inlined_modules, inside_module_op); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | void inline_block(std::shared_ptr<torch::jit::Graph>& graph, const std::vector<std::string>& module_operators) |
| 137 | { |
| 138 | std::set<std::string> inlined_modules; |
| 139 | |
| 140 | inlineCalls(graph->block(), module_operators, inlined_modules); |
| 141 | |
| 142 | for (const auto& x : inlined_modules) |
| 143 | { |
| 144 | if (x == "torch.nn.modules.container.Sequential") |
| 145 | continue; |
| 146 | |
| 147 | fprintf(stderr, "inline module = %s\n", x.c_str()); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | } // namespace pnnx |