Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

Introduce cold pool buffers for benchmarking memory-bound kernel (#537)

CCYeh4034d56ba6e0
diff --git a/crates/backend-uzu/benches/BENCHMARKS.md b/crates/backend-uzu/benches/BENCHMARKS.md
index 1d14e0437..df09f65d1 100644
--- a/crates/backend-uzu/benches/BENCHMARKS.md
+++ b/crates/backend-uzu/benches/BENCHMARKS.md
@@ -112,3 +112,9 @@ To inspect a specific label only:
open target/criterion/m2_max/report/index.html
open target/criterion/a19/report/index.html
```
+
+## Cold GEMV
+
+GEMV-class benches cycle through enough quant-buffer copies to cover a 256 MiB
+weight working set before reusing one. This avoids ranking kernels on
+cache-warm weights; pools allocate lazily, so criterion filters skip their cost.
diff --git a/crates/backend-uzu/src/lib.rs b/crates/backend-uzu/src/lib.rs
index df917b5ea..f547874f4 100644
--- a/crates/backend-uzu/src/lib.rs
+++ b/crates/backend-uzu/src/lib.rs
@@ -1,7 +1,7 @@
#![cfg_attr(test, feature(custom_test_frameworks, test))]
#![cfg_attr(test, test_runner(test_runner::uzu_harness))]
-mod array;
+pub mod array;
mod audio;
mod classifier;
mod config;
diff --git a/crates/backend-uzu/src/tests/cold_pool.rs b/crates/backend-uzu/src/tests/cold_pool.rs
new file mode 100644
index 000000000..72efbf1da
--- /dev/null
+++ b/crates/backend-uzu/src/tests/cold_pool.rs
@@ -0,0 +1,39 @@
+const COLD_WORKING_SET_BYTES: usize = 256 << 20;
+
+pub struct ColdPool<T, F: FnMut() -> T> {
+ bytes_per_copy: usize,
+ alloc: F,
+ copies: Vec<T>,
+ next: usize,
+}
+
+impl<T, F: FnMut() -> T> ColdPool<T, F> {
+ pub fn new(
+ bytes_per_copy: usize,
+ alloc: F,
+ ) -> Self {
+ Self {
+ bytes_per_copy,
+ alloc,
+ copies: Vec::new(),
+ next: 0,
+ }
+ }
+
+ pub fn next_mut(&mut self) -> &mut T {
+ if self.copies.is_empty() {
+ let count = copy_count(COLD_WORKING_SET_BYTES, self.bytes_per_copy);
+ self.copies = (0..count).map(|_| (self.alloc)()).collect();
+ }
+ let index = self.next;
+ self.next = (index + 1) % self.copies.len();
+ &mut self.copies[index]
+ }
+}
+
+pub(crate) fn copy_count(
+ working_set: usize,
+ bytes_per_copy: usize,
+) -> usize {
+ working_set.div_ceil(bytes_per_copy).max(1)
+}
diff --git a/crates/backend-uzu/src/tests/matmul/quant.rs b/crates/backend-uzu/src/tests/matmul/quant.rs
index 2ad058aa5..82bec5995 100644
--- a/crates/backend-uzu/src/tests/matmul/quant.rs
+++ b/crates/backend-uzu/src/tests/matmul/quant.rs
@@ -1,3 +1,5 @@
+use std::mem::size_of_val;
+
use num_traits::Float;
use rand::{RngExt, SeedableRng, rngs::SmallRng};
@@ -89,6 +91,13 @@ impl<T: ArrayElement + Float> QuantInput<T> {
mode: mode_for_bits(bits),
}
}
+
+ pub(crate) fn weight_buffer_bytes(&self) -> usize {
+ size_of_val(self.w_packed.as_slice())
+ + size_of_val(self.scales.as_slice())
+ + self.biases.as_ref().map_or(0, |biases| size_of_val(biases.as_slice()))
+ + self.zero_points.as_ref().map_or(0, |zero_points| size_of_val(zero_points.as_slice()))
+ }
}
pub struct QuantBuffers<B: Backend, T: ArrayElement + Float> {
diff --git a/crates/backend-uzu/src/tests/mod.rs b/crates/backend-uzu/src/tests/mod.rs
index 7c714015b..69e3af19d 100644
--- a/crates/backend-uzu/src/tests/mod.rs
+++ b/crates/backend-uzu/src/tests/mod.rs
@@ -5,7 +5,11 @@ pub extern crate test;
pub mod assert;
pub mod audio;
+pub mod cold_pool;
pub mod helpers;
pub mod matmul;
pub mod proptest;
pub mod util;
+
+#[path = "../../unit/bench_cold_pool_test.rs"]
+mod bench_cold_pool_test;
diff --git a/crates/backend-uzu/unit/backends/common/kernel/matmul/quant_gemv_bench.rs b/crates/backend-uzu/unit/backends/common/kernel/matmul/quant_gemv_bench.rs
index 7afd256ad..e4969e24a 100644
--- a/crates/backend-uzu/unit/backends/common/kernel/matmul/quant_gemv_bench.rs
+++ b/crates/backend-uzu/unit/backends/common/kernel/matmul/quant_gemv_bench.rs
@@ -12,6 +12,7 @@ use crate::{
kernel::{Kernels, matmul::MatmulKernel},
},
tests::{
+ cold_pool::ColdPool,
matmul::{QuantBuffers, QuantInput, bench_quant_gemv_shapes, iter_encode_loop_named, quant_arguments},
util::type_short_name,
},
@@ -31,7 +32,8 @@ fn bench_gemv_typed<B: Backend, T: ArrayElement + Float>(
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 = QuantBuffers::<B, T>::allocate(context, &input);
+ 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(),
@@ -44,7 +46,7 @@ fn bench_gemv_typed<B: Backend, T: ArrayElement + Float>(
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, |encoder| {
- let args = quant_arguments(&mut buffers, &input);
+ let args = quant_arguments(buffers.next_mut(), &input);
matmul.encode(args, encoder).expect("encode failed");
});
});
diff --git a/crates/backend-uzu/unit/backends/common/kernel/matmul/qwen3_bench.rs b/crates/backend-uzu/unit/backends/common/kernel/matmul/qwen3_bench.rs
index d9d6f9b32..80f7200b1 100644
--- a/crates/backend-uzu/unit/backends/common/kernel/matmul/qwen3_bench.rs
+++ b/crates/backend-uzu/unit/backends/common/kernel/matmul/qwen3_bench.rs
@@ -16,6 +16,7 @@ use crate::{
metal::{Metal, MetalContext},
},
tests::{
+ cold_pool::ColdPool,
matmul::{QuantBuffers, QuantInput, iter_encode_loop, quant_arguments, qwen3_layer_shapes},
util::type_short_name,
},
@@ -33,7 +34,8 @@ fn bench_qwen3_layers_typed<T: ArrayElement + Float>(
for (layer, shape) in qwen3_layer_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 = QuantBuffers::<Metal, T>::allocate(context, &input);
+ let mut buffers =
+ ColdPool::new(input.weight_buffer_bytes(), || QuantBuffers::<Metal, T>::allocate(context, &input));
let mut matmul = <<Metal as Backend>::Kernels as Kernels>::MatmulKernel::new(
context,
T::data_type(),
@@ -45,7 +47,7 @@ fn bench_qwen3_layers_typed<T: ArrayElement + Float>(
group.throughput(Throughput::Elements((m * n * k) as u64));
group.bench_function(BenchmarkId::from_parameter(format!("{layer}_{shape}")), |b| {
iter_encode_loop::<Metal, _>(context, b, |encoder| {
- matmul.encode(quant_arguments(&mut buffers, &input), encoder).expect("encode qwen3 layer");
+ matmul.encode(quant_arguments(buffers.next_mut(), &input), encoder).expect("encode qwen3 layer");
});
});
}
diff --git a/crates/backend-uzu/unit/bench_cold_pool_test.rs b/crates/backend-uzu/unit/bench_cold_pool_test.rs
new file mode 100644
index 000000000..ddbaa210d
--- /dev/null
+++ b/crates/backend-uzu/unit/bench_cold_pool_test.rs
@@ -0,0 +1,10 @@
+use proc_macros::uzu_test;
+
+use crate::tests::cold_pool::copy_count;
+
+#[uzu_test]
+fn copy_count_math() {
+ assert_eq!(copy_count(512 << 20, 64 << 20), 8);
+ assert_eq!(copy_count(512 << 20, 100 << 20), 6);
+ assert_eq!(copy_count(512 << 20, 1 << 30), 1);
+}