mirror of
https://github.com/hpcaitech/ColossalAI.git
synced 2025-04-28 03:43:01 +00:00
* feat: modify forward fn of critic and reward model * feat: modify calc_action_log_probs * to: add wandb in sft and rm trainer * feat: update train_sft * feat: update train_rm * style: modify type annotation and add warning * feat: pass tokenizer to ppo trainer * to: modify trainer base and maker base * feat: add wandb in ppo trainer * feat: pass tokenizer to generate * test: update generate fn tests * test: update train tests * fix: remove action_mask * feat: remove unused code * fix: fix wrong ignore_index * fix: fix mock tokenizer * chore: update requirements * revert: modify make_experience * fix: fix inference * fix: add padding side * style: modify _on_learn_batch_end * test: use mock tokenizer * fix: use bf16 to avoid overflow * fix: fix workflow * [chat] fix gemini strategy * [chat] fix * sync: update colossalai strategy * fix: fix args and model dtype * fix: fix checkpoint test * fix: fix requirements * fix: fix missing import and wrong arg * fix: temporarily skip gemini test in stage 3 * style: apply pre-commit * fix: temporarily skip gemini test in stage 1&2 --------- Co-authored-by: Mingyan Jiang <1829166702@qq.com>
70 lines
2.1 KiB
Python
70 lines
2.1 KiB
Python
from typing import Optional, Union
|
|
|
|
import torch
|
|
import torch.nn.functional as F
|
|
|
|
|
|
def _compute_approx_kl(
|
|
log_probs: torch.Tensor, log_probs_base: torch.Tensor, action_mask: Optional[torch.Tensor] = None
|
|
) -> torch.Tensor:
|
|
"""
|
|
Compute the approximate KL divergence between two distributions.
|
|
Schulman blog: http://joschu.net/blog/kl-approx.html
|
|
|
|
Args:
|
|
log_probs: Log probabilities of the new distribution.
|
|
log_probs_base: Log probabilities of the base distribution.
|
|
action_mask: Mask for actions.
|
|
"""
|
|
|
|
log_ratio = log_probs_base - log_probs
|
|
approx_kl = (log_ratio.exp() - 1) - log_ratio
|
|
if action_mask is not None:
|
|
approx_kl = masked_mean(approx_kl, action_mask, dim=1)
|
|
return approx_kl
|
|
approx_kl = approx_kl.mean(dim=1)
|
|
return approx_kl
|
|
|
|
|
|
def compute_reward(
|
|
r: Union[torch.Tensor, float],
|
|
kl_coef: float,
|
|
log_probs: torch.Tensor,
|
|
log_probs_base: torch.Tensor,
|
|
action_mask: Optional[torch.Tensor] = None,
|
|
) -> torch.Tensor:
|
|
if kl_coef <= 0.0:
|
|
return r
|
|
kl = _compute_approx_kl(log_probs, log_probs_base, action_mask=action_mask)
|
|
reward = r - kl_coef * kl
|
|
return reward
|
|
|
|
|
|
def _log_probs_from_logits(logits: torch.Tensor, labels: torch.Tensor) -> torch.Tensor:
|
|
log_probs = F.log_softmax(logits, dim=-1)
|
|
log_probs_labels = log_probs.gather(dim=-1, index=labels.unsqueeze(-1))
|
|
return log_probs_labels.squeeze(-1)
|
|
|
|
|
|
def calc_action_log_probs(logits: torch.Tensor, sequences: torch.LongTensor, num_actions: int) -> torch.Tensor:
|
|
"""Calculate action log probs.
|
|
|
|
Args:
|
|
output (torch.Tensor): Output tensor of Actor.forward.logits.
|
|
sequences (torch.LongTensor): Input sequences.
|
|
num_actions (int): Number of actions.
|
|
|
|
Returns:
|
|
torch.Tensor: Action log probs.
|
|
"""
|
|
log_probs = _log_probs_from_logits(logits[:, :-1, :], sequences[:, 1:])
|
|
return log_probs[:, -num_actions:]
|
|
|
|
|
|
def masked_mean(tensor: torch.Tensor, mask: torch.Tensor, dim: int = 1) -> torch.Tensor:
|
|
tensor = tensor * mask
|
|
tensor = tensor.sum(dim=dim)
|
|
mask_sum = mask.sum(dim=dim)
|
|
mean = tensor / (mask_sum + 1e-8)
|
|
return mean
|