fix: compute low-precision KL metrics in float32

This commit is contained in:
Rudy Mizrahi Celekli 2026-11-02 14:05:04 -05:00
parent 0db5c31e10
commit e134cbccef
2 changed files with 81 additions and 0 deletions

View File

@ -1,6 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright (C) 2025-2026 Philipp Emanuel Weidmann <pew@worldwidemann.com> + contributors
import torch
import torch.nn.functional as F
from pydantic import BaseModel, Field
@ -49,11 +50,15 @@ class KLDivergence(Scorer):
print("* Obtaining baseline first-token probability distributions...")
baseline_logits = ctx.get_logits(self.prompts)
if baseline_logits.dtype in (torch.float16, torch.bfloat16):
baseline_logits = baseline_logits.float()
self._baseline_logprobs = F.log_softmax(baseline_logits, dim=-1)
def get_score(self, ctx: Context) -> Score:
logits = ctx.get_logits(self.prompts)
if logits.dtype in (torch.float16, torch.bfloat16):
logits = logits.float()
logprobs = F.log_softmax(logits, dim=-1)
kl_divergence = F.kl_div(

View File

@ -0,0 +1,76 @@
import unittest
from unittest.mock import Mock
import torch
import torch.nn.functional as F
from heretic.config import InitOnlySettings
from heretic.plugin import Context
from heretic.scorers.kl_divergence import KLDivergence, Settings
from heretic.utils import Prompt
class KLPrecisionTests(unittest.TestCase):
def scorer(self, baseline):
context = Mock(spec=Context)
context.load_prompts.return_value = [Prompt("", "fixture")]
context.get_logits.return_value = baseline
scorer = KLDivergence(InitOnlySettings(model="unused"), Settings())
scorer.init(context)
return scorer, context
def reference(self, baseline, current):
return F.kl_div(
F.log_softmax(current.double(), dim=-1),
F.log_softmax(baseline.double(), dim=-1),
reduction="batchmean",
log_target=True,
).item()
def test_bfloat16_scores_preserve_nearby_trial_order(self):
baseline = torch.tensor([[0.5, 0.0]], dtype=torch.bfloat16)
scorer, context = self.scorer(baseline)
scores = []
for difference in [0.015625, 0.03125]:
current = torch.tensor([[0.5 + difference, 0.0]], dtype=torch.bfloat16)
context.get_logits.return_value = current
score = scorer.get_score(context).value
self.assertAlmostEqual(score, self.reference(baseline, current), delta=1e-7)
scores.append(score)
self.assertLess(scores[0], scores[1])
self.assertGreater(scores[0], 0.0)
def test_identical_low_precision_logits_score_zero(self):
for dtype in [torch.bfloat16, torch.float16]:
with self.subTest(dtype=dtype):
baseline = torch.tensor([[0.5, 0.0]], dtype=dtype)
scorer, context = self.scorer(baseline)
self.assertEqual(scorer.get_score(context).value, 0.0)
def test_full_precision_logits_remain_unchanged(self):
for dtype in [torch.float32, torch.float64]:
with self.subTest(dtype=dtype):
baseline = torch.tensor([[0.5, 0.0]], dtype=dtype)
current = torch.tensor([[0.7, 0.0]], dtype=dtype)
scorer, context = self.scorer(baseline)
context.get_logits.return_value = current
expected = F.kl_div(
F.log_softmax(current, dim=-1),
F.log_softmax(baseline, dim=-1),
reduction="batchmean",
log_target=True,
).item()
self.assertEqual(scorer.get_score(context).value, expected)
def test_bfloat16_metric_does_not_prefer_the_farther_trial(self):
baseline = torch.tensor([[0.5, 0.0]], dtype=torch.bfloat16)
scorer, context = self.scorer(baseline)
values = []
references = []
for value in [0.515625, 0.53125]:
current = torch.tensor([[value, 0.0]], dtype=torch.bfloat16)
context.get_logits.return_value = current
values.append(scorer.get_score(context).value)
references.append(self.reference(baseline, current))
self.assertLess(references[0], references[1])
self.assertLess(values[0], values[1])