oxicuda-blas 0.1.3

OxiCUDA BLAS - GPU-accelerated BLAS operations (cuBLAS equivalent)
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
//! Mixed precision GEMM configurations.
//!
//! Mixed precision GEMM uses one data type for inputs (A, B matrices) and
//! a wider type for the accumulator (C matrix). This is the dominant
//! pattern in deep learning:
//!
//! - **FP16 input, FP32 accumulator** — standard mixed-precision training
//! - **BF16 input, FP32 accumulator** — preferred for large language models
//! - **FP8 input, FP32 accumulator** — inference on Hopper+
//!
//! The [`MixedPrecisionConfig`] struct provides validation and tile
//! selection for all supported input/accumulator combinations.

use oxicuda_ptx::prelude::{PtxType, SmVersion};

use crate::error::{BlasError, BlasResult};
use crate::level3::gemm::dispatch::TileConfig;

/// Mixed precision GEMM configuration.
///
/// Maps input precision + accumulator precision to optimal kernel config.
/// The accumulator precision determines the output (C) matrix type.
pub struct MixedPrecisionConfig;

impl MixedPrecisionConfig {
    /// Check if a given input/accumulator combination is supported.
    ///
    /// Support depends on both the combination and the target architecture.
    #[must_use]
    pub fn is_supported(input: PtxType, acc: PtxType, sm: SmVersion) -> bool {
        match (input, acc) {
            // FP16 input, FP32 accumulator — most common, all Tensor Core arches
            (PtxType::F16, PtxType::F32) => sm >= SmVersion::Sm75,
            // FP16 input, FP16 accumulator — pure half-precision
            (PtxType::F16, PtxType::F16) => sm >= SmVersion::Sm75,
            // BF16 input, FP32 accumulator — Ampere+
            (PtxType::BF16, PtxType::F32) => sm >= SmVersion::Sm80,
            // Pure FP32 — always supported
            (PtxType::F32, PtxType::F32) => true,
            // TF32 input (FP32 storage), FP32 accumulator — Ampere+
            (PtxType::TF32, PtxType::F32) => sm >= SmVersion::Sm80,
            // Pure FP64 — always supported
            (PtxType::F64, PtxType::F64) => true,
            // FP8 E4M3 input, FP32 accumulator — Ada Lovelace+
            (PtxType::E4M3, PtxType::F32) => sm >= SmVersion::Sm89,
            // FP8 E5M2 input, FP32 accumulator — Ada Lovelace+
            (PtxType::E5M2, PtxType::F32) => sm >= SmVersion::Sm89,
            // FP8 E4M3 input, FP16 accumulator — Hopper+
            (PtxType::E4M3, PtxType::F16) => sm >= SmVersion::Sm90,
            // FP8 E5M2 input, FP16 accumulator — Hopper+
            (PtxType::E5M2, PtxType::F16) => sm >= SmVersion::Sm90,
            _ => false,
        }
    }

    /// Get optimal tile configuration for a mixed-precision combination.
    ///
    /// Returns `None` if the combination is not supported on the given
    /// architecture.
    #[must_use]
    pub fn optimal_config(input: PtxType, acc: PtxType, sm: SmVersion) -> Option<TileConfig> {
        if !Self::is_supported(input, acc, sm) {
            return None;
        }

        Some(match (input, acc) {
            (PtxType::F16, PtxType::F32) | (PtxType::F16, PtxType::F16) => Self::f16_config(sm),
            (PtxType::BF16, PtxType::F32) => Self::bf16_config(sm),
            (PtxType::TF32, PtxType::F32) => Self::tf32_config(sm),
            (PtxType::F32, PtxType::F32) => Self::f32_config(sm),
            (PtxType::F64, PtxType::F64) => Self::f64_config(sm),
            (PtxType::E4M3, PtxType::F32)
            | (PtxType::E5M2, PtxType::F32)
            | (PtxType::E4M3, PtxType::F16)
            | (PtxType::E5M2, PtxType::F16) => Self::fp8_config(sm),
            _ => return None,
        })
    }

    /// Validate a mixed-precision configuration for the given architecture
    /// and dimensions.
    pub fn validate(
        input: PtxType,
        acc: PtxType,
        sm: SmVersion,
        m: u32,
        n: u32,
        k: u32,
    ) -> BlasResult<()> {
        if m == 0 || n == 0 || k == 0 {
            return Err(BlasError::InvalidDimension(format!(
                "mixed-precision GEMM dimensions must be non-zero: m={m}, n={n}, k={k}"
            )));
        }
        if !Self::is_supported(input, acc, sm) {
            return Err(BlasError::UnsupportedOperation(format!(
                "mixed precision {input:?} -> {acc:?} not supported on {sm}"
            )));
        }
        Ok(())
    }

    /// Return the element size in bytes for the input type.
    #[must_use]
    pub fn input_element_bytes(input: PtxType) -> Option<u32> {
        match input {
            PtxType::E4M3 | PtxType::E5M2 => Some(1),
            PtxType::F16 | PtxType::BF16 => Some(2),
            PtxType::F32 | PtxType::TF32 => Some(4),
            PtxType::F64 => Some(8),
            _ => None,
        }
    }

    /// Return the element size in bytes for the accumulator type.
    #[must_use]
    pub fn accumulator_element_bytes(acc: PtxType) -> Option<u32> {
        match acc {
            PtxType::F16 => Some(2),
            PtxType::F32 => Some(4),
            PtxType::F64 => Some(8),
            _ => None,
        }
    }

    // -- Internal tile-config helpers ------------------------------------------

    fn f16_config(sm: SmVersion) -> TileConfig {
        match sm {
            SmVersion::Sm90 | SmVersion::Sm90a | SmVersion::Sm100 | SmVersion::Sm120 => {
                TileConfig {
                    tile_m: 128,
                    tile_n: 256,
                    tile_k: 64,
                    warp_m: 64,
                    warp_n: 128,
                    stages: 4,
                    use_tensor_core: true,
                    split_k: 1,
                }
            }
            SmVersion::Sm80 | SmVersion::Sm86 | SmVersion::Sm89 => TileConfig {
                tile_m: 128,
                tile_n: 128,
                tile_k: 32,
                warp_m: 64,
                warp_n: 64,
                stages: 4,
                use_tensor_core: true,
                split_k: 1,
            },
            _ => TileConfig {
                tile_m: 128,
                tile_n: 128,
                tile_k: 32,
                warp_m: 32,
                warp_n: 32,
                stages: 2,
                use_tensor_core: true,
                split_k: 1,
            },
        }
    }

    fn bf16_config(sm: SmVersion) -> TileConfig {
        match sm {
            SmVersion::Sm90 | SmVersion::Sm90a | SmVersion::Sm100 | SmVersion::Sm120 => {
                TileConfig {
                    tile_m: 128,
                    tile_n: 256,
                    tile_k: 64,
                    warp_m: 64,
                    warp_n: 128,
                    stages: 4,
                    use_tensor_core: true,
                    split_k: 1,
                }
            }
            _ => TileConfig {
                tile_m: 128,
                tile_n: 128,
                tile_k: 32,
                warp_m: 64,
                warp_n: 64,
                stages: 4,
                use_tensor_core: true,
                split_k: 1,
            },
        }
    }

    fn tf32_config(sm: SmVersion) -> TileConfig {
        match sm {
            SmVersion::Sm90 | SmVersion::Sm90a | SmVersion::Sm100 | SmVersion::Sm120 => {
                TileConfig {
                    tile_m: 128,
                    tile_n: 128,
                    tile_k: 32,
                    warp_m: 64,
                    warp_n: 64,
                    stages: 4,
                    use_tensor_core: true,
                    split_k: 1,
                }
            }
            _ => TileConfig {
                tile_m: 128,
                tile_n: 128,
                tile_k: 16,
                warp_m: 64,
                warp_n: 64,
                stages: 3,
                use_tensor_core: true,
                split_k: 1,
            },
        }
    }

    fn f32_config(sm: SmVersion) -> TileConfig {
        match sm {
            SmVersion::Sm90 | SmVersion::Sm90a | SmVersion::Sm100 | SmVersion::Sm120 => {
                TileConfig {
                    tile_m: 128,
                    tile_n: 128,
                    tile_k: 32,
                    warp_m: 64,
                    warp_n: 64,
                    stages: 4,
                    use_tensor_core: false,
                    split_k: 1,
                }
            }
            _ => TileConfig {
                tile_m: 128,
                tile_n: 64,
                tile_k: 16,
                warp_m: 32,
                warp_n: 32,
                stages: 2,
                use_tensor_core: false,
                split_k: 1,
            },
        }
    }

    fn f64_config(sm: SmVersion) -> TileConfig {
        match sm {
            SmVersion::Sm90 | SmVersion::Sm90a | SmVersion::Sm100 | SmVersion::Sm120 => {
                TileConfig {
                    tile_m: 64,
                    tile_n: 64,
                    tile_k: 16,
                    warp_m: 32,
                    warp_n: 32,
                    stages: 4,
                    use_tensor_core: true,
                    split_k: 1,
                }
            }
            SmVersion::Sm80 | SmVersion::Sm86 => TileConfig {
                tile_m: 64,
                tile_n: 64,
                tile_k: 16,
                warp_m: 32,
                warp_n: 32,
                stages: 3,
                use_tensor_core: true,
                split_k: 1,
            },
            _ => TileConfig {
                tile_m: 64,
                tile_n: 64,
                tile_k: 8,
                warp_m: 32,
                warp_n: 32,
                stages: 2,
                use_tensor_core: false,
                split_k: 1,
            },
        }
    }

    fn fp8_config(sm: SmVersion) -> TileConfig {
        match sm {
            SmVersion::Sm90 | SmVersion::Sm90a => TileConfig {
                tile_m: 128,
                tile_n: 256,
                tile_k: 128,
                warp_m: 64,
                warp_n: 128,
                stages: 4,
                use_tensor_core: true,
                split_k: 1,
            },
            SmVersion::Sm100 | SmVersion::Sm120 => TileConfig {
                tile_m: 256,
                tile_n: 256,
                tile_k: 128,
                warp_m: 64,
                warp_n: 128,
                stages: 4,
                use_tensor_core: true,
                split_k: 1,
            },
            _ => TileConfig {
                tile_m: 128,
                tile_n: 128,
                tile_k: 64,
                warp_m: 64,
                warp_n: 64,
                stages: 4,
                use_tensor_core: true,
                split_k: 1,
            },
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn f16_f32_supported_everywhere() {
        assert!(MixedPrecisionConfig::is_supported(
            PtxType::F16,
            PtxType::F32,
            SmVersion::Sm75
        ));
        assert!(MixedPrecisionConfig::is_supported(
            PtxType::F16,
            PtxType::F32,
            SmVersion::Sm90
        ));
    }

    #[test]
    fn bf16_requires_ampere() {
        assert!(!MixedPrecisionConfig::is_supported(
            PtxType::BF16,
            PtxType::F32,
            SmVersion::Sm75
        ));
        assert!(MixedPrecisionConfig::is_supported(
            PtxType::BF16,
            PtxType::F32,
            SmVersion::Sm80
        ));
    }

    #[test]
    fn fp8_requires_ada() {
        assert!(!MixedPrecisionConfig::is_supported(
            PtxType::E4M3,
            PtxType::F32,
            SmVersion::Sm86
        ));
        assert!(MixedPrecisionConfig::is_supported(
            PtxType::E4M3,
            PtxType::F32,
            SmVersion::Sm89
        ));
    }

    #[test]
    fn fp8_f16_acc_requires_hopper() {
        assert!(!MixedPrecisionConfig::is_supported(
            PtxType::E4M3,
            PtxType::F16,
            SmVersion::Sm89
        ));
        assert!(MixedPrecisionConfig::is_supported(
            PtxType::E4M3,
            PtxType::F16,
            SmVersion::Sm90
        ));
    }

    #[test]
    fn unsupported_combo_returns_none() {
        assert!(
            MixedPrecisionConfig::optimal_config(PtxType::BF16, PtxType::F64, SmVersion::Sm90)
                .is_none()
        );
    }

    #[test]
    fn optimal_config_f16_f32_hopper() {
        let cfg = MixedPrecisionConfig::optimal_config(PtxType::F16, PtxType::F32, SmVersion::Sm90);
        assert!(cfg.is_some());
        let cfg = cfg.unwrap_or_else(|| unreachable!());
        assert!(cfg.use_tensor_core);
        assert_eq!(cfg.tile_n, 256);
    }

    #[test]
    fn validate_ok() {
        assert!(
            MixedPrecisionConfig::validate(
                PtxType::F16,
                PtxType::F32,
                SmVersion::Sm80,
                128,
                128,
                128
            )
            .is_ok()
        );
    }

    #[test]
    fn validate_zero_dim() {
        assert!(
            MixedPrecisionConfig::validate(
                PtxType::F16,
                PtxType::F32,
                SmVersion::Sm80,
                0,
                128,
                128
            )
            .is_err()
        );
    }

    #[test]
    fn validate_unsupported_combo() {
        assert!(
            MixedPrecisionConfig::validate(
                PtxType::BF16,
                PtxType::F32,
                SmVersion::Sm75,
                128,
                128,
                128
            )
            .is_err()
        );
    }

    #[test]
    fn input_element_bytes() {
        assert_eq!(
            MixedPrecisionConfig::input_element_bytes(PtxType::E4M3),
            Some(1)
        );
        assert_eq!(
            MixedPrecisionConfig::input_element_bytes(PtxType::F16),
            Some(2)
        );
        assert_eq!(
            MixedPrecisionConfig::input_element_bytes(PtxType::F32),
            Some(4)
        );
        assert_eq!(
            MixedPrecisionConfig::input_element_bytes(PtxType::F64),
            Some(8)
        );
    }

    #[test]
    fn accumulator_element_bytes() {
        assert_eq!(
            MixedPrecisionConfig::accumulator_element_bytes(PtxType::F16),
            Some(2)
        );
        assert_eq!(
            MixedPrecisionConfig::accumulator_element_bytes(PtxType::F32),
            Some(4)
        );
        assert_eq!(
            MixedPrecisionConfig::accumulator_element_bytes(PtxType::F64),
            Some(8)
        );
        assert_eq!(
            MixedPrecisionConfig::accumulator_element_bytes(PtxType::U32),
            None
        );
    }

    #[test]
    fn f32_f32_always_supported() {
        assert!(MixedPrecisionConfig::is_supported(
            PtxType::F32,
            PtxType::F32,
            SmVersion::Sm75
        ));
        let cfg = MixedPrecisionConfig::optimal_config(PtxType::F32, PtxType::F32, SmVersion::Sm75);
        assert!(cfg.is_some());
    }
}