scirs2-core 0.4.3

Core utilities and common functionality for SciRS2 (scirs2-core)
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
//! Convolution kernels for GPU
//!
//! Implements various convolution operations for signal processing and neural networks.

use std::collections::HashMap;

use crate::gpu::kernels::{
    BaseKernel, DataType, GpuKernel, KernelMetadata, KernelParams, OperationType,
};
use crate::gpu::{GpuBackend, GpuError};

/// 1D convolution kernel
pub struct Conv1dKernel {
    base: BaseKernel,
}

impl Conv1dKernel {
    /// Create a new 1D convolution kernel
    pub fn new() -> Self {
        let metadata = KernelMetadata {
            workgroup_size: [256, 1, 1],
            local_memory_usage: 2048, // Kernel data cache
            supports_tensor_cores: false,
            operationtype: OperationType::ComputeIntensive,
            backend_metadata: HashMap::new(),
        };

        let (cuda_source, rocm_source, wgpu_source, metal_source, opencl_source) =
            Self::get_kernel_sources();

        Self {
            base: BaseKernel::new(
                "conv1d",
                &cuda_source,
                &rocm_source,
                &wgpu_source,
                &metal_source,
                &opencl_source,
                metadata,
            ),
        }
    }

    /// Get kernel sources for different backends
    fn get_kernel_sources() -> (String, String, String, String, String) {
        // CUDA kernel for 1D convolution
        let cuda_source = r#"
extern "C" __global__ void conv1d(
    const float* __restrict__ input,
    const float* __restrict__ kernel,
    float* __restrict__ output,
    int input_length,
    int kernel_length,
    int output_length,
    int stride,
    int padding
) {
    int out_idx = blockIdx.x * blockDim.x + threadIdx.x;
    
    if (out_idx >= output_length) {
        return;
    }
    
    float sum = 0.0f;
    
    for (int k = 0; k < kernel_length; k++) {
        int input_idx = out_idx * stride + k - padding;
        
        if (input_idx >= 0 && input_idx < input_length) {
            sum += input[input_idx] * kernel[k];
        }
    }
    
    output[out_idx] = sum;
}
"#
        .to_string();

        // WebGPU kernel for 1D convolution
        let wgpu_source = r#"
struct Uniforms {
    input_length: u32,
    kernel_length: u32,
    output_length: u32,
    stride: u32,
    padding: u32,
};

@group(0) @binding(0) var<uniform> uniforms: Uniforms;
@group(0) @binding(1) var<storage, read> input: array<f32>;
@group(0) @binding(2) var<storage, read> kernel_data: array<f32>;
@group(0) @binding(3) var<storage, write> output: array<f32>;

@compute @workgroup_size(256)
#[allow(dead_code)]
fn conv1d(@builtin(global_invocation_id) global_id: vec3<u32>) {
    let out_idx = global_id.x;
    
    if (out_idx >= uniforms.output_length) {
        return;
    }
    
    var sum = 0.0;
    
    for (var k = 0u; k < uniforms.kernel_length; k = k + 1u) {
        let input_idx = i32(out_idx * uniforms.stride + k) - i32(uniforms.padding);
        
        if (input_idx >= 0 && input_idx < i32(uniforms.input_length)) {
            sum += input[input_idx] * kernel_data[k];
        }
    }
    
    output[out_idx] = sum;
}
"#
        .to_string();

        // Metal kernel for 1D convolution
        let metal_source = r#"
#include <metal_stdlib>
using namespace metal;

kernel void conv1d(
    const device float* input [[buffer(0)]],
    const device float* kernel_data [[buffer(1)]],
    device float* output [[buffer(2)]],
    constant uint& input_length [[buffer(3)]],
    constant uint& kernel_length [[buffer(4)]],
    constant uint& output_length [[buffer(5)]],
    constant uint& stride [[buffer(6)]],
    constant uint& padding [[buffer(7)]],
    uint gid [[thread_position_in_grid]])
{
    if (gid >= output_length) {
        return;
    }
    
    float sum = 0.0f;
    
    for (uint k = 0; k < kernel_length; k++) {
        int input_idx = int(gid * stride + k) - int(padding);
        
        if (input_idx >= 0 && input_idx < int(input_length)) {
            sum += input[input_idx] * kernel_data[k];
        }
    }
    
    output[gid] = sum;
}
"#
        .to_string();

        // OpenCL kernel for 1D convolution
        let opencl_source = r#"
__kernel void conv1d(
    __global const float* input__global const float* kernel_data__global float* output,
    const int input_length,
    const int kernel_length,
    const int output_length,
    const int stride,
    const int padding)
{
    int out_idx = get_global_id(0);
    
    if (out_idx >= output_length) {
        return;
    }
    
    float sum = 0.0f;
    
    for (int k = 0; k < kernel_length; k++) {
        int input_idx = out_idx * stride + k - padding;
        
        if (input_idx >= 0 && input_idx < input_length) {
            sum += input[input_idx] * kernel_data[k];
        }
    }
    
    output[out_idx] = sum;
}
"#
        .to_string();

        // ROCm (HIP) kernel - similar to CUDA
        let rocm_source = cuda_source.clone();

        (
            cuda_source,
            rocm_source,
            wgpu_source,
            metal_source,
            opencl_source,
        )
    }
}

impl GpuKernel for Conv1dKernel {
    fn name(&self) -> &str {
        self.base.name()
    }

    fn source_for_backend(&self, backend: GpuBackend) -> Result<String, GpuError> {
        self.base.source_for_backend(backend)
    }

    fn metadata(&self) -> KernelMetadata {
        self.base.metadata()
    }

    fn can_specialize(&self, params: &KernelParams) -> bool {
        matches!(params.datatype, DataType::Float32 | DataType::Float64)
    }

    fn specialize(&self, params: &KernelParams) -> Result<Box<dyn GpuKernel>, GpuError> {
        if !self.can_specialize(params) {
            return Err(GpuError::SpecializationNotSupported);
        }

        Ok(Box::new(Self::new()))
    }
}

impl Default for Conv1dKernel {
    fn default() -> Self {
        Self::new()
    }
}

/// 2D convolution kernel for image processing and CNNs
pub struct Conv2dKernel {
    base: BaseKernel,
}

impl Conv2dKernel {
    /// Create a new 2D convolution kernel
    pub fn new() -> Self {
        let metadata = KernelMetadata {
            workgroup_size: [16, 16, 1],
            local_memory_usage: 4096,    // Kernel and input tile cache
            supports_tensor_cores: true, // 2D convolutions can use tensor cores
            operationtype: OperationType::ComputeIntensive,
            backend_metadata: HashMap::new(),
        };

        let (cuda_source, rocm_source, wgpu_source, metal_source, opencl_source) =
            Self::get_kernel_sources();

        Self {
            base: BaseKernel::new(
                "conv2d",
                &cuda_source,
                &rocm_source,
                &wgpu_source,
                &metal_source,
                &opencl_source,
                metadata,
            ),
        }
    }

    /// Get kernel sources for different backends
    fn get_kernel_sources() -> (String, String, String, String, String) {
        // CUDA kernel for 2D convolution
        let cuda_source = r#"
extern "C" __global__ void conv2d(
    const float* __restrict__ input,
    const float* __restrict__ kernel,
    float* __restrict__ output,
    int batch_size,
    int in_channels,
    int out_channels,
    int input_height,
    int input_width,
    int output_height,
    int output_width,
    int kernel_height,
    int kernel_width,
    int stride_y,
    int stride_x,
    int padding_y,
    int padding_x
) {
    int batch_idx = blockIdx.z;
    int out_channel = blockIdx.y;
    int out_y = blockIdx.x * blockDim.x + threadIdx.x;
    int out_x = threadIdx.y;

    if (batch_idx >= batch_size || out_channel >= out_channels || 
        out_y >= output_height || out_x >= output_width) {
        return;
    }

    float sum = 0.0f;

    // Input and output offsets
    int input_batch_offset = batch_idx * in_channels * input_height * input_width;
    int output_batch_offset = batch_idx * out_channels * output_height * output_width;
    int kernel_offset = out_channel * in_channels * kernel_height * kernel_width;

    // Convolution computation
    for (int in_ch = 0; in_ch < in_channels; in_ch++) {
        for (int ky = 0; ky < kernel_height; ky++) {
            for (int kx = 0; kx < kernel_width; kx++) {
                int input_y = out_y * stride_y + ky - padding_y;
                int input_x = out_x * stride_x + kx - padding_x;

                if (input_y >= 0 && input_y < input_height && 
                    input_x >= 0 && input_x < input_width) {
                    
                    int input_idx = input_batch_offset + 
                                   in_ch * input_height * input_width + 
                                   input_y * input_width + input_x;
                    
                    int kernel_idx = kernel_offset + 
                                    in_ch * kernel_height * kernel_width + 
                                    ky * kernel_width + kx;
                    
                    sum += input[input_idx] * kernel[kernel_idx];
                }
            }
        }
    }

    int output_idx = output_batch_offset + 
                     out_channel * output_height * output_width + 
                     out_y * output_width + out_x;
    output[output_idx] = sum;
}
"#
        .to_string();

        // WebGPU kernel for 2D convolution (simplified)
        let wgpu_source = r#"
struct Uniforms {
    batch_size: u32,
    in_channels: u32,
    out_channels: u32,
    input_height: u32,
    input_width: u32,
    output_height: u32,
    output_width: u32,
    kernel_height: u32,
    kernel_width: u32,
    stride_y: u32,
    stride_x: u32,
    padding_y: u32,
    padding_x: u32,
};

@group(0) @binding(0) var<uniform> uniforms: Uniforms;
@group(0) @binding(1) var<storage, read> input: array<f32>;
@group(0) @binding(2) var<storage, read> kernel_data: array<f32>;
@group(0) @binding(3) var<storage, write> output: array<f32>;

@compute @workgroup_size(16, 16)
#[allow(dead_code)]
fn conv2d(@builtin(global_invocation_id) global_id: vec3<u32>) {
    let batch_idx = global_id.z;
    let out_channel = global_id.y % uniforms.out_channels;
    let out_y = global_id.x;
    let out_x = global_id.y / uniforms.out_channels;

    if (batch_idx >= uniforms.batch_size || out_channel >= uniforms.out_channels || 
        out_y >= uniforms.output_height || out_x >= uniforms.output_width) {
        return;
    }

    var sum = 0.0;

    // Simplified convolution - would need optimization
    for (var in_ch = 0u; in_ch < uniforms.in_channels; in_ch = in_ch + 1u) {
        for (var ky = 0u; ky < uniforms.kernel_height; ky = ky + 1u) {
            for (var kx = 0u; kx < uniforms.kernel_width; kx = kx + 1u) {
                let input_y = i32(out_y * uniforms.stride_y + ky) - i32(uniforms.padding_y);
                let input_x = i32(out_x * uniforms.stride_x + kx) - i32(uniforms.padding_x);

                if (input_y >= 0 && input_y < i32(uniforms.input_height) && 
                    input_x >= 0 && input_x < i32(uniforms.input_width)) {
                    
                    let input_idx = batch_idx * uniforms.in_channels * uniforms.input_height * uniforms.input_width + 
                                   in_ch * uniforms.input_height * uniforms.input_width + 
                                   u32(input_y) * uniforms.input_width + u32(input_x);
                    
                    let kernel_idx = out_channel * uniforms.in_channels * uniforms.kernel_height * uniforms.kernel_width + 
                                    in_ch * uniforms.kernel_height * uniforms.kernel_width + 
                                    ky * uniforms.kernel_width + kx;
                    
                    sum += input[input_idx] * kernel_data[kernel_idx];
                }
            }
        }
    }

    let output_idx = batch_idx * uniforms.out_channels * uniforms.output_height * uniforms.output_width + 
                     out_channel * uniforms.output_height * uniforms.output_width + 
                     out_y * uniforms.output_width + out_x;
    output[output_idx] = sum;
}
"#
        .to_string();

        // Metal and OpenCL implementations would be similar but adapted for their respective syntaxes
        let metal_source = r#"
// Metal 2D convolution implementation (simplified)
#include <metal_stdlib>
using namespace metal;

kernel void conv2d(
    const device float* input [[buffer(0)]],
    const device float* kernel_data [[buffer(1)]],
    device float* output [[buffer(2)]],
    constant uint& batch_size [[buffer(3)]],
    constant uint& in_channels [[buffer(4)]],
    constant uint& out_channels [[buffer(5)]],
    constant uint& input_height [[buffer(6)]],
    constant uint& input_width [[buffer(7)]],
    constant uint& output_height [[buffer(8)]],
    constant uint& output_width [[buffer(9)]],
    constant uint& kernel_height [[buffer(10)]],
    constant uint& kernel_width [[buffer(11)]],
    constant uint& stride_y [[buffer(12)]],
    constant uint& stride_x [[buffer(13)]],
    constant uint& padding_y [[buffer(14)]],
    constant uint& padding_x [[buffer(15)]],
    uint3 global_id [[thread_position_in_grid]])
{
    // Similar implementation to CUDA kernel
}
"#
        .to_string();

        let opencl_source = r#"
// OpenCL 2D convolution implementation (simplified)
__kernel void conv2d(
    __global const float* input__global const float* kernel_data__global float* output,
    const int batch_size,
    const int in_channels,
    const int out_channels,
    const int input_height,
    const int input_width,
    const int output_height,
    const int output_width,
    const int kernel_height,
    const int kernel_width,
    const int stride_y,
    const int stride_x,
    const int padding_y,
    const int padding_x)
{
    // Similar implementation to CUDA kernel
}
"#
        .to_string();

        // ROCm (HIP) kernel - similar to CUDA
        let rocm_source = cuda_source.clone();

        (
            cuda_source,
            rocm_source,
            wgpu_source,
            metal_source,
            opencl_source,
        )
    }
}

impl GpuKernel for Conv2dKernel {
    fn name(&self) -> &str {
        self.base.name()
    }

    fn source_for_backend(&self, backend: GpuBackend) -> Result<String, GpuError> {
        self.base.source_for_backend(backend)
    }

    fn metadata(&self) -> KernelMetadata {
        self.base.metadata()
    }

    fn can_specialize(&self, params: &KernelParams) -> bool {
        matches!(
            params.datatype,
            DataType::Float32 | DataType::Float64 | DataType::Float16 | DataType::BFloat16
        )
    }

    fn specialize(&self, params: &KernelParams) -> Result<Box<dyn GpuKernel>, GpuError> {
        if !self.can_specialize(params) {
            return Err(GpuError::SpecializationNotSupported);
        }

        // In a real implementation, we would generate optimized kernels based on:
        // - Kernel size (3x3, 5x5, etc.)
        // - Number of channels
        // - Tensor core usage for appropriate data types
        // - Memory layout optimizations

        Ok(Box::new(Self::new()))
    }
}

impl Default for Conv2dKernel {
    fn default() -> Self {
        Self::new()
    }
}