Files
aha/tests/test_minicpm4.rs
T

87 lines
2.9 KiB
Rust
Raw Normal View History

2025-10-03 22:25:58 +08:00
use std::{pin::pin, time::Instant};
2025-09-25 12:09:25 +08:00
2025-10-15 21:03:49 +08:00
use aha::models::{GenerateModel, minicpm4::generate::MiniCPMGenerateModel};
2025-10-27 10:55:40 +08:00
use aha_openai_dive::v1::resources::chat::ChatCompletionParameters;
2025-09-25 12:09:25 +08:00
use anyhow::Result;
2025-10-03 22:25:58 +08:00
use rocket::futures::StreamExt;
2025-09-25 12:09:25 +08:00
#[test]
2025-10-03 22:25:58 +08:00
fn minicpm_generate() -> Result<()> {
2025-11-22 23:27:14 +08:00
// test with cpu :(太慢了, : RUST_BACKTRACE=1 cargo test minicpm_generate -r -- --nocapture
2026-03-08 21:20:24 +08:00
// test with cuda: RUST_BACKTRACE=1 cargo test -F cuda --test test_minicpm4 minicpm_generate -r -- --nocapture
2025-11-22 23:27:14 +08:00
// test with cuda+flash-attn: RUST_BACKTRACE=1 cargo test -F cuda,flash-attn minicpm_generate -r -- --nocapture
2025-10-15 21:03:49 +08:00
2026-01-08 00:04:24 +08:00
let save_dir =
aha::utils::get_default_save_dir().ok_or(anyhow::anyhow!("Failed to get save dir"))?;
let model_path = format!("{}/OpenBMB/MiniCPM4-0.5B/", save_dir);
2025-09-25 12:09:25 +08:00
let message = r#"
{
2025-10-03 22:25:58 +08:00
"temperature": 0.3,
"top_p": 0.8,
2025-09-25 12:09:25 +08:00
"model": "minicpm4",
"messages": [
{
"role": "user",
2025-10-11 13:14:51 +08:00
"content": "你吃饭了没"
2025-09-25 12:09:25 +08:00
}
]
}
"#;
let mes: ChatCompletionParameters = serde_json::from_str(message)?;
let i_start = Instant::now();
2026-01-08 00:04:24 +08:00
let mut model = MiniCPMGenerateModel::init(&model_path, None, None)?;
2025-09-25 12:09:25 +08:00
let i_duration = i_start.elapsed();
println!("Time elapsed in load model is: {:?}", i_duration);
let i_start = Instant::now();
let result = model.generate(mes)?;
let i_duration = i_start.elapsed();
2025-12-10 16:44:20 +08:00
println!("generate: \n {:?}", result);
if result.usage.is_some() {
let num_token = result.usage.as_ref().unwrap().total_tokens;
let duration_secs = i_duration.as_secs_f64();
let tps = num_token as f64 / duration_secs;
println!("Tokens per second (TPS): {:.2}", tps);
}
2025-09-25 12:09:25 +08:00
println!("Time elapsed in generate is: {:?}", i_duration);
Ok(())
}
#[tokio::test]
2025-10-03 22:25:58 +08:00
async fn minicpm_stream() -> Result<()> {
2025-11-22 23:27:14 +08:00
// test with cuda+flash-attn: RUST_BACKTRACE=1 cargo test -F cuda,flash-attn minicpm_stream -r -- --nocapture
2025-10-15 21:03:49 +08:00
2026-01-08 00:04:24 +08:00
let save_dir =
aha::utils::get_default_save_dir().ok_or(anyhow::anyhow!("Failed to get save dir"))?;
let model_path = format!("{}/OpenBMB/MiniCPM4-0.5B/", save_dir);
2025-09-25 12:09:25 +08:00
let message = r#"
{
2025-10-03 22:25:58 +08:00
"model": "minicpm4",
2025-09-25 12:09:25 +08:00
"messages": [
{
"role": "user",
2025-10-03 22:25:58 +08:00
"content": "你是谁"
2025-09-25 12:09:25 +08:00
}
]
}
"#;
let mes: ChatCompletionParameters = serde_json::from_str(message)?;
let i_start = Instant::now();
2026-01-08 00:04:24 +08:00
let mut model = MiniCPMGenerateModel::init(&model_path, None, None)?;
2025-09-25 12:09:25 +08:00
let i_duration = i_start.elapsed();
println!("Time elapsed in load model is: {:?}", i_duration);
let i_start = Instant::now();
let mut stream = pin!(model.generate_stream(mes)?);
while let Some(item) = stream.next().await {
println!("generate: \n {:?}", item);
}
let i_duration = i_start.elapsed();
println!("Time elapsed in generate is: {:?}", i_duration);
Ok(())
}