update voxcpm model init

This commit is contained in:
jhqxxx
2025-10-11 14:16:49 +08:00
parent c8cfda7ac4
commit 2bac7672e1
3 changed files with 6 additions and 7 deletions
+5 -6
View File
@@ -19,9 +19,7 @@ pub struct VoxCPMGenerate {
impl VoxCPMGenerate { impl VoxCPMGenerate {
pub fn init(path: &str, device: Option<&Device>, dtype: Option<DType>) -> Result<Self> { pub fn init(path: &str, device: Option<&Device>, dtype: Option<DType>) -> Result<Self> {
let device = &get_device(device); let device = &get_device(device);
let config_path = path.to_string() + "/config.json";
let config: VoxCPMConfig = serde_json::from_slice(&std::fs::read(config_path)?)?;
let cfg_dtype = config.dtype.as_str();
let model_list = find_type_files(path, "pth")?; let model_list = find_type_files(path, "pth")?;
// println!(" pth model_list: {:?}", model_list); // println!(" pth model_list: {:?}", model_list);
let mut dict_to_hashmap = HashMap::new(); let mut dict_to_hashmap = HashMap::new();
@@ -48,18 +46,19 @@ impl VoxCPMGenerate {
let model_list = find_type_files(path, "bin")?; let model_list = find_type_files(path, "bin")?;
// println!(" bin model_list: {:?}", model_list); // println!(" bin model_list: {:?}", model_list);
dict_to_hashmap = HashMap::new(); dict_to_hashmap = HashMap::new();
let config_path = path.to_string() + "/config.json";
let config: VoxCPMConfig = serde_json::from_slice(&std::fs::read(config_path)?)?;
let cfg_dtype = config.dtype.as_str();
let mut m_dtype = get_dtype(dtype, cfg_dtype); let mut m_dtype = get_dtype(dtype, cfg_dtype);
for m in model_list { for m in model_list {
let dict = read_all_with_key(m, Some("state_dict"))?; let dict = read_all_with_key(m, Some("state_dict"))?;
m_dtype = dict[0].1.dtype();
for (k, v) in dict { for (k, v) in dict {
// println!("key: {}, tensor shape: {:?}", k, v); // println!("key: {}, tensor shape: {:?}", k, v);
dict_to_hashmap.insert(k, v); dict_to_hashmap.insert(k, v);
} }
} }
// println!("model dtype: {:?}", m_dtype);
let vb_voxcpm = VarBuilder::from_tensors(dict_to_hashmap, m_dtype, device); let vb_voxcpm = VarBuilder::from_tensors(dict_to_hashmap, m_dtype, device);
let config_path = path.to_string() + "/config.json";
let config: VoxCPMConfig = serde_json::from_slice(&std::fs::read(config_path)?)?;
let tokenizer = SingleChineseTokenizer::new(path)?; let tokenizer = SingleChineseTokenizer::new(path)?;
let voxcpm = VoxCPMModel::new(vb_voxcpm, config, tokenizer, audio_vae)?; let voxcpm = VoxCPMModel::new(vb_voxcpm, config, tokenizer, audio_vae)?;
+1 -1
View File
@@ -585,7 +585,7 @@ impl VoxCPMModel {
) -> Result<Tensor> { ) -> Result<Tensor> {
let text_token = text_token.unsqueeze(0)?; let text_token = text_token.unsqueeze(0)?;
let text_mask = text_mask.unsqueeze(0)?; let text_mask = text_mask.unsqueeze(0)?;
let audio_feat = audio_feat.unsqueeze(0)?.to_dtype(DType::BF16)?; let audio_feat = audio_feat.unsqueeze(0)?.to_dtype(self.dtype)?;
let audio_mask = audio_mask.unsqueeze(0)?; let audio_mask = audio_mask.unsqueeze(0)?;
let latent_pred = self.inference( let latent_pred = self.inference(
BIN
View File
Binary file not shown.