numr 0.15.0

High-performance numerical computing with multi-backend GPU acceleration (CPU/CUDA/WebGPU)
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
//! Element-wise WGSL kernel launchers
//!
//! Binary and broadcast-binary ops support F32, I32, U32.
//! Unary ops: most are F32 only; neg/abs/sign and the five rounding ops
//! (identity on an integer) support I32 and U32.
//! Scalar ops: F32, I32, U32.
//! Compare ops: F32, I32, U32.

use wgpu::{Buffer, Queue};

use super::linear_grid::dispatch_linear;
use super::pipeline::{LayoutKey, PipelineCache};
use crate::dtype::DType;
use crate::error::{Error, Result};

// ============================================================================
// Static Shader Sources
// ============================================================================

const BINARY_F32_SHADER: &str = include_str!("binary.wgsl");
// The integer shaders share one saturating-power helper and one guarded
// integer division, prepended to each module that calls them. WGSL has no
// include, so this is where the single copy in the repository reaches the three
// I32 and three U32 modules. Order matters: WGSL has no forward declarations,
// so a helper must be concatenated ahead of its callers.
const BINARY_I32_SHADER: &str = concat!(
    include_str!("int_saturate.wgsl"),
    include_str!("ipow_i32.wgsl"),
    include_str!("idiv_i32.wgsl"),
    include_str!("binary_i32.wgsl")
);
const BINARY_U32_SHADER: &str = concat!(
    include_str!("int_saturate.wgsl"),
    include_str!("ipow_u32.wgsl"),
    include_str!("idiv_u32.wgsl"),
    include_str!("binary_u32.wgsl")
);
const BINARY_BROADCAST_F32_SHADER: &str = include_str!("binary_broadcast.wgsl");
const BINARY_BROADCAST_I32_SHADER: &str = concat!(
    include_str!("int_saturate.wgsl"),
    include_str!("ipow_i32.wgsl"),
    include_str!("idiv_i32.wgsl"),
    include_str!("binary_broadcast_i32.wgsl")
);
const BINARY_BROADCAST_U32_SHADER: &str = concat!(
    include_str!("int_saturate.wgsl"),
    include_str!("ipow_u32.wgsl"),
    include_str!("idiv_u32.wgsl"),
    include_str!("binary_broadcast_u32.wgsl")
);
const UNARY_SHADER: &str = include_str!("unary.wgsl");
const UNARY_I32_SHADER: &str = include_str!("unary_i32.wgsl");
const UNARY_U32_SHADER: &str = include_str!("unary_u32.wgsl");
const SCALAR_SHADER: &str = include_str!("scalar.wgsl");
const SCALAR_I32_SHADER: &str = concat!(
    include_str!("int_saturate.wgsl"),
    include_str!("ipow_i32.wgsl"),
    include_str!("idiv_i32.wgsl"),
    include_str!("scalar_i32.wgsl")
);
const SCALAR_U32_SHADER: &str = concat!(
    include_str!("int_saturate.wgsl"),
    include_str!("ipow_u32.wgsl"),
    include_str!("idiv_u32.wgsl"),
    include_str!("scalar_u32.wgsl")
);
const COMPARE_SHADER: &str = include_str!("compare.wgsl");
const COMPARE_I32_SHADER: &str = include_str!("compare_i32.wgsl");
const COMPARE_U32_SHADER: &str = include_str!("compare_u32.wgsl");

const CAST_F32_TO_I32_SHADER: &str = include_str!("cast_f32_to_i32.wgsl");
const CAST_F32_TO_U32_SHADER: &str = include_str!("cast_f32_to_u32.wgsl");
const CAST_I32_TO_F32_SHADER: &str = include_str!("cast_i32_to_f32.wgsl");
const CAST_I32_TO_U32_SHADER: &str = include_str!("cast_i32_to_u32.wgsl");
const CAST_U32_TO_F32_SHADER: &str = include_str!("cast_u32_to_f32.wgsl");
const CAST_U32_TO_I32_SHADER: &str = include_str!("cast_u32_to_i32.wgsl");

// ============================================================================
// Binary Operations
// ============================================================================

/// Launch a binary element-wise operation: `out[i] = a[i] op b[i]`. F32, I32, U32.
pub fn launch_binary_op(
    cache: &PipelineCache,
    queue: &Queue,
    op: &'static str,
    a: &Buffer,
    b: &Buffer,
    out: &Buffer,
    params_buffer: &Buffer,
    numel: usize,
    dtype: DType,
) -> Result<()> {
    let op_name = match op {
        "maximum" => "max",
        "minimum" => "min",
        _ => op,
    };

    let (module_key, shader, suffix) = match dtype {
        DType::F32 => ("binary_f32", BINARY_F32_SHADER, "f32"),
        DType::I32 => ("binary_i32", BINARY_I32_SHADER, "i32"),
        DType::U32 => ("binary_u32", BINARY_U32_SHADER, "u32"),
        _ => return Err(Error::UnsupportedDType { dtype, op }),
    };

    // atan2 has no integer meaning, so it stays float-only. Integer pow is
    // exact by squaring and saturating, matching CPU.
    if op_name == "atan2" && dtype != DType::F32 {
        return Err(Error::UnsupportedDType { dtype, op });
    }

    let entry_point: String = format!("{}_{}", op_name, suffix);
    let module = cache.get_or_create_module(module_key, shader);
    let layout = cache.get_or_create_layout(LayoutKey {
        num_storage_buffers: 3,
        num_uniform_buffers: 1,
        num_readonly_storage: 0,
    });
    let pipeline = cache.get_or_create_dynamic_pipeline(module_key, &entry_point, &module, &layout);
    let bind_group = cache.create_bind_group(&layout, &[a, b, out, params_buffer]);

    let mut encoder = cache
        .device()
        .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some(op) });
    {
        let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
            label: Some(op),
            timestamp_writes: None,
        });
        pass.set_pipeline(&pipeline);
        pass.set_bind_group(0, Some(&bind_group), &[]);
        dispatch_linear(&mut pass, numel);
    }
    queue.submit(std::iter::once(encoder.finish()));
    Ok(())
}

/// Launch a broadcast binary operation. F32, I32, U32.
/// Also serves broadcast compare: its mask dtype equals the input dtype, so the
/// buffer shape and bind-group layout are identical.
#[allow(clippy::too_many_arguments)]
pub fn launch_broadcast_binary_op(
    cache: &PipelineCache,
    queue: &Queue,
    op: &'static str,
    a: &Buffer,
    b: &Buffer,
    out: &Buffer,
    a_strides: &Buffer,
    b_strides: &Buffer,
    out_strides: &Buffer,
    params_buffer: &Buffer,
    numel: usize,
    dtype: DType,
) -> Result<()> {
    let op_name = match op {
        "maximum" => "max",
        "minimum" => "min",
        _ => op,
    };

    let (module_key, shader, suffix) = match dtype {
        DType::F32 => ("binary_broadcast_f32", BINARY_BROADCAST_F32_SHADER, "f32"),
        DType::I32 => ("binary_broadcast_i32", BINARY_BROADCAST_I32_SHADER, "i32"),
        DType::U32 => ("binary_broadcast_u32", BINARY_BROADCAST_U32_SHADER, "u32"),
        _ => return Err(Error::UnsupportedDType { dtype, op }),
    };

    let entry_point: String = format!("broadcast_{}_{}", op_name, suffix);
    let module = cache.get_or_create_module(module_key, shader);
    let layout = cache.get_or_create_layout(LayoutKey {
        num_storage_buffers: 6,
        num_uniform_buffers: 1,
        num_readonly_storage: 0,
    });
    let pipeline = cache.get_or_create_dynamic_pipeline(module_key, &entry_point, &module, &layout);
    let bind_group = cache.create_bind_group(
        &layout,
        &[a, b, out, a_strides, b_strides, out_strides, params_buffer],
    );

    let mut encoder = cache
        .device()
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("broadcast_binary"),
        });
    {
        let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
            label: Some("broadcast_binary"),
            timestamp_writes: None,
        });
        pass.set_pipeline(&pipeline);
        pass.set_bind_group(0, Some(&bind_group), &[]);
        dispatch_linear(&mut pass, numel);
    }
    queue.submit(std::iter::once(encoder.finish()));
    Ok(())
}

// ============================================================================
// Unary Operations
// ============================================================================

/// Launch a unary operation: `out[i] = op(a[i])`.
/// Most ops are F32 only. neg/abs/sign and the five rounding ops (identity on
/// an integer) support I32 and U32.
pub fn launch_unary_op(
    cache: &PipelineCache,
    queue: &Queue,
    op: &'static str,
    a: &Buffer,
    out: &Buffer,
    params_buffer: &Buffer,
    numel: usize,
    dtype: DType,
) -> Result<()> {
    // For I32 and U32: neg, abs, sign, and the five rounding ops (identity on
    // an integer) are supported.
    match dtype {
        DType::F32 => {}
        DType::I32 | DType::U32 => {
            if !matches!(
                op,
                "neg" | "abs" | "sign" | "floor" | "ceil" | "round" | "round_ties_even" | "trunc"
            ) {
                return Err(Error::UnsupportedDType { dtype, op });
            }
        }
        _ => return Err(Error::UnsupportedDType { dtype, op }),
    }

    let (module_key, shader, entry_point): (&str, &str, String) = match dtype {
        DType::I32 => ("unary_i32", UNARY_I32_SHADER, format!("{}_i32", op)),
        DType::U32 => ("unary_u32", UNARY_U32_SHADER, format!("{}_u32", op)),
        DType::F32 => {
            let ep: &'static str = match op {
                "neg" => "neg_f32",
                "abs" => "abs_f32",
                "sqrt" => "sqrt_f32",
                "exp" => "exp_f32",
                "log" => "log_f32",
                "sin" => "sin_f32",
                "cos" => "cos_f32",
                "tan" => "tan_f32",
                "atan" => "atan_f32",
                "tanh" => "tanh_f32",
                "recip" => "recip_f32",
                "floor" => "floor_f32",
                "ceil" => "ceil_f32",
                "round" => "round_f32",
                "round_ties_even" => "round_ties_even_f32",
                "trunc" => "trunc_f32",
                "rsqrt" => "rsqrt_f32",
                "cbrt" => "cbrt_f32",
                "exp2" => "exp2_f32",
                "expm1" => "expm1_f32",
                "log2" => "log2_f32",
                "log10" => "log10_f32",
                "log1p" => "log1p_f32",
                "asin" => "asin_f32",
                "acos" => "acos_f32",
                "sinh" => "sinh_f32",
                "cosh" => "cosh_f32",
                "asinh" => "asinh_f32",
                "acosh" => "acosh_f32",
                "atanh" => "atanh_f32",
                "square" => "square_f32",
                "sign" => "sign_f32",
                "relu" => "relu_f32",
                "sigmoid" => "sigmoid_f32",
                "silu" => "silu_f32",
                "gelu" => "gelu_f32",
                "isnan" => "isnan_f32",
                "isinf" => "isinf_f32",
                _ => return Err(Error::Internal(format!("Unknown unary op: {}", op))),
            };
            ("unary_f32", UNARY_SHADER, ep.to_string())
        }
        _ => unreachable!(),
    };

    let module = cache.get_or_create_module(module_key, shader);
    let layout = cache.get_or_create_layout(LayoutKey {
        num_storage_buffers: 2,
        num_uniform_buffers: 1,
        num_readonly_storage: 0,
    });
    let pipeline = cache.get_or_create_dynamic_pipeline(module_key, &entry_point, &module, &layout);
    let bind_group = cache.create_bind_group(&layout, &[a, out, params_buffer]);

    let mut encoder = cache
        .device()
        .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some(op) });
    {
        let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
            label: Some(op),
            timestamp_writes: None,
        });
        pass.set_pipeline(&pipeline);
        pass.set_bind_group(0, Some(&bind_group), &[]);
        dispatch_linear(&mut pass, numel);
    }
    queue.submit(std::iter::once(encoder.finish()));
    Ok(())
}

// ============================================================================
// Scalar Operations
// ============================================================================

/// Launch a scalar operation: `out[i] = a[i] op scalar`. F32, I32, U32.
/// leaky_relu and elu are F32-only.
pub fn launch_scalar_op(
    cache: &PipelineCache,
    queue: &Queue,
    op: &'static str,
    a: &Buffer,
    out: &Buffer,
    params_buffer: &Buffer,
    numel: usize,
    dtype: DType,
) -> Result<()> {
    // leaky_relu and elu have no integer meaning, so they stay float-only.
    if matches!(op, "leaky_relu" | "elu") && dtype != DType::F32 {
        return Err(Error::UnsupportedDType { dtype, op });
    }

    let (module_key, shader, suffix) = match dtype {
        DType::F32 => ("scalar_f32", SCALAR_SHADER, "f32"),
        DType::I32 => ("scalar_i32", SCALAR_I32_SHADER, "i32"),
        DType::U32 => ("scalar_u32", SCALAR_U32_SHADER, "u32"),
        _ => return Err(Error::UnsupportedDType { dtype, op }),
    };

    let entry_point: String = match dtype {
        DType::F32 => {
            // F32 uses static entry points
            let ep: &'static str = match op {
                "add_scalar" => "add_scalar_f32",
                "sub_scalar" => "sub_scalar_f32",
                "rsub_scalar" => "rsub_scalar_f32",
                "mul_scalar" => "mul_scalar_f32",
                "div_scalar" => "div_scalar_f32",
                "pow_scalar" => "pow_scalar_f32",
                "leaky_relu" => "leaky_relu_f32",
                "elu" => "elu_f32",
                _ => return Err(Error::Internal(format!("Unknown scalar op: {}", op))),
            };
            ep.to_string()
        }
        _ => {
            // I32/U32: format entry point
            match op {
                "add_scalar" | "sub_scalar" | "rsub_scalar" | "mul_scalar" | "div_scalar"
                | "pow_scalar" => format!("{}_{}", op, suffix),
                _ => return Err(Error::Internal(format!("Unknown scalar op: {}", op))),
            }
        }
    };

    let module = cache.get_or_create_module(module_key, shader);
    let layout = cache.get_or_create_layout(LayoutKey {
        num_storage_buffers: 2,
        num_uniform_buffers: 1,
        num_readonly_storage: 0,
    });
    let pipeline = cache.get_or_create_dynamic_pipeline(module_key, &entry_point, &module, &layout);
    let bind_group = cache.create_bind_group(&layout, &[a, out, params_buffer]);

    let mut encoder = cache
        .device()
        .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some(op) });
    {
        let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
            label: Some(op),
            timestamp_writes: None,
        });
        pass.set_pipeline(&pipeline);
        pass.set_bind_group(0, Some(&bind_group), &[]);
        dispatch_linear(&mut pass, numel);
    }
    queue.submit(std::iter::once(encoder.finish()));
    Ok(())
}

// ============================================================================
// Comparison Operations
// ============================================================================

/// Launch a comparison operation: `out[i] = (a[i] op b[i]) ? 1 : 0`. F32, I32, U32.
/// The mask carries the input dtype, matching CPU and CUDA.
pub fn launch_compare_op(
    cache: &PipelineCache,
    queue: &Queue,
    op: &'static str,
    a: &Buffer,
    b: &Buffer,
    out: &Buffer,
    params_buffer: &Buffer,
    numel: usize,
    dtype: DType,
) -> Result<()> {
    let (module_key, shader, suffix) = match dtype {
        DType::F32 => ("compare_f32", COMPARE_SHADER, "f32"),
        DType::I32 => ("compare_i32", COMPARE_I32_SHADER, "i32"),
        DType::U32 => ("compare_u32", COMPARE_U32_SHADER, "u32"),
        _ => return Err(Error::UnsupportedDType { dtype, op }),
    };

    let entry_point: String = match op {
        "eq" | "ne" | "lt" | "le" | "gt" | "ge" => format!("{}_{}", op, suffix),
        _ => return Err(Error::Internal(format!("Unknown compare op: {}", op))),
    };

    let module = cache.get_or_create_module(module_key, shader);
    let layout = cache.get_or_create_layout(LayoutKey {
        num_storage_buffers: 3,
        num_uniform_buffers: 1,
        num_readonly_storage: 0,
    });
    let pipeline = cache.get_or_create_dynamic_pipeline(module_key, &entry_point, &module, &layout);
    let bind_group = cache.create_bind_group(&layout, &[a, b, out, params_buffer]);

    let mut encoder = cache
        .device()
        .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some(op) });
    {
        let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
            label: Some(op),
            timestamp_writes: None,
        });
        pass.set_pipeline(&pipeline);
        pass.set_bind_group(0, Some(&bind_group), &[]);
        dispatch_linear(&mut pass, numel);
    }
    queue.submit(std::iter::once(encoder.finish()));
    Ok(())
}

// ============================================================================
// Cast Operations
// ============================================================================

/// Launch a cast operation: `out[i] = DstType(a[i])`. Supports F32 ↔ I32 ↔ U32.
pub fn launch_cast_op(
    cache: &PipelineCache,
    queue: &Queue,
    a: &Buffer,
    out: &Buffer,
    params_buffer: &Buffer,
    numel: usize,
    src_dtype: DType,
    dst_dtype: DType,
) -> Result<()> {
    if src_dtype == dst_dtype {
        return Ok(());
    }

    let (module_name, entry_point, shader_source): (&'static str, &'static str, &'static str) =
        match (src_dtype, dst_dtype) {
            (DType::F32, DType::I32) => ("cast_f32_i32", "cast_f32_to_i32", CAST_F32_TO_I32_SHADER),
            (DType::F32, DType::U32) => ("cast_f32_u32", "cast_f32_to_u32", CAST_F32_TO_U32_SHADER),
            (DType::I32, DType::F32) => ("cast_i32_f32", "cast_i32_to_f32", CAST_I32_TO_F32_SHADER),
            (DType::I32, DType::U32) => ("cast_i32_u32", "cast_i32_to_u32", CAST_I32_TO_U32_SHADER),
            (DType::U32, DType::F32) => ("cast_u32_f32", "cast_u32_to_f32", CAST_U32_TO_F32_SHADER),
            (DType::U32, DType::I32) => ("cast_u32_i32", "cast_u32_to_i32", CAST_U32_TO_I32_SHADER),
            _ => {
                return Err(Error::UnsupportedDType {
                    dtype: src_dtype,
                    op: "cast",
                });
            }
        };

    let module = cache.get_or_create_module(module_name, shader_source);
    let layout = cache.get_or_create_layout(LayoutKey {
        num_storage_buffers: 2,
        num_uniform_buffers: 1,
        num_readonly_storage: 0,
    });
    let pipeline = cache.get_or_create_pipeline(module_name, entry_point, &module, &layout);
    let bind_group = cache.create_bind_group(&layout, &[a, out, params_buffer]);

    let mut encoder = cache
        .device()
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("cast"),
        });
    {
        let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
            label: Some("cast"),
            timestamp_writes: None,
        });
        pass.set_pipeline(&pipeline);
        pass.set_bind_group(0, Some(&bind_group), &[]);
        dispatch_linear(&mut pass, numel);
    }
    queue.submit(std::iter::once(encoder.finish()));
    Ok(())
}