eredu-runtime 0.3.0

Backend-neutral model execution runtime for Eredu
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
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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
//! Backend-neutral causal-model and token-sampling contracts.

use eredu_core::{
    generation::ResolvedGenerationConfig, SpeculativeTokenFilterController, TokenFilter,
    TokenFilterController,
};
use eredu_nn::Tensor;

/// Monomorphized causal model used by generation sessions.
pub trait CausalModel<S> {
    /// Backend-native tensor handle containing logits and decode token ids.
    type Tensor: Tensor;
    /// Borrowed, tokenizer/media-prepared prefill input.
    type Input<'a>: Copy;
    /// Concrete model or backend failure.
    type Error;

    /// Computes initial logits and updates mutable state.
    fn prefill_input_logits(
        &mut self,
        input: Self::Input<'_>,
        state: &mut S,
        context: &<Self::Tensor as Tensor>::Context,
    ) -> Result<Self::Tensor, Self::Error>;

    /// Computes logits for decode tokens using existing mutable state.
    fn decode_logits(
        &mut self,
        input_tokens: &Self::Tensor,
        state: &mut S,
        context: &<Self::Tensor as Tensor>::Context,
    ) -> Result<Self::Tensor, Self::Error>;

    /// Adjusts prefill logits before backend-native sampling.
    fn adjust_prefill_logits(
        &mut self,
        logits: Self::Tensor,
        _state: &mut S,
        _context: &<Self::Tensor as Tensor>::Context,
    ) -> Result<Self::Tensor, Self::Error> {
        Ok(logits)
    }
}

/// Portable cardinality of one zero-based token-id domain.
///
/// Backends validate native token tensors without copying their values to the
/// host. Architectures select the exact domain for each prediction boundary.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct TokenDomain {
    cardinality: usize,
}

impl TokenDomain {
    /// Creates the token IDs `0..cardinality`.
    pub const fn new(cardinality: usize) -> Self {
        Self { cardinality }
    }

    /// Number of valid zero-based token IDs.
    pub const fn cardinality(self) -> usize {
        self.cardinality
    }
}

/// Backend primitives required by generic token-sampling policies.
///
/// The runtime owns ordering, history, adaptive state, and constraint rollback.
/// Implementations operate directly on native logits and random state without
/// copying values through a neutral tensor representation.
pub trait SamplingBackend {
    /// Backend-native logits tensor.
    type Logits: Clone;
    /// Backend-native sampled-token tensor.
    type Token: Clone;
    /// Backend-native random-key stream.
    type RandomState;
    /// Execution context, such as a stream.
    type Context: ?Sized;
    /// Backend failure.
    type Error;

    /// Creates a backend error for a portable policy or constraint failure.
    fn error(message: String) -> Self::Error;

    /// Validates a native token tensor against one architecture-selected domain.
    ///
    /// The returned token must retain a backend-native dependency on the range
    /// check so lazy backends cannot commit an unchecked forced token.
    fn validate_token(
        token: &Self::Token,
        domain: TokenDomain,
        context: &Self::Context,
    ) -> Result<Self::Token, Self::Error>;

    /// Scales logits by inverse temperature, preserving the native tensor.
    fn scale_temperature(
        logits: &Self::Logits,
        temperature: f32,
        context: &Self::Context,
    ) -> Result<Self::Logits, Self::Error>;

    /// Applies repetition, frequency, and presence penalties.
    fn apply_penalties(
        logits: &Self::Logits,
        history: &[u32],
        penalties: PenaltyConfig,
        context: &Self::Context,
    ) -> Result<Self::Logits, Self::Error>;

    /// Masks all but the highest `top_k` logits. Non-positive values disable it.
    fn apply_top_k(
        logits: Self::Logits,
        top_k: i32,
        context: &Self::Context,
    ) -> Result<Self::Logits, Self::Error>;

    /// Applies nucleus filtering while retaining canonical vocabulary order.
    fn apply_top_p(
        logits: Self::Logits,
        top_p: f32,
        context: &Self::Context,
    ) -> Result<Self::Logits, Self::Error>;

    /// Applies minimum-relative-probability filtering.
    fn apply_min_p(
        logits: Self::Logits,
        min_p: f32,
        context: &Self::Context,
    ) -> Result<Self::Logits, Self::Error>;

    /// Masks tokens rejected by a portable vocabulary filter.
    fn apply_token_filter(
        logits: &Self::Logits,
        filter: &TokenFilter,
        context: &Self::Context,
    ) -> Result<Self::Logits, Self::Error>;

    /// Applies Mirostat's surprise cutoff after penalties and temperature.
    fn apply_mirostat(
        logits: &Self::Logits,
        history: &[u32],
        penalties: PenaltyConfig,
        temperature: f32,
        mu: f32,
        context: &Self::Context,
    ) -> Result<Self::Logits, Self::Error>;

    /// Selects from raw logits, applying temperature for stochastic sampling.
    fn sample_raw(
        logits: &Self::Logits,
        temperature: f32,
        random: Option<&mut Self::RandomState>,
        context: &Self::Context,
    ) -> Result<Self::Token, Self::Error>;

    /// Selects from logits already scaled by the policy.
    fn sample_processed(
        logits: &Self::Logits,
        temperature: f32,
        random: Option<&mut Self::RandomState>,
        context: &Self::Context,
    ) -> Result<Self::Token, Self::Error>;

    /// Materializes only the selected scalar token identifier.
    fn token_id(token: &Self::Token, context: &Self::Context) -> Result<u32, Self::Error>;

    /// Materializes one committed token probability from processed logits.
    fn token_probability(
        logits: &Self::Logits,
        token: u32,
        context: &Self::Context,
    ) -> Result<f32, Self::Error>;
}

/// Backend-neutral repetition/frequency/presence controls.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PenaltyConfig {
    /// Repetition multiplier; `1.0` disables it.
    pub repeat_penalty: f32,
    /// Number of recent tokens considered; negative means all.
    pub repeat_last_n: i32,
    /// Per-occurrence logit penalty.
    pub frequency_penalty: f32,
    /// One-time penalty for every present token.
    pub presence_penalty: f32,
}

impl PenaltyConfig {
    /// Returns whether every penalty is disabled.
    pub fn is_identity(self) -> bool {
        self.repeat_penalty == 1.0 && self.frequency_penalty == 0.0 && self.presence_penalty == 0.0
    }
}

impl Default for PenaltyConfig {
    fn default() -> Self {
        Self {
            repeat_penalty: 1.0,
            repeat_last_n: 64,
            frequency_penalty: 0.0,
            presence_penalty: 0.0,
        }
    }
}

/// Sampling policy suitable for lossless speculative decoding.
pub trait SpeculativeSampler<B: SamplingBackend> {
    /// Whether loaded checkpoint defaults should wrap this policy.
    fn uses_checkpoint_defaults(&self) -> bool {
        false
    }

    /// Whether optimistic draft work is an exact discardable fork.
    fn supports_exact_optimistic_promotion(&self) -> bool {
        false
    }

    /// Whether the committed generation grammar is complete.
    fn grammar_is_complete(&mut self) -> Result<bool, B::Error> {
        Ok(false)
    }

    /// Whether an uncommitted logical prefix completes the grammar.
    fn prefix_is_complete(&self, _history: &[u32]) -> Result<bool, B::Error> {
        Ok(false)
    }

    /// Applies penalties, filters, and temperature.
    fn process_logits(
        &mut self,
        logits: &B::Logits,
        temperature: f32,
        history: &[u32],
        context: &B::Context,
    ) -> Result<B::Logits, B::Error>;

    /// Selects from already processed logits.
    fn sample_processed(
        &self,
        logits: &B::Logits,
        temperature: f32,
        random: Option<&mut B::RandomState>,
        context: &B::Context,
    ) -> Result<B::Token, B::Error> {
        B::sample_processed(logits, temperature, random, context)
    }

    /// Commits a token selected from a processed target distribution.
    fn commit_token(
        &mut self,
        _processed_logits: &B::Logits,
        _token: u32,
        _context: &B::Context,
    ) -> Result<(), B::Error> {
        Ok(())
    }
}

/// Strategy for choosing a token from model logits.
pub trait Sampler<B: SamplingBackend> {
    /// Whether loaded checkpoint defaults should wrap this policy.
    fn uses_checkpoint_defaults(&self) -> bool {
        false
    }

    /// Selects one token from raw model logits.
    fn sample(
        &mut self,
        logits: &B::Logits,
        temperature: f32,
        random: Option<&mut B::RandomState>,
        context: &B::Context,
    ) -> Result<B::Token, B::Error>;
}

/// Grammar-aware wrapper around a backend-neutral sampling policy.
pub struct ConstrainedSampler<S, C> {
    policy: S,
    controller: C,
}

struct ConstraintCheckpoint<S, C> {
    policy: S,
    controller: C,
}

impl<S: Clone, C: Clone> Clone for ConstrainedSampler<S, C> {
    fn clone(&self) -> Self {
        Self {
            policy: self.policy.clone(),
            controller: self.controller.clone(),
        }
    }
}

impl<S, C> ConstrainedSampler<S, C> {
    /// Wraps a policy with a portable canonical constraint controller.
    pub fn new(policy: S, controller: C) -> Self {
        Self { policy, controller }
    }

    /// Returns the wrapped policy.
    pub const fn policy(&self) -> &S {
        &self.policy
    }

    /// Returns the portable constraint controller.
    pub const fn controller(&self) -> &C {
        &self.controller
    }

    /// Returns the portable constraint controller mutably.
    pub fn controller_mut(&mut self) -> &mut C {
        &mut self.controller
    }
}

impl<S: Clone, C: Clone> ConstrainedSampler<S, C> {
    fn checkpoint(&self) -> ConstraintCheckpoint<S, C> {
        ConstraintCheckpoint {
            policy: self.policy.clone(),
            controller: self.controller.clone(),
        }
    }
}

impl<B, S, C> SpeculativeSampler<B> for ConstrainedSampler<S, C>
where
    B: SamplingBackend,
    S: SpeculativeSampler<B> + Clone,
    C: SpeculativeTokenFilterController,
{
    fn supports_exact_optimistic_promotion(&self) -> bool {
        self.policy.supports_exact_optimistic_promotion()
    }

    fn grammar_is_complete(&mut self) -> Result<bool, B::Error> {
        self.controller
            .is_complete()
            .map_err(|error| B::error(error.to_string()))
    }

    fn prefix_is_complete(&self, history: &[u32]) -> Result<bool, B::Error> {
        self.controller
            .prefix_is_complete(history)
            .map_err(|error| B::error(error.to_string()))
    }

    fn process_logits(
        &mut self,
        logits: &B::Logits,
        temperature: f32,
        history: &[u32],
        context: &B::Context,
    ) -> Result<B::Logits, B::Error> {
        let filter = self
            .controller
            .filter_at(history)
            .map_err(|error| B::error(error.to_string()))?;
        let masked = B::apply_token_filter(logits, &filter, context)?;
        self.policy
            .process_logits(&masked, temperature, history, context)
    }

    fn sample_processed(
        &self,
        logits: &B::Logits,
        temperature: f32,
        random: Option<&mut B::RandomState>,
        context: &B::Context,
    ) -> Result<B::Token, B::Error> {
        self.policy
            .sample_processed(logits, temperature, random, context)
    }

    fn commit_token(
        &mut self,
        processed_logits: &B::Logits,
        token: u32,
        context: &B::Context,
    ) -> Result<(), B::Error> {
        let checkpoint = self.checkpoint();
        if let Err(error) = self
            .policy
            .commit_token(processed_logits, token, context)
            .and_then(|()| {
                self.controller
                    .commit_token(token)
                    .map_err(|error| B::error(error.to_string()))
            })
        {
            self.policy = checkpoint.policy;
            self.controller = checkpoint.controller;
            return Err(error);
        }
        Ok(())
    }
}

impl<B, S, C> Sampler<B> for ConstrainedSampler<S, C>
where
    B: SamplingBackend,
    S: Sampler<B> + Clone,
    C: TokenFilterController + Clone,
{
    fn sample(
        &mut self,
        logits: &B::Logits,
        temperature: f32,
        random: Option<&mut B::RandomState>,
        context: &B::Context,
    ) -> Result<B::Token, B::Error> {
        let checkpoint = self.checkpoint();
        let filter = self
            .controller
            .current_filter()
            .map_err(|error| B::error(error.to_string()))?;
        let masked = B::apply_token_filter(logits, &filter, context)?;
        let token = self.policy.sample(&masked, temperature, random, context)?;
        let token_id = B::token_id(&token, context)?;
        if let Err(error) = self.controller.commit_token(token_id) {
            self.policy = checkpoint.policy;
            self.controller = checkpoint.controller;
            return Err(B::error(error.to_string()));
        }
        Ok(token)
    }
}

/// Stateless greedy/categorical sampler.
#[derive(Debug, Clone, Copy)]
pub struct DefaultSampler;

impl<B: SamplingBackend> SpeculativeSampler<B> for DefaultSampler {
    fn uses_checkpoint_defaults(&self) -> bool {
        true
    }

    fn supports_exact_optimistic_promotion(&self) -> bool {
        true
    }

    fn process_logits(
        &mut self,
        logits: &B::Logits,
        temperature: f32,
        _history: &[u32],
        context: &B::Context,
    ) -> Result<B::Logits, B::Error> {
        if temperature == 0.0 {
            Ok(logits.clone())
        } else {
            B::scale_temperature(logits, temperature, context)
        }
    }
}

impl<B: SamplingBackend> Sampler<B> for DefaultSampler {
    fn uses_checkpoint_defaults(&self) -> bool {
        true
    }

    fn sample(
        &mut self,
        logits: &B::Logits,
        temperature: f32,
        random: Option<&mut B::RandomState>,
        context: &B::Context,
    ) -> Result<B::Token, B::Error> {
        B::sample_raw(logits, temperature, random, context)
    }
}

/// Configurable backend-neutral text sampler.
#[derive(Debug, Clone)]
pub struct GenerationSampler {
    /// Keep only the `top_k` highest-logit tokens when positive.
    pub top_k: i32,
    /// Nucleus probability mass.
    pub top_p: f32,
    /// Minimum probability relative to the most probable token.
    pub min_p: f32,
    /// Repetition multiplier.
    pub repeat_penalty: f32,
    /// Number of recent tokens considered by penalties.
    pub repeat_last_n: i32,
    /// Per-occurrence penalty.
    pub frequency_penalty: f32,
    /// One-time presence penalty.
    pub presence_penalty: f32,
    generated_tokens: Vec<u32>,
}

impl Default for GenerationSampler {
    fn default() -> Self {
        Self {
            top_k: 40,
            top_p: 0.95,
            min_p: 0.05,
            repeat_penalty: 1.0,
            repeat_last_n: 64,
            frequency_penalty: 0.0,
            presence_penalty: 0.0,
            generated_tokens: Vec::new(),
        }
    }
}

impl GenerationSampler {
    /// Creates a sampler with default controls.
    pub fn new() -> Self {
        Self::default()
    }

    /// Creates a sampler from a resolved portable generation configuration.
    pub fn from_resolved(config: ResolvedGenerationConfig) -> Self {
        Self::new()
            .top_k(config.top_k)
            .top_p(config.top_p)
            .min_p(config.min_p)
            .penalties(
                config.repetition_penalty,
                config.repeat_last_n,
                config.frequency_penalty,
                config.presence_penalty,
            )
    }

    /// Seeds accepted-token history.
    pub fn with_generated_tokens(mut self, tokens: impl IntoIterator<Item = u32>) -> Self {
        self.generated_tokens = tokens.into_iter().collect();
        self
    }

    /// Sets top-k filtering.
    pub fn top_k(mut self, value: i32) -> Self {
        self.top_k = value;
        self
    }

    /// Sets nucleus filtering.
    pub fn top_p(mut self, value: f32) -> Self {
        self.top_p = value;
        self
    }

    /// Sets minimum-relative-probability filtering.
    pub fn min_p(mut self, value: f32) -> Self {
        self.min_p = value;
        self
    }

    /// Sets repetition, frequency, and presence penalties.
    pub fn penalties(
        mut self,
        repeat_penalty: f32,
        repeat_last_n: i32,
        frequency_penalty: f32,
        presence_penalty: f32,
    ) -> Self {
        self.repeat_penalty = repeat_penalty;
        self.repeat_last_n = repeat_last_n;
        self.frequency_penalty = frequency_penalty;
        self.presence_penalty = presence_penalty;
        self
    }

    /// Returns accepted-token history.
    pub fn generated_tokens(&self) -> &[u32] {
        &self.generated_tokens
    }

    /// Replaces accepted-token history.
    pub fn set_generated_tokens(&mut self, tokens: impl IntoIterator<Item = u32>) {
        self.generated_tokens = tokens.into_iter().collect();
    }

    /// Records a token accepted outside this sampler.
    pub fn accept_token(&mut self, token: u32) {
        self.generated_tokens.push(token);
    }

    /// Clears accepted-token history.
    pub fn clear_generated_tokens(&mut self) {
        self.generated_tokens.clear();
    }

    /// Returns the portable penalty controls.
    pub const fn penalty_config(&self) -> PenaltyConfig {
        PenaltyConfig {
            repeat_penalty: self.repeat_penalty,
            repeat_last_n: self.repeat_last_n,
            frequency_penalty: self.frequency_penalty,
            presence_penalty: self.presence_penalty,
        }
    }

    fn process_for<B: SamplingBackend>(
        &self,
        logits: &B::Logits,
        history: &[u32],
        context: &B::Context,
    ) -> Result<B::Logits, B::Error> {
        let logits = B::apply_penalties(logits, history, self.penalty_config(), context)?;
        let logits = B::apply_top_k(logits, self.top_k, context)?;
        let logits = B::apply_top_p(logits, self.top_p, context)?;
        B::apply_min_p(logits, self.min_p, context)
    }
}

impl<B: SamplingBackend> SpeculativeSampler<B> for GenerationSampler {
    fn supports_exact_optimistic_promotion(&self) -> bool {
        true
    }

    fn process_logits(
        &mut self,
        logits: &B::Logits,
        temperature: f32,
        history: &[u32],
        context: &B::Context,
    ) -> Result<B::Logits, B::Error> {
        let logits = self.process_for::<B>(logits, history, context)?;
        if temperature == 0.0 {
            Ok(logits)
        } else {
            B::scale_temperature(&logits, temperature, context)
        }
    }
}

impl<B: SamplingBackend> Sampler<B> for GenerationSampler {
    fn sample(
        &mut self,
        logits: &B::Logits,
        temperature: f32,
        random: Option<&mut B::RandomState>,
        context: &B::Context,
    ) -> Result<B::Token, B::Error> {
        let logits = self.process_for::<B>(logits, &self.generated_tokens, context)?;
        let token = B::sample_raw(&logits, temperature, random, context)?;
        self.generated_tokens.push(B::token_id(&token, context)?);
        Ok(token)
    }
}

/// Adaptive Mirostat V2 policy with backend-neutral state.
#[derive(Debug, Clone)]
pub struct MirostatV2Sampler {
    tau: f32,
    eta: f32,
    mu: f32,
    penalties: GenerationSampler,
}

impl Default for MirostatV2Sampler {
    fn default() -> Self {
        Self {
            tau: 5.0,
            eta: 0.1,
            mu: 10.0,
            penalties: GenerationSampler::new().top_k(0).top_p(1.0).min_p(0.0),
        }
    }
}

impl MirostatV2Sampler {
    /// Creates a sampler targeting `tau` bits of surprise.
    pub fn new(tau: f32, eta: f32) -> Result<Self, SamplingConfigurationError> {
        validate_positive_finite("Mirostat V2 tau", tau)?;
        validate_positive_finite("Mirostat V2 eta", eta)?;
        Ok(Self {
            tau,
            eta,
            mu: 2.0 * tau,
            penalties: GenerationSampler::new().top_k(0).top_p(1.0).min_p(0.0),
        })
    }

    /// Sets penalties applied before adaptive truncation.
    pub fn penalties(
        mut self,
        repeat_penalty: f32,
        repeat_last_n: i32,
        frequency_penalty: f32,
        presence_penalty: f32,
    ) -> Self {
        self.penalties = self.penalties.penalties(
            repeat_penalty,
            repeat_last_n,
            frequency_penalty,
            presence_penalty,
        );
        self
    }

    /// Target surprise in bits.
    pub const fn tau(&self) -> f32 {
        self.tau
    }

    /// Adaptation rate.
    pub const fn eta(&self) -> f32 {
        self.eta
    }

    /// Current adaptive surprise limit.
    pub const fn mu(&self) -> f32 {
        self.mu
    }

    /// Accepted-token history.
    pub fn generated_tokens(&self) -> &[u32] {
        self.penalties.generated_tokens()
    }

    /// Records an externally accepted token and its normalized probability.
    pub fn accept_token(
        &mut self,
        token: u32,
        probability: f32,
    ) -> Result<(), SamplingConfigurationError> {
        if !probability.is_finite() || probability <= 0.0 || probability > 1.0 {
            return Err(SamplingConfigurationError::Invalid(
                "accepted Mirostat V2 token probability must be finite and in (0, 1]".into(),
            ));
        }
        self.update_mu(-probability.log2());
        self.penalties.accept_token(token);
        Ok(())
    }

    /// Resets adaptive state and history.
    pub fn reset(&mut self) {
        self.mu = 2.0 * self.tau;
        self.penalties.clear_generated_tokens();
    }

    fn update_mu(&mut self, observed_surprise: f32) {
        self.mu -= self.eta * (observed_surprise - self.tau);
    }

    fn process_for<B: SamplingBackend>(
        &self,
        logits: &B::Logits,
        temperature: f32,
        history: &[u32],
        context: &B::Context,
    ) -> Result<B::Logits, B::Error> {
        if !temperature.is_finite() || temperature <= 0.0 {
            return Err(B::error(
                "Mirostat V2 requires a finite temperature greater than zero".into(),
            ));
        }
        B::apply_mirostat(
            logits,
            history,
            self.penalties.penalty_config(),
            temperature,
            self.mu,
            context,
        )
    }

    fn commit_for<B: SamplingBackend>(
        &mut self,
        logits: &B::Logits,
        token: u32,
        context: &B::Context,
    ) -> Result<(), B::Error> {
        let probability = B::token_probability(logits, token, context)?;
        self.accept_token(token, probability)
            .map_err(|error| B::error(error.to_string()))
    }
}

impl<B: SamplingBackend> Sampler<B> for MirostatV2Sampler {
    fn sample(
        &mut self,
        logits: &B::Logits,
        temperature: f32,
        random: Option<&mut B::RandomState>,
        context: &B::Context,
    ) -> Result<B::Token, B::Error> {
        let processed = self.process_for::<B>(
            logits,
            temperature,
            self.penalties.generated_tokens(),
            context,
        )?;
        let token = B::sample_processed(&processed, temperature, random, context)?;
        self.commit_for::<B>(&processed, B::token_id(&token, context)?, context)?;
        Ok(token)
    }
}

impl<B: SamplingBackend> SpeculativeSampler<B> for MirostatV2Sampler {
    fn process_logits(
        &mut self,
        logits: &B::Logits,
        temperature: f32,
        history: &[u32],
        context: &B::Context,
    ) -> Result<B::Logits, B::Error> {
        self.process_for::<B>(logits, temperature, history, context)
    }

    fn commit_token(
        &mut self,
        processed_logits: &B::Logits,
        token: u32,
        context: &B::Context,
    ) -> Result<(), B::Error> {
        self.commit_for::<B>(processed_logits, token, context)
    }
}

/// Invalid backend-neutral sampling configuration.
#[derive(Debug, Clone, Eq, PartialEq, thiserror::Error)]
pub enum SamplingConfigurationError {
    /// A numeric or probability control is invalid.
    #[error("{0}")]
    Invalid(String),
}

fn validate_positive_finite(name: &str, value: f32) -> Result<(), SamplingConfigurationError> {
    if value.is_finite() && value > 0.0 {
        Ok(())
    } else {
        Err(SamplingConfigurationError::Invalid(format!(
            "{name} must be finite and greater than zero"
        )))
    }
}

#[cfg(test)]
mod tests {
    use super::{GenerationSampler, MirostatV2Sampler};

    #[test]
    fn generation_history_is_backend_neutral() {
        let mut sampler = GenerationSampler::new().with_generated_tokens([1, 2]);
        sampler.accept_token(3);
        assert_eq!(sampler.generated_tokens(), &[1, 2, 3]);
        sampler.set_generated_tokens([5, 8]);
        assert_eq!(sampler.generated_tokens(), &[5, 8]);
        sampler.clear_generated_tokens();
        assert!(sampler.generated_tokens().is_empty());
    }

    #[test]
    fn mirostat_state_is_backend_neutral() {
        let mut sampler = MirostatV2Sampler::default();
        sampler.accept_token(42, 2.0f32.powi(-7)).unwrap();
        assert!((sampler.mu() - 9.8).abs() < 1e-6);
        assert_eq!(sampler.generated_tokens(), &[42]);
        sampler.reset();
        assert_eq!(sampler.mu(), 10.0);
        assert!(sampler.generated_tokens().is_empty());
    }
}