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
//! Shared loop nest for the SIMD `conv1d` kernels.
//!
//! # Why output positions, and not input channels
//!
//! The tensor layout is `(batch, channels, length)`, so two neighbouring input
//! CHANNELS sit `length` elements apart. Vectorising the channel axis therefore
//! cannot use a vector load at all — it has to pack the lanes one scalar at a
//! time (a manual gather of BOTH input and weight), reduce into a single
//! accumulator, and finish with a horizontal sum. It also degenerates to
//! nothing at all for a depthwise convolution, where `c_in_per_group == 1`.
//!
//! Neighbouring OUTPUT POSITIONS, by contrast, read neighbouring input
//! elements. Fixing `(b, g, oc)` and reducing over `(kx, ic)` while vectorising
//! `ox` turns the weight into a scalar broadcast (`set1`) and the input into a
//! contiguous vector load whenever `stride == 1`. Depthwise vectorises fully.
//!
//! # The interior/boundary split
//!
//! The tap index is
//!
//! ```text
//! ix(ox, kx) = ox * stride + kx * dilation - pad_left
//! ```
//!
//! which is monotonically non-decreasing in `kx`. So "every tap of this `ox` is
//! in bounds" needs checking only at the two ends, `kx = 0` and `kx = K - 1`:
//!
//! ```text
//! ix(ox, 0) >= 0 <=> ox * stride >= pad_left
//! <=> ox >= ceil(pad_left / stride) = interior_lo
//! ix(ox, K - 1) <= length-1 <=> ox * stride <= length - 1 + pad_left - (K-1)*dilation
//! <=> ox < ceil((length + pad_left - (K-1)*dilation) / stride)
//! = interior_hi
//! ```
//!
//! (The second equivalence uses `floor((M - 1) / s) + 1 == ceil(M / s)` for
//! `M >= 1`. When `M = length + pad_left - (K-1)*dilation` is zero or negative —
//! a kernel whose dilated span exceeds the padded input — NO output position is
//! fully interior, so `interior_hi` is clamped to 0. That subtraction is done in
//! `isize` precisely so `usize` cannot wrap here.)
//!
//! `ox` in `[0, interior_lo)` and `[interior_hi, output_length)` runs scalar,
//! keeping the per-tap `0 <= ix < length` check. Those two edges are together at
//! most `(K-1)*dilation / stride + 1` positions wide, so vectorising them would
//! buy nothing. `ox` in `[interior_lo, interior_hi)` runs vectorised with NO
//! per-tap bounds check at all.
//!
//! # Accumulation order
//!
//! Within a lane the reduction is `ic` outer, `kx` inner — the same order as the
//! scalar kernel — and the bias is added once, after the full reduction. The
//! only numerical difference from scalar is FMA contraction of the multiply-add.
/// Expands the `conv1d` loop nest for one dtype.
///
/// The ISA-specific block receives the fully interior output run for one
/// `(batch, group, out-channel)` triple and must compute all `n` of its outputs:
///
/// - `$op`: `*mut $ty`, output for `ox = interior_lo`, `n` contiguous elements
/// - `$ip`: `*const $ty`, input at `(b, c_in_start, ix(interior_lo, 0))`; the
/// element for `(j, ic, kx)` is `$ip[ic * length + j * stride + kx * dilation]`
/// - `$wp`: `*const $ty`, weight at `(c_out_idx, 0, 0)`; the element for
/// `(ic, kx)` is `$wp[ic * kernel_size + kx]`
/// - `$n`: number of interior output positions (always `>= 1`)
/// - `$nic`: `c_in_per_group`
/// - `$bv`: the bias value for this output channel (zero when there is no bias)
///
/// Every index above is in bounds by construction, so the block must not
/// bounds-check taps. `stride`, `dilation`, `kernel_size` and `length` are read
/// by the block from its own `params` argument (macro hygiene keeps the locals
/// expanded here invisible to it).
pub use conv1d_body;