tinyquant-core 1.1.0

CPU-only vector quantization codec — core types, codec, corpus, and backend trait (no_std).
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
//! Stateless `Codec` service (Phase 15).
//!
//! Pipeline mirrors `tinyquant_cpu.codec.Codec` exactly:
//!
//! - **compress**: rotate → quantize → (optional) residual on rotated vs reconstructed
//! - **decompress**: dequantize → (optional) add residual → inverse rotate
//!
//! Phase 26 adds `compress_prepared` / `decompress_prepared_into` which accept a
//! pre-built [`PreparedCodec`] so the `O(dim²)` rotation factorization is paid
//! only once per session rather than on every call.

use crate::codec::{
    codebook::Codebook,
    codec_config::CodecConfig,
    compressed_vector::CompressedVector,
    parallelism::Parallelism,
    prepared::PreparedCodec,
    residual::{apply_residual_into, compute_residual},
    rotation_matrix::RotationMatrix,
};
use crate::errors::CodecError;
use alloc::{vec, vec::Vec};

/// Zero-sized stateless codec service. Mirrors Python `tinyquant_cpu.codec.Codec`.
#[derive(Default, Debug, Clone, Copy)]
pub struct Codec;

impl Codec {
    /// Create a new `Codec` instance (zero allocation).
    #[must_use]
    pub const fn new() -> Self {
        Self
    }

    /// Compress a single vector. `vector.len()` must equal `config.dimension()`.
    ///
    /// Pipeline: rotate → quantize → (optional) residual.
    ///
    /// # Errors
    ///
    /// - [`CodecError::DimensionMismatch`] if `vector.len() != config.dimension()`
    /// - [`CodecError::CodebookIncompatible`] if `codebook.bit_width() != config.bit_width()`
    pub fn compress(
        &self,
        vector: &[f32],
        config: &CodecConfig,
        codebook: &Codebook,
    ) -> Result<CompressedVector, CodecError> {
        let dim = config.dimension() as usize;
        if vector.len() != dim {
            // Dimension is bounded by u32 (from CodecConfig); cast is safe in practice.
            #[allow(clippy::cast_possible_truncation)]
            let got = vector.len() as u32;
            return Err(CodecError::DimensionMismatch {
                expected: config.dimension(),
                got,
            });
        }
        if codebook.bit_width() != config.bit_width() {
            return Err(CodecError::CodebookIncompatible {
                expected: config.bit_width(),
                got: codebook.bit_width(),
            });
        }
        let rotation = RotationMatrix::from_config(config);
        Self::compress_with_rotation(vector, config, codebook, &rotation)
    }

    /// Compress using a pre-built [`PreparedCodec`] (hot path — rotation not rebuilt).
    ///
    /// Identical output to [`Self::compress`] for the same inputs.
    ///
    /// # Errors
    ///
    /// [`CodecError::DimensionMismatch`] if `vector.len() != prepared.config().dimension()`.
    pub fn compress_prepared(
        &self,
        vector: &[f32],
        prepared: &PreparedCodec,
    ) -> Result<CompressedVector, CodecError> {
        let dim = prepared.config().dimension() as usize;
        if vector.len() != dim {
            #[allow(clippy::cast_possible_truncation)]
            let got = vector.len() as u32;
            return Err(CodecError::DimensionMismatch {
                expected: prepared.config().dimension(),
                got,
            });
        }
        Self::compress_with_rotation(
            vector,
            prepared.config(),
            prepared.codebook(),
            prepared.rotation(),
        )
    }

    /// Inner compute path — caller supplies an already-built rotation.
    ///
    /// Preconditions (asserted by callers):
    /// - `vector.len() == config.dimension()`
    /// - `codebook.bit_width() == config.bit_width()`
    fn compress_with_rotation(
        vector: &[f32],
        config: &CodecConfig,
        codebook: &Codebook,
        rotation: &RotationMatrix,
    ) -> Result<CompressedVector, CodecError> {
        let dim = config.dimension() as usize;
        let mut rotated = vec![0.0_f32; dim];
        rotation.apply_into(vector, &mut rotated)?;

        let mut indices = vec![0_u8; dim];
        codebook.quantize_into(&rotated, &mut indices)?;

        let residual = if config.residual_enabled() {
            let mut reconstructed = vec![0.0_f32; dim];
            codebook.dequantize_into(&indices, &mut reconstructed)?;
            Some(compute_residual(&rotated, &reconstructed).into_boxed_slice())
        } else {
            None
        };

        CompressedVector::new(
            indices.into_boxed_slice(),
            residual,
            config.config_hash().clone(),
            config.dimension(),
            config.bit_width(),
        )
    }

    /// Allocating decompress — returns a new `Vec<f32>`.
    ///
    /// # Errors
    ///
    /// Propagates errors from [`Self::decompress_into`].
    pub fn decompress(
        &self,
        compressed: &CompressedVector,
        config: &CodecConfig,
        codebook: &Codebook,
    ) -> Result<Vec<f32>, CodecError> {
        let mut out = vec![0.0_f32; config.dimension() as usize];
        self.decompress_into(compressed, config, codebook, &mut out)?;
        Ok(out)
    }

    /// In-place decompress into caller-supplied buffer.
    ///
    /// Pipeline: dequantize → (optional) apply residual → inverse rotate.
    ///
    /// # Errors
    ///
    /// - [`CodecError::ConfigMismatch`] if `compressed.config_hash() != config.config_hash()`
    /// - [`CodecError::CodebookIncompatible`] on bit-width mismatch
    /// - [`CodecError::DimensionMismatch`] if `output.len() != config.dimension()`
    pub fn decompress_into(
        &self,
        compressed: &CompressedVector,
        config: &CodecConfig,
        codebook: &Codebook,
        output: &mut [f32],
    ) -> Result<(), CodecError> {
        if compressed.config_hash() != config.config_hash() {
            return Err(CodecError::ConfigMismatch {
                expected: config.config_hash().clone(),
                got: compressed.config_hash().clone(),
            });
        }
        if compressed.bit_width() != config.bit_width() {
            return Err(CodecError::CodebookIncompatible {
                expected: config.bit_width(),
                got: compressed.bit_width(),
            });
        }
        if codebook.bit_width() != config.bit_width() {
            return Err(CodecError::CodebookIncompatible {
                expected: config.bit_width(),
                got: codebook.bit_width(),
            });
        }
        if output.len() != config.dimension() as usize {
            #[allow(clippy::cast_possible_truncation)]
            let got = output.len() as u32;
            return Err(CodecError::DimensionMismatch {
                expected: config.dimension(),
                got,
            });
        }
        let rotation = RotationMatrix::from_config(config);
        Self::decompress_into_with_rotation(compressed, codebook, &rotation, output)
    }

    /// Decompress into a caller-allocated buffer using a pre-built [`PreparedCodec`]
    /// (hot path — rotation not rebuilt).
    ///
    /// Identical output to [`Self::decompress_into`] for the same inputs.
    ///
    /// # Errors
    ///
    /// - [`CodecError::ConfigMismatch`] if `cv.config_hash() != prepared.config().config_hash()`
    /// - [`CodecError::DimensionMismatch`] if `out.len() != prepared.config().dimension()`
    pub fn decompress_prepared_into(
        &self,
        cv: &CompressedVector,
        prepared: &PreparedCodec,
        out: &mut [f32],
    ) -> Result<(), CodecError> {
        if cv.config_hash() != prepared.config().config_hash() {
            return Err(CodecError::ConfigMismatch {
                expected: prepared.config().config_hash().clone(),
                got: cv.config_hash().clone(),
            });
        }
        // cv.bit_width() is not checked separately — the config_hash equality
        // above already covers all config fields including bit_width.  The
        // codebook bit_width is validated once at PreparedCodec::new time.
        if out.len() != prepared.config().dimension() as usize {
            #[allow(clippy::cast_possible_truncation)]
            let got = out.len() as u32;
            return Err(CodecError::DimensionMismatch {
                expected: prepared.config().dimension(),
                got,
            });
        }
        Self::decompress_into_with_rotation(cv, prepared.codebook(), prepared.rotation(), out)
    }

    /// Inner compute path — caller supplies an already-built rotation.
    ///
    /// Preconditions (asserted by callers):
    /// - `output.len() == config dimension`
    /// - `codebook` is compatible with the compressed vector
    fn decompress_into_with_rotation(
        compressed: &CompressedVector,
        codebook: &Codebook,
        rotation: &RotationMatrix,
        output: &mut [f32],
    ) -> Result<(), CodecError> {
        let mut rotated = vec![0.0_f32; output.len()];
        codebook.dequantize_into(compressed.indices(), &mut rotated)?;

        if let Some(residual) = compressed.residual() {
            apply_residual_into(&mut rotated, residual)?;
        }

        rotation.apply_inverse_into(&rotated, output)
    }

    /// Row-major batch compress using the serial strategy.
    ///
    /// # Errors
    ///
    /// - [`CodecError::DimensionMismatch`] if `cols != config.dimension()`
    /// - [`CodecError::LengthMismatch`] if `vectors.len() != rows * cols`
    pub fn compress_batch(
        &self,
        vectors: &[f32],
        rows: usize,
        cols: usize,
        config: &CodecConfig,
        codebook: &Codebook,
    ) -> Result<Vec<CompressedVector>, CodecError> {
        self.compress_batch_with(vectors, rows, cols, config, codebook, Parallelism::Serial)
    }

    /// Row-major batch compress with explicit parallelism strategy.
    ///
    /// Phase 21: honours `parallelism`. `Serial` runs the existing single-threaded
    /// loop; `Custom(driver)` uses the `MaybeUninit + AtomicPtr<CompressedVector>` parallel path in
    /// `batch.rs` (requires the `std` feature).
    ///
    /// The determinism contract guarantees byte-identical output regardless of
    /// the driver or thread count (see `batch.rs` module doc).
    ///
    /// # Errors
    ///
    /// Same as [`Self::compress_batch`].
    pub fn compress_batch_with(
        &self,
        vectors: &[f32],
        rows: usize,
        cols: usize,
        config: &CodecConfig,
        codebook: &Codebook,
        parallelism: Parallelism,
    ) -> Result<Vec<CompressedVector>, CodecError> {
        if cols != config.dimension() as usize {
            #[allow(clippy::cast_possible_truncation)]
            let got = cols as u32;
            return Err(CodecError::DimensionMismatch {
                expected: config.dimension(),
                got,
            });
        }
        let expected_len = rows.checked_mul(cols).ok_or(CodecError::LengthMismatch {
            left: vectors.len(),
            right: usize::MAX,
        })?;
        if vectors.len() != expected_len {
            return Err(CodecError::LengthMismatch {
                left: vectors.len(),
                right: expected_len,
            });
        }
        // Delegate to the parallel batch module when `std` is available.
        #[cfg(feature = "std")]
        {
            crate::codec::batch::compress_batch_parallel(
                vectors,
                rows,
                cols,
                config,
                codebook,
                parallelism,
            )
        }
        // no_std fallback: always serial regardless of `parallelism` argument.
        #[cfg(not(feature = "std"))]
        {
            let _ = parallelism;
            let mut out = Vec::with_capacity(rows);
            // Safety: vectors.len() == rows * cols (checked above); slices are in-bounds.
            #[allow(clippy::indexing_slicing)]
            for row in 0..rows {
                let start = row * cols;
                out.push(self.compress(&vectors[start..start + cols], config, codebook)?);
            }
            Ok(out)
        }
    }

    /// Batch decompress into a contiguous row-major `output` buffer.
    ///
    /// # Errors
    ///
    /// - [`CodecError::LengthMismatch`] if `output.len() != compressed.len() * config.dimension()`
    /// - Propagates per-vector decompress errors.
    pub fn decompress_batch_into(
        &self,
        compressed: &[CompressedVector],
        config: &CodecConfig,
        codebook: &Codebook,
        output: &mut [f32],
    ) -> Result<(), CodecError> {
        let cols = config.dimension() as usize;
        let needed = compressed.len() * cols;
        if output.len() != needed {
            return Err(CodecError::LengthMismatch {
                left: output.len(),
                right: needed,
            });
        }
        // Safety: output.len() == compressed.len() * cols (checked above); slices are in-bounds.
        #[allow(clippy::indexing_slicing)]
        for (row, cv) in compressed.iter().enumerate() {
            let start = row * cols;
            self.decompress_into(cv, config, codebook, &mut output[start..start + cols])?;
        }
        Ok(())
    }
}

/// Minimum batch size below which GPU offload is not attempted.
///
/// Below this threshold the host↔device transfer overhead exceeds the
/// compute savings.  Mirrors `tinyquant_gpu_wgpu::GPU_BATCH_THRESHOLD`.
pub const GPU_BATCH_THRESHOLD: usize = 512;

/// Trait that every `TinyQuant` GPU compute backend must satisfy.
///
/// Implementations of this trait live in external crates (e.g.
/// `tinyquant-gpu-wgpu`). The trait is defined here so
/// `tinyquant-core` can express the `compress_batch_gpu_with` method
/// without a dependency on any concrete GPU crate (which would create
/// a cyclic crate dependency, since GPU crates already depend on
/// `tinyquant-core`).
pub trait GpuComputeBackend {
    /// The error type returned by this backend's operations.
    ///
    /// Must be convertible to [`CodecError`] so
    /// [`Codec::compress_batch_gpu_with`] can map errors uniformly.
    type Error: core::fmt::Debug + Into<crate::errors::CodecError>;

    /// Upload `PreparedCodec` buffers to device memory. Idempotent.
    ///
    /// # Errors
    ///
    /// Returns `Self::Error` if device upload fails.
    fn prepare_for_device(&mut self, prepared: &mut PreparedCodec) -> Result<(), Self::Error>;

    /// Compress `rows` FP32 vectors of dimension `cols` on the GPU.
    ///
    /// # Errors
    ///
    /// Returns `Self::Error` if the GPU kernel dispatch or readback fails.
    fn compress_batch(
        &mut self,
        input: &[f32],
        rows: usize,
        cols: usize,
        prepared: &PreparedCodec,
    ) -> Result<alloc::vec::Vec<CompressedVector>, Self::Error>;
}

#[cfg(feature = "gpu-wgpu")]
impl Codec {
    /// Batch compress with automatic GPU routing.
    ///
    /// When `rows >= GPU_BATCH_THRESHOLD` (currently 512), the batch is
    /// dispatched to `backend` via the [`GpuComputeBackend`] trait.
    /// Otherwise the call falls through to the CPU path using `parallelism`.
    ///
    /// # Caller responsibility
    ///
    /// `backend` must have been created and initialized by the caller (which
    /// may require an async executor, depending on the backend). This method
    /// only calls synchronous trait methods on the backend.
    ///
    /// `prepared` is passed by mutable reference so `prepare_for_device` can
    /// attach GPU-resident state (idempotent — safe to call before every batch).
    ///
    /// # Async-context safety
    ///
    /// Do **not** call this function from an async context that owns an active
    /// tokio or other executor on the same thread. `wgpu` may internally call
    /// `device.poll(wgpu::Maintain::Wait)`, which blocks the thread.
    ///
    /// # Errors
    ///
    /// - [`CodecError::GpuUnavailable`] if `prepare_for_device` fails.
    /// - [`CodecError::GpuError`] if the GPU kernel dispatch or readback fails.
    /// - All errors from [`Codec::compress_batch_with`] when the CPU fallback
    ///   is taken.
    pub fn compress_batch_gpu_with<B>(
        &self,
        vectors: &[f32],
        rows: usize,
        cols: usize,
        prepared: &mut PreparedCodec,
        backend: &mut B,
        parallelism: Parallelism,
    ) -> Result<Vec<CompressedVector>, CodecError>
    where
        B: GpuComputeBackend,
    {
        if rows >= GPU_BATCH_THRESHOLD {
            backend.prepare_for_device(prepared).map_err(Into::into)?;
            backend
                .compress_batch(vectors, rows, cols, prepared)
                .map_err(Into::into)
        } else {
            self.compress_batch_with(
                vectors,
                rows,
                cols,
                prepared.config(),
                prepared.codebook(),
                parallelism,
            )
        }
    }
}

/// Module-level `compress` free function — mirrors `tinyquant_cpu.codec.compress`.
///
/// # Errors
///
/// Propagates errors from [`Codec::compress`].
pub fn compress(
    vector: &[f32],
    config: &CodecConfig,
    codebook: &Codebook,
) -> Result<CompressedVector, CodecError> {
    Codec::new().compress(vector, config, codebook)
}

/// Module-level `decompress` free function — mirrors `tinyquant_cpu.codec.decompress`.
///
/// # Errors
///
/// Propagates errors from [`Codec::decompress`].
pub fn decompress(
    compressed: &CompressedVector,
    config: &CodecConfig,
    codebook: &Codebook,
) -> Result<Vec<f32>, CodecError> {
    Codec::new().decompress(compressed, config, codebook)
}