numr 0.5.2

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
//! Matrix multiplication operation implementations for WebGPU.

use super::helpers::*;
use crate::error::Error;
use crate::error::Result;
use crate::ops::{matmul_bias_output_shape, matmul_output_shape, validate_matmul_bias_dtypes};
use crate::runtime::ensure_contiguous;
use crate::runtime::wgpu::shaders::{gemv_bt, matmul};
use crate::runtime::wgpu::{WgpuClient, WgpuRuntime};
use crate::tensor::Tensor;

/// Detect if a 2D tensor is a simple transpose of a contiguous [N,K] matrix.
/// Shape [K, N] with strides [1, K] means it's a transpose view of contiguous [N, K].
fn is_simple_transpose_2d(tensor: &Tensor<WgpuRuntime>) -> bool {
    let shape = tensor.shape();
    let strides = tensor.strides();
    if shape.len() != 2 {
        return false;
    }
    strides[0] == 1 && strides[1] == shape[0] as isize
}

/// Detect if the last two dims of a 3D tensor are a simple transpose.
/// Shape [B, K, N] with strides [N*K, 1, K] means each batch slice
/// is a transpose of contiguous [N, K].
fn is_batched_transpose_last2(tensor: &Tensor<WgpuRuntime>) -> bool {
    let shape = tensor.shape();
    let strides = tensor.strides();
    if shape.len() != 3 {
        return false;
    }
    let k = shape[1];
    let n = shape[2];
    strides[1] == 1 && strides[2] == k as isize && strides[0] == (n * k) as isize
}

pub(crate) fn native_matmul(
    client: &WgpuClient,
    a: &Tensor<WgpuRuntime>,
    b: &Tensor<WgpuRuntime>,
) -> Result<Tensor<WgpuRuntime>> {
    let dtype = a.dtype();

    let out_shape = matmul_output_shape(a.shape(), b.shape())
        .ok_or_else(|| Error::shape_mismatch(a.shape(), b.shape()))?;

    let a_shape = a.shape();
    let b_shape = b.shape();

    // Handle 2D case
    if a_shape.len() == 2 && b_shape.len() == 2 {
        let m = a_shape[0];
        let k = a_shape[1];
        let n = b_shape[1];

        // GEMV-BT fast path: transposed B with small M
        if m <= 16 && is_simple_transpose_2d(b) {
            let a_contig = ensure_contiguous(a);
            let out = alloc_output(client, &out_shape, dtype);

            let a_buf = get_tensor_buffer(&a_contig)?;
            let b_buf = get_tensor_buffer(b)?; // Use original [N,K] buffer directly
            let out_buf = get_tensor_buffer(&out)?;

            let params = MatmulParams {
                m: m as u32,
                k: k as u32,
                n: n as u32,
                batch_size: 1,
            };
            let params_buf = create_params_buffer(client, &params);

            gemv_bt::launch_gemv_bt(
                client.pipeline_cache(),
                client.wgpu_queue(),
                &a_buf,
                &b_buf,
                &out_buf,
                &params_buf,
                m,
                n,
                dtype,
            )?;

            return Ok(out);
        }

        let a_contig = ensure_contiguous(a);
        let b_contig = ensure_contiguous(b);

        let out = alloc_output(client, &out_shape, dtype);

        let a_buf = get_tensor_buffer(&a_contig)?;
        let b_buf = get_tensor_buffer(&b_contig)?;
        let out_buf = get_tensor_buffer(&out)?;

        let params = MatmulParams {
            m: m as u32,
            k: k as u32,
            n: n as u32,
            batch_size: 1,
        };
        let params_buf = create_params_buffer(client, &params);

        // Use tiled for larger matrices, simple for small ones
        if m * n > 256 * 256 {
            matmul::launch_matmul(
                client.pipeline_cache(),
                client.wgpu_queue(),
                &a_buf,
                &b_buf,
                &out_buf,
                &params_buf,
                m,
                n,
                dtype,
            )?;
        } else {
            matmul::launch_matmul_simple(
                client.pipeline_cache(),
                client.wgpu_queue(),
                &a_buf,
                &b_buf,
                &out_buf,
                &params_buf,
                m,
                n,
                dtype,
            )?;
        }

        return Ok(out);
    }

    // Handle batched (3D) matmul natively
    if a_shape.len() == 3 && b_shape.len() == 3 {
        let batch_size = a_shape[0];
        let m = a_shape[1];
        let k = a_shape[2];
        let n = b_shape[2];

        // Validate batch dimensions match
        if b_shape[0] != batch_size {
            return Err(Error::ShapeMismatch {
                expected: vec![batch_size, m, k],
                got: b_shape.to_vec(),
            });
        }

        // GEMV-BT fast path: transposed B with small M
        if m <= 16 && is_batched_transpose_last2(b) {
            let a_contig = ensure_contiguous(a);
            let out = alloc_output(client, &out_shape, dtype);

            let a_buf = get_tensor_buffer(&a_contig)?;
            let b_buf = get_tensor_buffer(b)?;
            let out_buf = get_tensor_buffer(&out)?;

            let params = MatmulParams {
                m: m as u32,
                k: k as u32,
                n: n as u32,
                batch_size: batch_size as u32,
            };
            let params_buf = create_params_buffer(client, &params);

            gemv_bt::launch_batched_gemv_bt(
                client.pipeline_cache(),
                client.wgpu_queue(),
                &a_buf,
                &b_buf,
                &out_buf,
                &params_buf,
                m,
                n,
                batch_size,
                dtype,
            )?;

            return Ok(out);
        }

        let a_contig = ensure_contiguous(a);
        let b_contig = ensure_contiguous(b);

        let out = alloc_output(client, &out_shape, dtype);

        let a_buf = get_tensor_buffer(&a_contig)?;
        let b_buf = get_tensor_buffer(&b_contig)?;
        let out_buf = get_tensor_buffer(&out)?;

        let params = MatmulParams {
            m: m as u32,
            k: k as u32,
            n: n as u32,
            batch_size: batch_size as u32,
        };
        let params_buf = create_params_buffer(client, &params);

        matmul::launch_batched_matmul(
            client.pipeline_cache(),
            client.wgpu_queue(),
            &a_buf,
            &b_buf,
            &out_buf,
            &params_buf,
            m,
            n,
            batch_size,
            dtype,
        )?;

        return Ok(out);
    }

    // >3D: flatten leading dims into batch, run 3D batched matmul, reshape back.
    // Same strategy as CUDA backend (which computes batch_size = product of leading dims).
    let ndim_a = a_shape.len();
    let ndim_b = b_shape.len();

    if ndim_a < 2 || ndim_b < 2 {
        return Err(Error::BackendLimitation {
            backend: "WebGPU",
            operation: "matmul",
            reason: format!(
                "requires at least 2D tensors, got shapes {:?} and {:?}",
                a_shape, b_shape
            ),
        });
    }

    let m = a_shape[ndim_a - 2];
    let k = a_shape[ndim_a - 1];
    let n = b_shape[ndim_b - 1];

    let batch_a: usize = a_shape[..ndim_a - 2].iter().product();
    let batch_b: usize = b_shape[..ndim_b - 2].iter().product();
    let batch_size = batch_a.max(batch_b);

    // Flatten to 3D
    let a_3d = ensure_contiguous(a)
        .reshape(&[batch_a, m, k])
        .map_err(|_| Error::shape_mismatch(a_shape, b_shape))?;
    let b_3d = ensure_contiguous(b)
        .reshape(&[batch_b, k, n])
        .map_err(|_| Error::shape_mismatch(a_shape, b_shape))?;

    // Broadcast if batch dims differ (one must be 1)
    let (a_batched, b_batched) = if batch_a == batch_b {
        (a_3d, b_3d)
    } else if batch_a == 1 {
        (
            a_3d.broadcast_to(&[batch_size, m, k])
                .map_err(|_| Error::shape_mismatch(a_shape, b_shape))?
                .contiguous(),
            b_3d,
        )
    } else if batch_b == 1 {
        (
            a_3d,
            b_3d.broadcast_to(&[batch_size, k, n])
                .map_err(|_| Error::shape_mismatch(a_shape, b_shape))?
                .contiguous(),
        )
    } else {
        return Err(Error::shape_mismatch(a_shape, b_shape));
    };

    let a_buf = get_tensor_buffer(&a_batched)?;
    let b_buf = get_tensor_buffer(&b_batched)?;
    let out_flat = alloc_output(client, &[batch_size, m, n], dtype);
    let out_buf = get_tensor_buffer(&out_flat)?;

    let params = MatmulParams {
        m: m as u32,
        k: k as u32,
        n: n as u32,
        batch_size: batch_size as u32,
    };
    let params_buf = create_params_buffer(client, &params);

    matmul::launch_batched_matmul(
        client.pipeline_cache(),
        client.wgpu_queue(),
        &a_buf,
        &b_buf,
        &out_buf,
        &params_buf,
        m,
        n,
        batch_size,
        dtype,
    )?;

    // Reshape back to original leading dims + [m, n]
    let result = out_flat
        .reshape(&out_shape)
        .map_err(|_| Error::shape_mismatch(a_shape, b_shape))?;
    Ok(result)
}

/// Native WGPU implementation of fused matrix multiplication with bias.
///
/// Computes C = A @ B + bias where bias is a 1D tensor [N] broadcast across all rows.
/// The bias addition is fused into the GEMM epilogue for efficiency.
pub(crate) fn native_matmul_bias(
    client: &WgpuClient,
    a: &Tensor<WgpuRuntime>,
    b: &Tensor<WgpuRuntime>,
    bias: &Tensor<WgpuRuntime>,
) -> Result<Tensor<WgpuRuntime>> {
    // Validate dtypes using unified helper (ensures consistent error handling across backends)
    let dtype = validate_matmul_bias_dtypes(a.dtype(), b.dtype(), bias.dtype())?;

    // Validate shapes and compute output shape
    let out_shape = matmul_bias_output_shape(a.shape(), b.shape(), bias.shape())
        .ok_or_else(|| Error::shape_mismatch(a.shape(), b.shape()))?;

    let a_shape = a.shape();
    let b_shape = b.shape();

    // Handle 2D case
    if a_shape.len() == 2 && b_shape.len() == 2 {
        let m = a_shape[0];
        let k = a_shape[1];
        let n = b_shape[1];

        let a_contig = ensure_contiguous(a);
        let b_contig = ensure_contiguous(b);
        let bias_contig = ensure_contiguous(bias);

        let out = alloc_output(client, &out_shape, dtype);

        let a_buf = get_tensor_buffer(&a_contig)?;
        let b_buf = get_tensor_buffer(&b_contig)?;
        let bias_buf = get_tensor_buffer(&bias_contig)?;
        let out_buf = get_tensor_buffer(&out)?;

        let params = MatmulParams {
            m: m as u32,
            k: k as u32,
            n: n as u32,
            batch_size: 1,
        };
        let params_buf = create_params_buffer(client, &params);

        matmul::launch_matmul_bias(
            client.pipeline_cache(),
            client.wgpu_queue(),
            &a_buf,
            &b_buf,
            &bias_buf,
            &out_buf,
            &params_buf,
            m,
            n,
            dtype,
        )?;

        return Ok(out);
    }

    // Handle batched matmul_bias (3D tensors)
    if a_shape.len() == 3 && b_shape.len() == 3 {
        let batch_size = a_shape[0];
        let m = a_shape[1];
        let k = a_shape[2];
        let n = b_shape[2];

        // Validate batch dimensions match
        if b_shape[0] != batch_size {
            return Err(Error::ShapeMismatch {
                expected: vec![batch_size, m, k],
                got: b_shape.to_vec(),
            });
        }

        let a_contig = ensure_contiguous(a);
        let b_contig = ensure_contiguous(b);
        let bias_contig = ensure_contiguous(bias);

        let out = alloc_output(client, &out_shape, dtype);

        let a_buf = get_tensor_buffer(&a_contig)?;
        let b_buf = get_tensor_buffer(&b_contig)?;
        let bias_buf = get_tensor_buffer(&bias_contig)?;
        let out_buf = get_tensor_buffer(&out)?;

        let params = MatmulParams {
            m: m as u32,
            k: k as u32,
            n: n as u32,
            batch_size: batch_size as u32,
        };
        let params_buf = create_params_buffer(client, &params);

        matmul::launch_batched_matmul_bias(
            client.pipeline_cache(),
            client.wgpu_queue(),
            &a_buf,
            &b_buf,
            &bias_buf,
            &out_buf,
            &params_buf,
            m,
            n,
            batch_size,
            dtype,
        )?;

        return Ok(out);
    }

    // >3D tensors are not supported - return error instead of silent fallback
    // (WebGPU shader dispatch is limited to 3D workgroups)
    Err(Error::BackendLimitation {
        backend: "WebGPU",
        operation: "matmul_bias",
        reason: format!(
            "only supports 2D and 3D tensors, got shapes {:?} and {:?}",
            a.shape(),
            b.shape()
        ),
    })
}