Update rl_replacements.py
This commit is contained in:
parent
673325dea7
commit
2bba4e02d3
1 changed files with 7 additions and 18 deletions
|
|
@ -177,6 +177,7 @@ def grpo_trainer__get_per_token_logps(function_name, function):
|
|||
if function_name != "_get_per_token_logps": return function
|
||||
|
||||
def _get_per_token_logps(self, model, input_ids, attention_mask, logits_to_keep):
|
||||
return None
|
||||
if not hasattr(self, '_autocast_dtype'):
|
||||
self._autocast_dtype = torch.float16 if os.environ.get('ACCELERATE_MIXED_PRECISION', 'fp16') == 'fp16' else torch.bfloat16
|
||||
with torch.amp.autocast(device_type = 'cuda', dtype = self._autocast_dtype):
|
||||
|
|
@ -221,7 +222,7 @@ def grpo_trainer_compute_loss(function_name, function):
|
|||
logits_to_keep = completion_ids.size(1) # we only need to compute the logits for the completion tokens
|
||||
_input_ids = input_ids
|
||||
_logits_to_keep = logits_to_keep
|
||||
per_token_logps = self._get_per_token_logps(model, input_ids, attention_mask, logits_to_keep)
|
||||
# per_token_logps = self._get_per_token_logps(model, input_ids, attention_mask, logits_to_keep)
|
||||
|
||||
# Compute the KL divergence between the model and the reference model
|
||||
ref_per_token_logps = inputs["ref_per_token_logps"]
|
||||
|
|
@ -233,25 +234,13 @@ def grpo_trainer_compute_loss(function_name, function):
|
|||
# per_token_loss = -(per_token_loss - self.beta * per_token_kl)
|
||||
# loss = ((per_token_loss * completion_mask).sum(dim=1) / completion_mask.sum(dim=1)).mean()
|
||||
input_ids = input_ids[:, -logits_to_keep:]
|
||||
loss, completion_length, mean_kl = grpo_compute_loss(
|
||||
ref_per_token_logps, per_token_logps, input_ids, completion_mask, self.beta, advantages, bsz,
|
||||
)
|
||||
# loss, completion_length, mean_kl = grpo_compute_loss(
|
||||
# ref_per_token_logps, per_token_logps, input_ids, completion_mask, self.beta, advantages, bsz,
|
||||
# )
|
||||
accumulated_loss, accumulated_completion_length, accumulated_mean_kl = grpo_accumulated_loss(
|
||||
self, _input_ids, logits_to_keep, completion_mask, advantages, n_chunks = 1,
|
||||
self, _input_ids, logits_to_keep, completion_mask, advantages, n_chunks = 2,
|
||||
)
|
||||
print("loss", loss, accumulated_loss)
|
||||
print("completion_length", completion_length, accumulated_completion_length)
|
||||
print("mean_kl", mean_kl, accumulated_mean_kl)
|
||||
|
||||
from unsloth_zoo.rl_replacements import RL_REPLACEMENTS
|
||||
RL_REPLACEMENTS["data"] = (
|
||||
ref_per_token_logps.detach(), per_token_logps.detach(), _input_ids, completion_mask, self.beta, advantages,
|
||||
loss.detach(), completion_length, mean_kl, completion_ids, _logits_to_keep,
|
||||
)
|
||||
if "count" in RL_REPLACEMENTS:
|
||||
RL_REPLACEMENTS["count"] += 1
|
||||
if RL_REPLACEMENTS["count"] == 10: raise
|
||||
else: RL_REPLACEMENTS["count"] = 1
|
||||
loss, completion_length, mean_kl = accumulated_loss, accumulated_completion_length, accumulated_mean_kl
|
||||
# Log the metrics
|
||||
# completion_length = self.accelerator.gather_for_metrics(completion_mask.sum(1)).float().mean().item()
|
||||
self._metrics["completion_length"].append(completion_length.item())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue