sungguk's picture
Release visual answerability benchmark v1.0.0
e1ced61 verified
Raw History Blame Contribute Delete
4.9 kB
"""Pinned source and private-API contract for the local PAPO-G adapter."""
from __future__ import annotations
import inspect
from typing import Any
PAPO_ADAPTER_VERSION = "trl_0.29.1_papo_g_on_trl_1.9.1_v1"
PAPO_UPSTREAM_SOURCE_URL = (
"https://github.com/huggingface/trl/blob/v0.29.1/trl/experimental/papo/papo_trainer.py"
)
PAPO_UPSTREAM_SOURCE_SHA256 = "9836f4505f7026c90cd5ca5b97151dd14c1bc27bcd2bc5a8fca6626c68279175"
# The gamma=0 reduction must recover the parent GRPO loss *bit-exactly* and the
# parent gradient *up to floating-point roundoff*. ``zero_gamma_loss`` is built as
# ``policy_loss - 0.0 * perception_term``, so its value is bit-identical to
# ``policy_loss`` (the forward loss diff is exactly 0.0). The two
# ``torch.autograd.grad`` calls therefore differentiate the *same* loss; the
# gamma-zero gradient agreement is an autograd *reproducibility* check (two
# passes over an identical loss whose graph structures differ), NOT the leak
# detector — a perception leak is caught by the liveness half of the contract
# (``perception_kl > 0`` / ``perception_gradient_norm > 0``). The two passes
# accumulate in different orders, so their gradients differ only by roundoff.
#
# Training runs in **bf16** mixed precision, not fp32. bf16 mantissa eps is
# ~0.78% and the GPU atomic-add reductions used in backward differ in order
# between the two passes, so the gradient roundoff is ~bf16-eps relative: a few
# percent of the parent gradient magnitude, i.e. ~1e-6 at normal parent
# magnitudes. This is observed directly on phase-2 (the resume-into-trained-
# policy arm): parent gradient ~5e-5, gamma-zero diff 1.19e-6 = 2.4% relative.
# The earlier calibration (a 1e-8 absolute floor assuming fp32 ~1e-10 roundoff)
# was set for the phase-1 *init-adapter* regime, where gradients sit at the
# float noise floor (~1e-12 diff); phase-2's restored *trained* policy (after
# the TRL ref-adapter resume fix) produces normal-magnitude gradients that
# expose the bf16 roundoff the fp32 floor could not absorb.
#
# A real perception leak instead produces a gradient diff on the order of
# ``perception_gradient_norm`` (typically 1e-4..1e-2 on a perception-live batch),
# ~3-4 orders of magnitude above the bf16 roundoff, so a bf16-calibrated
# tolerance still cleanly separates leak from roundoff. ``REL_TOL`` is set to
# ~2x the observed bf16 relative roundoff (5% vs the ~2.4% measured), carrying
# the normal/large-parent regime; ``ABS_FLOOR`` carries the tiny-parent regime
# where ``REL_TOL * parent`` underflows below the bf16 roundoff floor. A real
# leak (~100% relative of the parent) exceeds ``REL_TOL`` by ~20x and is still
# rejected.
GAMMA_ZERO_GRADIENT_REL_TOL = 5e-2
GAMMA_ZERO_GRADIENT_ABS_FLOOR = 1e-6
# The liveness half of the contract (``perception_kl > 0`` and
# ``perception_gradient_norm > 0``) is meaningful only on a batch whose
# completion logits actually depend on the image. A 60% random pixel mask can
# leave the per-token logps bit-exact identical when the mask hits only
# non-semantic regions or the answer is question-determined — a legitimate,
# data-dependent "image-invariant" batch on which both quantities are exactly
# 0.0 and a 0.0 gradient is the *correct* result, not a wiring failure. The
# probe therefore does NOT finalize on the first batch: it keeps the
# ``_probe_pending`` flag set, defers the expensive ``autograd.grad`` passes,
# and re-evaluates on subsequent batches until it finds one with real
# perception signal. Only after this many consecutive image-invariant batches
# (each reading ``mean_kl == 0``) does it conclude the masked forward never
# affects the logits — which would indicate a genuine masking/forward wiring
# bug, or an all-image-invariant probe batch order, and fail loudly. This bound
# is smoke-only: main runs set ``require_gpu_contract_probe`` false.
PAPO_PROBE_MAX_DEGENERATE_BATCHES = 20
def validate_trl_parent_contract(grpo_trainer_class: Any) -> None:
"""Fail when TRL's private methods no longer match the audited adapter."""
loss_parameters = tuple(inspect.signature(grpo_trainer_class._compute_loss).parameters)
if loss_parameters != ("self", "model", "inputs"):
raise RuntimeError(f"unsupported GRPOTrainer._compute_loss signature: {loss_parameters}")
logp_parameters = set(
inspect.signature(grpo_trainer_class._get_per_token_logps_and_entropies).parameters
)
required = {
"self",
"model",
"input_ids",
"attention_mask",
"logits_to_keep",
"compute_entropy",
"compute_aux_loss",
"pixel_values",
"image_grid_thw",
"spatial_shapes",
"image_position_ids",
}
missing = sorted(required - logp_parameters)
if missing:
raise RuntimeError(
"unsupported GRPOTrainer log-probability API; missing " + ", ".join(missing)
)