forked from groxaxo/Qwen3-TTS-Openai-Fastapi
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_optimizations.py
More file actions
300 lines (230 loc) · 9.64 KB
/
test_optimizations.py
File metadata and controls
300 lines (230 loc) · 9.64 KB
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
#!/usr/bin/env python3
"""
Test various PyTorch optimizations for Qwen3-TTS inference speed.
This script tests:
1. torch.compile() with different modes
2. TF32 precision
3. cuDNN benchmark mode
4. SDPA backend selection
5. Different torch.compile modes (reduce-overhead, max-autotune)
"""
import time
import torch
import numpy as np
from qwen_tts import Qwen3TTSModel
# Test texts of varying lengths
TEST_CASES = [
("Hello world!", "Short"),
("The quick brown fox jumps over the lazy dog.", "Sentence"),
("Artificial intelligence is transforming the way we live and work. From healthcare to transportation, AI is making our lives easier and more efficient.", "Medium"),
("In recent years, artificial intelligence has made remarkable progress across many domains. Machine learning algorithms can now recognize images, understand natural language, and even generate creative content like music and art. As these technologies continue to advance, they promise to revolutionize industries and create new opportunities for innovation.", "Long"),
]
def benchmark_model(model, test_cases, label="Baseline", warmup=1):
"""Benchmark model inference speed."""
results = []
# Warmup
print(f"\n[{label}] Warming up with {warmup} iterations...")
for _ in range(warmup):
_, _ = model.generate_custom_voice(
text="Warmup test.",
language="English",
speaker="Vivian",
)
# Clear CUDA cache
if torch.cuda.is_available():
torch.cuda.synchronize()
torch.cuda.empty_cache()
print(f"\n[{label}] Running benchmarks...")
for text, name in test_cases:
# Time the generation
start = time.time()
wavs, sr = model.generate_custom_voice(
text=text,
language="English",
speaker="Vivian",
)
if torch.cuda.is_available():
torch.cuda.synchronize() # Ensure GPU operations complete
elapsed = time.time() - start
# Calculate Real-Time Factor
audio_duration = len(wavs[0]) / sr
rtf = elapsed / audio_duration
word_count = len(text.split())
results.append({
'name': name,
'text': text,
'words': word_count,
'latency': elapsed,
'rtf': rtf,
'audio_duration': audio_duration,
})
print(f" {name:10s} ({word_count:2d}w): {elapsed:6.2f}s (RTF: {rtf:.2f}, Audio: {audio_duration:.2f}s)")
# Calculate average
avg_latency = np.mean([r['latency'] for r in results])
avg_rtf = np.mean([r['rtf'] for r in results])
print(f"\n[{label}] Average: {avg_latency:.2f}s latency, RTF {avg_rtf:.2f}")
return results, avg_latency, avg_rtf
def test_baseline(model_name):
"""Test baseline performance (Flash Attention 2, no torch.compile)."""
print("\n" + "="*80)
print("TEST 1: BASELINE (Flash Attention 2, no torch.compile)")
print("="*80)
model = Qwen3TTSModel.from_pretrained(
model_name,
device_map="cuda:0",
dtype=torch.bfloat16,
attn_implementation="flash_attention_2",
)
results, avg_lat, avg_rtf = benchmark_model(model, TEST_CASES, "Baseline")
del model
torch.cuda.empty_cache()
return results, avg_lat, avg_rtf
def test_torch_compile_reduce_overhead(model_name):
"""Test torch.compile() with reduce-overhead mode."""
print("\n" + "="*80)
print("TEST 2: torch.compile(mode='reduce-overhead')")
print("="*80)
model = Qwen3TTSModel.from_pretrained(
model_name,
device_map="cuda:0",
dtype=torch.bfloat16,
attn_implementation="flash_attention_2",
)
print("Compiling model with reduce-overhead mode...")
model.model = torch.compile(
model.model,
mode="reduce-overhead",
fullgraph=False,
)
results, avg_lat, avg_rtf = benchmark_model(model, TEST_CASES, "Compile-ReduceOverhead", warmup=2)
del model
torch.cuda.empty_cache()
return results, avg_lat, avg_rtf
def test_torch_compile_max_autotune(model_name):
"""Test torch.compile() with max-autotune mode."""
print("\n" + "="*80)
print("TEST 3: torch.compile(mode='max-autotune')")
print("="*80)
model = Qwen3TTSModel.from_pretrained(
model_name,
device_map="cuda:0",
dtype=torch.bfloat16,
attn_implementation="flash_attention_2",
)
print("Compiling model with max-autotune mode...")
model.model = torch.compile(
model.model,
mode="max-autotune",
fullgraph=False,
)
results, avg_lat, avg_rtf = benchmark_model(model, TEST_CASES, "Compile-MaxAutotune", warmup=2)
del model
torch.cuda.empty_cache()
return results, avg_lat, avg_rtf
def test_cudnn_benchmark(model_name):
"""Test with cuDNN benchmark enabled."""
print("\n" + "="*80)
print("TEST 4: cuDNN Benchmark + TF32")
print("="*80)
# Enable cuDNN benchmark and TF32
torch.backends.cudnn.benchmark = True
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = True
model = Qwen3TTSModel.from_pretrained(
model_name,
device_map="cuda:0",
dtype=torch.bfloat16,
attn_implementation="flash_attention_2",
)
results, avg_lat, avg_rtf = benchmark_model(model, TEST_CASES, "cuDNN+TF32")
del model
torch.cuda.empty_cache()
return results, avg_lat, avg_rtf
def test_all_optimizations(model_name):
"""Test with all optimizations combined."""
print("\n" + "="*80)
print("TEST 5: ALL OPTIMIZATIONS (Compile + cuDNN + TF32)")
print("="*80)
# Enable all optimizations
torch.backends.cudnn.benchmark = True
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = True
model = Qwen3TTSModel.from_pretrained(
model_name,
device_map="cuda:0",
dtype=torch.bfloat16,
attn_implementation="flash_attention_2",
)
print("Compiling model with reduce-overhead mode...")
model.model = torch.compile(
model.model,
mode="reduce-overhead",
fullgraph=False,
)
results, avg_lat, avg_rtf = benchmark_model(model, TEST_CASES, "ALL-OPTIMIZATIONS", warmup=2)
del model
torch.cuda.empty_cache()
return results, avg_lat, avg_rtf
def main():
model_name = "Qwen/Qwen3-TTS-12Hz-1.7B-CustomVoice"
print("="*80)
print("QWEN3-TTS OPTIMIZATION BENCHMARK")
print("="*80)
print(f"Model: {model_name}")
print(f"Device: CUDA (GPU)")
print(f"Precision: BFloat16")
print(f"Attention: Flash Attention 2")
print(f"PyTorch: {torch.__version__}")
print(f"CUDA: {torch.version.cuda}")
# Check GPU info
if torch.cuda.is_available():
print(f"GPU: {torch.cuda.get_device_name(0)}")
print(f"VRAM: {torch.cuda.get_device_properties(0).total_memory / 1024**3:.2f} GB")
all_results = {}
try:
# Test 1: Baseline (Flash Attention 2)
results, avg_lat, avg_rtf = test_baseline(model_name)
all_results['Baseline (Flash Attn 2)'] = {'results': results, 'avg_lat': avg_lat, 'avg_rtf': avg_rtf}
# Test 2: torch.compile with reduce-overhead
results, avg_lat, avg_rtf = test_torch_compile_reduce_overhead(model_name)
all_results['torch.compile (reduce-overhead)'] = {'results': results, 'avg_lat': avg_lat, 'avg_rtf': avg_rtf}
# Test 3: torch.compile with max-autotune
results, avg_lat, avg_rtf = test_torch_compile_max_autotune(model_name)
all_results['torch.compile (max-autotune)'] = {'results': results, 'avg_lat': avg_lat, 'avg_rtf': avg_rtf}
# Test 4: cuDNN benchmark + TF32
results, avg_lat, avg_rtf = test_cudnn_benchmark(model_name)
all_results['cuDNN Benchmark + TF32'] = {'results': results, 'avg_lat': avg_lat, 'avg_rtf': avg_rtf}
# Test 5: All optimizations combined
results, avg_lat, avg_rtf = test_all_optimizations(model_name)
all_results['ALL Optimizations'] = {'results': results, 'avg_lat': avg_lat, 'avg_rtf': avg_rtf}
except Exception as e:
print(f"\n❌ Error during benchmarking: {e}")
import traceback
traceback.print_exc()
return 1
# Print summary
print("\n" + "="*80)
print("OPTIMIZATION BENCHMARK SUMMARY")
print("="*80)
baseline_rtf = all_results['Baseline (Flash Attn 2)']['avg_rtf']
baseline_lat = all_results['Baseline (Flash Attn 2)']['avg_lat']
print(f"\n{'Configuration':<35s} │ Avg RTF │ Avg Latency │ vs Baseline")
print("─" * 80)
for config_name, data in all_results.items():
avg_rtf = data['avg_rtf']
avg_lat = data['avg_lat']
speedup_pct = ((baseline_rtf - avg_rtf) / baseline_rtf) * 100
marker = "🏆" if avg_rtf == min(d['avg_rtf'] for d in all_results.values()) else " "
print(f"{marker} {config_name:<33s} │ {avg_rtf:5.2f} │ {avg_lat:6.2f}s │ {speedup_pct:+6.1f}%")
# Find best configuration
best_config = min(all_results.items(), key=lambda x: x[1]['avg_rtf'])
print("\n" + "="*80)
print(f"🏆 BEST CONFIGURATION: {best_config[0]}")
print(f" Average RTF: {best_config[1]['avg_rtf']:.2f}")
print(f" Average Latency: {best_config[1]['avg_lat']:.2f}s")
improvement = ((baseline_rtf - best_config[1]['avg_rtf']) / baseline_rtf) * 100
print(f" Improvement over baseline: {improvement:.1f}%")
print("="*80)
return 0
if __name__ == "__main__":
exit(main())