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;
43#[cfg(feature = "kv")]
44pub mod kv;
45pub mod lattice;
46pub mod lloyd;
47pub mod metrics;
48pub mod persistence;
49pub mod profile;
50pub mod receipt;
51pub mod residual;
52pub mod rope;
53pub mod rotation;
54pub mod scoring;
55pub mod sidecar;
56pub mod spherical_beta;
57pub mod wire;
58
59pub use batch_ingest::{BatchIngestPipeline, IngestReceipt};
60pub use codebook::{build_initial_codebook, FibCodebookV1};
61pub use codec::{
62    CompactFeatureFlags, FibCodeV1, FibQuantizer, GpuStepReport, CODEC_ID, COMPACT_MAGIC,
63    COMPACT_V2_MAGIC, COMPACT_V2_VERSION, COMPACT_VERSION,
64};
65pub use directions::{fibonacci_sphere_3d, fibonacci_spiral_2d, roberts_kronecker};
66pub use error::{FibQuantError, Result};
67pub use eval::{ndcg_at_k, recall_at_k, run_benchmark, FibBenchmarkCorpus, FibBenchmarkReceiptV1};
68pub use lattice::{quantize_a2_pairs, quantize_z1, LatticeKind, LatticeQuantizationResult};
69pub use lloyd::{LloydRepairEventV1, LloydReportV1};
70pub use persistence::{load_from_file, save_to_file, FibSidecarFileV1, FILE_MAGIC, FILE_VERSION};
71#[cfg(feature = "mmap")]
72pub use persistence::{load_mmap, MmapSidecarIndex};
73pub use profile::{
74    DirectionMethod, EmptyCellPolicy, FibQuantProfileV1, LloydMode, NormFormat, RadiusMethod,
75    SourceMode, MAX_AMBIENT_DIM, MAX_BLOCK_DIM, MAX_CODEBOOK_SIZE, MAX_CODEBOOK_VALUES,
76    MAX_PACKED_INDEX_BITS, MAX_ROTATION_MATRIX_VALUES, MAX_TRAINING_SAMPLES,
77};
78pub use receipt::FibQuantCompressionReceiptV1;
79pub use residual::{
80    FibMultiLevelQuantizer, FibResidualCodeV1, FibResidualQuantizer, MultiLevelCode,
81    MultiLevelResidualCodebookV1, ResidualCodebookV1,
82};
83pub use rope::{
84    allocate_rope_bits, rope_block_energies, rope_blocks, RopeBitAllocation, RopeBlock,
85    RopeBlockEnergy,
86};
87pub use rotation::{StoredRotation, ROTATION_ALGORITHM_VERSION, ROTATION_SCHEMA};
88pub use scoring::{FibPreparedQuery, FibScorer, GramTable, ScoredItem};
89pub use sidecar::{
90    FibSidecarIndex, IvfCoarseQuantizer, ScoredCandidate, SearchReceiptIvfV1, SearchReceiptV1,
91};
92pub use spherical_beta::{
93    beta_d_k, radius_quantile, radius_quantile_k2_closed_form, sample_reference_projection,
94    sample_spherical_beta,
95};
96pub use wire::{FibCodeWireV1, WireHeader, WIRE_HEADER_SIZE, WIRE_MAGIC, WIRE_VERSION};