2025-10-03 22:25:58 +08:00
|
|
|
use std::{pin::pin, time::Instant};
|
2025-09-25 12:09:25 +08:00
|
|
|
|
2025-10-03 22:25:58 +08:00
|
|
|
use aha::models::{minicpm4::generate::MiniCPMGenerateModel, GenerateModel};
|
2025-09-25 12:09:25 +08:00
|
|
|
use anyhow::Result;
|
|
|
|
|
use candle_core::{DType, Device};
|
|
|
|
|
use openai_dive::v1::resources::chat::ChatCompletionParameters;
|
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<()> {
|
|
|
|
|
// test with cpu :(太慢了, : RUST_BACKTRACE=1 cargo test minicpm_generate -- --nocapture
|
|
|
|
|
// test with cuda: RUST_BACKTRACE=1 cargo test -F cuda minicpm_generate -- --nocapture
|
|
|
|
|
// test with cuda+flash-attn: RUST_BACKTRACE=1 cargo test -F cuda,flash-attn minicpm_generate -- --nocapture
|
|
|
|
|
|
2025-09-25 12:09:25 +08:00
|
|
|
let model_path = "/home/jhq/huggingface_model/OpenBMB/MiniCPM4-0.5B/";
|
|
|
|
|
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();
|
2025-10-03 22:25:58 +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)?;
|
|
|
|
|
println!("generate: \n {:?}", result);
|
|
|
|
|
let i_duration = i_start.elapsed();
|
|
|
|
|
println!("Time elapsed in generate is: {:?}", i_duration);
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
2025-10-03 22:25:58 +08:00
|
|
|
async fn minicpm_stream() -> Result<()> {
|
|
|
|
|
// test with cuda+flash-attn: RUST_BACKTRACE=1 cargo test -F cuda,flash-attn minicpm_stream -- --nocapture
|
|
|
|
|
|
|
|
|
|
let model_path = "/home/jhq/huggingface_model/OpenBMB/MiniCPM4-0.5B/";
|
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();
|
2025-10-03 22:25:58 +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(())
|
|
|
|
|
}
|