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
//! Shared constants and block-shape rules for the convolution launchers.
//!
//! The blocking factors here are duplicated in the kernel sources; the tests
//! at the bottom parse the `.cu` files and check the two still agree.
/// Module name for convolution operations
pub const CONV_MODULE: &str = "conv";
/// Candidate conv1d block widths (threads per block along the output axis),
/// narrowest first. `output_length` is as small as ~26 at some hot shapes, so a
/// fixed 256-wide block would leave most lanes idle; the launcher picks the
/// narrowest width that still covers the row.
///
/// A full warp is the floor for `conv1d_oc4` only, which needs each warp on one
/// output-channel slot so its four stores stay coalesced. The scalar kernel
/// indexes `ox`, `oc` and `batch` independently and carries no such assumption,
/// so it takes [`CONV1D_BLOCK_NARROW`] below a warp instead of leaving most of
/// the warp idle.
pub const CONV1D_BLOCK_CANDIDATES: = ;
/// Sub-warp block widths for the scalar kernel, narrowest first. Decode-shaped
/// convolutions run at `output_length` of 1, where a 32-wide block retires 31
/// lanes at the bounds check before they do any work. Threads freed here go to
/// the output-channel axis, which always has work.
pub const CONV1D_BLOCK_NARROW: = ;
/// Fallback conv1d block width when `output_length` exceeds every candidate.
pub const CONV1D_BLOCK_MAX: u32 = 256;
/// Target threads per block, shared by conv1d and depthwise_conv2d's
/// row/position-indexed kernels. conv1d pads a narrow row out along the second
/// grid axis (`blockDim.y`, output channels) instead of launching a 32-thread
/// block, because an SM holds at most 16 blocks and 32-thread blocks would cap
/// it at 16 of its 48 warp slots. `depthwise_conv2d_ox` reaches the same count
/// with a flat block: it folds the output-row axis into x, so it needs no
/// second block axis to pad with.
pub const CONV_BLOCK_THREADS: u32 = 128;
/// Output channels each thread of `conv1d_oc4_*` accumulates.
pub const CONV1D_OC_BLOCK: usize = 4;
/// Consecutive output positions each thread of `conv1d_ox_*` accumulates.
/// Must match `CONV1D_OX_BLOCK` in `conv1d_ox.cu`.
pub const CONV1D_OX_BLOCK: usize = 4;
/// Module name for the position-blocked conv1d kernel (`conv1d_ox.cu` compiles
/// to its own fatbin, separate from [`CONV_MODULE`]).
pub const CONV1D_OX_MODULE: &str = "conv1d_ox";
/// Minimum `output_length` before `conv1d_ox` is preferred over the scalar
/// `conv1d` kernel for depthwise/narrow-group shapes (the oc4 kernel is
/// unavailable there). Guarantees the row covers at least one full
/// [`CONV1D_OX_BLOCK`]-wide chunk plus a remainder.
pub const CONV1D_OX_MIN_OUTPUT_LENGTH: usize = 2 * CONV1D_OX_BLOCK;
/// Device waves the position-blocked grid must still reach after blocking.
///
/// `output_length` alone does not gate this kernel. Blocking divides the thread
/// count by [`CONV1D_OX_BLOCK`], so a shape with few channels can have a long
/// row and still be left with too few threads to fill the device: a narrow,
/// low-channel-count shape can regress badly versus the untiled kernel once
/// blocking leaves only a handful of warps to hide memory latency. One wave is
/// `compute_units * CONV_BLOCK_THREADS`; two is the smallest count that
/// rejects that shape while keeping every depthwise case that gains.
///
/// The wave counts the WHOLE launch, batch included, so a row can move from
/// the scalar kernel to this one as other rows join it. That is safe, and only
/// because the two kernels form one output element's sum identically: over
/// `ic` ascending, then `kx` ascending, in the same accumulator width, with
/// bias added last. `tests/cuda_conv_batch_invariance.rs` pins that. Any
/// change to either accumulation order breaks the batch invariance of conv1d
/// and the batch term has to leave this rule.
pub const CONV1D_OX_MIN_WAVES: usize = 2;
/// Consecutive output columns each thread of `depthwise_conv2d_ox_*`
/// accumulates. Must match `DEPTHWISE_CONV2D_OX_BLOCK` in
/// `depthwise_conv2d_ox.cu`.
pub const DEPTHWISE_CONV2D_OX_BLOCK: usize = 4;
/// Module name for the column-blocked depthwise conv2d kernel
/// (`depthwise_conv2d_ox.cu` compiles to its own fatbin, separate from
/// [`CONV_MODULE`]).
pub const DEPTHWISE_CONV2D_OX_MODULE: &str = "depthwise_conv2d_ox";
/// Minimum `output_w` before `depthwise_conv2d_ox` is preferred over the flat
/// kernel. Guarantees the row covers at least one full
/// [`DEPTHWISE_CONV2D_OX_BLOCK`]-wide chunk plus a remainder.
pub const DEPTHWISE_CONV2D_OX_MIN_OUTPUT_WIDTH: usize = 2 * DEPTHWISE_CONV2D_OX_BLOCK;
/// Device waves the column-blocked depthwise grid must still reach after
/// blocking. Blocking divides the thread count by
/// [`DEPTHWISE_CONV2D_OX_BLOCK`], so a wide row is not on its own enough to
/// keep the device fed once channels and rows are few.
///
/// The wave counts the whole launch, batch included, on the same footing as
/// [`CONV1D_OX_MIN_WAVES`]: the flat and the column-blocked kernel both sum
/// `ky` ascending then `kx` ascending with bias last, so which one a row gets
/// moves no bits.
pub const DEPTHWISE_CONV2D_OX_MIN_WAVES: usize = 2;
/// CUDA caps the y and z grid dimensions at 65535 blocks.
pub const CUDA_MAX_GRID_YZ: usize = 65535;
/// Block width (threads along the output-position axis) for the kernels that
/// index position, channel and batch independently, so may go below a warp.
/// `x_extent` is the number of thread slots the axis needs, already divided by
/// the per-thread blocking factor where one applies.
pub
/// Whether a position-blocked grid of `threads` still reaches `min_waves`
/// waves of the device.
///
/// `compute_units` is 0 on an unknown profile, which makes the test trivially
/// true and leaves the caller's row-extent gate deciding.
pub