88 lines
3.2 KiB
Rust
88 lines
3.2 KiB
Rust
//! Pinned GGML scalar to_float traits, for the Original CPU/FP32 bench oracle.
|
|
//! They are deliberately separate from quantized vec_dot accumulation.
|
|
use super::rust_ggml::Kind;
|
|
use super::{Error, Result};
|
|
fn u16at(x: &[u8], i: usize) -> u16 {
|
|
u16::from_le_bytes([x[i], x[i + 1]])
|
|
}
|
|
fn u32at(x: &[u8], i: usize) -> u32 {
|
|
u32::from_le_bytes(x[i..i + 4].try_into().unwrap())
|
|
}
|
|
pub(super) fn half(h: u16) -> f32 {
|
|
let w = (h as u32) << 16;
|
|
let sign = w & 0x80000000;
|
|
let twice = w.wrapping_add(w);
|
|
let normal = f32::from_bits((twice >> 4).wrapping_add(0xe0 << 23)) * f32::from_bits(0x07800000);
|
|
let sub = f32::from_bits((twice >> 17) | (126 << 23)) - 0.5;
|
|
f32::from_bits(sign | if twice < 1 << 27 { sub.to_bits() } else { normal.to_bits() })
|
|
}
|
|
pub(super) fn half_rn(f: f32) -> u16 {
|
|
let mut base = (f.abs() * f32::from_bits(0x77800000)) * f32::from_bits(0x08800000);
|
|
let w = f.to_bits();
|
|
let twice = w.wrapping_add(w);
|
|
let bias = (twice & 0xff000000).max(0x71000000);
|
|
base = f32::from_bits((bias >> 1) + 0x07800000) + base;
|
|
let bits = base.to_bits();
|
|
((w & 0x80000000) >> 16) as u16
|
|
| if twice > 0xff000000 { 0x7e00 } else { (((bits >> 13) & 0x7c00) + (bits & 0xfff)) as u16 }
|
|
}
|
|
fn h(x: &[u8], i: usize) -> f32 {
|
|
half(u16at(x, i))
|
|
}
|
|
const FP4: [i8; 16] = [0, 1, 2, 3, 4, 6, 8, 12, 0, -1, -2, -3, -4, -6, -8, -12];
|
|
fn e8_half(e: u8) -> f32 {
|
|
f32::from_bits(if e < 2 { 0x00200000u32 << e } else { ((e as u32) - 1) << 23 })
|
|
}
|
|
fn ue4(e: u8) -> f32 {
|
|
if e == 0 || e == 127 {
|
|
return 0.0;
|
|
}
|
|
let exp = (e >> 3) & 15;
|
|
let m = e & 7;
|
|
let raw = if exp == 0 {
|
|
m as f32 * f32::from_bits(118 << 23)
|
|
} else {
|
|
(1.0 + m as f32 / 8.0) * f32::from_bits(((exp as i32 - 7 + 127) as u32) << 23)
|
|
};
|
|
raw * 0.5
|
|
}
|
|
fn six(x: &[u8]) -> [i32; 16] {
|
|
let a = u32at(x, 0);
|
|
let b = u32at(x, 4);
|
|
let c = u32at(x, 8);
|
|
let u = [
|
|
(a & 0x0f0f0f0f) | ((c & 0x03030303) << 4),
|
|
(b & 0x0f0f0f0f) | (((c >> 2) & 0x03030303) << 4),
|
|
((a >> 4) & 0x0f0f0f0f) | (((c >> 4) & 0x03030303) << 4),
|
|
((b >> 4) & 0x0f0f0f0f) | (((c >> 6) & 0x03030303) << 4),
|
|
];
|
|
std::array::from_fn(|j| ((u[j / 4] >> (8 * (j % 4))) as u8 as i32) - 32)
|
|
}
|
|
fn k4(x: &[u8], j: usize) -> (i32, i32) {
|
|
if j < 4 {
|
|
((x[j] & 63) as i32, (x[j + 4] & 63) as i32)
|
|
} else {
|
|
(((x[j + 4] & 15) | ((x[j - 4] >> 6) << 4)) as i32, ((x[j + 4] >> 4) | ((x[j] >> 6) << 4)) as i32)
|
|
}
|
|
}
|
|
pub fn dequantize(kind: Kind, raw: &[u8], output: &mut [f32]) -> Result<()> {
|
|
if kind.row_bytes(output.len())? != raw.len() {
|
|
return Err(Error::Shape);
|
|
}
|
|
let (q, b) = kind.block();
|
|
for (x, y) in raw.chunks_exact(b).zip(output.chunks_exact_mut(q)) {
|
|
match kind {
|
|
Kind::Q2K | Kind::Q3K | Kind::Q4K | Kind::Q5K | Kind::Q6K | Kind::Tq1 | Kind::Tq2 => {
|
|
kquant::block(kind, x, y)
|
|
}
|
|
Kind::Iq2Xxs | Kind::Iq2Xs | Kind::Iq2S | Kind::Iq3Xxs | Kind::Iq3S => iq::block(kind, x, y),
|
|
Kind::Iq1S | Kind::Iq1M | Kind::Iq4Nl | Kind::Iq4Xs => iq::block(kind, x, y),
|
|
_ => plain::block(kind, x, y),
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
mod iq;
|
|
mod kquant;
|
|
mod plain;
|