wrapping.py 1023 B

123456789101112131415161718192021222324252627282930313233
  1. # Copyright (c) Meta Platforms, Inc. and affiliates.
  2. # This software may be used and distributed according to the terms of the Llama 2 Community License Agreement.
  3. import functools
  4. from transformers.models.llama.modeling_llama import LlamaDecoderLayer
  5. from torch.distributed.fsdp.wrap import (
  6. transformer_auto_wrap_policy,
  7. size_based_auto_wrap_policy,
  8. )
  9. def get_size_policy(min_params=1e8):
  10. num_wrap_policy = functools.partial(
  11. size_based_auto_wrap_policy, min_num_params=min_params
  12. )
  13. return num_wrap_policy
  14. def get_llama_wrapper():
  15. """we register our main layer class and use the fsdp transformer wrapping policy
  16. ensures embedding layers are in the root fsdp unit for shared access and that fsdp units map to transformer layers
  17. """
  18. # ==== use new transformer wrapper
  19. llama_auto_wrap_policy = functools.partial(
  20. transformer_auto_wrap_policy,
  21. transformer_layer_cls={
  22. LlamaDecoderLayer,
  23. },
  24. )
  25. return llama_auto_wrap_policy