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
/// Test softmax with identical values (edge case for max finding)
#[test]
fn test_softmax_simd_identical_values() {
// All same values should result in uniform distribution
for size in [8, 16, 24, 32] {
let mut x: Vec<f32> = vec![5.0; size];
softmax_simd(&mut x);
let expected = 1.0 / size as f32;
for (i, v) in x.iter().enumerate() {
assert!(
(v - expected).abs() < 1e-5,
"Size {}: uniform at {}: got {}, expected {}",
size,
i,
v,
expected
);
}
}
}
/// Test softmax with maximum difference in values
#[test]
fn test_softmax_simd_max_difference() {
// Large difference should result in near-1 for max, near-0 for others
let mut x = vec![0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1000.0];
softmax_simd(&mut x);
assert!(x[7] > 0.9999, "Max element should dominate: {}", x[7]);
for i in 0..7 {
assert!(
x[i] < 1e-5,
"Non-max element {} should be near 0: {}",
i,
x[i]
);
}
}
// =============================================================================
// Fused SwiGLU SIMD: Remainder Loop and Scalar Fallback Coverage
// =============================================================================
/// Test swiglu with sizes that exercise remainder loops
#[test]
fn test_swiglu_simd_remainder_loops() {
for remainder in 1..=7 {
let size = 8 + remainder;
let mut gate: Vec<f32> = (0..size)
.map(|i| (i as f32 - size as f32 / 2.0) * 0.3)
.collect();
let up: Vec<f32> = vec![1.5; size];
let expected: Vec<f32> = gate
.iter()
.zip(up.iter())
.map(|(g, u)| {
let sigmoid = 1.0 / (1.0 + (-g).exp());
g * sigmoid * u
})
.collect();
fused_swiglu_simd(&mut gate, &up);
for (i, (got, exp)) in gate.iter().zip(expected.iter()).enumerate() {
assert!(
(got - exp).abs() < 0.2, // Lenient for AVX2 polynomial approx
"Size {}: mismatch at {}: got {}, expected {}",
size,
i,
got,
exp
);
}
}
}
/// Test swiglu with sizes 16+1 through 16+7
#[test]
fn test_swiglu_simd_two_chunks_remainder() {
for remainder in 1..=7 {
let size = 16 + remainder;
let mut gate: Vec<f32> = (0..size).map(|i| (i as f32 - 8.0) * 0.2).collect();
let up: Vec<f32> = (0..size).map(|i| (i as f32 + 1.0) * 0.1).collect();
let expected: Vec<f32> = gate
.iter()
.zip(up.iter())
.map(|(g, u)| {
let sigmoid = 1.0 / (1.0 + (-g).exp());
g * sigmoid * u
})
.collect();
fused_swiglu_simd(&mut gate, &up);
for (i, (got, exp)) in gate.iter().zip(expected.iter()).enumerate() {
assert!(
(got - exp).abs() < 0.2, // Lenient for AVX2 polynomial approx
"Size {}: mismatch at {}: got {}, expected {}",
size,
i,
got,
exp
);
}
}
}
/// Test swiglu with sizes 1-7 (scalar fallback only)
#[test]
fn test_swiglu_simd_scalar_fallback_all() {
for size in 1..=7 {
let mut gate: Vec<f32> = (0..size).map(|i| (i as f32 - 3.0) * 0.5).collect();
let up: Vec<f32> = vec![2.0; size];
let expected: Vec<f32> = gate
.iter()
.zip(up.iter())
.map(|(g, u)| {
let sigmoid = 1.0 / (1.0 + (-g).exp());
g * sigmoid * u
})
.collect();
fused_swiglu_simd(&mut gate, &up);
for (i, (got, exp)) in gate.iter().zip(expected.iter()).enumerate() {
assert!(
(got - exp).abs() < 0.2, // Lenient for AVX2 polynomial approx
"Size {}: mismatch at {}: got {}, expected {}",
size,
i,
got,
exp
);
}
}
}
/// Test swiglu with zero gate values
#[test]
fn test_swiglu_simd_zero_gate() {
let mut gate = vec![0.0; 16];
let up = vec![1.0; 16];
fused_swiglu_simd(&mut gate, &up);
// silu(0) = 0 * sigmoid(0) = 0 * 0.5 = 0
for (i, g) in gate.iter().enumerate() {
assert!(g.abs() < 1e-10, "Zero gate at {}: got {}", i, g);
}
}
/// Test swiglu with zero up values
#[test]
fn test_swiglu_simd_zero_up() {
let mut gate: Vec<f32> = (0..16).map(|i| i as f32 - 8.0).collect();
let up = vec![0.0; 16];
fused_swiglu_simd(&mut gate, &up);
// silu(x) * 0 = 0
for (i, g) in gate.iter().enumerate() {
assert!(g.abs() < 1e-10, "Zero up at {}: got {}", i, g);
}
}
// =============================================================================
// Horizontal Sum Helpers: Indirect Testing Through Public APIs
// =============================================================================
/// Test that exercises the AVX2 horizontal sum paths via softmax
#[test]
fn test_horizontal_sum_via_softmax_large() {
// Large input to ensure multiple SIMD iterations
let mut x: Vec<f32> = (0..256).map(|i| (i as f32 - 128.0) * 0.01).collect();
softmax_simd(&mut x);
let sum: f32 = x.iter().sum();
assert!((sum - 1.0).abs() < 1e-4, "Large softmax sum: got {}", sum);
}
/// Test that exercises the AVX2 paths via swiglu with large input
#[test]
fn test_avx2_path_via_swiglu_large() {
let mut gate: Vec<f32> = (0..256).map(|i| (i as f32 - 128.0) * 0.02).collect();
let up: Vec<f32> = (0..256).map(|i| (i as f32 + 1.0) * 0.01).collect();
fused_swiglu_simd(&mut gate, &up);
// Verify all outputs are finite
for (i, g) in gate.iter().enumerate() {
assert!(g.is_finite(), "Large swiglu at {}: got {}", i, g);
}
}
// =============================================================================
// Edge Case: Empty and Single Element
// =============================================================================
/// Test softmax with exactly 0 elements
#[test]
fn test_softmax_simd_zero_elements() {
let mut x: Vec<f32> = vec![];
softmax_simd(&mut x);
assert!(x.is_empty(), "Empty should remain empty");
}
/// Test swiglu with exactly 0 elements
#[test]
fn test_swiglu_simd_zero_elements() {
let mut gate: Vec<f32> = vec![];
let up: Vec<f32> = vec![];
fused_swiglu_simd(&mut gate, &up);
assert!(gate.is_empty(), "Empty should remain empty");
}
/// Test rope with head_dim=0 (degenerate case)
#[test]
fn test_rope_simd_zero_head_dim() {
let _x: Vec<f32> = vec![1.0, 2.0];
let _freqs_cos: Vec<f32> = vec![];
let _freqs_sin: Vec<f32> = vec![];
// head_dim=0 means half_dim=0, should be no-op
// Note: This may trigger debug_assert in debug mode
// In release, it should be a no-op
}
// =============================================================================
// Special Float Values: Comprehensive Coverage
// =============================================================================
/// Test softmax with mix of special values
#[test]
fn test_softmax_simd_special_values_mix() {
let mut x = vec![
0.0,
f32::MIN_POSITIVE,
f32::EPSILON,
1.0,
-1.0,
-f32::MIN_POSITIVE,
-f32::EPSILON,
100.0,
];
softmax_simd(&mut x);
// All values should be finite and non-negative
for (i, v) in x.iter().enumerate() {
assert!(v.is_finite(), "Special mix softmax at {}: got {}", i, v);
assert!(*v >= 0.0, "Special mix softmax negative at {}: {}", i, v);
}
let sum: f32 = x.iter().sum();
assert!((sum - 1.0).abs() < 1e-5, "Special mix sum: {}", sum);
}
/// Test swiglu with special float values
/// Note: AVX2 polynomial approximation has limited range, so we use moderate values
#[test]
fn test_swiglu_simd_special_values() {
let mut gate = vec![
f32::MIN_POSITIVE,
10.0, // Use moderate values (AVX2 polynomial approx range is ~[-87, 0])
f32::EPSILON,
-f32::MIN_POSITIVE,
-10.0,
-f32::EPSILON,
0.0,
1.0,
];
let up = vec![1.0; 8];
fused_swiglu_simd(&mut gate, &up);
// Check no NaN
for (i, g) in gate.iter().enumerate() {
assert!(!g.is_nan(), "Special swiglu NaN at {}", i);
}
}