|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Test for KL functions""" |
| 3 | + |
| 4 | +import unittest |
| 5 | + |
| 6 | +import torch |
| 7 | + |
| 8 | +from trinity.algorithm.kl_fn.kl_fn import KL_FN |
| 9 | + |
| 10 | + |
| 11 | +class KLFnTest(unittest.TestCase): |
| 12 | + def setUp(self): |
| 13 | + seed = 42 |
| 14 | + torch.manual_seed(seed) |
| 15 | + torch.cuda.manual_seed(seed) |
| 16 | + torch.cuda.manual_seed_all(seed) |
| 17 | + torch.backends.cudnn.deterministic = True |
| 18 | + torch.backends.cudnn.benchmark = False |
| 19 | + |
| 20 | + shape = (4, 10) |
| 21 | + self.logprob = 2 * torch.rand(shape) - 1 |
| 22 | + self.ref_logprob = 2 * torch.rand(shape) - 1 |
| 23 | + self.old_logprob = 2 * torch.rand(shape) - 1 |
| 24 | + self.response_mask = torch.rand(shape) > 0.5 |
| 25 | + |
| 26 | + def test_k1_kl_fn(self): |
| 27 | + kl_fn_cls = KL_FN.get("k1") |
| 28 | + kl_fn = kl_fn_cls(kl_coef=0.01) |
| 29 | + kl = kl_fn.calculate_kl(self.logprob, self.ref_logprob) |
| 30 | + expected_kl = self.logprob - self.ref_logprob |
| 31 | + self.assertTrue(torch.allclose(kl, expected_kl)) |
| 32 | + |
| 33 | + def test_k2_kl_fn(self): |
| 34 | + kl_fn_cls = KL_FN.get("k2") |
| 35 | + kl_fn = kl_fn_cls(kl_coef=0.01) |
| 36 | + kl = kl_fn.calculate_kl(self.logprob, self.ref_logprob) |
| 37 | + expected_kl = (self.logprob - self.ref_logprob).square() * 0.5 |
| 38 | + self.assertTrue(torch.allclose(kl, expected_kl)) |
| 39 | + |
| 40 | + def test_k3_kl_fn(self): |
| 41 | + kl_fn_cls = KL_FN.get("k3") |
| 42 | + kl_fn = kl_fn_cls(kl_coef=0.01) |
| 43 | + kl = kl_fn.calculate_kl(self.logprob, self.ref_logprob) |
| 44 | + logr = self.ref_logprob - self.logprob |
| 45 | + expected_kl = logr.exp() - 1 - logr |
| 46 | + self.assertTrue(torch.allclose(kl, expected_kl)) |
| 47 | + |
| 48 | + def test_abs_kl_fn(self): |
| 49 | + kl_fn_cls = KL_FN.get("abs") |
| 50 | + kl_fn = kl_fn_cls(kl_coef=0.01) |
| 51 | + kl = kl_fn.calculate_kl(self.logprob, self.ref_logprob) |
| 52 | + expected_kl = torch.abs(self.logprob - self.ref_logprob) |
| 53 | + self.assertTrue(torch.allclose(kl, expected_kl)) |
| 54 | + |
| 55 | + def test_low_var_kl_fn(self): |
| 56 | + kl_fn_cls = KL_FN.get("low_var_kl") |
| 57 | + kl_fn = kl_fn_cls(kl_coef=0.01) |
| 58 | + kl = kl_fn.calculate_kl(self.logprob, self.ref_logprob) |
| 59 | + kl_intermediate = self.ref_logprob - self.logprob |
| 60 | + kl_intermediate = torch.clamp(kl_intermediate, min=-20, max=20) |
| 61 | + ratio = torch.exp(kl_intermediate) |
| 62 | + expected_kl = torch.clamp((ratio - kl_intermediate - 1).contiguous(), min=-10, max=10) |
| 63 | + self.assertTrue(torch.allclose(kl, expected_kl)) |
| 64 | + |
| 65 | + def test_dummy_kl_fn(self): |
| 66 | + kl_fn_cls = KL_FN.get("none") |
| 67 | + kl_fn = kl_fn_cls(kl_coef=0.01) |
| 68 | + kl = kl_fn.calculate_kl(self.logprob, self.ref_logprob) |
| 69 | + expected_kl = torch.zeros_like(self.logprob) |
| 70 | + self.assertTrue(torch.allclose(kl, expected_kl)) |
| 71 | + |
| 72 | + def test_corrected_k3_fallback(self): |
| 73 | + k3_fn = KL_FN.get("k3")(kl_coef=0.01) |
| 74 | + corrected_k3_fn = KL_FN.get("corrected_k3")(kl_coef=0.01) |
| 75 | + kl_standard = k3_fn.calculate_kl(self.logprob, self.ref_logprob) |
| 76 | + kl_corrected_no_old = corrected_k3_fn.calculate_kl( |
| 77 | + self.logprob, self.ref_logprob, old_logprob=None |
| 78 | + ) |
| 79 | + self.assertTrue(torch.allclose(kl_standard, kl_corrected_no_old)) |
| 80 | + |
| 81 | + def test_corrected_k3_with_old_logprob(self): |
| 82 | + corrected_k3_fn = KL_FN.get("corrected_k3")(kl_coef=0.01) |
| 83 | + kl_corrected = corrected_k3_fn.calculate_kl( |
| 84 | + self.logprob, self.ref_logprob, self.old_logprob |
| 85 | + ) |
| 86 | + logr = self.ref_logprob - self.logprob |
| 87 | + kl_standard = logr.exp() - 1 - logr |
| 88 | + log_ratio_is = self.logprob - self.old_logprob |
| 89 | + ratio_is = log_ratio_is.exp() |
| 90 | + ratio_is = torch.clamp(ratio_is, min=0.0, max=2.0) |
| 91 | + expected_kl = ratio_is * kl_standard |
| 92 | + self.assertTrue(torch.allclose(kl_corrected, expected_kl)) |
| 93 | + |
| 94 | + def test_corrected_k3_same_policy(self): |
| 95 | + k3_fn = KL_FN.get("k3")(kl_coef=0.01) |
| 96 | + corrected_k3_fn = KL_FN.get("corrected_k3")(kl_coef=0.01) |
| 97 | + kl_standard = k3_fn.calculate_kl(self.logprob, self.ref_logprob) |
| 98 | + kl_corrected = corrected_k3_fn.calculate_kl(self.logprob, self.ref_logprob, self.logprob) |
| 99 | + self.assertTrue(torch.allclose(kl_standard, kl_corrected, rtol=1e-4, atol=1e-6)) |
| 100 | + |
| 101 | + def test_corrected_k3_loss(self): |
| 102 | + corrected_k3_fn = KL_FN.get("corrected_k3")(kl_coef=0.01) |
| 103 | + kl_loss, metrics = corrected_k3_fn.calculate_kl_loss( |
| 104 | + logprob=self.logprob, |
| 105 | + ref_logprob=self.ref_logprob, |
| 106 | + response_mask=self.response_mask, |
| 107 | + loss_agg_mode="token-mean", |
| 108 | + old_logprob=self.old_logprob, |
| 109 | + ) |
| 110 | + self.assertEqual(kl_loss.dim(), 0) |
| 111 | + self.assertIn("kl_loss", metrics) |
| 112 | + self.assertIn("kl_coef", metrics) |
| 113 | + self.assertEqual(metrics["kl_coef"], 0.01) |
| 114 | + |
| 115 | + def test_kl_loss_aggregation_modes(self): |
| 116 | + corrected_k3_fn = KL_FN.get("corrected_k3")(kl_coef=0.01) |
| 117 | + kl_loss_mean, _ = corrected_k3_fn.calculate_kl_loss( |
| 118 | + logprob=self.logprob, |
| 119 | + ref_logprob=self.ref_logprob, |
| 120 | + response_mask=self.response_mask, |
| 121 | + loss_agg_mode="token-mean", |
| 122 | + old_logprob=self.old_logprob, |
| 123 | + ) |
| 124 | + kl_loss_sum, _ = corrected_k3_fn.calculate_kl_loss( |
| 125 | + logprob=self.logprob, |
| 126 | + ref_logprob=self.ref_logprob, |
| 127 | + response_mask=self.response_mask, |
| 128 | + loss_agg_mode="seq-mean-token-sum", |
| 129 | + old_logprob=self.old_logprob, |
| 130 | + ) |
| 131 | + self.assertGreater(kl_loss_sum.item(), kl_loss_mean.item()) |
0 commit comments