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
//! Masked-tail coverage for the squared-L2 and dot-product kernels.
//!
//! Companion to `cosine_tail_tests.rs`, which closed the same gap for cosine
//! (#2106 item 14). The dispatch thresholds are identical for all three —
//! `>= 1024` picks the 8-accumulator kernel, `>= 512` the 4-accumulator one,
//! anything smaller the 2-accumulator one — but the remainder handling is not
//! the same shape, and the dimensions that reach it differ per kernel:
//!
//! | kernel | main loop | remainder |
//! |---|---|---|
//! | 2-acc (`< 512`) | `len / 16` chunks of 16 | one masked chunk, `len % 16` |
//! | 4-acc (`512..1024`) | `len / 64 * 64` | 16-wide bridge loop, then one masked chunk |
//! | 8-acc (`>= 1024`) | `len / 128 * 128` | 16-wide bridge loop, then one masked chunk |
//!
//! The two-stage remainder is what the existing suite missed twice over. A
//! dimension that is an exact multiple of the stride skips both stages; a
//! dimension whose leftover is an exact multiple of 16 runs the bridge but
//! never the mask; and a leftover below 16 runs the mask but never the bridge.
//! Only a leftover that is neither exercises both.
//!
//! What the suite covered before this file, checked rather than assumed:
//!
//! - `distance_engine_tests.rs` uses 128, 256, 384, 512, 768, 1024, 1536 and
//! 3072. Every one is a multiple of 16, and every one at or above 512 is an
//! exact multiple of its kernel's stride — so none reaches any remainder.
//! - `simd_native_dispatch_tests.rs` adds 100, 127 and 255, which do reach the
//! **2-acc** mask. Nothing it names at or above 512 is a non-multiple.
//! - `warmup_tests.rs` looks like it reaches the 4-acc tail at 767, but that
//! 767 is a *value* inside the generator; the vector is 768 long, a multiple
//! of 64.
//! - `simd_native_tests.rs` — the crate-level suite, one directory up from
//! this one rather than beside it, which is why an earlier note here called
//! it absent. It exists. Its dimensions are 0, 16, 19, 32, 100, 384, 768 and
//! 1536: 19 and 100 reach the 2-acc mask, and every one at or above 512 is
//! an exact multiple of its kernel's stride. It changes the file list, not
//! the conclusion.
//!
//! So the 2-acc mask was already exercised and the 4-acc and 8-acc remainders
//! — both stages of each — were not.
//!
//! One qualification, because the equivalent claim was overstated once in the
//! #2106 record: `tests/simd_property_tests.simd-property-regressions` holds a
//! checked-in proptest seed with 1509-element vectors, and proptest replays
//! persisted seeds keyed by *source file*, so the 8-acc tails do get some
//! deterministic coverage today. What they do not get is a named test that
//! says which boundary it protects and fails legibly when it breaks.
use ;
/// Dimensions chosen so that every remainder stage of every kernel runs.
///
/// | dim | kernel | leftover after the main loop | reaches |
/// |---|---|---|---|
/// | 17 | 2-acc | 1 | mask |
/// | 100 | 2-acc | 4 | mask |
/// | 511 | 2-acc | 15 | mask (widest) |
/// | 513 | 4-acc | 1 | mask only |
/// | 533 | 4-acc | 21 | bridge once, then mask of 5 |
/// | 544 | 4-acc | 32 | bridge twice, mask skipped |
/// | 575 | 4-acc | 63 | bridge three times, mask of 15 |
/// | 1025 | 8-acc | 1 | mask only |
/// | 1041 | 8-acc | 17 | bridge once, then mask of 1 |
/// | 1152 | 8-acc | 0 | control — neither stage |
/// | 1279 | 8-acc | 127 | bridge seven times, mask of 15 |
///
/// The exact multiples (512, 1024, 1152, 2048) are controls: they skip the
/// remainder entirely, so a failure there is a main-loop bug and the pair
/// localizes which half broke.
const TAIL_DIMS: & = &;
/// Distinct, non-degenerate vectors.
///
/// Not equal, not orthogonal, and nowhere near zero: each of those makes at
/// least one of the two metrics insensitive to a dropped element. Every
/// component contributes a term of order one, so omitting one moves the sum by
/// order one — far above f32 rounding at these magnitudes.
/// The true squared L2, accumulated in `f64`.
///
/// The reference is deliberately not an f32 scalar loop: that would be one
/// more approximation with its own summation order, and a disagreement would
/// not say which side was wrong. `f64` is the value both are approximating.
/// The true dot product, accumulated in `f64`. See [`squared_l2_reference`].
/// Relative bound between a dispatched f32 kernel and the `f64` truth.
///
/// These are sums of up to ~2048 positive terms, so the absolute error grows
/// with the result and an absolute bound would either be vacuous at dim 2048
/// or impossible at dim 17. `1e-5` relative leaves roughly two orders of
/// magnitude over f32 accumulation noise while staying far below the ~1/dim
/// effect of a dropped element.
const RELATIVE_TOLERANCE: f64 = 1e-5;
/// Squared L2 must agree with the truth at every dimension that reaches a tail.
/// Dot product must agree with the truth at every dimension that reaches a tail.
/// The tail must carry its elements, not silently drop them.
///
/// Comparing dimension `n` against `n - 1` on the same prefix isolates the
/// tail: a kernel that skipped its last element would return the same number
/// twice. This catches a dropped element even if the reference were wrong in
/// the same way, which the parity tests above cannot.
/// The batch dot entry point agrees with the single one at every boundary.
///
/// `batch_dot_product_native` resolves the kernel once from the dimension and
/// then applies it to every candidate, which is a second dispatch site with
/// the same thresholds and its own opportunity to pick the wrong arm. Nothing
/// tied the two together, so a divergence would have surfaced only as a
/// ranking difference between a batched and an unbatched search.