_checkpointable.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. # Copyright (c) Meta Platforms, Inc. and affiliates
  2. from typing_extensions import Protocol, runtime_checkable
  3. import torch
  4. @runtime_checkable
  5. class _Checkpointable(Protocol): # noqa: PYI046
  6. """
  7. Interface for checkpointable objects.
  8. Implemented as a protocol, implicit subtyping is supported so subclasses do not need to inherit this explicitly.
  9. This is to allow arbitrary objects/tensor subclasses to hook into DCP seamlessly through implementing the interface.
  10. """
  11. def __create_write_items__(self, fqn: str, object: object) -> list[object]:
  12. """
  13. Return a list of WriteItems based on object's contents.
  14. """
  15. raise NotImplementedError(
  16. "_Checkpointable._create_write_items is not implemented"
  17. )
  18. def __create_chunk_list__(self) -> list[object]:
  19. """
  20. Return a list of `ChunkStorageMetadata` based on object's contents.
  21. """
  22. raise NotImplementedError(
  23. "_Checkpointable._create_chunk_list is not implemented"
  24. )
  25. def __get_tensor_shard__(self, index: int) -> torch.Tensor:
  26. """
  27. Return a 'torch.Tensor' shard based on 'MetadataIndex'.
  28. """
  29. raise NotImplementedError(
  30. "_Checkpointable._get_tensor_shard is not implemented"
  31. )