1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// use std::io::Cursor;
// use std::fs::File;
// use symphonia::core::io::MediaSourceStream;
// use std::io::{Read, Seek};
// use std::{io::Cursor, time::Instant};
// use aha::utils::tensor_utils::get_mask_from_lengths;
// use aha::utils::tensor_utils::repeat_interleave;
// use crate::params::chat::ChatCompletionParameters;
use anyhow::Result;
// use candle_core::Tensor;
// use kaldi_native_fbank::{
// FbankComputer, FbankOptions,
// window::{Window, extract_window},
// };
// use byteorder::{LittleEndian, ReadBytesExt};
// use candle_core::Tensor;
use modelscope::{DownloadOptions, ModelScope};
// use sentencepiece::SentencePieceProcessor;
// use zip::ZipArchive;
#[tokio::test]
async fn download_test() -> Result<()> {
// cargo test -F cuda --test messy_test download_test -r -- --nocapture
let model_id = "unsloth/Qwen3.5-4B-GGUF";
let model_name = "Qwen3.5-4B-IQ4_NL.gguf";
let save_dir =
aha::utils::get_default_save_dir().ok_or(anyhow::anyhow!("Failed to get save dir"))?;
let _ = ModelScope::download_with_options(
model_id,
save_dir,
DownloadOptions {
files: (vec![model_name.to_string()]).into(),
},
)
.await;
Ok(())
}
#[test]
fn messy_test() -> Result<()> {
// RUST_BACKTRACE=1 cargo test -F cuda --test messy_test messy_test -r -- --nocapture
// let device = aha::Device::Cpu;
let device = aha::Device::new_cuda(0)?;
println!("{:?}", device);
// let input = Tensor::new(&[5u32, 4, 3, 6], &device)?;
// let mask = get_mask_from_lengths(&input)?;
// println!("{}", mask);
// let audio_path = "file:///home/jhq/python_code/FireRedASR2S/assets/hello_zh.wav";
// let device = aha::Device::Cpu;
// let wave = load_audio_with_resample(audio_path, &device, Some(16000), true)?;
// println!("len: {}", wave);
// let wave = wave.squeeze(0)?.to_vec1::<f32>()?;
// println!("wave len: {}", wave.len());
// let mut opts = FbankOptions::default();
// opts.frame_opts.dither = 0.0;
// opts.frame_opts.samp_freq = 16000.;
// opts.frame_opts.frame_length_ms = 25.;
// opts.frame_opts.frame_shift_ms = 10.;
// opts.frame_opts.snip_edges = true;
// opts.mel_opts.num_bins = 80;
// opts.mel_opts.debug_mel = false;
// opts.use_energy = false;
// let mut comp =
// FbankComputer::new(opts.clone()).map_err(|e| anyhow!("fbank comput err: {e}"))?;
// let win = Window::new(&opts.frame_opts).unwrap();
// let padded = opts.frame_opts.padded_window_size();
// let mut feats = vec![];
// let mut window_buf = vec![0.0; padded];
// for frame in 0..230 {
// let raw_log_energy = extract_window(
// 0,
// &wave,
// frame,
// &opts.frame_opts,
// Some(&win),
// &mut window_buf,
// )
// .unwrap();
// let mut feat = vec![0.0; comp.dim()];
// comp.compute(raw_log_energy, 1.0, &mut window_buf, &mut feat);
// feats.push(feat);
// }
// let feats = Tensor::new(feats, &aha::Device::Cpu)?;
// println!("feats: {}", feats);
// println!("wave len: {}", wave.len());
// let mut feats = vec![];
// let frame_num = (wave.len() + padded - 1) / padded;
// for i in 0..frame_num {
// let mut window_buf = vec![0.0; padded];
// let raw_log_energy =
// extract_window(0, &wave, i, &opts.frame_opts, Some(&win), &mut window_buf)
// .map_err(|_| anyhow!("extract_window err"))?;
// let mut feat = vec![0.0; comp.dim()];
// comp.compute(raw_log_energy, 1.0, &mut window_buf, &mut feat);
// feats.push(feat);
// }
// let feats = Tensor::new(feats, &aha::Device::Cpu)?;
// println!("feats: {:?}", feats);
// let mut window_buf = vec![0.0; padded];
// println!("window_buf len: {}", window_buf.len());
// let raw_log_energy = extract_window(0, &wave, 0, &opts.frame_opts, Some(&win), &mut window_buf)
// .map_err(|_| anyhow!("extract_window err"))?;
// let mut feat = vec![0.0; comp.dim()];
// println!("feat len: {}", feat.len());
// comp.compute(raw_log_energy, 1.0, &mut window_buf, &mut feat);
// println!("{feat:?}");
// let model = WhichModel::LFM2_1_2B;
// println!("model: {:?}, model_id: {}", model, model.as_string());
// let model_list = WhichModel::model_list();
// println!("model_list: {:#?}", model_list);
// println!("当前秒级时间戳: {}", timestamp());
// println!("当前毫秒级时间戳: {}", timestamp_millis());
// let t1 = Tensor::randn(0.0, 1.0, (1, 2, 6), device)?;
// println!(" t1: {}", t1);
// let t2 = t1.pad_with_zeros(D::Minus1, -3, 0)?;
// println!(" t2: {}", t);
// let save_dir =
// aha::utils::get_default_save_dir().ok_or(anyhow::anyhow!("Failed to get save dir"))?;
// let model_path = format!("{}/deepseek-ai/DeepSeek-OCR-2/", save_dir);
// let stem = std::path::Path::new(&model_path)
// .file_name()
// .and_then(|s| s.to_str())
// .unwrap_or("qwen3.5");
// println!("stem: {:?}", stem);
// let device = &candle_core::Device::Cpu;
// let t1 = Tensor::randn(0.0, 1.0, (16, 9, 64, 128), device)?;
// let t2 = Tensor::randn(0.0, 1.0, (16, 9, 128, 64), device)?;
// let out = t1.matmul(&t2)?;
// println!("out shape: {:?}", out);
// let input = Tensor::arange(0.0f32, 25.0f32, device)?.reshape((5, 5))?;
// println!("input: {}", input);
// // let input = input.unsqueeze(D::Minus1)?;
// // let input = input.repeat((1, 1, 2))?;
// // let input = input.flatten(D::Minus2, D::Minus1)?;
// let output = repeat_interleave(&input, 2, 1)?;
// println!("output: {}", output);
// let x_nearest = interpolate_nearest_2d(&input, (10, 10))?;
// println!("x_nearest: {}", x_nearest);
// let input = Tensor::arange(0.0f32, 25.0f32, device)?.reshape((1, 5, 5))?;
// println!("input: {}", input);
// let x_nearest = interpolate_nearest_1d(&input, 10)?;
// println!("x_nearest: {}", x_nearest);
// 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 emo_matrix_path = model_path.clone() + "/feat2.pt";
// let t_emo = load_tensor_from_pt(
// &emo_matrix_path,
// "feat2/data/0",
// Shape::from_dims(&[73, 1280]),
// device,
// )?;
// println!("t_emo: {}", t_emo);
// let skp_matrix_path = model_path + "/feat1.pt";
// let t_skp = load_tensor_from_pt(
// &skp_matrix_path,
// "feat1/data/0",
// Shape::from_dims(&[73, 192]),
// device,
// )?;
// println!("t_skp: {}", t_skp);
// let file = File::open(emo_matrix_path)?;
// let mut archive = ZipArchive::new(file)?;
// // 列出所有文件(调试用)
// for i in 0..archive.len() {
// let file = archive.by_index(i)?;
// println!("File: {} ({} bytes)", file.name(), file.size());
// }
// // 读取原始字节数据
// let mut data_file = archive.by_name("feat2/data/0")?;
// let mut buffer = Vec::new();
// data_file.read_to_end(&mut buffer)?;
// // 将字节转换为 f32 (little endian)
// let mut cursor = Cursor::new(buffer);
// let num_elements = 73 * 1280; // 93,440
// let mut data = Vec::with_capacity(num_elements);
// for _ in 0..num_elements {
// let val = cursor.read_f32::<LittleEndian>()?;
// data.push(val);
// }
// let t = Tensor::from_vec(data, (73, 1280), device)?;
// println!("t: {}", t);
// let message = r#"
// {
// "model": "index-tts2",
// "messages": [
// {
// "role": "user",
// "content": [
// {
// "type": "audio",
// "audio_url":
// {
// "url": "file:///home/jhq/Videos/voice_01.wav"
// }
// },
// {
// "type": "text",
// "text": "你好啊"
// }
// ]
// }
// ],
// "metadata": {"emo_vector": "[0, 0, 0, 0, 0, 0, 0.45, 0]"}
// }
// "#;
// let mes: ChatCompletionParameters = serde_json::from_str(message)?;
// if let Some(map) = &mes.metadata
// && let Some(emo_vector_str) = map.get("emo_vector")
// {
// match serde_json::from_str::<Vec<f32>>(emo_vector_str) {
// Ok(emo_vector) => {
// println!("Parsed emo_vector: {:?}", emo_vector);
// // 现在 emo_vector 是 Vec<f32>: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.45, 0.0]
// }
// Err(e) => {
// eprintln!("Failed to parse emo_vector: {}", e);
// }
// }
// }
// let save_dir =
// 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 bpe_path = model_path.to_string() + "/bpe.model";
// let tokenizer = SentencePieceProcessor::open(bpe_path)
// .map_err(|e| anyhow!(format!("load bpe.model file error:{}", e)))?;
// let tokens = tokenizer
// .encode("你好啊")
// .map_err(|e| anyhow!(format!("tokenizer encode error:{}", e)))?;
// println!("tokens: {:?}", tokens);
// let t = Tensor::arange(0.0f32, 40.0, device)?.broadcast_as((1, 40, 40))?;
// println!("t: {}", t);
// 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);
// 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()?;
// let vec_u8 = response.bytes()?.to_vec();
// let mut content = Cursor::new(vec_u8);
// let mss = MediaSourceStream::new(Box::new(content), Default::default());
// let window = create_hann_window(400, DType::F32, device)?;
// println!("window: {}", window);
// let audio_path = "file:///home/jhq/Videos/voice_01.wav";
// let audio_path = "/home/jhq/Videos/zh.mp3";
// let audio_path = "/home/jhq/Videos/zh.mp3";
// // let audio_tensor = load_and_resample_audio_rubato(audio_path, 16000, device)?;
// // let audio_tensor = load_audio_with_resample(audio_path, device, Some(16000))?;
// // println!("audio_tensor: {}", audio_tensor);
// #[cfg(feature = "ffmpeg")]
// {
// use aha::utils::audio_utils::load_and_resample_audio_ffmpeg;
// let audio_tensor = load_and_resample_audio_ffmpeg(audio_path, Some(16000), device)?;
// println!("audio_tensor: {}", audio_tensor);
// }
// // let path = get_default_save_dir();
// // let x = Tensor::new(array, device)
// let x = Tensor::arange(0.0, 9.0, device)?;
// println!("x: {}", x);
// let x = x
// .unsqueeze(0)?
// .unsqueeze(0)?
// .broadcast_as((5, 5, 9))?
// .reshape((5, 5, 3, 3))?;
// println!("x: {}", x);
// let x = x.permute((0, 2, 1, 3))?;
// println!("x: {}", x);
// let x = x.reshape((15, 15))?;
// println!("x: {}", x);
// let xs = Tensor::rand(0.0, 5.0, (1, 1, 3, 3), device)?;
// println!("xs: {}", xs);
// let xs = xs.pad_with_zeros(3, 2, 2)?
// .pad_with_zeros(2, 2, 2)?;
// println!("xs: {}", xs);
// let xs = Tensor::arange(0.0, 25.0, device)?;
// println!("xs: {}", xs);
// let splits = split_tensor_with_size(&xs, 5, 0)?;
// for v in splits {
// println!("v: {}", v);
// }
// let xs = Tensor::arange(0.0, 25.0, device)?.broadcast_as((1, 1, 5, 5))?;
// println!("xs: {}", xs);
// let xs = xs.avg_pool2d(5)?;
// println!("xs: {}", xs);
// let xs = Tensor::rand(0.0, 1.0, (1, 4, 4, 2), device)?;
// println!("xs: {}", xs);
// let shape = Shape::from_dims(&[1, 2, 2, 2, 2, 2]);
// let xs = xs.reshape(shape)?;
// println!("xs: {}", xs);
// let x0 = xs.i((.., .., 0, .., 0, ..))?;
// let x1 = xs.i((.., .., 1, .., 0, ..))?;
// let x2 = xs.i((.., .., 0, .., 1, ..))?;
// let x3 = xs.i((.., .., 1, .., 1, ..))?;
// let xs = Tensor::cat(&[x0, x1, x2, x3], D::Minus1)?;
// println!("xs: {}", xs);
// let xs = xs.reshape((1, (), 4 * 2))?;
// println!("xs: {}", xs);
// let path_str = "file://./assets/img/ocr_test1.png";
// let path = url::Url::from_str(path_str)?;
// let path = path.to_file_path();
// let path = match path {
// Ok(path) => path,
// Err(_) => {
// let mut path = path_str.to_owned();
// path = path.split_off(7);
// PathBuf::from(path)
// }
// };
// println!("to file path: {:?}", path);
// let device = &candle_core::Device::Cpu;
// let t = Tensor::arange(0.0f32, 40.0, device)?.broadcast_as((1, 1, 40, 40))?;
// println!("t: {}", t);
// 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()?;
// println!("t2: {:?}", t2);
// let re = t1.broadcast_matmul(&t2)?;
// println!("re: {:?}", re);
// let index = Tensor::arange(0u32, 10u32, device)?;
// let index_2d_vec = vec![index;5];
// let index_2d = Tensor::stack(&index_2d_vec, 0)?;
// println!("index_2d: {}", index_2d);
// let t = Tensor::rand(0.0, 1.0, (20, 8), device)?;
// println!("t: {}", t);
// let res = index_select_2d(&t, &index_2d)?;
// println!("res: {}", res);
// let t = Tensor::arange(0.0, 10.0, device)?
// .unsqueeze(0)?
// .unsqueeze(0)?;
// println!("t: {}", t);
// let t_resized = interpolate_linear(&t, 20, None)?;
// println!("t_resized: {}", t_resized);
// let grid_thw = Tensor::new(vec![vec![3u32, 12, 20], vec![5, 30, 25]], device)?;
// let cu_seqlens = grid_thw.i((.., 1))?.mul(&grid_thw.i((.., 2))?)?;
// let grid_t = grid_thw.i((.., 0))?.to_vec1::<u32>()?;
// println!("cu_seqlens: {}", cu_seqlens);
// println!("cu_seqlens rank: {}", cu_seqlens.rank());
// println!("grid_t: {:?}", grid_t);
// 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);
// let t = Tensor::rand(-1.0, 1.0, (2, 2), &device)?;
// println!("t: {}", t);
// let re_t = t.recip()?;
// println!("re_t: {}", re_t);
Ok(())
}