From 38ea72e40754b31cbcae7b01435e4a0364d563fe Mon Sep 17 00:00:00 2001 From: XiaoYang Date: Sun, 8 Feb 2026 11:07:53 +0800 Subject: [PATCH] refactor: reorganize imports and improve code formatting across multiple modules --- src/exec/qwen3_asr.rs | 3 +-- src/models/campplus/mod.rs | 10 +++++++--- src/models/feature_extractor/config.rs | 2 +- src/models/feature_extractor/mod.rs | 4 ++-- src/models/glm_asr_nano/config.rs | 2 -- src/models/glm_asr_nano/processor.rs | 1 - src/models/index_tts2/generate.rs | 2 +- src/models/index_tts2/mod.rs | 2 +- src/models/index_tts2/processor.rs | 9 ++++++--- src/models/index_tts2/utils.rs | 8 ++++---- src/models/mask_gct/config.rs | 2 +- src/models/mask_gct/mod.rs | 2 +- src/models/mask_gct/model.rs | 26 ++++++++++++++++++++++---- src/models/qwen3_asr/generate.rs | 1 - src/models/w2v_bert_2_0/config.rs | 2 +- src/models/w2v_bert_2_0/mod.rs | 2 +- src/models/w2v_bert_2_0/model.rs | 8 +++----- src/position_embed/rope.rs | 2 +- src/utils/audio_utils.rs | 4 +++- src/utils/tensor_utils.rs | 2 +- tests/messy_test.rs | 11 +++++++---- tests/test_index_tts2.rs | 4 ++-- tests/test_qwen3_asr.rs | 2 +- tests/weight_test.rs | 8 ++++---- 24 files changed, 71 insertions(+), 48 deletions(-) diff --git a/src/exec/qwen3_asr.rs b/src/exec/qwen3_asr.rs index 44e2687..2b6bde0 100644 --- a/src/exec/qwen3_asr.rs +++ b/src/exec/qwen3_asr.rs @@ -5,14 +5,13 @@ use std::time::Instant; use anyhow::{Ok, Result}; use crate::exec::ExecModel; +use crate::models::GenerateModel; use crate::models::qwen3_asr::generate::Qwen3AsrGenerateModel; -use crate::models::{GenerateModel}; pub struct Qwen3ASRExec; impl ExecModel for Qwen3ASRExec { fn run(input: &[String], output: Option<&str>, weight_path: &str) -> Result<()> { - let i_start = Instant::now(); let mut model = Qwen3AsrGenerateModel::init(weight_path, None, None)?; let i_duration = i_start.elapsed(); diff --git a/src/models/campplus/mod.rs b/src/models/campplus/mod.rs index d5638e5..fdca544 100644 --- a/src/models/campplus/mod.rs +++ b/src/models/campplus/mod.rs @@ -25,7 +25,11 @@ impl Shortcut { ) -> Result { let conv_0 = get_conv2d(vb.pp("0"), in_c, out_c, ks, padding, 1, 1, 1, bias)?; let bn_1 = get_batch_norm(vb.pp("1"), 1e-5, out_c, true)?; - Ok(Self { conv_0, bn_1, stride }) + Ok(Self { + conv_0, + bn_1, + stride, + }) } pub fn forward(&self, x: &Tensor) -> Result { @@ -36,7 +40,7 @@ impl Shortcut { let indices = Tensor::arange(0u32, half_h as u32, x.device())?.affine(2.0, 0.0)?; x = x.index_select(&indices, 2)?; } - x = self.bn_1.forward_t(&x, false)?; + x = self.bn_1.forward_t(&x, false)?; Ok(x) } } @@ -106,7 +110,7 @@ impl BasicResBlock { } else { xs = xs.add(&residual)?; } - xs = xs.relu()?; + xs = xs.relu()?; Ok(xs) } } diff --git a/src/models/feature_extractor/config.rs b/src/models/feature_extractor/config.rs index b79a94d..8334c30 100644 --- a/src/models/feature_extractor/config.rs +++ b/src/models/feature_extractor/config.rs @@ -18,4 +18,4 @@ pub struct FeatureExtractor { fn default_sampling_rate() -> usize { 16000 -} \ No newline at end of file +} diff --git a/src/models/feature_extractor/mod.rs b/src/models/feature_extractor/mod.rs index 80016f5..6e3fd69 100644 --- a/src/models/feature_extractor/mod.rs +++ b/src/models/feature_extractor/mod.rs @@ -1,3 +1,3 @@ -pub mod seamless_m4t_feature_extractor; +pub mod config; pub mod feature_extraction_whisper; -pub mod config; \ No newline at end of file +pub mod seamless_m4t_feature_extractor; diff --git a/src/models/glm_asr_nano/config.rs b/src/models/glm_asr_nano/config.rs index ea90498..cf2a3dd 100644 --- a/src/models/glm_asr_nano/config.rs +++ b/src/models/glm_asr_nano/config.rs @@ -11,8 +11,6 @@ pub struct GlmAsrNanoProcessorConfig { pub max_audio_len: usize, } - - #[derive(Debug, Clone, PartialEq, Deserialize)] pub struct GlmAsrNanoConfig { pub audio_config: GlmAsrAudioConfig, diff --git a/src/models/glm_asr_nano/processor.rs b/src/models/glm_asr_nano/processor.rs index e20222d..a54a563 100644 --- a/src/models/glm_asr_nano/processor.rs +++ b/src/models/glm_asr_nano/processor.rs @@ -1,4 +1,3 @@ - use aha_openai_dive::v1::resources::chat::ChatCompletionParameters; use anyhow::Result; use candle_core::{D, DType, Device, IndexOp, Tensor}; diff --git a/src/models/index_tts2/generate.rs b/src/models/index_tts2/generate.rs index 923e7a3..dddaf81 100644 --- a/src/models/index_tts2/generate.rs +++ b/src/models/index_tts2/generate.rs @@ -19,7 +19,7 @@ impl IndexTTS2Generate { let device = get_device(device); let dtype = get_dtype(dtype, "bf16"); let processor = IndexTTS2Processor::new(path, &save_dir, &config, &device, dtype)?; - + Ok(Self { config, processor }) } pub fn generate(&mut self, mes: ChatCompletionParameters) -> Result<()> { diff --git a/src/models/index_tts2/mod.rs b/src/models/index_tts2/mod.rs index 6634bcd..d3c894b 100644 --- a/src/models/index_tts2/mod.rs +++ b/src/models/index_tts2/mod.rs @@ -2,4 +2,4 @@ pub mod config; pub mod generate; pub mod model; pub mod processor; -pub mod utils; \ No newline at end of file +pub mod utils; diff --git a/src/models/index_tts2/processor.rs b/src/models/index_tts2/processor.rs index 88c8e28..6985552 100644 --- a/src/models/index_tts2/processor.rs +++ b/src/models/index_tts2/processor.rs @@ -5,13 +5,16 @@ use candle_nn::VarBuilder; use crate::{ models::{ - campplus::CAMPPlus, feature_extractor::seamless_m4t_feature_extractor::SeamlessM4TFeatureExtractor, index_tts2::config::{IndexTTS2Config, PreprocessParams}, mask_gct::model::RepCodec, w2v_bert_2_0::model::W2VBert2_0Model + campplus::CAMPPlus, + feature_extractor::seamless_m4t_feature_extractor::SeamlessM4TFeatureExtractor, + index_tts2::config::{IndexTTS2Config, PreprocessParams}, + mask_gct::model::RepCodec, + w2v_bert_2_0::model::W2VBert2_0Model, }, utils::{ audio_utils::{ create_hann_window, extract_audio_url, get_waveform_and_window_properties, kaldi_fbank, - kaldi_get_mel_banks, load_audio, mel_filter_bank, resample_simple, - torch_stft, + kaldi_get_mel_banks, load_audio, mel_filter_bank, resample_simple, torch_stft, }, get_vb_model_path, tensor_utils::pad_reflect_last_dim, diff --git a/src/models/index_tts2/utils.rs b/src/models/index_tts2/utils.rs index e5c45fa..5ef628f 100644 --- a/src/models/index_tts2/utils.rs +++ b/src/models/index_tts2/utils.rs @@ -7,12 +7,12 @@ pub async fn download_index_tts2_need_model(save_dir: Option<&str>) -> anyhow::R }; let w2v_bert2_0 = "facebook/w2v-bert-2.0"; - let mask_gct= "amphion/MaskGCT"; + let mask_gct = "amphion/MaskGCT"; // let campplus= "funasr/campplus"; // huggingface - let campplus = "iic/speech_campplus_sv_zh-cn_16k-common"; // modelscope + let campplus = "iic/speech_campplus_sv_zh-cn_16k-common"; // modelscope download_model(w2v_bert2_0, &save_dir, 3).await?; download_model(mask_gct, &save_dir, 3).await?; download_model(campplus, &save_dir, 3).await?; - + Ok(()) -} \ No newline at end of file +} diff --git a/src/models/mask_gct/config.rs b/src/models/mask_gct/config.rs index a38930b..455562d 100644 --- a/src/models/mask_gct/config.rs +++ b/src/models/mask_gct/config.rs @@ -18,4 +18,4 @@ fn default_num_quantizers() -> usize { fn default_downsample_scale() -> usize { 1 -} \ No newline at end of file +} diff --git a/src/models/mask_gct/mod.rs b/src/models/mask_gct/mod.rs index 06008be..2852cdb 100644 --- a/src/models/mask_gct/mod.rs +++ b/src/models/mask_gct/mod.rs @@ -1,2 +1,2 @@ +pub mod config; pub mod model; -pub mod config; \ No newline at end of file diff --git a/src/models/mask_gct/model.rs b/src/models/mask_gct/model.rs index 7dd9c8f..167f645 100644 --- a/src/models/mask_gct/model.rs +++ b/src/models/mask_gct/model.rs @@ -116,10 +116,28 @@ impl FactorizedVectorQuantize { use_l2_normlize: bool, ) -> Result { let (in_project, out_project) = if input_dim != codebook_dim { - let in_project = - WNConv1d::new(vb.pp("in_project"), input_dim, codebook_dim, 1, 1, 0, 1, 1, true)?; - let out_project = - WNConv1d::new(vb.pp("out_project"), codebook_dim, input_dim, 1, 1, 0, 1, 1, true)?; + let in_project = WNConv1d::new( + vb.pp("in_project"), + input_dim, + codebook_dim, + 1, + 1, + 0, + 1, + 1, + true, + )?; + let out_project = WNConv1d::new( + vb.pp("out_project"), + codebook_dim, + input_dim, + 1, + 1, + 0, + 1, + 1, + true, + )?; (Some(in_project), Some(out_project)) } else { (None, None) diff --git a/src/models/qwen3_asr/generate.rs b/src/models/qwen3_asr/generate.rs index 22b70cd..087e9d1 100644 --- a/src/models/qwen3_asr/generate.rs +++ b/src/models/qwen3_asr/generate.rs @@ -1,4 +1,3 @@ - use aha_openai_dive::v1::resources::chat::{ ChatCompletionChunkResponse, ChatCompletionParameters, ChatCompletionResponse, }; diff --git a/src/models/w2v_bert_2_0/config.rs b/src/models/w2v_bert_2_0/config.rs index 51142ac..4b34833 100644 --- a/src/models/w2v_bert_2_0/config.rs +++ b/src/models/w2v_bert_2_0/config.rs @@ -58,4 +58,4 @@ pub struct W2VBert2_0Config { pub use_weighted_layer_sum: bool, pub vocab_size: Option, pub xvector_output_dim: usize, -} \ No newline at end of file +} diff --git a/src/models/w2v_bert_2_0/mod.rs b/src/models/w2v_bert_2_0/mod.rs index 3621b7a..2852cdb 100644 --- a/src/models/w2v_bert_2_0/mod.rs +++ b/src/models/w2v_bert_2_0/mod.rs @@ -1,2 +1,2 @@ pub mod config; -pub mod model; \ No newline at end of file +pub mod model; diff --git a/src/models/w2v_bert_2_0/model.rs b/src/models/w2v_bert_2_0/model.rs index fd4ddfd..ac48603 100644 --- a/src/models/w2v_bert_2_0/model.rs +++ b/src/models/w2v_bert_2_0/model.rs @@ -7,9 +7,7 @@ use candle_nn::{ use crate::{ models::{ - common::{ - GLU, TwoLinearMLP, eager_attention_forward, get_conv1d, get_layer_norm, - }, + common::{GLU, TwoLinearMLP, eager_attention_forward, get_conv1d, get_layer_norm}, w2v_bert_2_0::config::W2VBert2_0Config, }, position_embed::rope::{RoPE, apply_rotary_pos_emb}, @@ -488,7 +486,7 @@ impl Wav2Vec2BertEncoder { for (i, layer) in (&self.layers).iter().enumerate() { if output_hidden_states { hidden_states.push(xs.clone()); - } + } if let Some(id) = layer_id && id == i { @@ -500,7 +498,7 @@ impl Wav2Vec2BertEncoder { sin.as_ref(), attention_mask.as_ref(), conv_attention_mask, - )?; + )?; } let hidden_states = if hidden_states.len() > 0 { Some(hidden_states) diff --git a/src/position_embed/rope.rs b/src/position_embed/rope.rs index 82ed91d..319dfdb 100644 --- a/src/position_embed/rope.rs +++ b/src/position_embed/rope.rs @@ -350,7 +350,7 @@ impl Qwen3VLTextRotaryEmbedding { // for dim in 1..3 { for (dim, offset) in (1..3).enumerate() { - let dim = dim +1; + let dim = dim + 1; let length = mrope_section[dim]; let idx = Tensor::arange_step(offset as u32, length as u32, 3, freqs.device())?; let src = freqs.i(dim)?.contiguous()?; // (bs, seq_len, head_dim //2) diff --git a/src/utils/audio_utils.rs b/src/utils/audio_utils.rs index 1ec8ace..7eb22df 100644 --- a/src/utils/audio_utils.rs +++ b/src/utils/audio_utils.rs @@ -32,7 +32,9 @@ use symphonia::core::meta::MetadataOptions; use symphonia::core::probe::Hint; use crate::utils::get_default_save_dir; -use crate::utils::tensor_utils::{linspace, log10, pad_reflect_last_dim, pad_replicate_last_dim, split_tensor}; +use crate::utils::tensor_utils::{ + linspace, log10, pad_reflect_last_dim, pad_replicate_last_dim, split_tensor, +}; // 重采样方法枚举 #[derive(Debug, Clone, Copy)] diff --git a/src/utils/tensor_utils.rs b/src/utils/tensor_utils.rs index 025d82a..a890619 100644 --- a/src/utils/tensor_utils.rs +++ b/src/utils/tensor_utils.rs @@ -111,7 +111,7 @@ pub fn split_tensor_with_size( // "input tensor dim size % splits_size must be equal to 0" // ); for (i, split) in (0..dim_size).step_by(splits_size).enumerate() { - let size = splits_size.min(dim_size - i*splits_size); + let size = splits_size.min(dim_size - i * splits_size); split_res.push(t.narrow(dim, split, size)?); } Ok(split_res) diff --git a/tests/messy_test.rs b/tests/messy_test.rs index d7cfe61..4ac15cb 100644 --- a/tests/messy_test.rs +++ b/tests/messy_test.rs @@ -2,9 +2,9 @@ use std::time::Instant; -use aha::utils::{tensor_utils::interpolate_nearest_1d}; +use aha::utils::tensor_utils::interpolate_nearest_1d; use anyhow::Result; -use candle_core::{Tensor}; +use candle_core::Tensor; // use symphonia::core::io::MediaSourceStream; #[test] @@ -16,8 +16,11 @@ fn messy_test() -> Result<()> { let i_start = Instant::now(); let t_inter = interpolate_nearest_1d(&t, 20)?; let i_duration = i_start.elapsed(); - println!("Time elapsed in interpolate_nearest_1d is: {:?}", i_duration); - println!("t_inter: {}", t_inter); + println!( + "Time elapsed in interpolate_nearest_1d is: {:?}", + i_duration + ); + println!("t_inter: {}", t_inter); // let url = "https://sis-sample-audio.obs.cn-north-1.myhuaweicloud.com/16k16bit.mp3"; // let client = reqwest::blocking::Client::new(); // let response = client.get(url).send()?; diff --git a/tests/test_index_tts2.rs b/tests/test_index_tts2.rs index 1fd1745..bbd7d61 100644 --- a/tests/test_index_tts2.rs +++ b/tests/test_index_tts2.rs @@ -1,8 +1,8 @@ use std::time::Instant; -use anyhow::Result; use aha::models::index_tts2::{generate::IndexTTS2Generate, utils::download_index_tts2_need_model}; use aha_openai_dive::v1::resources::chat::ChatCompletionParameters; +use anyhow::Result; #[tokio::test] async fn index_tts2_generate() -> Result<()> { @@ -45,4 +45,4 @@ async fn index_tts2_generate() -> Result<()> { let i_duration = i_start.elapsed(); println!("Time elapsed in generate is: {:?}", i_duration); Ok(()) -} \ No newline at end of file +} diff --git a/tests/test_qwen3_asr.rs b/tests/test_qwen3_asr.rs index ad237c3..dc75cd4 100644 --- a/tests/test_qwen3_asr.rs +++ b/tests/test_qwen3_asr.rs @@ -9,7 +9,7 @@ fn qwen3_asr_generate() -> Result<()> { // RUST_BACKTRACE=1 cargo test -F cuda qwen3_asr_generate -r -- --nocapture let save_dir = aha::utils::get_default_save_dir().ok_or(anyhow::anyhow!("Failed to get save dir"))?; - let model_path = format!("{}/Qwen/Qwen3-ASR-0.6B/", save_dir); //Qwen/Qwen3-ASR-1.7B + let model_path = format!("{}/Qwen/Qwen3-ASR-0.6B/", save_dir); //Qwen/Qwen3-ASR-1.7B let message = r#" { "model": "qwen3-asr", diff --git a/tests/weight_test.rs b/tests/weight_test.rs index f433397..a1ad635 100644 --- a/tests/weight_test.rs +++ b/tests/weight_test.rs @@ -202,8 +202,8 @@ fn qwen3_weight() -> Result<()> { fn index_tts2_weight() -> Result<()> { let save_dir: String = aha::utils::get_default_save_dir().ok_or(anyhow::anyhow!("Failed to get save dir"))?; - let model_path = format!("{}/IndexTeam/IndexTTS-2/", save_dir); - let s2mel_path = model_path+ "/s2mel.pth"; + let model_path = format!("{}/IndexTeam/IndexTTS-2/", save_dir); + let s2mel_path = model_path + "/s2mel.pth"; // let wac2vec2_path = model_path+ "/wav2vec2bert_stats.pt"; // let model_path = format!("{}/iic/speech_campplus_sv_zh-cn_16k-common/", save_dir); // let campplus_path = model_path+ "/campplus_cn_common.bin"; @@ -229,9 +229,9 @@ fn index_tts2_weight() -> Result<()> { // let model_list = vec![semantic_codec_path]; // for m in model_list { // let weights = safetensors::load(m, &device)?; - // for (key, tensor) in weights.iter() { + // for (key, tensor) in weights.iter() { // println!("=== {} === {:?}", key, tensor.shape()); // } // } Ok(()) -} \ No newline at end of file +}