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
//! Scalar reference kernels — the canonical "source of truth" that all
//! SIMD paths must match byte-for-byte (Phase 20).
//!
//! Each function in this module has a single responsibility:
//!
//! * [`quantize_into`] — nearest-entry quantization (delegates to
//! [`crate::codec::quantize::scalar_quantize`]).
//! * [`dequantize_into`] — gather-by-index decode (delegates to
//! [`crate::codec::quantize::scalar_dequantize`]).
//! * [`cosine`] — dot-product / norm cosine similarity. Mirrors
//! the `tinyquant-bruteforce` reference but
//! drops the debug-only NaN assertions so the
//! function remains `no_std`-compatible.
//! * [`compute_residual_into`] — in-place fp16 residual encoding.
//! * [`apply_residual_into`] — in-place fp16 residual decoding
//! (re-exports
//! [`crate::codec::residual::apply_residual_into`]).
use crate;
use crateapply_residual_into as residual_apply_impl;
use crateCodecError;
use f16;
/// Quantize `values` against `entries`, writing nearest-entry indices
/// into `indices`.
///
/// # Errors
///
/// Propagates [`CodecError::LengthMismatch`] from
/// [`crate::codec::quantize::scalar_quantize`].
/// Gather `entries[indices[i]]` into `values[i]`.
///
/// # Errors
///
/// Propagates errors from [`crate::codec::quantize::scalar_dequantize`].
/// Compute cosine similarity between two equal-length `f32` slices.
///
/// Returns `0.0` when either vector is the zero vector (denominator
/// equals zero) **or** when either input contains a NaN. The NaN early
/// return makes the Phase 20 parity contract trivially satisfiable by
/// every dispatched path — SIMD kernels that sum NaN through FMA would
/// otherwise propagate NaN into the numerator and produce a divergent
/// bit pattern vs the scalar reference.
///
/// This function is `no_std`-compatible and deliberately omits the
/// debug-only panics that live in
/// `tinyquant-bruteforce::similarity::cosine_similarity`.
/// Compute `original - reconstructed` as little-endian fp16 bytes,
/// writing the 2 * N byte result into the pre-allocated `out` slice.
///
/// Mirrors [`crate::codec::residual::compute_residual`] exactly, but
/// writes into a caller-provided buffer instead of allocating a fresh
/// [`alloc::vec::Vec`].
///
/// # Panics
///
/// Debug-only: panics when `original.len() != reconstructed.len()` or
/// when `out.len() != original.len() * 2`. In release builds the short
/// iterator consumes whichever slice runs out first, which is an
/// implementation-defined but panic-free fallback.
/// Decode `residual` as fp16 LE and add in place into `values`.
///
/// # Errors
///
/// Propagates [`CodecError::LengthMismatch`] from
/// [`crate::codec::residual::apply_residual_into`].