cubek-test-utils 0.3.0

CubeK: Test Utils
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
use cubecl::quant::scheme::QuantMode;
use cubecl_common::{
    e2m1, e4m3, e5m2,
    quant::scheme::{QuantScheme, QuantStore, QuantValue},
};

pub fn quantize(
    values: &[f32],
    shape: &[usize],
    scales: &[f32],
    block_dims: &[usize],
    scheme: &QuantScheme,
) -> Vec<u8> {
    let scales_shape = crate::quant_layout::scales_grid(shape, block_dims);
    let quantized = quantized_values(values, shape, scales, block_dims, &scales_shape, scheme);

    match scheme.store {
        QuantStore::Native => encode_native(&quantized, scheme.value),
        QuantStore::PackedU32(_) => encode_packed_u32(&quantized, scheme),
        other => panic!("quantize stub: unsupported store {other:?}"),
    }
}

#[allow(dead_code)]
pub fn dequantize(
    bytes: &[u8],
    shape: &[usize],
    scales: &[f32],
    block_dims: &[usize],
    scheme: &QuantScheme,
) -> Vec<f32> {
    let scales_shape = crate::quant_layout::scales_grid(shape, block_dims);

    let quants = match scheme.store {
        QuantStore::Native => decode_native(bytes, scheme.value),
        QuantStore::PackedU32(_) => decode_packed_u32(bytes, scheme),
        other => panic!("dequantize stub: unsupported store {other:?}"),
    };

    dequantized_values(&quants, scales, shape, block_dims, &scales_shape, scheme)
}

fn quantized_values(
    values: &[f32],
    shape: &[usize],
    scales: &[f32],
    block_dims: &[usize],
    scales_shape: &[usize],
    scheme: &QuantScheme,
) -> Vec<f32> {
    let (range_min, range_max) = scheme.value.range();

    match scheme.mode {
        QuantMode::Symmetric => values
            .iter()
            .enumerate()
            .map(|(i, &v)| {
                let scale = scales[scale_index(i, shape, block_dims, scales_shape)];
                let scaled = v / scale;
                let snapped = if rounds_to_integers(scheme.value) {
                    scaled.round()
                } else {
                    scaled
                };
                snapped.clamp(range_min, range_max)
            })
            .collect(),
        // A lookup field is a table index, not a rounded value; these stubs carry no table, so
        // a lookup test states its indices exactly ([`pack_q_values`]) and folds the table into
        // its own host reference.
        QuantMode::Lookup => panic!("quantize stub: lookup takes exact indices via pack_q_values"),
    }
}

/// Whether the format's representable values are the integers in its range, which is what makes
/// rounding to a whole number here the right rounding for it.
///
/// Mirrors `quantize_symmetric` in `cubek-quant`: a minifloat's grid is not the integers — `e2m1`
/// holds `0.5` and `1.5` — so rounding one here would quantize it twice, and this reference would
/// then demand of the kernel exactly the degradation the format exists to avoid. Those formats
/// round where they are encoded, against the codes they actually have.
fn rounds_to_integers(quant: QuantValue) -> bool {
    match quant {
        QuantValue::Q8F
        | QuantValue::Q8S
        | QuantValue::Q4F
        | QuantValue::Q4S
        | QuantValue::Q2F
        | QuantValue::Q2S => true,
        QuantValue::E5M2 | QuantValue::E4M3 | QuantValue::E2M1 => false,
    }
}

#[allow(dead_code)]
fn dequantized_values(
    quantized: &[f32],
    scales: &[f32],
    shape: &[usize],
    block_dims: &[usize],
    scales_shape: &[usize],
    scheme: &QuantScheme,
) -> Vec<f32> {
    match scheme.mode {
        QuantMode::Symmetric => quantized
            .iter()
            .enumerate()
            .map(|(i, &q)| q * scales[scale_index(i, shape, block_dims, scales_shape)])
            .collect(),
        QuantMode::Lookup => panic!(
            "dequantize stub: lookup carries no table here; fold table[index] * scale into the \
             test's own host reference"
        ),
    }
}

/// 1:1 native storage: `i8` for `Q8*`, fp8 bits for `E4M3`/`E5M2`.
fn encode_native(quantized: &[f32], value: QuantValue) -> Vec<u8> {
    match value {
        QuantValue::Q8F | QuantValue::Q8S => quantized.iter().map(|&q| q as i8 as u8).collect(),
        QuantValue::E4M3 => quantized
            .iter()
            .map(|&q| e4m3::from_f32(q).to_bits())
            .collect(),
        QuantValue::E5M2 => quantized
            .iter()
            .map(|&q| e5m2::from_f32(q).to_bits())
            .collect(),
        other => panic!("quantize stub: {other:?} is not supported for native storage"),
    }
}

#[allow(dead_code)]
fn decode_native(bytes: &[u8], value: QuantValue) -> Vec<f32> {
    match value {
        QuantValue::Q8F | QuantValue::Q8S => bytes.iter().map(|&b| b as i8 as f32).collect(),
        QuantValue::E4M3 => bytes.iter().map(|&b| e4m3::from_bits(b).to_f32()).collect(),
        QuantValue::E5M2 => bytes.iter().map(|&b| e5m2::from_bits(b).to_f32()).collect(),
        other => panic!("dequantize stub: {other:?} is not supported for native storage"),
    }
}

/// [`encode_packed_u32`] over exact fields, as `u32` words: for tests that need bit-exact control
/// of the packed payload rather than the quantize stub's rounding.
///
/// The fields are taken as given, so under a minifloat scheme they are that format's *codes*, not
/// the magnitudes they stand for.
pub fn pack_q_values(q: &[i32], scheme: &QuantScheme) -> Vec<u32> {
    let size_quant = scheme.size_bits_value();
    let num_quants = scheme.num_quants();
    let mask = quant_mask(size_quant);
    assert!(
        q.len().is_multiple_of(num_quants),
        "pack_q_values: {} values do not fill whole {num_quants}-value words",
        q.len()
    );
    q.chunks(num_quants)
        .map(|chunk| {
            chunk.iter().enumerate().fold(0u32, |acc, (p, &v)| {
                acc | ((v as u32 & mask) << (p * size_quant))
            })
        })
        .collect()
}

/// Pack `num_quants` consecutive quants (along the innermost dimension) into
/// each `u32`, low bits first, matching `pack_q`.
fn encode_packed_u32(quantized: &[f32], scheme: &QuantScheme) -> Vec<u8> {
    let size_quant = scheme.size_bits_value();
    let num_quants = scheme.num_quants();
    let mask = quant_mask(size_quant);

    let packed: Vec<u32> = quantized
        .chunks(num_quants)
        .map(|chunk| {
            chunk.iter().enumerate().fold(0u32, |acc, (p, &q)| {
                let bits = field_code(q, scheme.value) & mask;
                acc | (bits << (p * size_quant))
            })
        })
        .collect();

    bytemuck::cast_slice(&packed).to_vec()
}

/// The field `q` occupies: the format's *code* for it, which is the value itself only for the
/// integer formats. Mirrors `pack_q`/`encode_minifloat` in `cubek-quant`.
///
/// A minifloat encoded by casting would leave the reference reconstructing on the integer grid —
/// `e2m1` losing `0.5` and `1.5`, `e4m3` wrapping its codes above ±127 — and a kernel that made
/// the same mistake would then match it.
fn field_code(q: f32, value: QuantValue) -> u32 {
    match value {
        QuantValue::E2M1 => e2m1::from_f32(q).to_bits() as u32,
        QuantValue::E4M3 => e4m3::from_f32(q).to_bits() as u32,
        QuantValue::E5M2 => e5m2::from_f32(q).to_bits() as u32,
        QuantValue::Q8F
        | QuantValue::Q8S
        | QuantValue::Q4F
        | QuantValue::Q4S
        | QuantValue::Q2F
        | QuantValue::Q2S => q as i32 as u32,
    }
}

/// Inverse of [`encode_packed_u32`]: unpack each `u32` into `num_quants` quant values, matching
/// `unpack_q`.
#[allow(dead_code)]
fn decode_packed_u32(bytes: &[u8], scheme: &QuantScheme) -> Vec<f32> {
    let size_quant = scheme.size_bits_value();
    let num_quants = scheme.num_quants();
    let mask = quant_mask(size_quant);

    let mut out = Vec::with_capacity((bytes.len() / 4) * num_quants);
    for &word in bytemuck::cast_slice::<u8, u32>(bytes) {
        for p in 0..num_quants {
            let raw = (word >> (p * size_quant)) & mask;
            out.push(code_value(raw, size_quant, scheme.value));
        }
    }
    out
}

/// The value a field stands for: the inverse of [`field_code`]. A minifloat code is decoded by its
/// format, and only an integer format's field is a two's complement number.
fn code_value(raw: u32, size_quant: usize, value: QuantValue) -> f32 {
    match value {
        QuantValue::E2M1 => e2m1::from_bits(raw as u8).to_f32(),
        QuantValue::E4M3 => e4m3::from_bits(raw as u8).to_f32(),
        QuantValue::E5M2 => e5m2::from_bits(raw as u8).to_f32(),
        QuantValue::Q8F
        | QuantValue::Q8S
        | QuantValue::Q4F
        | QuantValue::Q4S
        | QuantValue::Q2F
        | QuantValue::Q2S => {
            let mask = quant_mask(size_quant);
            let sign_bit = 1u32 << (size_quant - 1);
            // Two's-complement sign extension.
            let q = if raw & sign_bit != 0 {
                (raw | !mask) as i32
            } else {
                raw as i32
            };
            q as f32
        }
    }
}

fn quant_mask(size_quant: usize) -> u32 {
    if size_quant >= 32 {
        u32::MAX
    } else {
        (1u32 << size_quant) - 1
    }
}

/// Refuse a scheme this reference cannot stand in for.
///
/// It quantizes with one scale per value, so a two-level scheme would give a reference that
/// ignores the outer factor. A kernel that drops it too would then agree with the reference, and
/// the test would pass with both of them wrong.
pub(crate) fn assert_supported(scheme: &QuantScheme) {
    assert!(
        scheme.num_levels() <= 1,
        "two-level quantization is not supported by the reference quantizer, got {scheme:?}"
    );
}

/// The scheme's per-axis block edges over `shape`: per-tensor is one block spanning it all.
pub(crate) fn block_dims(scheme: &QuantScheme, shape: &[usize]) -> Vec<usize> {
    assert_supported(scheme);

    crate::quant_layout::block_dims(scheme, shape)
}

/// Map a logical (row-major) element index to the index of its block in the
/// row-major scales grid.
fn scale_index(
    linear: usize,
    shape: &[usize],
    block_dims: &[usize],
    scales_shape: &[usize],
) -> usize {
    let rank = shape.len();
    let mut rem = linear;
    let mut coord = vec![0usize; rank];
    for d in (0..rank).rev() {
        coord[d] = rem % shape[d];
        rem /= shape[d];
    }

    let mut block_linear = 0;
    for d in 0..rank {
        block_linear = block_linear * scales_shape[d] + coord[d] / block_dims[d];
    }
    block_linear
}

#[cfg(test)]
mod tests {
    use super::*;
    use cubecl_common::quant::scheme::{QuantMode, QuantStore, QuantValue, ScaleDtype};

    fn scheme(value: QuantValue, store: QuantStore) -> QuantScheme {
        QuantScheme::default()
            .with_mode(QuantMode::Symmetric)
            .with_value(value)
            .with_store(store)
    }

    /// Quantize then dequantize and assert the round-trip stays within one
    /// quantization step (`scale`), the worst-case symmetric error.
    fn assert_round_trips(value: QuantValue, store: QuantStore, scale: f32) {
        let s = scheme(value, store).per_tensor(ScaleDtype::F32);
        let n = 64; // multiple of every num_quants we test (≤16)
        let values: Vec<f32> = (0..n).map(|i| (i as f32 / n as f32) * 2.0 - 1.0).collect();

        let bytes = quantize(&values, &[n], &[scale], &[n], &s);
        let restored = dequantize(&bytes, &[n], &[scale], &[n], &s);

        let max_err = scale + 1e-6;
        for (got, want) in restored.iter().zip(&values) {
            assert!(
                (got - want).abs() <= max_err,
                "{value:?}/{store:?}: dequant {got} too far from {want}",
            );
        }
    }

    #[test]
    fn native_q8_is_one_to_one_i8() {
        let values = vec![-1.0, -0.5, 0.0, 0.5, 1.0];
        let scale = 1.0 / 127.0;
        let bytes = quantize(
            &values,
            &[values.len()],
            &[scale],
            &[values.len()],
            &scheme(QuantValue::Q8S, QuantStore::Native).per_tensor(ScaleDtype::F32),
        );

        let got: Vec<i8> = bytes.iter().map(|&b| b as i8).collect();
        assert_eq!(got, vec![-127, -64, 0, 64, 127]);
    }

    #[test]
    fn round_trips_for_integer_schemes() {
        assert_round_trips(QuantValue::Q8S, QuantStore::Native, 1.0 / 127.0);
        assert_round_trips(QuantValue::Q8S, QuantStore::PackedU32(0), 1.0 / 127.0);
        assert_round_trips(QuantValue::Q4S, QuantStore::PackedU32(0), 1.0 / 7.0);
        assert_round_trips(QuantValue::Q2S, QuantStore::PackedU32(0), 1.0 / 1.0);
    }

    #[test]
    fn round_trips_for_fp8_native() {
        // Like the kernel, fp8 quantization scales and then encodes, with no rounding of its own:
        // the only error the round trip carries is the format's own representation error.
        assert_round_trips(QuantValue::E4M3, QuantStore::Native, 1.0 / 8.0);
        assert_round_trips(QuantValue::E5M2, QuantStore::Native, 1.0 / 4.0);
    }

    /// Quantize then dequantize `values` at a scale of one, so what comes back is the codec's
    /// answer and nothing else.
    fn round_trip_at_unit_scale(value: QuantValue, store: QuantStore, values: &[f32]) -> Vec<f32> {
        let s = scheme(value, store).per_tensor(ScaleDtype::F32);
        let n = values.len();
        let bytes = quantize(values, &[n], &[1.0], &[n], &s);

        dequantize(&bytes, &[n], &[1.0], &[n], &s)
    }

    /// A packed minifloat field is the format's code, so every value the format holds comes back as
    /// itself — the fractions included. The integer codec this replaced returned `1.0` for `0.5`
    /// and `2.0` for `1.5`, having rounded both before they ever reached the format.
    #[test]
    fn packed_e2m1_holds_the_format_grid() {
        let values = [0.0f32, 0.5, 1.0, 1.5, 2.0, 3.0, -0.5, -6.0];
        let restored =
            round_trip_at_unit_scale(QuantValue::E2M1, QuantStore::PackedU32(0), &values);

        assert_eq!(
            restored, values,
            "an e2m1 code point did not come back as itself"
        );
    }

    /// The same for `e4m3`, whose codes reach ±448 while a byte read as two's complement stops at
    /// ±127: a field cast from the value rather than encoded wraps rather than merely losing
    /// precision.
    #[test]
    fn packed_e4m3_holds_fractions_and_large_magnitudes() {
        let values = [0.5f32, 1.5, -0.5, 0.0625, 288.0, -288.0, 256.0, 144.0];
        let restored =
            round_trip_at_unit_scale(QuantValue::E4M3, QuantStore::PackedU32(0), &values);

        assert_eq!(
            restored, values,
            "an e4m3 code point did not come back as itself"
        );
    }

    #[test]
    fn block_level_uses_per_block_scale() {
        // Two blocks of 4 along the last dim, each with its own scale.
        let s = scheme(QuantValue::Q8S, QuantStore::Native).per_block([4u8], ScaleDtype::F32);
        let values = vec![
            0.1, 0.2, 0.3, 0.4, // block 0
            10.0, 20.0, 30.0, 40.0, // block 1
        ];
        let scales = vec![0.4 / 127.0, 40.0 / 127.0];

        let bytes = quantize(&values, &[8], &scales, &[4], &s);
        let got: Vec<i8> = bytes.iter().map(|&b| b as i8).collect();

        // Largest element of each block saturates to the quant max.
        assert_eq!(got[3], 127);
        assert_eq!(got[7], 127);

        // And the per-block scale is applied on the way back.
        let restored = dequantize(&bytes, &[8], &scales, &[4], &s);
        for (got, want) in restored.iter().zip(&values) {
            let step = if *want < 1.0 { scales[0] } else { scales[1] };
            assert!((got - want).abs() <= step + 1e-6);
        }
    }
}