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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
//! Concatenation and related tensor manipulation operations
//!
//! This module contains functions for concatenating, stacking, splitting, tiling,
//! and repeating tensors along various dimensions.

#[cfg(feature = "gpu")]
use crate::gpu::buffer::GpuBuffer;
use crate::tensor::TensorStorage;
use crate::{Result, Tensor, TensorError};
use scirs2_core::ndarray::{ArrayD, IxDyn};
use scirs2_core::numeric::Zero;

/// Slice a tensor along specified ranges
pub fn slice<T>(tensor: &Tensor<T>, ranges: &[std::ops::Range<usize>]) -> Result<Tensor<T>>
where
    T: Clone + Default + Zero + Send + Sync + 'static,
{
    let shape = tensor.shape();

    if ranges.len() != shape.rank() {
        return Err(TensorError::invalid_argument(format!(
            "Slice ranges length {} does not match tensor rank {}",
            ranges.len(),
            shape.rank()
        )));
    }

    // Validate ranges
    for (i, range) in ranges.iter().enumerate() {
        if range.start > range.end || range.end > shape.dims()[i] {
            return Err(TensorError::invalid_argument(format!(
                "Invalid slice range {range:?} for dimension {i} of size {}",
                shape.dims()[i]
            )));
        }
    }

    match &tensor.storage {
        TensorStorage::Cpu(array) => {
            // Use ndarray's select method to extract the slice
            let out_shape: Vec<usize> = ranges.iter().map(|r| r.end - r.start).collect();

            // Create the indices for the slice
            let mut result = ArrayD::<T>::zeros(IxDyn(&out_shape));

            // Copy the sliced region
            // Note: This is not the most efficient approach but works without unsafe
            if let Some(result_slice) = result.as_slice_mut() {
                let mut idx = 0;
                let strides = tensor
                    .shape()
                    .dims()
                    .iter()
                    .rev()
                    .scan(1, |acc, &dim| {
                        let stride = *acc;
                        *acc *= dim;
                        Some(stride)
                    })
                    .collect::<Vec<_>>()
                    .into_iter()
                    .rev()
                    .collect::<Vec<_>>();

                fn copy_recursive<T: Clone>(
                    src: &ArrayD<T>,
                    dst: &mut [T],
                    ranges: &[std::ops::Range<usize>],
                    strides: &[usize],
                    current_idx: &mut usize,
                    depth: usize,
                    src_indices: &mut Vec<usize>,
                ) {
                    if depth == ranges.len() {
                        let linear_idx: usize = src_indices
                            .iter()
                            .zip(strides)
                            .map(|(idx, stride)| idx * stride)
                            .sum();
                        if let Some(val) = src.as_slice().and_then(|s| s.get(linear_idx)) {
                            dst[*current_idx] = val.clone();
                            *current_idx += 1;
                        }
                        return;
                    }

                    for i in ranges[depth].clone() {
                        src_indices.push(i);
                        copy_recursive(
                            src,
                            dst,
                            ranges,
                            strides,
                            current_idx,
                            depth + 1,
                            src_indices,
                        );
                        src_indices.pop();
                    }
                }

                let mut src_indices = Vec::new();
                copy_recursive(
                    array,
                    result_slice,
                    ranges,
                    &strides,
                    &mut idx,
                    0,
                    &mut src_indices,
                );
            }

            Ok(Tensor::from_array(result))
        }
        #[cfg(feature = "gpu")]
        TensorStorage::Gpu(gpu_buffer) => {
            gpu_slice_dispatch(gpu_buffer, tensor.shape().dims(), ranges)
        }
    }
}

/// Concatenate tensors along a specified axis
pub fn concat<T>(tensors: &[&Tensor<T>], axis: usize) -> Result<Tensor<T>>
where
    T: Clone + Default + Zero + Send + Sync + 'static,
{
    if tensors.is_empty() {
        return Err(TensorError::invalid_argument(
            "Cannot concatenate empty tensor list ".to_string(),
        ));
    }

    // Check device compatibility
    let device = tensors[0].device();
    for tensor in &tensors[1..] {
        if tensor.device() != device {
            return Err(TensorError::device_mismatch(
                "concat",
                &device.to_string(),
                &tensor.device().to_string(),
            ));
        }
    }

    // Infer result shape
    let shapes: Vec<_> = tensors.iter().map(|t| t.shape()).collect();
    let _result_shape = crate::ops::shape_inference::infer_concat(&shapes, axis as i32)?;

    // Perform concatenation based on device
    match &tensors[0].storage {
        TensorStorage::Cpu(_) => {
            // Extract CPU arrays
            let arrays: Result<Vec<_>> = tensors
                .iter()
                .map(|t| match &t.storage {
                    TensorStorage::Cpu(arr) => Ok(arr.view()),
                    #[cfg(feature = "gpu")]
                    _ => unreachable!("Device mismatch should have been caught "),
                })
                .collect();
            let arrays = arrays?;

            // Use ndarray's concatenate
            let concatenated =
                scirs2_core::ndarray::concatenate(scirs2_core::ndarray::Axis(axis), &arrays)
                    .map_err(|e| {
                        TensorError::invalid_argument(format!("Concatenation failed: {e}"))
                    })?;

            Ok(Tensor::from_array(concatenated))
        }
        #[cfg(feature = "gpu")]
        TensorStorage::Gpu(gpu_a) => {
            // Check if we're concatenating exactly two tensors and have proper GPU support
            if tensors.len() != 2 {
                return Err(TensorError::unsupported_operation_simple(
                    "GPU concatenate currently only supports exactly 2 tensors".to_string(),
                ));
            }

            // Check that the second tensor is also on GPU
            let gpu_b = match &tensors[1].storage {
                #[cfg(feature = "gpu")]
                TensorStorage::Gpu(gpu_b) => gpu_b,
                _ => {
                    return Err(TensorError::device_error_simple(
                        "All tensors must be on the same device for GPU concatenation".to_string(),
                    ))
                }
            };

            // Check type compatibility
            let type_name = std::any::type_name::<T>();
            match type_name {
                "f32" | "f64" | "i32" | "i64" => {
                    // NOTE(v0.2): Implement GPU concatenation
                    // For now, fallback to CPU implementation
                    Err(TensorError::unsupported_operation_simple(
                        "GPU concatenation not yet implemented".to_string()
                    ))
                },
                _ => {
                    Err(TensorError::unsupported_operation_simple(
                        format!("GPU concatenate not implemented for type {}. Supported types: f32, f64, i32, i64", type_name)
                    ))
                }
            }
        }
    }
}

/// Add a dimension of size 1 at the specified axis
pub fn expand_dims<T>(tensor: &Tensor<T>, axis: usize) -> Result<Tensor<T>>
where
    T: Clone + Default + Zero + Send + Sync + 'static,
{
    let mut new_shape = tensor.shape().dims().to_vec();

    if axis > new_shape.len() {
        return Err(TensorError::invalid_argument(format!(
            "Axis {axis} out of range for tensor of rank {}",
            tensor.shape().rank()
        )));
    }

    new_shape.insert(axis, 1);

    match &tensor.storage {
        TensorStorage::Cpu(array) => {
            let expanded = array
                .clone()
                .into_shape_with_order(IxDyn(&new_shape))
                .map_err(|e| TensorError::invalid_argument(format!("Expand dims failed: {e}")))?;
            Ok(Tensor::from_array(expanded))
        }
        #[cfg(feature = "gpu")]
        TensorStorage::Gpu(_) => {
            // GPU operations require T: Pod + Zeroable which isn't guaranteed for generic T
            Err(TensorError::unsupported_operation_simple(
                "GPU expand_dims not implemented for this type. Only f32 is currently supported."
                    .to_string(),
            ))
        }
    }
}

/// Stack tensors along a new axis
pub fn stack<T>(tensors: &[&Tensor<T>], axis: usize) -> Result<Tensor<T>>
where
    T: Clone + Default + Zero + Send + Sync + 'static,
{
    if tensors.is_empty() {
        return Err(TensorError::invalid_argument(
            "Cannot stack empty tensor list ".to_string(),
        ));
    }

    // All tensors must have the same shape
    let shape = tensors[0].shape();
    for tensor in &tensors[1..] {
        if tensor.shape() != shape {
            return Err(TensorError::invalid_argument(format!(
                "All tensors must have the same shape for stacking. Got {} and {}",
                shape,
                tensor.shape()
            )));
        }
    }

    // Check axis validity
    if axis > shape.rank() {
        return Err(TensorError::invalid_argument(format!(
            "Axis {axis} out of range for stacking tensors of rank {}",
            shape.rank()
        )));
    }

    // Expand dimensions and concatenate
    let expanded: Result<Vec<_>> = tensors.iter().map(|t| expand_dims(t, axis)).collect();
    let expanded = expanded?;
    let expanded_refs: Vec<_> = expanded.iter().collect();

    concat(&expanded_refs, axis)
}

/// Split a tensor into multiple tensors along an axis
pub fn split<T>(tensor: &Tensor<T>, num_splits: usize, axis: usize) -> Result<Vec<Tensor<T>>>
where
    T: Clone + Default + Zero + Send + Sync + 'static,
{
    let shape = tensor.shape();

    if axis >= shape.rank() {
        return Err(TensorError::invalid_argument(format!(
            "Axis {axis} out of range for tensor of rank {}",
            shape.rank()
        )));
    }

    let axis_size = shape.dims()[axis];
    if axis_size % num_splits != 0 {
        return Err(TensorError::invalid_argument(format!(
            "Axis size {axis_size} is not divisible by num_splits {num_splits}"
        )));
    }

    let split_size = axis_size / num_splits;
    let mut results = Vec::with_capacity(num_splits);

    for i in 0..num_splits {
        let mut ranges: Vec<_> = (0..shape.rank()).map(|j| 0..shape.dims()[j]).collect();
        ranges[axis] = (i * split_size)..((i + 1) * split_size);

        results.push(slice(tensor, &ranges)?);
    }

    Ok(results)
}

/// Tile operation - construct a tensor by repeating the input multiple times
pub fn tile<T>(tensor: &Tensor<T>, multiples: &[usize]) -> Result<Tensor<T>>
where
    T: Clone + Default + Zero + Send + Sync + 'static,
{
    if multiples.len() != tensor.shape().rank() {
        return Err(TensorError::invalid_argument(format!(
            "Multiples length {} does not match tensor rank {}",
            multiples.len(),
            tensor.shape().rank()
        )));
    }

    // Calculate output shape
    let out_shape: Vec<_> = tensor
        .shape()
        .dims()
        .iter()
        .zip(multiples)
        .map(|(&dim, &mult)| dim * mult)
        .collect();

    match &tensor.storage {
        TensorStorage::Cpu(array) => {
            // Create output array
            let mut result = ArrayD::<T>::zeros(IxDyn(&out_shape));

            // Get the input shape
            let in_shape = tensor.shape().dims();

            // Iterate through all positions in the output array
            let mut out_indices = vec![0; out_shape.len()];

            loop {
                // Calculate corresponding input indices
                let in_indices: Vec<_> = out_indices
                    .iter()
                    .zip(in_shape)
                    .map(|(&out_idx, &in_dim)| out_idx % in_dim)
                    .collect();

                // Copy the value
                result[IxDyn(&out_indices)] = array[IxDyn(&in_indices)].clone();

                // Increment output indices
                let mut carry = true;
                for i in (0..out_shape.len()).rev() {
                    if carry {
                        out_indices[i] += 1;
                        if out_indices[i] < out_shape[i] {
                            carry = false;
                        } else {
                            out_indices[i] = 0;
                        }
                    }
                }
                if carry {
                    break;
                }
            }

            Ok(Tensor::from_array(result))
        }
        #[cfg(feature = "gpu")]
        TensorStorage::Gpu(_) => gpu_tile_dispatch(tensor, multiples),
    }
}

/// Repeat operation - repeat elements of a tensor
pub fn repeat<T>(tensor: &Tensor<T>, repeats: usize, axis: Option<usize>) -> Result<Tensor<T>>
where
    T: Clone + Default + Zero + Send + Sync + 'static,
{
    if let Some(axis) = axis {
        if axis >= tensor.shape().rank() {
            return Err(TensorError::invalid_argument(format!(
                "Axis {} out of range for tensor of rank {}",
                axis,
                tensor.shape().rank()
            )));
        }
    }

    match &tensor.storage {
        TensorStorage::Cpu(array) => {
            if let Some(axis) = axis {
                // Repeat along a specific axis
                let mut out_shape = tensor.shape().dims().to_vec();
                out_shape[axis] *= repeats;

                let mut result = ArrayD::<T>::zeros(IxDyn(&out_shape));
                let _in_shape = tensor.shape().dims();

                // Iterate through all positions in the output
                let mut out_indices = vec![0; out_shape.len()];

                loop {
                    // Calculate corresponding input indices
                    let in_indices: Vec<_> = out_indices
                        .iter()
                        .enumerate()
                        .map(|(i, &out_idx)| {
                            if i == axis {
                                out_idx / repeats
                            } else {
                                out_idx
                            }
                        })
                        .collect();

                    // Copy the value
                    result[IxDyn(&out_indices)] = array[IxDyn(&in_indices)].clone();

                    // Increment output indices
                    let mut carry = true;
                    for i in (0..out_shape.len()).rev() {
                        if carry {
                            out_indices[i] += 1;
                            if out_indices[i] < out_shape[i] {
                                carry = false;
                            } else {
                                out_indices[i] = 0;
                            }
                        }
                    }
                    if carry {
                        break;
                    }
                }

                Ok(Tensor::from_array(result))
            } else {
                // Repeat the entire tensor (flatten, repeat, reshape)
                let flat = array
                    .view()
                    .into_shape_with_order((array.len(),))
                    .map_err(|e| {
                        TensorError::invalid_argument(format!("Failed to flatten: {e}"))
                    })?;

                let mut repeated = Vec::with_capacity(flat.len() * repeats);
                for elem in flat.iter() {
                    for _ in 0..repeats {
                        repeated.push(elem.clone());
                    }
                }

                let result =
                    ArrayD::from_shape_vec(IxDyn(&[repeated.len()]), repeated).map_err(|e| {
                        TensorError::invalid_argument(format!(
                            "Failed to create repeated array: {e}"
                        ))
                    })?;

                Ok(Tensor::from_array(result))
            }
        }
        #[cfg(feature = "gpu")]
        TensorStorage::Gpu(_) => gpu_repeat_dispatch(tensor, repeats, axis),
    }
}

// GPU dispatch functions

/// GPU slice dispatch for supported types
#[cfg(feature = "gpu")]
fn gpu_slice_dispatch<T>(
    gpu_buffer: &crate::gpu::buffer::GpuBuffer<T>,
    input_shape: &[usize],
    ranges: &[std::ops::Range<usize>],
) -> Result<Tensor<T>>
where
    T: Clone + Default + Zero + Send + Sync + 'static,
{
    // Currently, we only support f32 for GPU operations
    let type_name = std::any::type_name::<T>();

    if type_name == "f32" {
        // Cast to f32 buffer for the actual GPU operation
        let gpu_buffer_f32 = unsafe {
            std::mem::transmute::<
                &crate::gpu::buffer::GpuBuffer<T>,
                &crate::gpu::buffer::GpuBuffer<f32>,
            >(gpu_buffer)
        };

        // Calculate slice parameters
        let slice_starts: Vec<usize> = ranges.iter().map(|r| r.start).collect();
        let slice_ends: Vec<usize> = ranges.iter().map(|r| r.end).collect();
        let slice_steps: Vec<usize> = vec![1; ranges.len()]; // Default step of 1 for each dimension
        let output_shape: Vec<usize> = ranges.iter().map(|r| r.end - r.start).collect();
        let output_len: usize = output_shape.iter().product();

        let result_buffer = crate::gpu::ops::execute_slice(
            gpu_buffer_f32,
            &slice_starts,
            &slice_ends,
            &slice_steps,
            input_shape,
            output_len,
        )?;

        // Cast result back to T
        let result_buffer_t = unsafe {
            std::mem::transmute::<
                crate::gpu::buffer::GpuBuffer<f32>,
                crate::gpu::buffer::GpuBuffer<T>,
            >(result_buffer)
        };

        Ok(Tensor::from_gpu_buffer(
            result_buffer_t,
            crate::Shape::from_slice(&output_shape),
        ))
    } else {
        Err(TensorError::unsupported_operation_simple(format!(
            "GPU slice only supports f32, got {}",
            std::any::type_name::<T>()
        )))
    }
}

/// GPU tile dispatch for supported types
#[cfg(feature = "gpu")]
fn gpu_tile_dispatch<T>(tensor: &Tensor<T>, multiples: &[usize]) -> Result<Tensor<T>>
where
    T: Clone + Default + Zero + Send + Sync + 'static,
{
    let type_name = std::any::type_name::<T>();

    if type_name == "f32" {
        let gpu_buffer = match &tensor.storage {
            TensorStorage::Gpu(buf) => unsafe {
                std::mem::transmute::<
                    &crate::gpu::buffer::GpuBuffer<T>,
                    &crate::gpu::buffer::GpuBuffer<f32>,
                >(buf)
            },
            _ => {
                return Err(TensorError::device_error_simple(
                    "Expected GPU tensor ".to_string(),
                ))
            }
        };

        // Calculate output shape and length
        let out_shape: Vec<_> = tensor
            .shape()
            .dims()
            .iter()
            .zip(multiples)
            .map(|(&dim, &mult)| dim * mult)
            .collect();
        let output_len: usize = out_shape.iter().product();

        let result_buffer = crate::gpu::ops::execute_tile(
            gpu_buffer,
            multiples,
            tensor.shape().dims(),
            output_len,
        )?;

        let result_buffer_t = unsafe {
            std::mem::transmute::<
                crate::gpu::buffer::GpuBuffer<f32>,
                crate::gpu::buffer::GpuBuffer<T>,
            >(result_buffer)
        };

        Ok(Tensor::from_gpu_buffer(
            result_buffer_t,
            crate::Shape::from_slice(&out_shape),
        ))
    } else {
        Err(TensorError::unsupported_operation_simple(format!(
            "GPU tile only supports f32, got {}",
            std::any::type_name::<T>()
        )))
    }
}

/// GPU repeat dispatch for supported types
#[cfg(feature = "gpu")]
fn gpu_repeat_dispatch<T>(
    tensor: &Tensor<T>,
    repeats: usize,
    axis: Option<usize>,
) -> Result<Tensor<T>>
where
    T: Clone + Default + Zero + Send + Sync + 'static,
{
    let type_name = std::any::type_name::<T>();

    if type_name == "f32" {
        let gpu_buffer = match &tensor.storage {
            TensorStorage::Gpu(buf) => unsafe {
                std::mem::transmute::<
                    &crate::gpu::buffer::GpuBuffer<T>,
                    &crate::gpu::buffer::GpuBuffer<f32>,
                >(buf)
            },
            _ => {
                return Err(TensorError::device_error_simple(
                    "Expected GPU tensor ".to_string(),
                ))
            }
        };

        // Calculate output shape and length
        let out_shape = if let Some(axis) = axis {
            let mut shape = tensor.shape().dims().to_vec();
            shape[axis] *= repeats;
            shape
        } else {
            vec![tensor.shape().size() * repeats]
        };
        let output_len: usize = out_shape.iter().product();

        let result_buffer = crate::gpu::ops::execute_repeat(
            gpu_buffer,
            &[repeats], // Convert single repeat value to slice
            axis,
            tensor.shape().dims(),
            output_len,
        )?;

        let result_buffer_t = unsafe {
            std::mem::transmute::<
                crate::gpu::buffer::GpuBuffer<f32>,
                crate::gpu::buffer::GpuBuffer<T>,
            >(result_buffer)
        };

        Ok(Tensor::from_gpu_buffer(
            result_buffer_t,
            crate::Shape::from_slice(&out_shape),
        ))
    } else {
        Err(TensorError::unsupported_operation_simple(format!(
            "GPU repeat only supports f32, got {}",
            std::any::type_name::<T>()
        )))
    }
}