ruvector-cnn 2.0.6

CNN feature extraction for image embeddings with SIMD acceleration
Documentation
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
//! ARM NEON INT8 SIMD Kernels for Quantized CNN Operations
//!
//! Implements high-performance INT8 convolution using ARM NEON intrinsics:
//! - `vmlal_s8`: Multiply-accumulate i8 to i16
//! - `vmlal_s16`: Multiply-accumulate i16 to i32
//! - `vdotq_s32`: 4-way dot product (ARMv8.2-A+)
//!
//! Expected speedup: 2-3x over FP32 on ARMv8, 3-4x with dot product instructions

#[cfg(target_arch = "aarch64")]
use std::arch::aarch64::*;

/// NEON INT8 dot product using vmlal instructions
///
/// Computes: sum(a[i] * b[i]) where both are signed i8
/// Uses multiply-accumulate widening for efficient INT8 operations.
///
/// # Safety
/// - Requires ARM NEON support (standard on aarch64)
/// - Input slices must have equal length
#[cfg(target_arch = "aarch64")]
pub unsafe fn dot_product_int8_neon(a: &[i8], b: &[i8]) -> i32 {
    debug_assert_eq!(a.len(), b.len());

    let len = a.len();
    let chunks = len / 16; // Process 16 elements per iteration (2 NEON registers)

    // Accumulator for partial sums (4 x i32)
    let mut acc = vdupq_n_s32(0);

    for i in 0..chunks {
        let offset = i * 16;

        // Load 16 i8 values from each array
        let va_low = vld1_s8(a.as_ptr().add(offset));
        let va_high = vld1_s8(a.as_ptr().add(offset + 8));
        let vb_low = vld1_s8(b.as_ptr().add(offset));
        let vb_high = vld1_s8(b.as_ptr().add(offset + 8));

        // Multiply i8 * i8 -> i16, accumulate to i32
        // vmlal_s8: multiply-accumulate long (widens to i16)
        let prod16_low = vmull_s8(va_low, vb_low);
        let prod16_high = vmull_s8(va_high, vb_high);

        // Pairwise add i16 to i32
        acc = vpadalq_s16(acc, prod16_low);
        acc = vpadalq_s16(acc, prod16_high);
    }

    // Horizontal sum of 4 x i32
    let sum_pair = vpadd_s32(vget_low_s32(acc), vget_high_s32(acc));
    let sum_final = vpadd_s32(sum_pair, sum_pair);
    let mut result = vget_lane_s32(sum_final, 0);

    // Handle remainder
    for i in (chunks * 16)..len {
        result += (a[i] as i32) * (b[i] as i32);
    }

    result
}

/// NEON INT8 2D convolution with per-channel quantization
///
/// Processes 4 output channels simultaneously using NEON INT8 operations.
///
/// # Arguments
/// Same as AVX2 version, processes 4 channels at a time instead of 8.
///
/// # Safety
/// Requires ARM NEON support (standard on aarch64).
#[cfg(target_arch = "aarch64")]
pub unsafe fn conv2d_int8_neon(
    input: &[u8],
    input_zero_point: i32,
    kernel: &[i8],
    bias_i32: &[i32],
    output: &mut [i32],
    in_h: usize,
    in_w: usize,
    in_c: usize,
    out_c: usize,
    stride: usize,
    padding: usize,
) {
    let out_h = (in_h + 2 * padding - 3) / stride + 1;
    let out_w = (in_w + 2 * padding - 3) / stride + 1;

    let out_c_chunks = out_c / 4; // Process 4 output channels at a time
    let kernel_size = 3;

    // Pre-compute zero-point correction
    let mut weight_sums = vec![0i32; out_c];
    for oc in 0..out_c {
        let mut sum = 0i32;
        for ic in 0..in_c {
            for kh in 0..3 {
                for kw in 0..3 {
                    let idx = (oc * in_c + ic) * 9 + kh * 3 + kw;
                    sum += kernel[idx] as i32;
                }
            }
        }
        weight_sums[oc] = sum;
    }

    for oh in 0..out_h {
        for ow in 0..out_w {
            let out_spatial_idx = oh * out_w + ow;

            // Process 4 output channels at once
            for oc_chunk in 0..out_c_chunks {
                let oc_base = oc_chunk * 4;

                // Initialize accumulators
                let mut acc = [0i32; 4];
                for i in 0..4 {
                    let oc = oc_base + i;
                    acc[i] = bias_i32[oc] - input_zero_point * weight_sums[oc];
                }

                // Convolve over 3x3 kernel
                for kh in 0..kernel_size {
                    for kw in 0..kernel_size {
                        let ih = (oh * stride + kh) as isize - padding as isize;
                        let iw = (ow * stride + kw) as isize - 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;

                            // Process input channels in groups of 16
                            let ic_chunks = in_c / 16;

                            for ic_chunk in 0..ic_chunks {
                                let ic_base = ic_chunk * 16;

                                // Load 16 input activations (u8)
                                let input_base = (ih * in_w + iw) * in_c + ic_base;
                                let input_u8 = vld1q_u8(input.as_ptr().add(input_base));

                                // Convert u8 to i8 by subtracting 128 (treat as signed)
                                let offset = vdupq_n_u8(128);
                                let input_shifted = vsubq_u8(input_u8, offset);
                                let input_i8_low = vreinterpret_s8_u8(vget_low_u8(input_shifted));
                                let input_i8_high = vreinterpret_s8_u8(vget_high_u8(input_shifted));

                                // For each output channel
                                for i in 0..4 {
                                    let oc = oc_base + i;

                                    // Load 16 weights (i8)
                                    let mut w_buf = [0i8; 16];
                                    for j in 0..16 {
                                        let k_idx = (oc * in_c + ic_base + j) * 9 + kh * 3 + kw;
                                        w_buf[j] = kernel[k_idx];
                                    }
                                    let kernel_i8_low = vld1_s8(w_buf.as_ptr());
                                    let kernel_i8_high = vld1_s8(w_buf.as_ptr().add(8));

                                    // Multiply and accumulate
                                    let prod16_low = vmull_s8(input_i8_low, kernel_i8_low);
                                    let prod16_high = vmull_s8(input_i8_high, kernel_i8_high);

                                    // Sum to i32
                                    let sum_low = vpaddlq_s16(prod16_low);
                                    let sum_high = vpaddlq_s16(prod16_high);
                                    let total = vaddq_s32(sum_low, sum_high);

                                    // Horizontal sum
                                    let sum_pair = vpadd_s32(vget_low_s32(total), vget_high_s32(total));
                                    let sum_final = vpadd_s32(sum_pair, sum_pair);
                                    acc[i] += vget_lane_s32(sum_final, 0);
                                }
                            }

                            // Handle remainder input channels
                            for ic in (ic_chunks * 16)..in_c {
                                let input_idx = (ih * in_w + iw) * in_c + ic;
                                let input_val = input[input_idx] as i32;

                                for i in 0..4 {
                                    let oc = oc_base + i;
                                    let k_idx = (oc * in_c + ic) * 9 + kh * 3 + kw;
                                    let w_val = kernel[k_idx] as i32;
                                    acc[i] += input_val * w_val;
                                }
                            }
                        }
                    }
                }

                // Store results
                for i in 0..4 {
                    output[out_spatial_idx * out_c + oc_base + i] = acc[i];
                }
            }

            // Handle remainder output channels
            for oc in (out_c_chunks * 4)..out_c {
                let mut acc = bias_i32[oc] - input_zero_point * weight_sums[oc];

                for kh in 0..kernel_size {
                    for kw in 0..kernel_size {
                        let ih = (oh * stride + kh) as isize - padding as isize;
                        let iw = (ow * stride + kw) as isize - 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..in_c {
                                let input_idx = (ih * in_w + iw) * in_c + ic;
                                let k_idx = (oc * in_c + ic) * 9 + kh * 3 + kw;
                                acc += (input[input_idx] as i32) * (kernel[k_idx] as i32);
                            }
                        }
                    }
                }

                output[out_spatial_idx * out_c + oc] = acc;
            }
        }
    }
}

/// NEON INT8 depthwise 3x3 convolution
///
/// Processes 4 channels simultaneously using NEON.
///
/// # Safety
/// Requires ARM NEON support.
#[cfg(target_arch = "aarch64")]
pub unsafe fn depthwise_conv2d_int8_neon(
    input: &[u8],
    input_zero_point: i32,
    kernel: &[i8],
    bias_i32: &[i32],
    output: &mut [i32],
    h: usize,
    w: usize,
    c: usize,
    stride: usize,
    padding: usize,
) {
    let out_h = (h + 2 * padding - 3) / stride + 1;
    let out_w = (w + 2 * padding - 3) / stride + 1;
    let c_chunks = c / 4;

    // Pre-compute weight sums
    let mut weight_sums = vec![0i32; c];
    for ch in 0..c {
        let mut sum = 0i32;
        for k in 0..9 {
            sum += kernel[ch * 9 + k] as i32;
        }
        weight_sums[ch] = sum;
    }

    for oh in 0..out_h {
        for ow in 0..out_w {
            // Process 4 channels at a time
            for c_chunk in 0..c_chunks {
                let c_base = c_chunk * 4;

                let mut acc = [0i32; 4];
                for i in 0..4 {
                    let ch = c_base + i;
                    acc[i] = bias_i32[ch] - input_zero_point * weight_sums[ch];
                }

                // 3x3 convolution
                for kh in 0..3 {
                    for kw in 0..3 {
                        let ih = (oh * stride + kh) as isize - padding as isize;
                        let iw = (ow * stride + kw) as isize - padding as isize;

                        if ih >= 0 && ih < h as isize && iw >= 0 && iw < w as isize {
                            let ih = ih as usize;
                            let iw = iw as usize;

                            let input_base = (ih * w + iw) * c + c_base;
                            let kernel_base = c_base * 9 + kh * 3 + kw;

                            // Scalar for 4 channels (could vectorize further)
                            for i in 0..4 {
                                let input_val = input[input_base + i] as i32;
                                let kernel_val = kernel[kernel_base + i * 9] as i32;
                                acc[i] += input_val * kernel_val;
                            }
                        }
                    }
                }

                for i in 0..4 {
                    output[(oh * out_w + ow) * c + c_base + i] = acc[i];
                }
            }

            // Remainder channels
            for ch in (c_chunks * 4)..c {
                let mut acc = bias_i32[ch] - input_zero_point * weight_sums[ch];

                for kh in 0..3 {
                    for kw in 0..3 {
                        let ih = (oh * stride + kh) as isize - padding as isize;
                        let iw = (ow * stride + kw) as isize - padding as isize;

                        if ih >= 0 && ih < h as isize && iw >= 0 && iw < w as isize {
                            let ih = ih as usize;
                            let iw = iw as usize;
                            let input_idx = (ih * w + iw) * c + ch;
                            let kernel_idx = ch * 9 + kh * 3 + kw;
                            acc += (input[input_idx] as i32) * (kernel[kernel_idx] as i32);
                        }
                    }
                }

                output[(oh * out_w + ow) * c + ch] = acc;
            }
        }
    }
}

/// NEON INT8 matrix multiplication
///
/// Uses NEON multiply-accumulate instructions for efficient GEMM.
///
/// # Safety
/// Requires ARM NEON support.
#[cfg(target_arch = "aarch64")]
pub unsafe fn matmul_int8_neon(
    a: &[i8],
    b: &[i8],
    output: &mut [i32],
    m: usize,
    k: usize,
    n: usize,
) {
    debug_assert_eq!(a.len(), m * k);
    debug_assert_eq!(b.len(), k * n);
    debug_assert_eq!(output.len(), m * n);

    // Process 4 output columns at a time
    let n_chunks = n / 4;

    for row in 0..m {
        let a_row = &a[row * k..(row + 1) * k];

        for n_chunk in 0..n_chunks {
            let n_base = n_chunk * 4;

            let mut acc = [0i32; 4];

            // Process K in chunks of 16
            let k_chunks = k / 16;

            for k_chunk in 0..k_chunks {
                let k_base = k_chunk * 16;

                let va_low = vld1_s8(a_row.as_ptr().add(k_base));
                let va_high = vld1_s8(a_row.as_ptr().add(k_base + 8));

                for i in 0..4 {
                    let col = n_base + i;

                    let mut w_buf = [0i8; 16];
                    for j in 0..16 {
                        w_buf[j] = b[(k_base + j) * n + col];
                    }
                    let vb_low = vld1_s8(w_buf.as_ptr());
                    let vb_high = vld1_s8(w_buf.as_ptr().add(8));

                    let prod16_low = vmull_s8(va_low, vb_low);
                    let prod16_high = vmull_s8(va_high, vb_high);

                    let sum_low = vpaddlq_s16(prod16_low);
                    let sum_high = vpaddlq_s16(prod16_high);
                    let total = vaddq_s32(sum_low, sum_high);

                    let sum_pair = vpadd_s32(vget_low_s32(total), vget_high_s32(total));
                    let sum_final = vpadd_s32(sum_pair, sum_pair);
                    acc[i] += vget_lane_s32(sum_final, 0);
                }
            }

            // Remainder K
            for k_idx in (k_chunks * 16)..k {
                let a_val = a_row[k_idx] as i32;
                for i in 0..4 {
                    let col = n_base + i;
                    let b_val = b[k_idx * n + col] as i32;
                    acc[i] += a_val * b_val;
                }
            }

            for i in 0..4 {
                output[row * n + n_base + i] = acc[i];
            }
        }

        // Remainder N
        for col in (n_chunks * 4)..n {
            let mut sum = 0i32;
            for k_idx in 0..k {
                sum += (a_row[k_idx] as i32) * (b[k_idx * n + col] as i32);
            }
            output[row * n + col] = sum;
        }
    }
}

// Non-aarch64 stubs
#[cfg(not(target_arch = "aarch64"))]
pub unsafe fn dot_product_int8_neon(_a: &[i8], _b: &[i8]) -> i32 {
    0
}

#[cfg(not(target_arch = "aarch64"))]
pub unsafe fn conv2d_int8_neon(
    _input: &[u8],
    _input_zero_point: i32,
    _kernel: &[i8],
    _bias_i32: &[i32],
    _output: &mut [i32],
    _in_h: usize,
    _in_w: usize,
    _in_c: usize,
    _out_c: usize,
    _stride: usize,
    _padding: usize,
) {
}

#[cfg(not(target_arch = "aarch64"))]
pub unsafe fn depthwise_conv2d_int8_neon(
    _input: &[u8],
    _input_zero_point: i32,
    _kernel: &[i8],
    _bias_i32: &[i32],
    _output: &mut [i32],
    _h: usize,
    _w: usize,
    _c: usize,
    _stride: usize,
    _padding: usize,
) {
}

#[cfg(not(target_arch = "aarch64"))]
pub unsafe fn matmul_int8_neon(
    _a: &[i8],
    _b: &[i8],
    _output: &mut [i32],
    _m: usize,
    _k: usize,
    _n: usize,
) {
}

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

    #[test]
    fn test_dot_product_int8_neon() {
        let a: Vec<i8> = (0..32).map(|i| (i % 64) as i8).collect();
        let b: Vec<i8> = (0..32).map(|i| ((i % 64) as i8)).collect();

        #[cfg(target_arch = "aarch64")]
        {
            unsafe {
                let result = dot_product_int8_neon(&a, &b);

                let mut expected = 0i32;
                for i in 0..32 {
                    expected += (a[i] as i32) * (b[i] as i32);
                }

                assert_eq!(result, expected);
            }
        }
    }
}