Skip to content
Merged
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
6 changes: 5 additions & 1 deletion nemo_rl/algorithms/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@


def calculate_kl_penalty_joschu2020(
logprobs_policy: torch.Tensor, logprobs_reference: torch.Tensor
logprobs_policy: torch.Tensor,
logprobs_reference: torch.Tensor,
clamp_value: Optional[float] = 20.0,
) -> torch.Tensor:
"""Calculates a per-token estimate of the KL Divergence between two log_probs.

Expand All @@ -41,6 +43,8 @@ def calculate_kl_penalty_joschu2020(
logprobs_reference: torch.Tensor (b, s)
"""
r = logprobs_reference - logprobs_policy
if clamp_value is not None:
r = r.clamp(min=-clamp_value, max=clamp_value)
return torch.exp(r) - r - 1


Expand Down
Loading