"""Numpy-only drift metrics (testable without a GPU).""" import numpy as np def kl_and_top1(ref_logprobs: np.ndarray, test_logprobs: np.ndarray): """Per-token KL(P_ref || Q_test) in nats and top-1 agreement. Both inputs are [T, vocab] log-probabilities. Renormalised in float64 because the model emits bf16 logits, so the log-softmax is not exactly normalised.""" p = ref_logprobs.astype(np.float64) q = test_logprobs.astype(np.float64) p = p - np.logaddexp.reduce(p, axis=-1, keepdims=True) q = q - np.logaddexp.reduce(q, axis=-1, keepdims=True) kl = np.sum(np.exp(p) * (p - q), axis=-1) kl = np.maximum(kl, 0.0) # clamp tiny negative rounding top1 = np.argmax(p, axis=-1) == np.argmax(q, axis=-1) return kl, top1