Files
leleka/crates/quant-core/src/lib.rs
T
Pavlo Ratushnyi baf11daf8c
Linux / keyless-cuda-gate (pull_request) Skipped
Linux / verify (pull_request) Successful in 4m50s
Linux / keyless-cuda (pull_request) Skipped
Linux / static-cuda-package (pull_request) Skipped
Linux / hosted-cuda-package (pull_request) Skipped
Linux / linux-cli-container (pull_request) Successful in 4m10s
Linux / linux-static-cli (pull_request) Successful in 8m9s
Linux / release-cli (pull_request) Skipped
Desktop foundation / core (ubuntu-latest) (push) Failing after 8s
Desktop foundation / core (macos-latest) (push) Canceled after 0s
Desktop foundation / core (windows-latest) (push) Canceled after 0s
STRA-102 port IQ2_S and IQ3_XXS preparation to Rust
2026-10-07 22:35:42 +02:00

61 lines
1.7 KiB
Rust

//! Pure Rust CPU quantization primitives for Leleka preparation tools.
//! No inference, GPU operation or complete model support is implied.
pub mod iq2_s;
mod iq2s_grid;
pub mod iq3_s;
pub mod iq3_xxs;
mod iq3s_grid;
mod iq3xxs_grid;
pub mod iq4_nl;
pub mod iq4_xs;
mod k_scale_min;
pub mod q2_0;
pub mod q4_k;
pub mod q5_k;
pub mod q6_k;
pub mod q8_0;
pub fn f16_bits_to_f32(bits: u16) -> f32 {
let sign = (u32::from(bits & 0x8000)) << 16;
let exponent = (bits >> 10) & 0x1f;
let mantissa = u32::from(bits & 0x03ff);
let result = match exponent {
0 if mantissa == 0 => sign,
0 => {
let leading = mantissa.leading_zeros() - 21;
let normalized = mantissa << leading;
sign | ((113 - leading) << 23) | ((normalized & 0x03ff) << 13)
}
31 => sign | 0x7f80_0000 | (mantissa << 13),
_ => sign | ((u32::from(exponent) + 112) << 23) | (mantissa << 13),
};
f32::from_bits(result)
}
#[cfg(test)]
mod tests {
use super::f16_bits_to_f32;
#[test]
fn binary16_boundaries_preserve_exact_binary32_bits() {
// Independent IEEE 754 encodings, including signed zero and NaN payloads.
for (half, single) in [
(0x0000, 0x0000_0000),
(0x8000, 0x8000_0000),
(0x0001, 0x3380_0000),
(0x03ff, 0x387f_c000),
(0x0400, 0x3880_0000),
(0x3c00, 0x3f80_0000),
(0xc000, 0xc000_0000),
(0x7bff, 0x477f_e000),
(0x7c00, 0x7f80_0000),
(0xfc00, 0xff80_0000),
(0x7c01, 0x7f80_2000),
(0xfe00, 0xffc0_0000),
] {
assert_eq!(f16_bits_to_f32(half).to_bits(), single);
}
}
}