fsdp.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # Copyright 2024 The HuggingFace Team. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from __future__ import annotations
  15. import inspect
  16. import os
  17. from typing import TYPE_CHECKING
  18. from ..utils import is_torch_available, strtobool
  19. from ..utils.quantization_config import QuantizationMethod
  20. if TYPE_CHECKING:
  21. from torch import nn
  22. def is_fsdp_managed_module(module: nn.Module) -> bool:
  23. if not is_torch_available():
  24. return False
  25. import torch
  26. if not torch.distributed.is_available():
  27. return False
  28. import torch.distributed.fsdp
  29. return isinstance(module, torch.distributed.fsdp.FullyShardedDataParallel) or getattr(
  30. module, "_is_fsdp_managed_module", False
  31. )
  32. def is_fsdp_enabled():
  33. if is_torch_available():
  34. import torch
  35. return (
  36. torch.distributed.is_available()
  37. and torch.distributed.is_initialized()
  38. and strtobool(os.environ.get("ACCELERATE_USE_FSDP", "False")) == 1
  39. and strtobool(os.environ.get("FSDP_CPU_RAM_EFFICIENT_LOADING", "False")) == 1
  40. )
  41. return False
  42. def get_fsdp_ckpt_kwargs():
  43. """
  44. Returns checkpoint kwargs for FSDP model saving.
  45. Checks if the `adapter_only` parameter is supported by `save_fsdp_model` from accelerate
  46. and returns the appropriate kwargs.
  47. """
  48. from accelerate.utils import save_fsdp_model
  49. if "adapter_only" in list(inspect.signature(save_fsdp_model).parameters):
  50. return {"adapter_only": True}
  51. else:
  52. return {}
  53. def update_fsdp_plugin_peft(model, accelerator):
  54. """
  55. Updates the FSDP plugin for PEFT LoRA/QLoRA compatibility.
  56. When using FSDP with PEFT LoRA, the auto wrap policy needs to be updated to additionally wrap
  57. LoRA trainable layers separately. When using FSDP with QLoRA, the mixed precision policy needs
  58. to be updated to use the quantization storage data type.
  59. """
  60. from peft import PeftConfig
  61. from peft.utils.other import fsdp_auto_wrap_policy
  62. if isinstance(model.active_peft_config, PeftConfig):
  63. accelerator.state.fsdp_plugin.auto_wrap_policy = fsdp_auto_wrap_policy(model)
  64. if (
  65. getattr(model, "quantization_method", None) == QuantizationMethod.BITS_AND_BYTES
  66. and model.hf_quantizer.quantization_config.bnb_4bit_quant_storage.is_floating_point
  67. ):
  68. accelerator.state.fsdp_plugin.set_mixed_precision(
  69. model.hf_quantizer.quantization_config.bnb_4bit_quant_storage, override=True
  70. )