code: def traverse(out, x, x_copy, d, visited=None):
      """
      Function used by scan to parse the tree and figure out which nodes
      it needs to replace.
  
      There are two options :
          1) x and x_copy or on host, then you would replace x with x_copy
  
      """
      # ``visited`` is a set of nodes that are already known and don't need to be
      # checked again, speeding up the traversal of multiply-connected graphs.
      # if a ``visited`` set is given, it will be updated in-place so the callee
      # knows which nodes we have seen.
      if visited is None:
          visited = set()
      if out in visited:
          return d
      visited.add(out)
  
      if out == x:
          # assert isinstance(x.type, GpuArrayType)
          # d[out] = GpuFromHost(x.type.context_name)(x_copy)
          # return d
          raise RuntimeError("Not supported")
      elif out.owner is None:
          return d
      else:
          for inp in out.owner.inputs:
              d = traverse(inp, x, x_copy, d, visited)
          return d
  
imports: import copy
  import dataclasses
  import logging
  from collections import OrderedDict, namedtuple
  from collections.abc import Sequence
  from itertools import chain
  from typing import TYPE_CHECKING, Callable, Optional, Union
  from typing import cast
  import numpy
  from pytensor import scalar
  from pytensor import tensor
  from pytensor.compile.profiling import ProfileStats
  from pytensor.configdefaults import config
  from pytensor.graph.basic import Constant, Variable, equal_computations, graph_inputs
  from pytensor.graph.op import get_test_value
  from pytensor.graph.replace import clone_replace
  from pytensor.graph.type import HasDataType
  from pytensor.graph.utils import TestValueError
  from pytensor.tensor.basic import AllocEmpty, cast
  from pytensor.tensor.subtensor import set_subtensor
  from pytensor.tensor.variable import TensorConstant
label_desc: Use image node_id 2 for calling a global function with example usage: traverse(out, x, x_copy, d, visited) and returns: d, d, d