Spaces:
Sleeping
Sleeping
File size: 13,317 Bytes
f768eb3 |
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 |
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright [2024-09-27] <sxc19@mails.tsinghua.edu.cn, Xingchen Song>
import os
import time
from typing import Dict, Any
import numpy as np
import onnxruntime
import pytest
import s3tokenizer
import torch
def create_test_audio(duration_seconds: float = 20,
sample_rate: int = 16000) -> torch.Tensor:
"""Create synthetic test audio"""
length = int(duration_seconds * sample_rate)
# Create sinusoidal mixed audio
t = torch.linspace(0, duration_seconds, length)
audio = 0.5 * torch.sin(2 * torch.pi * 440 * t) # 440Hz fundamental
audio += 0.3 * torch.sin(2 * torch.pi * 880 * t) # 880Hz second harmonic
audio += 0.1 * torch.randn(length) # Add noise
return audio
@pytest.fixture
def test_audio_suite():
"""Create a suite of test audios with different lengths"""
return {
"short_audio_1": create_test_audio(5.0), # 5 seconds
"short_audio_2": create_test_audio(15.0), # 15 seconds
"medium_audio": create_test_audio(25.0), # 25 seconds
"medium_audio_2": create_test_audio(30.0), # 30 seconds
"long_audio": create_test_audio(
35.0), # 35 seconds - for torch and onnx, 2 segments with padding
"long_audio_2": create_test_audio(
56.0
), # 56 seconds - for torch and onnx, exactly 2 segments without padding
"very_long_audio": create_test_audio(
60.0), # 60 seconds - for torch and onnx, 3 segments with padding
}
def onnx_inference_short_audio(model_name: str, mel: torch.Tensor,
mel_len: torch.Tensor) -> torch.Tensor:
"""
ONNX inference for short audio (<=30s)
"""
# Load ONNX model
default = os.path.join(os.path.expanduser("~"), ".cache")
download_root = os.path.join(os.getenv("XDG_CACHE_HOME", default),
"s3tokenizer")
option = onnxruntime.SessionOptions()
option.graph_optimization_level = onnxruntime.GraphOptimizationLevel.ORT_ENABLE_ALL
option.intra_op_num_threads = 1
providers = ["CPUExecutionProvider"]
ort_session = onnxruntime.InferenceSession(
f"{download_root}/{model_name}.onnx",
sess_options=option,
providers=providers)
# Direct inference for short audio
onnx_output = ort_session.run(
None, {
ort_session.get_inputs()[0].name:
mel[:, :mel_len.item()].unsqueeze(0).detach().cpu().numpy(),
ort_session.get_inputs()[1].name:
np.array([mel_len.item()], dtype=np.int32)
})[0]
# Convert to numpy array to fix linter issues
onnx_output = np.array(onnx_output)
# Handle different output formats
if onnx_output.ndim == 2:
onnx_output = onnx_output[0, :]
elif onnx_output.ndim == 3:
onnx_output = onnx_output[0, 0, :]
return torch.tensor(onnx_output, dtype=torch.long)
def onnx_inference_long_audio(model_name: str, mel: torch.Tensor,
mel_len: torch.Tensor) -> torch.Tensor:
"""
ONNX inference for long audio (>30s) using sliding window approach
Based on _quantize_mixed_batch logic
Note: This may fail due to ONNX model limitations with dynamic lengths
"""
# Load ONNX model
default = os.path.join(os.path.expanduser("~"), ".cache")
download_root = os.path.join(os.getenv("XDG_CACHE_HOME", default),
"s3tokenizer")
option = onnxruntime.SessionOptions()
option.graph_optimization_level = onnxruntime.GraphOptimizationLevel.ORT_ENABLE_ALL
option.intra_op_num_threads = 1
providers = ["CPUExecutionProvider"]
ort_session = onnxruntime.InferenceSession(
f"{download_root}/{model_name}.onnx",
sess_options=option,
providers=providers)
# Parameters for sliding window (same as _quantize_mixed_batch)
sample_rate = 16000
hop_length = 160
window_size = 30 # seconds
overlap = 4 # seconds
# Calculate frame-based parameters
frames_per_window = window_size * sample_rate // hop_length # 3000 frames
frames_per_overlap = overlap * sample_rate // hop_length # 400 frames
frames_per_stride = frames_per_window - frames_per_overlap # 2600 frames
# Split into segments
segments = []
segments_len = []
start = 0
while start < mel_len.item():
end = min(start + frames_per_window, mel_len.item())
segment = mel[:, start:end]
if segment.size(1) < frames_per_window:
break
seg_len = segment.size(1)
segments.append(segment)
segments_len.append(seg_len)
start += frames_per_stride
if not segments:
raise ValueError("No valid segments for ONNX processing")
# Process each segment with ONNX
segment_results = []
for i, (segment, seg_len) in enumerate(zip(segments, segments_len)):
try:
onnx_output = ort_session.run(
None, {
ort_session.get_inputs()[0].name:
segment.unsqueeze(0).detach().cpu().numpy(),
ort_session.get_inputs()[1].name:
np.array([seg_len], dtype=np.int32)
})[0]
# Convert to numpy array to fix linter issues
onnx_output = np.array(onnx_output)
# Handle different output formats
if onnx_output.ndim == 2:
segment_codes = onnx_output[0, :].tolist()
elif onnx_output.ndim == 3:
segment_codes = onnx_output[0, 0, :].tolist()
else:
segment_codes = onnx_output.tolist()
segment_results.append(segment_codes)
except Exception as e:
print(f" ONNX error on segment {i+1}: {str(e)[:100]}...")
raise Exception(
f"ONNX inference failed on segment {i+1}: {str(e)}")
if not segment_results:
raise ValueError("All ONNX segments failed to process")
# Merge segments using the same logic as _quantize_mixed_batch
# Determine token rate based on model name
if model_name == "speech_tokenizer_v1":
token_rate = 50
else:
token_rate = 25
merged_codes = s3tokenizer.merge_tokenized_segments(
segment_results, overlap=overlap, token_rate=token_rate
)[:-overlap * token_rate] # NOTE(xcsong): drop the last overlap part.
return torch.tensor(merged_codes, dtype=torch.long)
def onnx_inference_with_long_audio_support(
model_name: str, mel: torch.Tensor,
mel_len: torch.Tensor) -> torch.Tensor:
"""
ONNX inference with automatic long audio support
"""
max_frames = 3000 # 30s * 16000 / 160 = 3000 frames
if mel_len.item() <= max_frames:
# Short audio - use direct inference
return onnx_inference_short_audio(model_name, mel, mel_len)
else:
# Long audio - use sliding window approach
return onnx_inference_long_audio(model_name, mel, mel_len)
def compare_torch_vs_onnx_single(model_name: str, audio: torch.Tensor,
audio_name: str) -> Dict[str, Any]:
"""Test single audio with both torch and onnx versions"""
duration = audio.shape[0] / 16000
# Load torch model
tokenizer = s3tokenizer.load_model(model_name)
tokenizer.eval()
# Prepare input
mel = s3tokenizer.log_mel_spectrogram(audio)
mels = mel.unsqueeze(0)
mels_lens = torch.tensor([mel.size(1)])
# Test torch version
start_time = time.time()
with torch.no_grad():
torch_codes, torch_codes_lens = tokenizer.quantize(mels, mels_lens)
torch_time = time.time() - start_time
torch_result = torch_codes[0, :torch_codes_lens[0].item()]
# Test onnx version with long audio support
try:
start_time = time.time()
onnx_result = onnx_inference_with_long_audio_support(
model_name, mel, mels_lens[0])
onnx_time = time.time() - start_time
# Compare results
min_len = min(len(torch_result), len(onnx_result))
torch_truncated = torch_result[:min_len]
onnx_truncated = onnx_result[:min_len]
are_equal = torch.equal(torch_truncated, onnx_truncated)
miss_rate = 0.0
if not are_equal:
miss_num = torch.sum(~(torch_truncated == onnx_truncated))
miss_rate = miss_num.item() * 100.0 / min_len
return {
"audio_name": audio_name,
"model_name": model_name,
"duration": duration,
"torch_tokens": torch_truncated,
"onnx_tokens": onnx_truncated,
"torch_time": torch_time,
"onnx_time": onnx_time,
"results_match": are_equal,
"miss_rate": miss_rate
}
except Exception as e:
return {
"audio_name": audio_name,
"model_name": model_name,
"duration": duration,
"torch_tokens": torch_result,
"onnx_tokens": [],
"torch_time": torch_time,
"onnx_time": 0.0,
"results_match": False,
"miss_rate": 100.0,
"error": str(e)
}
@pytest.mark.parametrize("model_name", [
"speech_tokenizer_v1", "speech_tokenizer_v1_25hz",
"speech_tokenizer_v2_25hz"
])
def test_torch_vs_onnx_short_audio(model_name, test_audio_suite):
"""Test torch vs onnx for short audio (<=30s)"""
print(f"\n=== Testing {model_name} on Short Audio ===")
short_audios = {
k: v
for k, v in test_audio_suite.items() if v.shape[0] / 16000 <= 30
}
results = []
for audio_name, audio in short_audios.items():
result = compare_torch_vs_onnx_single(model_name, audio, audio_name)
results.append(result)
duration = result["duration"]
torch_tokens = result["torch_tokens"]
onnx_tokens = result["onnx_tokens"]
match_status = "✅" if result["results_match"] else "❌"
print(
f"{match_status} {audio_name}: {duration:.1f}s → torch:{len(torch_tokens)}, onnx:{len(onnx_tokens)}"
)
if not result["results_match"] and "error" not in result:
print(f" Miss rate: {result['miss_rate']:.2f}%")
print(
f" torch_tokens:\n{torch_tokens}\nonnx_tokens:\n{onnx_tokens}"
)
# Assertions
successful_tests = [r for r in results if "error" not in r]
assert len(successful_tests) == len(
short_audios
), f"successful tests ({len(successful_tests)}) for {model_name} should be equal to number of short audios ({len(short_audios)})" # noqa
# For short audio, we expect reasonable match rate
for r in results:
assert r[
'miss_rate'] < 0.5, f"Miss rate too high for {model_name}: {r['miss_rate']:.2f}%"
print(f"\n{model_name} Short Audio Summary:")
print(f" Successful tests: {len(successful_tests)}/{len(results)}")
@pytest.mark.parametrize("model_name", [
"speech_tokenizer_v1", "speech_tokenizer_v1_25hz",
"speech_tokenizer_v2_25hz"
])
def test_torch_vs_onnx_long_audio(model_name, test_audio_suite):
"""Test torch vs onnx for long audio (>30s) with ONNX sliding window implementation"""
print(
f"\n=== Testing {model_name} on Long Audio (ONNX Sliding Window) ===")
long_audios = {
k: v
for k, v in test_audio_suite.items() if v.shape[0] / 16000 > 30
}
results = []
for audio_name, audio in long_audios.items():
result = compare_torch_vs_onnx_single(model_name, audio, audio_name)
results.append(result)
duration = result["duration"]
torch_tokens = result["torch_tokens"]
onnx_tokens = result["onnx_tokens"]
match_status = "✅" if result["results_match"] else "❌"
print(
f"{match_status} {audio_name}: {duration:.1f}s → torch:{len(torch_tokens)}, onnx:{len(onnx_tokens)}"
)
if not result["results_match"] and "error" not in result:
print(f" Miss rate: {result['miss_rate']:.2f}%")
print(
f" torch_tokens:\n{torch_tokens}\nonnx_tokens:\n{onnx_tokens}"
)
elif "error" in result:
print(f" Error: {result['error'][:100]}...")
# For long audio with ONNX, we document the current limitations
successful_tests = [r for r in results if "error" not in r]
assert len(successful_tests) == len(
long_audios
), f"successful tests ({len(successful_tests)}) for {model_name} should be equal to number of long audios ({len(long_audios)})" # noqa
print(f"\n{model_name} Long Audio Results:")
print(f" Total tests: {len(results)}")
print(f" Successful ONNX tests: {len(successful_tests)}")
for r in results:
# NOTE(xcsong): 0.5% is a reasonable miss rate for long audio, since we drop the last overlap part.
assert r[
'miss_rate'] < 0.5, f"Miss rate too high for {model_name}: {r['miss_rate']}%"
# The main requirement is that Torch always works
print(" ✅ Torch processing works reliably for all long audio")
if __name__ == "__main__":
# Run tests with pytest
pytest.main([__file__, "-v"])
|