prism-inference 0.1.16

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
//! Bounded multi-token projection with a single decoded packed weight block.
//!
//! The block/row loop surrounds the token loop. Unlike repeated matvec calls,
//! every PQ2/PTQ1 block is decoded once for the entire tile. Each token retains
//! its original activation quantizer, ordered block accumulation and pinned
//! PTQ1 four-lane reduction. No decoded matrix survives this call.

#![allow(
    clippy::redundant_pub_crate,
    reason = "Keep graph-consumed tile types crate scoped inside the private kernel module."
)]

use super::{
    MatvecBackend, MatvecScratch, PQ2, PTQ1, RowParallelism, dimensions, finite, integer_float,
    matvec_into, ptq1_values, scale,
};
use crate::{Result, invalid, model::Tensor};

#[path = "prefill_dot.rs"]
mod dot;

pub(crate) const MAX_TILE_TOKENS: usize = 32;
pub(crate) const ROW_TASK_SCRATCH_BYTES: usize = 16 * 1024;

#[derive(Default)]
pub(crate) struct ProjectionTile {
    backend: MatvecBackend,
    preparations: Vec<MatvecScratch>,
    row_parallelism: RowParallelism,
    pub(crate) packed_projections: u64,
    pub(crate) decoded_blocks: u64,
}

impl ProjectionTile {
    pub(crate) fn with_backend(backend: MatvecBackend) -> Self {
        Self {
            backend,
            ..Self::default()
        }
    }

    #[cfg_attr(
        not(feature = "parallel-rows"),
        allow(
            clippy::missing_const_for_fn,
            reason = "The parallel-rows build drops the replaced pool owner; retain one non-const signature across feature sets."
        )
    )]
    pub(crate) fn set_row_parallelism(&mut self, parallelism: RowParallelism) {
        self.row_parallelism = parallelism;
    }

    #[allow(
        clippy::suboptimal_flops,
        clippy::too_many_lines,
        reason = "Retain the separately rounded source arithmetic and auditable packed block order."
    )]
    pub(crate) fn project(
        &mut self,
        tensor: &Tensor,
        inputs: &[Vec<f32>],
        outputs: &mut [Vec<f32>],
        pinned: bool,
        check: &dyn Fn() -> Result<()>,
    ) -> Result<()> {
        for output in outputs.iter_mut() {
            output.clear();
        }
        let result = (|| {
            let (columns, rows, block_bytes) = dimensions(tensor)?;
            if inputs.is_empty() || inputs.len() > MAX_TILE_TOKENS || outputs.len() != inputs.len()
            {
                return Err(invalid("invalid bounded projection tile"));
            }
            while self.preparations.len() < inputs.len() {
                self.preparations.try_reserve_exact(1)?;
                self.preparations.push(if pinned {
                    MatvecScratch::with_pinned_backend(self.backend)
                } else {
                    MatvecScratch::with_backend(self.backend)
                });
            }
            for ((scratch, input), output) in self
                .preparations
                .iter_mut()
                .zip(inputs)
                .zip(outputs.iter_mut())
            {
                // Preparation also validates every input before publishing any
                // tile output, including unsupported-format fallback inputs.
                check()?;
                scratch.pinned_accumulation = pinned;
                scratch.prepare_input(tensor, input)?;
                output.try_reserve_exact(rows)?;
            }
            if !matches!(tensor.kind, PQ2 | PTQ1) {
                for ((input, output), scratch) in inputs
                    .iter()
                    .zip(outputs.iter_mut())
                    .zip(self.preparations.iter_mut())
                {
                    check()?;
                    matvec_into(tensor, input, output, scratch)?;
                }
                return Ok(());
            }
            self.packed_projections = self.packed_projections.saturating_add(1);
            let backend = self.backend;
            let preparations = &self.preparations[..inputs.len()];
            self.row_parallelism.fill_tile(
                &tensor.data,
                block_bytes,
                outputs,
                &mut self.decoded_blocks,
                check,
                |row, decoded_blocks| {
                    let mut sums = [0.0_f32; MAX_TILE_TOKENS];
                    let mut lanes = [[0.0_f32; 4]; MAX_TILE_TOKENS];
                    let bytes = if tensor.kind == PQ2 { 34 } else { 28 };
                    for (index, block) in row.chunks_exact(bytes).enumerate() {
                        let weight_scale = if tensor.kind == PQ2 {
                            scale(block)?
                        } else {
                            scale(&block[26..])?
                        };
                        // Prepare one SIMD/scalar block for all tokens. This stays
                        // stack-scoped and never expands or retains a matrix.
                        let weights = if tensor.kind == PQ2 {
                            dot::Block::from_pq2(block[2..34].try_into()?, backend)
                        } else {
                            dot::Block::new(ptq1_values(block)?, backend)
                        };
                        *decoded_blocks = decoded_blocks.saturating_add(1);
                        for (token, scratch) in preparations.iter().enumerate() {
                            if tensor.kind == PQ2 && columns.is_multiple_of(256) {
                                let activation = &scratch.q8k[index / 2];
                                let start = (index % 2) * 128;
                                let d = weight_scale * activation.scale;
                                let dot = integer_float(
                                    weights
                                        .dot128(activation.values[start..start + 128].try_into()?),
                                );
                                sums[token] = if pinned {
                                    d.mul_add(dot, sums[token])
                                } else {
                                    sums[token] + d * dot
                                };
                            } else {
                                let activations = &scratch.q8[index * 4..index * 4 + 4];
                                let mut sum = 0.0;
                                for (group, activation) in activations.iter().enumerate() {
                                    if tensor.kind == PTQ1 && pinned {
                                        let d = weight_scale * activation.scale;
                                        for (lane, integer) in lanes[token]
                                            .iter_mut()
                                            .zip(weights.lanes32(group, &activation.values))
                                        {
                                            *lane = integer_float(integer).mul_add(d, *lane);
                                        }
                                    } else {
                                        sum += activation.scale
                                            * integer_float(
                                                weights.dot32(group, &activation.values),
                                            );
                                    }
                                }
                                if tensor.kind != PTQ1 || !pinned {
                                    sums[token] += weight_scale * sum;
                                }
                            }
                        }
                    }
                    for (sum, lanes) in sums.iter_mut().zip(lanes).take(inputs.len()) {
                        *sum = finite(if tensor.kind == PTQ1 && pinned {
                            (lanes[0] + lanes[1]) + (lanes[2] + lanes[3])
                        } else {
                            *sum
                        })?;
                    }
                    Ok(sums)
                },
            )
        })();
        if result.is_err() {
            for output in outputs {
                output.clear();
            }
        }
        result
    }
}

#[cfg(test)]
mod tests {
    use std::cell::Cell;
    use std::sync::Arc;

    use super::*;

    #[cfg(feature = "parallel-rows")]
    #[test]
    #[allow(
        clippy::too_many_lines,
        reason = "Keep exact format/backend/tile parity and malformed-row recovery in one control."
    )]
    fn tiled_parallel_projection_matches_serial_and_recovers() -> Result<()> {
        for workers in [2, 4] {
            let parallelism = RowParallelism::new(workers)?;
            for (kind, columns) in [(PQ2, 256), (PQ2, 384), (PTQ1, 256)] {
                let mut packed = [if kind == PTQ1 { 0x66 } else { 0xe4 }; 34];
                packed[..2].copy_from_slice(&0x3555_u16.to_le_bytes());
                let block = if kind == PTQ1 {
                    bonsai_ternary::pack_ptq(&packed)?.to_vec()
                } else {
                    packed.to_vec()
                };
                let mut tensor = Tensor {
                    name: "parallel-packed-tile".into(),
                    dimensions: vec![columns, 7],
                    kind,
                    data: Arc::from(block.repeat(columns / 128 * 7)),
                };
                for count in [8, 16, 32] {
                    let inputs: Vec<Vec<f32>> = (0..count)
                        .map(|token| {
                            (0..columns)
                                .map(|index| {
                                    [1.5, -128.0, 0.0, -0.0, 63.5, -3.25][(index + token) % 6]
                                })
                                .collect()
                        })
                        .collect();
                    for backend in [
                        MatvecBackend::Scalar,
                        MatvecBackend::Auto,
                        MatvecBackend::ExperimentalNeon,
                    ] {
                        let mut tile = ProjectionTile::with_backend(backend);
                        tile.set_row_parallelism(parallelism.clone());
                        for pinned in [false, true] {
                            let mut actual = vec![vec![f32::NAN]; count];
                            let previous_blocks = tile.decoded_blocks;
                            tile.project(&tensor, &inputs, &mut actual, pinned, &|| Ok(()))?;
                            assert_eq!(
                                tile.decoded_blocks - previous_blocks,
                                u64::try_from(columns / 128 * 7)?
                            );
                            for (input, output) in inputs.iter().zip(&actual) {
                                let mut scratch = if pinned {
                                    MatvecScratch::with_pinned_backend(MatvecBackend::Scalar)
                                } else {
                                    MatvecScratch::with_backend(MatvecBackend::Scalar)
                                };
                                let mut expected = Vec::new();
                                matvec_into(&tensor, input, &mut expected, &mut scratch)?;
                                assert_eq!(
                                    output
                                        .iter()
                                        .map(|value| value.to_bits())
                                        .collect::<Vec<_>>(),
                                    expected
                                        .iter()
                                        .map(|value| value.to_bits())
                                        .collect::<Vec<_>>()
                                );
                            }
                            let valid = tensor.data.clone();
                            let mut bad = valid.to_vec();
                            let offset =
                                bad.len() - block.len() + if kind == PTQ1 { 26 } else { 0 };
                            bad[offset..offset + 2].copy_from_slice(&0x7c00_u16.to_le_bytes());
                            tensor.data = Arc::from(bad);
                            assert!(
                                tile.project(&tensor, &inputs, &mut actual, pinned, &|| Ok(()))
                                    .is_err()
                            );
                            assert!(actual.iter().all(Vec::is_empty));
                            tensor.data = valid;
                            tile.project(&tensor, &inputs, &mut actual, pinned, &|| Ok(()))?;
                            assert!(actual.iter().all(|output| output.len() == 7));
                        }
                        assert_eq!(tile.row_parallelism.workers(), workers);
                    }
                }
            }
        }
        Ok(())
    }

    #[test]
    fn tiled_packed_projection_reuses_blocks_and_preserves_independent_rows() -> Result<()> {
        for (kind, columns) in [(142, 256), (142, 384), (143, 256)] {
            let mut block = [if kind == 143 { 0x66 } else { 0xe4 }; 34];
            block[..2].copy_from_slice(&0x3555_u16.to_le_bytes());
            let encoded = if kind == 143 {
                bonsai_ternary::pack_ptq(&block)?.to_vec()
            } else {
                block.to_vec()
            };
            let tensor = Tensor {
                name: "tile-packed-reference".into(),
                dimensions: vec![columns, 3],
                kind,
                data: Arc::from(encoded.repeat(columns / 128 * 3)),
            };
            for count in [1, 8, 16, 32] {
                let inputs: Vec<Vec<f32>> = (0..count)
                    .map(|token| {
                        (0..columns)
                            .map(|index| [1.5, -128.0, 0.0, -0.0, 63.5, -3.25][(index + token) % 6])
                            .collect()
                    })
                    .collect();
                for backend in [
                    MatvecBackend::Scalar,
                    MatvecBackend::Auto,
                    MatvecBackend::ExperimentalNeon,
                ] {
                    let mut tiled = ProjectionTile::with_backend(backend);
                    for (pass, pinned) in [false, true, false].into_iter().enumerate() {
                        let mut actual = vec![vec![f32::NAN]; count];
                        tiled.project(&tensor, &inputs, &mut actual, pinned, &|| Ok(()))?;
                        assert_eq!(
                            tiled.decoded_blocks,
                            u64::try_from(columns / 128 * 3 * (pass + 1))?
                        );
                        assert!(tiled.preparations.iter().all(|scratch| {
                            scratch.requested_backend() == backend
                                && scratch.pinned_accumulation == pinned
                                && scratch.pq2_consumption.capacity() == 0
                        }));
                        for (input, actual) in inputs.iter().zip(actual) {
                            let mut scratch = if pinned {
                                MatvecScratch::with_pinned_backend(MatvecBackend::Scalar)
                            } else {
                                MatvecScratch::with_backend(MatvecBackend::Scalar)
                            };
                            let mut expected = Vec::new();
                            matvec_into(&tensor, input, &mut expected, &mut scratch)?;
                            assert_eq!(
                                actual.into_iter().map(f32::to_bits).collect::<Vec<_>>(),
                                expected.into_iter().map(f32::to_bits).collect::<Vec<_>>()
                            );
                        }
                    }
                }
            }
        }
        Ok(())
    }

    #[test]
    fn tiled_projection_clears_all_outputs_on_late_rejection() {
        let tensor = Tensor {
            name: "reject".into(),
            dimensions: vec![128, 2],
            kind: 142,
            data: Arc::from([0_u8; 68]),
        };
        let mut scratch = ProjectionTile::default();
        for count in [0, 8, 33] {
            let mut inputs = vec![vec![0.0; 128]; count];
            if count == 8 {
                inputs[7][127] = f32::NAN;
            }
            let mut outputs = vec![vec![1.0]; count];
            assert!(
                scratch
                    .project(&tensor, &inputs, &mut outputs, true, &|| Ok(()))
                    .is_err()
            );
            assert!(outputs.iter().all(Vec::is_empty));
        }
    }

    #[test]
    fn tiled_projection_clears_completed_rows_on_cancel_and_bad_scale() -> Result<()> {
        for backend in [
            MatvecBackend::Scalar,
            MatvecBackend::Auto,
            MatvecBackend::ExperimentalNeon,
        ] {
            for kind in [PQ2, PTQ1] {
                let mut packed = [0x66; 34];
                packed[..2].copy_from_slice(&0x3555_u16.to_le_bytes());
                let block = if kind == PTQ1 {
                    bonsai_ternary::pack_ptq(&packed)?.to_vec()
                } else {
                    packed.to_vec()
                };
                let mut tensor = Tensor {
                    name: "late-tile-error".into(),
                    dimensions: vec![256, 2],
                    kind,
                    data: Arc::from(block.repeat(4)),
                };
                let inputs = vec![vec![1.5; 256]; 8];
                let mut output = vec![vec![1.0]; 8];
                let mut tile = ProjectionTile::with_backend(backend);
                let calls = Cell::new(0);
                assert!(
                    tile.project(&tensor, &inputs, &mut output, true, &|| {
                        calls.set(calls.get() + 1);
                        // Eight preparations, first row, then cancel at row two.
                        if calls.get() == 10 {
                            Err(invalid("cancel after completed tile row"))
                        } else {
                            Ok(())
                        }
                    })
                    .is_err()
                );
                assert_eq!(tile.decoded_blocks, 2);
                assert!(output.iter().all(Vec::is_empty));
                tile.project(&tensor, &inputs, &mut output, true, &|| Ok(()))?;
                assert!(output.iter().all(|row| row.len() == 2));
                let mut bad = tensor.data.to_vec();
                let offset = block.len() * 3 + if kind == PTQ1 { 26 } else { 0 };
                bad[offset..offset + 2].copy_from_slice(&0x7c00_u16.to_le_bytes());
                tensor.data = Arc::from(bad);
                assert!(
                    tile.project(&tensor, &inputs, &mut output, false, &|| Ok(()))
                        .is_err()
                );
                assert!(output.iter().all(Vec::is_empty));
            }
        }
        Ok(())
    }
}