code: def _gemm_from_factored_list(fgraph, lst):
      """
      Returns None, or a list to replace node.outputs.
  
      """
      lst2 = []
      # Remove the tuple that can't be cast correctly.
      # This can happen when we try to cast a complex to a real
      for sM in lst:
          # Make every pair in list have matching dtypes
          # sM can be a tuple of 2 elements or an PyTensor variable.
          if isinstance(sM, tuple):
              sm0, sm1 = sM
              sm0 = ptb.as_tensor_variable(sm0)
              if (
                  pytensor.scalar.upcast(sm0.dtype, sm1.dtype)
                  == sm1.dtype
              ):
                  lst2.append((ptb.cast(sm0, sm1.dtype), sM[1]))
  
      lst = lst2
  
      def item_to_var(t):
          try:
              s, M = t
          except Exception:
              return t
          if s == 1:
              return M
          if s == -1:
              return -M
          return s * M
  
      # Try every pair in the sM_list, trying to turn it into a gemm operation
      for i in range(len(lst) - 1):
          s_i, M_i = lst[i]
  
          for j in range(i + 1, len(lst)):
              s_j, M_j = lst[j]
  
              if not M_j.type.in_same_class(M_i.type):
                  continue
  
              # print 'TRYING', (s_i, M_i, s_j, M_j)
  
              gemm_of_sM_list, old_dot22 = _beta_L_plus_alpha_M(
                  fgraph, s_i, M_i, s_j, M_j
              )
              # print 'GOT IT', gemm_of_sM_list
              if gemm_of_sM_list:
                  assert len(gemm_of_sM_list) == 1
                  add_inputs = [
                      item_to_var(input)
                      for k, input in enumerate(lst)
                      if k not in (i, j)
                  ]
                  add_inputs.extend(gemm_of_sM_list)
                  if len(add_inputs) > 1:
                      rval = [add(*add_inputs)]
                  else:
                      rval = add_inputs
                  # print "RETURNING GEMM THING", rval
                  return rval, old_dot22
  
imports: import copy
  import logging
  import time
  import numpy
  from pytensor.tensor.rewriting.basic import register_specialize
  import pytensor.scalar
  from pytensor.compile.mode import optdb
  from pytensor.configdefaults import config
  from pytensor.graph.features import ReplacementDidNotRemoveError, ReplaceValidate
  from pytensor.graph.rewriting.basic import EquilibriumGraphRewriter, GraphRewriter, copy_stack_trace, in2out, node_rewriter
  from pytensor.graph.rewriting.db import SequenceDB
  from pytensor.graph.utils import InconsistencyError
  from pytensor.tensor import basic
  from pytensor.tensor.blas import Dot22, _dot22, _dot22scalar, batched_dot, gemm_inplace, gemm_no_inplace, gemv_inplace, gemv_no_inplace, ger, ger_destructive
  from pytensor.tensor.elemwise import DimShuffle, Elemwise
  from pytensor.tensor.exceptions import NotScalarConstantError
  from pytensor.tensor.math import Dot, _matrix_matrix_matmul, add, mul, neg, sub
  from pytensor.tensor.rewriting.elemwise import local_dimshuffle_lift
  from pytensor.tensor.type import DenseTensorType, TensorType, integer_dtypes, values_eq_approx_remove_inf_nan
label_desc: Use image node_id 8 for calling a global function with example usage: _gemm_from_factored_list(fgraph, lst) and returns: unknown, M, unknown, t, rval,  old_dot22