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
//! WASM SIMD128 INT8 Kernels for Quantized CNN Operations
//!
//! Implements INT8 convolution using WebAssembly SIMD128 instructions:
//! - `i8x16` operations for 16-element INT8 vectors
//! - Widening multiply-add for accumulation
//! - Efficient for edge/browser deployment
//!
//! Expected speedup: 1.5-2.5x over scalar on WASM runtimes with SIMD support

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

/// WASM SIMD128 INT8 dot product
///
/// Computes: sum(a[i] * b[i]) using WASM SIMD128 i8x16 operations
///
/// # Safety
/// - Requires WASM SIMD128 support
/// - Input slices must have equal length
#[cfg(target_arch = "wasm32")]
pub unsafe fn dot_product_int8_wasm(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

    // Accumulator (4 x i32)
    let mut acc = i32x4_splat(0);

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

        // Load 16 i8 values
        let va = v128_load(a.as_ptr().add(offset) as *const v128);
        let vb = v128_load(b.as_ptr().add(offset) as *const v128);

        // Extract low and high 8 bytes
        let va_low = i16x8_extend_low_i8x16(va);
        let va_high = i16x8_extend_high_i8x16(va);
        let vb_low = i16x8_extend_low_i8x16(vb);
        let vb_high = i16x8_extend_high_i8x16(vb);

        // Multiply i16 * i16 -> i16
        let prod_low = i16x8_mul(va_low, vb_low);
        let prod_high = i16x8_mul(va_high, vb_high);

        // Extend to i32 and accumulate
        let prod32_low_lo = i32x4_extend_low_i16x8(prod_low);
        let prod32_low_hi = i32x4_extend_high_i16x8(prod_low);
        let prod32_high_lo = i32x4_extend_low_i16x8(prod_high);
        let prod32_high_hi = i32x4_extend_high_i16x8(prod_high);

        acc = i32x4_add(acc, prod32_low_lo);
        acc = i32x4_add(acc, prod32_low_hi);
        acc = i32x4_add(acc, prod32_high_lo);
        acc = i32x4_add(acc, prod32_high_hi);
    }

    // Horizontal sum of 4 x i32
    let mut result = i32x4_extract_lane::<0>(acc)
        + i32x4_extract_lane::<1>(acc)
        + i32x4_extract_lane::<2>(acc)
        + i32x4_extract_lane::<3>(acc);

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

    result
}

/// WASM SIMD128 INT8 2D convolution
///
/// Processes 4 output channels at a time using WASM SIMD128.
///
/// # Safety
/// Requires WASM SIMD128 support.
#[cfg(target_arch = "wasm32")]
pub unsafe fn conv2d_int8_wasm(
    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;
    let kernel_size = 3;

    // Pre-compute weight sums
    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
            for oc_chunk in 0..out_c_chunks {
                let oc_base = oc_chunk * 4;

                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];
                }

                // 3x3 convolution
                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;
                                let input_base = (ih * in_w + iw) * in_c + ic_base;

                                // Load 16 u8 inputs and convert to i8
                                let input_u8 = v128_load(input.as_ptr().add(input_base) as *const v128);
                                let offset = u8x16_splat(128);
                                let input_shifted = u8x16_sub(input_u8, offset);

                                for i in 0..4 {
                                    let oc = oc_base + i;

                                    // Load 16 i8 weights
                                    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 = v128_load(w_buf.as_ptr() as *const v128);

                                    // Reinterpret input_shifted as i8x16
                                    let input_i8 = input_shifted;

                                    // Extend and multiply
                                    let input_low = i16x8_extend_low_i8x16(input_i8);
                                    let input_high = i16x8_extend_high_i8x16(input_i8);
                                    let kernel_low = i16x8_extend_low_i8x16(kernel_i8);
                                    let kernel_high = i16x8_extend_high_i8x16(kernel_i8);

                                    let prod_low = i16x8_mul(input_low, kernel_low);
                                    let prod_high = i16x8_mul(input_high, kernel_high);

                                    // Extend to i32 and sum
                                    let sum_ll = i32x4_extend_low_i16x8(prod_low);
                                    let sum_lh = i32x4_extend_high_i16x8(prod_low);
                                    let sum_hl = i32x4_extend_low_i16x8(prod_high);
                                    let sum_hh = i32x4_extend_high_i16x8(prod_high);

                                    let total1 = i32x4_add(sum_ll, sum_lh);
                                    let total2 = i32x4_add(sum_hl, sum_hh);
                                    let total = i32x4_add(total1, total2);

                                    // Horizontal sum
                                    let sum = i32x4_extract_lane::<0>(total)
                                        + i32x4_extract_lane::<1>(total)
                                        + i32x4_extract_lane::<2>(total)
                                        + i32x4_extract_lane::<3>(total);

                                    acc[i] += sum;
                                }
                            }

                            // 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;
                                }
                            }
                        }
                    }
                }

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

            // 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;
            }
        }
    }
}

/// WASM SIMD128 depthwise convolution
///
/// # Safety
/// Requires WASM SIMD128 support.
#[cfg(target_arch = "wasm32")]
pub unsafe fn depthwise_conv2d_int8_wasm(
    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;

    // 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 {
            for ch in 0..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;
            }
        }
    }
}

/// WASM SIMD128 matrix multiplication
///
/// # Safety
/// Requires WASM SIMD128 support.
#[cfg(target_arch = "wasm32")]
pub unsafe fn matmul_int8_wasm(
    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);

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

        for col in 0..n {
            let mut sum = 0i32;

            // 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 = v128_load(a_row.as_ptr().add(k_base) as *const v128);

                let mut w_buf = [0i8; 16];
                for j in 0..16 {
                    w_buf[j] = b[(k_base + j) * n + col];
                }
                let vb = v128_load(w_buf.as_ptr() as *const v128);

                let va_low = i16x8_extend_low_i8x16(va);
                let va_high = i16x8_extend_high_i8x16(va);
                let vb_low = i16x8_extend_low_i8x16(vb);
                let vb_high = i16x8_extend_high_i8x16(vb);

                let prod_low = i16x8_mul(va_low, vb_low);
                let prod_high = i16x8_mul(va_high, vb_high);

                let sum_ll = i32x4_extend_low_i16x8(prod_low);
                let sum_lh = i32x4_extend_high_i16x8(prod_low);
                let sum_hl = i32x4_extend_low_i16x8(prod_high);
                let sum_hh = i32x4_extend_high_i16x8(prod_high);

                let total1 = i32x4_add(sum_ll, sum_lh);
                let total2 = i32x4_add(sum_hl, sum_hh);
                let total = i32x4_add(total1, total2);

                sum += i32x4_extract_lane::<0>(total)
                    + i32x4_extract_lane::<1>(total)
                    + i32x4_extract_lane::<2>(total)
                    + i32x4_extract_lane::<3>(total);
            }

            // Remainder K
            for k_idx in (k_chunks * 16)..k {
                sum += (a_row[k_idx] as i32) * (b[k_idx * n + col] as i32);
            }

            output[row * n + col] = sum;
        }
    }
}

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

#[cfg(not(target_arch = "wasm32"))]
pub unsafe fn conv2d_int8_wasm(
    _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 = "wasm32"))]
pub unsafe fn depthwise_conv2d_int8_wasm(
    _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 = "wasm32"))]
pub unsafe fn matmul_int8_wasm(
    _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_wasm() {
        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 = "wasm32")]
        {
            unsafe {
                let result = dot_product_int8_wasm(&a, &b);

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

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