use mirtal_sys::ffi;
use super::array;
use crate::{Array, Error, Graph, Result};
#[derive(Debug, Clone, Copy)]
pub struct MxFp8<'array> {
pub weight: &'array Array,
pub scales: &'array Array,
}
#[derive(Debug, Clone)]
pub struct MxFp8Arrays {
pub weight: Array,
pub scales: Array,
}
impl MxFp8Arrays {
#[must_use]
pub const fn as_ref(&self) -> MxFp8<'_> {
MxFp8 {
weight: &self.weight,
scales: &self.scales,
}
}
}
impl Graph<'_> {
pub fn quantize_mxfp8(self, input: &Array) -> Result<MxFp8Arrays> {
let outputs = ffi::quantize_mxfp8(input.native()?, self.native()?)?;
let outputs = outputs.as_ref().ok_or(Error::NullHandle("MXFP8 arrays"))?;
let actual = ffi::arrays_len(outputs);
if actual != 2 {
return Err(Error::Arity {
operation: "quantize MXFP8",
expected: 2,
actual,
});
}
Ok(MxFp8Arrays {
weight: array(outputs, 0, "MXFP8 weight")?,
scales: array(outputs, 1, "MXFP8 scales")?,
})
}
pub fn mxfp8_matmul(
self,
input: &Array,
quantized: MxFp8<'_>,
transpose: bool,
) -> Result<Array> {
Array::from_raw(
ffi::mxfp8_matmul(
input.native()?,
quantized.weight.native()?,
quantized.scales.native()?,
transpose,
self.native()?,
)?,
"MXFP8 matmul",
)
}
pub fn dequantize_mxfp8(self, quantized: MxFp8<'_>) -> Result<Array> {
Array::from_raw(
ffi::dequantize_mxfp8(
quantized.weight.native()?,
quantized.scales.native()?,
self.native()?,
)?,
"dequantize MXFP8",
)
}
}