Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion swift/rlhf_trainers/ppo_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from swift.trainers import SwiftMixin
from swift.utils import patch_getattr
from swift.callbacks import callbacks_map

if version.parse(trl.__version__) >= version.parse('0.26.0'):
from trl.experimental.ppo import PPOTrainer as HFPPOTrainer
Expand Down Expand Up @@ -49,7 +50,6 @@ def __init__(self, model: PreTrainedModel, ref_model: PreTrainedModel, *_args, *
'reward_model',
'value_model',
'eval_dataset',
'callbacks',
]
}
parameters = inspect.signature(ppo_trainer_init).parameters
Expand All @@ -62,6 +62,12 @@ def __init__(self, model: PreTrainedModel, ref_model: PreTrainedModel, *_args, *
else:
new_kwargs['tokenizer'] = self.tokenizer
ppo_trainer_init(self, model=model, ref_model=ref_model, **new_kwargs)
# TRL's `PPOTrainer.__init__` rebuilds `callback_handler`, discarding the user callbacks that
# `SwiftMixin` registered on the HF Trainer. Re-register them onto the new handler here. We
# call `add_callback` directly (rather than `SwiftMixin._add_callbacks`/`_get_callbacks`,
# whose names/signatures differ between swift branches) so this works across versions.
for callback in self.args.callbacks:
self.add_callback(callbacks_map[callback](self.args, self))
unwrap_model = self.accelerator.unwrap_model(self.model)
patch_getattr(unwrap_model.__class__, 'policy')

Expand Down