use crate::ml::matrix::{self as ml_matrix, QuantMatrix, Quantization};
use crate::{Error, Matrix, Result, matrix};
use super::super::module::{Module, ModuleRegistry};
pub struct QuantizedLinear {
input_features: usize,
output_features: usize,
weight: QuantMatrix,
bias: Option<Matrix>,
registry: ModuleRegistry,
}
impl QuantizedLinear {
pub fn from_linear(linear: &super::Linear, quantization: Quantization) -> Result<Self> {
let weight = ml_matrix::quantize(&linear.weight().data(), quantization)?;
let bias = linear.bias().map(|bias| bias.data());
Self::from_quantized(weight, bias)
}
pub fn from_quantized(weight: QuantMatrix, bias: Option<Matrix>) -> Result<Self> {
if weight.shape().len() != 2 {
return Err(Error::invalid_argument(
"QuantizedLinear weight must have rank 2 [N,K]",
));
}
let output_features = weight.shape()[0];
let input_features = weight.shape()[1];
if let Some(bias) = &bias
&& bias.shape() != [output_features]
{
return Err(Error::invalid_argument(format!(
"QuantizedLinear bias must have shape [{output_features}]; found {:?}",
bias.shape(),
)));
}
let mut registry = ModuleRegistry::new();
if let Some(bias) = &bias {
registry.register_buffer("bias", bias.clone(), true)?;
}
Ok(Self {
input_features,
output_features,
weight,
bias,
registry,
})
}
pub const fn input_features(&self) -> usize {
self.input_features
}
pub const fn output_features(&self) -> usize {
self.output_features
}
pub const fn quantization(&self) -> Quantization {
self.weight.quantization()
}
pub const fn weight(&self) -> &QuantMatrix {
&self.weight
}
pub const fn bias(&self) -> Option<&Matrix> {
self.bias.as_ref()
}
}
impl Module for QuantizedLinear {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
if input.shape().last() != Some(&self.input_features) {
return Err(Error::invalid_argument(format!(
"QuantizedLinear requires input last dimension {}; found {:?}",
self.input_features,
input.shape(),
)));
}
let output = ml_matrix::matmul(input, &self.weight)?;
if let Some(bias) = &self.bias {
return matrix::add(&output, bias);
}
Ok(output)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}