Skip to main content

hyprstream_bench/gen/
mod.rs

1//! Per-family item generators. Every generator is a pure function of
2//! `(seed, stratum)` over the pinned [`BenchRng`] stream — published
3//! generators + seeds make every item reproducible and immune to
4//! teacher-pretraining contamination.
5
6pub mod arith;
7pub mod jsoncheck;
8pub mod syllogism;
9pub mod temporal;
10pub mod unitconv;
11
12use hyprstream_decision::QuestionKind;
13
14use crate::family::{Family, Stratum};
15use crate::item::Item;
16use crate::rng::BenchRng;
17
18/// Benchmark generation configuration. The frozen release pins
19/// [`DEFAULT_SEEDS_PER_STRATUM`] and [`SEED_BASE`]; regenerating with the same
20/// values must reproduce the committed manifest bit-for-bit.
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22pub struct BenchConfig {
23    /// Base items per (family, base stratum). Each base item is one question
24    /// kind — total base items = families × strata × 3 kinds × this count.
25    pub seeds_per_stratum: u32,
26    /// Seed stream base; per-item seeds are `SEED_BASE + family * K + ...`.
27    pub seed_base: u64,
28}
29
30/// Seeds per (family, base stratum, kind) in the frozen vob-1.0 release.
31pub const DEFAULT_SEEDS_PER_STRATUM: u32 = 64;
32
33/// Seed base for the frozen vob-1.0 release.
34pub const SEED_BASE: u64 = 0x06;
35
36impl Default for BenchConfig {
37    fn default() -> Self {
38        Self {
39            seeds_per_stratum: DEFAULT_SEEDS_PER_STRATUM,
40            seed_base: SEED_BASE,
41        }
42    }
43}
44
45/// The base strata every family emits (permutation expansion is mechanical
46/// and applied afterwards to choice items only).
47pub const BASE_STRATA: [Stratum; 2] = [Stratum::Clean, Stratum::NearMiss];
48
49/// Deterministic per-(family, stratum, kind, index) seed.
50fn item_seed(config: &BenchConfig, family: Family, stratum: Stratum, kind: u64, index: u32) -> u64 {
51    let family_tag = Family::ALL
52        .iter()
53        .position(|f| *f == family)
54        .map_or(0, |position| position as u64);
55    let stratum_tag = u64::from(stratum == Stratum::NearMiss);
56    config.seed_base ^ (family_tag << 48) ^ (stratum_tag << 44) ^ (kind << 36) ^ u64::from(index)
57}
58
59/// Generate the full benchmark: all families × base strata × kinds × seeds,
60/// plus the cyclic-permutation expansion of every choice item.
61pub fn generate_all(config: &BenchConfig) -> Vec<Item> {
62    let mut items = Vec::new();
63    for family in Family::ALL {
64        for stratum in BASE_STRATA {
65            for index in 0..config.seeds_per_stratum {
66                let base = generate_family(family, stratum, config, index);
67                for item in base {
68                    if item.question.kind == QuestionKind::Choice {
69                        let n = item.question.cardinality();
70                        for rotation in 1..n {
71                            if let Some(permuted) = item.cyclic_permutation(rotation) {
72                                items.push(permuted);
73                            }
74                        }
75                    }
76                    items.push(item);
77                }
78            }
79        }
80    }
81    items
82}
83
84/// Generate one (kind-triple) of base items for a family at `index`.
85fn generate_family(
86    family: Family,
87    stratum: Stratum,
88    config: &BenchConfig,
89    index: u32,
90) -> Vec<Item> {
91    let mut triple = Vec::with_capacity(3);
92    for (kind_tag, build) in [
93        (
94            0u64,
95            match family {
96                Family::Arith => arith::noul as fn(u64, Stratum) -> Item,
97                Family::JsonCheck => jsoncheck::noul,
98                Family::UnitConv => unitconv::noul,
99                Family::Temporal => temporal::noul,
100                Family::Syllogism => syllogism::noul,
101            },
102        ),
103        (
104            1,
105            match family {
106                Family::Arith => arith::choice,
107                Family::JsonCheck => jsoncheck::choice,
108                Family::UnitConv => unitconv::choice,
109                Family::Temporal => temporal::choice,
110                Family::Syllogism => syllogism::choice,
111            },
112        ),
113        (
114            2,
115            match family {
116                Family::Arith => arith::score,
117                Family::JsonCheck => jsoncheck::score,
118                Family::UnitConv => unitconv::score,
119                Family::Temporal => temporal::score,
120                Family::Syllogism => syllogism::score,
121            },
122        ),
123    ] {
124        let seed = item_seed(config, family, stratum, kind_tag, index);
125        triple.push(build(seed, stratum));
126    }
127    triple
128}
129
130/// Convenience: a keyed stream for one item.
131pub(crate) fn stream(seed: u64) -> BenchRng {
132    BenchRng::new(seed)
133}