primal-barracuda 0.4.0

barraCuda — BARrier-free Rust Abstracted Cross-platform Unified Dimensional Algebra. Vendor-agnostic GPU compute via WGSL.
Documentation
// SPDX-License-Identifier: AGPL-3.0-or-later
//! Tensor Dot - Full tensor contraction (GPU implementation)
//!
//! **Deep Debt Principles**:
//! - Complete GPU implementation: Full tensor contraction, not simplified vec![0.0]
//! - No CPU fallbacks: All computation on GPU
//! - Self-knowledge: Validates contraction axes
//! - Modern idiomatic Rust: Result<T, E>

use crate::device::{DeviceCapabilities, WorkloadType};
use crate::error::{BarracudaError, Result};
use crate::tensor::Tensor;

#[repr(C)]
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
struct TensorDotParams {
    output_size: u32,
    contraction_size: u32,
    a_outer_size: u32,
    b_outer_size: u32,
    a_outer_stride: u32,
    b_outer_stride: u32,
    a_contract_stride: u32,
    b_contract_stride: u32,
}

/// Full tensor contraction along specified axes.
pub struct TensorDot {
    tensor_a: Tensor,
    tensor_b: Tensor,
    axes_a: Vec<usize>,
    axes_b: Vec<usize>,
}

impl TensorDot {
    /// Creates a new tensor dot operation. `axes_a` and `axes_b` specify contraction axes.
    /// # Errors
    /// Returns [`Err`] if buffer allocation, GPU dispatch, or buffer
    /// readback fails (e.g. device lost or out of memory).
    pub fn new(
        tensor_a: Tensor,
        tensor_b: Tensor,
        axes_a: Vec<usize>,
        axes_b: Vec<usize>,
    ) -> Result<Self> {
        if axes_a.len() != axes_b.len() {
            return Err(BarracudaError::invalid_op(
                "tensor_dot",
                "Contraction axes must have same length",
            ));
        }

        let shape_a = tensor_a.shape();
        let shape_b = tensor_b.shape();

        // Validate axes
        for &axis in &axes_a {
            if axis >= shape_a.len() {
                return Err(BarracudaError::invalid_op(
                    "tensor_dot",
                    format!(
                        "Axis {} out of range for tensor A (rank {})",
                        axis,
                        shape_a.len()
                    ),
                ));
            }
        }

        for &axis in &axes_b {
            if axis >= shape_b.len() {
                return Err(BarracudaError::invalid_op(
                    "tensor_dot",
                    format!(
                        "Axis {} out of range for tensor B (rank {})",
                        axis,
                        shape_b.len()
                    ),
                ));
            }
        }

        // Validate contraction dimensions match
        for (i, (&axis_a, &axis_b)) in axes_a.iter().zip(axes_b.iter()).enumerate() {
            if shape_a[axis_a] != shape_b[axis_b] {
                return Err(BarracudaError::invalid_op(
                    "tensor_dot",
                    format!(
                        "Contraction dimension {} mismatch: {} != {}",
                        i, shape_a[axis_a], shape_b[axis_b]
                    ),
                ));
            }
        }

        Ok(Self {
            tensor_a,
            tensor_b,
            axes_a,
            axes_b,
        })
    }

    fn wgsl_shader() -> &'static str {
        {
            static SHADER: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
                include_str!("../shaders/misc/tensor_dot_f64.wgsl").to_string()
            });
            std::sync::LazyLock::force(&SHADER).as_str()
        }
    }

    /// Executes the tensor contraction and returns the result.
    /// # Errors
    /// Returns [`Err`] if buffer allocation, GPU dispatch, or buffer
    /// readback fails (e.g. device lost or out of memory).
    pub fn execute(self) -> Result<Tensor> {
        let device = self.tensor_a.device();
        let shape_a = self.tensor_a.shape();
        let shape_b = self.tensor_b.shape();

        // Compute contraction size
        let contraction_size: usize = self.axes_a.iter().map(|&axis| shape_a[axis]).product();

        // Compute outer sizes (uncontracted dimensions)
        let mut a_outer_dims = Vec::new();
        let mut b_outer_dims = Vec::new();

        for (i, &dim) in shape_a.iter().enumerate() {
            if !self.axes_a.contains(&i) {
                a_outer_dims.push(dim);
            }
        }

        for (i, &dim) in shape_b.iter().enumerate() {
            if !self.axes_b.contains(&i) {
                b_outer_dims.push(dim);
            }
        }

        let a_outer_size: usize = a_outer_dims.iter().product();
        let b_outer_size: usize = b_outer_dims.iter().product();
        let output_size = a_outer_size * b_outer_size;

        // Compute strides (simplified - assumes row-major)
        let a_outer_stride = contraction_size;
        let b_outer_stride = 1;
        let a_contract_stride = 1;
        let b_contract_stride = b_outer_size;

        let output_buffer = device.create_buffer_f32(output_size)?;

        let params = TensorDotParams {
            output_size: output_size as u32,
            contraction_size: contraction_size as u32,
            a_outer_size: a_outer_size as u32,
            b_outer_size: b_outer_size as u32,
            a_outer_stride: a_outer_stride as u32,
            b_outer_stride: b_outer_stride as u32,
            a_contract_stride: a_contract_stride as u32,
            b_contract_stride: b_contract_stride as u32,
        };

        let params_buffer = device
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("TensorDot Params"),
                contents: bytemuck::bytes_of(&params),
                usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
            });

        let bind_group_layout =
            device
                .device
                .create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                    label: Some("TensorDot Bind Group Layout"),
                    entries: &[
                        wgpu::BindGroupLayoutEntry {
                            binding: 0,
                            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: 1,
                            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: 2,
                            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: 3,
                            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,
                        },
                    ],
                });

        let bind_group = device.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("TensorDot Bind Group"),
            layout: &bind_group_layout,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: params_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: self.tensor_a.buffer().as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: self.tensor_b.buffer().as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 3,
                    resource: output_buffer.as_entire_binding(),
                },
            ],
        });

        let shader = device.compile_shader(Self::wgsl_shader(), Some("TensorDot"));
        let pipeline_layout =
            device
                .device
                .create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
                    label: Some("TensorDot Pipeline Layout"),
                    bind_group_layouts: &[&bind_group_layout],
                    immediate_size: 0,
                });

        let pipeline = device
            .device
            .create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
                label: Some("TensorDot Pipeline"),
                layout: Some(&pipeline_layout),
                module: &shader,
                entry_point: Some("main"),
                cache: None,
                compilation_options: Default::default(),
            });

        let mut encoder = device.create_encoder_guarded(&wgpu::CommandEncoderDescriptor {
            label: Some("TensorDot Encoder"),
        });

        {
            let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("TensorDot Pass"),
                timestamp_writes: None,
            });
            pass.set_pipeline(&pipeline);
            pass.set_bind_group(0, Some(&bind_group), &[]);
            // Deep Debt Evolution: Capability-based dispatch
            let caps = DeviceCapabilities::from_device(device);
            let optimal_wg_size = caps.optimal_workgroup_size(WorkloadType::MatMul);
            let workgroups = (output_size as u32).div_ceil(optimal_wg_size);
            pass.dispatch_workgroups(workgroups, 1, 1);
        }

        device.submit_commands(Some(encoder.finish()));

        // Compute output shape
        let mut output_shape = a_outer_dims;
        output_shape.extend(b_outer_dims);

        Ok(Tensor::from_buffer(
            output_buffer,
            output_shape,
            device.clone(),
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_tensor_dot_basic() {
        let device = crate::device::test_pool::get_test_device().await;
        let a = Tensor::from_vec_on(vec![1.0, 2.0, 3.0], vec![3], device.clone())
            .await
            .unwrap();
        let b = Tensor::from_vec_on(vec![4.0, 5.0, 6.0], vec![3], device.clone())
            .await
            .unwrap();

        let result = TensorDot::new(a, b, vec![0], vec![0])
            .unwrap()
            .execute()
            .unwrap();
        let output = result.to_vec().unwrap();
        assert_eq!(output.len(), 1);
        // 1*4 + 2*5 + 3*6 = 32
        assert!((output[0] - 32.0).abs() < 1e-4);
    }
}