"""Exclude a callable region from checkpoint recomputation."""
def __init__(self, module: Callable[..., Any]) -> None:
"""Initialize a checkpoint exclusion wrapper for a MindSpore Cell or function."""
if not callable(module):
raise ValueError("module must be a MindSpore Cell or callable")
super().__init__(module, track_overlaps=False)
def construct(self, *args: Any, **kwargs: Any) -> Any:
"""Execute normally outside recompute and return the cached output in recompute."""
state = get_recompute_state()
if state is None:
return self._ckpt_wrapped_module(*args, **kwargs)
cache = state.get_resource(_CHECKPOINT_EXCLUDE_CACHE_KEY, _CheckpointExcludeCache)
if state.is_recomputing:
return cache.pop(id(self))
with _saved_tensors_context():
output = self._ckpt_wrapped_module(*args, **kwargs)
cache.save(id(self), output)
return output
def checkpoint_exclude_wrapper(module: Callable[..., Any]) -> CheckpointExcludeWrapper:
"""Wrap a MindSpore Cell or function so its region is not recomputed.