code: def record_shapeenv_event(
      *, save_tracked_fakes: bool = False
  ) -> Callable:
      def decorator(fn: Callable) -> Callable:
          assert callable(fn)
          name = fn.__name__
  
          @functools.wraps(fn)
          def wrapper(*args, **kwargs):
              from torch.fx.experimental.symbolic_shapes import ShapeEnv
  
              if isinstance(args[0], ShapeEnv) and args[0].is_recording:  # type: ignore[has-type]
                  # If ShapeEnv is already recording an event, call the wrapped
                  # function directly.
                  #
                  # NB: here, we skip the check of whether all ShapeEnv instances
                  # are equal, in favor of a faster dispatch.
                  return fn(*args, **kwargs)
  
              # Retrieve an instance of ShapeEnv.
              # Assumption: the collection of args and kwargs may not reference
              # different ShapeEnv instances.
              self = _extract_shape_env_and_assert_equal(args, kwargs)
  
              # If we are calling this function without any ShapeEnv instance
              # alive in its arguments, we don't record and call the original.
              if self is None:
                  return fn(*args, **kwargs)
  
              # Otherwise, start recording and call the function.
              with self.recording():
                  # Take a snapshot of the current tracked_fakes.
                  tracked_fakes = (
                      self.snapshot_tracked_fakes()
                      if save_tracked_fakes
                      else None
                  )
                  # Record the event for 'fn'.
                  event = ShapeEnvEvent(
                      fn,
                      list(args),
                      kwargs,
                      tracked_fakes,
                      name=fn.__name__,
                  )
                  self.events.append(event)
                  # Play the event on this ShapeEnv.
                  return event.run(self)
  
          return wrapper
  
      return decorator
  
imports: import functools
  import itertools
  from dataclasses import dataclass
  from typing import Any, Callable, Dict, List, Optional, Tuple, Union
  import torch
  import torch.utils._pytree
label_desc: Use image node_id 2 for calling a global function with example usage: record_shapeenv_event() and returns: decorator, wrapper, fn, fn, event