numr 0.9.0

High-performance numerical computing with multi-backend GPU acceleration (CPU/CUDA/WebGPU)
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
485
486
487
488
489
490
491
492
493
494
//! NEON 2D convolution kernels for ARM64
//!
//! Provides vectorized implementations of conv2d and depthwise_conv2d using
//! 128-bit NEON registers.
//!
//! # SIMD Strategy
//!
//! `conv2d` vectorizes over input channels using FMA instructions:
//! - f32: Process 4 input channels per iteration
//! - f64: Process 2 input channels per iteration
//!
//! For depthwise convolution (1 channel per group), vectorizes over output
//! width. (`conv1d` lives in the `conv1d` submodule, which vectorizes over
//! output positions for every shape.)

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

use crate::ops::conv_common::Conv2dParams;

// ============================================================================
// Conv2d
// ============================================================================

/// NEON conv2d for f32
///
/// # Safety
/// - All pointers must be valid and properly aligned
/// - Arrays must have sufficient size for the operation
#[cfg(target_arch = "aarch64")]
#[target_feature(enable = "neon")]
pub unsafe fn conv2d_f32(
    input: *const f32,
    weight: *const f32,
    bias: Option<*const f32>,
    output: *mut f32,
    params: Conv2dParams,
) {
    let lanes = 4;
    let c_in_per_group = params.c_in / params.groups;
    let c_out_per_group = params.c_out / params.groups;
    let chunks = c_in_per_group / lanes;

    let in_spatial = params.height * params.width;
    let out_spatial = params.output_h * params.output_w;
    let kernel_spatial = params.kernel_h * params.kernel_w;

    for b in 0..params.batch {
        for g in 0..params.groups {
            let c_in_start = g * c_in_per_group;
            let c_out_start = g * c_out_per_group;

            for oc in 0..c_out_per_group {
                let out_c = c_out_start + oc;

                let bias_val = if let Some(bias_ptr) = bias {
                    *bias_ptr.add(out_c)
                } else {
                    0.0
                };

                for oh in 0..params.output_h {
                    for ow in 0..params.output_w {
                        let mut acc = vdupq_n_f32(bias_val);
                        let mut scalar_acc = 0.0f32;

                        for kh in 0..params.kernel_h {
                            for kw in 0..params.kernel_w {
                                let ih_signed = (oh * params.stride_h) as isize
                                    + (kh * params.dilation_h) as isize
                                    - params.pad_top as isize;
                                let iw_signed = (ow * params.stride_w) as isize
                                    + (kw * params.dilation_w) as isize
                                    - params.pad_left as isize;
                                if ih_signed < 0
                                    || (ih_signed as usize) >= params.height
                                    || iw_signed < 0
                                    || (iw_signed as usize) >= params.width
                                {
                                    continue;
                                }
                                let ih = ih_signed as usize;
                                let iw = iw_signed as usize;

                                // Vectorize over input channels
                                for chunk in 0..chunks {
                                    let ic_base = chunk * lanes;

                                    // Gather 4 input values from different channels
                                    let mut in_arr = [0.0f32; 4];
                                    for lane in 0..lanes {
                                        let ic = ic_base + lane;
                                        let in_idx = b * params.c_in * in_spatial
                                            + (c_in_start + ic) * in_spatial
                                            + ih * params.width
                                            + iw;
                                        in_arr[lane] = *input.add(in_idx);
                                    }
                                    let v_in = vld1q_f32(in_arr.as_ptr());

                                    // Gather 4 weight values
                                    let mut w_arr = [0.0f32; 4];
                                    for lane in 0..lanes {
                                        let ic = ic_base + lane;
                                        let w_idx = out_c * c_in_per_group * kernel_spatial
                                            + ic * kernel_spatial
                                            + kh * params.kernel_w
                                            + kw;
                                        w_arr[lane] = *weight.add(w_idx);
                                    }
                                    let v_w = vld1q_f32(w_arr.as_ptr());

                                    acc = vfmaq_f32(acc, v_in, v_w);
                                }

                                // Scalar tail
                                for ic in (chunks * lanes)..c_in_per_group {
                                    let in_idx = b * params.c_in * in_spatial
                                        + (c_in_start + ic) * in_spatial
                                        + ih * params.width
                                        + iw;
                                    let w_idx = out_c * c_in_per_group * kernel_spatial
                                        + ic * kernel_spatial
                                        + kh * params.kernel_w
                                        + kw;
                                    scalar_acc += *input.add(in_idx) * *weight.add(w_idx);
                                }
                            }
                        }

                        let sum = vaddvq_f32(acc) + scalar_acc;
                        let out_idx = b * params.c_out * out_spatial
                            + out_c * out_spatial
                            + oh * params.output_w
                            + ow;
                        *output.add(out_idx) = sum;
                    }
                }
            }
        }
    }
}

/// NEON conv2d for f64
#[cfg(target_arch = "aarch64")]
#[target_feature(enable = "neon")]
pub unsafe fn conv2d_f64(
    input: *const f64,
    weight: *const f64,
    bias: Option<*const f64>,
    output: *mut f64,
    params: Conv2dParams,
) {
    let lanes = 2;
    let c_in_per_group = params.c_in / params.groups;
    let c_out_per_group = params.c_out / params.groups;
    let chunks = c_in_per_group / lanes;

    let in_spatial = params.height * params.width;
    let out_spatial = params.output_h * params.output_w;
    let kernel_spatial = params.kernel_h * params.kernel_w;

    for b in 0..params.batch {
        for g in 0..params.groups {
            let c_in_start = g * c_in_per_group;
            let c_out_start = g * c_out_per_group;

            for oc in 0..c_out_per_group {
                let out_c = c_out_start + oc;

                let bias_val = if let Some(bias_ptr) = bias {
                    *bias_ptr.add(out_c)
                } else {
                    0.0
                };

                for oh in 0..params.output_h {
                    for ow in 0..params.output_w {
                        let mut acc = vdupq_n_f64(bias_val);
                        let mut scalar_acc = 0.0f64;

                        for kh in 0..params.kernel_h {
                            for kw in 0..params.kernel_w {
                                let ih_signed = (oh * params.stride_h) as isize
                                    + (kh * params.dilation_h) as isize
                                    - params.pad_top as isize;
                                let iw_signed = (ow * params.stride_w) as isize
                                    + (kw * params.dilation_w) as isize
                                    - params.pad_left as isize;
                                if ih_signed < 0
                                    || (ih_signed as usize) >= params.height
                                    || iw_signed < 0
                                    || (iw_signed as usize) >= params.width
                                {
                                    continue;
                                }
                                let ih = ih_signed as usize;
                                let iw = iw_signed as usize;

                                for chunk in 0..chunks {
                                    let ic_base = chunk * lanes;

                                    let mut in_arr = [0.0f64; 2];
                                    for lane in 0..lanes {
                                        let ic = ic_base + lane;
                                        let in_idx = b * params.c_in * in_spatial
                                            + (c_in_start + ic) * in_spatial
                                            + ih * params.width
                                            + iw;
                                        in_arr[lane] = *input.add(in_idx);
                                    }
                                    let v_in = vld1q_f64(in_arr.as_ptr());

                                    let mut w_arr = [0.0f64; 2];
                                    for lane in 0..lanes {
                                        let ic = ic_base + lane;
                                        let w_idx = out_c * c_in_per_group * kernel_spatial
                                            + ic * kernel_spatial
                                            + kh * params.kernel_w
                                            + kw;
                                        w_arr[lane] = *weight.add(w_idx);
                                    }
                                    let v_w = vld1q_f64(w_arr.as_ptr());

                                    acc = vfmaq_f64(acc, v_in, v_w);
                                }

                                for ic in (chunks * lanes)..c_in_per_group {
                                    let in_idx = b * params.c_in * in_spatial
                                        + (c_in_start + ic) * in_spatial
                                        + ih * params.width
                                        + iw;
                                    let w_idx = out_c * c_in_per_group * kernel_spatial
                                        + ic * kernel_spatial
                                        + kh * params.kernel_w
                                        + kw;
                                    scalar_acc += *input.add(in_idx) * *weight.add(w_idx);
                                }
                            }
                        }

                        let sum = vaddvq_f64(acc) + scalar_acc;
                        let out_idx = b * params.c_out * out_spatial
                            + out_c * out_spatial
                            + oh * params.output_w
                            + ow;
                        *output.add(out_idx) = sum;
                    }
                }
            }
        }
    }
}

// ============================================================================
// Depthwise Conv2d
// ============================================================================

/// NEON depthwise conv2d for f32
///
/// For depthwise convolution, we vectorize over output width instead of channels.
///
/// # Safety
/// - All pointers must be valid and properly aligned
#[cfg(target_arch = "aarch64")]
#[target_feature(enable = "neon")]
pub unsafe fn depthwise_conv2d_f32(
    input: *const f32,
    weight: *const f32,
    bias: Option<*const f32>,
    output: *mut f32,
    params: Conv2dParams,
) {
    let lanes = 4;
    let out_w_chunks = params.output_w / lanes;

    let in_spatial = params.height * params.width;
    let out_spatial = params.output_h * params.output_w;
    let kernel_spatial = params.kernel_h * params.kernel_w;

    for b in 0..params.batch {
        for c in 0..params.c_out {
            let bias_val = if let Some(bias_ptr) = bias {
                *bias_ptr.add(c)
            } else {
                0.0
            };
            let v_bias = vdupq_n_f32(bias_val);

            for oh in 0..params.output_h {
                // Vectorized output width
                for ow_chunk in 0..out_w_chunks {
                    let ow_base = ow_chunk * lanes;
                    let mut acc = v_bias;

                    for kh in 0..params.kernel_h {
                        for kw in 0..params.kernel_w {
                            let w_val = *weight.add(c * kernel_spatial + kh * params.kernel_w + kw);
                            let v_w = vdupq_n_f32(w_val);

                            let ih_signed = (oh * params.stride_h) as isize
                                + (kh * params.dilation_h) as isize
                                - params.pad_top as isize;
                            if ih_signed < 0 || (ih_signed as usize) >= params.height {
                                continue;
                            }
                            let ih = ih_signed as usize;

                            // Gather 4 input values at different output width positions
                            let mut in_arr = [0.0f32; 4];
                            for lane in 0..lanes {
                                let ow = ow_base + lane;
                                let iw_signed = (ow * params.stride_w) as isize
                                    + (kw * params.dilation_w) as isize
                                    - params.pad_left as isize;
                                if iw_signed >= 0 && (iw_signed as usize) < params.width {
                                    let iw = iw_signed as usize;
                                    let in_idx = b * params.c_in * in_spatial
                                        + c * in_spatial
                                        + ih * params.width
                                        + iw;
                                    in_arr[lane] = *input.add(in_idx);
                                }
                            }
                            let v_in = vld1q_f32(in_arr.as_ptr());

                            acc = vfmaq_f32(acc, v_in, v_w);
                        }
                    }

                    // Store 4 output values
                    let out_idx = b * params.c_out * out_spatial
                        + c * out_spatial
                        + oh * params.output_w
                        + ow_base;
                    vst1q_f32(output.add(out_idx), acc);
                }

                // Scalar tail for remaining output width
                for ow in (out_w_chunks * lanes)..params.output_w {
                    let mut sum = bias_val;

                    for kh in 0..params.kernel_h {
                        for kw in 0..params.kernel_w {
                            let ih_signed = (oh * params.stride_h) as isize
                                + (kh * params.dilation_h) as isize
                                - params.pad_top as isize;
                            let iw_signed = (ow * params.stride_w) as isize
                                + (kw * params.dilation_w) as isize
                                - params.pad_left as isize;
                            if ih_signed < 0
                                || (ih_signed as usize) >= params.height
                                || iw_signed < 0
                                || (iw_signed as usize) >= params.width
                            {
                                continue;
                            }
                            let ih = ih_signed as usize;
                            let iw = iw_signed as usize;

                            let in_idx = b * params.c_in * in_spatial
                                + c * in_spatial
                                + ih * params.width
                                + iw;
                            let w_idx = c * kernel_spatial + kh * params.kernel_w + kw;

                            sum += *input.add(in_idx) * *weight.add(w_idx);
                        }
                    }

                    let out_idx = b * params.c_out * out_spatial
                        + c * out_spatial
                        + oh * params.output_w
                        + ow;
                    *output.add(out_idx) = sum;
                }
            }
        }
    }
}

/// NEON depthwise conv2d for f64
#[cfg(target_arch = "aarch64")]
#[target_feature(enable = "neon")]
pub unsafe fn depthwise_conv2d_f64(
    input: *const f64,
    weight: *const f64,
    bias: Option<*const f64>,
    output: *mut f64,
    params: Conv2dParams,
) {
    let lanes = 2;
    let out_w_chunks = params.output_w / lanes;

    let in_spatial = params.height * params.width;
    let out_spatial = params.output_h * params.output_w;
    let kernel_spatial = params.kernel_h * params.kernel_w;

    for b in 0..params.batch {
        for c in 0..params.c_out {
            let bias_val = if let Some(bias_ptr) = bias {
                *bias_ptr.add(c)
            } else {
                0.0
            };
            let v_bias = vdupq_n_f64(bias_val);

            for oh in 0..params.output_h {
                for ow_chunk in 0..out_w_chunks {
                    let ow_base = ow_chunk * lanes;
                    let mut acc = v_bias;

                    for kh in 0..params.kernel_h {
                        for kw in 0..params.kernel_w {
                            let w_val = *weight.add(c * kernel_spatial + kh * params.kernel_w + kw);
                            let v_w = vdupq_n_f64(w_val);

                            let ih_signed = (oh * params.stride_h) as isize
                                + (kh * params.dilation_h) as isize
                                - params.pad_top as isize;
                            if ih_signed < 0 || (ih_signed as usize) >= params.height {
                                continue;
                            }
                            let ih = ih_signed as usize;

                            let mut in_arr = [0.0f64; 2];
                            for lane in 0..lanes {
                                let ow = ow_base + lane;
                                let iw_signed = (ow * params.stride_w) as isize
                                    + (kw * params.dilation_w) as isize
                                    - params.pad_left as isize;
                                if iw_signed >= 0 && (iw_signed as usize) < params.width {
                                    let iw = iw_signed as usize;
                                    let in_idx = b * params.c_in * in_spatial
                                        + c * in_spatial
                                        + ih * params.width
                                        + iw;
                                    in_arr[lane] = *input.add(in_idx);
                                }
                            }
                            let v_in = vld1q_f64(in_arr.as_ptr());

                            acc = vfmaq_f64(acc, v_in, v_w);
                        }
                    }

                    let out_idx = b * params.c_out * out_spatial
                        + c * out_spatial
                        + oh * params.output_w
                        + ow_base;
                    vst1q_f64(output.add(out_idx), acc);
                }

                for ow in (out_w_chunks * lanes)..params.output_w {
                    let mut sum = bias_val;

                    for kh in 0..params.kernel_h {
                        for kw in 0..params.kernel_w {
                            let ih_signed = (oh * params.stride_h) as isize
                                + (kh * params.dilation_h) as isize
                                - params.pad_top as isize;
                            let iw_signed = (ow * params.stride_w) as isize
                                + (kw * params.dilation_w) as isize
                                - params.pad_left as isize;
                            if ih_signed < 0
                                || (ih_signed as usize) >= params.height
                                || iw_signed < 0
                                || (iw_signed as usize) >= params.width
                            {
                                continue;
                            }
                            let ih = ih_signed as usize;
                            let iw = iw_signed as usize;

                            let in_idx = b * params.c_in * in_spatial
                                + c * in_spatial
                                + ih * params.width
                                + iw;
                            let w_idx = c * kernel_spatial + kh * params.kernel_w + kw;

                            sum += *input.add(in_idx) * *weight.add(w_idx);
                        }
                    }

                    let out_idx = b * params.c_out * out_spatial
                        + c * out_spatial
                        + oh * params.output_w
                        + ow;
                    *output.add(out_idx) = sum;
                }
            }
        }
    }
}