A high-performance inference engine for AI models
use super::{MatmulA, MatmulArguments, QuantParamsLayout};
use crate::backends::common::{
Backend, BufferMut, BufferRef,
gpu_types::gemm::{GemmBPrologueKind, GemmDTransform},
kernel::ActivationQuantization,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ActivationFormat {
Bf16,
Int8(ActivationQuantization),
PackedHalf {
block_k: u32,
bits: u32,
},
Trellis,
}
#[derive(Clone, Copy)]
pub struct MatmulShape {
pub m: u32,
pub n: u32,
pub k: u32,
pub b_transpose: bool,
pub b_leading_dimension: Option<u32>,
pub b_prologue: GemmBPrologueKind,
pub b_is_trellis: bool,
pub b_bits: Option<u32>,
pub b_group_size: Option<u32>,
pub signed_codes: bool,
pub a: ActivationFormat,
pub gathered: bool,
pub params_layout: Option<QuantParamsLayout>,
pub d_transform: GemmDTransform,
}
impl MatmulShape {
pub fn from_arguments<B: Backend>(
arguments: &MatmulArguments<
'_,
B,
impl BufferRef<Backend = B>,
impl BufferRef<Backend = B>,
impl BufferMut<Backend = B>,
impl BufferRef<Backend = B>,
>
) -> Self {
Self {
m: arguments.m,
n: arguments.n,
k: arguments.k,
b_transpose: arguments.b_transpose,
b_leading_dimension: arguments.b_leading_dimension,
b_prologue: arguments.b.b_prologue(),
b_is_trellis: arguments.b.is_trellis(),
b_bits: arguments.b.bits_per_b(),
b_group_size: arguments.b.group_size(),
signed_codes: arguments.b.signed_codes(),
a: match &arguments.a {
MatmulA::FullPrecision {
..
} => ActivationFormat::Bf16,
MatmulA::PackedHalf {
block_k,
bits,
..
} => ActivationFormat::PackedHalf {
block_k: *block_k,
bits: *bits,
},
MatmulA::Int8Symmetric {
quantization,
..
} => ActivationFormat::Int8(*quantization),
MatmulA::Trellis {
..
} => ActivationFormat::Trellis,
},
gathered: arguments.gather_indices.is_some(),
params_layout: arguments.b.quant_params_layout(),
d_transform: arguments.output.ops.mask(),
}
}
pub fn is_quant(&self) -> bool {
self.b_prologue != GemmBPrologueKind::FullPrecision
}
pub fn a_is_quantized(&self) -> bool {
matches!(self.a, ActivationFormat::Int8(_) | ActivationFormat::Trellis)
}
}