cubecl-tile 0.12.0-pre.1

CubeCL Tile: a space cut into tiles, and the operations on them.
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
//! The tensor-core encoding of a plane tile ([`CmmaData`]) and its fragment↔memory transports.

use cubecl::{
    cmma::{self, Matrix, MatrixIdent, MatrixLayout},
    ir::{ElemType, FloatKind},
    prelude::*,
};

use crate::*;

/// A tensor-core fragment plus the comptime config its load/store paths dispatch on.
/// `Clone` duplicates the handle, not the fragment.
#[derive(CubeType, Clone)]
#[expand(derive(Clone))]
pub(crate) struct CmmaData {
    /// The fragment, kept at [`Held`] and named at the element it holds where it is used.
    pub fragment: Matrix<Held>,
    #[cube(comptime)]
    pub ident: MatrixIdent,
    #[cube(comptime)]
    pub layout: MatrixLayout,
    /// The whole MMA tile's `(m, n)`, whatever the role.
    #[cube(comptime)]
    pub shape: (usize, usize),
    /// This plane's one-tile window of shared memory the fragment bounces through, at the
    /// fragment's element.
    pub scratch: ComptimeOption<Raw<SharedSlice>>,
}

#[cube]
impl CmmaData {
    /// Allocate an uninitialized fragment. `m`/`n`/`k` are the whole MMA tile, whatever the role.
    pub(crate) fn alloc<T: Numeric>(
        #[comptime] ident: MatrixIdent,
        #[comptime] m: usize,
        #[comptime] n: usize,
        #[comptime] k: usize,
        #[comptime] layout: MatrixLayout,
    ) -> CmmaData {
        let matrix = unsafe { Matrix::<T>::uninitialized(ident, m, n, k, layout) };
        CmmaData {
            fragment: unsafe { matrix.downcast_unchecked::<Held>() },
            ident,
            layout,
            shape: comptime!((m, n)),
            scratch: ComptimeOption::new_None(),
        }
    }

    /// This fragment carrying `scratch`, one tile of shared memory for its plane.
    pub(crate) fn with_scratch<T: Numeric>(self, scratch: Shared<[T]>) -> CmmaData {
        CmmaData {
            fragment: self.fragment,
            ident: comptime!(self.ident),
            layout: comptime!(self.layout),
            shape: comptime!(self.shape),
            scratch: ComptimeOption::new_Some(Raw::<SharedSlice>::of::<T>(scratch)),
        }
    }

    /// Store this tile row-major into `scratch`, one tile's cells from its start.
    pub(crate) fn store_scratch<T: Numeric>(&self, scratch: &Shared<[T]>) {
        let n = comptime!(self.shape.1 as u32);
        let mut window = scratch.clone();
        cmma::store(
            &mut window,
            &self.fragment.downcast::<T>(),
            n,
            MatrixLayout::RowMajor,
        )
    }

    /// Load this tile back from `scratch`.
    pub(crate) fn load_scratch<T: Numeric>(&mut self, scratch: &Shared<[T]>) {
        let n = comptime!(self.shape.1 as u32);
        cmma::load_with_layout(
            &mut self.fragment.downcast::<T>(),
            scratch,
            n,
            MatrixLayout::RowMajor,
        )
    }

    /// Zero the fragment.
    pub(crate) fn zero<T: Numeric>(&mut self) {
        cmma::fill(&mut self.fragment.downcast::<T>(), T::from_int(0));
    }

    /// Fill this fragment from `mem`'s window, stepping rows by the physical row stride.
    pub(crate) fn load_window<T: Numeric>(&mut self, mem: &Memory, #[comptime] row: usize) {
        let element = mem.stage_element();
        comptime!(assert!(
            element.holds_values(),
            "CmmaData::load_window: a cmma fragment loads at one element type, so it cannot \
             unpack a packed source as it reads; contract it, which lands it, or decode it into a \
             stage (`stage.copy_from(&w.mul(&scales))`)"
        ));
        let stride = mem.row_stride_at(row);
        match comptime!(self.ident) {
            MatrixIdent::Accumulator => cmma::load_with_layout(
                &mut self.fragment.downcast::<T>(),
                mem.window_slice::<T>(),
                stride,
                comptime!(self.layout),
            ),
            _ => cmma::load(
                &mut self.fragment.downcast::<T>(),
                mem.window_slice::<T>(),
                stride,
            ),
        }
    }

    /// Drain this fragment into `mem`'s *window* (origin offset, physical row stride).
    pub(crate) fn store_window<T: Numeric>(&self, mem: &mut Memory, #[comptime] row: usize) {
        let stride = mem.row_stride_at(row);
        cmma::store(
            mem.window_slice_mut::<T>(),
            &self.fragment.downcast::<T>(),
            stride,
            comptime!(self.layout),
        )
    }

    /// Every cell multiplied by `factor`, bounced through the fragment's slot of the plane's
    /// scratch as [`mul_along`](Self::mul_along) bounces it: a cmma fragment is not read cell by
    /// cell.
    pub(crate) fn scale<T: Numeric>(&self, factor: T) {
        self.spill_to_scratch::<T>();
        sync_plane();
        let mut scratch = self.scratch_slot::<T>();
        let (m, n) = self.shape;
        let mut cell = UNIT_POS_PLANE as usize;
        while cell < m * n {
            scratch[cell] *= factor;
            cell += PLANE_DIM as usize;
        }
        sync_plane();
        self.reload_from_scratch::<T>();
        sync_plane();
    }

    /// `self[i, :] *= factors[first + i]` for every slice `i` along the fragment's columns,
    /// bounced through its slot of the plane's scratch: spilled, scaled by the plane's units in
    /// turns, and reloaded, each step met on `sync_plane`. The slot is the plane's own, so no
    /// other plane waits; every unit of the plane calls it.
    pub(crate) fn mul_along<T: Numeric>(&self, factors: &Array<T>, #[comptime] first: usize) {
        self.spill_to_scratch::<T>();
        sync_plane();
        self.mul_spilled(factors, first);
        sync_plane();
        self.reload_from_scratch::<T>();
        sync_plane();
    }

    /// `slot[i, :] *= factors[first + i]` over this fragment's spilled cells, the plane's units
    /// taking them in turns. The caller owns the barriers.
    pub(crate) fn mul_spilled<T: Numeric>(&self, factors: &Array<T>, #[comptime] first: usize) {
        let mut scratch = self.scratch_slot::<T>();
        let (m, n) = self.shape;
        let mut cell = UNIT_POS_PLANE as usize;
        while cell < m * n {
            scratch[cell] *= factors[first + cell / n];
            cell += PLANE_DIM as usize;
        }
    }

    /// Load this fragment back from its slot of the plane's scratch. The caller owns the barriers.
    pub(crate) fn reload_from_scratch<T: Numeric>(&self) {
        let scratch = self.scratch_slot::<T>();
        let mut fragment = self.clone();
        fragment.load_scratch(&scratch);
    }

    /// Store this fragment into its slot of the plane's scratch. The caller owns the barriers.
    pub(crate) fn spill_to_scratch<T: Numeric>(&self) {
        self.store_scratch(&self.scratch_slot::<T>());
    }

    /// Write this fragment's spilled cells into `mem` through the store's own write.
    pub(crate) fn add_from_scratch<T: Numeric, Out: Numeric>(
        &self,
        mem: &mut Memory,
        #[comptime] space: Space,
    ) {
        let scratch = self.scratch_slot::<T>();
        let (m, n) = comptime!(self.shape);
        let width = comptime!(mem.store.vector_size);
        comptime!(assert!(
            n.is_multiple_of(width),
            "CmmaData::add_from_scratch: the store's lines ({width}) do not divide the \
             fragment's columns ({n})"
        ));
        let size!(W) = width;
        let lines_per_row = comptime!(n / width);
        let lines = comptime!(m * lines_per_row);
        let axes = comptime!(MatrixAxes::trailing(&space));
        let mut sink = mem.matrix_mut::<Out, W>(0usize, axes, space);
        let plane_units = PLANE_DIM as usize;
        let mut line = UNIT_POS_PLANE as usize;
        while line < lines {
            let mut value = Vector::<Out, W>::empty();
            #[unroll]
            for e in 0..width {
                value.insert(e, Out::cast_from(scratch[line * width + e]));
            }
            sink.write(
                ((line / lines_per_row) as u32, (line % lines_per_row) as u32),
                value,
            );
            line += plane_units;
        }
    }

    /// Drain this fragment through the plane's scratch, with cube-wide syncs.
    pub(crate) fn bounce_cast_window<T: Numeric, Out: Numeric>(
        &self,
        mem: &mut Memory,
        #[comptime] space: Space,
    ) {
        sync_cube();
        self.spill_to_scratch::<T>();
        sync_cube();
        self.add_from_scratch::<T, Out>(mem, space);
        sync_cube();
    }

    /// This fragment's slot of the plane's scratch, or the reason there is none.
    fn scratch_slot<T: Numeric>(&self) -> Shared<[T]> {
        #[comptime]
        match &self.scratch {
            ComptimeOption::Some(scratch) => scratch.at::<T>().clone(),
            ComptimeOption::None => panic!(
                "CmmaData: a fragment bounces through a scratch, to scale its slices, to reach a \
                 store that sums, or a window the problem's edge cuts short; open the \
                 accumulator with `with_scratch`"
            ),
        }
    }

    /// Drain this fragment into `mem`'s window, casting `T` down to the sink's element type.
    pub(crate) fn store_cast_window<T: Numeric, Out: Numeric>(
        &self,
        mem: &mut Memory,
        #[comptime] row: usize,
    ) {
        let stride = mem.row_stride_at(row);
        let casted: Matrix<Out> = cmma::cast(&self.fragment.downcast::<T>());
        cmma::store(
            mem.window_slice_mut::<Out>(),
            &casted,
            stride,
            comptime!(self.layout),
        )
    }
}

/// Whether a fragment of `T` cast to `Out` is a fragment the device holds at `(m, n)`: the same
/// element, or one an accumulator is offered in at that shape. `wmma` offers none in `bf16`, so a
/// fragment cast there is a kernel that fails to compile, and a `bf16` sink takes the cells through
/// the scratch instead, each cast on its write. A device whose properties are not known at
/// expansion is taken at its word.
// `T` and `Out` are read by the expansion, which is where a type has its element.
#[allow(clippy::extra_unused_type_parameters)]
#[cube]
pub(crate) fn casts_in_place<T: Numeric, Out: Numeric>(
    #[comptime] m: usize,
    #[comptime] n: usize,
) -> comptime_type!(bool) {
    intrinsic!(|scope| {
        let (from, to) = (T::elem_type(scope), Out::elem_type(scope));
        from == to
            || scope
                .state()
                .device_properties
                .as_ref()
                .is_none_or(|properties| {
                    cmma_cast_layout_is_compatible(&properties.identity.fingerprint, from, to)
                        && properties.features.matmul.cmma.iter().any(|offered| {
                            offered.cd_type == to
                                && offered.m as usize == m
                                && offered.n as usize == n
                        })
                })
    })
}

/// Volta uses different register layouts for f32 and f16 accumulator fragments, so cross-type
/// casts must bounce through shared memory.
fn cmma_cast_layout_is_compatible(fingerprint: &str, from: ElemType, to: ElemType) -> bool {
    let f32 = ElemType::Float(FloatKind::F32);
    let f16 = ElemType::Float(FloatKind::F16);
    !(fingerprint == "ptx_sm70" && ((from == f32 && to == f16) || (from == f16 && to == f32)))
}

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

    #[test]
    fn volta_bounces_f32_f16_accumulator_casts() {
        let (f32, f16) = (
            ElemType::Float(FloatKind::F32),
            ElemType::Float(FloatKind::F16),
        );
        assert!(!cmma_cast_layout_is_compatible("ptx_sm70", f32, f16));
        assert!(!cmma_cast_layout_is_compatible("ptx_sm70", f16, f32));
    }

    #[test]
    fn later_architectures_keep_the_in_place_cast_path() {
        let (f32, f16) = (
            ElemType::Float(FloatKind::F32),
            ElemType::Float(FloatKind::F16),
        );
        assert!(cmma_cast_layout_is_compatible("ptx_sm75", f32, f16));
        assert!(cmma_cast_layout_is_compatible("ptx_sm80", f32, f16));
    }

    #[test]
    fn same_type_cast_does_not_depend_on_architecture() {
        let f16 = ElemType::Float(FloatKind::F16);
        assert!(cmma_cast_layout_is_compatible("ptx_sm70", f16, f16));
    }
}

/// How a cmma fragment reaches its destination.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub(crate) enum FragmentDrain {
    /// The fragment's store intrinsic: a replacing destination, window wholly inside it.
    Intrinsic,
    /// Through the plane's scratch: a destination that adds, or a window cut short by the edge.
    Bounce,
}

impl FragmentDrain {
    /// How a fragment drains into a destination written as `access` says, `addressed` when its
    /// values sit at an address the intrinsic can store to, `in_place` when the fragment cast to
    /// the destination's element is one the device holds ([`casts_in_place`]).
    pub(crate) const fn of(access: &Access, addressed: bool, in_place: bool) -> Self {
        match (access.write, access.overhang, addressed && in_place) {
            (Write::Replace, Overhang::Never | Overhang::Fits, true) => FragmentDrain::Intrinsic,
            (Write::Replace, _, _) | (Write::Accumulate | Write::Exclusive(_), _, _) => {
                FragmentDrain::Bounce
            }
        }
    }
}

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

    fn access(write: Write, overhang: Overhang) -> Access {
        Access {
            whole: false,
            overhang,
            write,
            fill: FillUnits::cube(0),
            storage: WindowStorage::Stored(Storage::Strided),
            delivery: Delivery::SyncPerUnit,
        }
    }

    #[test]
    fn a_replacing_window_inside_its_buffer_stores_through_the_intrinsic() {
        for overhang in [Overhang::Never, Overhang::Fits] {
            let drain = FragmentDrain::of(&access(Write::Replace, overhang), true, true);
            assert_eq!(drain, FragmentDrain::Intrinsic);
        }
    }

    #[test]
    fn an_overhanging_or_summing_window_bounces() {
        let masked = FragmentDrain::of(&access(Write::Replace, Overhang::Masked), true, true);
        assert_eq!(masked, FragmentDrain::Bounce);
        for overhang in [Overhang::Never, Overhang::Fits, Overhang::Masked] {
            for write in [Write::Accumulate, Write::Exclusive(Schedule::Sequential)] {
                assert_eq!(
                    FragmentDrain::of(&access(write, overhang), true, true),
                    FragmentDrain::Bounce,
                    "{write:?} {overhang:?}"
                );
            }
        }
    }

    /// A fragment cast to an element the device holds no fragment of cannot be stored whole, so
    /// even a replacing window wholly inside its buffer bounces, its cells cast on their write.
    #[test]
    fn a_fragment_with_no_cast_in_place_bounces() {
        for overhang in [Overhang::Never, Overhang::Fits] {
            let drain = FragmentDrain::of(&access(Write::Replace, overhang), true, false);
            assert_eq!(drain, FragmentDrain::Bounce);
        }
    }

    /// A write call has no address for the intrinsic to store to, so even a replacing window
    /// wholly inside it bounces.
    #[test]
    fn a_window_with_no_address_bounces() {
        for overhang in [Overhang::Never, Overhang::Fits] {
            let drain = FragmentDrain::of(&access(Write::Replace, overhang), false, true);
            assert_eq!(drain, FragmentDrain::Bounce);
        }
    }
}

#[cfg(test)]
mod held_fragment_tests {
    use cubecl::ir::{AddressType, Scope, settings};
    use half::f16;

    use super::*;

    fn test_scope() -> Scope {
        Scope::root(settings::KernelSettings::new(
            settings::Dim3::new_single(),
            settings::ExecutionMode::Checked,
            AddressType::U32,
        ))
    }

    /// A `16 × 16` accumulator fragment allocated at `H`, kept at [`Held`], and stored into a
    /// scratch of `N`.
    #[cube]
    fn fragment_stored_at<H: Numeric, N: Numeric>() {
        let data = CmmaData::alloc::<H>(
            MatrixIdent::Accumulator,
            16usize,
            16usize,
            16usize,
            MatrixLayout::Undefined,
        );
        let scratch = Shared::<[N]>::new_slice(256usize);
        data.store_scratch::<N>(&scratch);
    }

    /// A fragment kept at [`Held`] is named again at the element it was allocated at.
    #[test]
    fn a_held_fragment_is_named_at_its_element() {
        fragment_stored_at::expand::<f16, f16>(&test_scope());
    }

    /// A fragment kept at [`Held`] and named at an element it does not hold is refused at
    /// expansion.
    ///
    /// The tile keeps every fragment at one dummy type, so nothing in Rust ties a fragment to the
    /// element it was allocated at: only this check stops an `f16` fragment being stored as an
    /// `f32` one.
    #[test]
    #[should_panic(expected = "Matrix::downcast: the fragment holds")]
    fn a_held_fragment_refuses_another_element() {
        fragment_stored_at::expand::<f16, f32>(&test_scope());
    }
}