Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

Decoding loop benches (#487)

Aleksandr Golokoz08b45105a41e
diff --git a/Cargo.lock b/Cargo.lock
index e18d21558..4d08b7eb3 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -577,6 +577,7 @@ dependencies = [
"itertools 0.14.0",
"libc",
"libm",
+ "macmon",
"memmap2",
"minijinja",
"minijinja-contrib",
@@ -597,7 +598,7 @@ dependencies = [
"regex",
"rhai",
"rstest",
- "rustix",
+ "rustix 1.1.4",
"schemars 1.2.1",
"serde",
"serde_json",
@@ -768,6 +769,9 @@ name = "bitflags"
version = "2.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4512299f36f043ab09a583e57bceb5a5aab7a73db1805848e8fef3c9e8c78b3"
+dependencies = [
+ "serde_core",
+]
[[package]]
name = "bitstream-io"
@@ -1135,7 +1139,7 @@ name = "cli-storage"
version = "0.5.3"
dependencies = [
"clap",
- "crossterm",
+ "crossterm 0.29.0",
"dotenvy",
"download-manager",
"futures-util",
@@ -1526,6 +1530,23 @@ version = "0.8.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
+[[package]]
+name = "crossterm"
+version = "0.28.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "829d955a0bb380ef178a640b91779e3987da38c9aea133b20614cfed8cdea9c6"
+dependencies = [
+ "bitflags 2.11.1",
+ "crossterm_winapi",
+ "mio",
+ "parking_lot",
+ "rustix 0.38.44",
+ "serde",
+ "signal-hook",
+ "signal-hook-mio",
+ "winapi",
+]
+
[[package]]
name = "crossterm"
version = "0.29.0"
@@ -1539,7 +1560,8 @@ dependencies = [
"futures-core",
"mio",
"parking_lot",
- "rustix",
+ "rustix 1.1.4",
+ "serde",
"signal-hook",
"signal-hook-mio",
"winapi",
@@ -3097,7 +3119,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec09d74eb53878be6a7bd49f0ecb0e68d7322963d0a4fbbade67465083f38dbd"
dependencies = [
"bitflags 2.11.1",
- "crossterm",
+ "crossterm 0.29.0",
"futures",
"generational-box",
"iocraft-macros",
@@ -3461,6 +3483,12 @@ dependencies = [
"cc",
]
+[[package]]
+name = "linux-raw-sys"
+version = "0.4.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab"
+
[[package]]
name = "linux-raw-sys"
version = "0.12.1"
@@ -3582,6 +3610,23 @@ dependencies = [
"libc",
]
+[[package]]
+name = "macmon"
+version = "0.7.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f5c39c83f05d6ebe9eb6c8ea95db95279bb085124bf244d3383e9c7b0abd2465"
+dependencies = [
+ "chrono",
+ "clap",
+ "core-foundation 0.10.1",
+ "libc",
+ "num-traits",
+ "ratatui",
+ "serde",
+ "serde-inline-default",
+ "serde_json",
+]
+
[[package]]
name = "macro_rules_attribute"
version = "0.2.2"
@@ -4222,6 +4267,12 @@ dependencies = [
"rustc-hash 2.1.2",
]
+[[package]]
+name = "numtoa"
+version = "0.2.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6aa2c4e539b869820a2b82e1aef6ff40aa85e65decdd5185e83fb4b1249cd00f"
+
[[package]]
name = "objc-sys"
version = "0.3.5"
@@ -5179,8 +5230,10 @@ dependencies = [
"ratatui-core",
"ratatui-crossterm",
"ratatui-macros",
+ "ratatui-termion",
"ratatui-termwiz",
"ratatui-widgets",
+ "serde",
]
[[package]]
@@ -5196,6 +5249,7 @@ dependencies = [
"itertools 0.14.0",
"kasuari",
"lru",
+ "serde",
"strum",
"thiserror 2.0.18",
"unicode-segmentation",
@@ -5210,7 +5264,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "577c9b9f652b4c121fb25c6a391dd06406d3b092ba68827e6d2f09550edc54b3"
dependencies = [
"cfg-if",
- "crossterm",
+ "crossterm 0.28.1",
+ "crossterm 0.29.0",
"instability",
"ratatui-core",
]
@@ -5225,6 +5280,17 @@ dependencies = [
"ratatui-widgets",
]
+[[package]]
+name = "ratatui-termion"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4cade85a8591fbc911e147951422f0d6fd40f4948b271b6216c7dc01838996f8"
+dependencies = [
+ "instability",
+ "ratatui-core",
+ "termion",
+]
+
[[package]]
name = "ratatui-termwiz"
version = "0.1.0"
@@ -5248,6 +5314,7 @@ dependencies = [
"itertools 0.14.0",
"line-clipping",
"ratatui-core",
+ "serde",
"strum",
"time",
"unicode-segmentation",
@@ -5695,6 +5762,19 @@ dependencies = [
"semver",
]
+[[package]]
+name = "rustix"
+version = "0.38.44"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fdb5bc1ae2baa591800df16c9ca78619bf65c0488b41b96ccec5d11220d8c154"
+dependencies = [
+ "bitflags 2.11.1",
+ "errno",
+ "libc",
+ "linux-raw-sys 0.4.15",
+ "windows-sys 0.59.0",
+]
+
[[package]]
name = "rustix"
version = "1.1.4"
@@ -5704,7 +5784,7 @@ dependencies = [
"bitflags 2.11.1",
"errno",
"libc",
- "linux-raw-sys",
+ "linux-raw-sys 0.12.1",
"windows-sys 0.61.2",
]
@@ -6006,6 +6086,17 @@ dependencies = [
"serde_derive",
]
+[[package]]
+name = "serde-inline-default"
+version = "1.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2bf03b7a5281cfd4013bc788d057b0f09fc634397d83bc8973c955bd4f32727a"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.117",
+]
+
[[package]]
name = "serde-wasm-bindgen"
version = "0.6.5"
@@ -6559,7 +6650,7 @@ dependencies = [
"fastrand",
"getrandom 0.4.2",
"once_cell",
- "rustix",
+ "rustix 1.1.4",
"windows-sys 0.61.2",
]
@@ -6584,6 +6675,17 @@ dependencies = [
"phf_codegen",
]
+[[package]]
+name = "termion"
+version = "4.0.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f44138a9ae08f0f502f24104d82517ef4da7330c35acd638f1f29d3cd5475ecb"
+dependencies = [
+ "libc",
+ "numtoa",
+ "serde",
+]
+
[[package]]
name = "termios"
version = "0.3.3"
@@ -6618,6 +6720,7 @@ dependencies = [
"pest",
"pest_derive",
"phf",
+ "serde",
"sha2",
"signal-hook",
"siphasher",
@@ -7893,6 +7996,7 @@ checksum = "692daff6d93d94e29e4114544ef6d5c942a7ed998b37abdc19b17136ea428eb7"
dependencies = [
"getrandom 0.3.4",
"mac_address",
+ "serde",
"sha2",
"thiserror 1.0.69",
"uuid",
@@ -7907,6 +8011,7 @@ dependencies = [
"csscolorparser",
"deltae",
"lazy_static",
+ "serde",
"wezterm-dynamic",
]
diff --git a/Cargo.toml b/Cargo.toml
index cbc72228c..0e4e28bf5 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -206,6 +206,7 @@ rodio = { version = "0.22.2", default-features = false, features = [
"playback",
"wav",
] }
+macmon = "0.7"
[workspace.lints.clippy]
# TODO: add large_stack_frames, large_stack_arrays, trivially_copy_pass_by_ref, needless_pass_by_value, map_err_ignore, use_self, cloned_instead_of_copied, manual_let_else, semicolon_if_nothing_returned, uninlined_format_args, lossy_float_literal
diff --git a/crates/backend-uzu/Cargo.toml b/crates/backend-uzu/Cargo.toml
index 8003a73a0..a1bfbd7a3 100644
--- a/crates/backend-uzu/Cargo.toml
+++ b/crates/backend-uzu/Cargo.toml
@@ -67,6 +67,10 @@ rstest.workspace = true
criterion = { workspace = true, default-features = true }
proptest = { workspace = true, default-features = true }
+[target.'cfg(target_os = "macos")'.dev-dependencies]
+# for getting gpu temperature
+macmon.workspace = true
+
[build-dependencies]
anyhow.workspace = true
async-trait.workspace = true
diff --git a/crates/backend-uzu/src/session/chat_session.rs b/crates/backend-uzu/src/session/chat_session.rs
index 788b51baa..2970c98ea 100644
--- a/crates/backend-uzu/src/session/chat_session.rs
+++ b/crates/backend-uzu/src/session/chat_session.rs
@@ -200,7 +200,7 @@ impl ChatSession {
Ok((output, new_context))
}
- fn run_internal(
+ pub fn run_internal(
&mut self,
input: Input,
config: RunConfig,
diff --git a/crates/backend-uzu/tests/common/metrics.rs b/crates/backend-uzu/tests/common/metrics.rs
new file mode 100644
index 000000000..32e502630
--- /dev/null
+++ b/crates/backend-uzu/tests/common/metrics.rs
@@ -0,0 +1,29 @@
+use std::{thread::sleep, time::Duration};
+
+const COOL_GPU_TEMP: f32 = 60.0;
+
+fn get_gpu_temp() -> f32 {
+ #[cfg(target_os = "macos")]
+ {
+ use macmon::Sampler;
+ let mut sample = Sampler::new().unwrap();
+ let metrics = sample.get_metrics(0).unwrap();
+ metrics.temp.gpu_temp_avg
+ }
+
+ #[cfg(not(target_os = "macos"))]
+ 0.0f32
+}
+
+pub fn wait_gpu_cooldown() {
+ let mut gpu_temp = get_gpu_temp();
+ if gpu_temp <= COOL_GPU_TEMP {
+ return;
+ }
+
+ println!("Waiting GPU for cooldown...");
+ while gpu_temp > COOL_GPU_TEMP {
+ sleep(Duration::from_millis(10));
+ gpu_temp = get_gpu_temp();
+ }
+}
diff --git a/crates/backend-uzu/tests/common/mod.rs b/crates/backend-uzu/tests/common/mod.rs
index 30a73dfe6..312bc8c72 100644
--- a/crates/backend-uzu/tests/common/mod.rs
+++ b/crates/backend-uzu/tests/common/mod.rs
@@ -4,6 +4,7 @@ pub mod assert;
pub mod audio;
pub mod helpers;
pub mod matmul;
+pub mod metrics;
pub mod path;
pub mod perf;
pub mod proptest;
diff --git a/crates/backend-uzu/tests/performance/bench_entry.rs b/crates/backend-uzu/tests/performance/bench_entry.rs
index 15072e061..049f2e974 100644
--- a/crates/backend-uzu/tests/performance/bench_entry.rs
+++ b/crates/backend-uzu/tests/performance/bench_entry.rs
@@ -13,3 +13,4 @@ use proc_macros::__internal_uzu_bench as uzu_bench;
#[path = "../common/mod.rs"]
mod common;
mod language_model;
+mod session;
diff --git a/crates/backend-uzu/tests/performance/language_model/language_model_generator_bench.rs b/crates/backend-uzu/tests/performance/language_model/language_model_generator_bench.rs
index 3bd66874a..d0c2950ad 100644
--- a/crates/backend-uzu/tests/performance/language_model/language_model_generator_bench.rs
+++ b/crates/backend-uzu/tests/performance/language_model/language_model_generator_bench.rs
@@ -7,7 +7,10 @@ use backend_uzu::{
};
use criterion::{BenchmarkId, Criterion, Throughput};
-use crate::{common::path::get_test_model_path, uzu_bench};
+use crate::{
+ common::{metrics::wait_gpu_cooldown, path::get_test_model_path},
+ uzu_bench,
+};
fn create_generator<B: Backend>() -> LanguageModelGenerator<B> {
let model_path = get_test_model_path();
@@ -42,6 +45,8 @@ fn bench_forward_pass(c: &mut Criterion) {
// Empty prompt first decode: the generate path requires one seed token.
let tokens: Vec<u64> = vec![0];
for_each_non_cpu_backend!(|B| {
+ wait_gpu_cooldown();
+
let mut group = c.benchmark_group("Forward pass");
group.sample_size(10);
group.throughput(Throughput::Elements(tokens.len() as u64));
diff --git a/crates/backend-uzu/tests/performance/session/chat_session_bench.rs b/crates/backend-uzu/tests/performance/session/chat_session_bench.rs
new file mode 100644
index 000000000..aa3888301
--- /dev/null
+++ b/crates/backend-uzu/tests/performance/session/chat_session_bench.rs
@@ -0,0 +1,58 @@
+use std::time::Duration;
+
+use backend_uzu::{
+ backends::common::Backend,
+ prelude::{DecodingConfig, Input, Output, RunConfig},
+ session::ChatSession,
+};
+use criterion::{BenchmarkId, Criterion};
+
+use crate::{
+ common::{metrics::wait_gpu_cooldown, path::get_test_model_path},
+ uzu_bench,
+};
+
+fn create_session<B: Backend>() -> ChatSession {
+ let model_path = get_test_model_path();
+ let decoding_config = DecodingConfig::default();
+ ChatSession::new_with_backend::<B>(model_path, decoding_config).unwrap()
+}
+
+fn run_session(
+ session: &mut ChatSession,
+ input: Input,
+ tokens_limit: u64,
+) -> Duration {
+ let run_config = RunConfig {
+ tokens_limit,
+ ..Default::default()
+ };
+ let result = session.run_internal(input, run_config, None::<fn(Output) -> bool>).unwrap();
+ Duration::from_secs_f64(result.stats.total_stats.duration)
+}
+
+#[uzu_bench]
+fn bench_chat_session(c: &mut Criterion) {
+ let message = "How are you?";
+ let tokens_limit = 64;
+
+ for_each_non_cpu_backend!(|B| {
+ wait_gpu_cooldown();
+
+ let mut session = create_session::<B>();
+ let mut group = c.benchmark_group("ChatSession run");
+ group.sample_size(10);
+ group.bench_function(BenchmarkId::from_parameter(message), |b| {
+ b.iter_custom(|n_iter| {
+ let mut total_duration = Duration::from_secs(0);
+ for _ in 0..n_iter {
+ session.reset().unwrap();
+ let duration = run_session(&mut session, Input::Text(message.to_string()), tokens_limit);
+ total_duration += duration;
+ }
+ total_duration
+ });
+ });
+ group.finish();
+ });
+}
diff --git a/crates/backend-uzu/tests/performance/session/mod.rs b/crates/backend-uzu/tests/performance/session/mod.rs
new file mode 100644
index 000000000..6e1c28097
--- /dev/null
+++ b/crates/backend-uzu/tests/performance/session/mod.rs
@@ -0,0 +1 @@
+mod chat_session_bench;