eredu-evaluation 0.5.0

Backend-neutral evaluation drivers for Eredu models
Documentation
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
//! Reusable continuation checks over the production portable snapshot driver.
//!
//! Fixtures supply actual backend mechanisms, an ordinary prepared continuation,
//! resource bounds and a probe for forward execution, resets and weight loading.
//! Run with ordinary, captured, intervened and combined admissions. These checks
//! concern native/sampler/controller/record state; facade semantic conformance is
//! additional, and must exercise the facade's incremental decoding pipeline.

use eredu_core::{capture::*, execution_control::*, TextGenerationDriver, TokenFilterController};
use eredu_runtime::execution_control::{
    apply_sampling_override, ManagedTextContinuation, SamplingOverride, SnapshotBudget,
    SnapshotTokenController, TextBranchRequest, TextContinuationSnapshot,
    TextSamplingControlBackend, TextSnapshotBackend, TextSnapshotError, TokenChoiceController,
};
use std::fmt::Debug;

#[cfg(test)]
mod tests;

/// Known fixture storage bounds and explicitly admitted child limits.
pub struct ContinuationFixtureLimits {
    /// Controller/host storage through the fixture's complete continuation.
    pub host_bytes: u64,
    /// Additional native and host growth through the absolute prediction limit.
    pub growth_bytes: u64,
    /// Absolute prediction limit, at least eight for this scenario.
    pub max_predictions: u64,
    /// Explicit child capture limits, including inherited consumption.
    pub capture: Option<CaptureLimits>,
}

fn step<B: TextSnapshotBackend, C: TokenFilterController>(
    driver: &mut TextGenerationDriver<'_, B>,
    state: &mut ManagedTextContinuation<B, C>,
) -> (u32, Option<CapturedStep>) {
    let token = state.advance(driver).unwrap().expect("fixture ended early");
    assert!(matches!(
        state.boundary(driver),
        Err(eredu_core::TextContinuationError::NotQuiescent)
    ));
    let records = state.take_completed_step(driver).unwrap().map(|mut step| {
        step.capture_seconds = 0.0;
        step.cumulative_usage = Default::default();
        step
    });
    (token.token_id(), records)
}

fn branch_values(mut value: (u32, Option<CapturedStep>)) -> (u32, Option<CapturedStep>) {
    if let Some(step) = &mut value.1 {
        for record in &mut step.interventions {
            // Child plans are intentionally re-admitted with fresh identities.
            record.plan_id.clear();
        }
    }
    value
}

/// Checks read-only rejection, exact inherited randomness across temperature
/// changes, and reproducible explicit reseeding in an isolated child. Restores
/// the initial parent afterward without model replay.
pub fn sampling_override_conformance<B, C, P>(
    driver: &mut TextGenerationDriver<'_, B>,
    state: &mut ManagedTextContinuation<B, C>,
    limits: &ContinuationFixtureLimits,
    probe: impl Fn() -> P,
) where
    B: TextSnapshotBackend + TextSamplingControlBackend,
    C: SnapshotTokenController + Clone + PartialEq + Debug,
    P: PartialEq + Debug,
{
    let budget = SnapshotBudget::new(SnapshotLimits {
        max_snapshots: 2,
        max_branches: 1,
        retained_bytes: 64_000_000,
        cumulative_copy_bytes: 512_000_000,
    });
    let initial = TextContinuationSnapshot::capture(
        &mut state.boundary(driver).unwrap(),
        &budget,
        Some(limits.host_bytes),
    )
    .unwrap();
    let facts = B::sampling_control_facts(state.boundary(driver).unwrap().parts().1);
    let baseline: Vec<_> = (0..3).map(|_| step(driver, state)).collect();
    initial
        .restore(&mut state.boundary(driver).unwrap(), &budget)
        .unwrap();
    let before = probe();
    assert!(apply_sampling_override(
        &mut state.boundary(driver).unwrap(),
        SamplingOverride {
            temperature: Some(f32::NAN),
            reseed: Some(123),
        }
    )
    .is_err());
    let greedy = apply_sampling_override(
        &mut state.boundary(driver).unwrap(),
        SamplingOverride {
            temperature: Some(0.0),
            reseed: None,
        },
    );
    if facts.requires_positive_temperature {
        assert!(greedy.is_err());
    } else {
        assert!(
            greedy.unwrap().has_rng,
            "temporary greedy selection discarded inherited RNG"
        );
    }
    apply_sampling_override(
        &mut state.boundary(driver).unwrap(),
        SamplingOverride {
            temperature: Some(facts.temperature),
            reseed: None,
        },
    )
    .unwrap();
    assert_eq!(probe(), before);
    let actual: Vec<_> = (0..3).map(|_| step(driver, state)).collect();
    assert_eq!(
        actual, baseline,
        "invalid/no-draw changes lost RNG or sampler state"
    );
    initial
        .restore(&mut state.boundary(driver).unwrap(), &budget)
        .unwrap();
    let mut child = initial
        .fork(
            &mut state.boundary(driver).unwrap(),
            &budget,
            TextBranchRequest {
                session_id: "conformance-sampling-child",
                max_predictions: limits.max_predictions,
                capture_limits: limits.capture.clone(),
                intervention: None,
                host_bytes: Some(limits.host_bytes),
                continuation_growth_bytes: Some(limits.growth_bytes),
            },
        )
        .unwrap();
    child.exchange(driver, state).unwrap();
    let before = probe();
    let updated = apply_sampling_override(
        &mut state.boundary(driver).unwrap(),
        SamplingOverride {
            temperature: Some(0.35),
            reseed: Some(711),
        },
    )
    .unwrap();
    assert_eq!(updated.temperature, 0.35);
    assert!(updated.has_rng);
    let modified = TextContinuationSnapshot::capture(
        &mut state.boundary(driver).unwrap(),
        &budget,
        Some(limits.host_bytes),
    )
    .unwrap();
    assert_eq!(probe(), before, "sampling change executed model work");
    let changed: Vec<_> = (0..3).map(|_| step(driver, state)).collect();
    modified
        .restore(&mut state.boundary(driver).unwrap(), &budget)
        .unwrap();
    assert_eq!(
        B::sampling_control_facts(state.boundary(driver).unwrap().parts().1),
        updated
    );
    let again: Vec<_> = (0..3).map(|_| step(driver, state)).collect();
    assert_eq!(
        again, changed,
        "snapshot lost explicit reseed or temperature change"
    );
    child.exchange(driver, state).unwrap();
    assert_eq!(
        B::sampling_control_facts(state.boundary(driver).unwrap().parts().1),
        facts
    );
    let parent: Vec<_> = (0..3).map(|_| step(driver, state)).collect();
    assert_eq!(parent, baseline, "sampling override changed the parent");
    initial
        .restore(&mut state.boundary(driver).unwrap(), &budget)
        .unwrap();
}

/// Exercises a genuinely alternative canonical choice, isolated branch commitment,
/// and a reusable snapshot with a still-pending choice. The fixture must allow at
/// least two tokens at its initial decision. Restores the initial state afterward,
/// so the same run can enter `continuation_conformance` without replay.
pub fn forced_choice_conformance<B, C, P>(
    driver: &mut TextGenerationDriver<'_, B>,
    state: &mut ManagedTextContinuation<B, TokenChoiceController<C>>,
    limits: &ContinuationFixtureLimits,
    vocabulary: usize,
    probe: impl Fn() -> P,
) where
    B: TextSnapshotBackend,
    C: SnapshotTokenController + Clone + PartialEq + Debug,
    P: PartialEq + Debug,
{
    let budget = SnapshotBudget::new(SnapshotLimits {
        max_snapshots: 2,
        max_branches: 1,
        retained_bytes: 64_000_000,
        cumulative_copy_bytes: 512_000_000,
    });
    let initial = TextContinuationSnapshot::capture(
        &mut state.boundary(driver).unwrap(),
        &budget,
        Some(limits.host_bytes),
    )
    .unwrap();
    let baseline = step(driver, state).0;
    initial
        .restore(&mut state.boundary(driver).unwrap(), &budget)
        .unwrap();
    let filter = state.controller_mut().current_filter().unwrap();
    let alternative = (0..vocabulary as u32)
        .find(|&token| {
            token != baseline
                && filter
                    .allowed_mask()
                    .is_none_or(|mask| mask[token as usize])
        })
        .expect("fixture must admit an alternative canonical token");
    let mut expected = state.controller().inner().clone();
    expected.commit_token(alternative).unwrap();
    let before = probe();
    let mut child = initial
        .fork(
            &mut state.boundary(driver).unwrap(),
            &budget,
            TextBranchRequest {
                session_id: "conformance-forced-child",
                max_predictions: limits.max_predictions,
                capture_limits: limits.capture.clone(),
                intervention: None,
                host_bytes: Some(limits.host_bytes),
                continuation_growth_bytes: Some(limits.growth_bytes),
            },
        )
        .unwrap();
    child.exchange(driver, state).unwrap();
    state.controller_mut().force_next(alternative).unwrap();
    let pending = TextContinuationSnapshot::capture(
        &mut state.boundary(driver).unwrap(),
        &budget,
        Some(limits.host_bytes),
    )
    .unwrap();
    assert_eq!(probe(), before, "choice/fork/capture executed the model");
    assert_eq!(step(driver, state).0, alternative);
    assert!(state.controller().last_committed_was_forced());
    assert_eq!(state.controller().inner(), &expected);
    let continuation: Vec<_> = (0..3).map(|_| step(driver, state)).collect();
    assert!(!state.controller().last_committed_was_forced());
    for _ in 0..2 {
        let before = probe();
        pending
            .restore(&mut state.boundary(driver).unwrap(), &budget)
            .unwrap();
        assert_eq!(probe(), before, "restore replayed the forced prefix");
        assert_eq!(state.controller().pending_forced(), Some(alternative));
        assert_eq!(step(driver, state).0, alternative);
        assert_eq!(state.controller().inner(), &expected);
        let actual: Vec<_> = (0..3).map(|_| step(driver, state)).collect();
        assert_eq!(actual, continuation);
    }
    child.exchange(driver, state).unwrap();
    assert_eq!(state.controller().pending_forced(), None);
    assert_eq!(step(driver, state).0, baseline, "child changed the parent");
    initial
        .restore(&mut state.boundary(driver).unwrap(), &budget)
        .unwrap();
}

/// Checks reusable initial/decode snapshots, pending-input position, exact sampled
/// continuations, controller restoration, isolated interleaved siblings, fresh
/// child admissions, cumulative capture accounting and retention-lease ownership.
/// `probe` must change on model forward, reset, artifact reopen or weight reload;
/// it must not count copying. Nonzero-temperature/adaptive fixtures detect lost
/// RNG/history state. This function consumes the initial continuation for cleanup
/// checks; its driver remains usable only for inspecting the fixture afterward.
pub fn continuation_conformance<B, C, P>(
    driver: &mut TextGenerationDriver<'_, B>,
    mut state: ManagedTextContinuation<B, C>,
    limits: ContinuationFixtureLimits,
    probe: impl Fn() -> P,
) where
    B: TextSnapshotBackend,
    C: SnapshotTokenController + Clone + PartialEq + Debug,
    P: PartialEq + Debug,
{
    assert!(limits.max_predictions >= 8);
    let budget = SnapshotBudget::new(SnapshotLimits {
        max_snapshots: 2,
        max_branches: 2,
        retained_bytes: 64_000_000,
        cumulative_copy_bytes: 512_000_000,
    });
    let before = probe();
    assert!(matches!(
        TextContinuationSnapshot::capture(&mut state.boundary(driver).unwrap(), &budget, None),
        Err(TextSnapshotError::Control(
            ExecutionControlError::UnknownEstimate
        ))
    ));
    assert_eq!(budget.usage(), SnapshotUsage::default());
    assert_eq!(probe(), before);
    let initial = TextContinuationSnapshot::capture(
        &mut state.boundary(driver).unwrap(),
        &budget,
        Some(limits.host_bytes),
    )
    .unwrap();
    assert_eq!(initial.next_prediction(), 0);
    let initial_native = B::estimate_native_text_state(driver.runtime(), None)
        .unwrap()
        .unwrap()
        .retained_bytes;
    let initial_growth = initial
        .native_continuation_growth(driver.runtime(), limits.max_predictions)
        .unwrap();
    assert_eq!(
        probe(),
        before,
        "growth estimation executed or rebuilt the model"
    );
    let prefix: Vec<_> = (0..3).map(|_| step(driver, &mut state)).collect();
    let prefix_controller = state.controller().clone();
    let saved = TextContinuationSnapshot::capture(
        &mut state.boundary(driver).unwrap(),
        &budget,
        Some(limits.host_bytes),
    )
    .unwrap();
    assert_eq!(saved.next_prediction(), 3);
    let usage = budget.usage();
    let before = probe();
    assert!(matches!(
        TextContinuationSnapshot::capture(
            &mut state.boundary(driver).unwrap(),
            &budget,
            Some(limits.host_bytes),
        ),
        Err(TextSnapshotError::Control(ExecutionControlError::Limit(
            "snapshot count"
        )))
    ));
    assert_eq!(budget.usage(), usage);
    let native_growth = saved
        .native_continuation_growth(driver.runtime(), limits.max_predictions)
        .unwrap();
    let child = |session_id| TextBranchRequest {
        session_id,
        max_predictions: limits.max_predictions,
        capture_limits: limits.capture.clone(),
        intervention: None,
        host_bytes: Some(limits.host_bytes),
        continuation_growth_bytes: Some(limits.growth_bytes.checked_add(native_growth).unwrap()),
    };
    let mut left = saved
        .fork(
            &mut state.boundary(driver).unwrap(),
            &budget,
            child("conformance-left"),
        )
        .unwrap();
    let mut right = saved
        .fork(
            &mut state.boundary(driver).unwrap(),
            &budget,
            child("conformance-right"),
        )
        .unwrap();
    assert_eq!(probe(), before, "copy/fork executed or rebuilt the model");
    let baseline: Vec<_> = (0..5).map(|_| step(driver, &mut state)).collect();
    let native_after = B::estimate_native_text_state(driver.runtime(), None)
        .unwrap()
        .unwrap()
        .retained_bytes;
    assert!(
        native_after <= initial_native.checked_add(initial_growth).unwrap(),
        "retained native state exceeded the pre-generation growth allowance"
    );
    left.exchange(driver, &mut state).unwrap();
    assert!(matches!(
        saved.restore(&mut state.boundary(driver).unwrap(), &budget),
        Err(TextSnapshotError::IncompatibleRun)
    ));
    if let Some(checkpoint) = saved.capture_checkpoint() {
        let boundary = state.boundary(driver).unwrap();
        assert_eq!(
            B::capture_run(boundary.parts().1)
                .unwrap()
                .cumulative_usage(),
            checkpoint.inherited_usage()
        );
    }
    let first_left = step(driver, &mut state);
    if let (Some(child), Some(parent)) = (&first_left.1, &baseline[0].1) {
        for (child, parent) in child.interventions.iter().zip(&parent.interventions) {
            assert_ne!(
                child.plan_id, parent.plan_id,
                "child reused a session-bound admission"
            );
        }
    }
    assert_eq!(
        branch_values(first_left),
        branch_values(baseline[0].clone())
    );
    left.exchange(driver, &mut state).unwrap();
    right.exchange(driver, &mut state).unwrap();
    let other: Vec<_> = (0..5)
        .map(|_| branch_values(step(driver, &mut state)))
        .collect();
    assert_eq!(
        other,
        baseline
            .iter()
            .cloned()
            .map(branch_values)
            .collect::<Vec<_>>()
    );
    right.exchange(driver, &mut state).unwrap();
    left.exchange(driver, &mut state).unwrap();
    let rest: Vec<_> = (0..4)
        .map(|_| branch_values(step(driver, &mut state)))
        .collect();
    assert_eq!(
        rest,
        baseline[1..]
            .iter()
            .cloned()
            .map(branch_values)
            .collect::<Vec<_>>()
    );
    left.exchange(driver, &mut state).unwrap();
    let capture_usage = {
        let boundary = state.boundary(driver).unwrap();
        B::capture_run(boundary.parts().1).map(|run| run.cumulative_usage())
    };
    for _ in 0..2 {
        let before = probe();
        saved
            .restore(&mut state.boundary(driver).unwrap(), &budget)
            .unwrap();
        assert_eq!(probe(), before, "restore executed or rebuilt the model");
        assert_eq!(state.controller(), &prefix_controller);
        let actual: Vec<_> = (0..5).map(|_| step(driver, &mut state)).collect();
        assert_eq!(actual, baseline);
    }
    if let Some(before) = capture_usage {
        let boundary = state.boundary(driver).unwrap();
        let after = B::capture_run(boundary.parts().1)
            .unwrap()
            .cumulative_usage();
        assert!(after.captures >= before.captures);
        assert!(after.encoded_bytes >= before.encoded_bytes);
        if before.encoded_bytes != 0 {
            assert!(after.encoded_bytes > before.encoded_bytes);
        }
    }
    initial
        .restore(&mut state.boundary(driver).unwrap(), &budget)
        .unwrap();
    let actual: Vec<_> = (0..3).map(|_| step(driver, &mut state)).collect();
    assert_eq!(actual, prefix);
    drop(right);
    assert_eq!(budget.usage().branches, 1);
    left.exchange(driver, &mut state).unwrap();
    assert!(state.retained_branch_bytes().is_some());
    let usage = budget.usage();
    drop(left); // This slot now holds the original parent, not the charged child.
    assert_eq!(
        budget.usage(),
        usage,
        "dropping parent released active child retention"
    );
    drop(state);
    assert_eq!(budget.usage().branches, 0);
    drop((initial, saved));
    assert_eq!(budget.usage().retained_bytes, 0);
    assert_eq!(budget.usage().snapshots, 0);
    assert_eq!(
        budget.usage().cumulative_copy_bytes,
        usage.cumulative_copy_bytes
    );
}