wrapping.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  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 transformers.models.mllama.modeling_mllama import MllamaSelfAttentionDecoderLayer,MllamaCrossAttentionDecoderLayer,MllamaVisionEncoderLayer
  6. from torch.distributed.fsdp.wrap import (
  7. transformer_auto_wrap_policy,
  8. size_based_auto_wrap_policy,
  9. )
  10. def get_size_policy(min_params=1e8):
  11. num_wrap_policy = functools.partial(
  12. size_based_auto_wrap_policy, min_num_params=min_params
  13. )
  14. return num_wrap_policy
  15. def get_llama_wrapper():
  16. """we register our main layer class and use the fsdp transformer wrapping policy
  17. ensures embedding layers are in the root fsdp unit for shared access and that fsdp units map to transformer layers
  18. """
  19. # ==== use new transformer wrapper
  20. llama_auto_wrap_policy = functools.partial(
  21. transformer_auto_wrap_policy,
  22. transformer_layer_cls=set([LlamaDecoderLayer, MllamaSelfAttentionDecoderLayer,MllamaVisionEncoderLayer,MllamaCrossAttentionDecoderLayer])
  23. )
  24. return llama_auto_wrap_policy