pub struct Quantized {
pub weight: Array,
pub scales: Array,
pub biases: Option<Array>,
pub quant: Quant,
}Expand description
Quantized weights: packed codes (uint32), one scale per group (affine: the weights’ dtype; floating-point modes: uint8 codes), affine biases, and how they were quantized.
use nuvec_core::{DType, QuantMode, ops::ones};
let w = ones([2, 64], DType::F32);
let q = w.quantize(QuantMode::Mxfp4);
assert_eq!((q.weight.shape(), q.scales.shape(), q.biases.is_none()), (&[2, 8][..], &[2, 2][..], true));
assert_eq!(q.dequantize(DType::F32).to_vec::<f32>().unwrap(), vec![1.0; 128]);Fields§
§weight: ArrayThe packed codes (uint32, last dimension values · bits / 32).
scales: ArrayOne scale per group (last dimension values / group_size).
biases: Option<Array>One bias per group: required for affine, absent for the floating-point modes.
quant: QuantHow the weights were quantized.
Implementations§
Source§impl Quantized
impl Quantized
Sourcepub fn affine(
weight: Array,
scales: Array,
biases: Array,
group_size: i32,
bits: i32,
) -> Self
pub fn affine( weight: Array, scales: Array, biases: Array, group_size: i32, bits: i32, ) -> Self
Affine weights quantized elsewhere: the biases are required, as MLX requires them
(a checkpoint without biases quantized with zero offsets: pass
ops::zeros_like(&scales)).
Sourcepub fn fp(weight: Array, scales: Array, mode: QuantMode) -> Self
pub fn fp(weight: Array, scales: Array, mode: QuantMode) -> Self
Floating-point-mode weights quantized elsewhere (MXFP4, MXFP8, NVFP4), which have no biases.
Sourcepub fn new(
weight: Array,
scales: Array,
biases: Option<Array>,
quant: impl Into<Quant>,
) -> Self
pub fn new( weight: Array, scales: Array, biases: Option<Array>, quant: impl Into<Quant>, ) -> Self
Weights quantized elsewhere (a checkpoint’s tensors). biases must be Some for
QuantMode::Affine and None for the floating-point modes, as in MLX; otherwise
every operation on them fails with an error saying so. Quantized::affine and
Quantized::fp make the rule part of the signature.
Trait Implementations§
Source§impl Leaves for Quantized
A tree of buffers (never trainable, as quantized layers’ tensors) with MLX’s names
weight, scales and biases, and quant as a static field: eval(&q), the
transforms and save_safetensors take quantized weights directly.
impl Leaves for Quantized
A tree of buffers (never trainable, as quantized layers’ tensors) with MLX’s names
weight, scales and biases, and quant as a static field: eval(&q), the
transforms and save_safetensors take quantized weights directly.
Source§impl Tree for Quantized
impl Tree for Quantized
Source§fn visit_mut(&mut self, v: &mut VisitMut<'_>)
fn visit_mut(&mut self, v: &mut VisitMut<'_>)
v mutably, in the order of
Leaves::visit.Source§fn map(&self, f: impl FnMut(&str, &Array) -> Array) -> Self
fn map(&self, f: impl FnMut(&str, &Array) -> Array) -> Self
f(path, leaf) (same structure; parameters keep
their frozen flag).Source§fn flatten(&self) -> Vec<(String, Array)>
fn flatten(&self) -> Vec<(String, Array)>
tree_flatten).Source§fn unflatten(&self, leaves: impl IntoIterator<Item = Array>) -> Result<Self>
fn unflatten(&self, leaves: impl IntoIterator<Item = Array>) -> Result<Self>
self is the template) with leaves in order: exactly
Tree::len of them, else an error naming both counts.Source§fn load_flat(
&mut self,
named: impl IntoIterator<Item = (String, Array)>,
mode: LoadMode,
) -> Result<()>
fn load_flat( &mut self, named: impl IntoIterator<Item = (String, Array)>, mode: LoadMode, ) -> Result<()>
LoadMode::Strict requires exactly the tree’s paths with
matching shapes; LoadMode::Partial assigns the matching names and ignores
unknown ones (shapes must still match). Loaded dtypes are kept. On any problem the
error lists every one (missing, unknown and mismatched names, sorted) and the tree is
unchanged. Duplicate paths (in the tree or in named) are an error.Auto Trait Implementations§
impl !RefUnwindSafe for Quantized
impl !UnwindSafe for Quantized
impl Freeze for Quantized
impl Send for Quantized
impl Sync for Quantized
impl Unpin for Quantized
impl UnsafeUnpin for Quantized
Blanket Implementations§
impl<T> AutoreleaseSafe for Twhere
T: ?Sized,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more