MCPcopy Create free account
hub / github.com/XiaoMi/mace / fold_instance_norm

Method fold_instance_norm

tools/python/transform/transformer.py:3306–3413  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

3304 epsilon_arg.f = epsilon
3305
3306 def fold_instance_norm(self):
3307 net = self._model
3308 for op in net.op:
3309 is_reduce = (op.type == MaceOp.Reduce.name and
3310 len(op.input) == 1 and
3311 len(op.output) == 1)
3312 if not is_reduce:
3313 continue
3314 reduce_type_arg = ConverterUtil.get_arg(
3315 op, MaceKeyword.mace_reduce_type_str)
3316 reduce_type = reduce_type_arg.i
3317 axis_arg = ConverterUtil.get_arg(op, MaceKeyword.mace_axis_str)
3318 axis = axis_arg.ints
3319 keepdims_arg = ConverterUtil.get_arg(
3320 op, MaceKeyword.mace_keepdims_str)
3321 keepdims = keepdims_arg.i
3322 if not (reduce_type == ReduceType.MEAN.value and
3323 len(axis) == 2 and
3324 axis[0] == 1 and axis[1] == 2 and
3325 keepdims == 1 and
3326 len(op.output_shape[0].dims) == 4):
3327 continue
3328 # Ops that take Mean as input
3329 mean_consumers = self._consumers.get(op.output[0], [])
3330 if len(mean_consumers) != 2:
3331 continue
3332 sqr_diff_mean_idx = -1
3333 sqr_diff_mean_op = None
3334 for idx in range(2):
3335 if mean_consumers[idx].type == MaceOp.SqrDiffMean.name:
3336 sqr_diff_mean_idx = idx
3337 sqr_diff_mean_op = mean_consumers[idx]
3338 break
3339 if sqr_diff_mean_idx == -1:
3340 # SqrDiffMean is not found, it is not InstanceNorm
3341 continue
3342 second_mean_consumer_op = mean_consumers[1 - sqr_diff_mean_idx]
3343 if second_mean_consumer_op.type != MaceOp.Eltwise.name:
3344 continue
3345 elt_type = ConverterUtil.get_arg(
3346 second_mean_consumer_op, MaceKeyword.mace_element_type_str).i
3347 scale_offset = False
3348 # second consumer of Mean can only be NEG or PROD, otherwise,
3349 # it's not InstanceNorm
3350 if elt_type == EltwiseType.PROD.value:
3351 scale_offset = True
3352 elif elt_type == EltwiseType.NEG.value:
3353 scale_offset = False
3354 else:
3355 continue
3356 # var + epsilon
3357 sqr_diff_mean_consumers = self._consumers.get(
3358 sqr_diff_mean_op.output[0], [])
3359 if len(sqr_diff_mean_consumers) != 1:
3360 continue
3361 var_plus_epsilon_op = sqr_diff_mean_consumers[0]
3362 if var_plus_epsilon_op.type != MaceOp.Eltwise.name:
3363 continue

Callers

nothing calls this directly

Calls 5

get_rhs_op_scale_trueMethod · 0.95
get_lhs_and_final_addMethod · 0.95
do_fold_instance_normMethod · 0.95
get_argMethod · 0.45

Tested by

no test coverage detected