code: def try_to_transform(matmul_op, add_op, block):
      if matmul_op.x.val is None and matmul_op.y.val is None:
          # This is a dynamic matmul.
          return False
      if add_op.x.val is None and add_op.y.val is None:
          # This is a dynamic add.
          return False
  
      x_is_weight = matmul_op.x.val is not None
      if x_is_weight:
          weight, linear_x = matmul_op.x, matmul_op.y
          transpose_weight = matmul_op.transpose_x.val
          transpose_x = matmul_op.transpose_y.val
      else:
          weight, linear_x = matmul_op.y, matmul_op.x
          transpose_weight = matmul_op.transpose_y.val
          transpose_x = matmul_op.transpose_x.val
  
      if linear_x.rank < 2 or weight.rank != 2:
          # We don't support these cases yet.
          return False
  
      d_out = (
          weight.shape[1] if not transpose_weight else weight.shape[0]
      )
      bias = add_op.x.val if add_op.x.val is not None else add_op.y.val
      if len(bias.shape) > 1:
          if any([d != 1 for d in bias.shape[:-1]]):
              return  # cannot transform
  
          # squeeze leading dims of size 1
          bias = np.squeeze(bias)
  
      if len(bias.shape) != 1 or bias.shape[0] != d_out:
          return  # cannot transform
  
      if add_op.op_type == "sub":
          bias = -bias
      out_name = add_op.outputs[0].name
  
      if x_is_weight:
          # If transpose_x == transpose_weight == False:
          # w*x = (x^T w^T)^T = linear(x^T, w)^T
          x_transposed = (
              transpose(linear_x, before_op=matmul_op)
              if not transpose_x
              else linear_x
          )
          w_no_transpose = (
              weight
              if not transpose_weight
              else transpose(weight, before_op=matmul_op)
          )
          x = mb.linear(
              x=x_transposed,
              weight=w_no_transpose,
              bias=bias,
              before_op=matmul_op,
          )
          x = transpose(x, before_op=matmul_op, name=out_name)
      else:
          # If transpose_x == transpose_weight == False
          # x*w = x*(w^T)^T = linear(x, w^T)
          x_no_transpose = (
              transpose(linear_x, before_op=matmul_op)
              if transpose_x
              else linear_x
          )
          w_transposed = (
              weight
              if transpose_weight
              else transpose(weight, before_op=matmul_op)
          )
          x = mb.linear(
              x=x_no_transpose,
              weight=w_transposed,
              bias=bias,
              before_op=matmul_op,
              name=out_name,
          )
  
      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([matmul_op, add_op])
      return True
  
file_path: turicreate/src/external/coremltools_wrap/coremltools/coremltools/converters/mil/mil/passes/matmul_weight_bias_fusion.py
imports: from __future__ import print_function
  from __future__ import division
  from __future__ import absolute_import
  import numpy
  from coremltools.converters.mil.mil.passes.pass_registry import register_pass
  from coremltools.converters.mil.mil import Builder
label_desc: Use image node_id 3 for calling a global function with example usage: try_to_transform(matmul_op, add_op, block) and returns: True, False, False, False