f"CheckpointPolicy, but got {type(policy).__name__}."
)
def _torch_policy_adapter(
policy_fn: Callable, torch_context: Any, op: Any, *args: Any, **kwargs: Any
) -> Any:
"""Pass native Torch inputs through and adapt only the policy result."""
return _to_torch_checkpoint_policy(policy_fn(torch_context, op, *args, **kwargs))
def create_native_selective_checkpoint_contexts(policy_fn: Callable) -> Tuple[Any, Any]:
"""Create Torch-native selective-checkpoint contexts for compile capture."""
if not callable(policy_fn):
raise TypeError("policy_fn must be callable in HyperParallel compile mode.")
from torch.utils import checkpoint as torch_checkpoint # pylint: disable=C0415
return torch_checkpoint.create_selective_checkpoint_contexts(
partial(_torch_policy_adapter, policy_fn)
)
__all__ = ["create_native_selective_checkpoint_contexts"]