code: def transform_pattern(pattern):
      # get the scale
      scale_var = (
          pattern.scale.x
          if pattern.scale.x.val is not None
          else pattern.scale.y
      )
      scale = scale_var.val
      is_scalar = _is_scalar(pattern)
  
      # get weight and bias and groups from conv layer
      conv_weight = pattern.conv.weight.val
      conv_bias = pattern.conv.bias
      groups = pattern.conv.groups.val
  
      # get type of the conv layer
      is_deconv = pattern.conv.op_type == "conv_transpose"
      is_conv_1d = len(conv_weight.shape) == 3
  
      Cin, Cout = _cin_cout(pattern)
  
      # transform the scale to 1./scale for the real_div case
      if pattern.scale.op_type == "real_div":
          scale = 1.0 / scale
  
      # get the type of the conv weight
      conv_weight_type = conv_weight.dtype
  
      # create bias for conv if not exist
      if conv_bias is None:
          conv_bias = np.zeros(Cout)
      else:
          conv_bias = conv_bias.val
      conv_bias = conv_bias.astype(conv_weight_type)
  
      # get the original shape of weight and bias
      origin_weight_shape = conv_weight.shape
      origin_bias_shape = conv_bias.shape
  
      # update the weight/bias for conv layer
      if is_scalar:
          new_conv_bias = np.array(conv_bias * scale).astype(
              conv_weight_type
          )
          new_conv_weight = np.array(conv_weight * scale).astype(
              conv_weight_type
          )
  
      else:
          scale = np.reshape(scale, (Cout))
          new_conv_bias = np.array(conv_bias * scale).astype(
              conv_weight_type
          )
          new_conv_weight = []
          if is_deconv:
              conv_weight = np.transpose(
                  conv_weight, [1, 0, 2] if is_conv_1d else [1, 0, 2, 3]
              )
              conv_weight = np.reshape(
                  conv_weight,
                  [Cout, Cin // groups] + list(conv_weight.shape[2:]),
              )
  
          for i in range(Cout):
              _conv_weight = conv_weight[i] * scale[i]
              new_conv_weight.append(_conv_weight)
          new_conv_weight = np.array(new_conv_weight).astype(
              conv_weight_type
          )
  
          if is_deconv:
              new_conv_weight = np.reshape(
                  new_conv_weight,
                  [Cout // groups, Cin]
                  + list(new_conv_weight.shape[2:]),
              )
              new_conv_weight = np.transpose(
                  new_conv_weight,
                  [1, 0, 2] if is_conv_1d else [1, 0, 2, 3],
              )
  
      # make sure the updated weight and bias have the same shape as the original ones
      assert (
          new_conv_weight.shape == origin_weight_shape
      ), "conv weight should have the same shape before and after the fuse_conv_scale pass."
      assert (
          new_conv_bias.shape == origin_bias_shape
      ), "conv bias should have the same shape before and after the fuse_conv_scale pass."
  
      # create a new conv op with the new weight, bias value, copying rest of the attributes
      out_name = pattern.scale.outputs[0].name
      conv_kargs = {
          "weight": new_conv_weight,
          "bias": new_conv_bias,
          "name": out_name,
          "before_op": pattern.conv,
      }
  
      for k, v in pattern.conv.inputs.items():
          if k in ["weight", "bias"]:
              continue
          conv_kargs[k] = v
  
      if is_deconv:
          x = mb.conv_transpose(**conv_kargs)
      else:
          x = mb.conv(**conv_kargs)
  
      pattern.scale.enclosing_block.replace_uses_of_var_after_op(
          anchor_op=pattern.scale,
          old_var=pattern.scale.outputs[0],
          new_var=x,
      )
      # Remove all the ops at once
      pattern.block.remove_ops(pattern.op_list())
  
file_path: coremltools/coremltools/converters/mil/experimental/passes/generic_conv_scale_fusion.py
imports: import os
  import numpy
  from coremltools.converters.mil import Builder
  from coremltools.converters.mil.experimental.passes.generic_pass_infrastructure import register_generic_pass
label_desc: Use image node_id 4 for calling a global function with example usage: transform_pattern(pattern) without return types