Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

8.3 KB
Raw Blame
use derive_more::Debug;
use thiserror::Error;

use crate::{
    backends::common::{
        Backend,
        gpu_types::{
            quantization::QuantizationMode, quantization_method::QuantizationMethod, trellis::TrellisCodebook,
        },
        kernel::matmul::{
            MatmulB, QuantParams, QuantParamsLayout, QuantizedB, QuantizedCorrection, TrellisFormat, WeightsLayout,
        },
    },
    config::weight_matrix::{AnyWeightMatrixSpec, Layout},
    data_type::DataType,
    safetensors::{ParameterLoaderError, ParameterTree},
};

#[derive(Debug, Error)]
pub enum WeightMatrixError<B: Backend> {
    #[error("Parameter loading error: {0}")]
    ParameterError(#[from] ParameterLoaderError<B>),
    #[error("Unsupported weight matrix configuration: {0}")]
    UnsupportedConfiguration(String),
}

pub struct WeightMatrix<B: Backend> {
    values: B::GlobalGpuBuffer,
    encoding: WeightEncoding<B>,
}

enum WeightEncoding<B: Backend> {
    Dense,
    Quantized {
        scales: B::GlobalGpuBuffer,
        correction: QuantizedCorrection<B::GlobalGpuBuffer>,
        params: QuantParams,
        mode: QuantizationMode,
        group_size: u32,
        signed_codes: bool,
        weights_layout: WeightsLayout<B>,
    },
    Trellis {
        row_scales: B::GlobalGpuBuffer,
        codebook: TrellisCodebook,
        format: TrellisFormat,
    },
}

impl<B: Backend> WeightMatrix<B> {
    pub async fn load(
        output_dim: u32,
        input_dim: u32,
        data_type: DataType,
        spec: AnyWeightMatrixSpec,
        required_layout: Layout,
        tree: &ParameterTree<'_, B>,
    ) -> Result<Self, WeightMatrixError<B>> {
        let (layout, quantized) = match spec {
            AnyWeightMatrixSpec::FullPrecisionSpec(spec) => (spec.layout, None),
            AnyWeightMatrixSpec::MLXSpec(spec) => {
                (spec.layout, Some((spec.bits, spec.group_size, QuantizationMethod::ScaleBias)))
            },
            AnyWeightMatrixSpec::IntSpec(spec) => (
                spec.layout,
                Some((
                    spec.bits,
                    spec.group_size,
                    if spec.is_symmetric {
                        QuantizationMethod::ScaleSymmetric
                    } else {
                        QuantizationMethod::ScaleZeroPoint
                    },
                )),
            ),
            AnyWeightMatrixSpec::QtipGaussianSpec(spec) => {
                if spec.layout != Layout::OutputInput || required_layout != Layout::OutputInput {
                    return Err(WeightMatrixError::UnsupportedConfiguration(format!(
                        "Trellis requires OutputInput layout, got stored={:?}, requested={required_layout:?}",
                        spec.layout
                    )));
                }
                let format = TrellisFormat {
                    vector_width: spec.vector_width,
                    transition_bits: spec.transition_bits,
                    restart_columns: (spec.restart_columns != 0).then_some(spec.restart_columns),
                };
                let code_shape = [output_dim, format.code_row_bytes(input_dim)];
                let values = tree.read_buffer("codes", &code_shape, DataType::U8).await?;
                let row_scales = tree.read_buffer("scales", &[output_dim], DataType::F32).await?;
                return Ok(Self {
                    values,
                    encoding: WeightEncoding::Trellis {
                        row_scales,
                        codebook: spec.codebook,
                        format,
                    },
                });
            },
            spec => return Err(WeightMatrixError::UnsupportedConfiguration(format!("{spec:?}"))),
        };
        if layout != required_layout {
            return Err(WeightMatrixError::UnsupportedConfiguration(format!(
                "expected {required_layout:?} weight layout, got {layout:?}"
            )));
        }
        let (rows, columns) = match layout {
            Layout::OutputInput => (output_dim, input_dim),
            Layout::InputOutput => (input_dim, output_dim),
        };

        let Some((bits, group_size, method)) = quantized else {
            let values = tree.read_buffer("weights", &[rows, columns], data_type).await?;
            return Ok(Self {
                values,
                encoding: WeightEncoding::Dense,
            });
        };
        let mode = QuantizationMode::from_unsigned_bits(bits).ok_or_else(|| {
            WeightMatrixError::UnsupportedConfiguration(format!("{method} bits={bits}, group_size={group_size}"))
        })?;
        // Parameters swap the weight axes once K is grouped: output-input stores [G, N], input-output stores [N, G].
        let params_layout = match layout {
            Layout::OutputInput => QuantParamsLayout::GroupOutput,
            Layout::InputOutput => QuantParamsLayout::OutputGroup,
        };

        let packing_divisor = mode.packing_divisor();
        if !columns.is_multiple_of(packing_divisor) {
            return Err(WeightMatrixError::UnsupportedConfiguration(format!(
                "stored columns {columns} are not divisible by packing divisor {packing_divisor}"
            )));
        }
        let values = tree.read_buffer("weights", &[rows, columns / packing_divisor], mode.storage_type()).await?;
        let params = QuantParams::new(params_layout, rows, columns.div_ceil(group_size));
        let scales = tree.read_buffer("scales", &params.scale_shape(), data_type).await?;
        let correction = match method {
            QuantizationMethod::ScaleBias => {
                QuantizedCorrection::Biases(tree.read_buffer("biases", &params.scale_shape(), data_type).await?)
            },
            QuantizationMethod::ScaleZeroPoint => QuantizedCorrection::ZeroPoints(
                tree.read_buffer("zero_points", &params.zero_point_shape(mode), mode.storage_type()).await?,
            ),
            QuantizationMethod::ScaleSymmetric => QuantizedCorrection::Symmetric,
        };

        Ok(Self {
            values,
            encoding: WeightEncoding::Quantized {
                scales,
                correction,
                params,
                mode,
                group_size,
                signed_codes: false,
                weights_layout: Default::default(),
            },
        })
    }

    pub fn values(&self) -> &B::GlobalGpuBuffer {
        &self.values
    }

    pub fn scales(&self) -> Option<&B::GlobalGpuBuffer> {
        self.matmul_b().quantized().map(|quantized| quantized.scales)
    }

    pub fn zero_points(&self) -> Option<&B::GlobalGpuBuffer> {
        self.matmul_b().quantized()?.zero_points()
    }

    pub fn biases(&self) -> Option<&B::GlobalGpuBuffer> {
        self.matmul_b().quantized()?.biases()
    }

    pub fn matmul_b(&self) -> MatmulB<&B::GlobalGpuBuffer> {
        match &self.encoding {
            WeightEncoding::Dense => MatmulB::FullPrecision {
                b: &self.values,
            },
            WeightEncoding::Quantized {
                scales,
                correction,
                params,
                mode,
                group_size,
                signed_codes,
                weights_layout,
            } => MatmulB::Quantized(QuantizedB {
                codes: &self.values,
                scales,
                correction: correction.as_ref(),
                params: *params,
                mode: *mode,
                group_size: *group_size,
                signed_codes: *signed_codes,
                weights_layout: *weights_layout,
            }),
            WeightEncoding::Trellis {
                row_scales,
                codebook,
                format,
            } => MatmulB::Trellis {
                codes: &self.values,
                row_scales,
                codebook: *codebook,
                format: *format,
            },
        }
    }

    pub fn optimize_weights_layout<E>(
        &mut self,
        optimize: impl FnOnce(&mut B::GlobalGpuBuffer) -> Result<(WeightsLayout<B>, bool), E>,
    ) -> Result<(), E> {
        let WeightEncoding::Quantized {
            weights_layout,
            signed_codes,
            ..
        } = &mut self.encoding
        else {
            return Ok(());
        };
        if *weights_layout != WeightsLayout::<B>::default() {
            return Ok(());
        }
        (*weights_layout, *signed_codes) = optimize(&mut self.values)?;
        Ok(())
    }
}