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
use crate::support::{HostData, HostDataType, TestInput, ValidationResult};
use crate::tile::uncut;
use cubecl::quant::scheme::{QuantValue, ScaleDtype};
use cubecl::{bytes::Bytes, prelude::*, std::tensor::TensorHandle, zspace::shape};
use cubecl_common::{e2m1, e4m3, ue8m0};
use cubecl_tile::{
    Axis, Launcher, Levels, Partitioning, Projection, Space, TileArg, TileArgLaunch, TileSpec,
    kind::Field,
    launch::Grid,
    layout::{PhysicalAxisMap, split},
    space::{CubeOrder, PlaneOrder},
};

const M: Axis = Axis(0);
const KB: Axis = Axis(1);
const KI: Axis = Axis(2);
/// The blocks of the rows, one: the axis a packed matrix's rows split into beside [`M`], which a
/// source restated over its block axes leaves out.
const MB: Axis = Axis(3);

/// The values packed from `input`, under the block scales they carry, which the copy computes.
#[cube(launch)]
fn quantize_copy(input: &TileArg<'_>, packed: &TileArg<'_>, space: Partitioning) {
    let input = input.tile(&space);
    let mut packed = packed.tile(&space);
    packed.quantize_from(&input);
}

/// Each plane of the launch writing its box of the packed values from the float ones.
#[cube(launch)]
fn quantize_planes(input: &TileArg<'_>, packed: &TileArg<'_>, partitioning: Partitioning) {
    let input = input.tile(&partitioning);
    let packed = packed.tile(&partitioning);
    for plane in &partitioning {
        let mut packed = packed.at(&plane);
        packed.quantize_from(&input.at(&plane));
    }
}

/// How the float values are bound: as the words are, both block axes one dim of the row, or a
/// dim an axis, the rows' one block left out.
#[derive(Clone, Copy)]
enum Source {
    AsTheWords,
    PerAxis,
}

/// Which workers copy: one unit over the whole space, or a plane a row, its units taking the
/// row's scales in turns, `planes` planes a cube, writing `words` words a load.
#[derive(Clone, Copy)]
enum Workers {
    OneUnit,
    PlaneARow { planes: usize, words: usize },
}

/// How the block scales are stored: plain `f32`s, or packed into words as `dtype`, as many to a
/// word as it holds.
#[derive(Clone, Copy)]
enum Scales {
    Plain,
    Packed(ScaleDtype),
}

/// A block's scale as burn calibrates it: the block's largest magnitude over the format's,
/// relative to `global` where there is one, rounded up to the scale's dtype.
fn host_scale(block: &[f32], value: QuantValue, dtype: ScaleDtype, global: Option<f32>) -> f32 {
    let (low, high) = value.range();
    let most = block.iter().fold(0.0f32, |most, v| most.max(v.abs()));
    let wanted = most / ((high - low) / 2.0);
    dtype.round_up(match global {
        Some(global) => wanted / global,
        None => wanted,
    })
}

/// `value` over its effective scale, as the code `format` stores it.
fn host_code(value: f32, format: QuantValue) -> u32 {
    let (low, high) = format.range();
    match format {
        QuantValue::E2M1 => e2m1::from_f32(value.clamp(low, high)).to_bits() as u32,
        QuantValue::E4M3 => e4m3::from_f32(value.clamp(low, high)).to_bits() as u32,
        _ => (value.round().clamp(low, high) as i32 as u32) & ((1 << format.size_bits()) - 1),
    }
}

/// `words` bound as the values they pack: a packed operand is declared in values.
fn in_values(words: &TensorHandle, rows: usize, cols: usize) -> TensorArg {
    let mut binding = words.clone().binding();
    binding.shape = vec![rows, cols].into();
    binding.strides = vec![cols, 1].into();
    binding.into_tensor_arg()
}

/// The low `bits` of a word.
fn mask(bits: usize) -> u32 {
    ((1u64 << bits) - 1) as u32
}

/// The scale a code of a packed word of scales stores.
fn host_scale_value(code: u32, dtype: ScaleDtype) -> f32 {
    match dtype {
        ScaleDtype::UE8M0 => ue8m0::from_bits(code as u8).to_f32(),
        ScaleDtype::UE4M3 => e4m3::from_bits(code as u8).to_f32(),
        ScaleDtype::F32 => f32::from_bits(code),
        ScaleDtype::F16 => half::f16::from_bits(code as u16).to_f32(),
        ScaleDtype::BF16 => half::bf16::from_bits(code as u16).to_f32(),
    }
}

/// Codes `bits` wide packed into words, the first in each word's low bits.
fn host_words(codes: &[u32], bits: usize) -> Vec<u32> {
    codes
        .chunks(32 / bits)
        .map(|word| {
            word.iter()
                .enumerate()
                .fold(0, |acc, (i, &code)| acc | (code << (i * bits)))
        })
        .collect()
}

/// `rows` rows of 160 blocks of `block` values each, quantized to `value` under `scales`, and
/// under a per-tensor `global` where there is one: the words and the scales the copy writes match
/// burn's calibration code for code. An all-zero block, whose scale is zero, is written as zeros.
fn check_quantize(value: QuantValue, block: usize, scales: Scales, global: Option<f32>) {
    check_quantize_as(
        value,
        block,
        scales,
        global,
        Source::AsTheWords,
        Workers::OneUnit,
    );
}

/// [`check_quantize`], the source bound as `source` says and copied by `workers`.
fn check_quantize_as(
    value: QuantValue,
    block: usize,
    scales: Scales,
    global: Option<f32>,
    source: Source,
    workers: Workers,
) {
    let client = cubecl::test_device().client();
    let (rows, blocks) = (4, 160);
    let cols = block * blocks;
    let bits = value.size_bits();
    let per_word = 32 / bits;
    let width = per_word.min(client.properties().hardware.max_vector_size as usize);
    if width < 1 {
        ValidationResult::Skipped("no vectors".into()).enforce();
        return;
    }

    // Values over several magnitudes, signs and a zero block, deterministic.
    let mut seed = 0x2545f491u32;
    let input: Vec<f32> = (0..rows * cols)
        .map(|i| {
            seed = seed.wrapping_mul(1664525).wrapping_add(1013904223);
            let unit = (seed >> 8) as f32 / (1 << 24) as f32 - 0.5;
            match i / block % 7 {
                3 => 0.0,
                band => unit * (1 << band) as f32,
            }
        })
        .collect();
    let (input_t, _) = TestInput::builder(client.clone(), shape![rows, cols])
        .dtype(f32::elem_type_native())
        .custom(input.clone())
        .generate_with_f32_host_data();
    let words = TensorHandle::new_contiguous(
        vec![rows, cols / per_word],
        client.create(Bytes::from_elems(vec![0u32; rows * cols / per_word])),
        u32::elem_type_native(),
    );
    let space = Space::new(&[(MB, 1), (M, rows), (KB, blocks), (KI, block)]);
    let projection = Projection::new(
        &[MB, M, KB, KI],
        &[
            split(&[(MB, 1), (M, rows)]),
            PhysicalAxisMap::disjoint(&[(KB, block), (KI, 1)]),
        ],
    );
    let (scales_t, scales_arg, dtype) = match scales {
        Scales::Plain => {
            let t = TestInput::builder(client.clone(), shape![rows, blocks])
                .dtype(f32::elem_type_native())
                .zeros()
                .generate_without_host_data();
            let arg = TileArgLaunch::new(
                t.clone().binding().into_tensor_arg(),
                f32::elem_type_native(),
                1,
                TileSpec::new(projection.scales_per(KB)),
            );
            (t, arg, ScaleDtype::F32)
        }
        Scales::Packed(dtype) => {
            let per_word = Field::of_scale(dtype).per_word();
            let t = TensorHandle::new_contiguous(
                vec![rows, blocks / per_word],
                client.create(Bytes::from_elems(vec![0u32; rows * blocks / per_word])),
                u32::elem_type_native(),
            );
            let arg = TileArgLaunch::new(
                in_values(&t, rows, blocks),
                u32::elem_type_native(),
                1,
                TileSpec::new(projection.scales_per(KB)).packed(Field::of_scale(dtype)),
            );
            (t, arg, dtype)
        }
    };
    let words_a_load = match workers {
        Workers::OneUnit => 1,
        Workers::PlaneARow { words, .. } => words,
    };
    let values = TileArgLaunch::new(
        in_values(&words, rows, cols),
        u32::elem_type_native(),
        words_a_load,
        TileSpec::new(projection.clone()).packed(Field::Quant(value)),
    )
    .scales(scales_arg);
    let values = match global {
        None => values,
        Some(g) => {
            let t = TestInput::builder(client.clone(), shape![1])
                .dtype(f32::elem_type_native())
                .custom(vec![g])
                .generate_without_host_data();
            let per_tensor = Projection::new(&[MB, M, KB, KI], &[PhysicalAxisMap::broadcast()]);
            values.scales(TileArgLaunch::new(
                t.binding().into_tensor_arg(),
                f32::elem_type_native(),
                1,
                TileSpec::new(per_tensor),
            ))
        }
    };
    let input_arg = match source {
        Source::AsTheWords => TileArgLaunch::new(
            input_t.binding().into_tensor_arg(),
            f32::elem_type_native(),
            width,
            TileSpec::new(projection),
        ),
        Source::PerAxis => {
            let mut binding = input_t.binding();
            binding.shape = vec![rows, blocks, block].into();
            binding.strides = vec![cols, block, 1].into();
            TileArgLaunch::new(
                binding.into_tensor_arg(),
                f32::elem_type_native(),
                width,
                TileSpec::direct(&[M, KB, KI]),
            )
        }
    };
    match workers {
        Workers::OneUnit => quantize_copy::launch(
            &client,
            CubeCount::new_single(),
            CubeDim::new_single(),
            input_arg,
            values,
            uncut(&client, &space, &space).partitioning_arg(),
        ),
        Workers::PlaneARow { planes, .. } => {
            let order = PlaneOrder::new(&[], CubeOrder::RowMajor).cubes_of(planes);
            let levels = Levels::leaf(&[(MB, 1), (M, 1), (KB, blocks), (KI, block)])
                .launch_planes(&[M], order)
                .build();
            let launcher = Launcher::new(
                &client,
                Partitioning::new(space.clone(), levels),
                &space,
                Grid::FromLevels,
            )
            .unwrap();
            quantize_planes::launch(
                &client,
                launcher.cube_count(),
                launcher.cube_dim(),
                input_arg,
                values,
                launcher.partitioning_arg(),
            )
        }
    }

    let got_scales: Vec<f32> = match scales {
        Scales::Plain => {
            let got = HostData::from_tensor_handle(&client, scales_t, HostDataType::F32);
            (0..rows * blocks)
                .map(|i| got.get_f32(&[i / blocks, i % blocks]))
                .collect()
        }
        Scales::Packed(dtype) => {
            let bits = Field::of_scale(dtype).size_bits();
            let got = client.read_one(scales_t.handle).unwrap().to_vec();
            let got: Vec<u32> = bytemuck::cast_slice(&got).to_vec();
            got.iter()
                .flat_map(|&word| (0..32 / bits).map(move |i| (word >> (i * bits)) & mask(bits)))
                .map(|code| host_scale_value(code, dtype))
                .collect()
        }
    };
    let mut want_codes = Vec::new();
    for (i, block_values) in input.chunks(block).enumerate() {
        let want = host_scale(block_values, value, dtype, global);
        let have = got_scales[i];
        // The device's division is the fast one: an `f32` scale, which nothing rounds after it,
        // may land an ulp from the host's.
        let ulps = (have.to_bits() as i64 - want.to_bits() as i64).abs();
        let allowed = match dtype {
            ScaleDtype::F32 => 1,
            _ => 0,
        };
        assert!(
            ulps <= allowed,
            "{value:?} scale {i}: {have} against {want}"
        );
        let effective = have * global.unwrap_or(1.0);
        want_codes.extend(block_values.iter().map(|&v| match effective {
            0.0 => 0,
            _ => host_code(v / effective, value),
        }));
    }
    let got_words = client.read_one(words.handle).unwrap().to_vec();
    let got_words: Vec<u32> = bytemuck::cast_slice(&got_words).to_vec();
    assert_eq!(got_words, host_words(&want_codes, bits), "{value:?} values");
}

/// `q8` under one `f32` scale a block of sixteen: the integer formats round to the nearest
/// whole number and clamp.
#[test]
fn q8_quantizes_as_burn_calibrates_it() {
    check_quantize(QuantValue::Q8S, 16, Scales::Plain, None);
}

/// `q8` under `f32` scales bound as words, as a packed tensor's scales are.
#[test]
fn q8_quantizes_under_scales_in_words() {
    check_quantize(QuantValue::Q8S, 16, Scales::Packed(ScaleDtype::F32), None);
}

/// `q4` under `f16` scales, two to a word: each rounded up onto the `f16` grid.
#[test]
fn q4_quantizes_under_half_scales() {
    check_quantize(QuantValue::Q4S, 32, Scales::Packed(ScaleDtype::F16), None);
}

/// `q8` under `bf16` scales, two to a word.
#[test]
fn q8_quantizes_under_bf16_scales() {
    check_quantize(QuantValue::Q8S, 32, Scales::Packed(ScaleDtype::BF16), None);
}

/// MXFP8 from a source bound a dim an axis, as a tensor restated over the block axes is.
#[test]
fn mxfp8_quantizes_from_a_source_per_axis() {
    check_quantize_as(
        QuantValue::E4M3,
        32,
        Scales::Packed(ScaleDtype::UE8M0),
        None,
        Source::PerAxis,
        Workers::OneUnit,
    );
}

/// NVFP4 copied a plane a row, its units taking the row's words of scales in turns, and the
/// planes of one cube each copying their own row, a word or a block's two words a load.
#[test]
fn nvfp4_quantizes_a_plane_a_row() {
    for (planes, words) in [(1, 1), (4, 1), (4, 2)] {
        check_quantize_as(
            QuantValue::E2M1,
            16,
            Scales::Packed(ScaleDtype::UE4M3),
            Some(0.01),
            Source::PerAxis,
            Workers::PlaneARow { planes, words },
        );
    }
}

/// MXFP8 copied a plane a row, four words a load.
#[test]
fn mxfp8_quantizes_four_words_a_load() {
    check_quantize_as(
        QuantValue::E4M3,
        32,
        Scales::Packed(ScaleDtype::UE8M0),
        None,
        Source::PerAxis,
        Workers::PlaneARow {
            planes: 2,
            words: 4,
        },
    );
}

/// MXFP8: `e4m3` values under `ue8m0` scales, four to a word, a block of thirty-two.
#[test]
fn mxfp8_quantizes_as_burn_calibrates_it() {
    check_quantize(
        QuantValue::E4M3,
        32,
        Scales::Packed(ScaleDtype::UE8M0),
        None,
    );
}

/// MXFP4: `e2m1` values, eight to a word, under `ue8m0` scales.
#[test]
fn mxfp4_quantizes_as_burn_calibrates_it() {
    check_quantize(
        QuantValue::E2M1,
        32,
        Scales::Packed(ScaleDtype::UE8M0),
        None,
    );
}

/// NVFP4: `e2m1` values under `ue4m3` scales of sixteen, relative to the per-tensor scale the
/// caller wrote.
#[test]
fn nvfp4_quantizes_under_its_per_tensor_scale() {
    check_quantize(
        QuantValue::E2M1,
        16,
        Scales::Packed(ScaleDtype::UE4M3),
        Some(0.01),
    );
}