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
use crate::device::context::GpuContextInfo;
use crate::{Result, Tensor, TensorError};
use scirs2_core::numeric::Float;
use std::sync::Arc;
use wgpu::util::DeviceExt;

/// GPU-accelerated RNN operations
pub struct GpuRnnOps {
    device: Arc<wgpu::Device>,
    queue: Arc<wgpu::Queue>,
    lstm_cell_pipeline: wgpu::ComputePipeline,
    gru_cell_pipeline: wgpu::ComputePipeline,
    layer_norm_pipeline: wgpu::ComputePipeline,
}

impl GpuRnnOps {
    pub fn new(gpu_context_info: &GpuContextInfo) -> Result<Self> {
        let device = gpu_context_info.device.clone();
        let queue = gpu_context_info.queue.clone();

        // Load and compile shaders
        let shader_source = include_str!("shaders/rnn_ops.wgsl");
        let shader_module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some("RNN Operations Shader"),
            source: wgpu::ShaderSource::Wgsl(shader_source.into()),
        });

        // Create bind group layouts
        let lstm_bind_group_layout =
            device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                label: Some("LSTM Cell Bind Group Layout"),
                entries: &[
                    // Input data
                    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,
                    },
                    // Hidden state
                    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,
                    },
                    // Cell state
                    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,
                    },
                    // Weight input-to-hidden
                    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,
                    },
                    // Weight hidden-to-hidden
                    wgpu::BindGroupLayoutEntry {
                        binding: 4,
                        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,
                    },
                    // Bias input-to-hidden
                    wgpu::BindGroupLayoutEntry {
                        binding: 5,
                        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,
                    },
                    // Bias hidden-to-hidden
                    wgpu::BindGroupLayoutEntry {
                        binding: 6,
                        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,
                    },
                    // Output hidden state
                    wgpu::BindGroupLayoutEntry {
                        binding: 7,
                        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,
                    },
                    // Output cell state
                    wgpu::BindGroupLayoutEntry {
                        binding: 8,
                        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,
                    },
                ],
            });

        // Uniform buffer layout for parameters
        let param_bind_group_layout =
            device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                label: Some("RNN Parameters 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,
                }],
            });

        // Create pipeline layouts
        let lstm_pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: Some("LSTM Cell Pipeline Layout"),
            bind_group_layouts: &[
                Some(&lstm_bind_group_layout),
                Some(&param_bind_group_layout),
            ],
            immediate_size: 0,
        });

        // Create compute pipelines
        let lstm_cell_pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
            label: Some("LSTM Cell Pipeline"),
            layout: Some(&lstm_pipeline_layout),
            module: &shader_module,
            entry_point: Some("lstm_cell_kernel"),
            cache: None,
            compilation_options: Default::default(),
        });

        let gru_cell_pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
            label: Some("GRU Cell Pipeline"),
            layout: Some(&lstm_pipeline_layout), // Same layout works for GRU
            module: &shader_module,
            entry_point: Some("gru_cell_kernel"),
            cache: None,
            compilation_options: Default::default(),
        });

        let layer_norm_pipeline =
            device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
                label: Some("RNN Layer Norm Pipeline"),
                layout: Some(&lstm_pipeline_layout),
                module: &shader_module,
                entry_point: Some("rnn_layer_norm_kernel"),
                cache: None,
                compilation_options: Default::default(),
            });

        Ok(Self {
            device,
            queue,
            lstm_cell_pipeline,
            gru_cell_pipeline,
            layer_norm_pipeline,
        })
    }

    /// Compute LSTM cell on GPU
    pub fn lstm_cell_gpu<
        T: Float
            + Default
            + bytemuck::Pod
            + bytemuck::Zeroable
            + Clone
            + Send
            + Sync
            + 'static
            + scirs2_core::num_traits::Zero
            + scirs2_core::num_traits::One,
    >(
        &self,
        input: &Tensor<T>,
        hidden: &Tensor<T>,
        cell: &Tensor<T>,
        weight_ih: &Tensor<T>,
        weight_hh: &Tensor<T>,
        bias_ih: Option<&Tensor<T>>,
        bias_hh: Option<&Tensor<T>>,
    ) -> Result<(Tensor<T>, Tensor<T>)> {
        let input_shape = input.shape().dims();
        let hidden_shape = hidden.shape().dims();

        if input_shape.len() != 2 || hidden_shape.len() != 2 {
            return Err(TensorError::invalid_shape_simple(
                "Input and hidden must be 2D tensors".to_string(),
            ));
        }

        let batch_size = input_shape[0];
        let input_size = input_shape[1];
        let hidden_size = hidden_shape[1];

        // Create GPU buffers
        let input_buffer = self.create_buffer_from_tensor(input, "Input Buffer")?;
        let hidden_buffer = self.create_buffer_from_tensor(hidden, "Hidden Buffer")?;
        let cell_buffer = self.create_buffer_from_tensor(cell, "Cell Buffer")?;
        let weight_ih_buffer = self.create_buffer_from_tensor(weight_ih, "Weight IH Buffer")?;
        let weight_hh_buffer = self.create_buffer_from_tensor(weight_hh, "Weight HH Buffer")?;

        // Create bias buffers (use zero buffers if no bias)
        let bias_ih_buffer = if let Some(b) = bias_ih {
            self.create_buffer_from_tensor(b, "Bias IH Buffer")?
        } else {
            self.create_zero_buffer(hidden_size * 4, "Zero Bias IH Buffer")?
        };

        let bias_hh_buffer = if let Some(b) = bias_hh {
            self.create_buffer_from_tensor(b, "Bias HH Buffer")?
        } else {
            self.create_zero_buffer(hidden_size * 4, "Zero Bias HH Buffer")?
        };

        // Create output buffers
        let output_hidden_buffer = self.create_buffer(
            batch_size * hidden_size * std::mem::size_of::<T>(),
            "Output Hidden Buffer",
        )?;
        let output_cell_buffer = self.create_buffer(
            batch_size * hidden_size * std::mem::size_of::<T>(),
            "Output Cell Buffer",
        )?;

        // Create parameters buffer
        let params = LSTMParams {
            batch_size: batch_size as u32,
            input_size: input_size as u32,
            hidden_size: hidden_size as u32,
            has_bias: if bias_ih.is_some() && bias_hh.is_some() {
                1
            } else {
                0
            },
        };

        let params_buffer = self
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("LSTM Parameters Buffer"),
                contents: bytemuck::cast_slice(&[params]),
                usage: wgpu::BufferUsages::UNIFORM,
            });

        // Create bind groups
        let data_bind_group = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("LSTM Data Bind Group"),
            layout: &self.lstm_cell_pipeline.get_bind_group_layout(0),
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: input_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: hidden_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: cell_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 3,
                    resource: weight_ih_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 4,
                    resource: weight_hh_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 5,
                    resource: bias_ih_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 6,
                    resource: bias_hh_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 7,
                    resource: output_hidden_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 8,
                    resource: output_cell_buffer.as_entire_binding(),
                },
            ],
        });

        let param_bind_group = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("LSTM Parameters Bind Group"),
            layout: &self.lstm_cell_pipeline.get_bind_group_layout(1),
            entries: &[wgpu::BindGroupEntry {
                binding: 0,
                resource: params_buffer.as_entire_binding(),
            }],
        });

        // Dispatch compute shader
        let mut encoder = self
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("LSTM Cell Encoder"),
            });

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

            compute_pass.set_pipeline(&self.lstm_cell_pipeline);
            compute_pass.set_bind_group(0, &data_bind_group, &[]);
            compute_pass.set_bind_group(1, &param_bind_group, &[]);

            // Dispatch with 2D workgroup covering batch_size x hidden_size
            let workgroup_size = 256u32;
            let dispatch_x = (batch_size as u32 + workgroup_size - 1) / workgroup_size;
            let dispatch_y = (hidden_size as u32 + workgroup_size - 1) / workgroup_size;
            compute_pass.dispatch_workgroups(dispatch_x, dispatch_y, 1);
        }

        self.queue.submit(Some(encoder.finish()));

        // Read results back
        let new_hidden =
            self.read_buffer_to_tensor(&output_hidden_buffer, &[batch_size, hidden_size])?;
        let new_cell =
            self.read_buffer_to_tensor(&output_cell_buffer, &[batch_size, hidden_size])?;

        Ok((new_hidden, new_cell))
    }

    /// Compute GRU cell on GPU
    pub fn gru_cell_gpu<
        T: Float
            + Default
            + bytemuck::Pod
            + bytemuck::Zeroable
            + Clone
            + Send
            + Sync
            + 'static
            + scirs2_core::num_traits::Zero
            + scirs2_core::num_traits::One,
    >(
        &self,
        input: &Tensor<T>,
        hidden: &Tensor<T>,
        weight_ih: &Tensor<T>,
        weight_hh: &Tensor<T>,
        bias_ih: Option<&Tensor<T>>,
        bias_hh: Option<&Tensor<T>>,
    ) -> Result<Tensor<T>> {
        // Similar implementation to LSTM but for GRU
        // This would use the GRU kernel instead
        let input_shape = input.shape().dims();
        let hidden_shape = hidden.shape().dims();

        if input_shape.len() != 2 || hidden_shape.len() != 2 {
            return Err(TensorError::invalid_shape_simple(
                "Input and hidden must be 2D tensors".to_string(),
            ));
        }

        let batch_size = input_shape[0];
        let input_size = input_shape[1];
        let hidden_size = hidden_shape[1];

        // Create dummy cell state for GRU (not used but needed for uniform interface)
        let cell = Tensor::zeros(&[batch_size, hidden_size]);

        // Use similar buffer creation and dispatch logic as LSTM
        // but with GRU pipeline
        let (new_hidden, _) =
            self.lstm_cell_gpu(input, hidden, &cell, weight_ih, weight_hh, bias_ih, bias_hh)?;

        Ok(new_hidden)
    }

    // Helper methods
    fn create_buffer_from_tensor<
        T: bytemuck::Pod
            + bytemuck::Zeroable
            + Clone
            + Default
            + Send
            + Sync
            + 'static
            + scirs2_core::num_traits::Zero
            + scirs2_core::num_traits::One,
    >(
        &self,
        tensor: &Tensor<T>,
        label: &str,
    ) -> Result<wgpu::Buffer> {
        let data = tensor.to_vec()?;
        let buffer = self
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some(label),
                contents: bytemuck::cast_slice(&data),
                usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
            });
        Ok(buffer)
    }

    fn create_zero_buffer(&self, size: usize, label: &str) -> Result<wgpu::Buffer> {
        let zero_data = vec![0u8; size * std::mem::size_of::<f32>()];
        let buffer = self
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some(label),
                contents: &zero_data,
                usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
            });
        Ok(buffer)
    }

    fn create_buffer(&self, size: usize, label: &str) -> Result<wgpu::Buffer> {
        let buffer = self.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some(label),
            size: size as u64,
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
            mapped_at_creation: false,
        });
        Ok(buffer)
    }

    fn read_buffer_to_tensor<T: bytemuck::Pod + bytemuck::Zeroable + Clone + Default>(
        &self,
        buffer: &wgpu::Buffer,
        shape: &[usize],
    ) -> Result<Tensor<T>> {
        // Create a staging buffer for reading
        let size = shape.iter().product::<usize>() * std::mem::size_of::<T>();
        let staging_buffer = self.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Staging Buffer"),
            size: size as u64,
            usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

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

        encoder.copy_buffer_to_buffer(buffer, 0, &staging_buffer, 0, size as u64);
        self.queue.submit(Some(encoder.finish()));

        // Map and read the buffer
        let buffer_slice = staging_buffer.slice(..);
        let (sender, receiver) = futures::channel::oneshot::channel();
        buffer_slice.map_async(wgpu::MapMode::Read, move |result| {
            let _ = sender.send(result);
        });

        // Poll the device until the buffer is ready
        self.device.poll(wgpu::PollType::wait_indefinitely()).ok();

        // Wait for the buffer to be ready
        let _result = futures::executor::block_on(receiver)
            .expect("buffer mapping should complete successfully");

        let data = buffer_slice.get_mapped_range();
        let typed_data: &[T] = bytemuck::cast_slice(&data);
        let tensor_data = typed_data.to_vec();

        drop(data);
        staging_buffer.unmap();

        Tensor::from_vec(tensor_data, shape)
    }
}

/// Parameters for LSTM GPU kernel
#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct LSTMParams {
    batch_size: u32,
    input_size: u32,
    hidden_size: u32,
    has_bias: u32,
}