Skip to content

Commit

Permalink
Update accelerator.py (#2295)
Browse files Browse the repository at this point in the history
  • Loading branch information
stas00 committed Jan 9, 2024
1 parent 2241725 commit 4420ec6
Showing 1 changed file with 38 additions and 12 deletions.
50 changes: 38 additions & 12 deletions src/accelerate/accelerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1516,21 +1516,47 @@ def _prepare_deepspeed(self, *args):
)

if model is not None:
if hasattr(model, "config"):
hidden_size = (
max(model.config.hidden_sizes)
if getattr(model.config, "hidden_sizes", None)
else getattr(model.config, "hidden_size", None)
ds_config = deepspeed_plugin.deepspeed_config
# deal with config keys that use `auto` value and rely on model's hidden_size
hidden_size_based_keys = [
"zero_optimization.reduce_bucket_size",
"zero_optimization.stage3_prefetch_bucket_size",
"zero_optimization.stage3_param_persistence_threshold",
]

def is_auto(ds_config, ds_key_long):
nodes = ds_key_long.split(".")
val = ds_config.get(nodes[0], {}).get(nodes[1], None)
return False if None else val == "auto"

hidden_size_auto_keys = [x for x in hidden_size_based_keys if is_auto(ds_config, x)]
if len(hidden_size_auto_keys) > 0:
reasoning = (
"therefore it's not possible to automatically fill out the following `auto` entries "
+ f"in the DeepSpeed config file: {hidden_size_auto_keys}. You can fix that by replacing "
+ "`auto` values for these keys with an integer value of your choice."
)
if hidden_size is not None:
config_kwargs.update(
{
"zero_optimization.reduce_bucket_size": hidden_size * hidden_size,
"zero_optimization.stage3_prefetch_bucket_size": 0.9 * hidden_size * hidden_size,
"zero_optimization.stage3_param_persistence_threshold": 10 * hidden_size,
}
if not hasattr(model, "config"):
raise ValueError("Can't find `model.config` entry, " + reasoning)

if hasattr(model.config, "hidden_size"):
hidden_size = model.config.hidden_size
elif hasattr(model.config, "hidden_sizes"):
# if there are many hidden sizes pick the largest one
hidden_size = max(model.config.hidden_sizes)
else:
raise ValueError(
"Can't find neither `model.config.hidden_size` nor `model.config.hidden_sizes`, " + reasoning
)

config_kwargs.update(
{
"zero_optimization.reduce_bucket_size": hidden_size * hidden_size,
"zero_optimization.stage3_prefetch_bucket_size": 0.9 * hidden_size * hidden_size,
"zero_optimization.stage3_param_persistence_threshold": 10 * hidden_size,
}
)

if isinstance(optimizer, (DummyOptim)):
config_kwargs.update(
{"optimizer.params.lr": optimizer.lr, "optimizer.params.weight_decay": optimizer.weight_decay}
Expand Down

0 comments on commit 4420ec6

Please sign in to comment.