Files
leleka/crates/native-cpu-expert/src/rust_model_quant.rs
T

105 lines
4.2 KiB
Rust

//! Original ggml_quantize_chunk/type-trait adapter for numerical bench/conversion.
//! No weight/source/binding certificate is created by encoding a float slice.
use super::rust_ggml::Kind;
use super::{Error, Result};
pub fn requires_imatrix(kind: Kind) -> bool {
matches!(kind, Kind::Iq2Xxs | Kind::Iq2Xs | Kind::Iq1S)
}
pub fn quantize_init(kind: Kind) -> Result<()> {
super::rust_weight_iq::init(kind)
}
pub fn quantize_free() -> Result<()> {
for k in [Kind::Iq2Xxs, Kind::Iq2Xs, Kind::Iq2S, Kind::Iq1S, Kind::Iq1M, Kind::Iq3Xxs, Kind::Iq3S] {
super::rust_weight_iq::free(k)?;
}
Ok(())
}
fn validate(kind: Kind, x: &[f32], y: &[u8], imatrix: Option<&[f32]>) -> Result<()> {
if x.is_empty()
|| y.len() != kind.row_bytes(x.len())?
|| imatrix.is_some_and(|w| w.len() != x.len())
|| requires_imatrix(kind) && imatrix.is_none()
{
return Err(Error::Shape);
}
if x.iter().any(|x| !x.is_finite()) || imatrix.is_some_and(|w| w.iter().any(|x| !x.is_finite() || *x < 0.0)) {
return Err(Error::NonFinite);
}
Ok(())
}
/// Reference model traits. IQ2_XXS/XS/IQ1_S have no unweighted reference;
/// callers use quantize_chunk with their actual required imatrix instead.
pub fn quantize_row_ref(kind: Kind, x: &[f32], y: &mut [u8]) -> Result<()> {
validate(kind, x, y, None)?;
quantize_init(kind)?;
if !super::rust_weight_quant::row_without_iq(kind, x, y, None, true)? {
super::rust_weight_iq::row(kind, x, y, None)?;
}
Ok(())
}
/// Argument shapes of a chunk quantization: a row length, a row-aligned block-aligned start, a matching importance
/// matrix where the kind needs one.
fn check_chunk_args(kind: Kind, start: usize, n_per_row: usize, imatrix: Option<&[f32]>) -> Result<()> {
if n_per_row == 0
|| start % kind.block().0 != 0
|| start % n_per_row != 0
|| imatrix.is_some_and(|w| w.len() != n_per_row)
|| requires_imatrix(kind) && imatrix.is_none()
{
return Err(Error::Shape);
}
Ok(())
}
/// Which part of a weight matrix a chunk quantization covers: `nrows` rows of `n_per_row` weights from `start`.
#[derive(Clone, Copy)]
pub struct ChunkSpan {
pub start: usize,
pub nrows: usize,
pub n_per_row: usize,
}
/// Source end, destination start and end, and the byte count of the rows `span` covers.
fn chunk_extents(span: ChunkSpan, rowbytes: usize) -> Result<(usize, usize, usize, usize)> {
let ChunkSpan { start, nrows, n_per_row } = span;
let n = nrows.checked_mul(n_per_row).ok_or(Error::Shape)?;
let end = start.checked_add(n).ok_or(Error::Shape)?;
let at = (start / n_per_row).checked_mul(rowbytes).ok_or(Error::Shape)?;
let bytes = nrows.checked_mul(rowbytes).ok_or(Error::Shape)?;
let outend = at.checked_add(bytes).ok_or(Error::Shape)?;
Ok((end, at, outend, bytes))
}
pub fn quantize_chunk(
kind: Kind,
src: &[f32],
dst: &mut [u8],
span: ChunkSpan,
imatrix: Option<&[f32]>,
) -> Result<usize> {
let ChunkSpan { start, n_per_row, .. } = span;
check_chunk_args(kind, start, n_per_row, imatrix)?;
let rowbytes = kind.row_bytes(n_per_row)?;
let (end, at, outend, bytes) = chunk_extents(span, rowbytes)?;
if end > src.len() || outend > dst.len() {
return Err(Error::Shape);
}
check_chunk_finite(&src[start..end], imatrix)?;
quantize_init(kind)?;
for (x, y) in src[start..end].chunks_exact(n_per_row).zip(dst[at..outend].chunks_exact_mut(rowbytes)) {
quantize_row_chunk(kind, x, y, imatrix)?;
}
Ok(bytes)
}
/// Source weights finite; importance weights finite and non-negative.
fn check_chunk_finite(src: &[f32], imatrix: Option<&[f32]>) -> Result<()> {
if src.iter().any(|x| !x.is_finite()) || imatrix.is_some_and(|w| w.iter().any(|x| !x.is_finite() || *x < 0.0)) {
return Err(Error::NonFinite);
}
Ok(())
}
/// One row of a chunk: the non-IQ quantizers first, the IQ family when they decline the kind.
fn quantize_row_chunk(kind: Kind, x: &[f32], y: &mut [u8], imatrix: Option<&[f32]>) -> Result<()> {
if !super::rust_weight_quant::row_without_iq(kind, x, y, imatrix, false)? {
super::rust_weight_iq::row(kind, x, y, imatrix)?;
}
Ok(())
}