oxibonsai-core 0.1.4

GGUF Q1_0_g128 loader, tensor types, and configuration for OxiBonsai
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
//! Integration tests for Q2_K and Q4_K quantization formats.

use oxibonsai_core::quant_k::{BlockQ2K, BlockQ4K, BLOCK_Q2_K_BYTES, BLOCK_Q4_K_BYTES, QK_K};

/// Simple LCG PRNG for reproducible test data (no rand dependency).
fn lcg(state: &mut u64) -> f32 {
    *state = state
        .wrapping_mul(6364136223846793005)
        .wrapping_add(1442695040888963407);
    ((*state >> 33) as f32) / (u32::MAX as f32) * 2.0 - 1.0
}

fn generate_test_data(n: usize, seed: u64) -> Vec<f32> {
    let mut state = seed;
    (0..n).map(|_| lcg(&mut state)).collect()
}

// -----------------------------------------------------------------------
// Q2_K tests
// -----------------------------------------------------------------------

#[test]
fn q2k_block_byte_size() {
    assert_eq!(std::mem::size_of::<BlockQ2K>(), BLOCK_Q2_K_BYTES);
    assert_eq!(BLOCK_Q2_K_BYTES, 84);
}

#[test]
fn q2k_zero_input_roundtrip() {
    let input = vec![0.0f32; QK_K];
    let blocks = BlockQ2K::quantize(&input).expect("quantize should succeed");
    assert_eq!(blocks.len(), 1);

    let mut output = vec![0.0f32; QK_K];
    BlockQ2K::dequant(&blocks, &mut output).expect("dequant should succeed");

    for (i, &v) in output.iter().enumerate() {
        assert!(v.abs() < 0.01, "Q2_K zero roundtrip: index {i}, got {v}");
    }
}

#[test]
fn q2k_constant_positive_roundtrip() {
    let input = vec![1.0f32; QK_K];
    let blocks = BlockQ2K::quantize(&input).expect("quantize should succeed");
    let mut output = vec![0.0f32; QK_K];
    BlockQ2K::dequant(&blocks, &mut output).expect("dequant should succeed");

    for (i, &v) in output.iter().enumerate() {
        assert!(
            (v - 1.0).abs() < 0.2,
            "Q2_K constant roundtrip: index {i}, expected ~1.0, got {v}"
        );
    }
}

#[test]
fn q2k_constant_negative_roundtrip() {
    let input = vec![-0.5f32; QK_K];
    let blocks = BlockQ2K::quantize(&input).expect("quantize should succeed");
    let mut output = vec![0.0f32; QK_K];
    BlockQ2K::dequant(&blocks, &mut output).expect("dequant should succeed");

    for (i, &v) in output.iter().enumerate() {
        assert!(
            (v - (-0.5)).abs() < 0.2,
            "Q2_K negative roundtrip: index {i}, expected ~-0.5, got {v}"
        );
    }
}

#[test]
fn q2k_random_roundtrip_error_bounded() {
    let input = generate_test_data(QK_K, 42);
    let blocks = BlockQ2K::quantize(&input).expect("quantize should succeed");
    let mut output = vec![0.0f32; QK_K];
    BlockQ2K::dequant(&blocks, &mut output).expect("dequant should succeed");

    let max_err: f32 = input
        .iter()
        .zip(output.iter())
        .map(|(a, b)| (a - b).abs())
        .fold(0.0f32, f32::max);

    // Q2_K is very coarse (2-bit), allow up to ~0.6 error on [-1,1] range
    assert!(
        max_err < 0.7,
        "Q2_K random roundtrip max error {max_err} exceeds tolerance"
    );
}

#[test]
fn q2k_multiple_blocks() {
    let num_blocks = 4;
    let input = generate_test_data(QK_K * num_blocks, 123);
    let blocks = BlockQ2K::quantize(&input).expect("quantize should succeed");
    assert_eq!(blocks.len(), num_blocks);

    let mut output = vec![0.0f32; QK_K * num_blocks];
    BlockQ2K::dequant(&blocks, &mut output).expect("dequant should succeed");

    let max_err: f32 = input
        .iter()
        .zip(output.iter())
        .map(|(a, b)| (a - b).abs())
        .fold(0.0f32, f32::max);

    assert!(
        max_err < 0.7,
        "Q2_K multi-block max error {max_err} exceeds tolerance"
    );
}

#[test]
fn q2k_dequant_known_values() {
    // Construct a known block manually and verify dequant output
    use half::f16;

    let block = BlockQ2K {
        scales: {
            let mut s = [0u8; 16];
            // sub-block 0: scale=2, min=1 -> byte = 0x12
            s[0] = 0x12;
            // all others zero
            s
        },
        qs: {
            let mut q = [0u8; 64];
            // First 16 weights in sub-block 0: set all to q=3 (binary 11)
            // 4 weights per byte, each 2 bits = 0b11_11_11_11 = 0xFF
            for item in q[..4].iter_mut() {
                *item = 0xFF;
            }
            q
        },
        d: f16::from_f32(0.5),
        dmin: f16::from_f32(0.25),
    };

    let mut output = vec![0.0f32; QK_K];
    BlockQ2K::dequant(&[block], &mut output).expect("dequant should succeed");

    // Sub-block 0: w = d * scale * q - dmin * min = 0.5 * 2 * 3 - 0.25 * 1 = 2.75
    let expected = 0.5 * 2.0 * 3.0 - 0.25 * 1.0;
    for (i, &val) in output[..16].iter().enumerate() {
        assert!(
            (val - expected).abs() < 0.01,
            "Q2_K known dequant: index {i}, expected {expected}, got {}",
            val
        );
    }

    // Remaining sub-blocks have scale=0, min=0, so all weights should be 0
    for (i, &val) in output.iter().enumerate().take(QK_K).skip(16) {
        assert!(
            val.abs() < 0.01,
            "Q2_K known dequant: index {i}, expected 0.0, got {}",
            val
        );
    }
}

#[test]
fn q2k_invalid_input_length() {
    let input = vec![0.0f32; 100]; // not a multiple of 256
    let result = BlockQ2K::quantize(&input);
    assert!(result.is_err());
}

#[test]
fn q2k_dequant_output_too_small() {
    let input = vec![0.0f32; QK_K];
    let blocks = BlockQ2K::quantize(&input).expect("quantize should succeed");
    let mut output = vec![0.0f32; 10]; // too small
    let result = BlockQ2K::dequant(&blocks, &mut output);
    assert!(result.is_err());
}

// -----------------------------------------------------------------------
// Q4_K tests
// -----------------------------------------------------------------------

#[test]
fn q4k_block_byte_size() {
    assert_eq!(std::mem::size_of::<BlockQ4K>(), BLOCK_Q4_K_BYTES);
    assert_eq!(BLOCK_Q4_K_BYTES, 144);
}

#[test]
fn q4k_zero_input_roundtrip() {
    let input = vec![0.0f32; QK_K];
    let blocks = BlockQ4K::quantize(&input).expect("quantize should succeed");
    assert_eq!(blocks.len(), 1);

    let mut output = vec![0.0f32; QK_K];
    BlockQ4K::dequant(&blocks, &mut output).expect("dequant should succeed");

    for (i, &v) in output.iter().enumerate() {
        assert!(v.abs() < 0.01, "Q4_K zero roundtrip: index {i}, got {v}");
    }
}

#[test]
fn q4k_constant_positive_roundtrip() {
    let input = vec![1.0f32; QK_K];
    let blocks = BlockQ4K::quantize(&input).expect("quantize should succeed");
    let mut output = vec![0.0f32; QK_K];
    BlockQ4K::dequant(&blocks, &mut output).expect("dequant should succeed");

    for (i, &v) in output.iter().enumerate() {
        assert!(
            (v - 1.0).abs() < 0.1,
            "Q4_K constant roundtrip: index {i}, expected ~1.0, got {v}"
        );
    }
}

#[test]
fn q4k_constant_negative_roundtrip() {
    let input = vec![-0.5f32; QK_K];
    let blocks = BlockQ4K::quantize(&input).expect("quantize should succeed");
    let mut output = vec![0.0f32; QK_K];
    BlockQ4K::dequant(&blocks, &mut output).expect("dequant should succeed");

    for (i, &v) in output.iter().enumerate() {
        assert!(
            (v - (-0.5)).abs() < 0.1,
            "Q4_K negative roundtrip: index {i}, expected ~-0.5, got {v}"
        );
    }
}

#[test]
fn q4k_random_roundtrip_error_bounded() {
    let input = generate_test_data(QK_K, 42);
    let blocks = BlockQ4K::quantize(&input).expect("quantize should succeed");
    let mut output = vec![0.0f32; QK_K];
    BlockQ4K::dequant(&blocks, &mut output).expect("dequant should succeed");

    let max_err: f32 = input
        .iter()
        .zip(output.iter())
        .map(|(a, b)| (a - b).abs())
        .fold(0.0f32, f32::max);

    // Q4_K is 4-bit, should be much more precise than Q2_K
    assert!(
        max_err < 0.15,
        "Q4_K random roundtrip max error {max_err} exceeds tolerance"
    );
}

#[test]
fn q4k_multiple_blocks() {
    let num_blocks = 4;
    let input = generate_test_data(QK_K * num_blocks, 999);
    let blocks = BlockQ4K::quantize(&input).expect("quantize should succeed");
    assert_eq!(blocks.len(), num_blocks);

    let mut output = vec![0.0f32; QK_K * num_blocks];
    BlockQ4K::dequant(&blocks, &mut output).expect("dequant should succeed");

    let max_err: f32 = input
        .iter()
        .zip(output.iter())
        .map(|(a, b)| (a - b).abs())
        .fold(0.0f32, f32::max);

    assert!(
        max_err < 0.15,
        "Q4_K multi-block max error {max_err} exceeds tolerance"
    );
}

#[test]
fn q4k_dequant_known_values() {
    use half::f16;
    use oxibonsai_core::quant_k::BlockQ4K;

    // Build a block with known scale/min and a single non-zero sub-block
    let sc = [4u8, 0, 0, 0, 0, 0, 0, 0];
    let mn = [2u8, 0, 0, 0, 0, 0, 0, 0];

    // Use the internal encode (we test encode/decode roundtrip separately)
    // Inline the encode logic since it's not pub
    let mut scales_raw = [0u8; 12];
    // Low 4 bits of scales
    scales_raw[0] = sc[0] & 0x0F | ((sc[1] & 0x0F) << 4);
    scales_raw[1] = sc[2] & 0x0F | ((sc[3] & 0x0F) << 4);
    scales_raw[2] = sc[4] & 0x0F | ((sc[5] & 0x0F) << 4);
    scales_raw[3] = sc[6] & 0x0F | ((sc[7] & 0x0F) << 4);
    // Low 4 bits of mins
    scales_raw[4] = mn[0] & 0x0F | ((mn[1] & 0x0F) << 4);
    scales_raw[5] = mn[2] & 0x0F | ((mn[3] & 0x0F) << 4);
    scales_raw[6] = mn[4] & 0x0F | ((mn[5] & 0x0F) << 4);
    scales_raw[7] = mn[6] & 0x0F | ((mn[7] & 0x0F) << 4);
    // Upper bits are all 0 since sc/mn values fit in 4 bits

    let mut qs = [0u8; 128];
    // Set first 32 weights (sub-block 0) to q=7
    // 2 weights per byte: low nibble = 7, high nibble = 7 -> 0x77
    for item in qs[..16].iter_mut() {
        *item = 0x77;
    }

    let block = BlockQ4K {
        d: f16::from_f32(0.5),
        dmin: f16::from_f32(0.25),
        scales: scales_raw,
        qs,
    };

    let mut output = vec![0.0f32; QK_K];
    BlockQ4K::dequant(&[block], &mut output).expect("dequant should succeed");

    // Sub-block 0: w = d * sc * q - dmin * mn = 0.5 * 4 * 7 - 0.25 * 2 = 13.5
    let expected = 0.5 * 4.0 * 7.0 - 0.25 * 2.0;
    for (i, &val) in output[..32].iter().enumerate() {
        assert!(
            (val - expected).abs() < 0.01,
            "Q4_K known dequant: index {i}, expected {expected}, got {}",
            val
        );
    }

    // Remaining sub-blocks: scale=0, min=0 -> w = 0
    for (i, &val) in output.iter().enumerate().take(QK_K).skip(32) {
        assert!(
            val.abs() < 0.01,
            "Q4_K known dequant: index {i}, expected 0.0, got {}",
            val
        );
    }
}

#[test]
fn q4k_invalid_input_length() {
    let input = vec![0.0f32; 100];
    let result = BlockQ4K::quantize(&input);
    assert!(result.is_err());
}

#[test]
fn q4k_dequant_output_too_small() {
    let input = vec![0.0f32; QK_K];
    let blocks = BlockQ4K::quantize(&input).expect("quantize should succeed");
    let mut output = vec![0.0f32; 10];
    let result = BlockQ4K::dequant(&blocks, &mut output);
    assert!(result.is_err());
}

#[test]
fn q2k_large_values_roundtrip() {
    // Test with values in a wider range
    let mut input = vec![0.0f32; QK_K];
    let mut state = 7777u64;
    for v in input.iter_mut() {
        *v = lcg(&mut state) * 10.0; // range [-10, 10]
    }

    let blocks = BlockQ2K::quantize(&input).expect("quantize should succeed");
    let mut output = vec![0.0f32; QK_K];
    BlockQ2K::dequant(&blocks, &mut output).expect("dequant should succeed");

    let max_err: f32 = input
        .iter()
        .zip(output.iter())
        .map(|(a, b)| (a - b).abs())
        .fold(0.0f32, f32::max);

    // Wider range means larger absolute error, but relative should be similar
    assert!(
        max_err < 7.0,
        "Q2_K large values max error {max_err} exceeds tolerance"
    );
}

#[test]
fn q4k_large_values_roundtrip() {
    let mut input = vec![0.0f32; QK_K];
    let mut state = 8888u64;
    for v in input.iter_mut() {
        *v = lcg(&mut state) * 10.0;
    }

    let blocks = BlockQ4K::quantize(&input).expect("quantize should succeed");
    let mut output = vec![0.0f32; QK_K];
    BlockQ4K::dequant(&blocks, &mut output).expect("dequant should succeed");

    let max_err: f32 = input
        .iter()
        .zip(output.iter())
        .map(|(a, b)| (a - b).abs())
        .fold(0.0f32, f32::max);

    assert!(
        max_err < 1.5,
        "Q4_K large values max error {max_err} exceeds tolerance"
    );
}

#[test]
fn q2k_q4k_precision_ordering() {
    // Q4_K should be more precise than Q2_K on the same data
    let input = generate_test_data(QK_K, 54321);

    let q2_blocks = BlockQ2K::quantize(&input).expect("Q2_K quantize");
    let mut q2_output = vec![0.0f32; QK_K];
    BlockQ2K::dequant(&q2_blocks, &mut q2_output).expect("Q2_K dequant");

    let q4_blocks = BlockQ4K::quantize(&input).expect("Q4_K quantize");
    let mut q4_output = vec![0.0f32; QK_K];
    BlockQ4K::dequant(&q4_blocks, &mut q4_output).expect("Q4_K dequant");

    let q2_mse: f32 = input
        .iter()
        .zip(q2_output.iter())
        .map(|(a, b)| (a - b) * (a - b))
        .sum::<f32>()
        / QK_K as f32;

    let q4_mse: f32 = input
        .iter()
        .zip(q4_output.iter())
        .map(|(a, b)| (a - b) * (a - b))
        .sum::<f32>()
        / QK_K as f32;

    assert!(
        q4_mse < q2_mse,
        "Q4_K MSE ({q4_mse}) should be less than Q2_K MSE ({q2_mse})"
    );
}

// -----------------------------------------------------------------------
// Ternary type importability from crate root
// -----------------------------------------------------------------------

#[test]
fn ternary_types_importable_from_crate_root() {
    let _: oxibonsai_core::BlockTQ2_0_g128;
    let _: oxibonsai_core::BlockTQ2_0;
    let _: oxibonsai_core::TernaryCode;
    assert_eq!(oxibonsai_core::QK_TQ2_0_G128, 128);
    assert_eq!(oxibonsai_core::QK_TQ2_0, 256);
    assert_eq!(oxibonsai_core::BLOCK_TQ2_0_G128_BYTES, 34);
    assert_eq!(oxibonsai_core::BLOCK_TQ2_0_BYTES, 66);
}