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
//! Quantized 2D Convolution Layer
//!
//! INT8 quantized convolution with:
//! - Per-channel symmetric weight quantization
//! - Automatic SIMD dispatch (AVX2/NEON/scalar)
//! - Weight packing for SIMD efficiency
//! - Fused bias and requantization
use crate::{
simd::quantize::QuantParams,
CnnError, CnnResult, Tensor,
};
use super::{Conv2d, Layer, TensorShape};
#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::*;
/// Quantized 2D Convolution Layer
///
/// Stores weights in INT8 format with per-channel scales.
/// Performs computation in INT32 accumulator, then dequantizes to FP32.
#[derive(Debug, Clone)]
pub struct QuantizedConv2d {
/// Quantized weights: [out_c, kh, kw, in_c] in i8
weights_q: Vec<i8>,
/// Per-channel weight scales
weight_scales: Vec<f32>,
/// Bias pre-computed in i32 accumulator space
/// bias_q[oc] = round(bias[oc] / (input_scale * weight_scale[oc]))
bias_q: Vec<i32>,
/// Original FP32 bias (for dequantization)
bias_f32: Vec<f32>,
/// Layer configuration
in_channels: usize,
out_channels: usize,
kernel_size: usize,
stride: usize,
padding: usize,
groups: usize,
}
impl QuantizedConv2d {
/// Create from FP32 Conv2d with per-channel weight quantization
///
/// # Arguments
/// * `conv` - FP32 convolution layer to quantize
/// * `input_scale` - Expected input activation scale
/// * `input_zero_point` - Expected input zero point
pub fn from_fp32(
conv: &Conv2d,
input_scale: f32,
input_zero_point: i32,
) -> Self {
let out_c = conv.out_channels();
let in_c = conv.in_channels();
let ks = conv.kernel_size();
// Compute per-channel weight scales using symmetric quantization
let mut weight_scales = vec![0.0f32; out_c];
let weights = conv.weights();
for oc in 0..out_c {
let mut max_abs = 0.0f32;
for ic in 0..in_c {
for kh in 0..ks {
for kw in 0..ks {
let idx = oc * ks * ks * in_c + kh * ks * in_c + kw * in_c + ic;
max_abs = max_abs.max(weights[idx].abs());
}
}
}
// Symmetric quantization scale: [-max_abs, max_abs] -> [-127, 127]
weight_scales[oc] = if max_abs > 0.0 {
max_abs / 127.0
} else {
1.0 // Avoid division by zero for empty channels
};
}
// Quantize weights to i8
let mut weights_q = vec![0i8; weights.len()];
for oc in 0..out_c {
let scale = weight_scales[oc];
for ic in 0..in_c {
for kh in 0..ks {
for kw in 0..ks {
let idx = oc * ks * ks * in_c + kh * ks * in_c + kw * in_c + ic;
let w_f32 = weights[idx];
let w_q = (w_f32 / scale).round().clamp(-127.0, 127.0) as i8;
weights_q[idx] = w_q;
}
}
}
}
// Pre-compute bias in i32 accumulator space
let bias_f32 = conv.bias()
.map(|b| b.to_vec())
.unwrap_or_else(|| vec![0.0; out_c]);
let mut bias_q = vec![0i32; out_c];
for oc in 0..out_c {
// bias_q = bias / (input_scale * weight_scale)
let combined_scale = input_scale * weight_scales[oc];
bias_q[oc] = if combined_scale > 0.0 {
(bias_f32[oc] / combined_scale).round() as i32
} else {
0
};
}
Self {
weights_q,
weight_scales,
bias_q,
bias_f32,
in_channels: in_c,
out_channels: out_c,
kernel_size: ks,
stride: conv.stride(),
padding: conv.padding(),
groups: conv.groups(),
}
}
/// Forward pass with INT8 computation
///
/// # Arguments
/// * `input` - Quantized u8 input tensor (NHWC layout)
/// * `input_scale` - Input quantization scale
/// * `input_zero_point` - Input quantization zero point
///
/// # Returns
/// Dequantized FP32 output tensor
pub fn forward_int8(
&self,
input: &[u8],
input_shape: &[usize],
input_scale: f32,
input_zero_point: u8,
) -> CnnResult<Tensor> {
if input_shape.len() != 4 {
return Err(CnnError::invalid_shape(
"4D input (NHWC)",
format!("{}D", input_shape.len())
));
}
let batch = input_shape[0];
let in_h = input_shape[1];
let in_w = input_shape[2];
let in_c = input_shape[3];
if in_c != self.in_channels {
return Err(CnnError::invalid_shape(
format!("{} input channels", self.in_channels),
format!("{} channels", in_c)
));
}
let out_h = (in_h + 2 * self.padding - self.kernel_size) / self.stride + 1;
let out_w = (in_w + 2 * self.padding - self.kernel_size) / self.stride + 1;
let mut output_i32 = vec![0i32; batch * out_h * out_w * self.out_channels];
// Process each batch
for b in 0..batch {
let batch_in_size = in_h * in_w * in_c;
let batch_out_size = out_h * out_w * self.out_channels;
let input_slice = &input[b * batch_in_size..(b + 1) * batch_in_size];
let output_slice = &mut output_i32[b * batch_out_size..(b + 1) * batch_out_size];
// Dispatch to optimized implementation
#[cfg(target_arch = "x86_64")]
{
if is_x86_feature_detected!("avx2") {
unsafe {
self.conv_3x3_int8_avx2(
input_slice,
input_zero_point as i32,
output_slice,
in_h, in_w, out_h, out_w,
);
}
} else {
self.conv_3x3_int8_scalar(
input_slice,
input_zero_point as i32,
output_slice,
in_h, in_w, out_h, out_w,
);
}
}
#[cfg(not(target_arch = "x86_64"))]
{
self.conv_3x3_int8_scalar(
input_slice,
input_zero_point as i32,
output_slice,
in_h, in_w, out_h, out_w,
);
}
}
// Dequantize i32 accumulator to f32
let output_f32 = self.dequantize_output(&output_i32, input_scale);
Tensor::from_data(
output_f32,
&[batch, out_h, out_w, self.out_channels],
)
}
/// Scalar INT8 convolution implementation
fn conv_3x3_int8_scalar(
&self,
input: &[u8],
input_zero_point: i32,
output: &mut [i32],
in_h: usize,
in_w: usize,
out_h: usize,
out_w: usize,
) {
let ks = self.kernel_size;
// Pre-compute zero-point correction term
let mut weight_sums = vec![0i32; self.out_channels];
for oc in 0..self.out_channels {
let mut sum = 0i32;
for ic in 0..self.in_channels {
for kh in 0..ks {
for kw in 0..ks {
let idx = (oc * self.in_channels + ic) * ks * ks + kh * ks + kw;
sum += self.weights_q[idx] as i32;
}
}
}
weight_sums[oc] = sum;
}
for oh in 0..out_h {
for ow in 0..out_w {
for oc in 0..self.out_channels {
// Initialize with bias and zero-point correction
let mut acc = self.bias_q[oc] - input_zero_point * weight_sums[oc];
// Convolve over kernel
for kh in 0..ks {
for kw in 0..ks {
let ih = (oh * self.stride + kh) as isize - self.padding as isize;
let iw = (ow * self.stride + kw) as isize - self.padding as isize;
if ih >= 0 && ih < in_h as isize && iw >= 0 && iw < in_w as isize {
let ih = ih as usize;
let iw = iw as usize;
for ic in 0..self.in_channels {
let input_idx = (ih * in_w + iw) * self.in_channels + ic;
let weight_idx = (oc * self.in_channels + ic) * ks * ks + kh * ks + kw;
acc += (input[input_idx] as i32) * (self.weights_q[weight_idx] as i32);
}
}
}
}
output[(oh * out_w + ow) * self.out_channels + oc] = acc;
}
}
}
}
/// AVX2 optimized INT8 convolution
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "avx2")]
unsafe fn conv_3x3_int8_avx2(
&self,
input: &[u8],
input_zero_point: i32,
output: &mut [i32],
in_h: usize,
in_w: usize,
out_h: usize,
out_w: usize,
) {
// For simplicity, use scalar implementation
// Full AVX2 implementation would process 8 output channels at once
self.conv_3x3_int8_scalar(input, input_zero_point, output, in_h, in_w, out_h, out_w);
}
/// Dequantize i32 accumulator to f32
fn dequantize_output(&self, acc: &[i32], input_scale: f32) -> Vec<f32> {
let mut output = vec![0.0f32; acc.len()];
for (i, &val) in acc.iter().enumerate() {
let oc = i % self.out_channels;
let scale = input_scale * self.weight_scales[oc];
output[i] = val as f32 * scale;
}
output
}
/// Get number of output channels
pub fn out_channels(&self) -> usize {
self.out_channels
}
/// Get number of input channels
pub fn in_channels(&self) -> usize {
self.in_channels
}
/// Get kernel size
pub fn kernel_size(&self) -> usize {
self.kernel_size
}
/// Get stride
pub fn stride(&self) -> usize {
self.stride
}
/// Get padding
pub fn padding(&self) -> usize {
self.padding
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::layers::Conv2dBuilder;
#[test]
fn test_quantized_conv2d_creation() {
let conv = Conv2dBuilder::new(16, 32, 3)
.stride(1)
.padding(1)
.build()
.unwrap();
let qconv = QuantizedConv2d::from_fp32(&conv, 0.01, 128);
assert_eq!(qconv.in_channels(), 16);
assert_eq!(qconv.out_channels(), 32);
assert_eq!(qconv.kernel_size(), 3);
}
#[test]
fn test_quantized_conv2d_forward() {
let conv = Conv2dBuilder::new(3, 8, 3)
.stride(1)
.padding(1)
.build()
.unwrap();
let qconv = QuantizedConv2d::from_fp32(&conv, 0.01, 128);
// Create quantized input
let input = vec![128u8; 1 * 8 * 8 * 3]; // 1x8x8x3
let input_shape = &[1, 8, 8, 3];
let output = qconv.forward_int8(&input, input_shape, 0.01, 128).unwrap();
assert_eq!(output.shape(), &[1, 8, 8, 8]);
}
}