miyagi 0.1.1

Sparse XOR adaptation and search for true binary GGUF language models
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
use std::collections::{BTreeMap, BTreeSet};
use std::fs;
use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};

use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::architecture::Projection;
use crate::backend::MiyagiBackend;
use crate::error::{Error, Result};
use crate::fitness::{FitnessMode, compute_fitness};
use crate::patch::{Patch, PatchFlip};
use crate::probe::{CompiledProbe, ProbeMeasurement, measure_probes};

const CHECKPOINT_VERSION: u32 = 1;

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(default)]
pub struct SearchConfig {
    pub search_layers: Vec<usize>,
    pub search_projections: Vec<Projection>,
    pub max_iters: usize,
    pub control_penalty: f32,
    pub fitness_mode: FitnessMode,
    pub seed: u64,
    pub screen_probe_count: usize,
    pub patch_name: String,
    pub patch_description: String,
    pub base_model: String,
}

impl Default for SearchConfig {
    fn default() -> Self {
        Self {
            search_layers: vec![1, 2, 3, 4, 34],
            search_projections: vec![Projection::Gate, Projection::Up],
            max_iters: 200,
            control_penalty: 2.0,
            fitness_mode: FitnessMode::Mean,
            seed: 42,
            screen_probe_count: 2,
            patch_name: "untitled".to_owned(),
            patch_description: String::new(),
            base_model: "unknown".to_owned(),
        }
    }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct SearchCheckpoint {
    pub version: u32,
    pub architecture_signature: String,
    pub model_label: String,
    pub config: SearchConfig,
    pub completed_iterations: usize,
    pub accepted: Vec<PatchFlip>,
    pub tried: BTreeSet<PatchFlip>,
    pub current_fitness: f32,
    pub rng_state: u64,
    pub target_baseline: Vec<ProbeMeasurement>,
    pub control_baseline: Vec<ProbeMeasurement>,
}

impl SearchCheckpoint {
    pub fn load(path: impl AsRef<Path>) -> Result<Self> {
        Ok(serde_json::from_str(&fs::read_to_string(path)?)?)
    }

    pub fn save_atomic(&self, path: impl AsRef<Path>) -> Result<()> {
        let path = path.as_ref();
        let temporary = path.with_extension(format!(
            "{}.{}.tmp",
            path.extension()
                .and_then(|extension| extension.to_str())
                .unwrap_or("checkpoint"),
            std::process::id()
        ));
        let bytes = serde_json::to_vec_pretty(self)?;
        fs::write(&temporary, bytes)?;
        if let Err(error) = fs::rename(&temporary, path) {
            let _ = fs::remove_file(&temporary);
            return Err(error.into());
        }
        Ok(())
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ModelPatchState {
    Baseline,
    AcceptedPatchApplied,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct SearchResult {
    pub patch: Patch,
    pub target_baseline: Vec<ProbeMeasurement>,
    pub control_baseline: Vec<ProbeMeasurement>,
    pub final_target: Vec<ProbeMeasurement>,
    pub final_control: Vec<ProbeMeasurement>,
    pub final_fitness: f32,
    pub completed_iterations: usize,
    pub tried_candidates: usize,
    pub screened_out: usize,
    pub model_state: ModelPatchState,
}

#[derive(Clone, Debug, Serialize)]
#[serde(tag = "event", rename_all = "snake_case")]
pub enum SearchEvent {
    Baseline {
        target: Vec<ProbeMeasurement>,
        control: Vec<ProbeMeasurement>,
    },
    Candidate {
        iteration: usize,
        flip: PatchFlip,
    },
    ScreenedOut {
        iteration: usize,
        flip: PatchFlip,
    },
    Rejected {
        iteration: usize,
        flip: PatchFlip,
        fitness: f32,
        best_fitness: f32,
    },
    Accepted {
        iteration: usize,
        flip: PatchFlip,
        fitness: f32,
        accepted: usize,
    },
    Checkpoint {
        iteration: usize,
    },
    Completed {
        iterations: usize,
        accepted: usize,
        fitness: f32,
    },
}

#[derive(Clone, Debug)]
struct Candidate {
    flip: PatchFlip,
    weight: f64,
}

#[allow(clippy::too_many_arguments)]
pub fn run_search<B, F>(
    backend: &mut B,
    target_probes: &[CompiledProbe],
    control_probes: &[CompiledProbe],
    config: SearchConfig,
    resume: Option<SearchCheckpoint>,
    checkpoint_path: Option<&Path>,
    cancellation: Option<&AtomicBool>,
    mut on_event: F,
) -> Result<SearchResult>
where
    B: MiyagiBackend,
    F: FnMut(&SearchEvent),
{
    validate_config(backend, target_probes, control_probes, &config)?;
    let candidates = build_candidates(backend, &config)?;
    if candidates.is_empty() {
        return Err(Error::InvalidSearch("candidate pool is empty".to_owned()));
    }

    let fresh_target_baseline = measure_probes(backend, target_probes)?;
    let fresh_control_baseline = measure_probes(backend, control_probes)?;
    let architecture_signature = backend.architecture().signature().to_owned();
    let model_label = backend.model_label().to_owned();

    let (
        target_baseline,
        control_baseline,
        mut accepted,
        mut tried,
        mut current_fitness,
        mut completed_iterations,
        mut rng,
    ) = if let Some(checkpoint) = resume {
        validate_checkpoint(
            &checkpoint,
            &config,
            &architecture_signature,
            &fresh_target_baseline,
            &fresh_control_baseline,
        )?;
        apply_coordinates(backend, &checkpoint.accepted, "checkpoint restoration")?;
        (
            checkpoint.target_baseline,
            checkpoint.control_baseline,
            checkpoint.accepted,
            checkpoint.tried,
            checkpoint.current_fitness,
            checkpoint.completed_iterations,
            SplitMix64::from_state(checkpoint.rng_state),
        )
    } else {
        (
            fresh_target_baseline,
            fresh_control_baseline,
            Vec::new(),
            BTreeSet::new(),
            0.0,
            0,
            SplitMix64::new(config.seed),
        )
    };

    on_event(&SearchEvent::Baseline {
        target: target_baseline.clone(),
        control: control_baseline.clone(),
    });
    let screen_indices = screen_indices(&target_baseline, config.screen_probe_count);
    let baseline_by_name = target_baseline
        .iter()
        .map(|measurement| (measurement.name.clone(), measurement.gap))
        .collect::<BTreeMap<_, _>>();
    let mut screened_out = 0;

    while completed_iterations < config.max_iters {
        if cancellation.is_some_and(|flag| flag.load(Ordering::Relaxed)) {
            let checkpoint = checkpoint(
                &architecture_signature,
                &model_label,
                &config,
                completed_iterations,
                &accepted,
                &tried,
                current_fitness,
                rng.state(),
                &target_baseline,
                &control_baseline,
            );
            if let Some(path) = checkpoint_path {
                checkpoint.save_atomic(path)?;
            }
            return Err(Error::SearchCancelled);
        }

        let Some(candidate) = sample_candidate(&candidates, &tried, &mut rng) else {
            break;
        };
        tried.insert(candidate.flip.clone());
        completed_iterations += 1;
        on_event(&SearchEvent::Candidate {
            iteration: completed_iterations,
            flip: candidate.flip.clone(),
        });

        backend.flip_row(
            candidate.flip.layer,
            candidate.flip.proj,
            candidate.flip.row,
        )?;
        let evaluation = evaluate_candidate(
            backend,
            target_probes,
            control_probes,
            &screen_indices,
            &baseline_by_name,
            &target_baseline,
            &control_baseline,
            &config,
        );

        match evaluation {
            Ok(CandidateEvaluation::ScreenedOut) => {
                revert_candidate(backend, &candidate.flip, "screened candidate")?;
                screened_out += 1;
                on_event(&SearchEvent::ScreenedOut {
                    iteration: completed_iterations,
                    flip: candidate.flip.clone(),
                });
            }
            Ok(CandidateEvaluation::Measured { fitness }) if fitness > current_fitness => {
                current_fitness = fitness;
                accepted.push(candidate.flip.clone());
                on_event(&SearchEvent::Accepted {
                    iteration: completed_iterations,
                    flip: candidate.flip.clone(),
                    fitness,
                    accepted: accepted.len(),
                });
            }
            Ok(CandidateEvaluation::Measured { fitness }) => {
                revert_candidate(backend, &candidate.flip, "rejected candidate")?;
                on_event(&SearchEvent::Rejected {
                    iteration: completed_iterations,
                    flip: candidate.flip.clone(),
                    fitness,
                    best_fitness: current_fitness,
                });
            }
            Err(error) => {
                revert_candidate(backend, &candidate.flip, "failed candidate evaluation")?;
                return Err(error);
            }
        }

        if let Some(path) = checkpoint_path {
            checkpoint(
                &architecture_signature,
                &model_label,
                &config,
                completed_iterations,
                &accepted,
                &tried,
                current_fitness,
                rng.state(),
                &target_baseline,
                &control_baseline,
            )
            .save_atomic(path)?;
            on_event(&SearchEvent::Checkpoint {
                iteration: completed_iterations,
            });
        }
    }

    let final_target = measure_probes(backend, target_probes)?;
    let final_control = measure_probes(backend, control_probes)?;
    let final_fitness = compute_fitness(
        config.fitness_mode,
        &final_target,
        &final_control,
        &target_baseline,
        &control_baseline,
        config.control_penalty,
    )?;
    let mut patch = Patch::new(
        config.patch_name.clone(),
        config.patch_description.clone(),
        config.base_model.clone(),
        accepted,
    );
    patch.metadata.insert(
        "architecture_signature".to_owned(),
        Value::String(architecture_signature),
    );
    patch.metadata.insert(
        "search_algorithm".to_owned(),
        Value::String(
            format!("greedy_hill_climbing_screened_{:?}", config.fitness_mode).to_lowercase(),
        ),
    );
    patch
        .metadata
        .insert("seed".to_owned(), Value::Number(config.seed.into()));
    patch.metadata.insert(
        "completed_iterations".to_owned(),
        Value::Number((completed_iterations as u64).into()),
    );
    patch.metadata.insert(
        "control_penalty".to_owned(),
        serde_json::Number::from_f64(config.control_penalty as f64)
            .map(Value::Number)
            .unwrap_or(Value::Null),
    );
    let validated = patch.validate(backend.architecture(), Default::default())?;
    let patch = validated.into_patch();
    on_event(&SearchEvent::Completed {
        iterations: completed_iterations,
        accepted: patch.flips.len(),
        fitness: final_fitness,
    });
    Ok(SearchResult {
        patch,
        target_baseline,
        control_baseline,
        final_target,
        final_control,
        final_fitness,
        completed_iterations,
        tried_candidates: tried.len(),
        screened_out,
        model_state: ModelPatchState::AcceptedPatchApplied,
    })
}

enum CandidateEvaluation {
    ScreenedOut,
    Measured { fitness: f32 },
}

#[allow(clippy::too_many_arguments)]
fn evaluate_candidate<B: MiyagiBackend>(
    backend: &mut B,
    target_probes: &[CompiledProbe],
    control_probes: &[CompiledProbe],
    screen_indices: &[usize],
    baseline_by_name: &BTreeMap<String, f32>,
    target_baseline: &[ProbeMeasurement],
    control_baseline: &[ProbeMeasurement],
    config: &SearchConfig,
) -> Result<CandidateEvaluation> {
    let screen_probes = screen_indices
        .iter()
        .map(|index| target_probes[*index].clone())
        .collect::<Vec<_>>();
    let screen_measurements = measure_probes(backend, &screen_probes)?;
    if !screen_measurements.iter().any(|measurement| {
        baseline_by_name
            .get(&measurement.name)
            .is_some_and(|baseline| measurement.gap > *baseline)
    }) {
        return Ok(CandidateEvaluation::ScreenedOut);
    }

    let mut measured_by_index = screen_indices
        .iter()
        .copied()
        .zip(screen_measurements)
        .collect::<BTreeMap<_, _>>();
    for (index, probe) in target_probes.iter().enumerate() {
        if let std::collections::btree_map::Entry::Vacant(entry) = measured_by_index.entry(index) {
            let measurement = measure_probes(backend, std::slice::from_ref(probe))?
                .into_iter()
                .next()
                .expect("one probe returns one measurement");
            entry.insert(measurement);
        }
    }
    let target = (0..target_probes.len())
        .map(|index| {
            measured_by_index
                .remove(&index)
                .expect("every target probe was measured")
        })
        .collect::<Vec<_>>();
    let control = measure_probes(backend, control_probes)?;
    let fitness = compute_fitness(
        config.fitness_mode,
        &target,
        &control,
        target_baseline,
        control_baseline,
        config.control_penalty,
    )?;
    Ok(CandidateEvaluation::Measured { fitness })
}

fn validate_config<B: MiyagiBackend>(
    backend: &B,
    target_probes: &[CompiledProbe],
    control_probes: &[CompiledProbe],
    config: &SearchConfig,
) -> Result<()> {
    if target_probes.is_empty() || control_probes.is_empty() {
        return Err(Error::InvalidSearch(
            "target and control probes must both be non-empty".to_owned(),
        ));
    }
    if config.search_layers.is_empty() || config.search_projections.is_empty() {
        return Err(Error::InvalidSearch(
            "search layers and projections must both be non-empty".to_owned(),
        ));
    }
    if config.max_iters == 0 {
        return Err(Error::InvalidSearch(
            "max_iters must be greater than zero".to_owned(),
        ));
    }
    if config.screen_probe_count == 0 {
        return Err(Error::InvalidSearch(
            "screen_probe_count must be greater than zero".to_owned(),
        ));
    }
    if !config.control_penalty.is_finite() || config.control_penalty < 0.0 {
        return Err(Error::InvalidSearch(
            "control penalty must be finite and non-negative".to_owned(),
        ));
    }
    for layer in &config.search_layers {
        for projection in &config.search_projections {
            backend.architecture().tensor(*layer, *projection)?;
        }
    }
    Ok(())
}

fn build_candidates<B: MiyagiBackend>(
    backend: &mut B,
    config: &SearchConfig,
) -> Result<Vec<Candidate>> {
    let coordinates = config
        .search_layers
        .iter()
        .flat_map(|layer| {
            config
                .search_projections
                .iter()
                .map(move |projection| (*layer, *projection))
        })
        .collect::<Vec<_>>();
    let mut candidates = Vec::new();
    for (layer, projection) in coordinates {
        let rows = backend.architecture().tensor(layer, projection)?.rows;
        let scales = backend.row_scales(layer, projection)?;
        if scales.len() != rows {
            return Err(Error::InvalidSearch(format!(
                "L{layer}.{projection} returned {} scales for {rows} rows",
                scales.len()
            )));
        }
        for (row, scale) in scales.into_iter().enumerate() {
            candidates.push(Candidate {
                flip: PatchFlip {
                    layer,
                    proj: projection,
                    row,
                },
                weight: if scale.is_finite() && scale > 0.0 {
                    f64::from(scale)
                } else {
                    0.0
                },
            });
        }
    }
    Ok(candidates)
}

fn sample_candidate<'a>(
    candidates: &'a [Candidate],
    tried: &BTreeSet<PatchFlip>,
    rng: &mut SplitMix64,
) -> Option<&'a Candidate> {
    let available = candidates
        .iter()
        .filter(|candidate| !tried.contains(&candidate.flip))
        .collect::<Vec<_>>();
    if available.is_empty() {
        return None;
    }
    let total = available
        .iter()
        .map(|candidate| candidate.weight)
        .sum::<f64>();
    if total <= 0.0 || !total.is_finite() {
        let index = (rng.next_u64() as usize) % available.len();
        return Some(available[index]);
    }
    let mut threshold = rng.next_f64() * total;
    for candidate in &available {
        if threshold < candidate.weight {
            return Some(candidate);
        }
        threshold -= candidate.weight;
    }
    available.last().copied()
}

fn screen_indices(baseline: &[ProbeMeasurement], count: usize) -> Vec<usize> {
    let mut indices = (0..baseline.len()).collect::<Vec<_>>();
    indices.sort_by(|left, right| {
        baseline[*left]
            .gap
            .total_cmp(&baseline[*right].gap)
            .then_with(|| baseline[*left].name.cmp(&baseline[*right].name))
    });
    indices.truncate(count.min(indices.len()));
    indices
}

fn apply_coordinates<B: MiyagiBackend>(
    backend: &mut B,
    flips: &[PatchFlip],
    operation: &str,
) -> Result<()> {
    let mut applied: Vec<PatchFlip> = Vec::new();
    for flip in flips {
        if let Err(error) = backend.flip_row(flip.layer, flip.proj, flip.row) {
            for prior in applied.iter().rev() {
                if let Err(source) = backend.flip_row(prior.layer, prior.proj, prior.row) {
                    return Err(Error::RestorationFailed {
                        operation: operation.to_owned(),
                        source: Box::new(source),
                    });
                }
            }
            return Err(error);
        }
        applied.push(flip.clone());
    }
    Ok(())
}

fn revert_candidate<B: MiyagiBackend>(
    backend: &mut B,
    flip: &PatchFlip,
    operation: &str,
) -> Result<()> {
    backend
        .flip_row(flip.layer, flip.proj, flip.row)
        .map_err(|source| Error::RestorationFailed {
            operation: operation.to_owned(),
            source: Box::new(source),
        })
}

#[allow(clippy::too_many_arguments)]
fn checkpoint(
    architecture_signature: &str,
    model_label: &str,
    config: &SearchConfig,
    completed_iterations: usize,
    accepted: &[PatchFlip],
    tried: &BTreeSet<PatchFlip>,
    current_fitness: f32,
    rng_state: u64,
    target_baseline: &[ProbeMeasurement],
    control_baseline: &[ProbeMeasurement],
) -> SearchCheckpoint {
    SearchCheckpoint {
        version: CHECKPOINT_VERSION,
        architecture_signature: architecture_signature.to_owned(),
        model_label: model_label.to_owned(),
        config: config.clone(),
        completed_iterations,
        accepted: accepted.to_vec(),
        tried: tried.clone(),
        current_fitness,
        rng_state,
        target_baseline: target_baseline.to_vec(),
        control_baseline: control_baseline.to_vec(),
    }
}

fn validate_checkpoint(
    checkpoint: &SearchCheckpoint,
    config: &SearchConfig,
    architecture_signature: &str,
    target_baseline: &[ProbeMeasurement],
    control_baseline: &[ProbeMeasurement],
) -> Result<()> {
    if checkpoint.version != CHECKPOINT_VERSION {
        return Err(Error::IncompatibleCheckpoint(format!(
            "unsupported checkpoint version {}",
            checkpoint.version
        )));
    }
    let mut checkpoint_config = checkpoint.config.clone();
    checkpoint_config.max_iters = config.max_iters;
    if checkpoint_config != *config || config.max_iters < checkpoint.completed_iterations {
        return Err(Error::IncompatibleCheckpoint(
            "search configuration changed".to_owned(),
        ));
    }
    if checkpoint.architecture_signature != architecture_signature {
        return Err(Error::IncompatibleCheckpoint(
            "model architecture signature changed".to_owned(),
        ));
    }
    compare_baselines(&checkpoint.target_baseline, target_baseline)?;
    compare_baselines(&checkpoint.control_baseline, control_baseline)?;
    Ok(())
}

fn compare_baselines(expected: &[ProbeMeasurement], actual: &[ProbeMeasurement]) -> Result<()> {
    if expected.len() != actual.len() {
        return Err(Error::IncompatibleCheckpoint(
            "probe count changed".to_owned(),
        ));
    }
    for (expected, actual) in expected.iter().zip(actual) {
        if expected.name != actual.name || (expected.gap - actual.gap).abs() > 1e-5 {
            return Err(Error::IncompatibleCheckpoint(format!(
                "baseline changed for probe {}",
                expected.name
            )));
        }
    }
    Ok(())
}

#[derive(Clone, Copy, Debug)]
struct SplitMix64 {
    state: u64,
}

impl SplitMix64 {
    fn new(seed: u64) -> Self {
        Self { state: seed }
    }

    fn from_state(state: u64) -> Self {
        Self { state }
    }

    fn state(self) -> u64 {
        self.state
    }

    fn next_u64(&mut self) -> u64 {
        self.state = self.state.wrapping_add(0x9e37_79b9_7f4a_7c15);
        let mut value = self.state;
        value = (value ^ (value >> 30)).wrapping_mul(0xbf58_476d_1ce4_e5b9);
        value = (value ^ (value >> 27)).wrapping_mul(0x94d0_49bb_1331_11eb);
        value ^ (value >> 31)
    }

    fn next_f64(&mut self) -> f64 {
        const SCALE: f64 = 1.0 / ((1_u64 << 53) as f64);
        ((self.next_u64() >> 11) as f64) * SCALE
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn splitmix_state_can_resume_exactly() {
        let mut first = SplitMix64::new(42);
        let _ = first.next_u64();
        let state = first.state();
        let expected = first.next_u64();
        let mut resumed = SplitMix64::from_state(state);
        assert_eq!(resumed.next_u64(), expected);
    }

    #[test]
    fn screen_uses_worst_gaps_then_name() {
        fn measurement(name: &str, gap: f32) -> ProbeMeasurement {
            ProbeMeasurement {
                name: name.to_owned(),
                category: String::new(),
                prompt: String::new(),
                correct_token: String::new(),
                wrong_token: String::new(),
                correct_id: 0,
                wrong_id: 1,
                gap,
            }
        }
        let baseline = [
            measurement("b", -1.0),
            measurement("a", -1.0),
            measurement("c", 0.0),
        ];
        assert_eq!(screen_indices(&baseline, 2), vec![1, 0]);
    }
}