Skip to content

Commit

Permalink
Before the first training step, the model has no optimizer: fix ds3
Browse files Browse the repository at this point in the history
  • Loading branch information
qgallouedec committed Feb 6, 2025
1 parent e95f9fb commit bf6e7ed
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions trl/models/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ def setup_chat_format(

def remove_hooks(model: "DeepSpeedEngine") -> None:
"""Removes the optimizer hooks from a DeepSpeed ZeRO-3 model."""
if not hasattr(model, "optimizer"): # before the first training step, the model has no optimizer
return
if model.optimizer is not None and hasattr(model.optimizer, "parameter_offload"):
optimizer_offload = model.optimizer.parameter_offload
elif model.optimizer is not None:
Expand Down Expand Up @@ -164,6 +166,8 @@ def iter_params(module, recurse=False):

def add_hooks(model: "DeepSpeedEngine") -> None:
"""Adds the optimizer hooks from a DeepSpeed ZeRO-3 model."""
if not hasattr(model, "optimizer"): # before the first training step, the model has no optimizer
return
if model.optimizer is not None and hasattr(model.optimizer, "parameter_offload"):
optimizer_offload = model.optimizer.parameter_offload
elif model.optimizer is not None:
Expand Down

0 comments on commit bf6e7ed

Please sign in to comment.