code: def local_subtensor_merge(fgraph, node):
      """
      Refactored optimization to deal with all cases of tensor merging.
      Given a subgraph of the form Subtensor(Subtensor(u)), the optimization
      expresses all slices in a canonical form, and then merges them together.
  
      """
      from pytensor.scan.op import Scan
  
      if isinstance(node.op, Subtensor):
          u = node.inputs[0]
          if u.owner and isinstance(u.owner.op, Subtensor):
              # We can merge :)
              # x actual tensor on which we are picking slices
              x = u.owner.inputs[0]
              # slices of the first applied subtensor
              slices1 = get_idx_list(
                  u.owner.inputs, u.owner.op.idx_list
              )
              slices2 = get_idx_list(node.inputs, node.op.idx_list)
  
              # Don't try to do the optimization on do-while scan outputs,
              # as it will create a dependency on the shape of the outputs
              if (
                  x.owner is not None
                  and isinstance(x.owner.op, Scan)
                  and x.owner.op.info.as_while
              ):
                  return None
  
              # Get the shapes of the vectors !
              try:
                  # try not to introduce new shape into the graph
                  xshape = fgraph.shape_feature.shape_of[x]
                  ushape = fgraph.shape_feature.shape_of[u]
              except AttributeError:
                  # Following the suggested use of shape_feature which should
                  # consider the case when the compilation mode doesn't
                  # include the ShapeFeature
                  xshape = x.shape
                  ushape = u.shape
  
              merged_slices = []
              pos_2 = 0
              pos_1 = 0
              while (pos_1 < len(slices1)) and (pos_2 < len(slices2)):
                  slice1 = slices1[pos_1]
                  if isinstance(slice1, slice):
                      merged_slices.append(
                          merge_two_slices(
                              fgraph,
                              slice1,
                              xshape[pos_1],
                              slices2[pos_2],
                              ushape[pos_2],
                          )
                      )
                      pos_2 += 1
                  else:
                      merged_slices.append(slice1)
                  pos_1 += 1
  
              if pos_2 < len(slices2):
                  merged_slices += slices2[pos_2:]
              else:
                  merged_slices += slices1[pos_1:]
  
              merged_slices = tuple(
                  as_index_constant(s) for s in merged_slices
              )
              subtens = Subtensor(merged_slices)
  
              sl_ins = get_slice_elements(
                  merged_slices, lambda x: isinstance(x, Variable)
              )
              # Do not call make_node for test_value
              out = subtens(x, *sl_ins)
  
              # Copy over previous output stacktrace
              # and stacktrace from previous slicing operation.
              # Why? Because, the merged slicing operation could have failed
              # because of either of the two original slicing operations
              orig_out = node.outputs[0]
              copy_stack_trace([orig_out, node.inputs[0]], out)
              return [out]
  
imports: import sys
  from collections.abc import Iterable
  import numpy
  import pytensor
  import pytensor.scalar.basic
  from pytensor import compile
  from pytensor.graph.basic import Constant, Variable
  from pytensor.graph.rewriting.basic import WalkingGraphRewriter, copy_stack_trace, in2out, node_rewriter
  from pytensor.raise_op import Assert
  from pytensor.tensor.basic import Alloc, Join, MakeVector, ScalarFromTensor, TensorFromScalar, alloc, as_tensor, cast, concatenate, extract_constant, get_underlying_scalar_constant_value, register_infer_shape, switch
  from pytensor.tensor.blockwise import Blockwise
  from pytensor.tensor.elemwise import Elemwise
  from pytensor.tensor.exceptions import NotScalarConstantError
  from pytensor.tensor.math import Dot, add, and_, ceil_intdiv, dot, eq, ge, gt, le, lt, maximum, minimum, or_
  from pytensor.tensor.math import all
  from pytensor.tensor.rewriting.basic import register_canonicalize, register_specialize, register_stabilize
  from pytensor.tensor.shape import Shape, SpecifyShape, Unbroadcast, shape_padleft, shape_tuple, specify_shape, unbroadcast
  from pytensor.tensor.sharedvar import TensorSharedVariable
  from pytensor.tensor.subtensor import AdvancedIncSubtensor, AdvancedIncSubtensor1, AdvancedSubtensor, AdvancedSubtensor1, IncSubtensor, Subtensor, advanced_inc_subtensor1, advanced_subtensor, advanced_subtensor1, as_index_constant, as_index_literal, get_canonical_form_slice, get_constant_idx, get_idx_list, get_slice_elements, inc_subtensor, indices_from_subtensor
  from pytensor.tensor.type import TensorType
  from pytensor.tensor.type_other import NoneTypeT, SliceConstant, SliceType
  from pytensor.tensor.variable import TensorConstant, TensorVariable
label_desc: Use image node_id 10 for calling a global function with example usage: local_subtensor_merge(fgraph, node) and returns: list, None