code: def graph_replace(
      outputs: Union[Sequence[Variable], Variable],
      replace: Optional[ReplaceTypes] = None,
      *,
      strict=True,
  ) -> Union[list[Variable], Variable]:
      """Replace variables in ``outputs`` by ``replace``.
  
      Parameters
      ----------
      outputs: Union[Sequence[Variable], Variable]
          Output graph
      replace: Dict[Variable, Variable]
          Replace mapping
      strict: bool
          Raise an error if some replacements were not used
      return_unused: bool
          Return replacements that were not used
  
      Returns
      -------
      Union[Variable, List[Variable]]
          Output graph with subgraphs replaced, see function overload for the exact type
  
      Raises
      ------
      ValueError
          If some replacements could not be applied and strict is True
      """
      as_list = False
      if not isinstance(outputs, Sequence):
          outputs = [outputs]
      else:
          as_list = True
      replace_dict = _format_replace(replace)
      # collect minimum graph inputs which is required to compute outputs
      # and depend on replacements
      # additionally remove constants, they do not matter in clone get equiv
      conditions = [
          c
          for c in truncated_graph_inputs(outputs, replace_dict)
          if not isinstance(c, Constant)
      ]
      # for the function graph we need the clean graph where
      # inputs do not have owners
      # this is exactly the reason to clone conditions
      equiv = {
          c: c.clone(name=f"i-{i}") for i, c in enumerate(conditions)
      }
      # some replace keys may disappear
      # the reason is they are outside the graph
      # clone the graph but preserve the equiv mapping
      fg = FunctionGraph(
          conditions,
          outputs,
          # clone_get_equiv kwargs
          copy_orphans=False,
          copy_inputs=False,
          memo=equiv,
      )
      # replace the conditions back
      fg_replace = {equiv[c]: c for c in conditions}
      # add the replacements on top of input mappings
      fg_replace.update(
          {equiv[r]: v for r, v in replace_dict.items() if r in equiv}
      )
      # replacements have to be done in reverse topological order so that nested
      # expressions get recursively replaced correctly
  
      # some replacements may be initially outside the graph
      # but later introduced by a replacement
      # So far FunctionGraph does these replacements inplace it is thus unsafe
      # apply them using fg.replace, it may change the original graph
      if strict:
          non_fg_replace = {
              r: v for r, v in replace_dict.items() if r not in equiv
          }
          if non_fg_replace:
              raise ValueError(
                  f"Some replacements were not used: {non_fg_replace}"
              )
      toposort = fg.toposort()
  
      def toposort_key(
          fg: FunctionGraph,
          ts: list[Apply],
          pair: tuple[Variable, Variable],
      ) -> int:
          key, _ = pair
          if key.owner is not None:
              return ts.index(key.owner)
          else:
              if key in fg.variables:
                  return -1
              else:
                  raise ValueError(f"{key} is not a part of graph")
  
      sorted_replacements = sorted(
          fg_replace.items(),
          # sort based on the fg toposort, if a variable has no owner, it goes first
          key=partial(toposort_key, fg, toposort),
          reverse=True,
      )
      fg.replace_all(sorted_replacements, import_missing=True)
      if as_list:
          return list(fg.outputs)
      else:
          return fg.outputs[0]
  
imports: import warnings
  from collections.abc import Iterable, Mapping, Sequence
  from functools import partial, singledispatch
  from typing import Optional, Union, cast, overload
  from pytensor.graph.basic import Apply, Constant, Variable, io_toposort, truncated_graph_inputs
  from pytensor.graph.fg import FunctionGraph
  from pytensor.graph.op import Op
label_desc: Use image node_id 7 for calling a global function with example usage: graph_replace(outputs, replace) and returns: list, fg, ts, unknown