A high-performance inference engine for AI models
use criterion::{BenchmarkId, Criterion, Throughput};
use futures::executor::block_on;
use half::bf16;
use num_traits::Float;
use uzu_engine_macros::uzu_bench;
use crate::{
backends::common::{
Backend, Context,
gpu_types::quantization_method::QuantizationMethod,
kernel::{Kernels, matmul::MatmulKernel},
},
data_type::DataTypeRepr,
tests::{
cold_pool::ColdPool,
helpers::for_each_backend,
matmul::{QuantBuffers, QuantInput, bench_quant_gemv_shapes, iter_encode_loop_named, quant_arguments},
util::type_short_name,
},
};
fn bench_gemv_typed<B: Backend, T: DataTypeRepr + Float>(
c: &mut Criterion,
context: &B::Context,
label: &str,
group_size: u32,
bits: u32,
quant_method: QuantizationMethod,
) {
let group_path = format!("{}/Kernel/Gemv/{}", type_short_name::<B>(), label);
let mut group = c.benchmark_group(group_path.clone());
for shape in bench_quant_gemv_shapes(bits) {
let (m, k, n) = (shape.m, shape.k, shape.n);
let input = QuantInput::<T>::new(m, k, n, group_size, bits, quant_method, 42);
let mut buffers =
ColdPool::new(input.weight_buffer_bytes(), || QuantBuffers::<B, T>::allocate(context, &input));
let mut matmul = <<B as Backend>::Kernels as Kernels>::MatmulKernel::new(
context,
T::data_type(),
T::data_type(),
T::data_type(),
)
.unwrap();
group.throughput(Throughput::Elements(u64::from(m) * u64::from(n) * u64::from(k)));
group.bench_function(BenchmarkId::from_parameter(shape.to_string()), |b| {
let benchmark_path = format!("{group_path}/{shape}");
iter_encode_loop_named::<B, _>(context, b, &benchmark_path, |command_buffer| {
let args = quant_arguments(buffers.next_mut(), &input);
matmul.encode(args, command_buffer).expect("encode failed");
});
});
}
}
#[uzu_bench]
fn bench_gemv(c: &mut Criterion) {
for_each_backend!(|B| {
let context = block_on(<B as Backend>::Context::new()).unwrap();
bench_gemv_typed::<B, bf16>(c, &context, "ScaleBias_BF16_gs32", 32, 4, QuantizationMethod::ScaleBias);
bench_gemv_typed::<B, bf16>(c, &context, "ZP_BF16_gs32", 32, 4, QuantizationMethod::ScaleZeroPoint);
bench_gemv_typed::<B, bf16>(c, &context, "ScaleBias_BF16_gs64", 64, 4, QuantizationMethod::ScaleBias);
bench_gemv_typed::<B, bf16>(c, &context, "ZP_BF16_gs64", 64, 4, QuantizationMethod::ScaleZeroPoint);
bench_gemv_typed::<B, bf16>(c, &context, "Sym_BF16_gs64_8b", 64, 8, QuantizationMethod::ScaleSymmetric);
});
}