add minicpm with a bug

This commit is contained in:
jhqxxx
2025-09-25 12:09:25 +08:00
parent ec989f397f
commit 4fdab3e7cd
15 changed files with 900 additions and 49 deletions
+15 -4
View File
@@ -1,12 +1,23 @@
use aha::models::qwen2_5vl::config::Config;
use aha::models::{minicpm4::config::MiniCPM4Config, qwen2_5vl::config::Qwen2_5VLConfig};
use anyhow::Result;
#[test]
fn qwen2_5vl_config() -> Result<()> {
// cargo test qwen2_5vl_config -- --nocapture
fn qwen2_5_vl_config() -> Result<()> {
// cargo test -F cuda,flash-attn qwen2_5vl_config -- --nocapture
let model_path = "/home/jhq/huggingface_model/Qwen/Qwen2.5-VL-3B-Instruct/";
let config_path = model_path.to_string() + "/config.json";
let config: Config = serde_json::from_slice(&std::fs::read(config_path)?)?;
let config: Qwen2_5VLConfig = serde_json::from_slice(&std::fs::read(config_path)?)?;
println!("{:?}", config);
Ok(())
}
#[test]
fn minicpm4_config() -> Result<()> {
// cargo test -F cuda,flash-attn minicpm4_config -- --nocapture
let model_path = "/home/jhq/huggingface_model/OpenBMB/MiniCPM4-0.5B/";
let config_path = model_path.to_string() + "/config.json";
let config: MiniCPM4Config = serde_json::from_slice(&std::fs::read(config_path)?)?;
println!("{:?}", config);
Ok(())
}
+97
View File
@@ -0,0 +1,97 @@
use std::time::Instant;
use anyhow::Result;
use candle_core::{DType, Device};
use openai_dive::v1::resources::chat::ChatCompletionParameters;
#[test]
fn qwen2_5vl_generate() -> Result<()> {
// test with cpu :(太慢了, : RUST_BACKTRACE=1 cargo test qwen2_5vl_generate -- --nocapture
// test with cuda: RUST_BACKTRACE=1 cargo test -F cuda qwen2_5vl_generate -- --nocapture
// test with cuda+flash-attn: RUST_BACKTRACE=1 cargo test -F cuda,flash-attn qwen2_5vl_generate -- --nocapture
let device = Device::cuda_if_available(0)?;
let dtype = DType::BF16;
let model_path = "/home/jhq/huggingface_model/OpenBMB/MiniCPM4-0.5B/";
let message = r#"
{
"model": "minicpm4",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "你是谁"
}
]
}
]
}
"#;
let mes: ChatCompletionParameters = serde_json::from_str(message)?;
let i_start = Instant::now();
// let mut model = Qwen2_5VLGenerateModel::init(model_path, &device, dtype)?;
let mut model = ModelType::init(ModelType::Qwen2_5VL, model_path, None, None)?;
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]
async fn qwen2_5vl_stream() -> Result<()> {
// test with cuda+flash-attn: RUST_BACKTRACE=1 cargo test -F cuda,flash-attn qwen2_5vl_generate -- --nocapture
let device = Device::cuda_if_available(0)?;
let dtype = DType::BF16;
let model_path = "/home/jhq/huggingface_model/Qwen/Qwen2.5-VL-3B-Instruct/";
let message = r#"
{
"model": "qwen2.5vl",
"messages": [
{
"role": "user",
"content": [
{
"type": "image",
"image_url":
{
"url": "file://./assets/img/ocr_test.png"
}
},
{
"type": "text",
"text": "请分析图片并提取所有可见文本内容,按从左到右、从上到下的布局,返回纯文本"
}
]
}
]
}
"#;
let mes: ChatCompletionParameters = serde_json::from_str(message)?;
let i_start = Instant::now();
// let mut model = Qwen2_5VLGenerateModel::init(model_path, &device, dtype)?;
let mut model = ModelType::init(ModelType::Qwen2_5VL, model_path, None, None)?;
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(())
}
+18
View File
@@ -0,0 +1,18 @@
use aha::utils::utils::find_safetensors_files;
use anyhow::Result;
use candle_core::{safetensors, Device};
#[test]
fn minicpm4_weight() -> Result<()> {
let model_path = "/home/jhq/huggingface_model/OpenBMB/MiniCPM4-0.5B/";
let model_list = find_safetensors_files(&model_path)?;
let device = Device::Cpu;
for m in model_list {
let weights = safetensors::load(m, &device)?;
for (key, tensor) in weights.iter() {
println!("=== {} ===", key);
println!("Shape: {:?}", tensor.shape());
println!("DType: {:?}", tensor.dtype());
}
}
Ok(())
}