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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
//! GPU Operations Module
//!
//! This module provides GPU acceleration for tensor operations using WGPU.
//! It includes optimized kernels for various operations and automatic device management.

use crate::gpu_profiler::global_profiler;
#[cfg(feature = "gpu")]
use crate::{buffer::TensorBuffer, Device, Result, TensorError};
use scirs2_core::ndarray::ArrayD;
use std::sync::Arc;
use std::time::Instant;

// Macro to safely include WGSL shader files, working around Rust 2021 edition prefix parsing
// This version is for use within this gpu.rs module only
macro_rules! include_shader {
    ("activation_ops") => {
        include_str!("../shaders/activation_ops.wgsl")
    };
    ("manipulation_ops") => {
        include_str!("../shaders/manipulation_ops.wgsl")
    };
    ("comparison_ops") => {
        include_str!("../shaders/comparison_ops.wgsl")
    };
    ("logical_ops") => {
        include_str!("../shaders/logical_ops.wgsl")
    };
    ("random_ops") => {
        include_str!("../shaders/random_ops.wgsl")
    };
    ("reduction_ops") => {
        include_str!("../shaders/reduction_ops.wgsl")
    };
    ("einsum_ops") => {
        include_str!("../shaders/einsum_ops.wgsl")
    };
    ("binary_ops") => {
        include_str!("../shaders/binary_ops.wgsl")
    };
    ("conv_ops") => {
        include_str!("../shaders/conv_ops.wgsl")
    };
    ("matmul_ops") => {
        include_str!("../shaders/matmul_ops.wgsl")
    };
    ("attention_ops") => {
        include_str!("../shaders/attention_ops.wgsl")
    };
    ("embedding_ops") => {
        include_str!("../shaders/embedding_ops.wgsl")
    };
    ("normalization_ops") => {
        include_str!("../shaders/normalization_ops.wgsl")
    };
    ("pooling_ops") => {
        include_str!("../shaders/pooling_ops.wgsl")
    };
    ("scan_ops") => {
        include_str!("../shaders/scan_ops.wgsl")
    };
    ("segmented_ops") => {
        include_str!("../shaders/segmented_ops.wgsl")
    };
    ("strided_ops") => {
        include_str!("../shaders/strided_ops.wgsl")
    };
    ("unary_ops") => {
        include_str!("../shaders/unary_ops.wgsl")
    };
    ("unary_ops_f64") => {
        include_str!("../shaders/unary_ops_f64.wgsl")
    };
    ("unary_ops_i32") => {
        include_str!("../shaders/unary_ops_i32.wgsl")
    };
    ("unary_ops_i64") => {
        include_str!("../shaders/unary_ops_i64.wgsl")
    };
    ("unary_ops_u32") => {
        include_str!("../shaders/unary_ops_u32.wgsl")
    };
    ("unary_ops_u64") => {
        include_str!("../shaders/unary_ops_u64.wgsl")
    };
    ("binary_ops_f64") => {
        include_str!("../shaders/binary_ops_f64.wgsl")
    };
    ("binary_ops_i32") => {
        include_str!("../shaders/binary_ops_i32.wgsl")
    };
    ("binary_ops_i64") => {
        include_str!("../shaders/binary_ops_i64.wgsl")
    };
    ("topk_ops") => {
        include_str!("../shaders/topk_ops.wgsl")
    };
    ("manipulation_ops2") => {
        include_str!("../shaders/manipulation_ops2.wgsl")
    };
    ("fused_ops") => {
        include_str!("../shaders/fused_ops.wgsl")
    };
    ("fft_ops") => {
        include_str!("../shaders/fft_ops.wgsl")
    };
}

/// GPU compute context for managing GPU resources
pub struct GpuContext {
    pub device: Arc<wgpu::Device>,
    pub queue: Arc<wgpu::Queue>,
}

/// Binary scalar operation types for GPU kernels
pub enum BinaryScalarOp {
    Add,
    Sub,
    Mul,
    Div,
    Pow,
}

impl GpuContext {
    /// Create a new GPU context
    pub fn new() -> Result<Self> {
        pollster::block_on(async {
            let instance =
                wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle());

            let adapter = instance
                .request_adapter(&wgpu::RequestAdapterOptions {
                    power_preference: wgpu::PowerPreference::HighPerformance,
                    compatible_surface: None,
                    force_fallback_adapter: false,
                })
                .await
                .map_err(|_e| {
                    TensorError::gpu_error(
                        "GpuContext::new",
                        "Failed to find suitable GPU adapter",
                        None,
                        false,
                    )
                })?;

            let (device, queue) = adapter
                .request_device(&wgpu::DeviceDescriptor {
                    required_features: wgpu::Features::empty(),
                    required_limits: if cfg!(target_arch = "wasm32") {
                        wgpu::Limits::downlevel_webgl2_defaults()
                    } else {
                        wgpu::Limits::default()
                    },
                    label: Some("TenfloweRS GPU Device"),
                    memory_hints: Default::default(),
                    experimental_features: wgpu::ExperimentalFeatures::default(),
                    trace: wgpu::Trace::default(),
                })
                .await
                .map_err(|e| {
                    TensorError::gpu_error(
                        "GpuContext::new",
                        &format!("Failed to create GPU device: {}", e),
                        None,
                        false,
                    )
                })?;

            Ok(Self {
                device: Arc::new(device),
                queue: Arc::new(queue),
            })
        })
    }

    /// Get or create the global GPU context
    pub fn global() -> Result<&'static Self> {
        use std::sync::OnceLock;
        static GLOBAL_CONTEXT: OnceLock<Result<GpuContext>> = OnceLock::new();

        GLOBAL_CONTEXT
            .get_or_init(|| GpuContext::new())
            .as_ref()
            .map_err(|e| e.clone())
    }
}

/// Helper macro for including shaders - directly include to avoid scoping issues
/// Uses absolute paths from crate root to work from any calling context
#[macro_export]
macro_rules! gpu_include_shader {
    ("binary_ops") => {
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/gpu/shaders/binary_ops.wgsl"
        ))
    };
    ("binary_ops_f64") => {
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/gpu/shaders/binary_ops_f64.wgsl"
        ))
    };
    ("binary_ops_i32") => {
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/gpu/shaders/binary_ops_i32.wgsl"
        ))
    };
    ("binary_ops_i64") => {
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/gpu/shaders/binary_ops_i64.wgsl"
        ))
    };
    ("binary_ops_u32") => {
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/gpu/shaders/binary_ops_u32.wgsl"
        ))
    };
    ("binary_ops_u64") => {
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/gpu/shaders/binary_ops_u64.wgsl"
        ))
    };
    ("unary_ops") => {
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/gpu/shaders/unary_ops.wgsl"
        ))
    };
    ("unary_ops_f64") => {
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/gpu/shaders/unary_ops_f64.wgsl"
        ))
    };
    ("unary_ops_i32") => {
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/gpu/shaders/unary_ops_i32.wgsl"
        ))
    };
    ("unary_ops_i64") => {
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/gpu/shaders/unary_ops_i64.wgsl"
        ))
    };
    ("unary_ops_u32") => {
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/gpu/shaders/unary_ops_u32.wgsl"
        ))
    };
    ("unary_ops_u64") => {
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/gpu/shaders/unary_ops_u64.wgsl"
        ))
    };
    ("fft_ops") => {
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/gpu/shaders/fft_ops.wgsl"
        ))
    };
    ("einsum_ops") => {
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/gpu/shaders/einsum_ops.wgsl"
        ))
    };
    ("reduction_ops") => {
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/gpu/shaders/reduction_ops.wgsl"
        ))
    };
    ("matmul_ops") => {
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/gpu/shaders/matmul_ops.wgsl"
        ))
    };
    ("conv_ops") => {
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/gpu/shaders/conv_ops.wgsl"
        ))
    };
    ("attention_ops") => {
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/gpu/shaders/attention_ops.wgsl"
        ))
    };
    ("pooling_ops") => {
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/gpu/shaders/pooling_ops.wgsl"
        ))
    };
    ("activation_ops") => {
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/gpu/shaders/activation_ops.wgsl"
        ))
    };
    ("comparison_ops") => {
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/gpu/shaders/comparison_ops.wgsl"
        ))
    };
    ("logical_ops") => {
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/gpu/shaders/logical_ops.wgsl"
        ))
    };
    ("manipulation_ops") => {
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/gpu/shaders/manipulation_ops.wgsl"
        ))
    };
    ("manipulation_ops2") => {
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/gpu/shaders/manipulation_ops2.wgsl"
        ))
    };
    ("normalization_ops") => {
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/gpu/shaders/normalization_ops.wgsl"
        ))
    };
    ("embedding_ops") => {
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/gpu/shaders/embedding_ops.wgsl"
        ))
    };
    ("random_ops") => {
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/gpu/shaders/random_ops.wgsl"
        ))
    };
    ("scan_ops") => {
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/gpu/shaders/scan_ops.wgsl"
        ))
    };
    ("segmented_ops") => {
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/gpu/shaders/segmented_ops.wgsl"
        ))
    };
    ("strided_ops") => {
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/gpu/shaders/strided_ops.wgsl"
        ))
    };
    ("topk_ops") => {
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/gpu/shaders/topk_ops.wgsl"
        ))
    };
    ("fused_ops") => {
        include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/src/gpu/shaders/fused_ops.wgsl"
        ))
    };
}

pub use gpu_include_shader;

// Module declarations - organize functionality into logical groups

// Core GPU buffer and operation modules
pub mod binary_ops;
pub mod buffer;
pub mod logical_ops;
pub mod random_ops;
pub mod unary_ops;

// Async kernel execution module
#[cfg(feature = "gpu")]
pub mod async_kernel;

// Linear algebra operations module
#[cfg(feature = "gpu")]
pub mod linalg;

// Memory coalescing optimization module
#[cfg(feature = "gpu")]
pub mod memory_coalescing;

// Multi-stream GPU executor for CPU-GPU overlap
#[cfg(feature = "gpu")]
pub mod multi_stream_executor;

// RNN GPU operations module
#[cfg(feature = "gpu")]
pub mod rnn_ops;

// Attention operations module for neural networks
#[cfg(feature = "gpu")]
pub mod attention_ops;

// Kernel fusion module for performance optimization
#[cfg(feature = "gpu")]
pub mod kernel_fusion;

// Ultra-sophisticated fusion integration for production excellence
#[cfg(feature = "gpu")]
pub mod ultra_fusion_integration;

// Advanced memory pool management
#[cfg(feature = "gpu")]
pub mod memory_pool;

// GPU memory allocation tracing and diagnostics
#[cfg(feature = "gpu")]
pub mod memory_tracing;

// GPU memory diagnostics and profiling
#[cfg(feature = "gpu")]
pub mod memory_diagnostics;

// GPU reduction kernel templates and execution
#[cfg(feature = "gpu")]
pub mod reduction_kernels;

// Performance optimizer and profiler
#[cfg(feature = "gpu")]
pub mod performance_optimizer;

// Advanced kernel manager for cutting-edge GPU optimizations
#[cfg(feature = "gpu")]
pub mod advanced_kernel_manager;

// Platform-specific GPU backend modules
#[cfg(feature = "cudnn")]
pub mod cudnn;

#[cfg(all(target_os = "macos", feature = "metal"))]
pub mod metal_kernels;

#[cfg(feature = "rocm")]
pub mod rocm_kernels;

#[cfg(feature = "cuda")]
pub mod cuda_kernels;

#[cfg(feature = "nccl")]
pub mod nccl_integration;

// Modular GPU operations - NEW REFACTORED STRUCTURE
pub mod ops;

// Re-export commonly used types and functions
pub use binary_ops::{gpu_binary_op, BinaryOpKernel};
pub use buffer::{BufferManager, GpuBuffer, GpuBufferOps};
pub use unary_ops::{gpu_unary_op, UnaryOpKernel};

#[cfg(feature = "gpu")]
pub use attention_ops::*;
#[cfg(feature = "gpu")]
pub use kernel_fusion::*;
#[cfg(feature = "gpu")]
pub use linalg::*;
#[cfg(feature = "gpu")]
pub use ultra_fusion_integration::*;

// Re-export common types from ops module
pub use ops::ReductionOp;

// Re-export memory tracking functions
#[cfg(feature = "gpu")]
pub use memory_tracing::{
    current_gpu_memory_usage, generate_gpu_memory_report, peak_gpu_memory_usage,
    print_gpu_memory_report, MemoryReport, MemoryStats,
};

/// Trait for GPU operations on tensors
pub trait GpuOps {
    fn gpu_add(&self, other: &Self) -> crate::Result<Self>
    where
        Self: Sized;
    fn gpu_mul(&self, other: &Self) -> crate::Result<Self>
    where
        Self: Sized;
    fn gpu_sub(&self, other: &Self) -> crate::Result<Self>
    where
        Self: Sized;
    fn gpu_div(&self, other: &Self) -> crate::Result<Self>
    where
        Self: Sized;
}

/// Helper function to cast values to f32 for GPU shaders
fn cast_to_f32<T>(value: T) -> f32
where
    T: bytemuck::Pod + bytemuck::Zeroable + Clone + Send + Sync + 'static,
{
    // Safe casting implementation
    42.0 // Placeholder - implement proper casting based on type
}

/// GPU comparison operation dispatch function
/// Returns a `GpuBuffer<u8>` where 0 represents false and 1 represents true
pub fn gpu_comparison_op_dispatch<T>(
    input_a: &GpuBuffer<T>,
    input_b: &GpuBuffer<T>,
    operation: self::ops::ComparisonOp,
) -> Result<GpuBuffer<u8>>
where
    T: bytemuck::Pod + bytemuck::Zeroable + Clone + Send + Sync + 'static,
{
    // Fallback implementation - delegate to comparison_ops module
    let device_id = match input_a.device_enum() {
        Device::Gpu(id) => id,
        _ => {
            return Err(TensorError::DeviceMismatch {
                operation: "comparison".to_string(),
                device1: format!("{:?}", input_a.device_enum()),
                device2: "GPU".to_string(),
                context: None,
            })
        }
    };

    // For now, return a simple u8 buffer (1 = true, 0 = false)
    let result_data = vec![1u8; input_a.len()];
    GpuBuffer::from_slice(&result_data, &Device::Gpu(device_id))
}

/// Execute embedding lookup operation on GPU
/// This is a stub implementation that will be properly implemented later
pub fn execute_embedding_lookup<T>(
    indices: &GpuBuffer<T>,
    weights: &GpuBuffer<T>,
    num_embeddings: usize,
    embedding_dim: usize,
    total_indices: usize,
) -> Result<GpuBuffer<T>>
where
    T: bytemuck::Pod + bytemuck::Zeroable + Clone + Send + Sync + 'static + Default,
{
    // For now, create a stub output buffer with the correct size
    // NOTE(v0.2): Implement proper GPU embedding lookup using WGSL shaders
    let output_size = total_indices * embedding_dim;

    // Get device from indices buffer
    let device_id = match indices.device_enum() {
        Device::Gpu(id) => id,
        _ => {
            return Err(TensorError::DeviceMismatch {
                operation: "embedding_lookup".to_string(),
                device1: format!("{:?}", indices.device_enum()),
                device2: "GPU".to_string(),
                context: None,
            })
        }
    };

    // Create output buffer with zeros for now (stub implementation)
    let result_data = vec![T::default(); output_size];
    GpuBuffer::from_slice(&result_data, &Device::Gpu(device_id))
}