sungguk's picture
Release visual answerability benchmark v1.0.0
e1ced61 verified
Raw History Blame Contribute Delete
5.07 kB
"""Certificate-derived and controlled-baseline target mapping."""
from __future__ import annotations
from collections.abc import Mapping
from typing import Any
from .answers import UNANSWERABLE_TOKEN
ANSWERABLE_STATES = frozenset({"FULL", "A_SAME", "A_CHANGED"})
UNANSWERABLE_STATES = frozenset({"U_MISSING", "U_INVALID"})
TARGET_ROLES = frozenset({"TARGET", "TARGET_EVIDENCE", "TARGET_REFERENT", "SUBSTITUTE"})
class TargetMappingError(ValueError):
"""Raised when state, role, and certificate target disagree."""
def target_text(value: Any) -> str:
if isinstance(value, bool):
return "true" if value else "false"
return str(value)
def ours_target(view: Mapping[str, Any], full_answer: Any) -> str:
"""Map a view state to the intervention-GRPO/EVI certificate target."""
state = str(view.get("state", ""))
candidate = view.get("target_canonical")
full_text = target_text(full_answer)
if state in {"FULL", "A_SAME"}:
if candidate is not None and target_text(candidate) != full_text:
raise TargetMappingError(f"{state} target differs from FULL")
return full_text
if state == "A_CHANGED":
if candidate is None:
raise TargetMappingError("A_CHANGED requires target_canonical")
changed = target_text(candidate)
if changed == full_text:
raise TargetMappingError("A_CHANGED target equals FULL")
return changed
if state in UNANSWERABLE_STATES:
if candidate is not None and target_text(candidate) != UNANSWERABLE_TOKEN:
raise TargetMappingError(f"{state} target is not {UNANSWERABLE_TOKEN}")
return UNANSWERABLE_TOKEN
raise TargetMappingError(f"unknown intervention state: {state!r}")
def defacto_target(comparison_role: str, full_answer: Any) -> str:
"""Map controlled comparison roles to DeFacto's blanket-unknown target."""
if comparison_role in {"POSITIVE", "CONTROL"}:
return target_text(full_answer)
if comparison_role in TARGET_ROLES:
return UNANSWERABLE_TOKEN
raise TargetMappingError(f"unknown comparison role: {comparison_role!r}")
def no_recompute_target(view: Mapping[str, Any], full_answer: Any) -> str:
"""Certificate-derived target with the recompute branch dropped.
Identical to :func:`ours_target` except an ``A_CHANGED`` view is answered
with the FULL answer (maintain) rather than the certificate's recomputed
target, while abstention on ``U_MISSING``/``U_INVALID`` is preserved. This
is the "intervention minus recompute" target -- equivalently "DeFacto plus
certificate-derived abstention, no recompute" -- so ablation arms D and J
share it: a single run answers both the recompute-isolation and the
defacto-deconfound claims.
"""
state = str(view.get("state", ""))
candidate = view.get("target_canonical")
full_text = target_text(full_answer)
if state in {"FULL", "A_SAME"}:
if candidate is not None and target_text(candidate) != full_text:
raise TargetMappingError(f"{state} target differs from FULL")
return full_text
if state == "A_CHANGED":
# Drop the recompute branch: maintain the FULL answer despite changed
# evidence. The certificate's recomputed target is intentionally ignored.
return full_text
if state in UNANSWERABLE_STATES:
if candidate is not None and target_text(candidate) != UNANSWERABLE_TOKEN:
raise TargetMappingError(f"{state} target is not {UNANSWERABLE_TOKEN}")
return UNANSWERABLE_TOKEN
raise TargetMappingError(f"unknown intervention state: {state!r}")
def answerable_only_target(view: Mapping[str, Any], full_answer: Any) -> str:
"""Certificate-derived target with the abstention branch dropped.
Identical to :func:`ours_target` except ``U_MISSING``/``U_INVALID`` views
are answered with the FULL answer rather than abstaining, while the
``A_CHANGED`` recompute branch is preserved. This is the "intervention
minus abstention" target (ablation arm C): it isolates the contribution of
certificate-derived abstention by removing it.
"""
state = str(view.get("state", ""))
candidate = view.get("target_canonical")
full_text = target_text(full_answer)
if state in {"FULL", "A_SAME"}:
if candidate is not None and target_text(candidate) != full_text:
raise TargetMappingError(f"{state} target differs from FULL")
return full_text
if state == "A_CHANGED":
if candidate is None:
raise TargetMappingError("A_CHANGED requires target_canonical")
changed = target_text(candidate)
if changed == full_text:
raise TargetMappingError("A_CHANGED target equals FULL")
return changed
if state in UNANSWERABLE_STATES:
# Drop the abstention branch: answer the FULL answer even when the
# certificate marks the evidence missing/invalid.
return full_text
raise TargetMappingError(f"unknown intervention state: {state!r}")