MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / quantize

Function quantize

nodedb-codec/src/vector_quant/ternary/packing.rs:108–121  ·  view source on GitHub ↗

Quantize a FP32 vector to ternary trits using BitNet absmean scaling. Returns `(trits, scale)` where `scale = mean(|v_i|)`.

(v: &[f32])

Source from the content-addressed store, hash-verified

106///
107/// Returns `(trits, scale)` where `scale = mean(|v_i|)`.
108pub fn quantize(v: &[f32]) -> (Vec<i8>, f32) {
109 if v.is_empty() {
110 return (Vec::new(), 0.0);
111 }
112 let scale: f32 = v.iter().map(|x| x.abs()).sum::<f32>() / v.len() as f32;
113 let trits = if scale == 0.0 {
114 vec![0i8; v.len()]
115 } else {
116 v.iter()
117 .map(|&x| (x / scale).round().clamp(-1.0, 1.0) as i8)
118 .collect()
119 };
120 (trits, scale)
121}
122
123#[cfg(test)]
124mod tests {

Callers 3

encodeMethod · 0.85
prepare_queryMethod · 0.85

Calls 5

collectMethod · 0.80
roundMethod · 0.80
is_emptyMethod · 0.45
iterMethod · 0.45
lenMethod · 0.45

Tested by 1