Skip to content

Commit 5d35c41

Browse files
voidstackloopclaude
andcommitted
Determinism test: bit-identical results across 1/2/3/8 threads
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent ca58c6d commit 5d35c41

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//! Results must not depend on how many threads run: reductions, matmul, conv and softmax are bit-identical on 1, 2, 3 and 8
2+
//! threads (a parallel reduction that adds in scheduling order would break this).
3+
4+
use oxideon_core::Tensor;
5+
6+
fn data(n: usize, seed: f32) -> Vec<f32> {
7+
(0..n).map(|i| ((i as f32 + seed) * 0.37).sin() * 3.0 + 1e-3 * (i % 7) as f32).collect()
8+
}
9+
10+
fn run() -> Vec<Vec<u32>> {
11+
let a = Tensor::from_slice(&data(300 * 257, 0.0), &[300, 257]).unwrap();
12+
let b = Tensor::from_slice(&data(257 * 190, 1.0), &[257, 190]).unwrap();
13+
let img = Tensor::from_slice(&data(2 * 3 * 40 * 40, 2.0), &[2, 3, 40, 40]).unwrap();
14+
let w = Tensor::from_slice(&data(8 * 3 * 3 * 3, 3.0), &[8, 3, 3, 3]).unwrap();
15+
let outs = vec![
16+
a.sum().unwrap(),
17+
a.mean_axis(0, false).unwrap(),
18+
a.sum_axis(1, false).unwrap(),
19+
a.matmul(&b).unwrap(),
20+
a.softmax(-1).unwrap(),
21+
img.conv2d(&w, None, (1, 1), (1, 1), (1, 1)).unwrap(),
22+
a.var_axis(1, true, false).unwrap(),
23+
];
24+
outs.iter().map(|t| t.to_vec::<f32>().unwrap().iter().map(|v| v.to_bits()).collect()).collect()
25+
}
26+
27+
#[test]
28+
fn results_are_bit_identical_across_thread_counts() {
29+
let reference = rayon::ThreadPoolBuilder::new().num_threads(1).build().unwrap().install(run);
30+
for n in [2, 3, 8] {
31+
let got = rayon::ThreadPoolBuilder::new().num_threads(n).build().unwrap().install(run);
32+
for (i, (g, r)) in got.iter().zip(&reference).enumerate() {
33+
assert_eq!(g, r, "op #{i} differs between 1 and {n} threads");
34+
}
35+
}
36+
// and repeated runs on the default pool agree too
37+
assert_eq!(run(), run());
38+
}

0 commit comments

Comments
 (0)