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
//! Reduction operations trait.
use crate::error::Result;
use crate::ops::reduce::AccumulationPrecision;
use crate::runtime::Runtime;
use crate::tensor::Tensor;
/// Reduction operations
pub trait ReduceOps<R: Runtime> {
/// Sum along specified dimensions
fn sum(&self, a: &Tensor<R>, dims: &[usize], keepdim: bool) -> Result<Tensor<R>>;
/// Sum along specified dimensions with explicit accumulation precision.
///
/// See `AccumulationPrecision` for details on precision options.
fn sum_with_precision(
&self,
a: &Tensor<R>,
dims: &[usize],
keepdim: bool,
precision: AccumulationPrecision,
) -> Result<Tensor<R>>;
/// Mean along specified dimensions
fn mean(&self, a: &Tensor<R>, dims: &[usize], keepdim: bool) -> Result<Tensor<R>>;
/// Maximum along specified dimensions
fn max(&self, a: &Tensor<R>, dims: &[usize], keepdim: bool) -> Result<Tensor<R>>;
/// Maximum along specified dimensions with explicit accumulation precision.
///
/// See `AccumulationPrecision` for details on precision options.
fn max_with_precision(
&self,
a: &Tensor<R>,
dims: &[usize],
keepdim: bool,
precision: AccumulationPrecision,
) -> Result<Tensor<R>>;
/// Minimum along specified dimensions
fn min(&self, a: &Tensor<R>, dims: &[usize], keepdim: bool) -> Result<Tensor<R>>;
/// Minimum along specified dimensions with explicit accumulation precision.
///
/// See `AccumulationPrecision` for details on precision options.
fn min_with_precision(
&self,
a: &Tensor<R>,
dims: &[usize],
keepdim: bool,
precision: AccumulationPrecision,
) -> Result<Tensor<R>>;
/// Product along specified dimensions
///
/// Computes the product of elements along the specified dimensions.
///
/// # Arguments
///
/// * `a` - Input tensor
/// * `dims` - Dimensions to reduce over
/// * `keepdim` - If true, reduced dimensions are kept with size 1
///
/// # Returns
///
/// Tensor containing product values
///
/// # Example
///
/// ```
/// # use numr::prelude::*;
/// # let device = CpuDevice::new();
/// # let client = CpuRuntime::default_client(&device);
/// let a = Tensor::<CpuRuntime>::from_slice(&[1.0, 2.0, 3.0, 4.0], &[4], &device);
/// let result = client.prod(&a, &[0], false)?; // 24.0
/// # Ok::<(), numr::error::Error>(())
/// ```
fn prod(&self, a: &Tensor<R>, dims: &[usize], keepdim: bool) -> Result<Tensor<R>>;
/// Product along specified dimensions with explicit accumulation precision.
///
/// Accumulation precision is especially important for products as values
/// can grow or shrink exponentially, causing overflow or underflow.
///
/// See `AccumulationPrecision` for details on precision options.
fn prod_with_precision(
&self,
a: &Tensor<R>,
dims: &[usize],
keepdim: bool,
precision: AccumulationPrecision,
) -> Result<Tensor<R>>;
/// Test if any element is true (non-zero) along specified dimensions.
///
/// Returns true (1) if any element is non-zero along the specified dimensions,
/// false (0) otherwise. This operation performs logical OR reduction.
///
/// # Truth Value Semantics by DType
///
/// The "truthiness" of a value depends on its dtype:
///
/// | DType | False | True |
/// |-------|-------|------|
/// | Bool | `false` / 0 | `true` / 1 |
/// | F32, F64, F16, BF16 | `0.0`, `-0.0` | Any non-zero value, including **NaN** and **±Inf** |
/// | I8, I16, I32, I64 | `0` | Any non-zero value (positive or negative) |
/// | U8, U16, U32, U64 | `0` | Any non-zero value |
/// | FP8 variants | `0.0` | Any non-zero value |
///
/// # Important: NaN Handling
///
/// **NaN is considered truthy (non-zero).** This follows the convention that
/// `any` checks if values are non-zero, not whether they are valid numbers.
/// If you need to exclude NaN values, filter them first with `nan_to_num` or
/// check with `isnan`.
///
/// ```
/// # use numr::prelude::*;
/// # let device = CpuDevice::new();
/// # let client = CpuRuntime::default_client(&device);
/// // NaN is truthy
/// let a = Tensor::<CpuRuntime>::from_slice(&[0.0, f32::NAN, 0.0], &[3], &device);
/// let result = client.any(&a, &[0], false)?; // 1.0 (true, because NaN ≠ 0)
/// # Ok::<(), numr::error::Error>(())
/// ```
///
/// # Arguments
///
/// * `a` - Input tensor of any supported dtype
/// * `dims` - Dimensions to reduce over (empty = reduce over all dimensions)
/// * `keepdim` - If true, reduced dimensions are kept with size 1
///
/// # Returns
///
/// Tensor with the same dtype as input, containing:
/// - `1` (or `1.0` for floats) where any element is non-zero
/// - `0` (or `0.0` for floats) where all elements are zero
///
/// # Examples
///
/// ```
/// # use numr::prelude::*;
/// # let device = CpuDevice::new();
/// # let client = CpuRuntime::default_client(&device);
/// use numr::ops::ReduceOps;
///
/// // Float tensor - standard case
/// let a = Tensor::<CpuRuntime>::from_slice(&[0.0f32, 0.0, 1.0, 0.0], &[4], &device);
/// let result = client.any(&a, &[0], false)?; // 1.0 (true)
///
/// // Integer tensor
/// let a = Tensor::<CpuRuntime>::from_slice(&[0i32, 0, -5, 0], &[4], &device);
/// let result = client.any(&a, &[0], false)?; // 1 (true, -5 ≠ 0)
///
/// // All zeros
/// let a = Tensor::<CpuRuntime>::from_slice(&[0.0f32, 0.0, 0.0], &[3], &device);
/// let result = client.any(&a, &[0], false)?; // 0.0 (false)
///
/// // With infinity
/// let a = Tensor::<CpuRuntime>::from_slice(&[0.0f32, f32::INFINITY], &[2], &device);
/// let result = client.any(&a, &[0], false)?; // 1.0 (true)
///
/// // 2D tensor - reduce along rows
/// let a = Tensor::<CpuRuntime>::from_slice(&[0.0f32, 1.0, 0.0, 0.0], &[2, 2], &device);
/// let result = client.any(&a, &[1], false)?; // [1.0, 0.0]
/// # Ok::<(), numr::error::Error>(())
/// ```
///
/// # See Also
///
/// * `all` - Test if all elements are true (logical AND)
/// * `sum` - For counting non-zero elements, consider `sum(a != 0)`
fn any(&self, a: &Tensor<R>, dims: &[usize], keepdim: bool) -> Result<Tensor<R>>;
/// Test if all elements are true (non-zero) along specified dimensions.
///
/// Returns true (1) if all elements are non-zero along the specified dimensions,
/// false (0) otherwise. This operation performs logical AND reduction.
///
/// # Truth Value Semantics by DType
///
/// The "truthiness" of a value depends on its dtype:
///
/// | DType | False | True |
/// |-------|-------|------|
/// | Bool | `false` / 0 | `true` / 1 |
/// | F32, F64, F16, BF16 | `0.0`, `-0.0` | Any non-zero value, including **NaN** and **±Inf** |
/// | I8, I16, I32, I64 | `0` | Any non-zero value (positive or negative) |
/// | U8, U16, U32, U64 | `0` | Any non-zero value |
/// | FP8 variants | `0.0` | Any non-zero value |
///
/// # Important: NaN Handling
///
/// **NaN is considered truthy (non-zero).** This follows the convention that
/// `all` checks if values are non-zero, not whether they are valid numbers.
/// A tensor of all NaN values will return true. If you need to check for
/// valid (non-NaN) values, use `isnan` first.
///
/// ```
/// # use numr::prelude::*;
/// # let device = CpuDevice::new();
/// # let client = CpuRuntime::default_client(&device);
/// // All NaN values → true (all are non-zero)
/// let a = Tensor::<CpuRuntime>::from_slice(&[f32::NAN, f32::NAN], &[2], &device);
/// let result = client.all(&a, &[0], false)?; // 1.0 (true, because NaN ≠ 0)
///
/// // NaN mixed with zero → false
/// let a = Tensor::<CpuRuntime>::from_slice(&[f32::NAN, 0.0], &[2], &device);
/// let result = client.all(&a, &[0], false)?; // 0.0 (false, because 0.0 == 0)
/// # Ok::<(), numr::error::Error>(())
/// ```
///
/// # Arguments
///
/// * `a` - Input tensor of any supported dtype
/// * `dims` - Dimensions to reduce over (empty = reduce over all dimensions)
/// * `keepdim` - If true, reduced dimensions are kept with size 1
///
/// # Returns
///
/// Tensor with the same dtype as input, containing:
/// - `1` (or `1.0` for floats) where all elements are non-zero
/// - `0` (or `0.0` for floats) where any element is zero
///
/// # Examples
///
/// ```
/// # use numr::prelude::*;
/// # let device = CpuDevice::new();
/// # let client = CpuRuntime::default_client(&device);
/// use numr::ops::ReduceOps;
///
/// // Float tensor - all non-zero
/// let a = Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0, 3.0, 4.0], &[4], &device);
/// let result = client.all(&a, &[0], false)?; // 1.0 (true)
///
/// // Float tensor - contains zero
/// let a = Tensor::<CpuRuntime>::from_slice(&[1.0f32, 0.0, 3.0, 4.0], &[4], &device);
/// let result = client.all(&a, &[0], false)?; // 0.0 (false)
///
/// // Integer tensor - negative values are truthy
/// let a = Tensor::<CpuRuntime>::from_slice(&[-1i32, -2, -3], &[3], &device);
/// let result = client.all(&a, &[0], false)?; // 1 (true)
///
/// // With infinity - Inf is truthy
/// let a = Tensor::<CpuRuntime>::from_slice(&[f32::INFINITY, f32::NEG_INFINITY], &[2], &device);
/// let result = client.all(&a, &[0], false)?; // 1.0 (true)
///
/// // 2D tensor - reduce along rows
/// let a = Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0, 3.0, 0.0], &[2, 2], &device);
/// let result = client.all(&a, &[1], false)?; // [1.0, 0.0]
///
/// // Empty dimension reduction - edge case
/// let a = Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0], &[2, 1], &device);
/// let result = client.all(&a, &[1], false)?; // [1.0, 1.0] (single element is truthy)
/// # Ok::<(), numr::error::Error>(())
/// ```
///
/// # See Also
///
/// * `any` - Test if any element is true (logical OR)
/// * `prod` - Product reduction (different from logical AND)
fn all(&self, a: &Tensor<R>, dims: &[usize], keepdim: bool) -> Result<Tensor<R>>;
}