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
//! 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
use crate::codec::{
codebook::Codebook,
codec_config::CodecConfig,
compressed_vector::CompressedVector,
parallelism::Parallelism,
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);
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 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)?;
}
let rotation = RotationMatrix::from_config(config);
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(())
}
}
/// 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)
}