Skip to main content

fib_quant/
lib.rs

1#![warn(rustdoc::broken_intra_doc_links)]
2
3//! Experimental paper-core FibQuant math crate.
4//!
5//! This crate implements the normalize, deterministic rotation,
6//! spherical-Beta block source, radial-angular codebook, Lloyd-Max refinement,
7//! and fixed-rate codec path described in `FibQuant: Universal Vector
8//! Quantization for Random-Access KV-Cache Compression`.
9//!
10//! The `0.1.0-alpha.1` surface is deliberately narrow. It is not a production
11//! KV-cache compressor, not a benchmark reproduction package, and not
12//! integrated with any parent workspace memory crate. Profiles are validated
13//! against explicit alpha resource limits before allocation-heavy paths run.
14//!
15//! ```
16//! use fib_quant::{FibQuantProfileV1, FibQuantizer};
17//!
18//! # fn main() -> fib_quant::Result<()> {
19//! let mut profile = FibQuantProfileV1::paper_default(8, 2, 8, 7)?;
20//! profile.training_samples = 128;
21//! profile.lloyd_restarts = 1;
22//! profile.lloyd_iterations = 2;
23//! let quantizer = FibQuantizer::new(profile)?;
24//! let input = vec![0.25, -0.5, 0.75, 1.0, -1.25, 0.5, 0.125, -0.875];
25//! let code = quantizer.encode(&input)?;
26//! let decoded = quantizer.decode(&code)?;
27//! assert_eq!(decoded.len(), input.len());
28//! # Ok(())
29//! # }
30//! ```
31
32pub mod batch_ingest;
33pub mod beta_inv;
34pub mod bitpack;
35pub mod codebook;
36pub mod codec;
37#[cfg(feature = "compat")]
38pub mod compat;
39pub mod digest;
40pub mod directions;
41pub mod error;
42pub mod eval;
43pub mod ffi;
44#[cfg(feature = "kv")]
45pub mod kv;
46pub mod lattice;
47pub mod lloyd;
48pub mod metrics;
49pub mod persistence;
50pub mod profile;
51pub mod receipt;
52pub mod residual;
53pub mod rope;
54pub mod rotation;
55pub mod scoring;
56pub mod sidecar;
57pub mod spherical_beta;
58pub mod wire;
59
60// Archived Rust implementations of hot paths replaced by C kernels.
61// Not compiled — kept for historical reference.
62#[allow(unused)]
63mod archive;
64
65pub use batch_ingest::{BatchIngestPipeline, IngestReceipt};
66pub use codebook::{build_initial_codebook, FibCodebookV1};
67pub use codec::{
68    CompactFeatureFlags, FibCodeV1, FibQuantizer, GpuStepReport, CODEC_ID, COMPACT_MAGIC,
69    COMPACT_V2_MAGIC, COMPACT_V2_VERSION, COMPACT_VERSION,
70};
71pub use directions::{fibonacci_sphere_3d, fibonacci_spiral_2d, roberts_kronecker};
72pub use error::{FibQuantError, Result};
73pub use eval::{ndcg_at_k, recall_at_k, run_benchmark, FibBenchmarkCorpus, FibBenchmarkReceiptV1};
74pub use lattice::{quantize_a2_pairs, quantize_z1, LatticeKind, LatticeQuantizationResult};
75pub use lloyd::{LloydRepairEventV1, LloydReportV1};
76pub use persistence::{load_from_file, save_to_file, FibSidecarFileV1, FILE_MAGIC, FILE_VERSION};
77#[cfg(feature = "mmap")]
78pub use persistence::{load_mmap, MmapSidecarIndex};
79pub use profile::{
80    DirectionMethod, EmptyCellPolicy, FibQuantProfileV1, LloydMode, NormFormat, RadiusMethod,
81    SourceMode, MAX_AMBIENT_DIM, MAX_BLOCK_DIM, MAX_CODEBOOK_SIZE, MAX_CODEBOOK_VALUES,
82    MAX_PACKED_INDEX_BITS, MAX_ROTATION_MATRIX_VALUES, MAX_TRAINING_SAMPLES,
83};
84pub use receipt::FibQuantCompressionReceiptV1;
85pub use residual::{
86    FibMultiLevelQuantizer, FibResidualCodeV1, FibResidualQuantizer, MultiLevelCode,
87    MultiLevelResidualCodebookV1, ResidualCodebookV1,
88};
89pub use rope::{
90    allocate_rope_bits, rope_block_energies, rope_blocks, RopeBitAllocation, RopeBlock,
91    RopeBlockEnergy,
92};
93pub use rotation::{StoredRotation, ROTATION_ALGORITHM_VERSION, ROTATION_SCHEMA};
94pub use scoring::{FibPreparedQuery, FibScorer, GramTable, ScoredItem};
95pub use sidecar::{
96    FibSidecarIndex, IvfCoarseQuantizer, ScoredCandidate, SearchReceiptIvfV1, SearchReceiptV1,
97};
98pub use spherical_beta::{
99    beta_d_k, radius_quantile, radius_quantile_k2_closed_form, sample_reference_projection,
100    sample_spherical_beta,
101};
102pub use wire::{FibCodeWireV1, WireHeader, WIRE_HEADER_SIZE, WIRE_MAGIC, WIRE_VERSION};