code: def try_to_transform(conv_op, add_op, block):
      if add_op.op_type == "sub":
          bias_var = add_op.y
      else:
          bias_var = add_op.x if add_op.x.val is not None else add_op.y
      bias_value = bias_var.val
  
      # check that the bias value is a constant array or a scalar constant
      if not isinstance(bias_value, (np.ndarray, np.generic)):
          return False
  
      is_bias_scalar = False
      if not isinstance(bias_value, np.ndarray):
          is_bias_scalar = True
  
      # find rank of the conv input
      rank = conv_op.x.rank
      if rank is None:
          return False
      if not (rank == 3 or rank == 4 or rank == 5):
          return False
  
      # check compatibility of bias value with the rank of the conv op
      # either bias value should be a scalar or:
      # rank=3 ==> (B,C,D), which means bias must be (1,C,1) or (C,1)
      # rank=4 ==> (B,C,D1,D2), which means bias must be (1,C,1,1) or (C,1,1)
      # rank=5 ==> (B,C,D1,D2,D3), which means bias must be (1,C,1,1,1) or (C,1,1,1)
  
      if is_bias_scalar:
          bias_value = np.array([bias_value])
      else:
          # check that there is at most one dimension in the shape that is not 1
          if len(np.squeeze(bias_value).shape) > 1:
              return False
          # check that addition is not happening on the batch dimension
          if len(bias_value) == rank:
              if bias_value.shape[0] != 1:
                  return False
          # check that last rank-2 entries in the shape vector are all 1s
          if np.prod(bias_value.shape[-(rank - 2) :]) != 1:
              return False
          bias_value = np.squeeze(bias_value)
  
      if add_op.op_type == "sub":
          bias_value *= -1
  
      # everything looks good, now find the new updated bias
      old_bias = conv_op.inputs.get("bias", None)
      old_bias_value = None
      if old_bias is not None and old_bias.val is not None:
          old_bias_value = old_bias.val
      if old_bias is None:
          # need to create a fresh numpy array for bias
          if np.prod(bias_value.shape) == 1:
              # its a scalar bias
              # need to find the value of Cout to form a new bias
              if conv_op.weight.val is None:
                  return False
              Cout = conv_op.weight.val.shape[0]
              new_bias_value = np.broadcast_to(bias_value, (Cout,))
          else:
              new_bias_value = bias_value
      else:
          # just need to update the existing bias array
          try:
              new_bias_value = old_bias_value + bias_value
          except:
              return False
  
      # create a new conv op with the new bias value, copying rest of the attributes
      out_name = add_op.outputs[0].name
      new_bias_var = mb.const(
          val=new_bias_value, mode="file_value", before_op=conv_op
      )
  
      conv_kargs = {
          "bias": new_bias_var,
          "name": out_name,
          "before_op": conv_op,
      }
  
      for k, v in conv_op.inputs.items():
          if k == "bias":
              continue
          conv_kargs[k] = v
  
      if conv_op.op_type == "conv":
          x = mb.conv(**conv_kargs)
      else:
          x = mb.conv_transpose(**conv_kargs)
  
      add_op.enclosing_block.replace_uses_of_var_after_op(
          anchor_op=add_op, old_var=add_op.outputs[0], new_var=x
      )
      # Remove all the ops at once
      block.remove_ops([conv_op, add_op])
      return True
  
file_path: turicreate/src/external/coremltools_wrap/coremltools/coremltools/converters/mil/mil/passes/conv_bias_fusion.py
imports: from __future__ import print_function
  from __future__ import division
  from __future__ import absolute_import
  from coremltools.converters.mil.mil.passes.pass_registry import register_pass
  from coremltools.converters.mil.mil import Builder
  import numpy
label_desc: Use image node_id 2 for calling a global function with example usage: try_to_transform(conv_op, add_op, block) and returns: True, False, False, False, False, False, False, False, False