prism-inference 0.1.0

Bounded Rust CPU inference and tokenization for Prism-compatible GGUF models
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
457
458
459
//! Safe scalar `IQ2_S/IQ3` reference and importance fitting.
//!
//! Ported from pinned Prism `b87ca7225456b06b0975b8239c66b5aecf07e37c`,
//! ggml/src/ggml-quants.c (MIT; see root NOTICE). Geometric neighbour shells,
//! index tie breaking, weighted refitting and source scalar association are
//! retained. Search neighbours are cached only for the duration of one row.

#![allow(
    clippy::as_conversions,
    clippy::cast_possible_truncation,
    clippy::cast_precision_loss,
    clippy::cast_sign_loss,
    clippy::float_cmp,
    clippy::similar_names,
    clippy::suboptimal_flops,
    clippy::suspicious_operation_groupings,
    reason = "Pinned fitting specifies bounded integer bit fields and exact scalar multiply/add association."
)]

use std::collections::HashMap;

use bonsai_ternary::half_to_float;

use super::{finite, half_bits};
use crate::{Result, invalid};

struct Grid<'a, const N: usize> {
    points: &'a [[u8; N]],
    map: Vec<i16>,
    neighbours: HashMap<usize, Vec<usize>>,
    bits: usize,
    shells: usize,
}

impl<'a, const N: usize> Grid<'a, N> {
    fn new(points: &'a [[u8; N]], bits: usize, shells: usize) -> Result<Self> {
        let mut map = Vec::new();
        map.try_reserve_exact(1 << (bits * N))?;
        map.resize(1 << (bits * N), -1);
        for (index, point) in points.iter().enumerate() {
            let key = Self::key(&point.map(|value| (value - 1) / 2), bits);
            map[key] = i16::try_from(index)?;
        }
        Ok(Self {
            points,
            map,
            neighbours: HashMap::new(),
            bits,
            shells,
        })
    }

    fn key(levels: &[u8], bits: usize) -> usize {
        levels.iter().enumerate().fold(0, |key, (index, &level)| {
            key | (usize::from(level) << (bits * index))
        })
    }

    fn index(&self, levels: &[u8]) -> Result<usize> {
        usize::try_from(self.map[Self::key(levels, self.bits)])
            .map_err(|_| invalid("IQ fitted point is absent from native grid"))
    }

    fn candidates(&mut self, key: usize, levels: &[u8]) -> Result<&[usize]> {
        if !self.neighbours.contains_key(&key) {
            let mut distances = Vec::new();
            distances.try_reserve_exact(self.points.len())?;
            for (index, point) in self.points.iter().enumerate() {
                let distance = point.iter().zip(levels).fold(0_i32, |sum, (&q, &l)| {
                    let difference = i32::from(q) - (2 * i32::from(l) + 1);
                    sum + difference * difference
                });
                distances.push((distance, index));
            }
            // Native qsort compares squared distance and then original index.
            distances.sort_unstable();
            let mut previous = distances[0].0;
            let mut shells = 1;
            let mut neighbours = Vec::new();
            neighbours.try_reserve_exact(distances.len())?;
            for (distance, index) in distances {
                if distance > previous {
                    if shells == self.shells {
                        break;
                    }
                    previous = distance;
                    shells += 1;
                }
                neighbours.push(index);
            }
            self.neighbours.try_reserve(1)?;
            self.neighbours.insert(key, neighbours);
        }
        self.neighbours
            .get(&key)
            .map(Vec::as_slice)
            .ok_or_else(|| invalid("IQ neighbour cache entry missing"))
    }

    fn fit(
        &mut self,
        levels: &mut [u8],
        values: &[f32],
        weights: &[f32],
        scale: f32,
    ) -> Result<bool> {
        let key = Self::key(levels, self.bits);
        if self.map[key] >= 0 {
            return Ok(true);
        }
        let points = self.points;
        let candidates = self.candidates(key, levels)?;
        let mut best_distance = f32::MAX;
        let mut best_index = None;
        for &candidate in candidates {
            let mut distance = 0.0_f32;
            for ((&q, &value), &weight) in points[candidate].iter().zip(values).zip(weights) {
                let difference = scale * f32::from(q) - value;
                distance += weight * difference * difference;
            }
            if distance < best_distance {
                best_distance = distance;
                best_index = Some(candidate);
            }
        }
        let index = best_index.ok_or_else(|| invalid("IQ neighbour fit overflowed"))?;
        for (level, &point) in levels.iter_mut().zip(&points[index]) {
            *level = (point - 1) / 2;
        }
        Ok(false)
    }
}

fn nearest(value: f32) -> Result<i32> {
    if !value.is_finite() || value.abs() > 4_194_303.0 {
        return Err(invalid("IQ rounding exceeds native nearest_int range"));
    }
    Ok(((value + 12_582_912.0).to_bits() & 0x007f_ffff).cast_signed() - 0x0040_0000)
}

fn levels(values: &[f32], inverse: f32, maximum: u8, output: &mut [u8]) -> Result<()> {
    for (level, &value) in output.iter_mut().zip(values) {
        *level = nearest(0.5 * (inverse * value - 1.0))?.clamp(0, i32::from(maximum)) as u8;
    }
    Ok(())
}

fn sums(levels: &[u8], values: &[f32], weights: &[f32]) -> Result<(f32, f32)> {
    let mut sum_qx = 0.0_f32;
    let mut sum_q2 = 0.0_f32;
    for ((&level, &value), &weight) in levels.iter().zip(values).zip(weights) {
        let q = f32::from(2 * level + 1);
        sum_qx += weight * value * q;
        sum_q2 += weight * q * q;
    }
    Ok((finite(sum_qx)?, finite(sum_q2)?))
}

struct Configuration {
    kind: u32,
    bytes: usize,
    width: usize,
    maximum: u8,
    trials: i32,
    delta: f32,
    epsilon: f32,
    factor: f32,
}

impl Configuration {
    fn new(kind: u32) -> Result<Self> {
        match kind {
            18 => Ok(Self {
                kind,
                bytes: 98,
                width: 32,
                maximum: 7,
                trials: 15,
                delta: 0.2,
                epsilon: 1.0e-8,
                factor: 1.0125,
            }),
            21 => Ok(Self {
                kind,
                bytes: 110,
                width: 32,
                maximum: 7,
                trials: 9,
                delta: 0.2,
                epsilon: 0.0,
                factor: 1.033,
            }),
            22 => Ok(Self {
                kind,
                bytes: 82,
                width: 16,
                maximum: 2,
                trials: 9,
                delta: 0.1,
                epsilon: 1.0e-8,
                factor: 0.9875,
            }),
            _ => Err(invalid("unsupported IQ fitting type")),
        }
    }

    fn pack<const N: usize>(
        &self,
        grid: &Grid<'_, N>,
        group: usize,
        written_group: usize,
        levels: &[u8],
        signs: &[u8],
        output: &mut [u8],
    ) -> Result<()> {
        for (local, levels) in levels.as_chunks::<N>().0.iter().enumerate() {
            let index = grid.index(levels)?;
            let absolute = group * (self.width / N) + local;
            let written = written_group * (self.width / N) + local;
            output[2 + written] = index as u8;
            if self.kind == 21 {
                output[66 + absolute / 8] |= ((index >> 8) as u8) << (absolute % 8);
            } else if self.kind == 22 {
                output[66 + absolute / 4] |= ((index >> 8) as u8) << (2 * (absolute % 4));
            }
        }
        if self.kind == 18 {
            let word = u32::from(signs[0])
                | (u32::from(signs[1]) << 7)
                | (u32::from(signs[2]) << 14)
                | (u32::from(signs[3]) << 21);
            output[66 + group * 4..70 + group * 4].copy_from_slice(&word.to_le_bytes());
        } else {
            let offset = if self.kind == 21 { 74 } else { 34 };
            let offset = offset + written_group * (self.width / 8);
            output[offset..offset + signs.len()].copy_from_slice(signs);
        }
        Ok(())
    }
}

#[allow(
    clippy::too_many_lines,
    reason = "Retain native sign correction, scale search, refit and byte packing in their source order."
)]
pub(super) fn quantize<const N: usize>(
    kind: u32,
    input: &[f32],
    importance: Option<&[f32]>,
    points: &[[u8; N]],
) -> Result<Vec<u8>> {
    let config = Configuration::new(kind)?;
    if input.is_empty()
        || !input.len().is_multiple_of(256)
        || input.iter().any(|value| !value.is_finite())
        || importance.is_some_and(|weights| {
            weights.len() != input.len()
                || weights
                    .iter()
                    .any(|value| !value.is_finite() || *value < 0.0)
        })
    {
        return Err(invalid(
            "IQ fitting needs complete finite blocks and nonnegative importance",
        ));
    }
    let mut grid = Grid::new(
        points,
        if kind == 22 { 2 } else { 3 },
        match kind {
            18 => 2,
            21 => 3,
            _ => 1,
        },
    )?;
    let bytes = (input.len() / 256)
        .checked_mul(config.bytes)
        .ok_or_else(|| invalid("IQ fitting byte extent overflow"))?;
    let mut output = Vec::new();
    output.try_reserve_exact(bytes)?;
    output.resize(bytes, 0);
    for (block_index, (input, output)) in input
        .as_chunks::<256>()
        .0
        .iter()
        .zip(output.chunks_exact_mut(config.bytes))
        .enumerate()
    {
        let mut sum_x2 = 0.0_f32;
        for &value in input {
            sum_x2 += value * value;
        }
        let sigma2 = finite(2.0 * sum_x2 / 256.0)?;
        let importance =
            importance.map(|weights| &weights[block_index * 256..(block_index + 1) * 256]);
        let mut scales = [0.0_f32; 16];
        let mut maximum_scale = 0.0_f32;
        let mut written_groups = 0;
        for (group, source) in input.chunks_exact(config.width).enumerate() {
            let mut values = [0.0_f32; 32];
            let mut weight = [0.0_f32; 32];
            let mut auxiliary_weight = [0.0_f32; 32];
            let mut signs = [0_u8; 4];
            for index in 0..config.width {
                let square = source[index] * source[index];
                weight[index] = finite(importance.map_or_else(
                    || {
                        if kind == 22 {
                            0.25 * sigma2 + square
                        } else {
                            square
                        }
                    },
                    |importance| {
                        importance[group * config.width + index] * (sigma2 + square).sqrt()
                    },
                ))?;
                auxiliary_weight[index] = weight[index].sqrt();
            }
            for (octet, source) in source.as_chunks::<8>().0.iter().enumerate() {
                for (lane, &value) in source.iter().enumerate() {
                    if value >= 0.0 {
                        values[octet * 8 + lane] = value;
                    } else {
                        values[octet * 8 + lane] = -value;
                        signs[octet] |= 1 << lane;
                    }
                }
                if kind == 18 && !signs[octet].count_ones().is_multiple_of(2) {
                    let mut minimum_index = 0;
                    let offset = octet * 8;
                    let mut minimum = weight[offset] * source[0] * source[0];
                    for index in 1..8 {
                        let candidate = weight[offset + index] * source[index] * source[index];
                        if candidate < minimum {
                            minimum = candidate;
                            minimum_index = index;
                        }
                    }
                    values[offset + minimum_index] = -values[offset + minimum_index];
                    signs[octet] ^= 1 << minimum_index;
                }
                if kind == 18 {
                    signs[octet] &= 127;
                }
            }
            let values = &values[..config.width];
            let weight = &weight[..config.width];
            let auxiliary_weight = &auxiliary_weight[..config.width];
            let maximum = values.iter().copied().fold(values[0], f32::max);
            if maximum == 0.0 || maximum < config.epsilon {
                continue;
            }
            let mut best = 0.0_f32;
            let mut scale = maximum / f32::from(2 * config.maximum + 1);
            let mut chosen = [0_u8; 32];
            let mut on_grid = [kind != 21; 8];
            for trial in -config.trials..=config.trials {
                let inverse =
                    (f32::from(2 * config.maximum + 1) + trial as f32 * config.delta) / maximum;
                let candidate_scale = finite(1.0 / inverse)?;
                let mut candidate = [0_u8; 32];
                let mut candidate_on_grid = [true; 8];
                levels(
                    values,
                    inverse,
                    config.maximum,
                    &mut candidate[..config.width],
                )?;
                for (index, levels) in candidate[..config.width]
                    .as_chunks_mut::<N>()
                    .0
                    .iter_mut()
                    .enumerate()
                {
                    candidate_on_grid[index] = grid.fit(
                        levels,
                        &values[index * N..(index + 1) * N],
                        &auxiliary_weight[index * N..(index + 1) * N],
                        candidate_scale,
                    )?;
                }
                let (sum_qx, sum_q2) = sums(&candidate[..config.width], values, weight)?;
                if sum_q2 > 0.0 && finite(sum_qx * sum_qx)? > finite(best * sum_q2)? {
                    scale = finite(sum_qx / sum_q2)?;
                    best = finite(scale * sum_qx)?;
                    chosen = candidate;
                    on_grid = candidate_on_grid;
                }
            }
            if on_grid[..config.width / N].iter().any(|&value| !value) && scale > 0.0 {
                let inverse = finite(1.0 / scale)?;
                for index in 0..config.width / N {
                    if kind != 21 && on_grid[index] {
                        continue;
                    }
                    let selected = &mut chosen[index * N..(index + 1) * N];
                    levels(
                        &values[index * N..(index + 1) * N],
                        inverse,
                        config.maximum,
                        selected,
                    )?;
                    grid.fit(
                        selected,
                        &values[index * N..(index + 1) * N],
                        &auxiliary_weight[index * N..(index + 1) * N],
                        scale,
                    )?;
                }
                let (sum_qx, sum_q2) = sums(&chosen[..config.width], values, weight)?;
                if sum_q2 > 0.0 {
                    scale = finite(sum_qx / sum_q2)?;
                }
            }
            if scale < 0.0 {
                scale = -scale;
                for sign in &mut signs[..config.width / 8] {
                    *sign = !*sign;
                    if kind == 18 {
                        *sign &= 127;
                    }
                }
            }
            config.pack(
                &grid,
                group,
                if kind == 21 { written_groups } else { group },
                &chosen[..config.width],
                &signs[..config.width / 8],
                output,
            )?;
            // Native IQ3_S advances its low-code/sign pointers only after a
            // nonzero group. High bits and scales still use the original group.
            // Preserve these defined bytes, including mixed zero/nonzero groups.
            written_groups += 1;
            scales[group] = scale;
            maximum_scale = maximum_scale.max(scale);
        }
        if maximum_scale == 0.0 {
            continue;
        }
        let d = maximum_scale / 31.0;
        let half = half_bits(finite(d * config.factor)?);
        finite(half_to_float(half))?;
        output[..2].copy_from_slice(&half.to_le_bytes());
        let inverse = finite(1.0 / d)?;
        for (group, &scale) in scales[..256 / config.width].iter().enumerate() {
            let level = nearest(0.5 * (inverse * scale - 1.0))?.clamp(0, 15) as u8;
            if kind == 18 {
                output[69 + group * 4] |= level << 4;
            } else {
                let offset = if kind == 21 { 106 } else { 74 };
                output[offset + group / 2] |= level << (4 * (group % 2));
            }
        }
    }
    Ok(output)
}