-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbq.rs
More file actions
189 lines (169 loc) 路 5.03 KB
/
Copy pathbq.rs
File metadata and controls
189 lines (169 loc) 路 5.03 KB
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
//! Binary quantization (BQ) implementation.
//!
//! Binary quantization is the simplest form of quantization, mapping each value
//! to one of two discrete levels based on a threshold comparison.
use crate::core::error::{VqError, VqResult};
use crate::core::quantizer::Quantizer;
/// Binary quantizer that maps values above/below a threshold to two discrete levels.
///
/// # Example
///
/// ```
/// use vq::BinaryQuantizer;
/// use vq::Quantizer;
///
/// let bq = BinaryQuantizer::new(0.0, 0, 1).unwrap();
/// let quantized = bq.quantize(&[-1.0, 0.5, 1.0]).unwrap();
/// assert_eq!(quantized, vec![0, 1, 1]);
/// ```
pub struct BinaryQuantizer {
threshold: f32,
low: u8,
high: u8,
}
impl BinaryQuantizer {
/// Creates a new binary quantizer.
///
/// # Arguments
///
/// * `threshold` - Values >= threshold map to `high`, values < threshold map to `low`
/// * `low` - The output value for inputs below the threshold
/// * `high` - The output value for inputs at or above the threshold
///
/// # Example
///
/// ```
/// use vq::BinaryQuantizer;
/// use vq::Quantizer;
///
/// // Create a binary quantizer with threshold 0.5
/// let bq = BinaryQuantizer::new(0.5, 0, 1).unwrap();
///
/// // Values >= 0.5 map to 1, values < 0.5 map to 0
/// let result = bq.quantize(&[0.3, 0.5, 0.7]).unwrap();
/// assert_eq!(result, vec![0, 1, 1]);
/// ```
///
/// # Errors
///
/// Returns an error if:
/// - `low >= high`
/// - `threshold` is NaN or infinite
pub fn new(threshold: f32, low: u8, high: u8) -> VqResult<Self> {
if !threshold.is_finite() {
return Err(VqError::InvalidParameter {
parameter: "threshold",
reason: "must be finite (not NaN or infinite)".to_string(),
});
}
if low >= high {
return Err(VqError::InvalidParameter {
parameter: "low/high",
reason: "low must be less than high".to_string(),
});
}
Ok(Self {
threshold,
low,
high,
})
}
/// Returns the threshold value.
pub fn threshold(&self) -> f32 {
self.threshold
}
/// Returns the low quantization level.
pub fn low(&self) -> u8 {
self.low
}
/// Returns the high quantization level.
pub fn high(&self) -> u8 {
self.high
}
}
impl Quantizer for BinaryQuantizer {
type QuantizedOutput = Vec<u8>;
fn quantize(&self, vector: &[f32]) -> VqResult<Self::QuantizedOutput> {
Ok(vector
.iter()
.map(|&x| {
if x >= self.threshold {
self.high
} else {
self.low
}
})
.collect())
}
fn dequantize(&self, quantized: &Self::QuantizedOutput) -> VqResult<Vec<f32>> {
Ok(quantized
.iter()
.map(|&x| {
if x >= self.high {
self.high as f32
} else {
self.low as f32
}
})
.collect())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_basic() {
let bq = BinaryQuantizer::new(0.0, 0, 1).unwrap();
let input = vec![-1.0, 0.0, 1.0, -0.5, 0.5];
let result = bq.quantize(&input).unwrap();
assert_eq!(result, vec![0, 1, 1, 0, 1]);
}
#[test]
fn test_large_vector() {
let bq = BinaryQuantizer::new(0.0, 0, 1).unwrap();
let input: Vec<f32> = (0..1024).map(|i| (i as f32) - 512.0).collect();
let result = bq.quantize(&input).unwrap();
assert_eq!(result.len(), 1024);
for (i, &val) in result.iter().enumerate() {
let expected = if input[i] >= 0.0 { 1 } else { 0 };
assert_eq!(val, expected);
}
}
#[test]
fn test_invalid_levels() {
let result = BinaryQuantizer::new(0.0, 1, 0);
assert!(result.is_err());
}
#[test]
fn test_getters() {
let bq = BinaryQuantizer::new(0.5, 10, 20).unwrap();
assert_eq!(bq.threshold(), 0.5);
assert_eq!(bq.low(), 10);
assert_eq!(bq.high(), 20);
}
#[test]
fn test_invalid_parameters() {
// low == high
let result = BinaryQuantizer::new(0.0, 5, 5);
assert!(result.is_err());
// low > high
let result = BinaryQuantizer::new(0.0, 6, 5);
assert!(result.is_err());
}
#[test]
fn test_empty_input() {
let bq = BinaryQuantizer::new(0.0, 0, 1).unwrap();
let input: Vec<f32> = vec![];
let result = bq.quantize(&input).unwrap();
assert!(result.is_empty());
// Dequantize empty
let empty_codes: Vec<u8> = vec![];
let reconstructed = bq.dequantize(&empty_codes).unwrap();
assert!(reconstructed.is_empty());
}
#[test]
fn test_nan_threshold_rejected() {
let result = BinaryQuantizer::new(f32::NAN, 0, 1);
assert!(result.is_err());
}
}