| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2024 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 "eliminate_noop.h" |
| 16 | |
| 17 | #include <sstream> |
| 18 | #include <string> |
| 19 | #include <unordered_set> |
| 20 | |
| 21 | #include "dead_code_elimination.h" |
| 22 | |
| 23 | namespace pnnx { |
| 24 | |
| 25 | namespace onnx2pnnx { |
| 26 | |
| 27 | static onnx::ValueInfoProto* find_value_info_by_name(onnx::GraphProto* graph, const std::string& name) |
| 28 | { |
| 29 | if (name.empty()) |
| 30 | return NULL; |
| 31 | |
| 32 | // input |
| 33 | for (int i = 0; i < graph->input_size(); i++) |
| 34 | { |
| 35 | if (graph->input(i).name() == name) |
| 36 | { |
| 37 | return graph->mutable_input(i); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | // output |
| 42 | for (int i = 0; i < graph->output_size(); i++) |
| 43 | { |
| 44 | if (graph->output(i).name() == name) |
| 45 | { |
| 46 | return graph->mutable_output(i); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | for (int i = 0; i < graph->value_info_size(); i++) |
| 51 | { |
| 52 | if (graph->mutable_value_info(i)->name() == name) |
| 53 | { |
| 54 | return graph->mutable_value_info(i); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | return NULL; |
| 59 | } |
| 60 | |
| 61 | void eliminate_noop(onnx::ModelProto& model) |
| 62 | { |
| 63 | onnx::GraphProto* graph = model.mutable_graph(); |
| 64 | |
| 65 | for (int i = 0; i < graph->node_size(); i++) |
| 66 | { |
| 67 | const onnx::NodeProto& node = graph->node(i); |
| 68 | const std::string& op_type = node.op_type(); |
| 69 | |
| 70 | bool noop = false; |
| 71 | if (op_type == "Identity" || op_type == "Dropout" || op_type == "aten_copy") |
| 72 | { |
| 73 | noop = true; |
| 74 | } |
| 75 | |
| 76 | if (!noop) |
| 77 | continue; |
| 78 | |
| 79 | const std::string& input_name = node.input(0); |
| 80 | const std::string& output_name = node.output(0); |
| 81 | |
| 82 | for (int j = i + 1; j < graph->node_size(); j++) |
| 83 | { |
| 84 | onnx::NodeProto* node2 = graph->mutable_node(j); |
| 85 | |
| 86 | for (int k = 0; k < node2->input_size(); k++) |
| 87 | { |
| 88 | if (node2->input(k) == output_name) |
| 89 | { |
| 90 | node2->set_input(k, input_name); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | for (int j = 0; j < graph->output_size(); j++) |
| 96 | { |
| 97 | if (graph->output(j).name() == output_name) |
| 98 | { |
| 99 | graph->mutable_output(j)->set_name(input_name); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | onnx2pnnx::dead_code_elimination(model); |
| 105 | } |
| 106 | |
| 107 | void eliminate_noop_with_shape(onnx::ModelProto& model) |
| 108 | { |
| 109 | onnx::GraphProto* graph = model.mutable_graph(); |
| 110 | |
| 111 | for (int i = 0; i < graph->node_size(); i++) |
| 112 | { |
| 113 | const onnx::NodeProto& node = graph->node(i); |
| 114 | const std::string& op_type = node.op_type(); |
| 115 | |
| 116 | bool noop = false; |
| 117 | |
| 118 | if (op_type == "Cast") |
| 119 | { |
| 120 | onnx::ValueInfoProto* input_value = find_value_info_by_name(graph, node.input(0)); |
| 121 | onnx::ValueInfoProto* output_value = find_value_info_by_name(graph, node.output(0)); |
| 122 | |
| 123 | if (!input_value || !output_value) |
| 124 | continue; |
| 125 | |
| 126 | if (input_value->type().has_tensor_type() && output_value->type().has_tensor_type()) |
| 127 | { |
| 128 | if (input_value->type().tensor_type().elem_type() == output_value->type().tensor_type().elem_type()) |
| 129 | noop = true; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | if (op_type == "Reshape") |
| 134 | { |
| 135 | onnx::ValueInfoProto* input_value = find_value_info_by_name(graph, node.input(0)); |
| 136 | onnx::ValueInfoProto* output_value = find_value_info_by_name(graph, node.output(0)); |
| 137 | |
| 138 | if (!input_value || !output_value) |
| 139 | continue; |
| 140 | |
| 141 | if (input_value->type().has_tensor_type() && output_value->type().has_tensor_type()) |
| 142 | { |
| 143 | const onnx::TensorShapeProto& input_tsp = input_value->type().tensor_type().shape(); |
| 144 | const onnx::TensorShapeProto& output_tsp = output_value->type().tensor_type().shape(); |
| 145 | if (input_tsp.dim_size() == output_tsp.dim_size()) |
| 146 | { |
| 147 | bool is_shape_same = true; |
| 148 | |
| 149 | int dynamic_index_count = 0; |
| 150 | for (int j = 0; j < input_tsp.dim_size(); j++) |
| 151 | { |
| 152 | if (input_tsp.dim(j).has_dim_value() && output_tsp.dim(j).has_dim_value()) |
| 153 | { |
| 154 | if (input_tsp.dim(j).dim_value() != output_tsp.dim(j).dim_value()) |
| 155 | { |
| 156 | is_shape_same = false; |
| 157 | break; |
| 158 | } |
| 159 | } |
| 160 | else |
| 161 | { |
| 162 | dynamic_index_count++; |
| 163 | if (dynamic_index_count > 1) |
| 164 | { |
| 165 | is_shape_same = false; |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | if (is_shape_same) |
| 172 | noop = true; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | if (!noop) |
| 178 | continue; |
| 179 | |
| 180 | const std::string& input_name = node.input(0); |
| 181 | const std::string& output_name = node.output(0); |
| 182 | |
| 183 | for (int j = i + 1; j < graph->node_size(); j++) |
| 184 | { |
| 185 | onnx::NodeProto* node2 = graph->mutable_node(j); |
| 186 | |
| 187 | for (int k = 0; k < node2->input_size(); k++) |
| 188 | { |
| 189 | if (node2->input(k) == output_name) |
| 190 | { |
| 191 | node2->set_input(k, input_name); |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | for (int j = 0; j < graph->output_size(); j++) |
| 197 | { |
| 198 | if (graph->output(j).name() == output_name) |
| 199 | { |
| 200 | graph->mutable_output(j)->set_name(input_name); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | onnx2pnnx::dead_code_elimination(model); |
| 206 | } |
| 207 | |
| 208 | } // namespace onnx2pnnx |
| 209 | |
| 210 | } // namespace pnnx |