tenflowers-core 0.1.1

Core tensor operations and execution engine for TenfloweRS
Documentation
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! Argument-based reduction operations
//!
//! This module contains reduction operations that return indices rather than values,
//! such as argmax, argmin, and topk operations. These functions are useful for
//! finding the positions of specific elements in tensors.

use super::common::normalize_axis;
use crate::tensor::TensorStorage;
use crate::{Result, Tensor, TensorError};
use scirs2_core::ndarray::{ArrayD, Axis};

/// Compute argmax along axes
pub fn argmax<T>(x: &Tensor<T>, axis: Option<i32>, keepdims: bool) -> Result<Tensor<usize>>
where
    T: Clone + Default + PartialOrd + Send + Sync + 'static + bytemuck::Pod + bytemuck::Zeroable,
{
    match &x.storage {
        TensorStorage::Cpu(arr) => {
            if let Some(axis) = axis {
                let axis = normalize_axis(axis, x.shape().rank() as i32)?;

                // Map along the axis to find argmax indices
                let indices = arr.map_axis(Axis(axis), |view| {
                    view.iter()
                        .enumerate()
                        .max_by(|(_, a), (_, b)| {
                            a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal)
                        })
                        .map(|(idx, _)| idx)
                        .unwrap_or(0)
                });

                let result = if keepdims {
                    indices.insert_axis(Axis(axis))
                } else {
                    indices
                };

                Ok(Tensor::from_array(result))
            } else {
                // Find argmax of flattened array
                let (max_idx, _) = arr
                    .iter()
                    .enumerate()
                    .max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
                    .unwrap_or((0, &T::default()));

                let result = if keepdims {
                    ArrayD::from_elem(vec![1; x.shape().rank()], max_idx)
                } else {
                    ArrayD::from_elem(vec![], max_idx)
                };
                Ok(Tensor::from_array(result))
            }
        }
        #[cfg(feature = "gpu")]
        TensorStorage::Gpu(gpu_buffer) => {
            use crate::gpu::ops::{execute_axis_reduction_op, ReductionOp};

            // For argmax, we need to handle the result as indices (usize) but GPU returns f32
            // Calculate output shape and size
            let result_shape = if let Some(axis) = axis {
                let mut shape = x.shape().dims().to_vec();
                let normalized_axis = normalize_axis(axis, x.shape().rank() as i32)?;
                if keepdims {
                    shape[normalized_axis] = 1;
                } else {
                    shape.remove(normalized_axis);
                }
                shape
            } else if keepdims {
                vec![1; x.shape().rank()]
            } else {
                vec![]
            };

            let output_len = if result_shape.is_empty() {
                1
            } else {
                result_shape.iter().product()
            };

            // Convert axis to the format expected by GPU operations
            let axes_array;
            let gpu_axes = if let Some(axis) = axis {
                let normalized_axis = normalize_axis(axis, x.shape().rank() as i32)?;
                axes_array = vec![normalized_axis as i32];
                Some(axes_array.as_slice())
            } else {
                None
            };

            let result_buffer = execute_axis_reduction_op(
                gpu_buffer,
                ReductionOp::ArgMax,
                x.shape().dims(),
                gpu_axes,
                keepdims,
                output_len,
            )?;

            // The GPU returns f32 values representing indices, we need to convert them to usize
            // For now, we'll create a new tensor and handle the type conversion
            let f32_tensor =
                Tensor::from_gpu_buffer(result_buffer, crate::Shape::new(result_shape.clone()));

            // Convert f32 indices to usize indices by reading from GPU and converting
            match &f32_tensor.storage {
                TensorStorage::Gpu(gpu_buf) => {
                    // For now, fall back to CPU for the final conversion
                    // In a production system, this would be optimized to stay on GPU
                    let cpu_tensor = f32_tensor.to_device(crate::Device::Cpu)?;
                    match &cpu_tensor.storage {
                        TensorStorage::Cpu(arr) => {
                            let indices: Vec<usize> = arr
                                .iter()
                                .map(|&f| {
                                    // Safe conversion from f32 to usize for indices
                                    if let Ok(f_val) = bytemuck::try_cast::<T, f32>(f) {
                                        f_val as usize
                                    } else {
                                        0 // fallback value
                                    }
                                })
                                .collect();
                            let indices_tensor = Tensor::from_vec(indices, &result_shape)?;
                            Ok(indices_tensor)
                        }
                        _ => unreachable!(),
                    }
                }
                _ => unreachable!(),
            }
        }
    }
}

/// Compute argmin along axes
pub fn argmin<T>(x: &Tensor<T>, axis: Option<i32>, keepdims: bool) -> Result<Tensor<usize>>
where
    T: Clone + Default + PartialOrd + Send + Sync + 'static + bytemuck::Pod + bytemuck::Zeroable,
{
    match &x.storage {
        TensorStorage::Cpu(arr) => {
            if let Some(axis) = axis {
                let axis = normalize_axis(axis, x.shape().rank() as i32)?;

                // Map along the axis to find argmin indices
                let indices = arr.map_axis(Axis(axis), |view| {
                    view.iter()
                        .enumerate()
                        .min_by(|(_, a), (_, b)| {
                            a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal)
                        })
                        .map(|(idx, _)| idx)
                        .unwrap_or(0)
                });

                let result = if keepdims {
                    indices.insert_axis(Axis(axis))
                } else {
                    indices
                };

                Ok(Tensor::from_array(result))
            } else {
                // Find argmin of flattened array
                let (min_idx, _) = arr
                    .iter()
                    .enumerate()
                    .min_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
                    .unwrap_or((0, &T::default()));

                let result = if keepdims {
                    ArrayD::from_elem(vec![1; x.shape().rank()], min_idx)
                } else {
                    ArrayD::from_elem(vec![], min_idx)
                };
                Ok(Tensor::from_array(result))
            }
        }
        #[cfg(feature = "gpu")]
        TensorStorage::Gpu(gpu_buffer) => {
            use crate::gpu::ops::{execute_axis_reduction_op, ReductionOp};

            // For argmin, we need to handle the result as indices (usize) but GPU returns f32
            // Calculate output shape and size
            let result_shape = if let Some(axis) = axis {
                let mut shape = x.shape().dims().to_vec();
                let normalized_axis = normalize_axis(axis, x.shape().rank() as i32)?;
                if keepdims {
                    shape[normalized_axis] = 1;
                } else {
                    shape.remove(normalized_axis);
                }
                shape
            } else if keepdims {
                vec![1; x.shape().rank()]
            } else {
                vec![]
            };

            let output_len = if result_shape.is_empty() {
                1
            } else {
                result_shape.iter().product()
            };

            // Convert axis to the format expected by GPU operations
            let axes_array;
            let gpu_axes = if let Some(axis) = axis {
                let normalized_axis = normalize_axis(axis, x.shape().rank() as i32)?;
                axes_array = vec![normalized_axis as i32];
                Some(axes_array.as_slice())
            } else {
                None
            };

            let result_buffer = execute_axis_reduction_op(
                gpu_buffer,
                ReductionOp::ArgMin,
                x.shape().dims(),
                gpu_axes,
                keepdims,
                output_len,
            )?;

            // The GPU returns f32 values representing indices, we need to convert them to usize
            // For now, we'll create a new tensor and handle the type conversion
            let f32_tensor =
                Tensor::from_gpu_buffer(result_buffer, crate::Shape::new(result_shape.clone()));

            // Convert f32 indices to usize indices by reading from GPU and converting
            match &f32_tensor.storage {
                TensorStorage::Gpu(gpu_buf) => {
                    // For now, fall back to CPU for the final conversion
                    // In a production system, this would be optimized to stay on GPU
                    let cpu_tensor = f32_tensor.to_device(crate::Device::Cpu)?;
                    match &cpu_tensor.storage {
                        TensorStorage::Cpu(arr) => {
                            let indices: Vec<usize> = arr
                                .iter()
                                .map(|&f| {
                                    // Safe conversion from f32 to usize for indices
                                    if let Ok(f_val) = bytemuck::try_cast::<T, f32>(f) {
                                        f_val as usize
                                    } else {
                                        0 // fallback value
                                    }
                                })
                                .collect();
                            let indices_tensor = Tensor::from_vec(indices, &result_shape)?;
                            Ok(indices_tensor)
                        }
                        _ => unreachable!(),
                    }
                }
                _ => unreachable!(),
            }
        }
    }
}

/// Find the top K largest elements along the last axis
/// Returns (values, indices) where both have shape [..., k]
pub fn topk<T>(x: &Tensor<T>, k: usize, axis: Option<i32>) -> Result<(Tensor<T>, Tensor<usize>)>
where
    T: Clone + Default + PartialOrd + Send + Sync + 'static,
{
    match &x.storage {
        TensorStorage::Cpu(arr) => {
            let shape = arr.shape();
            let default_axis = shape.len() as i32 - 1; // Default to last axis
            let axis = normalize_axis(axis.unwrap_or(default_axis), shape.len() as i32)?;
            let axis_size = shape[axis];

            if k > axis_size {
                return Err(TensorError::invalid_argument(format!(
                    "k={k} is larger than axis size={axis_size}"
                )));
            }

            // Create output shapes
            let mut values_shape = shape.to_vec();
            let mut indices_shape = shape.to_vec();
            values_shape[axis] = k;
            indices_shape[axis] = k;

            // Initialize output arrays
            let mut values_data = vec![T::default(); values_shape.iter().product()];
            let mut indices_data = vec![0usize; indices_shape.iter().product()];

            // Calculate strides for iteration
            let mut strides = vec![1; shape.len()];
            for i in (0..shape.len() - 1).rev() {
                strides[i] = strides[i + 1] * shape[i + 1];
            }

            // Calculate output strides
            let mut out_strides = vec![1; values_shape.len()];
            for i in (0..values_shape.len() - 1).rev() {
                out_strides[i] = out_strides[i + 1] * values_shape[i + 1];
            }

            // Iterate over all slices along the specified axis
            let total_elements: usize = shape.iter().product();
            let slices_count = total_elements / axis_size;

            for slice_idx in 0..slices_count {
                // Calculate the starting index for this slice
                let mut coords = vec![0; shape.len()];
                let mut remaining = slice_idx;

                for (i, &dim_size) in shape.iter().enumerate() {
                    if i == axis {
                        continue;
                    }
                    let reduced_stride = total_elements / (dim_size * axis_size);
                    coords[i] = remaining / reduced_stride;
                    remaining %= reduced_stride;
                }

                // Collect values and their indices along the axis
                let mut values_with_indices: Vec<(T, usize)> = Vec::with_capacity(axis_size);
                for i in 0..axis_size {
                    coords[axis] = i;
                    let linear_idx = coords
                        .iter()
                        .zip(strides.iter())
                        .map(|(&c, &s)| c * s)
                        .sum::<usize>();

                    if let Some(val) = arr.as_slice() {
                        values_with_indices.push((val[linear_idx].clone(), i));
                    }
                }

                // Sort by value in descending order and take top k
                values_with_indices
                    .sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(std::cmp::Ordering::Equal));
                values_with_indices.truncate(k);

                // Store results
                for (rank, (value, orig_idx)) in values_with_indices.iter().enumerate() {
                    coords[axis] = rank;
                    let out_linear_idx = coords
                        .iter()
                        .zip(out_strides.iter())
                        .map(|(&c, &s)| c * s)
                        .sum::<usize>();

                    values_data[out_linear_idx] = value.clone();
                    indices_data[out_linear_idx] = *orig_idx;
                }
            }

            let values_tensor = Tensor::from_vec(values_data, &values_shape)?;
            let indices_tensor = Tensor::from_vec(indices_data, &indices_shape)?;

            Ok((values_tensor, indices_tensor))
        }
        #[cfg(feature = "gpu")]
        TensorStorage::Gpu(gpu_buffer) => {
            // GPU topk implementation
            let shape = x.shape();
            let default_axis = shape.rank() as i32 - 1; // Default to last axis
            let axis = normalize_axis(axis.unwrap_or(default_axis), shape.rank() as i32)?;
            let axis_size = shape.dims()[axis];
            let total_elements: usize = shape.dims().iter().product();
            let num_slices = total_elements / axis_size;

            // Use type-safe transmute to call GPU topk with correct types
            let (values_buffer, indices_buffer) = if std::any::type_name::<T>() == "f32" {
                let gpu_buffer_f32 = unsafe {
                    std::mem::transmute::<
                        &crate::gpu::buffer::GpuBuffer<T>,
                        &crate::gpu::buffer::GpuBuffer<f32>,
                    >(gpu_buffer)
                };
                // NOTE(v0.2): Implement GPU topk operation
                return Err(TensorError::unsupported_operation_simple(
                    "GPU topk operation not yet implemented".to_string(),
                ));
            } else {
                return Err(TensorError::unsupported_operation_simple(format!(
                    "GPU topk only supports f32 currently, got {}",
                    std::any::type_name::<T>()
                )));
            };

            #[allow(unreachable_code)]
            // Create output tensors
            // Compute output shape: same as input but axis dimension becomes k
            let mut values_shape = shape.dims().to_vec();
            values_shape[axis] = k;
            let indices_shape = values_shape.clone();

            let values_tensor =
                Tensor::from_gpu_buffer(values_buffer, crate::Shape::new(values_shape));
            let indices_tensor =
                Tensor::from_gpu_buffer(indices_buffer, crate::Shape::new(indices_shape));

            Ok((values_tensor, indices_tensor))
        }
    }
}