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
//! Transpose and permutation operations for tensors
//!
//! This module contains functions for transposing tensors, flipping axes,
//! rolling elements, and other permutation-based tensor manipulations.
//!
//! All operations support both CPU and GPU execution paths when the GPU
//! feature is enabled.

#[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;
#[cfg(feature = "gpu")]
use wgpu::util::DeviceExt;

/// Transpose a tensor (reverse all dimensions)
pub fn transpose<T>(tensor: &Tensor<T>) -> Result<Tensor<T>>
where
    T: Clone + Default + Zero + Send + Sync + 'static,
{
    transpose_axes(tensor, None)
}

/// Transpose a tensor with specified axis permutation
pub fn transpose_axes<T>(tensor: &Tensor<T>, axes: Option<&[usize]>) -> Result<Tensor<T>>
where
    T: Clone + Default + Zero + Send + Sync + 'static,
{
    let rank = tensor.shape().rank();

    // Default axes: reverse all dimensions
    let default_axes: Vec<usize> = (0..rank).rev().collect();
    let axes = axes.unwrap_or(&default_axes);

    // Validate axes
    if axes.len() != rank {
        return Err(TensorError::invalid_argument(format!(
            "Axes length {} does not match tensor rank {rank}",
            axes.len()
        )));
    }

    // Check for valid permutation
    let mut seen = vec![false; rank];
    for &axis in axes {
        if axis >= rank {
            return Err(TensorError::invalid_argument(format!(
                "Axis {axis} out of range for tensor of rank {rank}"
            )));
        }
        if seen[axis] {
            return Err(TensorError::invalid_argument(format!(
                "Duplicate axis: {axis}"
            )));
        }
        seen[axis] = true;
    }

    match &tensor.storage {
        TensorStorage::Cpu(array) => {
            // Use ndarray's permuted_axes
            let new_array = array.clone().permuted_axes(axes);
            Ok(Tensor::from_array(new_array))
        }
        #[cfg(feature = "gpu")]
        TensorStorage::Gpu(gpu_buffer) => {
            gpu_transpose_dispatch(gpu_buffer, tensor.shape().dims(), axes)
        }
    }
}

/// Roll operation - roll tensor elements along specified axes
pub fn roll<T>(tensor: &Tensor<T>, shift: isize, 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 {
                // Roll along a specific axis
                let axis_size = tensor.shape().dims()[axis] as isize;
                let normalized_shift = ((shift % axis_size) + axis_size) % axis_size;

                if normalized_shift == 0 {
                    return Ok(tensor.clone());
                }

                let mut result = array.clone();
                let shape = tensor.shape().dims();

                // Create indices for iteration
                let mut indices = vec![0; shape.len()];

                loop {
                    // Calculate source indices (where to copy from)
                    let mut src_indices = indices.clone();
                    src_indices[axis] = ((indices[axis] as isize - normalized_shift + axis_size)
                        % axis_size) as usize;

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

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

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

                let len = flat.len() as isize;
                let normalized_shift = ((shift % len) + len) % len;

                if normalized_shift == 0 {
                    return Ok(tensor.clone());
                }

                let mut rolled = Vec::with_capacity(flat.len());

                // Copy elements in rolled order
                for i in 0..flat.len() {
                    let src_idx = ((i as isize - normalized_shift + len) % len) as usize;
                    rolled.push(flat[src_idx].clone());
                }

                let rolled_array =
                    ArrayD::from_shape_vec(array.raw_dim(), rolled).map_err(|e| {
                        TensorError::invalid_argument(format!("Failed to create rolled array: {e}"))
                    })?;

                Ok(Tensor::from_array(rolled_array))
            }
        }
        #[cfg(feature = "gpu")]
        TensorStorage::Gpu(_) => gpu_roll_dispatch(tensor, shift, axis),
    }
}

/// Flip (reverse) tensor along specified axes
pub fn flip<T>(tensor: &Tensor<T>, axes: &[usize]) -> Result<Tensor<T>>
where
    T: Clone + Default + Zero + Send + Sync + 'static + bytemuck::Pod + bytemuck::Zeroable,
{
    let shape = tensor.shape();
    let rank = shape.rank();

    // Validate axes
    for &axis in axes {
        if axis >= rank {
            return Err(TensorError::invalid_argument(format!(
                "Axis {axis} out of range for tensor of rank {rank}"
            )));
        }
    }

    match &tensor.storage {
        TensorStorage::Cpu(array) => {
            let mut result = array.clone();

            // Flip along each specified axis
            for &axis in axes {
                result.invert_axis(scirs2_core::ndarray::Axis(axis));
            }

            Ok(Tensor::from_array(result))
        }
        #[cfg(feature = "gpu")]
        TensorStorage::Gpu(_) => {
            // For GPU, we need to flip along each axis sequentially
            let mut result = tensor.clone();
            for &axis in axes {
                result = gpu_flip_dispatch(&result, axis)?;
            }
            Ok(result)
        }
    }
}

/// GPU transpose dispatch for supported types
#[cfg(feature = "gpu")]
fn gpu_transpose_dispatch<T>(
    gpu_buffer: &crate::gpu::buffer::GpuBuffer<T>,
    input_shape: &[usize],
    axes: &[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 output shape
        let output_shape: Vec<usize> = axes.iter().map(|&i| input_shape[i]).collect();
        let output_len: usize = output_shape.iter().product();

        let result_buffer =
            crate::gpu::ops::execute_transpose(gpu_buffer_f32, axes, 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 transpose only supports f32, got {}",
            std::any::type_name::<T>()
        )))
    }
}

/// GPU roll dispatch for supported types
#[cfg(feature = "gpu")]
fn gpu_roll_dispatch<T>(tensor: &Tensor<T>, shift: isize, 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(),
                ))
            }
        };

        // Convert single values to arrays for GPU function
        let shifts = &[shift as i32];
        let axes = axis.as_ref().map(|a| std::slice::from_ref(a));

        let result_buffer =
            crate::gpu::ops::execute_roll(gpu_buffer, shifts, axes, tensor.shape().dims())?;

        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,
            tensor.shape().clone(),
        ))
    } else {
        Err(TensorError::unsupported_operation_simple(format!(
            "GPU roll only supports f32, got {}",
            std::any::type_name::<T>()
        )))
    }
}

/// GPU flip dispatch for supported types
#[cfg(feature = "gpu")]
fn gpu_flip_dispatch<T>(tensor: &Tensor<T>, axis: usize) -> Result<Tensor<T>>
where
    T: bytemuck::Pod + bytemuck::Zeroable + Clone + Default + Send + Sync + 'static,
{
    let shape = tensor.shape().dims();
    let total_size = shape.iter().product::<usize>();

    if let TensorStorage::Gpu(gpu_buffer) = &tensor.storage {
        let gpu_ctx = crate::device::context::get_gpu_context(gpu_buffer.device_enum().id())?;

        // Create output buffer
        let result_buffer = gpu_ctx.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("flip_output"),
            size: (total_size * std::mem::size_of::<T>()) as u64,
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
            mapped_at_creation: false,
        });

        // Create shape buffer
        let shape_data: Vec<u32> = shape.iter().map(|&s| s as u32).collect();
        let shape_buffer = gpu_ctx
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("flip_shape"),
                contents: bytemuck::cast_slice(&shape_data),
                usage: wgpu::BufferUsages::STORAGE,
            });

        // Create uniform buffer
        let info = [axis as u32, shape.len() as u32, total_size as u32, 0u32];
        let uniform_buffer = gpu_ctx
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("flip_uniform"),
                contents: bytemuck::cast_slice(&info),
                usage: wgpu::BufferUsages::UNIFORM,
            });

        // Create compute shader
        let shader = gpu_ctx
            .device
            .create_shader_module(wgpu::ShaderModuleDescriptor {
                label: Some("flip_shader"),
                source: wgpu::ShaderSource::Wgsl(
                    include_str!("../../gpu/shaders/manipulation_ops2.wgsl").into(),
                ),
            });

        // Create bind group layout
        let bind_group_layout =
            gpu_ctx
                .device
                .create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                    label: Some("flip_bind_group_layout"),
                    entries: &[
                        wgpu::BindGroupLayoutEntry {
                            binding: 0,
                            visibility: wgpu::ShaderStages::COMPUTE,
                            ty: wgpu::BindingType::Buffer {
                                ty: wgpu::BufferBindingType::Storage { read_only: true },
                                has_dynamic_offset: false,
                                min_binding_size: None,
                            },
                            count: None,
                        },
                        wgpu::BindGroupLayoutEntry {
                            binding: 1,
                            visibility: wgpu::ShaderStages::COMPUTE,
                            ty: wgpu::BindingType::Buffer {
                                ty: wgpu::BufferBindingType::Storage { read_only: false },
                                has_dynamic_offset: false,
                                min_binding_size: None,
                            },
                            count: None,
                        },
                        wgpu::BindGroupLayoutEntry {
                            binding: 2,
                            visibility: wgpu::ShaderStages::COMPUTE,
                            ty: wgpu::BindingType::Buffer {
                                ty: wgpu::BufferBindingType::Uniform,
                                has_dynamic_offset: false,
                                min_binding_size: None,
                            },
                            count: None,
                        },
                        wgpu::BindGroupLayoutEntry {
                            binding: 3,
                            visibility: wgpu::ShaderStages::COMPUTE,
                            ty: wgpu::BindingType::Buffer {
                                ty: wgpu::BufferBindingType::Storage { read_only: true },
                                has_dynamic_offset: false,
                                min_binding_size: None,
                            },
                            count: None,
                        },
                    ],
                });

        // Create bind group
        let bind_group = gpu_ctx
            .device
            .create_bind_group(&wgpu::BindGroupDescriptor {
                label: Some("flip_bind_group"),
                layout: &bind_group_layout,
                entries: &[
                    wgpu::BindGroupEntry {
                        binding: 0,
                        resource: gpu_buffer.buffer().as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 1,
                        resource: result_buffer.as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 2,
                        resource: uniform_buffer.as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 3,
                        resource: shape_buffer.as_entire_binding(),
                    },
                ],
            });

        // Create compute pipeline
        let pipeline_layout =
            gpu_ctx
                .device
                .create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
                    label: Some("flip_pipeline_layout"),
                    bind_group_layouts: &[Some(&bind_group_layout)],
                    immediate_size: 0,
                });

        let compute_pipeline =
            gpu_ctx
                .device
                .create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
                    label: Some("flip_pipeline"),
                    layout: Some(&pipeline_layout),
                    module: &shader,
                    entry_point: Some("flip_op"),
                    cache: None,
                    compilation_options: Default::default(),
                });

        // Execute compute shader
        let mut encoder = gpu_ctx
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("flip_encoder"),
            });

        {
            let mut compute_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("flip_pass"),
                timestamp_writes: None,
            });
            compute_pass.set_pipeline(&compute_pipeline);
            compute_pass.set_bind_group(0, &bind_group, &[]);
            compute_pass.dispatch_workgroups((total_size as u32 + 63) / 64, 1, 1);
        }

        gpu_ctx.queue.submit(std::iter::once(encoder.finish()));

        let result_buffer = GpuBuffer::from_raw_buffer(
            result_buffer,
            gpu_ctx.device.clone(),
            gpu_ctx.queue.clone(),
            gpu_buffer.device_enum(),
            total_size,
        );

        Ok(Tensor::from_gpu_buffer(
            result_buffer,
            crate::Shape::from_slice(shape),
        ))
    } else {
        Err(TensorError::device_mismatch("transpose", "GPU", "CPU"))
    }
}