add hunyuan_ocr

This commit is contained in:
jhqxxx
2025-12-03 17:21:01 +08:00
parent 7d72cb3baf
commit 697484cf23
29 changed files with 1756 additions and 190 deletions
+14 -4
View File
@@ -1,7 +1,7 @@
use aha::models::{
deepseek_ocr::config::DeepseekOCRConfig, minicpm4::config::MiniCPM4Config,
qwen2_5vl::config::Qwen2_5VLConfig, qwen3vl::config::Qwen3VLConfig,
voxcpm::config::VoxCPMConfig,
deepseek_ocr::config::DeepseekOCRConfig, hunyuan_ocr::config::HunYuanVLConfig,
minicpm4::config::MiniCPM4Config, qwen2_5vl::config::Qwen2_5VLConfig,
qwen3vl::config::Qwen3VLConfig, voxcpm::config::VoxCPMConfig,
};
use anyhow::Result;
@@ -48,10 +48,20 @@ fn qwen3vl_config() -> Result<()> {
#[test]
fn deepseek_ocr_config() -> Result<()> {
// cargo test -F cuda qwen3vl_config -r -- --nocapture
// cargo test -F cuda deepseek_ocr_config -r -- --nocapture
let model_path = "/home/jhq/huggingface_model/deepseek-ai/DeepSeek-OCR/";
let config_path = model_path.to_string() + "/config.json";
let config: DeepseekOCRConfig = serde_json::from_slice(&std::fs::read(config_path)?)?;
println!("{:?}", config);
Ok(())
}
#[test]
fn hunyuan_ocr_config() -> Result<()> {
// cargo test -F cuda hunyuan_ocr_config -r -- --nocapture
let model_path = "/home/jhq/huggingface_model/Tencent-Hunyuan/HunyuanOCR/";
let config_path = model_path.to_string() + "/config.json";
let config: HunYuanVLConfig = serde_json::from_slice(&std::fs::read(config_path)?)?;
println!("{:?}", config);
Ok(())
}
+20 -5
View File
@@ -1,4 +1,6 @@
use aha::utils::tensor_utils::interpolate_bicubic;
use std::time::Instant;
use aha::utils::tensor_utils::interpolate_bilinear;
use anyhow::Result;
use candle_core::Tensor;
@@ -6,11 +8,24 @@ use candle_core::Tensor;
fn messy_test() -> Result<()> {
// RUST_BACKTRACE=1 cargo test -F cuda messy_test -r -- --nocapture
let device = &candle_core::Device::Cpu;
// let t = Tensor::randn(0.0f32, 1.0, (1, 768, 64, 64), device)?;
let t = Tensor::arange(0.0f32, 10.0, device)?.broadcast_as((1, 1, 10, 10))?;
let t = Tensor::arange(0.0f32, 40.0, device)?.broadcast_as((1, 1, 40, 40))?;
println!("t: {}", t);
let t_resized = interpolate_bicubic(&t, (5, 5), Some(true), Some(false))?;
println!("t_resized: {}", t_resized);
let i_start = Instant::now();
let t_inter = interpolate_bilinear(&t, (20, 20), Some(false))?;
let i_duration = i_start.elapsed();
println!("Time elapsed in interpolate_bilinear is: {:?}", i_duration);
println!("t_inter: {}", t_inter);
// let x: Vec<u32> = (0..5).flat_map(|_| 0u32..10).collect();
// let id: Vec<u32> = (0..5).flat_map(|h| vec![h; 10]).collect();
// println!("x: {:?}", id);
// let t = Tensor::randn(0.0f32, 1.0, (1, 768, 64, 64), device)?;
// let t = Tensor::arange(0u32, 10, device)?.broadcast_as((1, 10))?;
// let eq = t.broadcast_eq(&Tensor::new(5u32, device)?)?;
// println!("eq: {}", eq);
// let t = Tensor::arange(0.0f32, 10.0, device)?.broadcast_as((1, 1, 10, 10))?;
// println!("t: {}", t);
// let t_resized = interpolate_bicubic(&t, (5, 5), Some(true), Some(false))?;
// println!("t_resized: {}", t_resized);
// let t1 = Tensor::rand(0.0, 1.0, (1, 5, 5, 10), device)?;
// let t2 = Tensor::rand(0.0, 1.0, (5, 8, 10), device)?;
// let t2 = t2.t()?;
+88
View File
@@ -0,0 +1,88 @@
use std::{pin::pin, time::Instant};
use aha::models::{GenerateModel, hunyuan_ocr::generate::HunyuanOCRGenerateModel};
use aha_openai_dive::v1::resources::chat::ChatCompletionParameters;
use anyhow::Result;
use rocket::futures::StreamExt;
#[test]
fn hunyuan_ocr_generate() -> Result<()> {
// RUST_BACKTRACE=1 cargo test -F cuda hunyuan_ocr_generate -r -- --nocapture
let message = r#"
{
"model": "hunyuan-ocr",
"messages": [
{
"role": "user",
"content": [
{
"type": "image",
"image_url":
{
"url": "file://./assets/img/ocr_test1.png"
}
},
{
"type": "text",
"text": "检测并识别图片中的文字,将文本坐标格式化输出。"
}
]
}
]
}
"#;
let model_path = "/home/jhq/huggingface_model/Tencent-Hunyuan/HunyuanOCR/";
let mes: ChatCompletionParameters = serde_json::from_str(message)?;
let i_start = Instant::now();
let mut model = HunyuanOCRGenerateModel::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 = model.generate(mes)?;
let i_duration = i_start.elapsed();
println!("Time elapsed in generate is: {:?}", i_duration);
println!("generate: \n {:?}", res);
Ok(())
}
#[tokio::test]
async fn hunyuan_ocr_stream() -> Result<()> {
// test with cuda: RUST_BACKTRACE=1 cargo test -F cuda hunyuan_ocr_stream -r -- --nocapture
let message = r#"
{
"model": "hunyuan-ocr",
"messages": [
{
"role": "user",
"content": [
{
"type": "image",
"image_url":
{
"url": "file://./assets/img/ocr_test1.png"
}
},
{
"type": "text",
"text": "检测并识别图片中的文字,将文本坐标格式化输出。"
}
]
}
]
}
"#;
let model_path = "/home/jhq/huggingface_model/Tencent-Hunyuan/HunyuanOCR/";
let mes: ChatCompletionParameters = serde_json::from_str(message)?;
let i_start = Instant::now();
let mut model = HunyuanOCRGenerateModel::init(model_path, None, None)?;
let i_duration = i_start.elapsed();
println!("Time elapsed in load model is: {:?}", i_duration);
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(())
}
+5 -5
View File
@@ -19,15 +19,15 @@ fn qwen3vl_generate() -> Result<()> {
"role": "user",
"content": [
{
"type": "video",
"video_url":
"type": "image",
"image_url":
{
"url": "https://www.w3schools.com/html/movie.mp4"
"url": "file://./assets/img/ocr_test1.png"
}
},
},
{
"type": "text",
"text": "视频中发生了什么"
"text": "请分析图片并提取所有可见文本内容,按从左到右、从上到下的布局,返回纯文本"
}
]
}
+19
View File
@@ -80,3 +80,22 @@ fn deepseekocr_weight() -> Result<()> {
println!("model_list: {:?}", model_list);
Ok(())
}
#[test]
fn hunyuanocr_weight() -> Result<()> {
let model_path = "/home/jhq/huggingface_model/Tencent-Hunyuan/HunyuanOCR/";
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() {
if key.contains(".image_") {
println!("=== {} === {:?}", key, tensor.shape());
}
// println!("=== {} === {:?}", key, tensor.shape());
}
}
println!("model_list: {:?}", model_list);
Ok(())
}