cubek-matmul 0.2.0

CubeK: Matrix Multiplication Kernels
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
use crate::definition::{
    MatmulAvailabilityError, MatmulElems, MatmulSetupError, MatmulVectorSizes, StageIdent,
    TilingBlueprint,
};
use cubecl::{
    features::{MmaConfig, Plane as PlaneFeature, TypeUsage},
    ir::{DeviceProperties, ElemType, FloatKind, StorageType},
    prelude::*,
};
use cubek_std::{
    CubeDimResource, InvalidConfigError, MatrixLayout, TileSize,
    stage::SwizzleMode,
    tile::{
        CmmaMatmul, InterleavedMatmul, MmaIOConfig, MmaMatmul, Plane, PlaneVecMatInnerProduct,
        RegisterMatmul, TileScope as _, Unit,
    },
};

/// Tile-level matmul configuration. Each variant carries the per-kind config.
///
/// This is both the runtime selector and the comptime configuration: pick the
/// variant that matches the kernel you want, then forward the value into the
/// stage layer where its accessors drive allocation and execution.
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub enum TileMatmul {
    Cmma(CmmaMatmul),
    Mma(MmaMatmul),
    Register(RegisterMatmul),
    PlaneVec(PlaneVecMatInnerProduct),
    Interleaved(InterleavedMatmul),
}

/// Selector for the tile-level matmul kind, used before per-kind config exists.
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub enum TileMatmulKind {
    Cmma,
    Mma,
    Register,
    PlaneVec,
    Interleaved,
}

impl TileMatmul {
    pub fn plane_dim(&self) -> u32 {
        match self {
            TileMatmul::Cmma(c) => c.plane_dim,
            TileMatmul::Mma(c) => c.plane_dim,
            TileMatmul::Register(c) => c.plane_dim,
            TileMatmul::PlaneVec(c) => c.plane_dim,
            TileMatmul::Interleaved(c) => c.plane_dim,
        }
    }

    pub fn elements_in_tile_m(&self) -> u32 {
        match self {
            TileMatmul::Cmma(c) => c.tile_size.m(),
            TileMatmul::Mma(c) => c.tile_size.m(),
            TileMatmul::Register(c) => c.tile_size.m(),
            TileMatmul::PlaneVec(c) => c.tile_size.m(),
            TileMatmul::Interleaved(c) => c.tile_size.m(),
        }
    }

    pub fn elements_in_tile_n(&self) -> u32 {
        match self {
            TileMatmul::Cmma(c) => c.tile_size.n(),
            TileMatmul::Mma(c) => c.tile_size.n(),
            TileMatmul::Register(c) => c.tile_size.n(),
            TileMatmul::PlaneVec(c) => c.tile_size.n(),
            TileMatmul::Interleaved(c) => c.tile_size.n(),
        }
    }

    pub fn elements_in_tile_k(&self) -> u32 {
        match self {
            TileMatmul::Cmma(c) => c.tile_size.k(),
            TileMatmul::Mma(c) => c.tile_size.k(),
            TileMatmul::Register(c) => c.tile_size.k(),
            TileMatmul::PlaneVec(c) => c.tile_size.k(),
            TileMatmul::Interleaved(c) => c.tile_size.k(),
        }
    }

    pub fn swizzle_mode(&self, ident: StageIdent) -> SwizzleMode {
        let modes = match self {
            TileMatmul::Cmma(c) => c.swizzle_modes,
            TileMatmul::Mma(c) => c.swizzle_modes,
            TileMatmul::Register(c) => c.swizzle_modes,
            TileMatmul::PlaneVec(c) => c.swizzle_modes,
            TileMatmul::Interleaved(c) => c.swizzle_modes,
        };

        match ident {
            StageIdent::Lhs => modes.lhs,
            StageIdent::Rhs => modes.rhs,
            StageIdent::Acc => modes.acc,
            StageIdent::Out => modes.out,
        }
    }
}

impl TileMatmulKind {
    /// Returns whether this tile matmul requires specialized hardware accelerators (e.g., tensor cores).
    pub fn requires_accelerator(&self) -> bool {
        match self {
            TileMatmulKind::Cmma | TileMatmulKind::Mma => true,
            TileMatmulKind::Register | TileMatmulKind::PlaneVec | TileMatmulKind::Interleaved => {
                false
            }
        }
    }

    /// Whether this matmul kind is able to cast on load/store from the stage.
    pub fn can_cast_stage_element(&self) -> bool {
        match self {
            TileMatmulKind::Cmma => false,
            TileMatmulKind::Mma
            | TileMatmulKind::Register
            | TileMatmulKind::PlaneVec
            | TileMatmulKind::Interleaved => true,
        }
    }

    /// Returns whether this tile matmul may benefit from swizzling.
    /// Used to determine the selection, since swizzling may require different stage sizes.
    pub fn should_swizzle<R: Runtime>(&self, client: &ComputeClient<R>) -> bool {
        match self {
            TileMatmulKind::Cmma => {
                // Unsupported
                false
            }
            TileMatmulKind::Mma => {
                // No alignment means swizzling can't be properly used, since it needs to be applied to
                // the address, and alignment guarantees the offset is aligned to the pattern repeat.
                client.properties().features.alignment
            }
            TileMatmulKind::Register => {
                // Selection isn't getting rid of all conflicts with the current load strategy, but does
                // reduce conflicts significantly (i.e. average 18 vs average 5). Should try to find more
                // optimal settings in the future.
                client.properties().features.alignment
            }
            TileMatmulKind::PlaneVec => {
                // Supported but need to find good settings for this tiling. Currently tuned for `ldmatrix`.
                // Need to profile at some point
                false
            }
            TileMatmulKind::Interleaved => {
                // Selection isn't getting rid of all conflicts with the current load strategy, but does
                // reduce conflicts significantly (i.e. average 18 vs average 5). Should try to find more
                // optimal settings in the future.
                client.properties().features.alignment
            }
        }
    }

    /// Returns the compute resources required to run this matmul.
    pub fn cubedim_resource(&self) -> Result<CubeDimResource, InvalidConfigError> {
        Ok(match self {
            TileMatmulKind::Cmma
            | TileMatmulKind::Mma
            | TileMatmulKind::PlaneVec
            | TileMatmulKind::Interleaved => Plane::default_resource(),
            TileMatmulKind::Register => Unit::default_resource(),
        })
    }

    /// Constructs the configuration based on the matmul problem, selection, and vector sizes.
    pub fn expand_tile_matmul(
        &self,
        device_props: &DeviceProperties,
        blueprint: &TilingBlueprint,
        dtypes: &MatmulElems,
        vector_sizes: &MatmulVectorSizes,
    ) -> Result<TileMatmul, MatmulSetupError> {
        Ok(match self {
            TileMatmulKind::Cmma => TileMatmul::Cmma(CmmaMatmul::new(
                blueprint.tiling_scheme.tile_size,
                blueprint.plane_dim,
                blueprint.swizzle_modes,
            )),
            TileMatmulKind::Mma => TileMatmul::Mma(MmaMatmul {
                tile_size: blueprint.tiling_scheme.tile_size,
                plane_dim: blueprint.plane_dim,
                swizzle_modes: blueprint.swizzle_modes,
                mma_io_config: MmaIOConfig::new(
                    device_props,
                    dtypes.lhs_stage,
                    dtypes.rhs_stage,
                    dtypes.acc_stage,
                ),
            }),
            TileMatmulKind::Register => TileMatmul::Register(RegisterMatmul::new(
                blueprint.lhs_layout,
                blueprint.rhs_layout,
                blueprint.tiling_scheme.tile_size,
                blueprint.plane_dim,
                blueprint.swizzle_modes,
            )),
            TileMatmulKind::PlaneVec => TileMatmul::PlaneVec(PlaneVecMatInnerProduct::new(
                blueprint.tiling_scheme.tile_size,
                blueprint.plane_dim,
                blueprint.swizzle_modes,
                vector_sizes.lhs as u32,
            )),
            TileMatmulKind::Interleaved => TileMatmul::Interleaved(InterleavedMatmul::new(
                blueprint.tiling_scheme.tile_size,
                blueprint.plane_dim,
                blueprint.swizzle_modes,
            )),
        })
    }

    /// Returns whether a tile configuration is supported.
    pub fn is_supported<R: Runtime>(&self, client: &ComputeClient<R>, config: MmaConfig) -> bool {
        match self {
            TileMatmulKind::Cmma => client.properties().features.matmul.cmma.contains(&config),
            TileMatmulKind::Mma => client.properties().features.matmul.mma.contains(&config),
            TileMatmulKind::Register | TileMatmulKind::PlaneVec | TileMatmulKind::Interleaved => {
                true
            }
        }
    }

    /// Returns all sizes supported for these types, if any.
    pub fn supported_sizes<R: Runtime>(
        &self,
        client: &ComputeClient<R>,
        lhs_ty: StorageType,
        rhs_ty: StorageType,
        acc_ty: StorageType,
    ) -> Vec<TileSize> {
        let iters = match self {
            TileMatmulKind::Cmma => &client.properties().features.matmul.cmma,
            TileMatmulKind::Mma => &client.properties().features.matmul.mma,
            TileMatmulKind::Register | TileMatmulKind::PlaneVec | TileMatmulKind::Interleaved => {
                return Vec::new();
            }
        };

        iters
            .iter()
            .filter(|it| it.a_type == lhs_ty && it.b_type == rhs_ty && it.cd_type == acc_ty)
            .map(|it| (it.m, it.n, it.k).into())
            .collect()
    }

    pub fn validate_blueprint<R: Runtime>(
        &self,
        client: &ComputeClient<R>,
        blueprint: &TilingBlueprint,
        dtypes: &MatmulElems,
        vector_sizes: &MatmulVectorSizes,
    ) -> Result<(), MatmulSetupError> {
        match self {
            TileMatmulKind::Cmma => validate_cmma(client, blueprint, dtypes),
            TileMatmulKind::Mma => validate_mma(client, blueprint, dtypes),
            TileMatmulKind::Register => validate_register(client, blueprint, dtypes, vector_sizes),
            TileMatmulKind::PlaneVec => validate_plane_vec(client, blueprint, dtypes, vector_sizes),
            TileMatmulKind::Interleaved => {
                validate_interleaved(client, blueprint, dtypes, vector_sizes)
            }
        }
    }
}

fn validate_cmma<R: Runtime>(
    client: &ComputeClient<R>,
    blueprint: &TilingBlueprint,
    dtypes: &MatmulElems,
) -> Result<(), MatmulSetupError> {
    let lhs = dtypes.lhs_register;
    let rhs = dtypes.rhs_register;
    let acc = dtypes.acc_register;

    let size = blueprint.tiling_scheme.tile_size;
    if !client
        .properties()
        .features
        .matmul
        .cmma
        .contains(&MmaConfig {
            a_type: lhs,
            b_type: rhs,
            cd_type: acc,
            m: size.m(),
            k: size.k(),
            n: size.n(),
        })
    {
        return Err(MatmulSetupError::Unavailable(
            MatmulAvailabilityError::CmmaInstructionUnavailable {
                lhs,
                rhs,
                output: acc,
                size: Some(TileSize::new(size.m(), size.n(), size.k())),
            },
        ));
    }

    if blueprint.swizzle_modes.has_swizzle() {
        return Err(MatmulSetupError::InvalidConfig(Box::new(
            "This tile matmul doesn't support swizzling",
        )));
    }

    Ok(())
}

fn validate_mma<R: Runtime>(
    client: &ComputeClient<R>,
    blueprint: &TilingBlueprint,
    dtypes: &MatmulElems,
) -> Result<(), MatmulSetupError> {
    let lhs = dtypes.lhs_register;
    let rhs = dtypes.rhs_register;
    let acc = dtypes.acc_register;

    let size = blueprint.tiling_scheme.tile_size;
    if !client
        .properties()
        .features
        .matmul
        .mma
        .contains(&MmaConfig {
            a_type: lhs,
            b_type: rhs,
            cd_type: acc,
            m: size.m(),
            k: size.k(),
            n: size.n(),
        })
    {
        return Err(MatmulSetupError::Unavailable(
            MatmulAvailabilityError::CmmaInstructionUnavailable {
                lhs,
                rhs,
                output: acc,
                size: Some(TileSize::new(size.m(), size.n(), size.k())),
            },
        ));
    }

    Ok(())
}

fn validate_register<R: Runtime>(
    client: &ComputeClient<R>,
    blueprint: &TilingBlueprint,
    dtypes: &MatmulElems,
    vector_sizes: &MatmulVectorSizes,
) -> Result<(), MatmulSetupError> {
    check_types_available(client, dtypes, false)?;

    let m = blueprint.tiling_scheme.tile_size.m();
    let n = blueprint.tiling_scheme.tile_size.n();
    let k = blueprint.tiling_scheme.tile_size.k();

    let lhs = vector_sizes.lhs as u32;
    let rhs = vector_sizes.rhs as u32;
    let out = vector_sizes.out as u32;

    match blueprint.lhs_layout {
        MatrixLayout::RowMajor => {
            if !k.is_multiple_of(lhs) {
                return Err(MatmulSetupError::InvalidConfig(Box::new(format!(
                    "Tile shape in vectorized axis k({k:?}) should be divisible by vector size lhs({lhs:?})"
                ))));
            }
        }
        MatrixLayout::ColMajor => {
            if !m.is_multiple_of(lhs) {
                return Err(MatmulSetupError::InvalidConfig(Box::new(format!(
                    "Tile shape in vectorized axis m({m:?}) should be divisible by vector size lhs({lhs:?})"
                ))));
            }
        }
    }
    match blueprint.rhs_layout {
        MatrixLayout::RowMajor => {
            if !n.is_multiple_of(rhs) {
                return Err(MatmulSetupError::InvalidConfig(Box::new(format!(
                    "Tile shape in vectorized axis n({n:?}) should be divisible by vector size rhs({rhs:?})"
                ))));
            }
        }
        MatrixLayout::ColMajor => {
            if !k.is_multiple_of(rhs) {
                return Err(MatmulSetupError::InvalidConfig(Box::new(format!(
                    "Tile shape in vectorized axis k({k:?}) should be divisible by vector size rhs({rhs:?})"
                ))));
            }
        }
    }

    if !n.is_multiple_of(out) {
        return Err(MatmulSetupError::InvalidConfig(Box::new(format!(
            "Tile shape in vectorized axis n({n:?}) should be divisible by vector size out({out:?})"
        ))));
    }

    Ok(())
}

fn validate_plane_vec<R: Runtime>(
    client: &ComputeClient<R>,
    blueprint: &TilingBlueprint,
    dtypes: &MatmulElems,
    vector_sizes: &MatmulVectorSizes,
) -> Result<(), MatmulSetupError> {
    check_types_available(client, dtypes, true)?;

    if blueprint.lhs_layout != MatrixLayout::RowMajor {
        return Err(MatmulSetupError::InvalidConfig(Box::new(
            "Only Row Major layout is supported for Lhs",
        )));
    }

    if blueprint.rhs_layout != MatrixLayout::ColMajor {
        return Err(MatmulSetupError::InvalidConfig(Box::new(
            "Only Col Major layout is supported for Rhs",
        )));
    }

    let m = blueprint.tiling_scheme.tile_size.m();
    let n = blueprint.tiling_scheme.tile_size.n();
    let k = blueprint.tiling_scheme.tile_size.k();

    let lhs_vector = vector_sizes.lhs as u32;
    let rhs_vector = vector_sizes.rhs as u32;
    let out_vector = vector_sizes.out as u32;

    if m != 1 {
        return Err(MatmulSetupError::InvalidConfig(Box::new(format!(
            "Only m=1 is supported, got m={m:?}",
        ))));
    }

    if lhs_vector != rhs_vector {
        return Err(MatmulSetupError::InvalidConfig(Box::new(format!(
            "Lhs and Rhs must have same vector size, got lhs={lhs_vector:?} and rhs={rhs_vector:?}",
        ))));
    }

    if k != blueprint.plane_dim * lhs_vector {
        return Err(MatmulSetupError::InvalidConfig(Box::new(format!(
            "k must be equal to plane_dim times vector size (of both lhs and rhs), got k={:?}, plane_dim={:?} vector_size={:?}",
            k, blueprint.plane_dim, lhs_vector
        ))));
    }

    if !n.is_multiple_of(out_vector) {
        return Err(MatmulSetupError::InvalidConfig(Box::new(format!(
            "n must be divisible by out vector size, got n={n:?}, out_vector_size={out_vector:?}",
        ))));
    }

    Ok(())
}

fn validate_interleaved<R: Runtime>(
    client: &ComputeClient<R>,
    blueprint: &TilingBlueprint,
    dtypes: &MatmulElems,
    vector_sizes: &MatmulVectorSizes,
) -> Result<(), MatmulSetupError> {
    check_types_available(client, dtypes, false)?;

    let m = blueprint.tiling_scheme.tile_size.m();
    let n = blueprint.tiling_scheme.tile_size.n();
    let k = blueprint.tiling_scheme.tile_size.k();

    let plane_dim = blueprint.plane_dim;
    if !k.is_multiple_of(plane_dim) {
        return Err(MatmulSetupError::InvalidConfig(Box::new(format!(
            "k must be divisible by plane_dim. Got k={:?}, plane_dim={:?}",
            k, plane_dim,
        ))));
    }

    let k_local = k / plane_dim;

    let lhs = vector_sizes.lhs as u32;
    let rhs = vector_sizes.rhs as u32;
    let out = vector_sizes.out as u32;

    match blueprint.lhs_layout {
        MatrixLayout::RowMajor => {
            if !k_local.is_multiple_of(lhs) {
                return Err(MatmulSetupError::InvalidConfig(Box::new(format!(
                    "Local shape in vectorized axis k ({k_local:?}) should be divisible by vector size lhs ({lhs:?})"
                ))));
            }
        }
        MatrixLayout::ColMajor => {
            if !m.is_multiple_of(lhs) {
                return Err(MatmulSetupError::InvalidConfig(Box::new(format!(
                    "Tile shape in vectorized axis m ({m:?}) should be divisible by vector size lhs ({lhs:?})"
                ))));
            }
        }
    }
    match blueprint.rhs_layout {
        MatrixLayout::RowMajor => {
            if !n.is_multiple_of(rhs) {
                return Err(MatmulSetupError::InvalidConfig(Box::new(format!(
                    "Tile shape in vectorized axis n ({n:?}) should be divisible by vector size rhs ({rhs:?})"
                ))));
            }
        }
        MatrixLayout::ColMajor => {
            if !k_local.is_multiple_of(rhs) {
                return Err(MatmulSetupError::InvalidConfig(Box::new(format!(
                    "Local shape in vectorized axis k ({k_local:?}) should be divisible by vector size rhs ({rhs:?})"
                ))));
            }
        }
    }

    if !n.is_multiple_of(out) {
        return Err(MatmulSetupError::InvalidConfig(Box::new(format!(
            "Tile shape in vectorized axis n ({n:?}) should be divisible by vector size out ({out:?})"
        ))));
    }

    Ok(())
}

fn check_types_available<R: Runtime>(
    client: &ComputeClient<R>,
    dtypes: &MatmulElems,
    require_plane_ops: bool,
) -> Result<(), MatmulSetupError> {
    if require_plane_ops
        && !client
            .properties()
            .features
            .plane
            .contains(PlaneFeature::Ops)
    {
        return Err(MatmulSetupError::Unavailable(
            MatmulAvailabilityError::PlaneOpsUnavailable,
        ));
    }

    let lhs = normalize_flex32(dtypes.lhs_register);
    let rhs = normalize_flex32(dtypes.rhs_register);
    let output = normalize_flex32(dtypes.acc_register);

    if !(client
        .properties()
        .features
        .type_usage(lhs)
        .contains(TypeUsage::Arithmetic)
        && client
            .properties()
            .features
            .type_usage(rhs)
            .contains(TypeUsage::Arithmetic)
        && client
            .properties()
            .features
            .type_usage(output)
            .contains(TypeUsage::Arithmetic))
    {
        return Err(MatmulSetupError::Unavailable(
            MatmulAvailabilityError::TypesUnavailable { lhs, rhs, output },
        ));
    }

    Ok(())
}

fn normalize_flex32(ty: StorageType) -> StorageType {
    match ty {
        StorageType::Scalar(ElemType::Float(FloatKind::Flex32)) => {
            ElemType::Float(FloatKind::F32).into()
        }
        _ => ty,
    }
}