sharder.py 911 B

123456789101112131415161718192021222324252627
  1. import abc
  2. import torch.nn as nn
  3. class Sharder(abc.ABC):
  4. """
  5. This is an interface which allows user to create more advanced
  6. sharding strategies that are not easily be composed by the
  7. `ShardingSpec`.
  8. :class:`torch.distributed._shard.sharding_plan.ShardingPlan` could
  9. take an object of the `Sharder` and call `shard` to shard the module,
  10. then replace the original module with sharded module returned.
  11. """
  12. @abc.abstractmethod
  13. def shard(self, module: nn.Module) -> nn.Module:
  14. """
  15. Shard a module base on the implementation of this method, and
  16. return the sharded version of the module.
  17. Args:
  18. module (:class:`torch.nn.Module`):
  19. The module to apply sharding to.
  20. Returns:
  21. A :class:`torch.nn.Module` object that represents a module
  22. that's already been sharded.
  23. """
  24. pass