code: def local_reshape_chain(op):
      @node_rewriter([op])
      def f(fgraph, node):
          """
          Reshape(Reshape(shape1),shape2) -> Reshape(shape2)
  
          """
          if not check_chain(node, op, op):
              return False
  
          # TODO: this can permit a failing program to run by eliminating
          #       the lower reshape
          rval = node.op(node.inputs[0].owner.inputs[0], node.inputs[1])
  
          # Copy over stacktrace from previous output node, as any error
          # in new computational graph would have been caused by last op
          # in the old computational graph.
          copy_stack_trace(node.outputs, rval)
  
          # It might happen that the desired output of this node has a
          # broadcastable pattern that does not match that of 'rval'. This is
          # when originally, we were able to figure out that one of the
          # dimensions of the reshape is one, but some other transformation
          # replaced the shape by one for which this cannot be guessed.
          # We should try to figure out why we lost the information about this
          # constant value... but in the meantime, better not apply this
          # rewrite.
          if rval.type.ndim == node.outputs[0].type.ndim and all(
              s1 == s1
              for s1, s2 in zip(
                  rval.type.shape, node.outputs[0].type.shape
              )
              if s1 == 1 or s2 == 1
          ):
              return [rval]
          else:
              return False
  
      return f
  
imports: import traceback
  from io import StringIO
  from typing import Optional
  from typing import cast
  from warnings import warn
  import numpy
  import pytensor
  from pytensor.configdefaults import config
  from pytensor.graph.basic import Constant, Variable, ancestors, equal_computations
  from pytensor.graph.features import AlreadyThere, Feature
  from pytensor.graph.fg import FunctionGraph
  from pytensor.graph.rewriting.basic import GraphRewriter, check_chain, copy_stack_trace, node_rewriter
  from pytensor.graph.utils import InconsistencyError, get_variable_trace_string
  from pytensor.tensor.basic import MakeVector, as_tensor_variable, cast, constant, extract_constant, get_underlying_scalar_constant_value, register_infer_shape, stack
  from pytensor.tensor.elemwise import DimShuffle, Elemwise
  from pytensor.tensor.exceptions import NotScalarConstantError, ShapeError
  from pytensor.tensor.rewriting.basic import register_canonicalize, register_specialize, register_stabilize, register_useless, topo_constant_folding
  from pytensor.tensor.shape import Reshape, Shape, Shape_i, SpecifyShape, Unbroadcast, shape_i, specify_shape, unbroadcast
  from pytensor.tensor.subtensor import Subtensor, get_idx_list
  from pytensor.tensor.type import TensorType, discrete_dtypes, integer_dtypes
  from pytensor.tensor.type_other import NoneConst
label_desc: Use image node_id 1 for calling a global function with example usage: local_reshape_chain(op) and returns: f, False, list, False