metrale_bench/benchmarks/video/
geometry.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3//! 2026-09-26: Video token geometry: tokens per temporal group, and the check
4//! that temporal groups scale with clip duration.
5//!
6//! A clip's absolute token count depends on the server's `--video-fps`, which
7//! the benchmark neither sends nor reads, so the geometry leg asserts a
8//! relation between three durations instead of absolute counts.
9//!
10//! Owner: bench, video.
11//! Invariants: none beyond the types.
12
13/// 2026-09-26: Merged tokens one temporal group of a `w x h` clip occupies:
14/// the still-image count, `vision::geometry::expected_vision_tokens`.
15pub fn tokens_per_group(w: u32, h: u32, patch: u32, merge: u32) -> u32 {
16    crate::benchmarks::vision::geometry::expected_vision_tokens(w, h, patch, merge)
17}
18
19/// 2026-09-26: What the geometry leg concluded.
20#[derive(Debug, Clone, PartialEq, Eq)]
21pub struct Ratio {
22    /// 2026-09-26: Temporal groups inferred for the shortest clip.
23    pub unit_groups: usize,
24    /// 2026-09-26: Template overhead implied, in tokens.
25    pub overhead: usize,
26}
27
28/// 2026-09-26: Check that temporal groups scale with duration, from the totals
29/// of three clips at 1x, 2x and 4x, and infer the group count and template
30/// overhead.
31///
32/// Two totals cannot test this: any difference `t2 - t1` fits a 2:1 ratio
33/// once the implied overhead absorbs the rest. With three the system is
34/// over-determined:
35///
36/// ```text
37///   t4 - t2  ==  2 * (t2 - t1)
38/// ```
39///
40/// which involves neither the overhead nor the tokens-per-group figure. It
41/// also returns `None` when `t2 - t1` is zero or not a whole number of
42/// `plane`s, when `plane` is zero, when the implied overhead is negative, and
43/// on overflow.
44pub fn check_proportional(t1: usize, t2: usize, t4: usize, plane: usize) -> Option<Ratio> {
45    let d21 = t2.checked_sub(t1)?;
46    let d42 = t4.checked_sub(t2)?;
47    if d21 == 0 || d42 != d21.checked_mul(2)? {
48        return None;
49    }
50    if plane == 0 || d21 % plane != 0 {
51        return None;
52    }
53    let unit_groups = d21 / plane;
54    // 2026-09-26: A 1x total smaller than one unit of groups would imply a
55    // negative overhead.
56    let step = unit_groups.checked_mul(plane)?;
57    let overhead = t1.checked_sub(step)?;
58    Some(Ratio {
59        unit_groups,
60        overhead,
61    })
62}
63
64#[cfg(test)]
65#[path = "geometry_tests.rs"]
66mod geometry_tests;