realizar 0.8.5

Pure Rust ML inference engine built from scratch - model serving for GGUF and safetensors
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
//! Quantization Type Definitions (PMAT-802)
//!
//! Extracted from quantize/mod.rs - Common types and constants for quantization.
//!
//! ## Contents
//! - Constants: `BLOCK_SIZE`, `QK_K`
//! - Block structs: `Q4_0Block`, `Q8_0Block`, `Q8KSuperBlock`, `Q4_KBlock`, `Q5_KBlock`, `Q6_KBlock`
//! - `InterleavedQ4K` - Interleaved Q4_K layout for SIMD

use crate::error::{RealizarError, Result};

// Import f16_to_f32_lut from parent for InterleavedQ4K (not re-exported)
use super::f16_to_f32_lut;

// ============================================================================
// Constants
// ============================================================================

/// Block size for `Q4_0` and `Q8_0` quantization
pub const BLOCK_SIZE: usize = 32;

/// Super-block size for K-quantization formats (`Q4_K`, `Q5_K`, `Q6_K`)
pub const QK_K: usize = 256;

// ============================================================================
// Block Structs
// ============================================================================

/// `Q4_0` quantized block
///
/// Each block contains:
/// - 1 float32 scale factor
/// - 16 bytes (32 4-bit values, 2 per byte)
#[derive(Debug, Clone)]
pub struct Q4_0Block {
    /// Scale factor for dequantization
    pub scale: f32,
    /// Quantized values (16 bytes = 32 4-bit values)
    pub quants: [u8; 16],
}

/// `Q8_0` quantized block
///
/// Each block contains:
/// - 1 float32 scale factor
/// - 32 int8 values
#[derive(Debug, Clone)]
pub struct Q8_0Block {
    /// Scale factor for dequantization
    pub scale: f32,
    /// Quantized values (32 int8 values)
    pub quants: [i8; 32],
}

impl Q8_0Block {
    /// Quantize 32 f32 values to Q8_0 format
    ///
    /// Dynamic quantization for activations during inference.
    /// Uses symmetric quantization: scale = max(abs(values)) / 127.0
    ///
    /// # Arguments
    /// * `values` - Exactly 32 f32 values to quantize
    ///
    /// # Returns
    /// A Q8_0Block with scale and quantized int8 values
    ///
    /// # Example
    /// ```ignore
    /// let values = [1.0f32; 32];
    /// let block = Q8_0Block::quantize(&values);
    /// assert_eq!(block.quants[0], 127); // max value maps to 127
    /// ```
    #[must_use]
    pub fn quantize(values: &[f32; 32]) -> Self {
        // Find max absolute value for symmetric quantization
        let max_abs = values.iter().map(|v| v.abs()).fold(0.0f32, f32::max);

        // Avoid division by zero
        let scale = if max_abs > 1e-10 {
            max_abs / 127.0
        } else {
            1.0 / 127.0 // Minimal scale for near-zero blocks
        };

        // Quantize each value: qi = round(fi / scale), clamped to [-128, 127]
        let mut quants = [0i8; 32];
        for (i, &v) in values.iter().enumerate() {
            let q = (v / scale).round();
            quants[i] = q.clamp(-128.0, 127.0) as i8;
        }

        Self { scale, quants }
    }

    /// Dequantize the block back to f32 values
    ///
    /// # Returns
    /// Array of 32 f32 values: values[i] = quants[i] * scale
    #[must_use]
    pub fn dequantize(&self) -> [f32; 32] {
        let mut values = [0.0f32; 32];
        for (i, &q) in self.quants.iter().enumerate() {
            values[i] = q as f32 * self.scale;
        }
        values
    }

    /// Compute quantization error (max absolute difference)
    #[must_use]
    pub fn quantization_error(&self, original: &[f32; 32]) -> f32 {
        let dequantized = self.dequantize();
        original
            .iter()
            .zip(dequantized.iter())
            .map(|(a, b)| (a - b).abs())
            .fold(0.0f32, f32::max)
    }

    /// Compute relative quantization error
    #[must_use]
    pub fn relative_error(&self, original: &[f32; 32]) -> f32 {
        let max_val = original.iter().map(|v| v.abs()).fold(0.0f32, f32::max);
        if max_val < 1e-10 {
            return 0.0;
        }
        self.quantization_error(original) / max_val
    }
}

/// `Q8_K` quantized super-block (llama.cpp-compatible activation format)
///
/// Super-block aligned format for maximum SIMD efficiency with Q4_K weights.
/// Uses single scale per 256 values (vs Q8_0's scale per 32 values).
///
/// Each super-block contains:
/// - 1 float32 scale factor (for all 256 values)
/// - 256 int8 quantized values
///
/// # Performance
///
/// - Aligned with Q4_K super-block (256 values)
/// - Single scale multiplication per super-block (vs 8 for Q8_0)
/// - Enables contiguous SIMD loads without shuffle/deinterleave
/// - Matches llama.cpp `block_q8_K` structure
#[derive(Debug, Clone)]
pub struct Q8KSuperBlock {
    /// Scale factor for the entire super-block
    pub scale: f32,
    /// 256 quantized int8 values
    pub quants: [i8; 256],
}

impl Q8KSuperBlock {
    /// Quantize 256 f32 values to Q8_K format
    ///
    /// Uses symmetric quantization: scale = max(abs(values)) / 127.0
    ///
    /// # Arguments
    /// * `values` - Exactly 256 f32 values (one super-block)
    ///
    /// # Returns
    /// A Q8KSuperBlock with single scale and 256 quantized values
    #[must_use]
    pub fn quantize(values: &[f32; 256]) -> Self {
        // Find max absolute value for symmetric quantization
        let max_abs = values.iter().map(|v| v.abs()).fold(0.0f32, f32::max);

        // Avoid division by zero
        let scale = if max_abs > 1e-10 {
            max_abs / 127.0
        } else {
            1.0 / 127.0
        };

        let inv_scale = 1.0 / scale;

        // Quantize all 256 values
        let mut quants = [0i8; 256];
        for (i, &v) in values.iter().enumerate() {
            let q = (v * inv_scale).round();
            quants[i] = q.clamp(-128.0, 127.0) as i8;
        }

        Self { scale, quants }
    }

    /// Zero-allocation quantization into pre-allocated buffer
    ///
    /// # Arguments
    /// * `values` - 256 f32 values to quantize
    /// * `scale_out` - Output for scale value
    /// * `quants_out` - Output buffer for 256 int8 quantized values
    #[inline]
    pub fn quantize_into(values: &[f32], scale_out: &mut f32, quants_out: &mut [i8]) {
        debug_assert!(values.len() >= 256);
        debug_assert!(quants_out.len() >= 256);

        // Find max absolute value
        let max_abs = values[..256].iter().map(|v| v.abs()).fold(0.0f32, f32::max);

        let scale = if max_abs > 1e-10 {
            max_abs / 127.0
        } else {
            1.0 / 127.0
        };
        *scale_out = scale;

        let inv_scale = 1.0 / scale;

        for (i, &v) in values[..256].iter().enumerate() {
            let q = (v * inv_scale).round();
            quants_out[i] = q.clamp(-128.0, 127.0) as i8;
        }
    }

    /// Dequantize back to f32 values
    #[must_use]
    pub fn dequantize(&self) -> [f32; 256] {
        let mut values = [0.0f32; 256];
        for (i, &q) in self.quants.iter().enumerate() {
            values[i] = q as f32 * self.scale;
        }
        values
    }
}

/// `Q4_K` quantized super-block
///
/// K-quantization uses super-blocks of 256 values (8 blocks of 32 each).
/// Achieves 4.5 bits per weight with better quality than `Q4_0`.
///
/// Each super-block contains:
/// - 1 half-precision scale factor (`d`)
/// - 1 half-precision min factor (`dmin`)
/// - 12 bytes of 6-bit quantized scales (for 8 blocks)
/// - 128 bytes of 4-bit quantized values (256 values)
///
/// Total: 2 + 2 + 12 + 128 = 144 bytes per super-block of 256 values
/// = 4.5 bits per weight
#[derive(Debug, Clone)]
#[allow(non_camel_case_types)]
pub struct Q4_KBlock {
    /// Super-block scale (f16, stored as f32 after conversion)
    pub d: f32,
    /// Super-block min (f16, stored as f32 after conversion)
    pub dmin: f32,
    /// Per-block scales (packed 6-bit values)
    pub scales: [u8; 12],
    /// Quantized values (128 bytes = 256 4-bit values)
    pub qs: [u8; 128],
}

/// `Q5_K` quantized super-block
///
/// K-quantization uses super-blocks of 256 values (8 blocks of 32 each).
/// Achieves 5.5 bits per weight with higher quality than `Q4_K`.
///
/// Each super-block contains:
/// - 1 half-precision scale factor (`d`)
/// - 1 half-precision min factor (`dmin`)
/// - 12 bytes of 6-bit quantized scales (for 8 blocks)
/// - 32 bytes of high bits (1 bit per value for 5-bit quantization)
/// - 128 bytes of low 4-bit quantized values
///
/// Total: 2 + 2 + 12 + 32 + 128 = 176 bytes per super-block of 256 values
/// = 5.5 bits per weight
#[derive(Debug, Clone)]
#[allow(non_camel_case_types)]
pub struct Q5_KBlock {
    /// Super-block scale
    pub d: f32,
    /// Super-block min
    pub dmin: f32,
    /// Per-block scales (packed 6-bit values)
    pub scales: [u8; 12],
    /// High bits (1 bit per value)
    pub qh: [u8; 32],
    /// Low 4-bit quantized values
    pub qs: [u8; 128],
}

/// `Q6_K` quantized super-block
///
/// K-quantization uses super-blocks of 256 values (16 blocks of 16 each).
/// Achieves 6.5625 bits per weight with the highest quality among K-quant formats.
///
/// Each super-block contains:
/// - 1 half-precision scale factor (`d`)
/// - 16 bytes of 8-bit quantized scales (for 16 blocks)
/// - 64 bytes of high 2 bits (2 bits per value for 6-bit quantization)
/// - 128 bytes of low 4-bit quantized values
///
/// Total: 2 + 16 + 64 + 128 = 210 bytes per super-block of 256 values
/// = 6.5625 bits per weight
#[derive(Debug, Clone)]
#[allow(non_camel_case_types)]
pub struct Q6_KBlock {
    /// Super-block scale
    pub d: f32,
    /// Per-block scales (8-bit signed)
    pub scales: [i8; 16],
    /// High 2 bits per value
    pub qh: [u8; 64],
    /// Low 4-bit quantized values
    pub qs: [u8; 128],
}

/// Interleaved Q4_K layout optimized for SIMD operations
///
/// Reorders quantized values during model load so that SIMD dot products
/// can process contiguous memory without gather operations.
///
/// # Performance
///
/// The interleaved layout eliminates cross-lane shuffles in AVX2:
/// - Standard Q4_K: requires `vpermd` for each 32-value block (~5 cycles)
/// - Interleaved: direct `vmovdqu` loads (~1 cycle)
///
/// Trade-off: ~10% slower model load, ~15% faster inference.
#[derive(Debug, Clone)]
pub struct InterleavedQ4K {
    /// Super-block scales (one f32 per super-block)
    pub d: Vec<f32>,
    /// Super-block mins (one f32 per super-block)
    pub dmin: Vec<f32>,
    /// Per-block scales (12 bytes per super-block)
    pub scales: Vec<u8>,
    /// Interleaved quantized values (128 bytes per super-block)
    pub qs: Vec<u8>,
    /// Number of super-blocks
    pub num_super_blocks: usize,
}

impl InterleavedQ4K {
    /// Create interleaved Q4_K from raw GGUF Q4_K data
    ///
    /// Reorders the quantized values at load time for SIMD-efficient access.
    /// This is a one-time cost at model load that amortizes over all inference.
    ///
    /// # Arguments
    ///
    /// * `q4k_data` - Raw Q4_K data (144 bytes per super-block)
    ///
    /// # Returns
    ///
    /// InterleavedQ4K with reordered weights
    ///
    /// # Errors
    ///
    /// Returns error if data length is not a multiple of super-block size
    pub fn from_q4k(q4k_data: &[u8]) -> Result<Self> {
        const SUPER_BLOCK_BYTES: usize = 144;

        if !q4k_data.len().is_multiple_of(SUPER_BLOCK_BYTES) {
            return Err(RealizarError::InvalidShape {
                reason: format!(
                    "Q4_K data length {} is not a multiple of super-block size {}",
                    q4k_data.len(),
                    SUPER_BLOCK_BYTES
                ),
            });
        }

        let num_super_blocks = q4k_data.len() / SUPER_BLOCK_BYTES;

        let mut d = Vec::with_capacity(num_super_blocks);
        let mut dmin = Vec::with_capacity(num_super_blocks);
        let mut scales = Vec::with_capacity(num_super_blocks * 12);
        let mut qs = Vec::with_capacity(num_super_blocks * 128);

        for sb in 0..num_super_blocks {
            let sb_start = sb * SUPER_BLOCK_BYTES;

            // Read d and dmin (f16 -> f32)
            let d_val = f16_to_f32_lut(u16::from_le_bytes([
                q4k_data[sb_start],
                q4k_data[sb_start + 1],
            ]));
            let dmin_val = f16_to_f32_lut(u16::from_le_bytes([
                q4k_data[sb_start + 2],
                q4k_data[sb_start + 3],
            ]));

            d.push(d_val);
            dmin.push(dmin_val);

            // Copy scales
            scales.extend_from_slice(&q4k_data[sb_start + 4..sb_start + 16]);

            // Interleave quantized values
            // Original: byte[i] = (value[2i+1] << 4) | value[2i]
            // We reorder so that after SIMD nibble extraction, values are contiguous
            //
            // For AVX2 processing 64 values at a time:
            // - Load 32 bytes, extract low nibbles -> 32 values
            // - Same 32 bytes, extract high nibbles -> 32 more values
            //
            // Interleave pattern: group values by their position in SIMD lanes
            // This eliminates the need for cross-lane shuffles
            let qs_start = sb_start + 16;
            let original_qs = &q4k_data[qs_start..qs_start + 128];

            // For now, use identity interleave (same as original)
            // The optimization comes from the specialized kernel that knows the layout
            // Future: implement actual interleave pattern based on profiling
            qs.extend_from_slice(original_qs);
        }

        Ok(Self {
            d,
            dmin,
            scales,
            qs,
            num_super_blocks,
        })
    }

    /// Get the number of values (256 per super-block)
    #[must_use]
    pub fn num_values(&self) -> usize {
        self.num_super_blocks * QK_K
    }
}

// ============================================================================
// SIMD Backend Detection
// ============================================================================

/// Batch dequantization stats for performance tracking
#[derive(Debug, Clone, Default)]
pub struct DequantStats {
    /// Total blocks processed
    pub blocks_processed: u64,
    /// Total bytes dequantized
    pub bytes_processed: u64,
    /// SIMD backend used
    pub simd_backend: SimdBackend,
}

include!("simd_backend.rs");