Files
aha/tests/weight_test.rs
T

122 lines
3.9 KiB
Rust
Raw Normal View History

2025-10-03 22:25:58 +08:00
use std::collections::HashMap;
2025-10-15 21:03:49 +08:00
use aha::utils::{find_type_files, get_device};
2025-09-25 12:09:25 +08:00
use anyhow::Result;
2025-10-15 21:03:49 +08:00
use candle_core::{Device, pickle::read_all_with_key, safetensors};
2025-10-03 22:25:58 +08:00
use candle_nn::VarBuilder;
2025-09-25 12:09:25 +08:00
#[test]
fn minicpm4_weight() -> Result<()> {
let model_path = "/home/jhq/huggingface_model/OpenBMB/MiniCPM4-0.5B/";
2025-10-15 21:03:49 +08:00
let model_list = find_type_files(model_path, "safetensors")?;
2025-09-25 12:09:25 +08:00
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(())
2025-10-03 22:25:58 +08:00
}
#[test]
fn voxcpm_weight() -> Result<()> {
let model_path = "/home/jhq/huggingface_model/openbmb/VoxCPM-0.5B/";
2025-10-15 21:03:49 +08:00
let model_list = find_type_files(model_path, "pth")?;
2025-10-03 22:25:58 +08:00
println!("model_list: {:?}", model_list);
let dev = get_device(None);
let mut dict_to_hashmap = HashMap::new();
let mut dtype = candle_core::DType::F16;
2025-10-15 21:03:49 +08:00
for m in model_list {
let dict = read_all_with_key(m, Some("state_dict"))?;
2025-10-03 22:25:58 +08:00
dtype = dict[0].1.dtype();
for (k, v) in dict {
println!("key: {}, tensor shape: {:?}", k, v);
dict_to_hashmap.insert(k, v);
2025-10-15 21:03:49 +08:00
}
2025-10-03 22:25:58 +08:00
}
let vb = VarBuilder::from_tensors(dict_to_hashmap, dtype, &dev);
let contain_key = vb.contains_tensor("encoder.block.4.block.2.block.3.weight_g");
2025-10-15 21:03:49 +08:00
println!(
"contain encoder.block.4.block.2.block.3.weight_g: {}",
contain_key
);
2025-10-03 22:25:58 +08:00
Ok(())
2025-10-10 14:17:42 +08:00
}
2025-10-26 21:39:23 +08:00
2025-12-11 18:33:35 +08:00
#[test]
fn voxcpm1_5_weight() -> Result<()> {
let model_path = "/home/jhq/huggingface_model/OpenBMB/VoxCPM1.5/";
let model_list = find_type_files(model_path, "pth")?;
println!("model_list: {:?}", model_list);
2025-12-11 23:37:47 +08:00
// let dev = get_device(None);
2025-12-11 18:33:35 +08:00
let mut dict_to_hashmap = HashMap::new();
2025-12-11 23:37:47 +08:00
// let mut dtype = candle_core::DType::F32;
2025-12-11 18:33:35 +08:00
for m in model_list {
let dict = read_all_with_key(m, Some("state_dict"))?;
2025-12-11 23:37:47 +08:00
// dtype = dict[0].1.dtype();
2025-12-11 18:33:35 +08:00
for (k, v) in dict {
println!("key: {}, tensor shape: {:?}", k, v);
dict_to_hashmap.insert(k, v);
}
}
2025-12-11 23:30:07 +08:00
2025-12-11 18:33:35 +08:00
Ok(())
}
2025-10-26 21:39:23 +08:00
#[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")?;
2025-10-26 21:57:53 +08:00
2025-10-26 21:39:23 +08:00
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(())
2025-10-26 21:57:53 +08:00
}
2025-11-22 23:27:14 +08:00
#[test]
fn deepseekocr_weight() -> Result<()> {
let model_path = "/home/jhq/huggingface_model/deepseek-ai/DeepSeek-OCR/";
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() {
2025-11-25 17:45:09 +08:00
if key.contains("rel_pos_h") {
2025-11-22 23:27:14 +08:00
println!("=== {} === {:?}", key, tensor.shape());
}
// println!("=== {} === {:?}", key, tensor.shape());
}
}
println!("model_list: {:?}", model_list);
Ok(())
}
2025-12-03 17:21:01 +08:00
#[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(())
}