primal-barracuda 0.4.0

barraCuda — BARrier-free Rust Abstracted Cross-platform Unified Dimensional Algebra. Vendor-agnostic GPU compute via WGSL.
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
// SPDX-License-Identifier: AGPL-3.0-or-later
//! Cumulative Product (f64) - GPU-accelerated prefix product
//!
//! Computes cumulative product along a dimension in double precision.
//! For input [a, b, c, d], output is [a, a*b, a*b*c, a*b*c*d].
//!
//! **Use cases**:
//! - Probability chains
//! - Running products
//! - Gamma function approximations
//! - Factorial computations
//!
//! **Deep Debt Principles**:
//! - Pure WGSL implementation (hardware-agnostic)
//! - Full f64 precision via SPIR-V/Vulkan
//! - Zero unsafe code
//! - Self-contained (no external dependencies)

use crate::device::WgpuDevice;
use crate::device::capabilities::WORKGROUP_SIZE_1D;
use crate::error::{BarracudaError, Result};
use crate::tensor::Tensor;
use bytemuck::{Pod, Zeroable};
use std::sync::Arc;

use wgpu;

/// Parameters passed to the `cumprod_f64` shader
#[repr(C)]
#[derive(Debug, Clone, Copy, Pod, Zeroable)]
struct CumprodF64Params {
    size: u32,       // Total number of elements
    dim_size: u32,   // Size of the dimension to scan along
    outer_size: u32, // Product of dimensions before dim
    inner_size: u32, // Product of dimensions after dim
}

/// Cumulative product variants
pub enum CumprodVariant {
    /// Standard inclusive cumprod: [a, a*b, a*b*c, ...]
    Inclusive,
    /// Exclusive cumprod (shifted): [1, a, a*b, ...]
    Exclusive,
    /// Reverse cumprod: [a*b*c*d, b*c*d, c*d, d]
    Reverse,
    /// Log-domain cumprod (numerically stable)
    LogDomain,
}

/// F64 cumulative product operation
pub struct CumprodF64 {
    input: Tensor,
    dim: usize,
    variant: CumprodVariant,
}

impl CumprodF64 {
    /// Create a new inclusive cumprod operation along the specified dimension
    #[must_use]
    pub fn new(input: Tensor, dim: usize) -> Self {
        Self {
            input,
            dim,
            variant: CumprodVariant::Inclusive,
        }
    }

    /// Create an exclusive cumprod (shifted, starts with 1)
    #[must_use]
    pub fn exclusive(input: Tensor, dim: usize) -> Self {
        Self {
            input,
            dim,
            variant: CumprodVariant::Exclusive,
        }
    }

    /// Create a reverse cumprod (from end to start)
    #[must_use]
    pub fn reverse(input: Tensor, dim: usize) -> Self {
        Self {
            input,
            dim,
            variant: CumprodVariant::Reverse,
        }
    }

    /// Create a log-domain cumprod (numerically stable for long sequences)
    #[must_use]
    pub fn log_domain(input: Tensor, dim: usize) -> Self {
        Self {
            input,
            dim,
            variant: CumprodVariant::LogDomain,
        }
    }

    /// WGSL shader source for f64 cumprod
    fn shader() -> &'static str {
        include_str!("../shaders/reduce/cumprod_f64.wgsl")
    }

    fn entry_point(&self) -> &'static str {
        match self.variant {
            CumprodVariant::Inclusive => "cumprod_f64",
            CumprodVariant::Exclusive => "cumprod_exclusive_f64",
            CumprodVariant::Reverse => "cumprod_reverse_f64",
            CumprodVariant::LogDomain => "cumprod_log_f64",
        }
    }

    /// Execute the cumprod operation
    /// # Errors
    /// Returns [`Err`] if dimension is out of range for the tensor rank, buffer allocation fails,
    /// GPU dispatch fails, or the device is lost.
    pub fn execute(self) -> Result<Tensor> {
        let _device = self.input.device();
        let shape = self.input.shape().to_vec();
        let n_dims = shape.len();

        // Validate dimension
        if self.dim >= n_dims {
            return Err(BarracudaError::InvalidInput {
                message: format!(
                    "Dimension {} out of bounds for tensor with {} dimensions",
                    self.dim, n_dims
                ),
            });
        }

        let size: usize = shape.iter().product();
        let dim_size = shape[self.dim];

        // Compute outer_size (product of dims before dim) and inner_size (product after)
        let outer_size: usize = shape[..self.dim].iter().product();
        let inner_size: usize = shape[self.dim + 1..].iter().product();

        // Handle edge cases
        if outer_size == 0 {
            return self.execute_with_params(size, dim_size, 1, inner_size.max(1));
        }

        self.execute_with_params(size, dim_size, outer_size, inner_size.max(1))
    }

    fn execute_with_params(
        self,
        size: usize,
        dim_size: usize,
        outer_size: usize,
        inner_size: usize,
    ) -> Result<Tensor> {
        let device = self.input.device();

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

        // Create params buffer
        let params = CumprodF64Params {
            size: size as u32,
            dim_size: dim_size as u32,
            outer_size: outer_size as u32,
            inner_size: inner_size as u32,
        };

        let params_buffer = device.create_uniform_buffer("CumprodF64 Params", &params);

        // Compile shader
        let shader = device.compile_shader_f64(Self::shader(), Some("CumprodF64"));

        // Create bind group layout
        let bind_group_layout =
            device
                .device()
                .create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                    label: Some("CumprodF64 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,
                        },
                    ],
                });

        // Create bind group
        let bind_group = device
            .device()
            .create_bind_group(&wgpu::BindGroupDescriptor {
                label: Some("CumprodF64 Bind Group"),
                layout: &bind_group_layout,
                entries: &[
                    wgpu::BindGroupEntry {
                        binding: 0,
                        resource: self.input.buffer().as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 1,
                        resource: output_buffer.as_entire_binding(),
                    },
                    wgpu::BindGroupEntry {
                        binding: 2,
                        resource: params_buffer.as_entire_binding(),
                    },
                ],
            });

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

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

        // Dispatch
        let total_pairs = (outer_size * inner_size) as u32;
        let workgroups = total_pairs.div_ceil(WORKGROUP_SIZE_1D);

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

        {
            let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("CumprodF64 Pass"),
                timestamp_writes: None,
            });
            pass.set_pipeline(&pipeline);
            pass.set_bind_group(0, Some(&bind_group), &[]);
            pass.dispatch_workgroups(workgroups, 1, 1);
        }

        device.submit_commands(std::iter::once(encoder.finish()));

        // Create output tensor
        Ok(Tensor::from_buffer(
            output_buffer,
            self.input.shape().to_vec(),
            device.clone(),
        ))
    }

    /// Execute 1D cumprod directly on a slice (convenience method)
    /// # Errors
    /// Returns [`Err`] if buffer allocation fails, GPU dispatch fails, buffer readback fails,
    /// or the device is lost.
    pub async fn execute_1d(device: &Arc<WgpuDevice>, data: &[f64]) -> Result<Vec<f64>> {
        let n = data.len();
        if n == 0 {
            return Ok(Vec::new());
        }

        let tensor = Tensor::from_f64_data(data, vec![n], device.clone())?;

        let result = Self::new(tensor, 0).execute()?;

        result.to_f64_vec()
    }

    /// Execute 1D exclusive cumprod directly on a slice
    /// # Errors
    /// Returns [`Err`] if buffer allocation fails, GPU dispatch fails, buffer readback fails,
    /// or the device is lost.
    pub async fn execute_1d_exclusive(device: &Arc<WgpuDevice>, data: &[f64]) -> Result<Vec<f64>> {
        let n = data.len();
        if n == 0 {
            return Ok(Vec::new());
        }

        let tensor = Tensor::from_f64_data(data, vec![n], device.clone())?;

        let result = Self::exclusive(tensor, 0).execute()?;

        result.to_f64_vec()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::device::test_pool::get_test_device_if_f64_gpu_available;

    #[tokio::test]
    async fn test_cumprod_f64_1d() {
        let Some(device) = get_test_device_if_f64_gpu_available().await else {
            return;
        };

        let input = [1.0, 2.0, 3.0, 4.0, 5.0];
        let result = CumprodF64::execute_1d(&device, &input).await.unwrap();

        // Expected: [1, 2, 6, 24, 120]
        let expected = [1.0, 2.0, 6.0, 24.0, 120.0];
        for (i, (&got, &exp)) in result.iter().zip(expected.iter()).enumerate() {
            assert!(
                (got - exp).abs() < 1e-10,
                "Mismatch at {i}: got {got}, expected {exp}"
            );
        }
    }

    #[tokio::test]
    async fn test_cumprod_f64_exclusive() {
        let Some(device) = get_test_device_if_f64_gpu_available().await else {
            return;
        };

        let input = [1.0, 2.0, 3.0, 4.0, 5.0];
        let result = CumprodF64::execute_1d_exclusive(&device, &input)
            .await
            .unwrap();

        // Expected exclusive: [1, 1, 2, 6, 24]
        let expected = [1.0, 1.0, 2.0, 6.0, 24.0];
        for (i, (&got, &exp)) in result.iter().zip(expected.iter()).enumerate() {
            assert!(
                (got - exp).abs() < 1e-10,
                "Mismatch at {i}: got {got}, expected {exp}"
            );
        }
    }

    #[tokio::test]
    async fn test_cumprod_f64_with_identity() {
        let Some(device) = get_test_device_if_f64_gpu_available().await else {
            return;
        };

        // All ones should give all ones
        let input = [1.0, 1.0, 1.0, 1.0];
        let result = CumprodF64::execute_1d(&device, &input).await.unwrap();

        for (i, &val) in result.iter().enumerate() {
            assert!(
                (val - 1.0).abs() < 1e-10,
                "Mismatch at {i}: got {val}, expected 1.0"
            );
        }
    }

    #[tokio::test]
    async fn test_cumprod_f64_precision() {
        let Some(device) = get_test_device_if_f64_gpu_available().await else {
            return;
        };

        // Test with small increments to verify f64 precision
        let n = 100;
        let input: Vec<f64> = vec![1.001; n];
        let result = CumprodF64::execute_1d(&device, &input).await.unwrap();

        // Final should be 1.001^100
        let expected_final = 1.001_f64.powi(n as i32);
        let error = (result[n - 1] - expected_final).abs() / expected_final;
        assert!(
            error < 1e-10,
            "Precision error too large: {} (expected {}, got {})",
            error,
            expected_final,
            result[n - 1]
        );
    }
}