add Qwen3VL model

This commit is contained in:
jhqxxx
2025-10-26 21:39:23 +08:00
parent cdae7fc45f
commit 7b190e7d54
22 changed files with 2591 additions and 53 deletions
+11 -2
View File
@@ -1,6 +1,5 @@
use aha::models::{
minicpm4::config::MiniCPM4Config, qwen2_5vl::config::Qwen2_5VLConfig,
voxcpm::config::VoxCPMConfig,
minicpm4::config::MiniCPM4Config, qwen2_5vl::config::Qwen2_5VLConfig, qwen3vl::config::Qwen3VLConfig, voxcpm::config::VoxCPMConfig
};
use anyhow::Result;
@@ -34,3 +33,13 @@ fn voxcpm_config() -> Result<()> {
println!("{:?}", config);
Ok(())
}
#[test]
fn qwen3vl_config() -> Result<()> {
// cargo test -F cuda qwen3vl_config -- --nocapture
let model_path = "/home/jhq/huggingface_model/Qwen/Qwen3-VL-4B-Instruct/";
let config_path = model_path.to_string() + "/config.json";
let config: Qwen3VLConfig = serde_json::from_slice(&std::fs::read(config_path)?)?;
println!("{:?}", config);
Ok(())
}
+18 -6
View File
@@ -1,13 +1,25 @@
use aha::utils::audio_utils::load_audio_with_resample;
use aha::utils::{tensor_utils::bitor_tensor};
use anyhow::Result;
use candle_core::Tensor;
#[test]
fn messy_test() -> Result<()> {
let device = candle_core::Device::Cpu;
let wav_path = "./assets/audio/voice_01.wav";
let audio_tensor = load_audio_with_resample(wav_path, device, Some(16000))?;
println!("audio_tensor: {}", audio_tensor);
let device = &candle_core::Device::Cpu;
let image_mask = Tensor::new(vec![0u32, 0, 0, 1, 0, 1], device)?;
let video_mask = Tensor::new(vec![0u32, 1, 0, 1, 0, 1], device)?;
let visual_mask = bitor_tensor(&image_mask, &video_mask)?;
println!("visual_mask: {}", visual_mask);
// let x = Tensor::arange_step(0.0_f32, 5., 0.5, &device)?;
// let x_int = x.to_dtype(candle_core::DType::U32)?;
// println!("x: {}", x);
// println!("x_int: {}", x_int);
// let x_affine = x_int.affine(1.0, 1.0)?;
// println!("x_affine: {}", x_affine);
// let x_clamp = x_affine.clamp(0u32, 3u32)?;
// println!("x_clamp: {}", x_clamp);
// let wav_path = "./assets/audio/voice_01.wav";
// let audio_tensor = load_audio_with_resample(wav_path, device, Some(16000))?;
// println!("audio_tensor: {}", audio_tensor);
// let string = "你好啊".to_string();
// let vec_str: Vec<String>= string.chars().map(|c| c.to_string()).collect();
// println!("vec_str: {:?}", vec_str);
+94
View File
@@ -0,0 +1,94 @@
use std::{pin::pin, time::Instant};
use aha::models::{GenerateModel, qwen3vl::generate::Qwen3VLGenerateModel};
use anyhow::Result;
use openai_dive::v1::resources::chat::ChatCompletionParameters;
use rocket::futures::StreamExt;
#[test]
fn qwen3vl_generate() -> Result<()> {
// test with cuda: RUST_BACKTRACE=1 cargo test -F cuda qwen3vl_generate -- --nocapture
let model_path = "/home/jhq/huggingface_model/Qwen/Qwen3-VL-2B-Instruct/";
let message = r#"
{
"model": "qwen3vl",
"messages": [
{
"role": "user",
"content": [
{
"type": "video",
"video_url":
{
"url": "./assets/video/video_test.mp4"
}
},
{
"type": "text",
"text": "视频中发生了什么."
}
]
}
]
}
"#;
let mes: ChatCompletionParameters = serde_json::from_str(message)?;
let i_start = Instant::now();
let mut qwen3vl = Qwen3VLGenerateModel::init(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 res = qwen3vl.generate(mes)?;
let i_duration = i_start.elapsed();
println!("generate: \n {:?}", res);
println!("Time elapsed in generate is: {:?}", i_duration);
Ok(())
}
#[tokio::test]
async fn qwen3vl_stream() -> Result<()> {
// test with cuda: RUST_BACKTRACE=1 cargo test -F cuda qwen3vl_stream -- --nocapture
let model_path = "/home/jhq/huggingface_model/Qwen/Qwen3-VL-2B-Instruct/";
let message = r#"
{
"model": "qwen3vl",
"messages": [
{
"role": "user",
"content": [
{
"type": "image",
"image_url":
{
"url": "file://./assets/img/voxcpm.png"
}
},
{
"type": "text",
"text": "描述这张图片"
}
]
}
]
}
"#;
let mes: ChatCompletionParameters = serde_json::from_str(message)?;
let i_start = Instant::now();
let mut qwen3vl = Qwen3VLGenerateModel::init(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!(qwen3vl.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(())
}
+16
View File
@@ -45,3 +45,19 @@ fn voxcpm_weight() -> Result<()> {
);
Ok(())
}
#[test]
fn qwen3vl_weight() -> Result<()> {
let model_path = "/home/jhq/huggingface_model/Qwen/Qwen3-VL-4B-Instruct/";
let model_list = find_type_files(model_path, "safetensors")?;
let device = Device::Cpu;
for m in &model_list {
let weights = safetensors::load(m, &device)?;
for (key, tensor) in weights.iter() {
println!("=== {} === {:?}", key, tensor.shape());
}
}
println!("model_list: {:?}", model_list);
Ok(())
}