2023-03-28 12:25:36 +00:00
|
|
|
from typing import Optional, Union
|
|
|
|
|
|
|
|
import torch
|
|
|
|
import torch.nn.functional as F
|
|
|
|
|
|
|
|
|
2023-08-02 02:17:36 +00:00
|
|
|
def _compute_approx_kl(log_probs: torch.Tensor,
|
|
|
|
log_probs_base: torch.Tensor,
|
|
|
|
action_mask: Optional[torch.Tensor] = None) -> torch.Tensor:
|
2023-03-28 12:25:36 +00:00
|
|
|
"""
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
2023-08-01 02:21:45 +00:00
|
|
|
log_ratio = log_probs_base - log_probs
|
2023-03-28 12:25:36 +00:00
|
|
|
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
|
2023-08-02 02:17:36 +00:00
|
|
|
kl = _compute_approx_kl(log_probs, log_probs_base, action_mask=action_mask)
|
2023-03-28 12:25:36 +00:00
|
|
|
reward = r - kl_coef * kl
|
|
|
|
return reward
|
|
|
|
|
|
|
|
|
2023-08-02 02:17:36 +00:00
|
|
|
def _log_probs_from_logits(logits: torch.Tensor, labels: torch.Tensor) -> torch.Tensor:
|
2023-03-28 12:25:36 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2023-07-19 14:18:30 +00:00
|
|
|
def calc_action_log_probs(output: torch.Tensor, sequences: torch.LongTensor, num_actions: int) -> torch.Tensor:
|
2023-06-13 05:31:56 +00:00
|
|
|
"""Calculate action log probs.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
output (torch.Tensor): Output tensor of Actor.forward.
|
|
|
|
sequences (torch.LongTensor): Input sequences.
|
|
|
|
num_actions (int): Number of actions.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
torch.Tensor: Action log probs.
|
|
|
|
"""
|
|
|
|
logits = output['logits']
|
2023-08-02 02:17:36 +00:00
|
|
|
log_probs = _log_probs_from_logits(logits[:, :-1, :], sequences[:, 1:])
|
2023-06-13 05:31:56 +00:00
|
|
|
return log_probs[:, -num_actions:]
|
|
|
|
|
|
|
|
|
2023-03-28 12:25:36 +00:00
|
|
|
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
|