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
//! Quantized semiring weights for memory-efficient WFSTs.
//!
//! This module provides quantized weight representations that store weights
//! in reduced precision (8-bit or 4-bit) for significant memory savings.
//!
//! ## Use Cases
//!
//! - Large-vocabulary language models where memory is constrained
//! - Mobile/embedded deployment with limited RAM
//! - Approximate computations where full precision is unnecessary
//! - Fast nearest-neighbor lookups using quantized distances
//!
//! ## Quantization Schemes
//!
//! | Type | Bits | Range | Use Case |
//! |------|------|-------|----------|
//! | `Quantized8Weight` | 8 | 256 levels | General purpose |
//! | `Quantized4Weight` | 4 | 16 levels | Extreme compression |
//!
//! ## Example
//!
//! ```rust
//! use lling_llang::semiring::{LogWeight, Semiring};
//! use lling_llang::semiring::quantized::{Quantized8Weight, QuantizationParams};
//!
//! // Create quantization parameters for log probabilities in [-10, 0]
//! let params = QuantizationParams::new(-10.0, 0.0);
//!
//! // Quantize a LogWeight
//! let weight = LogWeight::new(-3.5);
//! let quantized = Quantized8Weight::from_log_weight(weight, &params);
//!
//! // Dequantize back
//! let recovered = quantized.to_log_weight(&params);
//! assert!((recovered.value() - (-3.5)).abs() < 0.1); // Within quantization error
//! ```

use crate::semiring::basic::{LogWeight, TropicalWeight};
use std::fmt;

/// Parameters for quantization/dequantization.
///
/// Defines the mapping between continuous weight values and quantized integers.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct QuantizationParams {
    /// Minimum value in the quantization range.
    pub min_val: f64,
    /// Maximum value in the quantization range.
    pub max_val: f64,
    /// Scale factor from real values to the 8-bit max raw index.
    scale: f64,
    /// Real-valued spacing between adjacent 8-bit raw values.
    inv_scale: f64,
}

impl QuantizationParams {
    /// Create new quantization parameters for a given range.
    ///
    /// # Arguments
    ///
    /// * `min_val` - Minimum expected weight value
    /// * `max_val` - Maximum expected weight value
    ///
    /// # Panics
    ///
    /// Panics if either bound is non-finite, if `min_val >= max_val`, or if
    /// the range cannot be represented as a finite `f64`.
    pub fn new(min_val: f64, max_val: f64) -> Self {
        assert!(
            min_val.is_finite() && max_val.is_finite(),
            "quantization bounds must be finite: [{}, {}]",
            min_val,
            max_val
        );
        assert!(
            min_val < max_val,
            "min_val must be less than max_val: {} >= {}",
            min_val,
            max_val
        );
        let range = max_val - min_val;
        assert!(
            range.is_finite(),
            "quantization range must be finite: {} - {}",
            max_val,
            min_val
        );
        Self {
            min_val,
            max_val,
            scale: 255.0 / range, // For 8-bit quantization
            inv_scale: range / 255.0,
        }
    }

    /// Create parameters from observed data by computing min/max.
    pub fn from_data(values: impl Iterator<Item = f64>) -> Option<Self> {
        let mut min_val = f64::INFINITY;
        let mut max_val = f64::NEG_INFINITY;
        let mut count = 0;

        for v in values {
            if v.is_finite() {
                min_val = min_val.min(v);
                max_val = max_val.max(v);
                count += 1;
            }
        }

        if count == 0 || min_val >= max_val {
            return None;
        }

        // Add small margin to avoid boundary issues.
        let range = max_val - min_val;
        if !range.is_finite() {
            return None;
        }
        let margin = range * 0.01;
        if !margin.is_finite() {
            return None;
        }
        let lower = min_val - margin;
        let upper = max_val + margin;
        if !(lower.is_finite() && upper.is_finite()) {
            return None;
        }
        Some(Self::new(lower, upper))
    }

    /// Create parameters for log-space weights (typical range: -20 to 0).
    pub fn for_log_weights() -> Self {
        Self::new(-20.0, 0.0)
    }

    /// Create parameters for tropical weights (typical range: 0 to 20).
    pub fn for_tropical_weights() -> Self {
        Self::new(0.0, 20.0)
    }

    /// Get the quantization scale factor (for 8-bit).
    #[inline]
    pub fn scale_8bit(&self) -> f64 {
        self.scale
    }

    /// Get the quantization scale factor for 4-bit.
    #[inline]
    pub fn scale_4bit(&self) -> f64 {
        15.0 / (self.max_val - self.min_val)
    }

    /// Quantize a value to 8-bit.
    #[inline]
    pub fn quantize_8bit(&self, value: f64) -> u8 {
        if !value.is_finite() {
            return if value.is_sign_positive() { 255 } else { 0 };
        }
        let clamped = value.clamp(self.min_val, self.max_val);
        let normalized = (clamped - self.min_val) * self.scale;
        normalized.round() as u8
    }

    /// Dequantize an 8-bit value.
    #[inline]
    pub fn dequantize_8bit(&self, quantized: u8) -> f64 {
        self.min_val + (quantized as f64) * self.inv_scale
    }

    /// Quantize a value to 4-bit.
    #[inline]
    pub fn quantize_4bit(&self, value: f64) -> u8 {
        if !value.is_finite() {
            return if value.is_sign_positive() { 15 } else { 0 };
        }
        let clamped = value.clamp(self.min_val, self.max_val);
        let scale = self.scale_4bit();
        let normalized = (clamped - self.min_val) * scale;
        (normalized.round() as u8).min(15)
    }

    /// Dequantize a 4-bit value.
    #[inline]
    pub fn dequantize_4bit(&self, quantized: u8) -> f64 {
        let inv_scale = (self.max_val - self.min_val) / 15.0;
        self.min_val + ((quantized & 0x0F) as f64) * inv_scale
    }
}

// ============================================================================
// 8-bit Quantized Weight
// ============================================================================

/// 8-bit quantized weight providing 256 distinct levels.
///
/// Memory usage is 1 byte per weight compared to 8 bytes for f64,
/// giving 8× memory reduction.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Quantized8Weight {
    /// The quantized value (0-255).
    value: u8,
}

impl Quantized8Weight {
    /// Create a new quantized weight from a raw byte value.
    #[inline]
    pub const fn from_raw(value: u8) -> Self {
        Self { value }
    }

    /// Get the raw byte value.
    #[inline]
    pub const fn raw(&self) -> u8 {
        self.value
    }

    /// Create from a LogWeight using given quantization parameters.
    #[inline]
    pub fn from_log_weight(weight: LogWeight, params: &QuantizationParams) -> Self {
        Self {
            value: params.quantize_8bit(weight.value()),
        }
    }

    /// Convert back to LogWeight.
    #[inline]
    pub fn to_log_weight(&self, params: &QuantizationParams) -> LogWeight {
        LogWeight::new(params.dequantize_8bit(self.value))
    }

    /// Create from a TropicalWeight.
    #[inline]
    pub fn from_tropical_weight(weight: TropicalWeight, params: &QuantizationParams) -> Self {
        Self {
            value: params.quantize_8bit(weight.value()),
        }
    }

    /// Convert back to TropicalWeight.
    #[inline]
    pub fn to_tropical_weight(&self, params: &QuantizationParams) -> TropicalWeight {
        TropicalWeight::new(params.dequantize_8bit(self.value))
    }

    /// Zero value (represents minimum of range, typically infinity).
    pub const ZERO: Self = Self { value: 255 };

    /// One value (represents neutral element).
    pub const ONE: Self = Self { value: 0 };
}

impl fmt::Debug for Quantized8Weight {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Q8({})", self.value)
    }
}

impl fmt::Display for Quantized8Weight {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.value)
    }
}

impl Default for Quantized8Weight {
    fn default() -> Self {
        Self::ZERO
    }
}

// ============================================================================
// 4-bit Quantized Weight
// ============================================================================

/// 4-bit quantized weight providing 16 distinct levels.
///
/// Two weights can be packed into a single byte for extreme compression.
/// Useful when weight precision is less important than memory footprint.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Quantized4Weight {
    /// The quantized value (0-15).
    value: u8,
}

impl Quantized4Weight {
    /// Create a new quantized weight from a raw nibble value.
    #[inline]
    pub const fn from_raw(value: u8) -> Self {
        Self {
            value: value & 0x0F,
        }
    }

    /// Get the raw nibble value.
    #[inline]
    pub const fn raw(&self) -> u8 {
        self.value
    }

    /// Create from a LogWeight.
    #[inline]
    pub fn from_log_weight(weight: LogWeight, params: &QuantizationParams) -> Self {
        Self {
            value: params.quantize_4bit(weight.value()),
        }
    }

    /// Convert back to LogWeight.
    #[inline]
    pub fn to_log_weight(&self, params: &QuantizationParams) -> LogWeight {
        LogWeight::new(params.dequantize_4bit(self.value))
    }

    /// Create from a TropicalWeight.
    #[inline]
    pub fn from_tropical_weight(weight: TropicalWeight, params: &QuantizationParams) -> Self {
        Self {
            value: params.quantize_4bit(weight.value()),
        }
    }

    /// Convert back to TropicalWeight.
    #[inline]
    pub fn to_tropical_weight(&self, params: &QuantizationParams) -> TropicalWeight {
        TropicalWeight::new(params.dequantize_4bit(self.value))
    }

    /// Zero value (represents minimum of range).
    pub const ZERO: Self = Self { value: 15 };

    /// One value (represents neutral element).
    pub const ONE: Self = Self { value: 0 };
}

impl fmt::Debug for Quantized4Weight {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Q4({})", self.value)
    }
}

impl fmt::Display for Quantized4Weight {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.value)
    }
}

impl Default for Quantized4Weight {
    fn default() -> Self {
        Self::ZERO
    }
}

// ============================================================================
// Packed Weight Storage
// ============================================================================

/// Packed storage for 4-bit weights, storing two weights per byte.
///
/// This is useful for storing large arrays of weights with minimal memory.
#[derive(Debug, Clone)]
pub struct PackedWeights4 {
    /// Packed bytes, each containing two 4-bit weights.
    data: Vec<u8>,
    /// Number of weights stored.
    len: usize,
}

impl PackedWeights4 {
    /// Create a new packed weight array with the given capacity.
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            data: Vec::with_capacity(capacity.div_ceil(2)),
            len: 0,
        }
    }

    /// Create from a slice of 4-bit weights.
    pub fn from_weights(weights: &[Quantized4Weight]) -> Self {
        let mut packed = Self::with_capacity(weights.len());
        for w in weights {
            packed.push(*w);
        }
        packed
    }

    /// Push a weight onto the array.
    pub fn push(&mut self, weight: Quantized4Weight) {
        if self.len % 2 == 0 {
            self.data.push(weight.raw());
        } else {
            let last = self.data.last_mut().expect("non-empty after odd push");
            *last |= weight.raw() << 4;
        }
        self.len += 1;
    }

    /// Get the weight at the given index.
    #[inline]
    pub fn get(&self, index: usize) -> Option<Quantized4Weight> {
        if index >= self.len {
            return None;
        }
        let byte_idx = index / 2;
        let nibble = if index % 2 == 0 {
            self.data[byte_idx] & 0x0F
        } else {
            (self.data[byte_idx] >> 4) & 0x0F
        };
        Some(Quantized4Weight::from_raw(nibble))
    }

    /// Set the weight at the given index.
    pub fn set(&mut self, index: usize, weight: Quantized4Weight) {
        if index >= self.len {
            return;
        }
        let byte_idx = index / 2;
        if index % 2 == 0 {
            self.data[byte_idx] = (self.data[byte_idx] & 0xF0) | weight.raw();
        } else {
            self.data[byte_idx] = (self.data[byte_idx] & 0x0F) | (weight.raw() << 4);
        }
    }

    /// Get the number of weights stored.
    #[inline]
    pub fn len(&self) -> usize {
        self.len
    }

    /// Check if empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// Get the memory size in bytes.
    #[inline]
    pub fn memory_size(&self) -> usize {
        self.data.len()
    }

    /// Iterate over the weights.
    pub fn iter(&self) -> impl Iterator<Item = Quantized4Weight> + '_ {
        (0..self.len).map(|i| self.get(i).expect("valid index"))
    }
}

// ============================================================================
// Quantization Statistics
// ============================================================================

/// Statistics about quantization error.
#[derive(Debug, Clone, Default)]
pub struct QuantizationStats {
    /// Total number of weights quantized.
    pub count: usize,
    /// Sum of absolute quantization errors.
    pub total_error: f64,
    /// Maximum absolute quantization error.
    pub max_error: f64,
    /// Sum of squared errors.
    pub sum_squared_error: f64,
}

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

    /// Add an observation of original and quantized values.
    pub fn add(&mut self, original: f64, quantized: f64) {
        let error = (original - quantized).abs();
        self.count += 1;
        self.total_error += error;
        self.max_error = self.max_error.max(error);
        self.sum_squared_error += error * error;
    }

    /// Get the mean absolute error.
    pub fn mean_error(&self) -> f64 {
        if self.count == 0 {
            0.0
        } else {
            self.total_error / self.count as f64
        }
    }

    /// Get the root mean squared error.
    pub fn rmse(&self) -> f64 {
        if self.count == 0 {
            0.0
        } else {
            (self.sum_squared_error / self.count as f64).sqrt()
        }
    }
}

// ============================================================================
// Batch Quantization
// ============================================================================

/// Quantize a batch of log weights to 8-bit.
pub fn quantize_log_weights_8bit(
    weights: &[LogWeight],
    params: &QuantizationParams,
) -> Vec<Quantized8Weight> {
    weights
        .iter()
        .map(|w| Quantized8Weight::from_log_weight(*w, params))
        .collect()
}

/// Dequantize a batch of 8-bit weights to log weights.
pub fn dequantize_8bit_to_log(
    weights: &[Quantized8Weight],
    params: &QuantizationParams,
) -> Vec<LogWeight> {
    weights.iter().map(|w| w.to_log_weight(params)).collect()
}

/// Quantize a batch of log weights to 4-bit.
pub fn quantize_log_weights_4bit(
    weights: &[LogWeight],
    params: &QuantizationParams,
) -> Vec<Quantized4Weight> {
    weights
        .iter()
        .map(|w| Quantized4Weight::from_log_weight(*w, params))
        .collect()
}

/// Quantize and pack log weights to 4-bit packed format.
pub fn quantize_and_pack_4bit(
    weights: &[LogWeight],
    params: &QuantizationParams,
) -> PackedWeights4 {
    let quantized: Vec<_> = weights
        .iter()
        .map(|w| Quantized4Weight::from_log_weight(*w, params))
        .collect();
    PackedWeights4::from_weights(&quantized)
}

/// Compute quantization statistics for a batch of weights.
pub fn compute_quantization_stats_8bit(
    weights: &[LogWeight],
    params: &QuantizationParams,
) -> QuantizationStats {
    let mut stats = QuantizationStats::new();
    for w in weights {
        let original = w.value();
        let quantized = Quantized8Weight::from_log_weight(*w, params);
        let dequantized = quantized.to_log_weight(params).value();
        stats.add(original, dequantized);
    }
    stats
}

/// Compute quantization statistics for 4-bit quantization.
pub fn compute_quantization_stats_4bit(
    weights: &[LogWeight],
    params: &QuantizationParams,
) -> QuantizationStats {
    let mut stats = QuantizationStats::new();
    for w in weights {
        let original = w.value();
        let quantized = Quantized4Weight::from_log_weight(*w, params);
        let dequantized = quantized.to_log_weight(params).value();
        stats.add(original, dequantized);
    }
    stats
}

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

    #[test]
    fn test_quantization_params() {
        let params = QuantizationParams::new(-10.0, 10.0);

        // Test boundary values
        assert_eq!(params.quantize_8bit(-10.0), 0);
        assert_eq!(params.quantize_8bit(10.0), 255);

        // Test midpoint
        let mid = params.quantize_8bit(0.0);
        assert!((mid as i32 - 127).abs() <= 1); // Should be around 127-128
    }

    #[test]
    #[should_panic(expected = "quantization bounds must be finite")]
    fn test_quantization_params_reject_nonfinite_bounds() {
        let _ = QuantizationParams::new(0.0, f64::INFINITY);
    }

    #[test]
    #[should_panic(expected = "quantization range must be finite")]
    fn test_quantization_params_reject_overflowing_range() {
        let _ = QuantizationParams::new(-f64::MAX, f64::MAX);
    }

    #[test]
    fn test_roundtrip_8bit() {
        let params = QuantizationParams::for_log_weights();

        let original = LogWeight::new(-5.0);
        let quantized = Quantized8Weight::from_log_weight(original, &params);
        let recovered = quantized.to_log_weight(&params);

        // Should be within quantization error (~0.08 for 256 levels over range of 20)
        assert!((recovered.value() - original.value()).abs() < 0.1);
    }

    #[test]
    fn test_roundtrip_8bit_error_bound() {
        let params = QuantizationParams::new(-20.0, 20.0);
        let half_step = (params.max_val - params.min_val) / 255.0 / 2.0;

        for i in 0..=1000 {
            let value = params.min_val + (params.max_val - params.min_val) * (i as f64) / 1000.0;
            let quantized = params.quantize_8bit(value);
            let recovered = params.dequantize_8bit(quantized);
            let error = (recovered - value).abs();
            assert!(
                error <= half_step + 1e-12,
                "8-bit quantization error {} exceeded half-step {} for value {}",
                error,
                half_step,
                value
            );
        }
    }

    #[test]
    fn test_roundtrip_4bit() {
        let params = QuantizationParams::for_log_weights();

        let original = LogWeight::new(-10.0);
        let quantized = Quantized4Weight::from_log_weight(original, &params);
        let recovered = quantized.to_log_weight(&params);

        // 4-bit has larger error (~1.33 for 16 levels over range of 20)
        assert!((recovered.value() - original.value()).abs() < 2.0);
    }

    #[test]
    fn test_roundtrip_4bit_error_bound() {
        let params = QuantizationParams::new(-20.0, 20.0);
        let half_step = (params.max_val - params.min_val) / 15.0 / 2.0;

        for i in 0..=1000 {
            let value = params.min_val + (params.max_val - params.min_val) * (i as f64) / 1000.0;
            let quantized = params.quantize_4bit(value);
            let recovered = params.dequantize_4bit(quantized);
            let error = (recovered - value).abs();
            assert!(
                error <= half_step + 1e-12,
                "4-bit quantization error {} exceeded half-step {} for value {}",
                error,
                half_step,
                value
            );
        }
    }

    #[test]
    fn test_packed_weights() {
        let params = QuantizationParams::for_log_weights();
        let weights: Vec<LogWeight> = (0..10).map(|i| LogWeight::new(-i as f64 * 2.0)).collect();

        let packed = quantize_and_pack_4bit(&weights, &params);

        assert_eq!(packed.len(), 10);
        assert_eq!(packed.memory_size(), 5); // 10 weights in 5 bytes

        // Verify roundtrip
        for (i, w) in weights.iter().enumerate() {
            let packed_w = packed.get(i).expect("valid index");
            let recovered = packed_w.to_log_weight(&params);
            assert!((recovered.value() - w.value()).abs() < 2.0);
        }
    }

    #[test]
    fn test_quantization_stats() {
        let params = QuantizationParams::for_log_weights();
        let weights: Vec<LogWeight> = (0..100).map(|i| LogWeight::new(-i as f64 * 0.2)).collect();

        let stats = compute_quantization_stats_8bit(&weights, &params);

        assert_eq!(stats.count, 100);
        assert!(stats.mean_error() < 0.1);
        assert!(stats.rmse() < 0.1);
    }

    #[test]
    fn test_infinity_handling() {
        let params = QuantizationParams::for_log_weights();

        let inf = LogWeight::new(f64::INFINITY);
        let below_range = LogWeight::new(-1.0e300);

        let q_inf = Quantized8Weight::from_log_weight(inf, &params);
        let q_below_range = Quantized8Weight::from_log_weight(below_range, &params);

        assert_eq!(q_inf.raw(), 255);
        assert_eq!(q_below_range.raw(), 0);
    }

    #[test]
    fn test_tropical_quantization() {
        let params = QuantizationParams::for_tropical_weights();

        let original = TropicalWeight::new(5.0);
        let quantized = Quantized8Weight::from_tropical_weight(original, &params);
        let recovered = quantized.to_tropical_weight(&params);

        assert!((recovered.value() - original.value()).abs() < 0.1);
    }

    #[test]
    fn test_params_from_data() {
        let values = vec![1.0, 5.0, 3.0, 7.0, 2.0];
        let params = QuantizationParams::from_data(values.into_iter()).expect("valid data");

        // Should include some margin
        assert!(params.min_val < 1.0);
        assert!(params.max_val > 7.0);
    }

    #[test]
    fn test_params_from_data_rejects_nonfinite_range() {
        let values = vec![-f64::MAX, f64::MAX];
        assert!(QuantizationParams::from_data(values.into_iter()).is_none());
    }

    #[test]
    fn test_batch_quantize() {
        let params = QuantizationParams::for_log_weights();
        let weights: Vec<LogWeight> = vec![
            LogWeight::new(-1.0),
            LogWeight::new(-5.0),
            LogWeight::new(-10.0),
        ];

        let quantized = quantize_log_weights_8bit(&weights, &params);
        let dequantized = dequantize_8bit_to_log(&quantized, &params);

        assert_eq!(dequantized.len(), 3);
        for (orig, deq) in weights.iter().zip(dequantized.iter()) {
            assert!((orig.value() - deq.value()).abs() < 0.1);
        }
    }

    #[test]
    fn test_packed_weights_set() {
        let mut packed = PackedWeights4::with_capacity(4);
        packed.push(Quantized4Weight::from_raw(3));
        packed.push(Quantized4Weight::from_raw(7));
        packed.push(Quantized4Weight::from_raw(11));
        packed.push(Quantized4Weight::from_raw(15));

        assert_eq!(packed.get(0).expect("valid").raw(), 3);
        assert_eq!(packed.get(1).expect("valid").raw(), 7);

        packed.set(1, Quantized4Weight::from_raw(9));
        assert_eq!(packed.get(1).expect("valid").raw(), 9);

        // Verify other values unchanged
        assert_eq!(packed.get(0).expect("valid").raw(), 3);
        assert_eq!(packed.get(2).expect("valid").raw(), 11);
    }
}