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
//! Wide accumulators for kernels whose element type cannot hold a running total.
//!
//! A summing kernel that keeps its accumulator in the element type `T` is wrong
//! for every dtype narrower than the total it builds:
//!
//! - A float narrower than F32 (F16, BF16, FP8E4M3, FP8E5M2) stops growing as
//! soon as the accumulator's spacing exceeds twice the increment, so the sum
//! converges on a constant or saturates to infinity.
//! - An integer wraps (release) or panics (debug) on overflow, so a total that
//! crosses the type's range and comes back — a matmul with cancellation, a
//! mean whose sum overflows but whose average does not — reports a value that
//! is not merely imprecise but the wrong sign and magnitude.
//!
//! The fix is the same in both cases: accumulate in a wider type and narrow
//! exactly once at write-out. [`WideAcc`] gives the two accumulators those
//! kernels need, so `cumsum`, `matmul`, and the integer reduction epilogues
//! share one definition of "wider" instead of open-coding three.
//!
//! Pick the accumulator with [`WideAcc`]'s two implementors:
//!
//! - `f32` for `DType::is_narrow_float()`. F32 is the accumulator format the
//! rest of numr already uses for these dtypes (see `reduce_kernel` and
//! `reduce_epilogue`), and it holds every F16/BF16/FP8 total that the output
//! dtype can represent.
//! - `i128` for `DType::is_int()`. It is exact for any product or sum of two
//! 64-bit integers, so no integer dtype loses information on the way in.
//!
//! Output dtypes never change: [`WideAcc::to_elem`] narrows back to `T`.
//!
//! # Saturating, not wrapping
//!
//! Integer narrowing clamps to the output dtype's range, and the i128
//! accumulator itself uses saturating add and multiply. This is a deliberate
//! choice, not an accident of the types:
//!
//! - It matches [`Element::from_f64`], which already saturates because Rust's
//! float-to-int `as` cast saturates. Widening the accumulator therefore does
//! not introduce a second narrowing convention.
//! - It is the only option that stays a total function. Wrapping reports a
//! value with the wrong sign, and returning an error would make an overflow
//! anywhere in a batch fail the whole call.
//!
//! `i64` and `u64` have no wider integer dtype in numr, so their *output* still
//! cannot represent a total that overflows them; saturation is what they get,
//! and the i128 accumulator at least keeps intermediate partial sums exact.
//!
//! # Which side of the line a kernel is on
//!
//! - ACCUMULATORS saturate: `cumsum`, `cumprod`, `matmul`, `mean`, and the
//! integer reductions. They build a running total wider than one element, so
//! clamping the total is the only answer that keeps the sign right.
//! - ELEMENTWISE ops wrap: `add`, `sub`, and `mul` on two tensors. Each output
//! is one machine operation on two elements, and wrapping is what every
//! array library and every GPU ISA gives there.
//!
//! A new kernel picks the accumulator side only when it carries state across
//! elements.
use crateElement;
/// An accumulator wider than the element type being summed.
///
/// Implemented for `f32` (narrow-float elements) and `i128` (integer elements).
/// Finish an integer mean: divide a wide sum by the element count.
///
/// The division truncates toward zero, which is what the previous
/// `T::from_f64(sum.to_f64() / count as f64)` epilogue did for every sum it
/// could represent. Doing it in i128 keeps it right for the sums it could not:
/// `mean([2_000_000_000, 2_000_000_000])` as I32 is 2_000_000_000, even though
/// the sum needs 33 bits.
///
/// `count.max(1)` fixes the answer for an integer mean over ZERO elements at 0.
/// A float answers `0 / 0`, which is NaN, and that is what every backend gives
/// it; an integer dtype cannot represent NaN, so 0 is a choice forced by the
/// dtype rather than a mathematical identity. CUDA's `numr128_div_u64_trunc`
/// forces a zero divisor to 1 for the same reason, and WebGPU's
/// `empty_reduce_identity` answers 0 for a non-float `mean`. Do not remove the
/// clamp: without it this divides by zero and panics.