File size: 8,987 Bytes
be903e2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | // Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2022 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
#include "eliminate_noop_math.h"
#include <algorithm>
#include "utils.h"
#include "pass_level2.h"
#include "pass_level4/dead_code_elimination.h"
namespace pnnx {
static bool constant_is_all_constant(const Operator* op_constant, float vf, int vi)
{
const Parameter& param = op_constant->params.at("value");
if (param.type == 2)
{
if (param.i != vi)
return false;
}
else if (param.type == 3)
{
if (param.f != vf)
return false;
}
else
{
// unsupported data type
return false;
}
return true;
}
static bool attribute_is_all_constant(const Operator* op_attr, float vf, int vi)
{
const Attribute& attr = op_attr->attrs.begin()->second;
if (attr.shape.empty())
{
fprintf(stderr, "shape empty!\n");
return false;
}
int size = attr.shape[0];
for (size_t i = 1; i < attr.shape.size(); i++)
{
size *= attr.shape[i];
}
if (attr.type == 1)
{
const float* p = (const float*)attr.data.data();
for (int i = 0; i < size; i++)
{
if (p[i] != vf)
return false;
}
}
else if (attr.type == 2)
{
const double* p = (const double*)attr.data.data();
for (int i = 0; i < size; i++)
{
if (p[i] != vf)
return false;
}
}
else if (attr.type == 3)
{
// f16
const unsigned short* p = (const unsigned short*)attr.data.data();
for (int i = 0; i < size; i++)
{
if (float16_to_float32(p[i]) != vf)
return false;
}
}
else if (attr.type == 4)
{
const int* p = (const int*)attr.data.data();
for (int i = 0; i < size; i++)
{
if (p[i] != vi)
return false;
}
}
else if (attr.type == 5)
{
const int64_t* p = (const int64_t*)attr.data.data();
for (int i = 0; i < size; i++)
{
if (p[i] != vi)
return false;
}
}
else if (attr.type == 7)
{
const signed char* p = (const signed char*)attr.data.data();
for (int i = 0; i < size; i++)
{
if (p[i] != vi)
return false;
}
}
else if (attr.type == 8)
{
const unsigned char* p = (const unsigned char*)attr.data.data();
for (int i = 0; i < size; i++)
{
if (p[i] != vi)
return false;
}
}
else
{
// unsupported data type
return false;
}
return true;
}
static bool operator_is_all_constant(const Operator* op, float vf, int vi)
{
if (op->type == "pnnx.Attribute")
return attribute_is_all_constant(op, vf, vi);
if (op->type == "prim::Constant")
return constant_is_all_constant(op, vf, vi);
if (op->type == "torch.zeros" || op->type == "torch.zeros_like")
return (vf == 0.f && vi == 0);
if (op->type == "torch.ones" || op->type == "torch.ones_like")
return (vf == 1.f && vi == 1);
return false;
}
void eliminate_noop_math(Graph& graph)
{
for (;;)
{
bool need_eliminate = false;
// build expression via reverse order
for (int i = (int)graph.ops.size() - 1; i >= 0; i--)
{
Operator* op = graph.ops[i];
int identity_input_id = 0;
if (op->type == "aten::add")
{
Operator* op0 = op->inputs[0]->producer;
Operator* op1 = op->inputs[1]->producer;
Operator* op2 = op->inputs[2]->producer;
if (operator_is_all_constant(op1, 0.f, 0))
{
// x <= a + 0 * c
need_eliminate = true;
identity_input_id = 0;
}
else if (operator_is_all_constant(op2, 0.f, 0))
{
// x <= a + b * 0
need_eliminate = true;
identity_input_id = 0;
}
else if (operator_is_all_constant(op0, 0.f, 0) && operator_is_all_constant(op2, 1.f, 1))
{
// x <= 0 + b * 1
need_eliminate = true;
identity_input_id = 1;
}
}
if (op->type == "aten::sub")
{
Operator* op1 = op->inputs[1]->producer;
Operator* op2 = op->inputs[2]->producer;
if (operator_is_all_constant(op1, 0.f, 0))
{
// x <= a - 0 * c
need_eliminate = true;
identity_input_id = 0;
}
else if (operator_is_all_constant(op2, 0.f, 0))
{
// x <= a - b * 0
need_eliminate = true;
identity_input_id = 0;
}
}
if (op->type == "aten::rsub")
{
Operator* op0 = op->inputs[0]->producer;
Operator* op1 = op->inputs[1]->producer;
Operator* op2 = op->inputs[2]->producer;
if (operator_is_all_constant(op0, 0.f, 0) && operator_is_all_constant(op2, 1.f, 1))
{
// x <= b * 1 - 0
need_eliminate = true;
identity_input_id = 1;
}
else if (operator_is_all_constant(op0, 0.f, 0) && operator_is_all_constant(op1, 1.f, 1))
{
// x <= 1 * c - 0
need_eliminate = true;
identity_input_id = 2;
}
}
if (op->type == "aten::mul")
{
Operator* op0 = op->inputs[0]->producer;
Operator* op1 = op->inputs[1]->producer;
if (operator_is_all_constant(op0, 1.f, 1))
{
// x <= 1 * b
need_eliminate = true;
identity_input_id = 1;
}
if (operator_is_all_constant(op1, 1.f, 1))
{
// x <= a * 1
need_eliminate = true;
identity_input_id = 0;
}
}
if (op->type == "aten::div")
{
Operator* op1 = op->inputs[1]->producer;
if (operator_is_all_constant(op1, 1.f, 1))
{
// x <= a / 1
need_eliminate = true;
identity_input_id = 0;
}
}
if (op->type == "aten::pow")
{
Operator* op1 = op->inputs[1]->producer;
if (operator_is_all_constant(op1, 1.f, 1))
{
// x <= x ^ 1
need_eliminate = true;
identity_input_id = 0;
}
}
if (!need_eliminate)
continue;
// fprintf(stderr, "eliminate_noop_math %s %s\n", op->type.c_str(), op->name.c_str());
for (auto& x : op->inputs)
{
x->remove_consumer(op);
}
Operand* math_out = op->outputs[0];
for (auto& x : math_out->consumers)
{
for (size_t j = 0; j < x->inputs.size(); j++)
{
if (x->inputs[j] == math_out)
x->inputs[j] = op->inputs[identity_input_id];
}
op->inputs[identity_input_id]->consumers.push_back(x);
}
math_out->producer = 0;
math_out->consumers.clear();
graph.operands.erase(std::find(graph.operands.begin(), graph.operands.end(), math_out));
delete math_out;
op->inputs.clear();
op->outputs.clear();
graph.ops.erase(graph.ops.begin() + i);
delete op;
break;
}
if (!need_eliminate)
break;
}
// dce
dead_code_elimination(graph);
}
} // namespace pnnx
|