)
def recompute_fn(self, function: Callable, *inputs: Any) -> None:
"""Restore execution state and rerun the checkpointed ``function``."""
function_kwargs, *function_args = inputs
with torch.random.fork_rng(
devices=self.device_ids,
enabled=self.cpu_state is not None,
device_type=self.device_type,
):
if self.cpu_state is not None:
torch.set_rng_state(self.cpu_state)
if self.device_was_initialized:
_set_device_states(self.device_type, self.device_ids, self.device_states)
device_autocast_context = contextlib.nullcontext()
if self.device_autocast_kwargs is not None:
device_autocast_context = torch.amp.autocast(
device_type=self.device_type, **self.device_autocast_kwargs
)
with device_autocast_context, torch.amp.autocast("cpu", **self.cpu_autocast_kwargs), self.recompute_context:
function(*function_args, **function_kwargs)
def _resolve_contexts(context_fn: Callable) -> Tuple[Any, Any]:
"""Validate and unpack the (forward, recompute) context tuple from ``context_fn``."""