lling-llang 0.1.0

WFST framework for text normalization and grammar correction
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
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
//! Soft pruning for efficient GPU token management.
//!
//! This module provides soft pruning abstractions that avoid expensive memory
//! deallocation on GPU by marking tokens as pruned rather than removing them.
//!
//! ## Problem
//!
//! During beam search decoding, many tokens are pruned. On GPU, memory
//! deallocation is expensive and causes fragmentation. Additionally, removing
//! elements from vectors requires shifting all subsequent elements.
//!
//! ## Solution: Soft Pruning
//!
//! Instead of removing pruned tokens:
//! 1. Mark them as "soft-pruned" by zeroing their out-arc degree
//! 2. Load balancer safely ignores zero-degree tokens
//! 3. Periodically compact remaining tokens (batch operation)
//!
//! ```text
//! procedure SOFT_PRUNE(token, threshold):
//!     if token.cost > threshold:
//!         token.out_degree = 0  // Mark as pruned
//!     // Token remains in memory but is ignored
//!
//! procedure COMPACT(tokens):
//!     surviving = []
//!     for token in tokens:
//!         if token.out_degree > 0:
//!             surviving.push(token)
//!     tokens = surviving
//! ```
//!
//! ## Benefits
//!
//! - **No deallocation**: Avoids expensive GPU memory operations
//! - **No shifting**: Elements stay in place until compaction
//! - **Batch compaction**: Amortizes compaction cost over many prunes
//! - **Load balancer compatible**: Zero-degree tokens automatically skipped
//!
//! ## References
//!
//! - Braun et al., "GPU-Accelerated Viterbi Exact Lattice Decoder for Batched Online and Offline
//!   Speech Recognition" (ICASSP 2020, arXiv:1910.10032)
//! - Chen et al., "A GPU-based WFST Decoder with Exact Lattice Generation" (Interspeech 2018)

use std::sync::atomic::{AtomicBool, AtomicU32, AtomicUsize, Ordering};

/// A soft-prunable token that can be marked as pruned without deallocation.
#[derive(Debug)]
pub struct SoftToken<T> {
    /// The token data.
    data: T,
    /// Number of outgoing arcs (0 = pruned).
    out_degree: AtomicU32,
    /// Whether this token is active (not pruned).
    active: AtomicBool,
    /// Frame index when this token was created.
    frame: u32,
    /// Cost of this token.
    cost: f32,
}

impl<T> SoftToken<T> {
    /// Create a new soft token.
    pub fn new(data: T, out_degree: u32, frame: u32, cost: f32) -> Self {
        Self {
            data,
            out_degree: AtomicU32::new(out_degree),
            active: AtomicBool::new(true),
            frame,
            cost,
        }
    }

    /// Get the token data.
    pub fn data(&self) -> &T {
        &self.data
    }

    /// Get mutable access to token data.
    pub fn data_mut(&mut self) -> &mut T {
        &mut self.data
    }

    /// Get the out-degree.
    pub fn out_degree(&self) -> u32 {
        self.out_degree.load(Ordering::Acquire)
    }

    /// Set the out-degree.
    pub fn set_out_degree(&self, degree: u32) {
        self.out_degree.store(degree, Ordering::Release);
    }

    /// Check if the token is active (not pruned).
    pub fn is_active(&self) -> bool {
        self.active.load(Ordering::Acquire)
    }

    /// Check if the token is pruned.
    pub fn is_pruned(&self) -> bool {
        !self.is_active() || self.out_degree() == 0
    }

    /// Get the frame index.
    pub fn frame(&self) -> u32 {
        self.frame
    }

    /// Get the cost.
    pub fn cost(&self) -> f32 {
        self.cost
    }

    /// Soft-prune this token by zeroing out-degree.
    pub fn soft_prune(&self) {
        self.out_degree.store(0, Ordering::Release);
        self.active.store(false, Ordering::Release);
    }

    /// Check if this token should be pruned based on threshold.
    pub fn should_prune(&self, threshold: f32) -> bool {
        self.cost > threshold
    }

    /// Soft-prune if cost exceeds threshold.
    ///
    /// Returns `true` if the token was pruned.
    pub fn prune_if_above(&self, threshold: f32) -> bool {
        if self.should_prune(threshold) {
            self.soft_prune();
            true
        } else {
            false
        }
    }
}

impl<T: Clone> Clone for SoftToken<T> {
    fn clone(&self) -> Self {
        Self {
            data: self.data.clone(),
            out_degree: AtomicU32::new(self.out_degree.load(Ordering::Relaxed)),
            active: AtomicBool::new(self.active.load(Ordering::Relaxed)),
            frame: self.frame,
            cost: self.cost,
        }
    }
}

/// Configuration for soft pruning.
#[derive(Clone, Copy, Debug)]
pub struct SoftPruneConfig {
    /// Beam width (cost threshold relative to best).
    pub beam: f32,
    /// Maximum number of active tokens.
    pub max_active: usize,
    /// Compaction threshold (compact when this fraction is pruned).
    pub compact_threshold: f32,
    /// Minimum tokens before considering compaction.
    pub min_tokens_for_compact: usize,
}

impl SoftPruneConfig {
    /// Create a new configuration.
    pub fn new(beam: f32, max_active: usize) -> Self {
        Self {
            beam,
            max_active,
            compact_threshold: 0.5,
            min_tokens_for_compact: 1000,
        }
    }

    /// Create with custom compaction settings.
    pub fn with_compaction(
        beam: f32,
        max_active: usize,
        compact_threshold: f32,
        min_tokens_for_compact: usize,
    ) -> Self {
        Self {
            beam,
            max_active,
            compact_threshold,
            min_tokens_for_compact,
        }
    }
}

impl Default for SoftPruneConfig {
    fn default() -> Self {
        Self::new(16.0, 10000)
    }
}

/// Buffer for soft-prunable tokens with automatic compaction.
#[derive(Debug)]
pub struct SoftPruneBuffer<T> {
    /// Tokens in the buffer.
    tokens: Vec<SoftToken<T>>,
    /// Number of active (non-pruned) tokens.
    active_count: AtomicUsize,
    /// Configuration.
    config: SoftPruneConfig,
    /// Current best cost (for beam pruning).
    best_cost: f32,
}

impl<T> SoftPruneBuffer<T> {
    /// Create a new soft prune buffer.
    pub fn new(config: SoftPruneConfig) -> Self {
        Self {
            tokens: Vec::new(),
            active_count: AtomicUsize::new(0),
            config,
            best_cost: f32::INFINITY,
        }
    }

    /// Create with initial capacity.
    pub fn with_capacity(config: SoftPruneConfig, capacity: usize) -> Self {
        Self {
            tokens: Vec::with_capacity(capacity),
            active_count: AtomicUsize::new(0),
            config,
            best_cost: f32::INFINITY,
        }
    }

    /// Get the configuration.
    pub fn config(&self) -> &SoftPruneConfig {
        &self.config
    }

    /// Get the number of active tokens.
    pub fn active_count(&self) -> usize {
        self.active_count.load(Ordering::Acquire)
    }

    /// Get the total number of tokens (including pruned).
    pub fn total_count(&self) -> usize {
        self.tokens.len()
    }

    /// Get the pruned token count.
    ///
    /// Note: This counts pruned tokens by scanning, not from cached atomic count,
    /// to ensure accuracy when tokens are pruned directly via `SoftToken::soft_prune()`.
    pub fn pruned_count(&self) -> usize {
        self.tokens.iter().filter(|t| t.is_pruned()).count()
    }

    /// Get the actual active count by scanning tokens.
    ///
    /// More accurate than `active_count()` when tokens are pruned directly.
    pub fn actual_active_count(&self) -> usize {
        self.tokens.iter().filter(|t| t.is_active()).count()
    }

    /// Get the current best cost.
    pub fn best_cost(&self) -> f32 {
        self.best_cost
    }

    /// Get the current beam threshold.
    pub fn threshold(&self) -> f32 {
        self.best_cost + self.config.beam
    }

    /// Check if compaction is needed.
    pub fn needs_compaction(&self) -> bool {
        let total = self.total_count();
        if total < self.config.min_tokens_for_compact {
            return false;
        }

        let pruned_ratio = self.pruned_count() as f32 / total as f32;
        pruned_ratio >= self.config.compact_threshold
    }

    /// Push a new token to the buffer.
    ///
    /// Returns the token index, or `None` if pruned immediately.
    pub fn push(&mut self, token: SoftToken<T>) -> Option<usize> {
        // Update best cost
        if token.cost() < self.best_cost {
            self.best_cost = token.cost();
        }

        // Check if should be pruned immediately
        if token.should_prune(self.threshold()) {
            return None;
        }

        let index = self.tokens.len();
        self.tokens.push(token);
        self.active_count.fetch_add(1, Ordering::AcqRel);
        Some(index)
    }

    /// Get a token by index.
    pub fn get(&self, index: usize) -> Option<&SoftToken<T>> {
        self.tokens.get(index)
    }

    /// Iterate over active tokens.
    pub fn active_tokens(&self) -> impl Iterator<Item = (usize, &SoftToken<T>)> {
        self.tokens
            .iter()
            .enumerate()
            .filter(|(_, t)| t.is_active())
    }

    /// Apply beam pruning to all tokens.
    ///
    /// Returns the number of tokens pruned.
    pub fn apply_beam_pruning(&self) -> usize {
        let threshold = self.threshold();
        let mut pruned = 0;

        for token in &self.tokens {
            if token.is_active() && token.prune_if_above(threshold) {
                self.active_count.fetch_sub(1, Ordering::AcqRel);
                pruned += 1;
            }
        }

        pruned
    }

    /// Update best cost and apply beam pruning.
    pub fn update_best_and_prune(&mut self, new_best: f32) -> usize {
        if new_best < self.best_cost {
            self.best_cost = new_best;
        }
        self.apply_beam_pruning()
    }

    /// Clear all tokens.
    pub fn clear(&mut self) {
        self.tokens.clear();
        self.active_count.store(0, Ordering::Release);
        self.best_cost = f32::INFINITY;
    }

    /// Reset for a new frame.
    pub fn reset_for_frame(&mut self) {
        self.clear();
    }
}

impl<T: Clone> SoftPruneBuffer<T> {
    /// Compact the buffer, removing pruned tokens.
    ///
    /// Returns the number of tokens removed.
    pub fn compact(&mut self) -> usize {
        let original_len = self.tokens.len();

        // Retain only active tokens
        self.tokens.retain(|t| t.is_active());

        let removed = original_len - self.tokens.len();

        // Update active count to match actual count
        self.active_count
            .store(self.tokens.len(), Ordering::Release);

        removed
    }

    /// Compact if needed based on configuration.
    ///
    /// Returns the number of tokens removed, or 0 if no compaction occurred.
    pub fn compact_if_needed(&mut self) -> usize {
        if self.needs_compaction() {
            self.compact()
        } else {
            0
        }
    }

    /// Extract surviving tokens, consuming the buffer.
    pub fn into_survivors(self) -> Vec<T> {
        self.tokens
            .into_iter()
            .filter(|t| t.is_active())
            .map(|t| t.data)
            .collect()
    }
}

/// Statistics about soft pruning operations.
#[derive(Clone, Debug, Default)]
pub struct SoftPruneStats {
    /// Total tokens processed.
    pub total_tokens: usize,
    /// Tokens pruned by beam.
    pub beam_pruned: usize,
    /// Tokens pruned by max-active limit.
    pub limit_pruned: usize,
    /// Compaction operations performed.
    pub compactions: usize,
    /// Tokens removed by compaction.
    pub compacted_tokens: usize,
}

impl SoftPruneStats {
    /// Create new stats.
    pub fn new() -> Self {
        Self::default()
    }

    /// Get the total pruned count.
    pub fn total_pruned(&self) -> usize {
        self.beam_pruned + self.limit_pruned
    }

    /// Get the prune ratio.
    pub fn prune_ratio(&self) -> f64 {
        if self.total_tokens == 0 {
            0.0
        } else {
            self.total_pruned() as f64 / self.total_tokens as f64
        }
    }

    /// Get the compaction efficiency.
    pub fn compaction_efficiency(&self) -> f64 {
        if self.compactions == 0 {
            0.0
        } else {
            self.compacted_tokens as f64 / self.compactions as f64
        }
    }

    /// Record a beam prune.
    pub fn record_beam_prune(&mut self, count: usize) {
        self.beam_pruned += count;
    }

    /// Record a limit prune.
    pub fn record_limit_prune(&mut self, count: usize) {
        self.limit_pruned += count;
    }

    /// Record a compaction.
    pub fn record_compaction(&mut self, tokens_removed: usize) {
        self.compactions += 1;
        self.compacted_tokens += tokens_removed;
    }

    /// Record tokens processed.
    pub fn record_tokens(&mut self, count: usize) {
        self.total_tokens += count;
    }

    /// Merge stats from another instance.
    pub fn merge(&mut self, other: &SoftPruneStats) {
        self.total_tokens += other.total_tokens;
        self.beam_pruned += other.beam_pruned;
        self.limit_pruned += other.limit_pruned;
        self.compactions += other.compactions;
        self.compacted_tokens += other.compacted_tokens;
    }
}

/// Histogram-based adaptive beam for soft pruning.
///
/// Uses a histogram to quickly find the beam threshold that keeps
/// approximately `max_active` tokens.
#[derive(Debug)]
pub struct AdaptiveBeam {
    /// Number of histogram buckets.
    num_buckets: usize,
    /// Bucket counts.
    buckets: Vec<AtomicUsize>,
    /// Minimum cost seen.
    min_cost: f32,
    /// Maximum cost seen.
    max_cost: f32,
    /// Target number of active tokens.
    target_active: usize,
}

impl AdaptiveBeam {
    /// Create a new adaptive beam.
    pub fn new(num_buckets: usize, target_active: usize) -> Self {
        Self {
            num_buckets,
            buckets: (0..num_buckets).map(|_| AtomicUsize::new(0)).collect(),
            min_cost: f32::INFINITY,
            max_cost: f32::NEG_INFINITY,
            target_active,
        }
    }

    /// Reset the histogram.
    pub fn reset(&mut self) {
        for bucket in &self.buckets {
            bucket.store(0, Ordering::Relaxed);
        }
        self.min_cost = f32::INFINITY;
        self.max_cost = f32::NEG_INFINITY;
    }

    /// Add a cost to the histogram.
    pub fn add(&mut self, cost: f32) {
        if cost < self.min_cost {
            self.min_cost = cost;
        }
        if cost > self.max_cost {
            self.max_cost = cost;
        }

        // We'll compute the bucket index after all costs are added
        // For now, just track min/max
    }

    /// Set the cost range for threshold computation.
    pub fn set_range(&mut self, min_cost: f32, max_cost: f32) {
        self.min_cost = min_cost;
        self.max_cost = max_cost;
    }

    /// Add a cost with known range.
    pub fn add_with_range(&self, cost: f32, range_min: f32, range_max: f32) {
        if range_max <= range_min {
            return;
        }

        let normalized = (cost - range_min) / (range_max - range_min);
        let bucket = ((normalized * self.num_buckets as f32) as usize).min(self.num_buckets - 1);
        self.buckets[bucket].fetch_add(1, Ordering::Relaxed);
    }

    /// Compute the adaptive threshold.
    ///
    /// Returns the cost threshold that keeps approximately `target_active` tokens.
    pub fn compute_threshold(&self) -> f32 {
        if self.max_cost <= self.min_cost {
            return f32::INFINITY;
        }

        let mut cumulative = 0;
        let bucket_width = (self.max_cost - self.min_cost) / self.num_buckets as f32;

        for (i, bucket) in self.buckets.iter().enumerate() {
            cumulative += bucket.load(Ordering::Relaxed);
            if cumulative >= self.target_active {
                // Return the upper bound of this bucket
                return self.min_cost + (i + 1) as f32 * bucket_width;
            }
        }

        // All tokens fit within target
        f32::INFINITY
    }

    /// Get the total count in the histogram.
    pub fn total_count(&self) -> usize {
        self.buckets.iter().map(|b| b.load(Ordering::Relaxed)).sum()
    }
}

/// Manager for soft pruning across multiple frames.
#[derive(Debug)]
pub struct SoftPruneManager<T> {
    /// Current frame buffer.
    current: SoftPruneBuffer<T>,
    /// Previous frame buffer (for swapping).
    previous: SoftPruneBuffer<T>,
    /// Current frame index.
    frame: u32,
    /// Adaptive beam for histogram pruning.
    adaptive_beam: AdaptiveBeam,
    /// Statistics.
    stats: SoftPruneStats,
}

impl<T> SoftPruneManager<T> {
    /// Create a new soft prune manager.
    pub fn new(config: SoftPruneConfig) -> Self {
        Self {
            current: SoftPruneBuffer::with_capacity(config, config.max_active),
            previous: SoftPruneBuffer::with_capacity(config, config.max_active),
            frame: 0,
            adaptive_beam: AdaptiveBeam::new(100, config.max_active),
            stats: SoftPruneStats::new(),
        }
    }

    /// Get the current frame.
    pub fn frame(&self) -> u32 {
        self.frame
    }

    /// Get the current buffer.
    pub fn current(&self) -> &SoftPruneBuffer<T> {
        &self.current
    }

    /// Get mutable access to the current buffer.
    pub fn current_mut(&mut self) -> &mut SoftPruneBuffer<T> {
        &mut self.current
    }

    /// Get the previous buffer.
    pub fn previous(&self) -> &SoftPruneBuffer<T> {
        &self.previous
    }

    /// Get statistics.
    pub fn stats(&self) -> &SoftPruneStats {
        &self.stats
    }

    /// Add a token to the current frame.
    pub fn add_token(&mut self, data: T, out_degree: u32, cost: f32) -> Option<usize> {
        let token = SoftToken::new(data, out_degree, self.frame, cost);
        self.stats.record_tokens(1);
        self.current.push(token)
    }

    /// Apply beam pruning to current frame.
    pub fn apply_pruning(&mut self) -> usize {
        let pruned = self.current.apply_beam_pruning();
        self.stats.record_beam_prune(pruned);
        pruned
    }
}

impl<T: Clone> SoftPruneManager<T> {
    /// Advance to the next frame.
    pub fn advance_frame(&mut self) {
        // Compact current if needed
        let compacted = self.current.compact_if_needed();
        if compacted > 0 {
            self.stats.record_compaction(compacted);
        }

        // Swap buffers
        std::mem::swap(&mut self.current, &mut self.previous);

        // Reset current for new frame
        self.current.reset_for_frame();
        self.adaptive_beam.reset();
        self.frame += 1;
    }

    /// Get surviving tokens from the current frame.
    pub fn survivors(&self) -> Vec<T> {
        self.current
            .tokens
            .iter()
            .filter(|t| t.is_active())
            .map(|t| t.data.clone())
            .collect()
    }
}

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

    #[test]
    fn test_soft_token_creation() {
        let token = SoftToken::new(42, 5, 0, 1.5);

        assert_eq!(*token.data(), 42);
        assert_eq!(token.out_degree(), 5);
        assert_eq!(token.frame(), 0);
        assert!((token.cost() - 1.5).abs() < 1e-6);
        assert!(token.is_active());
        assert!(!token.is_pruned());
    }

    #[test]
    fn test_soft_token_pruning() {
        let token = SoftToken::new(42, 5, 0, 1.5);

        assert!(token.is_active());
        token.soft_prune();
        assert!(!token.is_active());
        assert!(token.is_pruned());
        assert_eq!(token.out_degree(), 0);
    }

    #[test]
    fn test_soft_token_threshold_pruning() {
        let token = SoftToken::new(42, 5, 0, 10.0);

        assert!(!token.prune_if_above(15.0)); // Cost 10 < threshold 15
        assert!(token.is_active());

        assert!(token.prune_if_above(5.0)); // Cost 10 > threshold 5
        assert!(!token.is_active());
    }

    #[test]
    fn test_soft_prune_config() {
        let config = SoftPruneConfig::new(10.0, 5000);

        assert!((config.beam - 10.0).abs() < 1e-6);
        assert_eq!(config.max_active, 5000);
    }

    #[test]
    fn test_soft_prune_buffer() {
        let config = SoftPruneConfig::new(10.0, 100);
        let mut buffer = SoftPruneBuffer::new(config);

        // Add tokens
        let idx1 = buffer.push(SoftToken::new(1, 3, 0, 1.0));
        let idx2 = buffer.push(SoftToken::new(2, 2, 0, 2.0));
        let idx3 = buffer.push(SoftToken::new(3, 1, 0, 15.0)); // Should be pruned (cost > beam)

        assert!(idx1.is_some());
        assert!(idx2.is_some());
        assert!(idx3.is_none()); // Pruned immediately

        assert_eq!(buffer.active_count(), 2);
        assert_eq!(buffer.total_count(), 2);
    }

    #[test]
    fn test_soft_prune_buffer_beam_pruning() {
        let config = SoftPruneConfig::new(5.0, 100);
        let mut buffer = SoftPruneBuffer::new(config);

        buffer.push(SoftToken::new(1, 3, 0, 1.0));
        buffer.push(SoftToken::new(2, 2, 0, 3.0));
        buffer.push(SoftToken::new(3, 1, 0, 4.0));

        assert_eq!(buffer.active_count(), 3);

        // Update best cost and prune
        let pruned = buffer.update_best_and_prune(0.5);
        // New threshold = 0.5 + 5.0 = 5.5, so token with cost 1.0, 3.0, 4.0 should survive
        assert_eq!(pruned, 0);
        assert_eq!(buffer.active_count(), 3);
    }

    #[test]
    fn test_soft_prune_buffer_compact() {
        let config = SoftPruneConfig::new(10.0, 100);
        let mut buffer = SoftPruneBuffer::new(config);

        buffer.push(SoftToken::new(1, 3, 0, 1.0));
        buffer.push(SoftToken::new(2, 2, 0, 2.0));
        buffer.push(SoftToken::new(3, 1, 0, 3.0));

        // Manually prune one token
        buffer
            .get(1)
            .expect("gpu/soft_prune.rs: required value was None/Err")
            .soft_prune();

        assert_eq!(buffer.total_count(), 3);
        let removed = buffer.compact();
        assert_eq!(removed, 1);
        assert_eq!(buffer.total_count(), 2);
    }

    #[test]
    fn test_soft_prune_stats() {
        let mut stats = SoftPruneStats::new();

        stats.record_tokens(100);
        stats.record_beam_prune(20);
        stats.record_limit_prune(10);
        stats.record_compaction(15);

        assert_eq!(stats.total_tokens, 100);
        assert_eq!(stats.total_pruned(), 30);
        assert!((stats.prune_ratio() - 0.3).abs() < 1e-6);
        assert_eq!(stats.compactions, 1);
        assert_eq!(stats.compacted_tokens, 15);
    }

    #[test]
    fn test_adaptive_beam() {
        let mut beam = AdaptiveBeam::new(10, 50);

        // Set the range before computing threshold
        beam.set_range(0.0, 100.0);

        // Add costs with known range
        for i in 0..100 {
            beam.add_with_range(i as f32, 0.0, 100.0);
        }

        let threshold = beam.compute_threshold();
        // Should be around 50 to keep ~50 tokens
        assert!(threshold > 40.0 && threshold < 60.0);
    }

    #[test]
    fn test_soft_prune_manager() {
        let config = SoftPruneConfig::new(10.0, 100);
        let mut manager = SoftPruneManager::new(config);

        // Add tokens
        manager.add_token(1, 3, 1.0);
        manager.add_token(2, 2, 2.0);
        manager.add_token(3, 1, 3.0);

        assert_eq!(manager.current().active_count(), 3);
        assert_eq!(manager.frame(), 0);

        // Advance frame
        manager.advance_frame();
        assert_eq!(manager.frame(), 1);
        assert_eq!(manager.current().active_count(), 0);
        assert_eq!(manager.previous().active_count(), 3);
    }

    #[test]
    fn test_soft_prune_manager_survivors() {
        let config = SoftPruneConfig::new(10.0, 100);
        let mut manager = SoftPruneManager::new(config);

        manager.add_token(1, 3, 1.0);
        manager.add_token(2, 2, 2.0);

        // Prune one
        manager
            .current()
            .get(0)
            .expect("gpu/soft_prune.rs: required value was None/Err")
            .soft_prune();

        let survivors = manager.survivors();
        assert_eq!(survivors.len(), 1);
        assert_eq!(survivors[0], 2);
    }

    #[test]
    fn test_needs_compaction() {
        let config = SoftPruneConfig::with_compaction(10.0, 100, 0.5, 4);
        let mut buffer = SoftPruneBuffer::new(config);

        // Add 10 tokens
        for i in 0..10 {
            buffer.push(SoftToken::new(i, 3, 0, i as f32));
        }

        assert!(!buffer.needs_compaction()); // 0% pruned

        // Prune 6 tokens (60%)
        for i in 0..6 {
            buffer
                .get(i)
                .expect("gpu/soft_prune.rs: required value was None/Err")
                .soft_prune();
        }

        assert!(buffer.needs_compaction()); // >50% pruned
    }
}

// =============================================================================
// Property-Based Tests
// =============================================================================

#[cfg(test)]
mod property_tests {
    use super::*;
    use proptest::prelude::*;

    // =========================================================================
    // SoftToken Properties
    // =========================================================================

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(100))]

        /// Newly created SoftToken is always active and not pruned.
        #[test]
        fn soft_token_new_is_active(
            data in any::<i32>(),
            out_degree in 1u32..100,
            frame in 0u32..1000,
            cost in -1000.0f32..1000.0
        ) {
            let token = SoftToken::new(data, out_degree, frame, cost);
            prop_assert!(token.is_active());
            prop_assert!(!token.is_pruned());
            prop_assert_eq!(*token.data(), data);
            prop_assert_eq!(token.out_degree(), out_degree);
            prop_assert_eq!(token.frame(), frame);
            prop_assert!((token.cost() - cost).abs() < 1e-6);
        }

        /// soft_prune always makes token inactive and pruned.
        #[test]
        fn soft_prune_makes_inactive(
            data in any::<i32>(),
            out_degree in 1u32..100,
            frame in 0u32..1000,
            cost in -1000.0f32..1000.0
        ) {
            let token = SoftToken::new(data, out_degree, frame, cost);
            token.soft_prune();
            prop_assert!(!token.is_active());
            prop_assert!(token.is_pruned());
            prop_assert_eq!(token.out_degree(), 0);
        }

        /// prune_if_above prunes only when cost > threshold.
        #[test]
        fn prune_if_above_correct(
            cost in 0.0f32..100.0,
            threshold in 0.0f32..100.0
        ) {
            let token = SoftToken::new(42, 5, 0, cost);
            let was_pruned = token.prune_if_above(threshold);

            if cost > threshold {
                prop_assert!(was_pruned);
                prop_assert!(!token.is_active());
            } else {
                prop_assert!(!was_pruned);
                prop_assert!(token.is_active());
            }
        }

        /// Clone preserves all SoftToken fields.
        #[test]
        fn soft_token_clone_preserves_fields(
            data in any::<i32>(),
            out_degree in 0u32..100,
            frame in 0u32..1000,
            cost in -1000.0f32..1000.0
        ) {
            let token = SoftToken::new(data, out_degree, frame, cost);
            let cloned = token.clone();

            prop_assert_eq!(*cloned.data(), data);
            prop_assert_eq!(cloned.out_degree(), out_degree);
            prop_assert_eq!(cloned.frame(), frame);
            prop_assert!((cloned.cost() - cost).abs() < 1e-6);
            prop_assert_eq!(cloned.is_active(), token.is_active());
        }

        /// Token with out_degree=0 is considered pruned.
        #[test]
        fn zero_out_degree_is_pruned(
            data in any::<i32>(),
            frame in 0u32..1000,
            cost in -1000.0f32..1000.0
        ) {
            let token = SoftToken::new(data, 0, frame, cost);
            prop_assert!(token.is_pruned());
        }
    }

    // =========================================================================
    // SoftPruneConfig Properties
    // =========================================================================

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(50))]

        /// SoftPruneConfig preserves all settings.
        #[test]
        fn config_preserves_settings(
            beam in 0.1f32..100.0,
            max_active in 1usize..10000
        ) {
            let config = SoftPruneConfig::new(beam, max_active);
            prop_assert!((config.beam - beam).abs() < 1e-6);
            prop_assert_eq!(config.max_active, max_active);
        }

        /// Custom compaction settings are preserved.
        #[test]
        fn config_custom_compaction(
            beam in 0.1f32..100.0,
            max_active in 1usize..10000,
            compact_threshold in 0.0f32..1.0,
            min_tokens in 0usize..1000
        ) {
            let config = SoftPruneConfig::with_compaction(
                beam,
                max_active,
                compact_threshold,
                min_tokens,
            );
            prop_assert!((config.beam - beam).abs() < 1e-6);
            prop_assert_eq!(config.max_active, max_active);
            prop_assert!((config.compact_threshold - compact_threshold).abs() < 1e-6);
            prop_assert_eq!(config.min_tokens_for_compact, min_tokens);
        }
    }

    // =========================================================================
    // SoftPruneBuffer Properties
    // =========================================================================

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(50))]

        /// Push increases active count for tokens within threshold.
        #[test]
        fn buffer_push_increases_active_count(
            num_tokens in 1usize..20,
            beam in 10.0f32..100.0
        ) {
            let config = SoftPruneConfig::new(beam, 1000);
            let mut buffer = SoftPruneBuffer::new(config);

            // Add tokens with costs well below the beam
            for i in 0..num_tokens {
                buffer.push(SoftToken::new(i as i32, 5, 0, i as f32 * 0.1));
            }

            prop_assert_eq!(buffer.active_count(), num_tokens);
            prop_assert_eq!(buffer.total_count(), num_tokens);
        }

        /// Tokens exceeding threshold are rejected on push.
        #[test]
        fn buffer_rejects_high_cost_tokens(
            base_cost in 0.0f32..10.0,
            beam in 1.0f32..5.0
        ) {
            let config = SoftPruneConfig::new(beam, 1000);
            let mut buffer = SoftPruneBuffer::new(config);

            // Add first token to set best cost
            let idx1 = buffer.push(SoftToken::new(1, 5, 0, base_cost));
            prop_assert!(idx1.is_some());

            // Add token that exceeds threshold
            let high_cost = base_cost + beam + 1.0;
            let idx2 = buffer.push(SoftToken::new(2, 5, 0, high_cost));
            prop_assert!(idx2.is_none());

            prop_assert_eq!(buffer.active_count(), 1);
        }

        /// Compact removes exactly the pruned tokens.
        #[test]
        fn buffer_compact_removes_pruned(
            num_tokens in 5usize..20,
            num_to_prune in 1usize..5
        ) {
            let config = SoftPruneConfig::new(100.0, 1000);
            let mut buffer = SoftPruneBuffer::<i32>::new(config);

            // Add tokens
            for i in 0..num_tokens {
                buffer.push(SoftToken::new(i as i32, 5, 0, i as f32));
            }

            // Prune some tokens
            let actual_prune = num_to_prune.min(num_tokens);
            for i in 0..actual_prune {
                if let Some(token) = buffer.get(i) {
                    token.soft_prune();
                }
            }

            let removed = buffer.compact();
            prop_assert_eq!(removed, actual_prune);
            prop_assert_eq!(buffer.total_count(), num_tokens - actual_prune);
        }

        /// into_survivors returns only active tokens.
        #[test]
        fn buffer_into_survivors_only_active(
            num_tokens in 2usize..10,
            prune_indices in proptest::collection::vec(0usize..10, 0..5)
        ) {
            let config = SoftPruneConfig::new(100.0, 1000);
            let mut buffer = SoftPruneBuffer::new(config);

            for i in 0..num_tokens {
                buffer.push(SoftToken::new(i as i32, 5, 0, i as f32 * 0.5));
            }

            // Prune tokens at specified indices
            let mut pruned_count = 0;
            for &idx in &prune_indices {
                if idx < num_tokens {
                    if let Some(token) = buffer.get(idx) {
                        if token.is_active() {
                            token.soft_prune();
                            pruned_count += 1;
                        }
                    }
                }
            }

            let survivors = buffer.into_survivors();
            prop_assert_eq!(survivors.len(), num_tokens - pruned_count);
        }

        /// best_cost tracks minimum cost seen.
        #[test]
        fn buffer_tracks_best_cost(costs in proptest::collection::vec(0.0f32..100.0, 1..20)) {
            let config = SoftPruneConfig::new(200.0, 1000);
            let mut buffer = SoftPruneBuffer::new(config);

            for (i, &cost) in costs.iter().enumerate() {
                buffer.push(SoftToken::new(i as i32, 5, 0, cost));
            }

            let expected_best = costs.iter().cloned().fold(f32::INFINITY, f32::min);
            prop_assert!((buffer.best_cost() - expected_best).abs() < 1e-6);
        }

        /// Clear resets buffer state.
        #[test]
        fn buffer_clear_resets(num_tokens in 1usize..20) {
            let config = SoftPruneConfig::new(100.0, 1000);
            let mut buffer = SoftPruneBuffer::new(config);

            for i in 0..num_tokens {
                buffer.push(SoftToken::new(i as i32, 5, 0, i as f32));
            }

            buffer.clear();

            prop_assert_eq!(buffer.active_count(), 0);
            prop_assert_eq!(buffer.total_count(), 0);
            prop_assert!(buffer.best_cost().is_infinite());
        }
    }

    // =========================================================================
    // SoftPruneStats Properties
    // =========================================================================

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(100))]

        /// total_pruned = beam_pruned + limit_pruned.
        #[test]
        fn stats_total_pruned_correct(
            beam_pruned in 0usize..1000,
            limit_pruned in 0usize..1000
        ) {
            let mut stats = SoftPruneStats::new();
            stats.record_beam_prune(beam_pruned);
            stats.record_limit_prune(limit_pruned);

            prop_assert_eq!(stats.total_pruned(), beam_pruned + limit_pruned);
        }

        /// prune_ratio is in [0, 1] when total_tokens > 0.
        #[test]
        fn stats_prune_ratio_bounded(
            total_tokens in 1usize..1000,
            beam_pruned in 0usize..500,
            limit_pruned in 0usize..500
        ) {
            let mut stats = SoftPruneStats::new();
            stats.record_tokens(total_tokens);
            stats.record_beam_prune(beam_pruned.min(total_tokens));
            stats.record_limit_prune(limit_pruned.min(total_tokens - beam_pruned.min(total_tokens)));

            let ratio = stats.prune_ratio();
            prop_assert!(ratio >= 0.0);
            prop_assert!(ratio <= 1.0);
        }

        /// prune_ratio is 0 when no tokens processed.
        #[test]
        fn stats_prune_ratio_zero_tokens(
            beam_pruned in 0usize..100,
            limit_pruned in 0usize..100
        ) {
            let mut stats = SoftPruneStats::new();
            stats.record_beam_prune(beam_pruned);
            stats.record_limit_prune(limit_pruned);

            prop_assert!((stats.prune_ratio() - 0.0).abs() < 1e-10);
        }

        /// Merge combines all stats correctly.
        #[test]
        fn stats_merge_correct(
            total1 in 0usize..500,
            beam1 in 0usize..250,
            limit1 in 0usize..250,
            compact1 in 0usize..100,
            total2 in 0usize..500,
            beam2 in 0usize..250,
            limit2 in 0usize..250,
            compact2 in 0usize..100
        ) {
            let mut stats1 = SoftPruneStats::new();
            stats1.record_tokens(total1);
            stats1.record_beam_prune(beam1);
            stats1.record_limit_prune(limit1);
            stats1.record_compaction(compact1);

            let mut stats2 = SoftPruneStats::new();
            stats2.record_tokens(total2);
            stats2.record_beam_prune(beam2);
            stats2.record_limit_prune(limit2);
            stats2.record_compaction(compact2);

            stats1.merge(&stats2);

            prop_assert_eq!(stats1.total_tokens, total1 + total2);
            prop_assert_eq!(stats1.beam_pruned, beam1 + beam2);
            prop_assert_eq!(stats1.limit_pruned, limit1 + limit2);
            prop_assert_eq!(stats1.compactions, 2);
            prop_assert_eq!(stats1.compacted_tokens, compact1 + compact2);
        }

        /// compaction_efficiency is 0 when no compactions.
        #[test]
        fn stats_compaction_efficiency_zero(compacted in 0usize..100) {
            let mut stats = SoftPruneStats::new();
            // Don't record any compactions, just set compacted_tokens manually
            stats.compacted_tokens = compacted;

            prop_assert!((stats.compaction_efficiency() - 0.0).abs() < 1e-10);
        }
    }

    // =========================================================================
    // AdaptiveBeam Properties
    // =========================================================================

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(50))]

        /// Reset clears all bucket counts.
        #[test]
        fn adaptive_beam_reset_clears(
            num_buckets in 5usize..50,
            target in 10usize..100,
            num_adds in 1usize..50
        ) {
            let mut beam = AdaptiveBeam::new(num_buckets, target);
            beam.set_range(0.0, 100.0);

            for i in 0..num_adds {
                beam.add_with_range(i as f32, 0.0, 100.0);
            }

            beam.reset();

            prop_assert_eq!(beam.total_count(), 0);
            prop_assert!(beam.min_cost.is_infinite());
            prop_assert!(beam.max_cost.is_infinite() && beam.max_cost.is_sign_negative());
        }

        /// total_count matches number of adds.
        #[test]
        fn adaptive_beam_total_count_correct(
            num_buckets in 5usize..50,
            target in 10usize..100,
            costs in proptest::collection::vec(0.0f32..100.0, 1..50)
        ) {
            let beam = AdaptiveBeam::new(num_buckets, target);

            for &cost in &costs {
                beam.add_with_range(cost, 0.0, 100.0);
            }

            prop_assert_eq!(beam.total_count(), costs.len());
        }

        /// compute_threshold returns INFINITY when all tokens fit.
        #[test]
        fn adaptive_beam_threshold_infinity_when_fits(
            num_buckets in 5usize..20,
            num_costs in 1usize..10
        ) {
            // Target more than we'll add
            let target = num_costs + 10;
            let mut beam = AdaptiveBeam::new(num_buckets, target);
            beam.set_range(0.0, 100.0);

            for i in 0..num_costs {
                beam.add_with_range(i as f32 * 5.0, 0.0, 100.0);
            }

            let threshold = beam.compute_threshold();
            prop_assert!(threshold.is_infinite());
        }

        /// compute_threshold is between min_cost and max_cost when pruning needed.
        #[test]
        fn adaptive_beam_threshold_in_range(
            num_buckets in 10usize..50,
            costs in proptest::collection::vec(0.0f32..100.0, 20..100)
        ) {
            // Target less than half of what we add
            let target = costs.len() / 3;
            let mut beam = AdaptiveBeam::new(num_buckets, target);

            let min_cost = costs.iter().cloned().fold(f32::INFINITY, f32::min);
            let max_cost = costs.iter().cloned().fold(f32::NEG_INFINITY, f32::max);

            if max_cost > min_cost {
                beam.set_range(min_cost, max_cost);

                for &cost in &costs {
                    beam.add_with_range(cost, min_cost, max_cost);
                }

                let threshold = beam.compute_threshold();

                // Threshold should be between min and max (or infinity if all fit)
                if !threshold.is_infinite() {
                    prop_assert!(threshold >= min_cost);
                    prop_assert!(threshold <= max_cost + 1e-6);
                }
            }
        }
    }

    // =========================================================================
    // SoftPruneManager Properties
    // =========================================================================

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(50))]

        /// advance_frame increments frame counter.
        #[test]
        fn manager_advance_frame_increments(num_advances in 1usize..10) {
            let config = SoftPruneConfig::new(100.0, 1000);
            let mut manager = SoftPruneManager::<i32>::new(config);

            for i in 0..num_advances {
                prop_assert_eq!(manager.frame(), i as u32);
                manager.advance_frame();
            }

            prop_assert_eq!(manager.frame(), num_advances as u32);
        }

        /// Frame swap moves current to previous.
        #[test]
        fn manager_frame_swap(num_tokens in 1usize..10) {
            let config = SoftPruneConfig::new(100.0, 1000);
            let mut manager = SoftPruneManager::new(config);

            // Add tokens to current frame
            for i in 0..num_tokens {
                manager.add_token(i as i32, 5, i as f32);
            }

            let current_count_before = manager.current().active_count();
            manager.advance_frame();

            // Previous should have the tokens from before
            prop_assert_eq!(manager.previous().active_count(), current_count_before);
            // Current should be empty
            prop_assert_eq!(manager.current().active_count(), 0);
        }

        /// survivors only returns active tokens.
        #[test]
        fn manager_survivors_only_active(
            num_tokens in 2usize..10,
            prune_first in any::<bool>()
        ) {
            let config = SoftPruneConfig::new(100.0, 1000);
            let mut manager = SoftPruneManager::new(config);

            for i in 0..num_tokens {
                manager.add_token(i as i32, 5, i as f32);
            }

            let expected_survivors = if prune_first {
                manager.current().get(0).expect("gpu/soft_prune.rs: required value was None/Err").soft_prune();
                num_tokens - 1
            } else {
                num_tokens
            };

            let survivors = manager.survivors();
            prop_assert_eq!(survivors.len(), expected_survivors);
        }

        /// Stats track tokens added.
        #[test]
        fn manager_stats_track_tokens(num_tokens in 1usize..20) {
            let config = SoftPruneConfig::new(100.0, 1000);
            let mut manager = SoftPruneManager::new(config);

            for i in 0..num_tokens {
                manager.add_token(i as i32, 5, i as f32);
            }

            prop_assert_eq!(manager.stats().total_tokens, num_tokens);
        }

        /// apply_pruning updates stats.
        #[test]
        fn manager_apply_pruning_updates_stats(num_tokens in 5usize..20) {
            let beam = 5.0f32;
            let config = SoftPruneConfig::new(beam, 1000);
            let mut manager = SoftPruneManager::new(config);

            // Add tokens with increasing costs - some will be beyond beam
            for i in 0..num_tokens {
                manager.add_token(i as i32, 5, i as f32);
            }

            let pruned = manager.apply_pruning();
            prop_assert_eq!(manager.stats().beam_pruned, pruned);
        }
    }
}