| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2022 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_math.h" |
| 16 | |
| 17 | #include <algorithm> |
| 18 | #include "utils.h" |
| 19 | #include "pass_level2.h" |
| 20 | #include "pass_level4/dead_code_elimination.h" |
| 21 | |
| 22 | namespace pnnx { |
| 23 | |
| 24 | static bool constant_is_all_constant(const Operator* op_constant, float vf, int vi) |
| 25 | { |
| 26 | const Parameter& param = op_constant->params.at("value"); |
| 27 | |
| 28 | if (param.type == 2) |
| 29 | { |
| 30 | if (param.i != vi) |
| 31 | return false; |
| 32 | } |
| 33 | else if (param.type == 3) |
| 34 | { |
| 35 | if (param.f != vf) |
| 36 | return false; |
| 37 | } |
| 38 | else |
| 39 | { |
| 40 | // unsupported data type |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | static bool attribute_is_all_constant(const Operator* op_attr, float vf, int vi) |
| 48 | { |
| 49 | const Attribute& attr = op_attr->attrs.begin()->second; |
| 50 | |
| 51 | if (attr.shape.empty()) |
| 52 | { |
| 53 | fprintf(stderr, "shape empty!\n"); |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | int size = attr.shape[0]; |
| 58 | for (size_t i = 1; i < attr.shape.size(); i++) |
| 59 | { |
| 60 | size *= attr.shape[i]; |
| 61 | } |
| 62 | |
| 63 | if (attr.type == 1) |
| 64 | { |
| 65 | const float* p = (const float*)attr.data.data(); |
| 66 | for (int i = 0; i < size; i++) |
| 67 | { |
| 68 | if (p[i] != vf) |
| 69 | return false; |
| 70 | } |
| 71 | } |
| 72 | else if (attr.type == 2) |
| 73 | { |
| 74 | const double* p = (const double*)attr.data.data(); |
| 75 | for (int i = 0; i < size; i++) |
| 76 | { |
| 77 | if (p[i] != vf) |
| 78 | return false; |
| 79 | } |
| 80 | } |
| 81 | else if (attr.type == 3) |
| 82 | { |
| 83 | // f16 |
| 84 | const unsigned short* p = (const unsigned short*)attr.data.data(); |
| 85 | for (int i = 0; i < size; i++) |
| 86 | { |
| 87 | if (float16_to_float32(p[i]) != vf) |
| 88 | return false; |
| 89 | } |
| 90 | } |
| 91 | else if (attr.type == 4) |
| 92 | { |
| 93 | const int* p = (const int*)attr.data.data(); |
| 94 | for (int i = 0; i < size; i++) |
| 95 | { |
| 96 | if (p[i] != vi) |
| 97 | return false; |
| 98 | } |
| 99 | } |
| 100 | else if (attr.type == 5) |
| 101 | { |
| 102 | const int64_t* p = (const int64_t*)attr.data.data(); |
| 103 | for (int i = 0; i < size; i++) |
| 104 | { |
| 105 | if (p[i] != vi) |
| 106 | return false; |
| 107 | } |
| 108 | } |
| 109 | else if (attr.type == 7) |
| 110 | { |
| 111 | const signed char* p = (const signed char*)attr.data.data(); |
| 112 | for (int i = 0; i < size; i++) |
| 113 | { |
| 114 | if (p[i] != vi) |
| 115 | return false; |
| 116 | } |
| 117 | } |
| 118 | else if (attr.type == 8) |
| 119 | { |
| 120 | const unsigned char* p = (const unsigned char*)attr.data.data(); |
| 121 | for (int i = 0; i < size; i++) |
| 122 | { |
| 123 | if (p[i] != vi) |
| 124 | return false; |
| 125 | } |
| 126 | } |
| 127 | else |
| 128 | { |
| 129 | // unsupported data type |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | static bool operator_is_all_constant(const Operator* op, float vf, int vi) |
| 137 | { |
| 138 | if (op->type == "pnnx.Attribute") |
| 139 | return attribute_is_all_constant(op, vf, vi); |
| 140 | |
| 141 | if (op->type == "prim::Constant") |
| 142 | return constant_is_all_constant(op, vf, vi); |
| 143 | |
| 144 | if (op->type == "torch.zeros" || op->type == "torch.zeros_like") |
| 145 | return (vf == 0.f && vi == 0); |
| 146 | |
| 147 | if (op->type == "torch.ones" || op->type == "torch.ones_like") |
| 148 | return (vf == 1.f && vi == 1); |
| 149 | |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | void eliminate_noop_math(Graph& graph) |
| 154 | { |
| 155 | for (;;) |
| 156 | { |
| 157 | bool need_eliminate = false; |
| 158 | |
| 159 | // build expression via reverse order |
| 160 | for (int i = (int)graph.ops.size() - 1; i >= 0; i--) |
| 161 | { |
| 162 | Operator* op = graph.ops[i]; |
| 163 | |
| 164 | int identity_input_id = 0; |
| 165 | if (op->type == "aten::add") |
| 166 | { |
| 167 | Operator* op0 = op->inputs[0]->producer; |
| 168 | Operator* op1 = op->inputs[1]->producer; |
| 169 | |
| 170 | if (op->inputs.size() == 2) |
| 171 | { |
| 172 | if (operator_is_all_constant(op0, 0.f, 0)) |
| 173 | { |
| 174 | // x <= 0 + b |
| 175 | need_eliminate = true; |
| 176 | identity_input_id = 1; |
| 177 | } |
| 178 | else if (operator_is_all_constant(op1, 0.f, 0)) |
| 179 | { |
| 180 | // x <= a + 0 |
| 181 | need_eliminate = true; |
| 182 | identity_input_id = 0; |
| 183 | } |
| 184 | } |
| 185 | else // if (op->inputs.size() == 3) |
| 186 | { |
| 187 | Operator* op2 = op->inputs[2]->producer; |
| 188 | |
| 189 | if (operator_is_all_constant(op1, 0.f, 0)) |
| 190 | { |
| 191 | // x <= a + 0 * c |
| 192 | need_eliminate = true; |
| 193 | identity_input_id = 0; |
| 194 | } |
| 195 | else if (operator_is_all_constant(op2, 0.f, 0)) |
| 196 | { |
| 197 | // x <= a + b * 0 |
| 198 | need_eliminate = true; |
| 199 | identity_input_id = 0; |
| 200 | } |
| 201 | else if (operator_is_all_constant(op0, 0.f, 0) && operator_is_all_constant(op2, 1.f, 1)) |
| 202 | { |
| 203 | // x <= 0 + b * 1 |
| 204 | need_eliminate = true; |
| 205 | identity_input_id = 1; |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | if (op->type == "aten::sub") |
| 210 | { |
| 211 | Operator* op1 = op->inputs[1]->producer; |
| 212 | |
| 213 | if (op->inputs.size() == 2) |
| 214 | { |
| 215 | if (operator_is_all_constant(op1, 0.f, 0)) |
| 216 | { |
| 217 | // x <= a - 0 |
| 218 | need_eliminate = true; |
| 219 | identity_input_id = 0; |
| 220 | } |
| 221 | } |
| 222 | else // if (op->inputs.size() == 3) |
| 223 | { |
| 224 | Operator* op2 = op->inputs[2]->producer; |
| 225 | |
| 226 | if (operator_is_all_constant(op1, 0.f, 0)) |
| 227 | { |
| 228 | // x <= a - 0 * c |
| 229 | need_eliminate = true; |
| 230 | identity_input_id = 0; |
| 231 | } |
| 232 | else if (operator_is_all_constant(op2, 0.f, 0)) |
| 233 | { |
| 234 | // x <= a - b * 0 |
| 235 | need_eliminate = true; |
| 236 | identity_input_id = 0; |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | if (op->type == "aten::rsub") |
| 241 | { |
| 242 | Operator* op0 = op->inputs[0]->producer; |
| 243 | Operator* op1 = op->inputs[1]->producer; |
| 244 | |
| 245 | if (op->inputs.size() == 2) |
| 246 | { |
| 247 | if (operator_is_all_constant(op0, 0.f, 0)) |
| 248 | { |
| 249 | // x <= b - 0 |
| 250 | need_eliminate = true; |
| 251 | identity_input_id = 1; |
| 252 | } |
| 253 | } |
| 254 | else // if (op->inputs.size() == 3) |
| 255 | { |
| 256 | Operator* op2 = op->inputs[2]->producer; |
| 257 | |
| 258 | if (operator_is_all_constant(op0, 0.f, 0) && operator_is_all_constant(op2, 1.f, 1)) |
| 259 | { |
| 260 | // x <= b * 1 - 0 |
| 261 | need_eliminate = true; |
| 262 | identity_input_id = 1; |
| 263 | } |
| 264 | else if (operator_is_all_constant(op0, 0.f, 0) && operator_is_all_constant(op1, 1.f, 1)) |
| 265 | { |
| 266 | // x <= 1 * c - 0 |
| 267 | need_eliminate = true; |
| 268 | identity_input_id = 2; |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | if (op->type == "aten::mul") |
| 273 | { |
| 274 | Operator* op0 = op->inputs[0]->producer; |
| 275 | Operator* op1 = op->inputs[1]->producer; |
| 276 | |
| 277 | if (operator_is_all_constant(op0, 1.f, 1)) |
| 278 | { |
| 279 | // x <= 1 * b |
| 280 | need_eliminate = true; |
| 281 | identity_input_id = 1; |
| 282 | } |
| 283 | if (operator_is_all_constant(op1, 1.f, 1)) |
| 284 | { |
| 285 | // x <= a * 1 |
| 286 | need_eliminate = true; |
| 287 | identity_input_id = 0; |
| 288 | } |
| 289 | } |
| 290 | if (op->type == "aten::div") |
| 291 | { |
| 292 | Operator* op1 = op->inputs[1]->producer; |
| 293 | |
| 294 | if (operator_is_all_constant(op1, 1.f, 1)) |
| 295 | { |
| 296 | // x <= a / 1 |
| 297 | need_eliminate = true; |
| 298 | identity_input_id = 0; |
| 299 | } |
| 300 | } |
| 301 | if (op->type == "aten::pow") |
| 302 | { |
| 303 | Operator* op1 = op->inputs[1]->producer; |
| 304 | |
| 305 | if (operator_is_all_constant(op1, 1.f, 1)) |
| 306 | { |
| 307 | // x <= x ^ 1 |
| 308 | need_eliminate = true; |
| 309 | identity_input_id = 0; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | // but if shape changes |
| 314 | if (need_eliminate && op->inputs[identity_input_id]->shape != op->outputs[0]->shape) |
| 315 | { |
| 316 | need_eliminate = false; |
| 317 | } |
| 318 | |
| 319 | if (!need_eliminate) |
| 320 | continue; |
| 321 | |
| 322 | // fprintf(stderr, "eliminate_noop_math %s %s\n", op->type.c_str(), op->name.c_str()); |
| 323 | |
| 324 | for (auto& x : op->inputs) |
| 325 | { |
| 326 | x->remove_consumer(op); |
| 327 | } |
| 328 | |
| 329 | Operand* math_out = op->outputs[0]; |
| 330 | |
| 331 | for (auto& x : math_out->consumers) |
| 332 | { |
| 333 | for (size_t j = 0; j < x->inputs.size(); j++) |
| 334 | { |
| 335 | if (x->inputs[j] == math_out) |
| 336 | x->inputs[j] = op->inputs[identity_input_id]; |
| 337 | } |
| 338 | |
| 339 | op->inputs[identity_input_id]->consumers.push_back(x); |
| 340 | } |
| 341 | |
| 342 | math_out->producer = 0; |
| 343 | math_out->consumers.clear(); |
| 344 | |
| 345 | graph.operands.erase(std::find(graph.operands.begin(), graph.operands.end(), math_out)); |
| 346 | delete math_out; |
| 347 | |
| 348 | op->inputs.clear(); |
| 349 | op->outputs.clear(); |
| 350 | |
| 351 | graph.ops.erase(graph.ops.begin() + i); |
| 352 | delete op; |
| 353 | |
| 354 | break; |
| 355 | } |
| 356 | |
| 357 | if (!need_eliminate) |
| 358 | break; |
| 359 | } |
| 360 | |
| 361 | // dce |
| 362 | dead_code_elimination(graph); |
| 363 | } |
| 364 | |
| 365 | } // namespace pnnx |