use serde::Serialize;
pub const REPEAT_EPSILON: f64 = 0.0;
pub const LOOP_TRIP_CAP: usize = 500;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum GridSource {
TokenStream,
LabelMe,
}
pub const AUTHORITATIVE_GRID_SOURCE: GridSource = GridSource::TokenStream;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum UncoveredPosition {
KeepHole,
FillWithBoundingBox,
ExtendSpan,
DropSample,
}
pub const UNCOVERED_POSITION_RULE: UncoveredPosition = UncoveredPosition::KeepHole;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub struct UpperBound {
pub span_beyond_vocabulary: bool,
pub beyond_loop_trip_cap: bool,
}
impl UpperBound {
pub fn is_deducted(&self) -> bool {
self.span_beyond_vocabulary || self.beyond_loop_trip_cap
}
pub fn reason(&self) -> Option<&'static str> {
if self.span_beyond_vocabulary {
Some("span_beyond_vocabulary")
} else if self.beyond_loop_trip_cap {
Some("beyond_loop_trip_cap")
} else {
None
}
}
}
pub fn classify_upper_bound(span_beyond_vocabulary: bool, structure_tokens: usize) -> UpperBound {
UpperBound {
span_beyond_vocabulary,
beyond_loop_trip_cap: structure_tokens > LOOP_TRIP_CAP,
}
}
pub fn within_repeat_epsilon(left: f64, right: f64) -> bool {
if REPEAT_EPSILON == 0.0 {
left.to_bits() == right.to_bits()
} else {
(left - right).abs() <= REPEAT_EPSILON
}
}
#[derive(Debug, Clone, Serialize)]
pub struct ProtocolSection {
pub document: &'static str,
pub repeat_epsilon: f64,
pub repeat_epsilon_meaning: &'static str,
pub authoritative_grid_source: GridSource,
pub uncovered_position_rule: UncoveredPosition,
pub loop_trip_cap: usize,
pub loop_trip_cap_source: &'static str,
pub upper_bound_policy: &'static str,
}
pub fn protocol_section() -> ProtocolSection {
ProtocolSection {
document: "docs/10-table-evaluation-protocol.md",
repeat_epsilon: REPEAT_EPSILON,
repeat_epsilon_meaning: "0 means bit-identical, not approximately equal; a non-zero \
difference between two same-configuration runs is a \
nondeterminism defect, not noise",
authoritative_grid_source: AUTHORITATIVE_GRID_SOURCE,
uncovered_position_rule: UNCOVERED_POSITION_RULE,
loop_trip_cap: LOOP_TRIP_CAP,
loop_trip_cap_source: "the `Loop` node's trip-count constant in the ONNX graph; ORT does \
not expose graph internals, so this is a constant whose staleness \
shows up as a growing deducted-sample count",
upper_bound_policy: "samples whose truth cannot be expressed by the model (span beyond \
the vocabulary, or a structure stream longer than the loop trip \
cap) are counted in the headline metric but excluded from the \
regression criterion",
}
}
#[cfg(test)]
mod tests {
use super::{
GridSource, LOOP_TRIP_CAP, REPEAT_EPSILON, UNCOVERED_POSITION_RULE, UncoveredPosition,
UpperBound, classify_upper_bound, protocol_section, within_repeat_epsilon,
};
#[test]
fn repeat_epsilon_is_zero_because_the_runs_are_deterministic() {
assert_eq!(REPEAT_EPSILON, 0.0);
}
#[test]
fn zero_epsilon_compares_bits_not_values() {
assert!(within_repeat_epsilon(0.706518, 0.706518));
assert!(!within_repeat_epsilon(0.706518, 0.706519));
assert!(!within_repeat_epsilon(0.0, -0.0));
}
#[test]
fn a_positive_epsilon_would_fall_back_to_a_numeric_tolerance() {
let epsilon = 0.01_f64;
let close = |left: f64, right: f64| (left - right).abs() <= epsilon;
assert!(close(0.7065, 0.7100));
assert!(!close(0.7065, 0.7200));
}
#[test]
fn the_grid_source_is_the_token_stream() {
assert_eq!(super::AUTHORITATIVE_GRID_SOURCE, GridSource::TokenStream);
}
#[test]
fn holes_are_kept_rather_than_filled_or_dropped() {
assert_eq!(UNCOVERED_POSITION_RULE, UncoveredPosition::KeepHole);
}
#[test]
fn the_loop_cap_is_the_measured_graph_constant() {
assert_eq!(LOOP_TRIP_CAP, 500);
}
#[test]
fn a_table_at_the_cap_is_not_yet_deducted() {
let at_cap = classify_upper_bound(false, LOOP_TRIP_CAP);
assert!(!at_cap.beyond_loop_trip_cap);
assert!(!at_cap.is_deducted());
assert_eq!(at_cap.reason(), None);
let past_cap = classify_upper_bound(false, LOOP_TRIP_CAP + 1);
assert!(past_cap.beyond_loop_trip_cap);
assert!(past_cap.is_deducted());
assert_eq!(past_cap.reason(), Some("beyond_loop_trip_cap"));
}
#[test]
fn a_span_beyond_the_vocabulary_is_deducted_and_reported_first() {
let both = classify_upper_bound(true, LOOP_TRIP_CAP + 1);
assert!(both.is_deducted());
assert_eq!(both.reason(), Some("span_beyond_vocabulary"));
}
#[test]
fn an_ordinary_table_is_not_deducted() {
let ordinary = classify_upper_bound(false, 48);
assert_eq!(
ordinary,
UpperBound {
span_beyond_vocabulary: false,
beyond_loop_trip_cap: false,
}
);
assert!(!ordinary.is_deducted());
}
#[test]
fn the_protocol_section_carries_its_own_document_reference() {
let section = protocol_section();
assert_eq!(section.document, "docs/10-table-evaluation-protocol.md");
assert_eq!(section.repeat_epsilon, REPEAT_EPSILON);
assert_eq!(section.loop_trip_cap, LOOP_TRIP_CAP);
assert_eq!(section.authoritative_grid_source, GridSource::TokenStream);
assert_eq!(section.uncovered_position_rule, UncoveredPosition::KeepHole);
let value = serde_json::to_value(§ion).expect("protocol section is serializable");
assert_eq!(value["authoritative_grid_source"], "token_stream");
assert_eq!(value["uncovered_position_rule"], "keep_hole");
assert!(value["upper_bound_policy"].is_string());
}
}