Skip to content

Commit

Permalink
add ratio threshold to avoid spikes (huggingface#488)
Browse files Browse the repository at this point in the history
  • Loading branch information
lvwerra authored Jul 4, 2023
1 parent aa9770c commit a86eaab
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions trl/trainer/ppo_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ class PPOConfig(object):
default=1,
metadata={"help": "Number of steps between comparison of the current reward with the best seen so far"},
)
ratio_threshold: Optional[float] = field(
default=10.0, metadata={"help": "Skip mini-batches with high PPO ratios that can cause loss spikes"}
)

def __post_init__(self):
if self.forward_batch_size is not None:
Expand Down
9 changes: 9 additions & 0 deletions trl/trainer/ppo_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,15 @@ def loss(

loss = pg_loss + self.config.vf_coef * vf_loss

avg_ratio = masked_mean(ratio, mask).item()
if avg_ratio > self.config.ratio_threshold:
warnings.warn(
f"The average ratio of batch ({avg_ratio:.2f}) exceeds threshold {self.config.ratio_threshold:.2f}. Skipping batch."
)
pg_loss = pg_loss * 0.0
vf_loss = vf_loss * 0.0
loss = loss * 0.0

entropy = masked_mean(entropy_from_logits(logits), mask)
approxkl = 0.5 * masked_mean((logprobs - old_logprobs) ** 2, mask)
policykl = masked_mean(old_logprobs - logprobs, mask)
Expand Down

0 comments on commit a86eaab

Please sign in to comment.