gemmkit 0.1.1

A clean, extensible, high-performance GEMM (general matrix multiply) engine
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
//! Integer (`i8` -> `i32`) and requantizing (`i8` -> `i8`/`u8`) GEMM entries: the
//! heterogeneous-output counterpart of the plain `gemm` surface, needed because
//! `i8 -> i32` and `i8 -> i8`/`u8` cannot be expressed through a single `T` type param
use super::*;
#[cfg(feature = "epilogue")]
use crate::kernel::epilogue::BiasDim;

/// Integer GEMM: `C <- alpha*A*B + beta*C` with `i8` inputs accumulated into an `i32`
/// output (`alpha`, `beta`, `C` are `i32`). Wraps on overflow, the standard integer-GEMM
/// convention. Uses the thread-local workspace pool
///
/// A separate entry point from [`gemm`] because the input/output types differ (`i8` vs
/// `i32`), which the homogeneous `gemm<T>` surface cannot express
///
/// # Panics
/// Same shape / bounds / aliasing conditions as [`gemm`]: `A.cols == B.rows`,
/// `A.rows == C.rows`, `B.cols == C.cols`, every view in bounds, and `C` addresses each
/// element uniquely without overlapping `A`/`B`. For negative strides or raw pointers use
/// [`gemm_i8_unchecked`] ([`gemm_unchecked`] is homogeneous and cannot serve `i8 -> i32`)
#[cfg(feature = "int8")]
pub fn gemm_i8(
    alpha: i32,
    a: MatRef<'_, i8>,
    b: MatRef<'_, i8>,
    beta: i32,
    c: MatMut<'_, i32>,
    par: Parallelism,
) {
    workspace::with_thread_pool(|ws| gemm_i8_with(ws, alpha, a, b, beta, c, par));
}

/// Like [`gemm_i8`] but reuses a caller-owned [`Workspace`] instead of the thread-local pool
///
/// # Panics
/// Same conditions as [`gemm_i8`]
#[cfg(feature = "int8")]
pub fn gemm_i8_with(
    ws: &mut Workspace,
    alpha: i32,
    a: MatRef<'_, i8>,
    b: MatRef<'_, i8>,
    beta: i32,
    c: MatMut<'_, i32>,
    par: Parallelism,
) {
    validate_gemm_views(&a, &b, &c);

    // SAFETY: validated above, shapes agree, every stride in bounds, C addresses
    // each (i,j) uniquely and does not overlap A/B
    unsafe {
        dispatch::execute_int(
            dispatch::IntTask {
                m: a.rows,
                k: a.cols,
                n: b.cols,
                alpha,
                a: a.data.as_ptr(),
                rsa: a.rs,
                csa: a.cs,
                b: b.data.as_ptr(),
                rsb: b.rs,
                csb: b.cs,
                beta,
                c: c.data.as_mut_ptr(),
                rsc: c.rs,
                csc: c.cs,
            },
            par,
            ws,
        );
    }
}

/// `C(i32) <- alpha*A(i8)*B(i8) + beta*C` over raw pointers and `isize` element strides,
/// with no bounds, alias, or shape checks: the `i8 -> i32` escape hatch for negative
/// strides or raw-pointer callers, since [`gemm_unchecked`] is typed for the homogeneous
/// surface and cannot express a differing output type. Uses the thread-local workspace pool
///
/// # Safety
/// `a`/`b` valid for reads and `c` valid for read+write over every `(i,j)` implied by the
/// dimensions and strides; `c` does not alias `a`/`b`; when `beta == 0`, `c` need not be
/// initialized
#[cfg(feature = "int8")]
#[allow(clippy::too_many_arguments)]
pub unsafe fn gemm_i8_unchecked(
    m: usize,
    k: usize,
    n: usize,
    alpha: i32,
    a: *const i8,
    rsa: isize,
    csa: isize,
    b: *const i8,
    rsb: isize,
    csb: isize,
    beta: i32,
    c: *mut i32,
    rsc: isize,
    csc: isize,
    par: Parallelism,
) {
    unsafe {
        workspace::with_thread_pool(|ws| {
            dispatch::execute_int(
                dispatch::IntTask {
                    m,
                    k,
                    n,
                    alpha,
                    a,
                    rsa,
                    csa,
                    b,
                    rsb,
                    csb,
                    beta,
                    c,
                    rsc,
                    csc,
                },
                par,
                ws,
            );
        });
    }
}

/// Like [`gemm_i8_unchecked`] but reuses a caller-owned [`Workspace`] instead of the
/// thread-local pool
///
/// # Safety
/// See [`gemm_i8_unchecked`]
#[cfg(feature = "int8")]
#[allow(clippy::too_many_arguments)]
pub unsafe fn gemm_i8_unchecked_with(
    ws: &mut Workspace,
    m: usize,
    k: usize,
    n: usize,
    alpha: i32,
    a: *const i8,
    rsa: isize,
    csa: isize,
    b: *const i8,
    rsb: isize,
    csb: isize,
    beta: i32,
    c: *mut i32,
    rsc: isize,
    csc: isize,
    par: Parallelism,
) {
    // SAFETY: the caller guarantees `a`/`b` valid for reads and `c` for read+write over the
    // shape/strides, `c` not aliasing `a`/`b`, and `beta == 0` may leave `c` uninitialized
    unsafe {
        dispatch::execute_int(
            dispatch::IntTask {
                m,
                k,
                n,
                alpha,
                a,
                rsa,
                csa,
                b,
                rsb,
                csb,
                beta,
                c,
                rsc,
                csc,
            },
            par,
            ws,
        );
    }
}

/// The output scale for the requantizing entries: either 1 value applied to the whole tensor,
/// or 1 value per output row/channel (the per-channel quantized-inference convention). Every
/// scale must be finite and `> 0`
#[cfg(all(feature = "int8", feature = "epilogue"))]
#[derive(Copy, Clone)]
pub enum RequantScale<'a> {
    /// 1 scale applied to every output element (`alpha` folds into this)
    PerTensor(f32),
    /// 1 scale per output row / channel (length `A.rows == m`), the standard per-channel
    /// quantized-inference convention: `C[i,j]` is scaled by `scale_i`
    PerRow(&'a [f32]),
}

/// The quantization parameters for the requantizing entries: [`RequantScale`], an integer
/// `zero_point`, and an optional per-row `i32` bias (length `m`, the standard qlinear layer
/// bias). The output is `C[i,j] = clamp(zero_point + round_ne(scale*(sum_k A*B + bias[i])),
/// LO, HI)` with round-half-to-even, where `scale` is the per-tensor value or the per-row
/// `scale_i`, and `[LO, HI]` is set by the entry: `[-128, 127]` for [`gemm_i8_requant`],
/// `[0, 255]` for [`gemm_i8_requant_u8`]
#[cfg(all(feature = "int8", feature = "epilogue"))]
pub struct Requantize<'a> {
    /// Output scale, per-tensor or per-row/channel; every value must be finite and `> 0`
    pub scale: RequantScale<'a>,
    /// Output zero-point, added after rounding. Must lie in the output domain of the chosen
    /// entry: `[-128, 127]` for [`gemm_i8_requant`], `[0, 255]` for [`gemm_i8_requant_u8`]
    pub zero_point: i32,
    /// Optional per-row `i32` bias (length `m`), added to the accumulator before scaling
    pub bias: Option<&'a [i32]>,
}

/// Requantizing integer GEMM: `i8` inputs multiplied into an `i32` accumulator, then
/// requantized to an `i8` output in 1 pass, skipping the full `m x n` `i32` materialization
/// a separate [`gemm_i8`] followed by a requantize pass would need. No `alpha` (it folds into
/// `scale`) and no `beta` (accumulating into an already-quantized `C` is not well-defined).
/// Uses the thread-local workspace pool
///
/// # Panics
/// Same shape / bounds / aliasing conditions as [`gemm_i8`], plus: a non-finite or
/// non-positive `scale` (per-tensor or any per-row element); a per-row scale slice whose length
/// is not `A.rows` or which overlaps `C`; a `zero_point` outside `[-128, 127]`; or a bias whose
/// length is not `A.rows` or which overlaps `C`
#[cfg(all(feature = "int8", feature = "epilogue"))]
pub fn gemm_i8_requant(
    a: MatRef<'_, i8>,
    b: MatRef<'_, i8>,
    req: Requantize<'_>,
    c: MatMut<'_, i8>,
    par: Parallelism,
) {
    workspace::with_thread_pool(|ws| gemm_i8_requant_with(ws, a, b, req, c, par));
}

/// Bias validation shared by the `i8`- and `u8`-output requantizing entries: checks length
/// against `a_rows` and checks the byte ranges of `bias` and `C` for overlap (`TO` is 1 byte
/// either way), then lowers to the `(ptr, has_bias)` pair the dispatch task carries. Both checks
/// delegate to [`crate::adapter::requant_bias`], the single pointer-level implementation the view
/// adapters also consume, over `C`'s backing slice as a unit-stride footprint (the same byte range
/// the slice-based check compared). `scale` and `zero_point` are checked separately in each entry
/// since the zero-point band differs between `i8` and `u8`
#[cfg(all(feature = "int8", feature = "epilogue"))]
fn requant_bias<TO>(a_rows: usize, c: &MatMut<'_, TO>, bias: Option<&[i32]>) -> (*const i32, bool) {
    crate::adapter::requant_bias(a_rows, c.data.as_ptr(), &[(c.data.len(), 1)], bias)
}

/// Scale validation shared by the `i8`- and `u8`-output requantizing entries: a
/// [`RequantScale::PerTensor`] must be finite and `> 0`; a [`RequantScale::PerRow`] must have
/// length `a_rows`, must not overlap `C`'s byte range, and every element must be finite and
/// `> 0`. Lowers to the `(scale, row_scales_ptr, has_row_scales)` triple the dispatch task
/// carries: `PerTensor(s) -> (s, null, false)`, `PerRow(p) -> (0.0, p, true)`. Delegates to
/// [`crate::adapter::requant_scale`], the single pointer-level implementation the view adapters
/// also consume, over `C`'s backing slice as a unit-stride footprint
#[cfg(all(feature = "int8", feature = "epilogue"))]
fn requant_scale<TO>(
    a_rows: usize,
    c: &MatMut<'_, TO>,
    scale: RequantScale<'_>,
) -> (f32, *const f32, bool) {
    crate::adapter::requant_scale(a_rows, c.data.as_ptr(), &[(c.data.len(), 1)], scale)
}

/// Like [`gemm_i8_requant`] but reuses a caller-owned [`Workspace`] instead of the
/// thread-local pool
///
/// # Panics
/// Same conditions as [`gemm_i8_requant`]
#[cfg(all(feature = "int8", feature = "epilogue"))]
pub fn gemm_i8_requant_with(
    ws: &mut Workspace,
    a: MatRef<'_, i8>,
    b: MatRef<'_, i8>,
    req: Requantize<'_>,
    c: MatMut<'_, i8>,
    par: Parallelism,
) {
    validate_gemm_views(&a, &b, &c);

    let (scale, row_scales, has_row_scales) = requant_scale(a.rows, &c, req.scale);
    assert!(
        (-128..=127).contains(&req.zero_point),
        "gemmkit: requantize zero_point ({}) out of i8 range [-128, 127]",
        req.zero_point
    );

    let (bias_ptr, has_bias) = requant_bias(a.rows, &c, req.bias);

    // SAFETY: validated above, shapes agree, strides in bounds, C addresses each (i,j)
    // uniquely and does not alias A/B or the bias/scales; scale/zp are in range. The bias
    // and row-scale slices (borrowed for this call) outlive the `execute_int_requant` frame
    unsafe {
        dispatch::execute_int_requant(
            dispatch::RequantTask {
                m: a.rows,
                k: a.cols,
                n: b.cols,
                a: a.data.as_ptr(),
                rsa: a.rs,
                csa: a.cs,
                b: b.data.as_ptr(),
                rsb: b.rs,
                csb: b.cs,
                c: c.data.as_mut_ptr(),
                rsc: c.rs,
                csc: c.cs,
                scale,
                row_scales,
                has_row_scales,
                zp: req.zero_point,
                bias: bias_ptr,
                has_bias,
                bias_dim: BiasDim::PerRow,
            },
            par,
            ws,
        );
    }
}

/// `C(i8) <- clamp(zp + round_ne(scale*(A*B + bias)), -128, 127)` over raw pointers and
/// `isize` element strides, with no bounds, alias, or shape checks. `bias` is a per-row `i32`
/// pointer, read only when `has_bias`. The scale is the scalar `scale` unless `has_row_scales`
/// is set, in which case `row_scales` supplies 1 `f32` per output row (length `m`) instead.
/// Uses the thread-local workspace pool
///
/// # Safety
/// `a`/`b` valid for reads and `c` valid for writes over the shape/strides; `c` does not alias
/// `a`/`b`; when `has_bias`, `bias` is valid for `m` reads and disjoint from `c`; when
/// `has_row_scales`, `row_scales` is valid for `m` reads and disjoint from `c` (otherwise
/// `row_scales` may be null or dangling); every applied scale is finite and `> 0`, and
/// `zero_point` is in `[-128, 127]` (the checked API enforces the last 2)
#[cfg(all(feature = "int8", feature = "epilogue"))]
#[allow(clippy::too_many_arguments)]
pub unsafe fn gemm_i8_requant_unchecked(
    m: usize,
    k: usize,
    n: usize,
    a: *const i8,
    rsa: isize,
    csa: isize,
    b: *const i8,
    rsb: isize,
    csb: isize,
    scale: f32,
    row_scales: *const f32,
    has_row_scales: bool,
    zero_point: i32,
    bias: *const i32,
    has_bias: bool,
    c: *mut i8,
    rsc: isize,
    csc: isize,
    par: Parallelism,
) {
    // SAFETY: preconditions forwarded to the caller (see # Safety)
    unsafe {
        workspace::with_thread_pool(|ws| {
            gemm_i8_requant_unchecked_with(
                ws,
                m,
                k,
                n,
                a,
                rsa,
                csa,
                b,
                rsb,
                csb,
                scale,
                row_scales,
                has_row_scales,
                zero_point,
                bias,
                has_bias,
                c,
                rsc,
                csc,
                par,
            );
        });
    }
}

/// Like [`gemm_i8_requant_unchecked`] but reuses a caller-owned [`Workspace`] instead of
/// the thread-local pool
///
/// # Safety
/// See [`gemm_i8_requant_unchecked`]
#[cfg(all(feature = "int8", feature = "epilogue"))]
#[allow(clippy::too_many_arguments)]
pub unsafe fn gemm_i8_requant_unchecked_with(
    ws: &mut Workspace,
    m: usize,
    k: usize,
    n: usize,
    a: *const i8,
    rsa: isize,
    csa: isize,
    b: *const i8,
    rsb: isize,
    csb: isize,
    scale: f32,
    row_scales: *const f32,
    has_row_scales: bool,
    zero_point: i32,
    bias: *const i32,
    has_bias: bool,
    c: *mut i8,
    rsc: isize,
    csc: isize,
    par: Parallelism,
) {
    // SAFETY: the caller guarantees `a`/`b` valid for reads and `c` for writes over the
    // shape/strides, `c` not aliasing `a`/`b`, when `has_bias` a valid disjoint `m`-length bias,
    // when `has_row_scales` a valid disjoint `m`-length scale vector, and `scale`/`zero_point`
    // in range (see [`gemm_i8_requant_unchecked`])
    unsafe {
        dispatch::execute_int_requant(
            dispatch::RequantTask {
                m,
                k,
                n,
                a,
                rsa,
                csa,
                b,
                rsb,
                csb,
                c,
                rsc,
                csc,
                scale,
                row_scales,
                has_row_scales,
                zp: zero_point,
                bias,
                has_bias,
                bias_dim: BiasDim::PerRow,
            },
            par,
            ws,
        );
    }
}

/// Requantizing integer GEMM with an unsigned `u8` output (the ONNX QLinearMatMul
/// activation convention): `i8` inputs multiplied into an `i32` accumulator, then requantized
/// in 1 pass to `C[i,j] = clamp(zero_point + round_ne(scale*(sum_k A*B + bias[i])), 0, 255)`
/// with round-half-to-even, where `scale` is the per-tensor value or the per-row `scale_i`.
/// The `u8`-output twin of [`gemm_i8_requant`], differing only in the output domain (`[0, 255]`
/// instead of `[-128, 127]`) and the accepted `zero_point` range. No `alpha` (folds into
/// `scale`) and no `beta` (accumulating into an already-quantized `C` is not well-defined).
/// Uses the thread-local workspace pool
///
/// # Determinism
/// Same contract as [`gemm_i8_requant`]: the `i32` accumulation is exact and ISA-independent,
/// and the requantize step is bit-exact across every ISA (scalar, FMA, AVX-512F, VNNI) and
/// across the vector and scalar store paths
///
/// # Panics
/// Same shape / bounds / aliasing conditions as [`gemm_i8`], plus: a non-finite or non-positive
/// `scale` (per-tensor or any per-row element); a per-row scale slice whose length is not `A.rows`
/// or which overlaps `C`; a `zero_point` outside `[0, 255]`; or a bias whose length is not
/// `A.rows` or which overlaps `C`
#[cfg(all(feature = "int8", feature = "epilogue"))]
pub fn gemm_i8_requant_u8(
    a: MatRef<'_, i8>,
    b: MatRef<'_, i8>,
    req: Requantize<'_>,
    c: MatMut<'_, u8>,
    par: Parallelism,
) {
    workspace::with_thread_pool(|ws| gemm_i8_requant_u8_with(ws, a, b, req, c, par));
}

/// Like [`gemm_i8_requant_u8`] but reuses a caller-owned [`Workspace`] instead of the
/// thread-local pool
///
/// # Panics
/// Same conditions as [`gemm_i8_requant_u8`]
#[cfg(all(feature = "int8", feature = "epilogue"))]
pub fn gemm_i8_requant_u8_with(
    ws: &mut Workspace,
    a: MatRef<'_, i8>,
    b: MatRef<'_, i8>,
    req: Requantize<'_>,
    c: MatMut<'_, u8>,
    par: Parallelism,
) {
    validate_gemm_views(&a, &b, &c);

    let (scale, row_scales, has_row_scales) = requant_scale(a.rows, &c, req.scale);
    assert!(
        (0..=255).contains(&req.zero_point),
        "gemmkit: requantize zero_point ({}) out of u8 range [0, 255]",
        req.zero_point
    );

    let (bias_ptr, has_bias) = requant_bias(a.rows, &c, req.bias);

    // SAFETY: validated above, shapes agree, strides in bounds, C addresses each (i,j)
    // uniquely and does not alias A/B or the bias/scales; scale/zp are in range. The bias
    // and row-scale slices (borrowed for this call) outlive the `execute_int_requant` frame
    unsafe {
        dispatch::execute_int_requant(
            dispatch::RequantTask {
                m: a.rows,
                k: a.cols,
                n: b.cols,
                a: a.data.as_ptr(),
                rsa: a.rs,
                csa: a.cs,
                b: b.data.as_ptr(),
                rsb: b.rs,
                csb: b.cs,
                c: c.data.as_mut_ptr(),
                rsc: c.rs,
                csc: c.cs,
                scale,
                row_scales,
                has_row_scales,
                zp: req.zero_point,
                bias: bias_ptr,
                has_bias,
                bias_dim: BiasDim::PerRow,
            },
            par,
            ws,
        );
    }
}

/// `C(u8) <- clamp(zp + round_ne(scale*(A*B + bias)), 0, 255)` over raw pointers and `isize`
/// element strides, with no bounds, alias, or shape checks: the unsigned twin of
/// [`gemm_i8_requant_unchecked`]. `bias` is a per-row `i32` pointer, read only when `has_bias`.
/// The scale is the scalar `scale` unless `has_row_scales` is set, in which case `row_scales`
/// supplies 1 `f32` per output row (length `m`) instead. Uses the thread-local workspace pool
///
/// # Safety
/// `a`/`b` valid for reads and `c` valid for writes over the shape/strides; `c` does not alias
/// `a`/`b`; when `has_bias`, `bias` is valid for `m` reads and disjoint from `c`; when
/// `has_row_scales`, `row_scales` is valid for `m` reads and disjoint from `c` (otherwise
/// `row_scales` may be null or dangling); every applied scale is finite and `> 0`, and
/// `zero_point` is in `[0, 255]` (the checked API enforces the last 2)
#[cfg(all(feature = "int8", feature = "epilogue"))]
#[allow(clippy::too_many_arguments)]
pub unsafe fn gemm_i8_requant_u8_unchecked(
    m: usize,
    k: usize,
    n: usize,
    a: *const i8,
    rsa: isize,
    csa: isize,
    b: *const i8,
    rsb: isize,
    csb: isize,
    scale: f32,
    row_scales: *const f32,
    has_row_scales: bool,
    zero_point: i32,
    bias: *const i32,
    has_bias: bool,
    c: *mut u8,
    rsc: isize,
    csc: isize,
    par: Parallelism,
) {
    // SAFETY: preconditions forwarded to the caller (see # Safety)
    unsafe {
        workspace::with_thread_pool(|ws| {
            gemm_i8_requant_u8_unchecked_with(
                ws,
                m,
                k,
                n,
                a,
                rsa,
                csa,
                b,
                rsb,
                csb,
                scale,
                row_scales,
                has_row_scales,
                zero_point,
                bias,
                has_bias,
                c,
                rsc,
                csc,
                par,
            );
        });
    }
}

/// Like [`gemm_i8_requant_u8_unchecked`] but reuses a caller-owned [`Workspace`] instead
/// of the thread-local pool
///
/// # Safety
/// See [`gemm_i8_requant_u8_unchecked`]
#[cfg(all(feature = "int8", feature = "epilogue"))]
#[allow(clippy::too_many_arguments)]
pub unsafe fn gemm_i8_requant_u8_unchecked_with(
    ws: &mut Workspace,
    m: usize,
    k: usize,
    n: usize,
    a: *const i8,
    rsa: isize,
    csa: isize,
    b: *const i8,
    rsb: isize,
    csb: isize,
    scale: f32,
    row_scales: *const f32,
    has_row_scales: bool,
    zero_point: i32,
    bias: *const i32,
    has_bias: bool,
    c: *mut u8,
    rsc: isize,
    csc: isize,
    par: Parallelism,
) {
    // SAFETY: the caller guarantees `a`/`b` valid for reads and `c` for writes over the
    // shape/strides, `c` not aliasing `a`/`b`, when `has_bias` a valid disjoint `m`-length bias,
    // when `has_row_scales` a valid disjoint `m`-length scale vector, and `scale`/`zero_point`
    // in range (see [`gemm_i8_requant_u8_unchecked`])
    unsafe {
        dispatch::execute_int_requant(
            dispatch::RequantTask {
                m,
                k,
                n,
                a,
                rsa,
                csa,
                b,
                rsb,
                csb,
                c,
                rsc,
                csc,
                scale,
                row_scales,
                has_row_scales,
                zp: zero_point,
                bias,
                has_bias,
                bias_dim: BiasDim::PerRow,
            },
            par,
            ws,
        );
    }
}