Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

3.3 KB
Raw Blame
mod arguments;
mod d_ops;
mod kernel;
mod matmul_a;
mod matmul_b;
mod output;
pub mod routing;

pub use arguments::MatmulArguments;
pub use d_ops::MatmulDOps;
pub use kernel::MatmulKernel;
pub use matmul_a::{Int8CodeLayout, MatmulA, packed_half_bytes};
pub use matmul_b::{MatmulB, QuantizedB, QuantizedCorrection, TrellisFormat};
pub use output::MatmulOutput;
#[cfg(any(backend = "cpu", backend = "metal"))] // TODO: webgpu
pub use output::MatmulOutputError;
pub use routing::{ActivationFormat, MatmulShape};

use crate::backends::common::{
    Backend, Kernels,
    gpu_types::{matmul::QUANT_PARAMS_GROUP_OUTPUT_ALIGNMENT, quantization::QuantizationMode},
};

pub type WeightsLayout<B> = <<<B as Backend>::Kernels as Kernels>::MatmulKernel as MatmulKernel>::WeightsLayout;

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum QuantParamsLayout {
    /// `[N, G]`.
    OutputGroup,
    /// `[G, round_up_4(N)]`.
    GroupOutput,
}

#[derive(Clone, Copy, Default)]
pub struct QuantParamsStrides {
    pub output_stride: u32,
    pub group_stride: u32,
}

#[derive(Clone, Copy)]
pub struct QuantParams {
    layout: QuantParamsLayout,
    row_count: u32,
    group_count: u32,
}

impl QuantParams {
    pub const fn new(
        layout: QuantParamsLayout,
        row_count: u32,
        group_count: u32,
    ) -> Self {
        Self {
            layout,
            row_count,
            group_count,
        }
    }

    pub const fn is_per_row(self) -> bool {
        self.group_count == 1
    }

    pub const fn layout(self) -> QuantParamsLayout {
        self.layout
    }

    pub const fn scale_shape(self) -> [u32; 2] {
        self.shape(1)
    }

    pub fn zero_point_shape(
        self,
        mode: QuantizationMode,
    ) -> [u32; 2] {
        self.shape(mode.packing_divisor())
    }

    pub const fn scale_strides(self) -> QuantParamsStrides {
        self.strides(1)
    }

    pub fn zero_point_strides(
        self,
        mode: QuantizationMode,
    ) -> QuantParamsStrides {
        self.strides(mode.packing_divisor())
    }

    const fn shape(
        self,
        packing_divisor: u32,
    ) -> [u32; 2] {
        let row_values = self.padded_values_per_row(packing_divisor);
        match self.layout {
            QuantParamsLayout::OutputGroup => [self.row_count, row_values / packing_divisor],
            QuantParamsLayout::GroupOutput => [self.group_count, row_values / packing_divisor],
        }
    }

    const fn strides(
        self,
        packing_divisor: u32,
    ) -> QuantParamsStrides {
        let row_values = self.padded_values_per_row(packing_divisor);
        let (output_stride, mut group_stride) = match self.layout {
            QuantParamsLayout::OutputGroup => (row_values, 1),
            QuantParamsLayout::GroupOutput => (1, row_values),
        };
        if self.is_per_row() {
            group_stride = 0;
        }
        QuantParamsStrides {
            output_stride,
            group_stride,
        }
    }

    const fn padded_values_per_row(
        self,
        packing_divisor: u32,
    ) -> u32 {
        match self.layout {
            QuantParamsLayout::OutputGroup => self.group_count.next_multiple_of(packing_divisor),
            QuantParamsLayout::GroupOutput => self.row_count.next_multiple_of(QUANT_PARAMS_GROUP_OUTPUT_ALIGNMENT),
        }
    }
}