Skip to content

Commit

Permalink
fix fsdp auto wrap policy (#1302)
Browse files Browse the repository at this point in the history
* fix fsdp policy

* fix fsdp

* revert

* refactor to be inline with Accelerate
  • Loading branch information
pacman100 authored Dec 27, 2023
1 parent 6fe1aac commit 4023da9
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/peft/utils/other.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,20 @@ def fsdp_auto_wrap_policy(model):

from ..tuners import PrefixEncoder, PromptEmbedding, PromptEncoder

default_transformer_cls_names_to_wrap = (
",".join(model._no_split_modules) if getattr(model, "_no_split_modules", None) is not None else ""
)
transformer_cls_names_to_wrap = os.environ.get(
"FSDP_TRANSFORMER_CLS_TO_WRAP", default_transformer_cls_names_to_wrap
).split(",")
transformer_cls_to_wrap = {PrefixEncoder, PromptEncoder, PromptEmbedding}
for layer_class in transformer_cls_names_to_wrap:
transformer_cls = FullyShardedDataParallelPlugin.get_module_class_from_name(model, layer_class)
if transformer_cls is None:
raise Exception("Could not find the transformer layer class to wrap in the model.")
else:
transformer_cls_to_wrap.add(transformer_cls)

def lambda_policy_fn(module):
if (
len(list(module.named_children())) == 0
Expand All @@ -372,14 +386,7 @@ def lambda_policy_fn(module):
lambda_policy = functools.partial(lambda_auto_wrap_policy, lambda_fn=lambda_policy_fn)
transformer_wrap_policy = functools.partial(
transformer_auto_wrap_policy,
transformer_layer_cls=(
PrefixEncoder,
PromptEncoder,
PromptEmbedding,
FullyShardedDataParallelPlugin.get_module_class_from_name(
model, os.environ.get("FSDP_TRANSFORMER_CLS_TO_WRAP", "")
),
),
transformer_layer_cls=transformer_cls_to_wrap,
)

auto_wrap_policy = functools.partial(_or_policy, policies=[lambda_policy, transformer_wrap_policy])
Expand Down

0 comments on commit 4023da9

Please sign in to comment.