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
//! GPU Matrix Determinant Computation
//!
//! This module provides GPU implementations for computing matrix determinants using
//! Gaussian elimination with partial pivoting, optimized for parallel execution.

use super::super::context::{GpuLinalgContext, LinalgMetadata};
use crate::gpu::buffer::GpuBuffer;
use crate::{Result, Shape, TensorError};
use bytemuck::{Pod, Zeroable};
use scirs2_core::numeric::{Float, One};
use wgpu::{BufferDescriptor, BufferUsages};

/// Matrix determinant computation using Gaussian elimination
pub fn determinant<T>(
    context: &mut GpuLinalgContext,
    input: &GpuBuffer<T>,
    shape: &Shape,
) -> Result<T>
where
    T: Float + Pod + Zeroable + Clone + Send + Sync + 'static,
{
    context.determinant(input, shape)
}

impl GpuLinalgContext {
    /// Matrix determinant computation
    ///
    /// Computes the determinant of a square matrix using Gaussian elimination
    /// with partial pivoting. This implementation uses multiple kernel dispatches
    /// to handle the sequential nature of the elimination process.
    pub fn determinant<T>(&mut self, input: &GpuBuffer<T>, shape: &Shape) -> Result<T>
    where
        T: Float + Pod + Zeroable + Clone + Send + Sync + 'static,
    {
        // Validate input
        if shape.len() != 2 || shape[0] != shape[1] {
            return Err(TensorError::invalid_shape_simple(
                "Determinant requires a square matrix".to_string(),
            ));
        }

        let n = shape[0];
        if n == 0 {
            return Ok(T::one());
        }

        let buffers = self.create_determinant_buffers::<T>(n)?;
        self.initialize_determinant_computation(input, &buffers, n)?;

        let pipelines = self.create_determinant_pipelines()?;
        let metadata = self.create_determinant_metadata(n);
        let metadata_buffer = self.create_metadata_buffer(&metadata)?;

        // Perform Gaussian elimination for each column
        self.perform_gaussian_elimination(
            &pipelines,
            &buffers,
            &metadata_buffer,
            n,
        )?;

        // Compute final determinant from diagonal elements
        self.compute_final_determinant(&pipelines.compute_det, &buffers, &metadata_buffer)?;

        // Read back the result
        self.read_determinant_result::<T>(&buffers.determinant_buffer)
    }

    /// Create working buffers for determinant computation
    fn create_determinant_buffers<T>(&self, n: usize) -> Result<DeterminantBuffers>
    where
        T: Float + Pod + Zeroable + Clone + Send + Sync + 'static,
    {
        let matrix_size = n * n;

        let working_matrix = self.device().create_buffer(&BufferDescriptor {
            label: Some("det_working_matrix"),
            size: (matrix_size * std::mem::size_of::<T>()) as u64,
            usage: BufferUsages::STORAGE | BufferUsages::COPY_SRC | BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        let determinant_buffer = self.device().create_buffer(&BufferDescriptor {
            label: Some("det_result"),
            size: std::mem::size_of::<T>() as u64,
            usage: BufferUsages::STORAGE | BufferUsages::COPY_SRC | BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        let pivot_info_buffer = self.device().create_buffer(&BufferDescriptor {
            label: Some("pivot_info"),
            size: (2 * std::mem::size_of::<u32>()) as u64,
            usage: BufferUsages::STORAGE | BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        Ok(DeterminantBuffers {
            working_matrix,
            determinant_buffer,
            pivot_info_buffer,
        })
    }

    /// Initialize determinant computation by copying input and setting initial determinant
    fn initialize_determinant_computation<T>(
        &mut self,
        input: &GpuBuffer<T>,
        buffers: &DeterminantBuffers,
        n: usize,
    ) -> Result<()>
    where
        T: Float + Pod + Zeroable + Clone + Send + Sync + 'static,
    {
        let matrix_size = n * n;

        // Copy input matrix to working matrix
        let mut encoder = self
            .device()
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("det_copy_encoder"),
            });

        encoder.copy_buffer_to_buffer(
            input.buffer(),
            0,
            &buffers.working_matrix,
            0,
            (matrix_size * std::mem::size_of::<T>()) as u64,
        );

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

        // Initialize determinant to 1.0
        let initial_det = T::one();
        let det_bytes = bytemuck::bytes_of(&initial_det);
        self.queue().write_buffer(&buffers.determinant_buffer, 0, det_bytes);

        Ok(())
    }

    /// Create compute pipelines for determinant computation
    fn create_determinant_pipelines(&self) -> Result<DeterminantPipelines> {
        let shader_source = include_str!("../../shaders/linalg_determinant.wgsl");
        let shader_module = self
            .device()
            .create_shader_module(wgpu::ShaderModuleDescriptor {
                label: Some("determinant_shader"),
                source: wgpu::ShaderSource::Wgsl(shader_source.into()),
            });

        let find_pivot = self
            .device()
            .create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
                label: Some("find_pivot_pipeline"),
                layout: None,
                module: &shader_module,
                entry_point: Some("find_pivot"),
                cache: None,
                compilation_options: Default::default(),
            });

        let swap_rows = self
            .device()
            .create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
                label: Some("swap_rows_pipeline"),
                layout: None,
                module: &shader_module,
                entry_point: Some("swap_rows"),
                cache: None,
                compilation_options: Default::default(),
            });

        let elimination = self
            .device()
            .create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
                label: Some("elimination_pipeline"),
                layout: None,
                module: &shader_module,
                entry_point: Some("elimination_step"),
                cache: None,
                compilation_options: Default::default(),
            });

        let compute_det = self
            .device()
            .create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
                label: Some("compute_det_pipeline"),
                layout: None,
                module: &shader_module,
                entry_point: Some("compute_determinant"),
                cache: None,
                compilation_options: Default::default(),
            });

        Ok(DeterminantPipelines {
            find_pivot,
            swap_rows,
            elimination,
            compute_det,
        })
    }

    /// Create metadata for determinant computation
    fn create_determinant_metadata(&self, n: usize) -> LinalgMetadata {
        LinalgMetadata::new(n, n).with_tolerance(1e-10)
    }

    /// Perform Gaussian elimination for each column
    fn perform_gaussian_elimination(
        &mut self,
        pipelines: &DeterminantPipelines,
        buffers: &DeterminantBuffers,
        metadata_buffer: &wgpu::Buffer,
        n: usize,
    ) -> Result<()> {
        for k in 0..n {
            // Set current column index
            let k_value = k as u32;
            let k_bytes = bytemuck::bytes_of(&k_value);
            self.queue().write_buffer(&buffers.pivot_info_buffer, 0, k_bytes);

            // Create bind group for this iteration
            let bind_group = self.create_elimination_bind_group(
                &pipelines.find_pivot,
                buffers,
                metadata_buffer,
            )?;

            // Execute elimination steps
            self.execute_elimination_iteration(pipelines, &bind_group, n)?;
        }

        Ok(())
    }

    /// Create bind group for elimination operations
    fn create_elimination_bind_group(
        &self,
        pipeline: &wgpu::ComputePipeline,
        buffers: &DeterminantBuffers,
        metadata_buffer: &wgpu::Buffer,
    ) -> Result<wgpu::BindGroup> {
        let bind_group = self.device().create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("det_bind_group"),
            layout: &pipeline.get_bind_group_layout(0),
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: buffers.working_matrix.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: buffers.determinant_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: buffers.pivot_info_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 3,
                    resource: metadata_buffer.as_entire_binding(),
                },
            ],
        });

        Ok(bind_group)
    }

    /// Execute one iteration of the elimination process
    fn execute_elimination_iteration(
        &mut self,
        pipelines: &DeterminantPipelines,
        bind_group: &wgpu::BindGroup,
        n: usize,
    ) -> Result<()> {
        let mut encoder = self
            .device()
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("det_elimination_encoder"),
            });

        {
            let mut compute_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("det_elimination_pass"),
                timestamp_writes: None,
            });

            // Find pivot
            compute_pass.set_pipeline(&pipelines.find_pivot);
            compute_pass.set_bind_group(0, bind_group, &[]);
            compute_pass.dispatch_workgroups(1, 1, 1);

            // Swap rows if needed
            compute_pass.set_pipeline(&pipelines.swap_rows);
            compute_pass.set_bind_group(0, bind_group, &[]);
            compute_pass.dispatch_workgroups((n as u32 + 255) / 256, 1, 1);

            // Perform elimination
            compute_pass.set_pipeline(&pipelines.elimination);
            compute_pass.set_bind_group(0, bind_group, &[]);
            let workgroups_x = (n as u32 + 15) / 16;
            let workgroups_y = (n as u32 + 15) / 16;
            compute_pass.dispatch_workgroups(workgroups_x, workgroups_y, 1);
        }

        self.queue().submit(std::iter::once(encoder.finish()));
        Ok(())
    }

    /// Compute final determinant from diagonal elements
    fn compute_final_determinant(
        &mut self,
        compute_det_pipeline: &wgpu::ComputePipeline,
        buffers: &DeterminantBuffers,
        metadata_buffer: &wgpu::Buffer,
    ) -> Result<()> {
        let bind_group = self.device().create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("det_final_bind_group"),
            layout: &compute_det_pipeline.get_bind_group_layout(0),
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: buffers.working_matrix.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: buffers.determinant_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: buffers.pivot_info_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 3,
                    resource: metadata_buffer.as_entire_binding(),
                },
            ],
        });

        let mut encoder = self
            .device()
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("det_final_encoder"),
            });

        {
            let mut compute_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("det_final_pass"),
                timestamp_writes: None,
            });

            compute_pass.set_pipeline(compute_det_pipeline);
            compute_pass.set_bind_group(0, &bind_group, &[]);
            compute_pass.dispatch_workgroups(1, 1, 1);
        }

        self.queue().submit(std::iter::once(encoder.finish()));
        Ok(())
    }

    /// Read back the determinant result from GPU
    fn read_determinant_result<T>(&mut self, determinant_buffer: &wgpu::Buffer) -> Result<T>
    where
        T: Float + Pod + Zeroable + Clone + Send + Sync + 'static,
    {
        // Create readback buffer
        let result_buffer = self.device().create_buffer(&BufferDescriptor {
            label: Some("det_result_readback"),
            size: std::mem::size_of::<T>() as u64,
            usage: BufferUsages::COPY_DST | BufferUsages::MAP_READ,
            mapped_at_creation: false,
        });

        // Copy determinant result to readback buffer
        let mut encoder = self
            .device()
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("det_readback_encoder"),
            });

        encoder.copy_buffer_to_buffer(
            determinant_buffer,
            0,
            &result_buffer,
            0,
            std::mem::size_of::<T>() as u64,
        );

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

        // Map and read the result
        let buffer_slice = result_buffer.slice(..);
        let (tx, rx) = std::sync::mpsc::channel();
        buffer_slice.map_async(wgpu::MapMode::Read, move |result| {
            tx.send(result).expect("channel send should succeed");
        });

        self.device().poll(wgpu::PollType::wait_indefinitely()).ok();
        rx.recv().expect("channel recv should succeed").map_err(|e| TensorError::ComputeError {
            operation: "gpu_read_determinant".to_string(),
            details: format!("Failed to read determinant result: {:?}", e),
            retry_possible: true,
            context: None,
        })?;

        let data = buffer_slice.get_mapped_range();
        let result = bytemuck::from_bytes::<T>(&data[..std::mem::size_of::<T>()]);
        let determinant_value = *result;

        drop(data);
        result_buffer.unmap();

        Ok(determinant_value)
    }
}

/// Working buffers for determinant computation
struct DeterminantBuffers {
    working_matrix: wgpu::Buffer,
    determinant_buffer: wgpu::Buffer,
    pivot_info_buffer: wgpu::Buffer,
}

/// Collection of compute pipelines for determinant computation
struct DeterminantPipelines {
    find_pivot: wgpu::ComputePipeline,
    swap_rows: wgpu::ComputePipeline,
    elimination: wgpu::ComputePipeline,
    compute_det: wgpu::ComputePipeline,
}