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
/*
* fib_quant.h — C kernel declarations for fib-quant hot paths.
*
* These functions replace the inner numerical loops of the Rust
* encode/decode and compressed-attention paths with compiled C,
* compiled with -O3 -mavx2 -mfma via the `cc` crate in build.rs.
*
* The Rust side retains all validation, receipt generation, and
* orchestration — only the tight numerical inner loops delegate here.
*/
/*
* Encode a vector block: for each block of `block_dim` consecutive floats
* in `vec`, find the nearest codeword in `codebook` (row-major N×k) and
* store the index in `out_indices`.
*
* vec — input vector, length = dim
* dim — total dimension (must be divisible by block_dim)
* codebook — row-major codewords, length = codebook_size * block_dim
* codebook_size— number of codewords (N)
* block_dim — dimension of each block (k)
* out_indices — output array, length = dim / block_dim
*
* Returns the number of blocks encoded.
*/
size_t ;
/*
* Decode a vector block: for each index in `indices`, gather the
* corresponding codeword from `codebook` and write it to `out_vec`.
*
* indices — codeword indices, length = block_count
* block_count — number of blocks (= dim / block_dim)
* codebook — row-major codewords, length = codebook_size * block_dim
* codebook_size— number of codewords (N)
* block_dim — dimension of each block (k)
* out_vec — output vector, length = block_count * block_dim
*/
void ;
/*
* Compute compressed attention logits: for each key, sum the Gram table
* lookups for the (key_index, query_index) pair across all blocks, then
* scale by query_norm * stored_norm / sqrt(head_dim).
*
* key_indices — flattened key codeword indices (n_keys * n_blocks)
* n_keys — number of keys
* key_norms — stored norms for each key, length = n_keys
* gram_table — flat N×N f32 matrix, row-major
* gram_size — N (codebook size)
* query_indices — precomputed query codeword indices, length = n_blocks
* query_norm — L2 norm of the query
* n_blocks — number of blocks per vector
* out_logits — output, length = n_keys
*/
void ;
/*
* Numerically stable softmax with max-subtraction (f64 accumulator).
* Modifies `logits` in-place, replacing each element with exp(x-max)/sum.
*
* logits — array to softmax, length = n
* n — number of elements
*
* Returns 0 on success, -1 on underflow/empty.
*/
int ;
/* FIB_QUANT_H */