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
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
// Copyright (C) 2026 quantz8a
// SPDX-License-Identifier: Apache-2.0
//! Offline batch engine plus the continuous-batching scheduler.
use std::cmp::Ordering;
use std::collections::{HashMap, VecDeque};
use crate::apc::ApcCache;
use crate::dsl::{Op, Program, StreamResult};
use crate::grammar::mask_logits;
use crate::kv_cache::{BlockId, PagedKvCache};
use crate::model::{ModelEngine, ModelSession, NGramModel};
use crate::radix_cache::{KvSlot, RadixCache};
use crate::sampler::{log_softmax, Sampler};
use crate::spec::{
FinishReason, GenerationOutput, RejectReason, SamplingParams, TokenId, WriteRequest,
};
use crate::tokenizer::{ByteTokenizer, Tokenizer};
/// Admission / prefill ordering for the waiting queue.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum SchedulePolicy {
/// First-come, first-served (the original behavior).
#[default]
Fcfs,
/// Materialize the request with the longest shared prefix first, maximizing
/// cross-request reuse (best for TTFT / compute-saved under chat traffic).
LongestPrefix,
/// Materialize the request with the fewest remaining prompt tokens first,
/// maximizing admission throughput (best for low tail latency).
ShortestPrompt,
}
/// Which prefix-cache structure the engine consults on admission.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum CacheBackend {
/// Radix trie (token-granular walk, partial-tail reuse).
#[default]
Radix,
/// Block-level chained-hash table (vLLM APC-style, block-granular reuse
/// and eviction).
Apc,
}
/// Runtime configuration.
#[derive(Clone, Debug)]
pub struct EngineConfig {
/// Number of physical KV pages.
pub num_kv_blocks: usize,
/// Tokens per page.
pub block_size: usize,
/// Admitted-request cap for a decode batch.
pub max_running_requests: usize,
/// Per-step token budget for prefill (chunked prefill).
pub max_prefill_tokens_per_step: usize,
/// RNG seed for the engine sampler.
pub seed: u64,
/// Waiting-queue ordering policy.
pub schedule_policy: SchedulePolicy,
/// Evict the least-recently-used radix prefix entries when the KV pool is
/// exhausted instead of failing the request.
pub evict_on_pressure: bool,
/// Maximum number of requests admitted into the waiting queue of one batch.
/// Requests beyond this cap are rejected with [`RejectReason::QueueFull`].
pub max_waiting_requests: usize,
/// Per-request SLO guard: a request whose `prompt + max_tokens` exceeds
/// this cap is rejected with [`RejectReason::TooLong`].
pub max_total_tokens: usize,
/// Prefix-cache structure consulted on admission.
pub cache_backend: CacheBackend,
}
impl Default for EngineConfig {
fn default() -> Self {
Self {
num_kv_blocks: 4096,
block_size: 16,
max_running_requests: 32,
max_prefill_tokens_per_step: 32,
seed: 0,
schedule_policy: SchedulePolicy::default(),
evict_on_pressure: true,
max_waiting_requests: 256,
max_total_tokens: 4096,
cache_backend: CacheBackend::default(),
}
}
}
/// A token production loop independent of the model/tokenizer backends.
pub struct Engine<M: ModelEngine, T: Tokenizer> {
tokenizer: T,
model: M,
sampler: Sampler,
kv: PagedKvCache,
radix: RadixCache,
apc: ApcCache,
config: EngineConfig,
next_request_id: usize,
max_prefill_tokens_per_step: usize,
checkpoints: HashMap<u64, Checkpoint>,
next_checkpoint_id: u64,
total_requests: u64,
total_forward: u64,
total_decode_steps: u64,
total_decode_calls: u64,
total_graft_tokens: u64,
total_prefill_chunks: u64,
total_prompt_tokens: u64,
total_prefill_tokens: u64,
total_output_tokens: u64,
total_faults: u64,
total_rejected: u64,
total_checkpoint_hits: u64,
}
/// Handle to a pinned KV checkpoint: a materialized prompt prefix that later
/// requests can branch from with a guaranteed full prefix hit.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct CheckpointId(pub u64);
/// A pinned prefix: logical tokens, their physical slots, and the deduped
/// block list whose references the checkpoint holds as its pin.
struct Checkpoint {
tokens: Vec<TokenId>,
locs: Vec<KvSlot>,
blocks: Vec<BlockId>,
/// Model-side KV snapshot of the trunk, when the backend supports
/// sessions: branches fork it instead of recomputing the prefix.
session: Option<Box<dyn ModelSession>>,
}
/// The battery out-of-the-box engine: byte tokenizer + n-gram toy model.
pub type ToyEngine = Engine<NGramModel, ByteTokenizer>;
struct Seq {
id: usize,
sampling: SamplingParams,
/// Full prompt tokens (logical).
prompt: Vec<TokenId>,
/// Logical buffer materialized so far (prefix + prefilled suffix), then generated tokens.
tokens: Vec<TokenId>,
/// Generated tokens only.
output: Vec<TokenId>,
prompt_len: usize,
prefix_hit: usize,
prefill_cost: usize,
/// How many prompt tokens have been materialized into KV pages so far.
prefill_pos: usize,
/// Physical pages holding `tokens`.
blocks: Vec<BlockId>,
/// Physical slot of each token in `tokens` (parallel array).
locs: Vec<KvSlot>,
/// Incremental decode session (models with a real KV cache). `None` keeps
/// the stateless full-replay contract.
session: Option<Box<dyn ModelSession>>,
}
enum Advance {
Running(Seq),
Done(GenerationOutput),
}
struct Job {
vars: HashMap<String, String>,
transcript: String,
ops: Vec<Op>,
}
impl<M: ModelEngine, T: Tokenizer> Engine<M, T> {
pub fn new(tokenizer: T, model: M, config: EngineConfig) -> Self {
let sampler = Sampler::new(config.seed);
let kv = PagedKvCache::new(config.num_kv_blocks, config.block_size);
let max_prefill = config.max_prefill_tokens_per_step.max(1);
Self {
tokenizer,
model,
sampler,
kv,
radix: RadixCache::new(),
apc: ApcCache::new(),
config,
next_request_id: 0,
max_prefill_tokens_per_step: max_prefill,
checkpoints: HashMap::new(),
next_checkpoint_id: 0,
total_requests: 0,
total_forward: 0,
total_decode_steps: 0,
total_decode_calls: 0,
total_graft_tokens: 0,
total_prefill_chunks: 0,
total_prompt_tokens: 0,
total_prefill_tokens: 0,
total_output_tokens: 0,
total_faults: 0,
total_rejected: 0,
total_checkpoint_hits: 0,
}
}
pub fn vocab_size(&self) -> usize {
self.model.vocab_size()
}
pub fn tokenizer(&self) -> &T {
&self.tokenizer
}
pub fn model(&self) -> &M {
&self.model
}
pub fn config(&self) -> &EngineConfig {
&self.config
}
pub fn stats(&self) -> EngineStats {
EngineStats {
total_requests: self.total_requests,
total_forward: self.total_forward,
total_decode_steps: self.total_decode_steps,
total_decode_calls: self.total_decode_calls,
model_graft_tokens: self.total_graft_tokens,
radix_nodes: self.radix.num_nodes(),
radix_queries: self.radix.num_queries() as u64,
radix_hit_queries: self.radix.hit_queries() as u64,
radix_hit_tokens: self.radix.hit_tokens() as u64,
apc_blocks: self.apc.num_blocks(),
apc_hit_tokens: self.apc.hit_tokens() as u64,
kv_blocks: self.kv.num_blocks(),
kv_free_blocks: self.kv.num_free_blocks(),
kv_allocations: self.kv.num_allocations(),
kv_frees: self.kv.num_frees(),
prefill_chunks: self.total_prefill_chunks,
total_prompt_tokens: self.total_prompt_tokens,
total_prefill_tokens: self.total_prefill_tokens,
total_output_tokens: self.total_output_tokens,
faulted_requests: self.total_faults,
rejected_requests: self.total_rejected,
active_checkpoints: self.checkpoints.len(),
checkpoint_hit_tokens: self.total_checkpoint_hits,
}
}
/// Fraction of prefix-cache queries that hit a cached prefix.
pub fn radix_hit_rate(&self) -> f64 {
self.radix.cache_hit_rate()
}
/// Generate for a single request. Reseeds the sampler from the request so
/// output is reproducible per request.
pub fn generate(&mut self, req: &WriteRequest) -> GenerationOutput {
self.sampler.seed(req.sampling.seed);
self.generate_batch(std::slice::from_ref(req))
.pop()
.expect("generate_batch returned no output")
}
/// Continuous (dynamic) batching with chunked prefill: each scheduler step
/// spends up to `max_prefill_tokens_per_step` materializing waiting prompts
/// (long prompts are split across steps), then decodes one token for every
/// ready sequence, until both the waiting queue and the decode batch drain.
pub fn generate_batch(&mut self, reqs: &[WriteRequest]) -> Vec<GenerationOutput> {
if let Some(first) = reqs.first() {
self.sampler.seed(first.sampling.seed);
}
let mut waiting: VecDeque<Seq> = VecDeque::with_capacity(reqs.len());
let mut outputs = Vec::with_capacity(reqs.len());
for req in reqs {
if waiting.len() >= self.config.max_waiting_requests {
outputs.push(self.reject(RejectReason::QueueFull, 0));
continue;
}
match self.admit(req) {
Ok(seq) => waiting.push_back(seq),
Err(rejected) => outputs.push(rejected),
}
}
self.drain(waiting, outputs)
}
/// Continuous-batching loop: prefill waiting sequences under the per-step
/// token budget, decode one token for every ready sequence, until both the
/// waiting queue and the decode batch drain.
fn drain(
&mut self,
mut waiting: VecDeque<Seq>,
mut outputs: Vec<GenerationOutput>,
) -> Vec<GenerationOutput> {
let mut ready: VecDeque<Seq> = VecDeque::new();
let mut active: Vec<Seq> = Vec::new();
loop {
// Prefill phase: spend the per-step token budget.
let mut budget = self.max_prefill_tokens_per_step;
while budget > 0 {
let mut seq = match pop_next(&mut waiting, self.config.schedule_policy) {
Some(s) => s,
None => break,
};
let remaining = seq.prompt.len().saturating_sub(seq.prefill_pos);
let chunk = remaining.min(budget);
if chunk == 0 {
// Entire prompt already served from the prefix cache.
ready.push_back(seq);
continue;
}
self.prefill_chunk(&mut seq, chunk);
budget -= chunk;
if seq.prefill_pos >= seq.prompt.len() {
ready.push_back(seq);
} else {
waiting.push_back(seq);
}
}
// Admit fully prefilled sequences while there is decode room.
while active.len() < self.config.max_running_requests {
match ready.pop_front() {
Some(seq) => active.push(seq),
None => break,
}
}
if active.is_empty() && ready.is_empty() && waiting.is_empty() {
break;
}
// Decode phase: one token for every ready sequence. Sequences
// whose session is missing exactly one token are advanced in a
// single batched model call when the backend supports it:
// decode is weight-bandwidth-bound, so one [B, 1] forward costs
// roughly the memory traffic of one [1, 1] forward.
let mut next = Vec::with_capacity(active.len());
// Length gate first: sequences at their token budget finalize
// without touching the model.
let mut pending: Vec<Seq> = Vec::with_capacity(active.len());
for seq in active.drain(..) {
if seq.output.len() >= seq.sampling.max_tokens {
outputs.push(self.finalize(seq, FinishReason::Length));
} else {
pending.push(seq);
}
}
// Partition batch candidates: a session that is missing exactly
// one token. Multi-token feeds (first step after prefill) and
// empty feeds (fresh checkpoint branches) stay on the
// per-sequence path; stateless sequences always do.
let mut batch_pos: Vec<usize> = Vec::new();
let mut batch_sessions: Vec<Box<dyn ModelSession>> = Vec::new();
let mut batch_feeds: Vec<Vec<TokenId>> = Vec::new();
for (i, seq) in pending.iter_mut().enumerate() {
let Some(session) = seq.session.as_ref() else {
continue;
};
let fed = session.context_len();
if fed > seq.tokens.len() || seq.tokens.len() - fed != 1 {
continue;
}
batch_feeds.push(vec![seq.tokens[fed]]);
batch_pos.push(i);
batch_sessions.push(seq.session.take().expect("session checked above"));
}
let mut logits_slots: Vec<Option<Vec<f32>>> =
(0..pending.len()).map(|_| None).collect();
if !batch_sessions.is_empty() {
let batched: Option<Vec<Vec<f32>>> = if batch_sessions.len() >= 2 {
let feed_refs: Vec<&[TokenId]> =
batch_feeds.iter().map(|f| f.as_slice()).collect();
self.model
.session_forward_batch(&mut batch_sessions, &feed_refs)
.map(|all| {
self.total_decode_calls += 1;
all
})
} else {
None
};
let logits = match batched {
Some(all) if all.len() == batch_pos.len() => all,
Some(all) => {
// Length-mismatch recovery: the sessions already
// consumed their feeds, so re-read cached last
// logits with an empty feed instead of double-feeding.
let mut fixed = Vec::with_capacity(batch_sessions.len());
for (i, session) in batch_sessions.iter_mut().enumerate() {
fixed.push(match all.get(i) {
Some(lg) => lg.clone(),
None => session.forward(&[]),
});
}
fixed
}
None => {
// Unbatched fallback: one model call per session.
let mut all = Vec::with_capacity(batch_sessions.len());
for (session, feed) in batch_sessions.iter_mut().zip(&batch_feeds) {
self.total_decode_calls += 1;
all.push(session.forward(feed));
}
all
}
};
for (pos, logits) in batch_pos.iter().zip(logits) {
logits_slots[*pos] = Some(logits);
}
for (pos, session) in batch_pos.iter().zip(batch_sessions) {
pending[*pos].session = Some(session);
}
}
for (i, mut seq) in pending.into_iter().enumerate() {
self.total_forward += 1;
self.total_decode_steps += 1;
let logits = match logits_slots[i].take() {
Some(logits) => logits,
None => {
// Per-sequence path: session with a multi-token or
// empty feed, or stateless full replay. A misbehaving
// session (context ahead of the sequence) is dropped
// rather than panicking the engine.
let use_session = matches!(
seq.session.as_ref(),
Some(s) if s.context_len() <= seq.tokens.len()
);
if use_session {
let session =
seq.session.as_mut().expect("session checked above");
let fed = session.context_len();
if fed < seq.tokens.len() {
self.total_decode_calls += 1;
}
session.forward(&seq.tokens[fed..])
} else {
seq.session = None;
self.total_decode_calls += 1;
self.model.forward(&seq.tokens)
}
}
};
match self.advance_with_logits(seq, logits) {
Advance::Running(s) => next.push(s),
Advance::Done(out) => outputs.push(out),
}
}
active = next;
}
outputs
}
/// Materialize `text` into the KV cache, publish it into the prefix cache,
/// and pin its physical blocks so later branches always get a full prefix
/// hit. This is the primitive for agent-tree / parallel-branch workloads:
/// create the shared trunk once, then branch many continuations off it
/// without re-prefill and without fear of eviction.
pub fn create_checkpoint(&mut self, text: &str) -> CheckpointId {
let tokens = self.tokenizer.encode(text);
let (prefix_hit, prefix_slots) = self.match_prefix(&tokens);
let mut seq = Seq {
id: usize::MAX,
sampling: SamplingParams::default(),
prompt: tokens.clone(),
tokens: tokens[..prefix_hit].to_vec(),
output: Vec::new(),
prompt_len: tokens.len(),
prefix_hit,
prefill_cost: tokens.len() - prefix_hit,
prefill_pos: prefix_hit,
blocks: Vec::new(),
locs: Vec::with_capacity(tokens.len()),
session: None,
};
for slot in &prefix_slots {
let block = slot.0;
if seq.blocks.last().copied() != Some(block) {
self.kv.inc_ref(block);
seq.blocks.push(block);
}
}
seq.locs.extend_from_slice(&prefix_slots);
while seq.prefill_pos < seq.prompt.len() {
let chunk = (seq.prompt.len() - seq.prefill_pos).min(self.max_prefill_tokens_per_step);
self.prefill_chunk(&mut seq, chunk);
}
self.publish_prefix(&seq.tokens, &seq.locs);
// Model-side pinned trunk: keep a session holding the checkpoint KV
// so branches fork it instead of recomputing the prefix. Graft from
// the backend's vault first (the trunk may repeat an earlier path),
// then feed only the missing suffix; the cap keeps >=1 fed token so
// the trunk's last-position logits exist for empty-continuation
// branches. Backends without sessions keep the stateless path.
let mut session = match self
.model
.graft_session(&seq.tokens, seq.tokens.len().saturating_sub(1))
{
Some(s) => {
self.total_graft_tokens += s.context_len() as u64;
Some(s)
}
None => self.model.begin_session(),
};
if let Some(s) = session.as_mut() {
let fed = s.context_len();
if fed < seq.tokens.len() {
s.forward(&seq.tokens[fed..]);
}
}
// Also offer the trunk to the backend's vault, so unrelated future
// requests (not just checkpoint branches) can graft this prefix.
if !seq.tokens.is_empty() {
if let Some(fork) = session.as_ref().and_then(|s| s.fork()) {
self.model
.offer_session_kv(&seq.tokens, seq.tokens.len(), fork);
}
}
// The request-side references are kept as the checkpoint pin: the
// blocks stay resident (immune to prefix-cache eviction) until
// `drop_checkpoint`.
let id = CheckpointId(self.next_checkpoint_id);
self.next_checkpoint_id += 1;
self.checkpoints.insert(
id.0,
Checkpoint {
tokens: seq.tokens,
locs: seq.locs,
blocks: seq.blocks,
session,
},
);
id
}
/// Number of currently pinned checkpoints.
pub fn num_checkpoints(&self) -> usize {
self.checkpoints.len()
}
/// Token count of a checkpoint's pinned prefix.
pub fn checkpoint_tokens(&self, id: CheckpointId) -> Option<usize> {
self.checkpoints.get(&id.0).map(|c| c.tokens.len())
}
/// Release a checkpoint pin. The blocks stay in the prefix cache and
/// remain reusable until ordinary eviction reclaims them. Returns `false`
/// when no such checkpoint exists.
pub fn drop_checkpoint(&mut self, id: CheckpointId) -> bool {
let Some(cp) = self.checkpoints.remove(&id.0) else {
return false;
};
for b in cp.blocks {
self.kv.dec_ref(b);
}
true
}
/// Branch generation off a pinned checkpoint: the checkpoint tokens act as
/// the prompt prefix with a guaranteed full hit (no prefix-cache walk,
/// immune to eviction), and `continuation` is appended on top. Returns
/// `None` when the checkpoint does not exist.
pub fn generate_from_checkpoint(
&mut self,
id: CheckpointId,
continuation: &str,
sampling: SamplingParams,
) -> Option<GenerationOutput> {
let (tokens, locs, blocks, session) = {
let cp = self.checkpoints.get(&id.0)?;
(
cp.tokens.clone(),
cp.locs.clone(),
cp.blocks.clone(),
// Branch off the checkpoint's KV snapshot when the backend
// supports forking; the branch then only pays for the
// continuation, not the trunk.
cp.session.as_ref().and_then(|s| s.fork()),
)
};
self.sampler.seed(sampling.seed);
let max_new = sampling.max_tokens;
let mut prompt = tokens.clone();
prompt.extend_from_slice(&self.tokenizer.encode(continuation));
let prompt_len = prompt.len();
let reason = if sampling.grammar.is_some() && !self.tokenizer.is_byte_level() {
Some(RejectReason::Unsupported)
} else if prompt_len.saturating_add(max_new) > self.config.max_total_tokens {
Some(RejectReason::TooLong)
} else {
None
};
if let Some(reason) = reason {
return Some(self.reject(reason, prompt_len));
}
// Take branch-side references on the pinned blocks.
for &b in &blocks {
self.kv.inc_ref(b);
}
let prefix_hit = tokens.len();
let prefill_cost = prompt_len - prefix_hit;
self.total_forward += prefill_cost as u64;
self.total_prompt_tokens += prompt_len as u64;
self.total_prefill_tokens += prefill_cost as u64;
self.total_requests += 1;
self.total_checkpoint_hits += prefix_hit as u64;
let rid = self.next_request_id;
self.next_request_id += 1;
let seq = Seq {
id: rid,
sampling,
prompt,
tokens,
output: Vec::with_capacity(max_new),
prompt_len,
prefix_hit,
prefill_cost,
prefill_pos: prefix_hit,
blocks,
locs,
session: session.or_else(|| self.model.begin_session()),
};
let mut waiting = VecDeque::new();
waiting.push_back(seq);
let mut outputs = self.drain(waiting, Vec::new());
Some(outputs.pop().expect("one sequence yields one output"))
}
/// Build a rejected-output marker and charge the rejection metric.
fn reject(&mut self, reason: RejectReason, prompt_tokens: usize) -> GenerationOutput {
self.total_rejected += 1;
GenerationOutput {
text: String::new(),
full_text: None,
output_token_ids: Vec::new(),
finish_reason: FinishReason::Rejected,
prompt_tokens,
prefix_hit_tokens: 0,
forward_count: 0,
rejection: Some(reason),
}
}
/// Encode, apply admission / SLO guards, match the prefix cache, and take
/// references to the shared blocks. Returns a not-yet-prefilled sequence for
/// accepted requests, or a `Rejected` [`GenerationOutput`] otherwise.
fn admit(&mut self, req: &WriteRequest) -> Result<Seq, GenerationOutput> {
let prompt = self.tokenizer.encode(&req.text);
let prompt_len = prompt.len();
let max_new = req.sampling.max_tokens;
let reason = if req.sampling.grammar.is_some() && !self.tokenizer.is_byte_level() {
Some(RejectReason::Unsupported)
} else if prompt_len == 0 && req.sampling.grammar.is_none() {
Some(RejectReason::EmptyPrompt)
} else if prompt_len.saturating_add(max_new) > self.config.max_total_tokens {
Some(RejectReason::TooLong)
} else {
None
};
if let Some(reason) = reason {
return Err(self.reject(reason, prompt_len));
}
let id = self.next_request_id;
self.next_request_id += 1;
let (prefix_hit, prefix_slots) = self.match_prefix(&prompt);
let prefill_cost = prompt_len.saturating_sub(prefix_hit);
self.total_forward += prefill_cost as u64;
self.total_prompt_tokens += prompt_len as u64;
self.total_prefill_tokens += prefill_cost as u64;
let mut seq = Seq {
id,
sampling: req.sampling.clone(),
prompt: prompt.clone(),
tokens: prompt[..prefix_hit].to_vec(),
output: Vec::with_capacity(max_new),
prompt_len,
prefix_hit,
prefill_cost,
prefill_pos: prefix_hit,
blocks: Vec::new(),
locs: Vec::with_capacity(prompt_len + max_new),
session: {
// Tensor-level prefix reuse (true RadixAttention): ask the
// backend for a session whose KV already covers a prefix of
// this prompt. The cap guarantees the first step still feeds
// >=1 token (which produces the first-step logits).
match self
.model
.graft_session(&prompt, prompt_len.saturating_sub(1))
{
Some(s) => {
self.total_graft_tokens += s.context_len() as u64;
Some(s)
}
None => self.model.begin_session(),
}
},
};
for slot in &prefix_slots {
let block = slot.0;
if seq.blocks.last().copied() != Some(block) {
self.kv.inc_ref(block);
seq.blocks.push(block);
}
}
seq.locs.extend_from_slice(&prefix_slots);
self.total_requests += 1;
Ok(seq)
}
/// Materialize the next `n` prompt tokens into copy-on-write paged memory.
fn prefill_chunk(&mut self, seq: &mut Seq, n: usize) {
if n == 0 {
return;
}
let end = seq.prefill_pos + n;
let chunk = seq.prompt[seq.prefill_pos..end].to_vec();
for tok in &chunk {
self.append_physical(seq, *tok);
}
seq.tokens.extend_from_slice(&chunk);
seq.prefill_pos = end;
self.total_prefill_chunks += 1;
}
/// Score candidate continuations for SGLang-style `select`.
/// Returns `(choice, mean_log_prob)` pairs.
pub fn score(&mut self, context_text: &str, choices: &[String]) -> Vec<(String, f32)> {
let ctx = self.tokenizer.encode(context_text);
choices
.iter()
.map(|choice| {
let toks = self.tokenizer.encode(choice);
let mut prefix = ctx.clone();
let mut total = 0.0f32;
for &t in &toks {
self.total_forward += 1;
let logits = self.model.forward(&prefix);
let lp = log_softmax(&logits);
let idx = t as usize;
total += if idx < lp.len() { lp[idx] } else { -10.0 };
prefix.push(t);
}
let mean = if toks.is_empty() {
f32::NEG_INFINITY
} else {
total / toks.len() as f32
};
(choice.clone(), mean)
})
.collect()
}
/// Execute an SGLang-style frontend program. Returns one [`StreamResult`]
/// per forked branch.
pub fn run_program(&mut self, program: &Program) -> Vec<StreamResult> {
let mut jobs = VecDeque::new();
jobs.push_back(Job {
vars: HashMap::new(),
transcript: String::new(),
ops: program.ops.clone(),
});
let mut results = Vec::new();
while let Some(mut job) = jobs.pop_front() {
if job.ops.is_empty() {
results.push(StreamResult {
vars: job.vars,
transcript: job.transcript,
});
continue;
}
let op = job.ops.remove(0);
match op {
Op::System(text) => {
job.transcript.push_str(&format!("[SYS] {text}\n"));
jobs.push_back(job);
}
Op::User(text) => {
job.transcript.push_str(&format!("[USER] {text}\n"));
jobs.push_back(job);
}
Op::Assistant(text) => {
job.transcript.push_str(&format!("[ASSISTANT] {text}\n"));
jobs.push_back(job);
}
Op::Gen { name, params } => {
let out = self.generate(&WriteRequest::new(job.transcript.clone(), params));
job.vars.insert(name, out.text.clone());
job.transcript.push_str(&format!("[ASSISTANT] {}\n", out.text));
jobs.push_back(job);
}
Op::Select {
name,
choices,
params: _params,
} => {
let scores = self.score(&job.transcript, &choices);
let best = scores
.into_iter()
.max_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(Ordering::Equal))
.unwrap_or((String::new(), f32::NEG_INFINITY));
job.vars.insert(name, best.0.clone());
job.transcript.push_str(&format!("[ASSISTANT] {}\n", best.0));
jobs.push_back(job);
}
Op::Fork { branches } => {
let remaining = job.ops.clone();
for branch in branches {
let mut ops = branch.ops.clone();
ops.extend(remaining.clone());
jobs.push_back(Job {
vars: job.vars.clone(),
transcript: job.transcript.clone(),
ops,
});
}
}
}
}
results
}
// --- internals -----------------------------------------------------------
/// Consult the configured prefix cache for `prompt`, returning the number
/// of reusable tokens and the physical slot of each, in order.
fn match_prefix(&mut self, prompt: &[TokenId]) -> (usize, Vec<KvSlot>) {
match self.config.cache_backend {
CacheBackend::Radix => {
let (hit, slots) = self.radix.match_path(prompt);
self.radix.record_matched(hit);
(hit, slots)
}
CacheBackend::Apc => {
let bs = self.kv.block_size();
let (hit, blocks) = self.apc.match_blocks(prompt, bs);
let slots: Vec<KvSlot> = blocks
.iter()
.flat_map(|b| (0..bs).map(move |off| (*b, off)))
.collect();
debug_assert_eq!(hit, slots.len());
(hit, slots)
}
}
}
/// Publish a finished token path (with physical slots) into the configured
/// prefix cache, adopting cache-owned references on newly indexed blocks.
fn publish_prefix(&mut self, tokens: &[TokenId], locs: &[KvSlot]) {
match self.config.cache_backend {
CacheBackend::Radix => {
let _ = self.radix.insert_with_kv(tokens, locs, &mut self.kv);
}
CacheBackend::Apc => self.apc.insert_blocks(tokens, locs, &mut self.kv),
}
}
/// Allocate a KV block, evicting LRU prefix-cache entries on pressure when
/// enabled. Eviction repeats until a block is actually freed: evicting one
/// radix leaf does not necessarily release its block (sibling nodes may
/// still reference it), and pinned checkpoint blocks are never freed.
fn alloc_kv_block(&mut self) -> Option<BlockId> {
if let Some(block) = self.kv.alloc() {
return Some(block);
}
if self.config.evict_on_pressure {
loop {
let evicted = match self.config.cache_backend {
CacheBackend::Radix => self.radix.evict_lru(&mut self.kv, 1),
CacheBackend::Apc => self.apc.evict_lru(&mut self.kv, 1),
};
if let Some(block) = self.kv.alloc() {
return Some(block);
}
if evicted == 0 {
// Nothing left to evict: the pool is genuinely exhausted.
return None;
}
}
}
None
}
/// Append one token to a sequence's physical pages, copy-on-writing the
/// tail block when it is shared. Only updates `blocks`/`locs`, not the
/// logical token buffer.
fn append_physical(&mut self, seq: &mut Seq, tok: TokenId) {
match seq.blocks.last().copied() {
Some(tail) if !self.kv.is_full(tail) => {
if self.kv.ref_count(tail) > 1 {
// Copy-on-write: the tail block is shared with the prefix
// cache or a checkpoint pin. Allocate the private copy
// through the engine allocator so eviction policy applies,
// then move this sequence's tail slots onto the copy (the
// prefix content stays identical in the shared block).
// Note the shared block may hold *more* tokens than this
// sequence owns (the cached chain may continue past the
// matched prefix), so only copy the owned head.
let owned = seq.locs.iter().rev().take_while(|s| s.0 == tail).count();
debug_assert!(owned > 0, "tail block must hold >=1 token of this sequence");
let private = self
.alloc_kv_block()
.expect("KV cache exhausted: raise num_kv_blocks");
let copied: Vec<TokenId> = self.kv.tokens(tail)[..owned].to_vec();
for t in &copied {
let _ = self.kv.append(private, *t);
}
self.kv.dec_ref(tail);
*seq.blocks.last_mut().expect("tail block exists") = private;
for slot in seq.locs.iter_mut().rev().take(owned) {
debug_assert_eq!(slot.0, tail);
*slot = (private, slot.1);
}
let _ = self.kv.append(private, tok);
seq.locs.push((private, owned));
} else {
let off = self.kv.len(tail);
let _ = self.kv.append(tail, tok);
seq.locs.push((tail, off));
}
}
_ => {
let nb = self
.alloc_kv_block()
.expect("KV cache exhausted: raise num_kv_blocks");
seq.blocks.push(nb);
let _ = self.kv.append(nb, tok);
seq.locs.push((nb, 0));
}
}
}
/// A model backend returns sane logits when they are exactly vocab-sized
/// and contain no NaN / infinity. Anything else is a request-level fault:
/// the affected sequence is terminated in isolation rather than poisoning
/// the rest of the batch.
fn logits_sane(&self, logits: &[f32]) -> bool {
logits.len() == self.model.vocab_size() && logits.iter().all(|x| x.is_finite())
}
/// Per-sequence decode tail shared by the batched and unbatched paths:
/// validate the logits, apply sampling penalties and grammar masks,
/// sample one token, materialize it into paged KV, and check the finish
/// conditions.
fn advance_with_logits(&mut self, mut seq: Seq, mut logits: Vec<f32>) -> Advance {
if !self.logits_sane(&logits) {
self.total_faults += 1;
return Advance::Done(self.finalize(seq, FinishReason::Fault));
}
apply_penalties(&mut logits, &seq.output, &seq.sampling);
if let Some(grammar) = seq.sampling.grammar.as_ref() {
let partial = self.tokenizer.decode(&seq.output);
let Some(allowed) = grammar.allowed_bytes(&partial) else {
// The partial output can no longer be completed: stop instead
// of sampling a token that violates the constraint.
return Advance::Done(self.finalize(seq, FinishReason::Stop));
};
mask_logits(&allowed, &mut logits);
// Once a valid complete result is available, keep the end-of-stream
// token as a legal continuation so the model can finish here.
if grammar.is_complete(&partial) {
let eos = self.tokenizer.eos_token_id() as usize;
if eos < logits.len() && !logits[eos].is_finite() {
logits[eos] = 0.0;
}
}
}
let tok = self.sampler.sample(&logits, &seq.sampling);
self.append_physical(&mut seq, tok);
seq.tokens.push(tok);
seq.output.push(tok);
if let Some(reason) = self.finish_reason(&seq) {
return Advance::Done(self.finalize(seq, reason));
}
Advance::Running(seq)
}
fn finish_reason(&self, seq: &Seq) -> Option<FinishReason> {
if let Some(&last) = seq.output.last() {
if last == self.tokenizer.eos_token_id()
|| seq.sampling.stop_token_ids.contains(&last)
{
return Some(FinishReason::Stop);
}
}
if !seq.sampling.stop.is_empty() {
let text = self.tokenizer.decode(&seq.output);
if seq
.sampling
.stop
.iter()
.any(|s| !s.is_empty() && text.ends_with(s))
{
return Some(FinishReason::Stop);
}
}
None
}
fn finalize(&mut self, seq: Seq, reason: FinishReason) -> GenerationOutput {
// Hand the finished session's KV to the backend's vault for
// tensor-level prefix grafts by later requests — unless the sequence
// faulted (its KV may be corrupt). Nothing reads `seq.session` after.
if reason != FinishReason::Fault {
if let Some(session) = seq.session {
self.model
.offer_session_kv(&seq.tokens, seq.prompt_len, session);
}
}
// Index the full token path into the prefix cache, adopting new blocks
// with a cache base reference, then release this request's reference
// on every block it holds.
self.publish_prefix(&seq.tokens, &seq.locs);
for &b in &seq.blocks {
self.kv.dec_ref(b);
}
let text = self.tokenizer.decode(&seq.output);
let full = self.tokenizer.decode(&seq.tokens);
let out_len = seq.output.len();
self.total_output_tokens += out_len as u64;
GenerationOutput {
text,
full_text: Some(full),
output_token_ids: seq.output,
finish_reason: reason,
prompt_tokens: seq.prompt_len,
prefix_hit_tokens: seq.prefix_hit,
forward_count: seq.prefill_cost + out_len,
rejection: None,
}
}
}
impl Engine<NGramModel, ByteTokenizer> {
/// Build the battery-included engine (byte tokenizer + n-gram toy model).
pub fn toy(config: EngineConfig) -> Self {
Self::new(ByteTokenizer::new(), NGramModel::default(), config)
}
/// Build the toy engine with a custom n-gram order and training corpus.
pub fn toy_with_corpus(config: EngineConfig, order: usize, corpus: &str) -> Self {
Self::new(ByteTokenizer::new(), NGramModel::new(order, corpus), config)
}
}
fn apply_penalties(logits: &mut [f32], output: &[TokenId], params: &SamplingParams) {
if params.frequency_penalty.abs() < 1e-6 && params.presence_penalty.abs() < 1e-6 {
return;
}
let mut counts: HashMap<TokenId, usize> = HashMap::new();
for &t in output {
*counts.entry(t).or_insert(0) += 1;
}
for (&t, &c) in counts.iter() {
if (t as usize) < logits.len() {
logits[t as usize] -= params.frequency_penalty * (c as f32);
}
}
if params.presence_penalty.abs() > 1e-6 {
for &t in counts.keys() {
if (t as usize) < logits.len() {
logits[t as usize] -= params.presence_penalty;
}
}
}
}
/// Pop the next sequence to prefill according to the configured policy.
fn pop_next(waiting: &mut VecDeque<Seq>, policy: SchedulePolicy) -> Option<Seq> {
if waiting.is_empty() {
return None;
}
match policy {
SchedulePolicy::Fcfs => waiting.pop_front(),
SchedulePolicy::LongestPrefix => {
let mut best = 0usize;
let mut best_score = 0usize;
for (i, s) in waiting.iter().enumerate() {
if s.prefix_hit > best_score {
best_score = s.prefix_hit;
best = i;
}
}
waiting.remove(best)
}
SchedulePolicy::ShortestPrompt => {
let mut best = 0usize;
let mut best_score = usize::MAX;
for (i, s) in waiting.iter().enumerate() {
let remaining = s.prompt.len().saturating_sub(s.prefill_pos);
if remaining < best_score {
best_score = remaining;
best = i;
}
}
waiting.remove(best)
}
}
}
/// Aggregated runtime metrics.
#[derive(Clone, Copy, Debug)]
pub struct EngineStats {
pub total_requests: u64,
pub total_forward: u64,
/// Logical per-sequence decode advances (one per token per sequence).
pub total_decode_steps: u64,
/// Physical model invocations during the decode phase. A batched step
/// counts once no matter how many sequences it advanced.
pub total_decode_calls: u64,
/// Prompt tokens never fed to the model because the backend grafted a
/// cached KV prefix (tensor-level RadixAttention). Logical prefix hits
/// (radix/APC) are counted separately in `radix_hit_tokens` /
/// `apc_hit_tokens`; this is the physical recompute that disappeared.
pub model_graft_tokens: u64,
pub radix_nodes: usize,
pub radix_queries: u64,
pub radix_hit_queries: u64,
pub radix_hit_tokens: u64,
/// Blocks currently indexed by the APC hash table (APC backend only).
pub apc_blocks: usize,
/// Tokens served from the APC cache (APC backend only).
pub apc_hit_tokens: u64,
pub kv_blocks: usize,
pub kv_free_blocks: usize,
pub kv_allocations: usize,
pub kv_frees: usize,
/// Number of distinct prefill chunk materializations.
pub prefill_chunks: u64,
/// Prompt tokens submitted (sum of request prompt lengths).
pub total_prompt_tokens: u64,
/// Prompt tokens actually materialized into KV (prefill work performed).
pub total_prefill_tokens: u64,
/// Newly generated tokens.
pub total_output_tokens: u64,
/// Requests aborted because the model returned invalid logits.
pub faulted_requests: u64,
/// Requests refused during admission (queue full / too long / empty prompt).
pub rejected_requests: u64,
/// Currently pinned KV checkpoints.
pub active_checkpoints: usize,
/// Prompt tokens served from pinned checkpoints (guaranteed-hit branches).
pub checkpoint_hit_tokens: u64,
}
impl EngineStats {
/// Compute-saved tokens: prompt tokens served from the prefix cache (radix
/// or APC, whichever backend is active) or from pinned checkpoints, rather
/// than re-materialized. The revenue head-line metric for "省算力".
pub fn compute_saved_tokens(&self) -> u64 {
self.radix_hit_tokens + self.apc_hit_tokens + self.checkpoint_hit_tokens
}
/// Fraction of prefill work that the prefix cache skipped.
pub fn prefill_skip_ratio(&self) -> f64 {
let saved = self.compute_saved_tokens();
let denom = saved + self.total_prefill_tokens;
if denom == 0 {
0.0
} else {
saved as f64 / denom as f64
}
}
/// Forward-pass cost per generated token (a token/watt proxy: lower is
/// better).
pub fn avg_forward_per_output_token(&self) -> f64 {
if self.total_output_tokens == 0 {
0.0
} else {
self.total_forward as f64 / self.total_output_tokens as f64
}
}
/// Logical decode steps fused per physical model call. 1.0 means every
/// sequence ran its own forward; 8.0 means eight sequences shared one
/// batched forward on average — the continuous-batching win.
pub fn decode_batch_factor(&self) -> f64 {
if self.total_decode_calls == 0 {
0.0
} else {
self.total_decode_steps as f64 / self.total_decode_calls as f64
}
}
/// Fraction of the KV pool currently resident.
pub fn kv_utilization(&self) -> f64 {
if self.kv_blocks == 0 {
0.0
} else {
(self.kv_blocks - self.kv_free_blocks) as f64 / self.kv_blocks as f64
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn engine() -> ToyEngine {
ToyEngine::toy(EngineConfig {
num_kv_blocks: 64,
block_size: 8,
..EngineConfig::default()
})
}
fn params(max_tokens: usize) -> SamplingParams {
SamplingParams {
max_tokens,
..SamplingParams::default()
}
}
#[test]
fn branch_appends_never_touch_checkpoint_blocks() {
let mut e = engine();
// 31 bytes: 3 full blocks + a partial tail block of 7 tokens.
let cp = e.create_checkpoint("checkpoint with a partial tail");
let tail = e.checkpoints[&cp.0].blocks.last().copied().expect("tail");
let tail_len = e.kv.len(tail);
assert!(tail_len < e.kv.block_size());
let out = e
.generate_from_checkpoint(cp, " + branch A continuation", params(8))
.expect("branch");
assert!(matches!(
out.finish_reason,
FinishReason::Stop | FinishReason::Length
));
assert_eq!(
e.kv.len(tail),
tail_len,
"copy-on-write must keep the checkpoint's tail block immutable"
);
}
#[test]
fn completed_branch_leaves_no_dangling_references() {
let mut e = engine();
let cp = e.create_checkpoint("shared trunk for reference accounting");
let blocks = e.checkpoints[&cp.0].blocks.clone();
let before: Vec<usize> = blocks.iter().map(|&b| e.kv.ref_count(b)).collect();
let out = e
.generate_from_checkpoint(cp, "branch once", params(6))
.expect("branch");
assert!(matches!(
out.finish_reason,
FinishReason::Stop | FinishReason::Length
));
let after: Vec<usize> = blocks.iter().map(|&b| e.kv.ref_count(b)).collect();
assert_eq!(
before, after,
"a finished branch must release exactly the references it took"
);
}
#[test]
fn frequency_penalty_scales_with_occurrence_count() {
let mut logits = vec![0.0f32; 8];
logits[3] = 5.0;
let output = vec![3u32, 7, 3]; // token 3 seen twice, 7 once
let params = SamplingParams {
frequency_penalty: 1.0,
..SamplingParams::default()
};
apply_penalties(&mut logits, &output, ¶ms);
assert_eq!(logits[3], 5.0 - 2.0, "penalty must scale with count");
assert_eq!(logits[7], -1.0, "seen once: one penalty unit");
assert_eq!(logits[4], 0.0, "unseen token untouched");
}
#[test]
fn presence_penalty_applies_once_per_token() {
let mut logits = vec![0.0f32; 8];
logits[3] = 5.0;
logits[7] = 4.0;
let output = vec![3u32, 3, 7];
let params = SamplingParams {
presence_penalty: 1.0,
..SamplingParams::default()
};
apply_penalties(&mut logits, &output, ¶ms);
assert_eq!(logits[3], 4.0, "once per token, not per occurrence");
assert_eq!(logits[7], 3.0);
assert_eq!(logits[4], 0.0);
}
}