| 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 "pass_level1.h" |
| 16 | |
| 17 | #include "../utils.h" |
| 18 | |
| 19 | namespace pnnx { |
| 20 | |
| 21 | class Upsample : public FuseModulePass |
| 22 | { |
| 23 | public: |
| 24 | const char* match_type_str() const |
| 25 | { |
| 26 | return "__torch__.torch.nn.modules.upsampling.Upsample"; |
| 27 | } |
| 28 | |
| 29 | const char* type_str() const |
| 30 | { |
| 31 | return "nn.Upsample"; |
| 32 | } |
| 33 | |
| 34 | void write(Operator* op, const std::shared_ptr<torch::jit::Graph>& graph) const |
| 35 | { |
| 36 | const torch::jit::Node* upsample_nearest1d = find_node_by_kind(graph, "aten::upsample_nearest1d"); |
| 37 | const torch::jit::Node* upsample_linear1d = find_node_by_kind(graph, "aten::upsample_linear1d"); |
| 38 | |
| 39 | const torch::jit::Node* upsample_nearest2d = find_node_by_kind(graph, "aten::upsample_nearest2d"); |
| 40 | const torch::jit::Node* upsample_bilinear2d = find_node_by_kind(graph, "aten::upsample_bilinear2d"); |
| 41 | const torch::jit::Node* upsample_bicubic2d = find_node_by_kind(graph, "aten::upsample_bicubic2d"); |
| 42 | |
| 43 | const torch::jit::Node* upsample_nearest3d = find_node_by_kind(graph, "aten::upsample_nearest3d"); |
| 44 | const torch::jit::Node* upsample_trilinear3d = find_node_by_kind(graph, "aten::upsample_trilinear3d"); |
| 45 | |
| 46 | const torch::jit::Node* upsample = 0; |
| 47 | if (upsample_nearest1d) |
| 48 | { |
| 49 | upsample = upsample_nearest1d; |
| 50 | op->params["mode"] = "nearest"; |
| 51 | } |
| 52 | else if (upsample_linear1d) |
| 53 | { |
| 54 | upsample = upsample_linear1d; |
| 55 | op->params["mode"] = "linear"; |
| 56 | } |
| 57 | else if (upsample_nearest2d) |
| 58 | { |
| 59 | upsample = upsample_nearest2d; |
| 60 | op->params["mode"] = "nearest"; |
| 61 | } |
| 62 | else if (upsample_bilinear2d) |
| 63 | { |
| 64 | upsample = upsample_bilinear2d; |
| 65 | op->params["mode"] = "bilinear"; |
| 66 | } |
| 67 | else if (upsample_bicubic2d) |
| 68 | { |
| 69 | upsample = upsample_bicubic2d; |
| 70 | op->params["mode"] = "bicubic"; |
| 71 | } |
| 72 | else if (upsample_nearest3d) |
| 73 | { |
| 74 | upsample = upsample_nearest3d; |
| 75 | op->params["mode"] = "nearest"; |
| 76 | } |
| 77 | else if (upsample_trilinear3d) |
| 78 | { |
| 79 | upsample = upsample_trilinear3d; |
| 80 | op->params["mode"] = "trilinear"; |
| 81 | } |
| 82 | |
| 83 | if (upsample->hasNamedInput("output_size")) |
| 84 | { |
| 85 | op->params["size"] = upsample->namedInput("output_size"); |
| 86 | } |
| 87 | |
| 88 | if (upsample->hasNamedInput("scale_factors")) |
| 89 | { |
| 90 | op->params["scale_factor"] = upsample->namedInput("scale_factors"); |
| 91 | } |
| 92 | |
| 93 | if (upsample->hasNamedInput("align_corners")) |
| 94 | { |
| 95 | op->params["align_corners"] = upsample->namedInput("align_corners"); |
| 96 | } |
| 97 | |
| 98 | bool recompute_scale_factor = true; |
| 99 | if (op->params.find("size") != op->params.end()) |
| 100 | { |
| 101 | if (op->params.at("size").type == 2) |
| 102 | { |
| 103 | int s = op->params.at("size").i; |
| 104 | if (s != 0) |
| 105 | { |
| 106 | recompute_scale_factor = false; |
| 107 | } |
| 108 | } |
| 109 | if (op->params.at("size").type == 5) |
| 110 | { |
| 111 | const std::vector<int>& size = op->params.at("size").ai; |
| 112 | for (auto s : size) |
| 113 | { |
| 114 | if (s != 0) |
| 115 | { |
| 116 | recompute_scale_factor = false; |
| 117 | break; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | if (op->params.find("scale_factor") != op->params.end()) |
| 123 | { |
| 124 | if (op->params.at("scale_factor").type != 0) |
| 125 | recompute_scale_factor = false; |
| 126 | } |
| 127 | |
| 128 | if (recompute_scale_factor) |
| 129 | { |
| 130 | op->params["size"] = Parameter(); |
| 131 | |
| 132 | // FIXME does this param really counts ? |
| 133 | // op->params["recompute_scale_factor"] = true; |
| 134 | |
| 135 | // resolve scale_factor in recompute scale graph |
| 136 | std::vector<float> scale_factor; |
| 137 | try |
| 138 | { |
| 139 | const torch::jit::Node* size_list = find_node_by_kind(graph, "prim::ListConstruct"); |
| 140 | for (auto x : size_list->inputs()) |
| 141 | { |
| 142 | auto scale_tensor = x->node()->inputs()[0]->node()->inputs()[0]->node()->inputs()[0]->node()->inputs()[1]->node()->inputs()[0]->node()->inputs()[0]->node(); |
| 143 | auto t = scale_tensor->t(torch::jit::attr::value); |
| 144 | float s = (float)t.item<double>(); |
| 145 | scale_factor.push_back(s); |
| 146 | } |
| 147 | |
| 148 | op->params["scale_factor"] = scale_factor; |
| 149 | } |
| 150 | catch (...) |
| 151 | { |
| 152 | fprintf(stderr, "unhandled upsample recompute_scale_factor graph"); |
| 153 | graph->dump(); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | }; |
| 158 | |
| 159 | REGISTER_GLOBAL_PNNX_FUSE_MODULE_PASS(Upsample) |
| 160 | |
| 161 | } // namespace pnnx |