Accelerate documentation

Công cụ Fully Sharded Data Parallel

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v1.15.0).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Công cụ Fully Sharded Data Parallel

enable_fsdp_ram_efficient_loading

accelerate.utils.enable_fsdp_ram_efficient_loading

< >

( )

Cho phép tải các mô hình Hugging Face một cách hiệu quả về RAM cho FSDP trong môi trường.

disable_fsdp_ram_efficient_loading

accelerate.utils.disable_fsdp_ram_efficient_loading

< >

( )

Tắt tính năng nạp hiệu quả RAM cho các mô hình Hugging Face dành cho FSDP trong môi trường.

merge_fsdp_weights

accelerate.utils.merge_fsdp_weights

< >

( checkpoint_dir: stroutput_path: strsafe_serialization: bool = Trueremove_checkpoint_dir: bool = False )

Tham số

  • checkpoint_dir (str) — Thư mục chứa các checkpoint của FSDP (có thể là của model hoặc optimizer).
  • output_path (str) — Đường dẫn để lưu checkpoint đã hợp nhất.
  • safe_serialization (bool, tùy chọn, mặc định là True) — Có nên lưu các trọng số đã hợp nhất bằng safetensors (được khuyến nghị) hay không.
  • remove_checkpoint_dir (bool, tùy chọn, mặc định là False) — Có nên xóa thư mục checkpoint sau khi gộp hay không.

Gộp các trọng số từ các checkpoint mô hình FSDP dạng phân mảnh (sharded) thành một checkpoint kết hợp duy nhất. Nên sử dụng nếu SHARDED_STATE_DICT đã được dùng cho mô hình. Các trọng số sẽ được lưu vào {output_path}/model.safetensors nếu safe_serialization, ngược lại là pytorch_model.bin.

Lưu ý: đây là một quy trình bị giới hạn bởi CPU.

FullyShardedDataParallelPlugin

lớp accelerate.FullyShardedDataParallelPlugin

< >

( fsdp_version: int = Nonesharding_strategy: str | ForwardRef('torch.distributed.fsdp.ShardingStrategy') = Nonereshard_after_forward: str | ForwardRef('torch.distributed.fsdp.ShardingStrategy') | bool = Nonebackward_prefetch: str | ForwardRef('torch.distributed.fsdp.BackwardPrefetch') | None = Nonemixed_precision_policy: dict | str | ForwardRef('torch.distributed.fsdp.MixedPrecision') | ForwardRef('torch.distributed.fsdp.MixedPrecisionPolicy') | None = Noneauto_wrap_policy: typing.Callable | typing.Literal['transformer_based_wrap', 'size_based_wrap', 'no_wrap'] | None = Nonecpu_offload: bool | ForwardRef('torch.distributed.fsdp.CPUOffload') | ForwardRef('torch.distributed.fsdp.CPUOffloadPolicy') = Noneignored_modules: collections.abc.Iterable[torch.nn.Module] | str | None = Nonestate_dict_type: str | ForwardRef('torch.distributed.fsdp.StateDictType') = Nonestate_dict_config: ForwardRef('torch.distributed.fsdp.FullStateDictConfig') | ForwardRef('torch.distributed.fsdp.ShardedStateDictConfig') | None = Noneoptim_state_dict_config: ForwardRef('torch.distributed.fsdp.FullOptimStateDictConfig') | ForwardRef('torch.distributed.fsdp.ShardedOptimStateDictConfig') | None = Nonelimit_all_gathers: bool = Trueuse_orig_params: bool | None = Noneparam_init_fn: typing.Callable[[torch.nn.Module], NoneType] | None = Nonesync_module_states: bool | None = Noneforward_prefetch: bool = Noneactivation_checkpointing: bool = Noneactivation_checkpointing_offload: bool = Nonecpu_ram_efficient_loading: bool = Nonetransformer_cls_names_to_wrap: list[str] | None = Nonemin_num_params: int | None = None )

Tham số

  • fsdp_version (int, mặc định là 1) — Phiên bản của FSDP cần sử dụng. Mặc định là 1. Nếu đặt thành 2, launcher sẽ kỳ vọng cấu hình đã được chuyển đổi sang định dạng FSDP2.
  • sharding_strategy (Union[str, torch.distributed.fsdp.ShardingStrategy], mặc định là 'FULL_SHARD') — Chiến lược sharding cần sử dụng. Phải là một str hoặc một instance của torch.distributed.fsdp.fully_sharded_data_parallel.ShardingStrategy. Đã bị ngừng khuyến khích, thay thế bằng reshard_after_forward.
  • reshard_after_forward (Union[str, torch.distributed.fsdp.ShardingStrategy, bool], mặc định là 'FULL_SHARD' cho fsdp_version=1 và True cho fsdp_version=2) — Chiến lược sharding cần sử dụng. Phải là một bool nếu fsdp_version được đặt thành 2, ngược lại phải là một str hoặc một instance của torch.distributed.fsdp.fully_sharded_data_parallel.ShardingStrategy.
  • backward_prefetch (Union[str, torch.distributed.fsdp.BackwardPrefetch], mặc định là 'NO_PREFETCH') — Chiến lược prefetch cho quá trình backward. Phải là một str hoặc một instance của torch.distributed.fsdp.fully_sharded_data_parallel.BackwardPrefetch.
  • mixed_precision_policy (Optional[Union[dict, str, torch.distributed.fsdp.MixedPrecision, torch.distributed.fsdp.MixedPrecisionPolicy]], mặc định là None) — Cấu hình để bật huấn luyện độ chính xác hỗn hợp với FullyShardedDataParallel. Nếu truyền vào một dict, nó phải có các khóa sau: param_dtype, reduce_dtype và buffer_dtype, có thể là một instance của torch.distributed.fsdp.MixedPrecisionPolicy nếu fsdp_version được đặt thành 2. Nếu truyền vào một str, nó phải là một trong các giá trị sau: fp8, fp16, bf16, fp32, và được dùng để đặt param_dtype, reduce_dtype và buffer_dtype.
  • auto_wrap_policy (Optional(Union[Callable, Literal["transformer_based_wrap", "size_based_wrap", "no_wrap"]]), defaults to NO_WRAP) -- A callable or string specifying a policy to recursively wrap layers with FSDP. If a string, it must be one of transformer_based_wrap, size_based_wrap, or no_wrap. See torch.distributed.fsdp.wrap.size_based_wrap_policy` for a direction on what it should look like.
  • cpu_offload (Union[bool, torch.distributed.fsdp.CPUOffload, torch.distributed.fsdp.CPUOffloadPolicy], mặc định là False) — Có nên offload tham số sang CPU hay không. Giá trị phải là bool hoặc một instance của torch.distributed.fsdp.fully_sharded_data_parallel.CPUOffload hoặc torch.distributed.fsdp.fully_sharded_data_parallel.CPUOffloadPolicy nếu fsdp_version được đặt thành 2.
  • ignored_modules (Optional[Union[Iterable[torch.nn.Module], str]], mặc định là None) — Một danh sách các module cần bỏ qua khi bọc bằng FSDP. Khi truyền một chuỗi, các module sẽ được khớp theo tên bằng cách sử dụng regex fullmatch. Nếu fsdp_version được đặt thành 2, các module sẽ được chuyển đổi thành các tham số và được sử dụng.
  • state_dict_type (Union[str, torch.distributed.fsdp.StateDictType], mặc định là 'FULL_STATE_DICT') — Loại state dict cần sử dụng. Nếu là chuỗi, nó phải là một trong full_state_dict, local_state_dict hoặc sharded_state_dict.
  • state_dict_config (Optional[Union[torch.distributed.fsdp.FullStateDictConfig, torch.distributed.fsdp.ShardedStateDictConfig], mặc định là None) — Cấu hình state dict cần sử dụng. Nếu không được truyền vào, giá trị này sẽ được xác định dựa trên state_dict_type.
  • optim_state_dict_config (Optional[Union[torch.distributed.fsdp.FullOptimStateDictConfig, torch.distributed.fsdp.ShardedOptimStateDictConfig], mặc định là None) — Cấu hình optim state dict cần sử dụng. Nếu không được truyền vào, giá trị này sẽ được xác định dựa trên state_dict_type.
  • limit_all_gathers (bool, mặc định là True) — Xác định việc FSDP có đồng bộ hóa rõ ràng luồng CPU để ngăn quá nhiều all-gathers đang chạy hay không. Giá trị bool này chỉ ảnh hưởng đến các chiến lược phân mảnh (sharded) có lịch trình all-gathers. Bật tùy chọn này có thể giúp giảm số lần thử lại (retries) của CUDA malloc.
  • use_orig_params (bool, mặc định là False) — Có sử dụng các tham số gốc cho optimizer hay không.
  • param_init_fn (Optional[Callable[[torch.nn.Module], None], mặc định là None) — Một Callable[torch.nn.Module] -> None quy định cách khởi tạo các module hiện đang trên thiết bị meta lên một thiết bị thực. Chỉ áp dụng khi sync_module_states là True. Mặc định là một lambda gọi to_empty trên module.
  • sync_module_states (bool, mặc định là False) — Liệu mỗi đơn vị FSDP được bọc riêng lẻ có nên phát trực tiếp (broadcast) các tham số module từ rank 0 để đảm bảo chúng giống nhau trên tất cả các rank sau khi khởi tạo hay không. Mặc định là False trừ khi cpu_ram_efficient_loading là True, khi đó nó sẽ được bật bắt buộc.
  • forward_prefetch (bool, mặc định là False) — Xác định việc FSDP có thực hiện prefetch (tải trước) cho phép all-gather tiếp theo trong khi thực hiện bước forward hay không. Chỉ sử dụng với đồ thị tĩnh (Static graphs).
  • activation_checkpointing (bool, mặc định là False) — Một kỹ thuật để giảm mức sử dụng bộ nhớ bằng cách xóa các activation của một số lớp nhất định và tính toán lại chúng trong quá trình truyền ngược (backward pass). Về cơ bản, kỹ thuật này đánh đổi thêm thời gian tính toán để đổi lấy việc giảm mức sử dụng bộ nhớ.
  • cpu_ram_efficient_loading (bool, mặc định là None) — Nếu là True, chỉ tiến trình đầu tiên tải checkpoint của model được huấn luyện trước, trong khi tất cả các tiến trình khác có trọng số rỗng. Chỉ áp dụng cho Transformers. Khi sử dụng tính năng này, sync_module_states cần được đặt là True.
  • transformer_cls_names_to_wrap (Optional[List[str]], mặc định là None) — Một danh sách tên lớp của các lớp transformer cần bọc. Chỉ áp dụng khi auto_wrap_policy là transformer_based_wrap.
  • min_num_params (Optional[int], mặc định là None) — Số lượng tham số tối thiểu mà một module phải có để được bọc. Chỉ áp dụng khi auto_wrap_policy là size_based_wrap.

Plugin này được sử dụng để bật song song dữ liệu phân mảnh hoàn toàn.

set_auto_wrap_policy

< >

( model )

Với model, tạo một auto_wrap_policy dựa trên chính sách được truyền vào và nếu chúng ta có thể sử dụng transformer_cls_to_wrap

set_mixed_precision

< >

( mixed_precisionbuffer_autocast = Falseoverride = False )

Đặt chính sách độ chính xác hỗn hợp cho FSDP

set_state_dict_type

< >

( state_dict_type = None )

Đặt cấu hình state dict dựa trên StateDictType.

validate_mixed_precision_policy

< >

( )

Kiểm tra chính sách độ chính xác hỗn hợp, được trừu tượng hóa để không đưa các lệnh import vào nếu không cần thiết.

fsdp2_load_full_state_dict

accelerate.utils.fsdp2_load_full_state_dict

< >

( acceleratormodel: Modulefull_sd: dictcpu_offload: bool = False )

Tham số

  • accelerator (Accelerator) — Bản thể accelerator
  • model (torch.nn.Module) — Mô hình để nạp state dict vào, được kỳ vọng nằm trên meta device, nếu không có thể xảy ra hiện tượng tăng đột biến VRAM
  • full_sd (dict) — Trạng thái dict đầy đủ để tải, chỉ có thể nằm trên rank 0
  • cpu_offload (bool, mặc định là False) — Nếu là True, di chuyển các tham số đã chia mảnh sang CPU sau khi phân phối. Bắt buộc khi bật tính năng offload CPU của FSDP.

Tải toàn bộ state dict (chỉ có thể nằm trên rank 0) vào mô hình đã phân mảnh. Việc này được thực hiện bằng cách phát trực tiếp (broadcast) các tham số từ rank 0 đến tất cả các rank khác. Hàm này sửa đổi mô hình tại chỗ (in-place).

fsdp2_switch_optimizer_parameters

accelerate.utils.fsdp2_switch_optimizer_parameters

< >

( optimizer: Optimizermapping: dict )

Tham số

  • optimizer (torch.optim.Optimizer) — Đối tượng Optimizer chứa các tham số của mô hình gốc
  • mapping (dict) — Ánh xạ từ tham số gốc (được chỉ định bởi data_ptr) sang tham số đã chia

Gây ra

KeyError

  • KeyError — Nếu một tham số trong optimizer không thể được chuyển sang phiên bản sharded. Điều này không bao giờ xảy ra và cho thấy có lỗi. Nếu chúng ta giữ nguyên các tham số gốc thay vì ném ngoại lệ, quá trình huấn luyện sẽ không chính xác về mặt số học và trọng số sẽ không được cập nhật.

Chuyển các tham số của bộ tối ưu sang các tham số mới (tham số phân mảnh trong trường hợp thông thường). Hàm này sửa đổi bộ tối ưu tại chỗ.

fsdp2_prepare_model

accelerate.utils.fsdp2_prepare_model

< >

( acceleratormodel: Module ) → torch.nn.Module

Tham số

  • accelerator (Accelerator) — Bản thể accelerator
  • model (torch.nn.Module) — Mô hình cần chuẩn bị

Trả về

torch.nn.Module

Mô hình đã được chuẩn bị

Chuẩn bị mô hình cho FSDP2 tại chỗ. Đồng thời trả về mô hình để tránh việc sử dụng sai mô hình gốc.

fsdp2_prepare_auto_wrap_policy

Update on GitHub