code: def local_useless_inc_subtensor_alloc(fgraph, node):
      """
      Replaces an [Advanced]IncSubtensor[1], whose increment is an `alloc` of
      a fully or partially broadcastable variable, by one that skips the
      intermediate `alloc` where possible.
  
      """
      if isinstance(
          node.op,
          (IncSubtensor, AdvancedIncSubtensor, AdvancedIncSubtensor1),
      ):
          x = node.inputs[0]
          y = node.inputs[1]
          i = node.inputs[2:]
  
          if y.owner is not None and isinstance(y.owner.op, Alloc):
              # `z` is the input of the Alloc op, i.e. at.alloc(z, <shape>)
              z = y.owner.inputs[0]
  
              try:
                  shape_feature = fgraph.shape_feature
              except AttributeError:
                  # The shape feature may not be available in some mode, but we
                  # need it for this optimization, so don't continue.
                  return False
  
              shape_of = shape_feature.shape_of
              same_shape = shape_feature.same_shape
  
              # Get the subtensor of `x` indexed by `i` in order to compare
              # shapes later.
              if isinstance(node.op, IncSubtensor):
                  xi = Subtensor(node.op.idx_list)(x, *i)
              elif isinstance(node.op, AdvancedIncSubtensor):
                  xi = advanced_subtensor(x, *i)
              elif isinstance(node.op, AdvancedIncSubtensor1):
                  xi = advanced_subtensor1(x, *i)
              else:
                  raise Exception("Should never happen!")
  
              reason = "local_useless_incsubtensor_alloc"
  
              # Add `xi` to the shape feature `fgraph`. This is important for
              # shape inference later because the variable must be part of the
              # function graph in order to call `same_shape` on it.
              if xi not in shape_of:
                  shape_feature.on_import(
                      fgraph, xi.owner, f"{reason}: add `xi`"
                  )
  
              # `xi` may have more dimensions than `y` since the subtensor ops
              # do automatic broadcasting of the increment internally. Thus, we
              # need to make the leading implicitly broadcasted dimensions
              # explicit for shape comparison later.
              if xi.ndim > y.ndim:
                  y = shape_padleft(y, xi.ndim - y.ndim)
                  if y not in shape_of:
                      shape_feature.on_import(
                          fgraph, y.owner, f"{reason}: add `y`"
                      )
  
              # Build `z_broad` explicitly to include extra implicit dimensions.
              z_broad = (True,) * (xi.ndim - z.ndim) + z.broadcastable
  
              cond = [
                  # The shapes of `y` and `xi` must either agree or `y` may
                  # also have shape equal to 1 which may be treated as a
                  # broadcastable dimension by the subtensor op.
                  or_(eq(y.shape[k], 1), eq(y.shape[k], xi.shape[k]))
                  # Loop over all dimensions.
                  for k in range(xi.ndim)
                  # We need to check the above shapes, if
                  # * the pre-alloc increment `z` is broadcastable in
                  # dimension `k` (if it isn't, then the shapes of `z` and
                  # `y` are the same by the definition of the `Alloc` op in
                  # this dimension and replacing `y` by `z` will not hide a
                  # shape error), and
                  # * `xi` and `y` do not have the same shape in dimension
                  # `k` or we cannot infer the shape statically (if the
                  # shapes of `xi` and `y` are not the same, then replacing
                  # `y` by `z` will hide the shape error of `y`), and
                  # * the shape of `y` is not equal to 1 or we cannot infer
                  # the shape statically (if the shape of `y` is equal to
                  # 1, then `y` is broadcasted by the inc_subtensor op
                  # internally, so the shapes of `xi` and `y` do not need
                  # to match in dimension `k`; else we need to check at
                  # runtime that the shape of `y` is either 1 or the same
                  # as `xi` or otherwise replacing `y` by `z` will hide a
                  # shape error).
                  if (
                      z_broad[k]
                      and not same_shape(xi, y, dim_x=k, dim_y=k)
                      and shape_of[y][k] != 1
                  )
              ]
  
              if len(cond) > 0:
                  msg = "`x[i]` and `y` do not have the same shape."
                  z = Assert(msg)(z, *cond)
  
              r = node.op(x, z, *i)
              # Copy over stacktrace from previous output, since
              # we don't expect problems when removing the intermediate
              # alloc operation and so we still want to point at the line
              # of the inc_subtensor operation.
              copy_stack_trace(node.outputs, r)
  
              return [r]
  
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 28 for calling a global function with example usage: local_useless_inc_subtensor_alloc(fgraph, node) and returns: list, False