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
//! Quantization-Aware Training (QAT) module.
//!
//! This module provides fake quantization operators for simulating quantization
//! effects during training. The forward pass uses quantized values while gradients
//! flow through using the Straight-Through Estimator (STE).
//!
//! # Overview
//!
//! ```text
//! Training with QAT:
//!
//! ┌─────────────┐ ┌──────────────┐ ┌─────────────┐
//! │ Input │ ──► │ FakeQuantize │ ──► │ Output │
//! │ (f32) │ │ (simulate) │ │ (f32) │
//! └─────────────┘ └──────────────┘ └─────────────┘
//! │
//! ┌──────┴──────┐
//! │ STE Gradient │
//! │ (pass-thru) │
//! └─────────────┘
//! ```
//!
//! # References
//!
//! - Jacob, B., et al. (2018). Quantization and Training of Neural Networks
//! for Efficient Integer-Arithmetic-Only Inference. CVPR.
//! - Krishnamoorthi, R. (2018). Quantizing deep convolutional networks for
//! efficient inference. arXiv:1806.08342.
use crate::autograd::Tensor;
use crate::nn::Module;
/// Fake quantization configuration for QAT.
#[derive(Debug, Clone)]
pub struct FakeQuantConfig {
/// Number of bits for quantization (4 or 8 typical)
pub bits: usize,
/// Whether to use symmetric quantization (centered at 0)
pub symmetric: bool,
/// Whether to learn scale/zero-point (LSQ)
pub learnable: bool,
/// Observation method for scale calibration
pub observer: ObserverMethod,
}
/// Method for observing tensor statistics for quantization.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ObserverMethod {
/// Min-max observation
MinMax,
/// Percentile observation (reduces outlier impact)
Percentile,
/// Moving average min-max
MovingAverage,
/// Mean + std deviation based (for weights)
MeanStd,
}
impl Default for FakeQuantConfig {
fn default() -> Self {
Self {
bits: 8,
symmetric: true,
learnable: false,
observer: ObserverMethod::MinMax,
}
}
}
impl FakeQuantConfig {
/// Create 8-bit symmetric quantization config.
#[must_use]
pub fn int8() -> Self {
Self {
bits: 8,
symmetric: true,
learnable: false,
observer: ObserverMethod::MinMax,
}
}
/// Create 4-bit symmetric quantization config.
#[must_use]
pub fn int4() -> Self {
Self {
bits: 4,
symmetric: true,
learnable: false,
observer: ObserverMethod::MinMax,
}
}
/// Enable learnable scale quantization (LSQ).
#[must_use]
pub fn with_learnable(mut self) -> Self {
self.learnable = true;
self
}
/// Set observation method.
#[must_use]
pub fn with_observer(mut self, observer: ObserverMethod) -> Self {
self.observer = observer;
self
}
/// Compute quantization range for given bits.
#[must_use]
pub fn quant_range(&self) -> (f32, f32) {
if self.symmetric {
let max = (1 << (self.bits - 1)) - 1;
(-(max as f32), max as f32)
} else {
let max = (1 << self.bits) - 1;
(0.0, max as f32)
}
}
}
/// Observer for tracking tensor statistics.
#[derive(Debug, Clone)]
pub struct QuantObserver {
/// Observed minimum value
min_val: f32,
/// Observed maximum value
max_val: f32,
/// Running average of min (for moving average)
avg_min: f32,
/// Running average of max (for moving average)
avg_max: f32,
/// Number of observations
count: usize,
/// Averaging constant
averaging_constant: f32,
/// Observer method
method: ObserverMethod,
}
impl QuantObserver {
/// Create a new observer.
#[must_use]
pub fn new(method: ObserverMethod) -> Self {
Self {
min_val: f32::INFINITY,
max_val: f32::NEG_INFINITY,
avg_min: 0.0,
avg_max: 0.0,
count: 0,
averaging_constant: 0.01,
method,
}
}
/// Update statistics from tensor data.
pub fn observe(&mut self, data: &[f32]) {
if data.is_empty() {
return;
}
match self.method {
ObserverMethod::MinMax => {
let min = data.iter().fold(f32::INFINITY, |a, &b| a.min(b));
let max = data.iter().fold(f32::NEG_INFINITY, |a, &b| a.max(b));
self.min_val = self.min_val.min(min);
self.max_val = self.max_val.max(max);
}
ObserverMethod::Percentile => {
// Use 0.1% and 99.9% percentiles
let mut sorted = data.to_vec();
sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
let p_low = (sorted.len() as f32 * 0.001).floor() as usize;
let p_high = ((sorted.len() as f32 * 0.999).ceil() as usize).min(sorted.len() - 1);
let min = sorted[p_low];
let max = sorted[p_high];
self.min_val = self.min_val.min(min);
self.max_val = self.max_val.max(max);
}
ObserverMethod::MovingAverage => {
let min = data.iter().fold(f32::INFINITY, |a, &b| a.min(b));
let max = data.iter().fold(f32::NEG_INFINITY, |a, &b| a.max(b));
if self.count == 0 {
self.avg_min = min;
self.avg_max = max;
} else {
let c = self.averaging_constant;
self.avg_min = (1.0 - c) * self.avg_min + c * min;
self.avg_max = (1.0 - c) * self.avg_max + c * max;
}
self.min_val = self.avg_min;
self.max_val = self.avg_max;
}
ObserverMethod::MeanStd => {
let mean: f32 = data.iter().sum::<f32>() / data.len() as f32;
let variance: f32 =
data.iter().map(|x| (x - mean).powi(2)).sum::<f32>() / data.len() as f32;
let std = variance.sqrt();
// Use 3-sigma range
let min = mean - 3.0 * std;
let max = mean + 3.0 * std;
self.min_val = self.min_val.min(min);
self.max_val = self.max_val.max(max);
}
}
self.count += 1;
}
/// Get observed range.
#[must_use]
pub fn range(&self) -> (f32, f32) {
(self.min_val, self.max_val)
}
/// Reset observer.
pub fn reset(&mut self) {
self.min_val = f32::INFINITY;
self.max_val = f32::NEG_INFINITY;
self.avg_min = 0.0;
self.avg_max = 0.0;
self.count = 0;
}
/// Compute scale and zero-point for asymmetric quantization.
#[must_use]
pub fn compute_qparams(&self, config: &FakeQuantConfig) -> (f32, f32) {
let (qmin, qmax) = config.quant_range();
let (min_val, max_val) = self.range();
if config.symmetric {
// Symmetric: scale = max(|min|, |max|) / qmax
let max_abs = min_val.abs().max(max_val.abs());
let scale = if max_abs > 0.0 { max_abs / qmax } else { 1.0 };
(scale, 0.0)
} else {
// Asymmetric: scale = (max - min) / (qmax - qmin)
let scale = if (max_val - min_val).abs() > 1e-10 {
(max_val - min_val) / (qmax - qmin)
} else {
1.0
};
let zero_point = qmin - min_val / scale;
(scale, zero_point)
}
}
}
/// Fake quantization layer for QAT.
///
/// Applies fake quantization during forward pass (simulates int8/int4)
/// while allowing gradients to flow through using STE.
#[derive(Debug)]
pub struct FakeQuantize {
config: FakeQuantConfig,
observer: QuantObserver,
/// Current scale
scale: f32,
/// Current zero point
zero_point: f32,
/// Whether in calibration mode
calibrating: bool,
}
impl FakeQuantize {
/// Create fake quantizer with config.
#[must_use]
pub fn new(config: FakeQuantConfig) -> Self {
let observer = QuantObserver::new(config.observer);
Self {
config,
observer,
scale: 1.0,
zero_point: 0.0,
calibrating: true,
}
}
/// Enable calibration mode (observe statistics).
pub fn enable_calibration(&mut self) {
self.calibrating = true;
}
/// Disable calibration and freeze quantization params.
pub fn disable_calibration(&mut self) {
self.calibrating = false;
let (scale, zp) = self.observer.compute_qparams(&self.config);
self.scale = scale;
self.zero_point = zp;
}
/// Perform fake quantization on data.
pub fn fake_quantize(&mut self, data: &[f32]) -> Vec<f32> {
if self.calibrating {
self.observer.observe(data);
let (scale, zp) = self.observer.compute_qparams(&self.config);
self.scale = scale;
self.zero_point = zp;
}
let (qmin, qmax) = self.config.quant_range();
data.iter()
.map(|&x| {
// Quantize
let q = (x / self.scale + self.zero_point).round().clamp(qmin, qmax);
// Dequantize (fake quantize = quantize then dequantize)
(q - self.zero_point) * self.scale
})
.collect()
}
/// Get current scale.
#[must_use]
pub fn scale(&self) -> f32 {
self.scale
}
/// Get current zero point.
#[must_use]
pub fn zero_point(&self) -> f32 {
self.zero_point
}
/// Get config.
#[must_use]
pub fn config(&self) -> &FakeQuantConfig {
&self.config
}
}
impl Module for FakeQuantize {
fn forward(&self, input: &Tensor) -> Tensor {
let data = input.data();
let (qmin, qmax) = self.config.quant_range();
let output_data: Vec<f32> = data
.iter()
.map(|&x| {
let q = (x / self.scale + self.zero_point).round().clamp(qmin, qmax);
(q - self.zero_point) * self.scale
})
.collect();
Tensor::new(&output_data, input.shape())
}
fn parameters(&self) -> Vec<&Tensor> {
vec![]
}
fn parameters_mut(&mut self) -> Vec<&mut Tensor> {
vec![]
}
}
/// Quantized linear layer for inference.
///
/// Stores pre-computed quantization parameters for fast inference
/// without floating point multiplication.
#[derive(Debug)]
pub struct QuantizedLinear {
/// Weight scale
pub(crate) weight_scale: f32,
/// Input scale
pub(crate) input_scale: f32,
/// Output scale
pub(crate) output_scale: f32,
/// Quantized weights (i8)
pub(crate) weights_q: Vec<i8>,
/// Quantized bias (i32)
pub(crate) bias_q: Option<Vec<i32>>,
/// Input dimension
pub(crate) in_features: usize,
/// Output dimension
pub(crate) out_features: usize,
}
#[path = "quantization_linear.rs"]
mod quantization_linear;
pub use quantization_linear::*;
#[path = "quantization_tests.rs"]
mod quantization_tests;