forked from SesameAILabs/csm
-
-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathgenerator.py
More file actions
executable file
·1102 lines (892 loc) · 43 KB
/
generator.py
File metadata and controls
executable file
·1102 lines (892 loc) · 43 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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from dataclasses import dataclass
import math
import os
from typing import List, Tuple, Generator as PyGenerator, Optional, Callable
import time
import queue
import threading
import platform
from typing_extensions import OrderedDict
import wave
import numpy as np
import torch
import torchaudio
from huggingface_hub import hf_hub_download
from models import Model, ModelArgs
from moshi.models import loaders
from tokenizers.processors import TemplateProcessing
from transformers import AutoTokenizer
import logging
logger = logging.getLogger(__name__)
@dataclass
class Segment:
speaker: int
text: str
sample_rate = 24_000
audio: torch.Tensor
def load_llama3_tokenizer():
"""
https://github.com/huggingface/transformers/issues/22794#issuecomment-2092623992
"""
tokenizer_name = "unsloth/Llama-3.2-1B"
tokenizer = AutoTokenizer.from_pretrained(tokenizer_name)
bos = tokenizer.bos_token
eos = tokenizer.eos_token
tokenizer._tokenizer.post_processor = TemplateProcessing(
single=f"{bos}:0 $A:0 {eos}:0",
pair=f"{bos}:0 $A:0 {eos}:0 {bos}:1 $B:1 {eos}:1",
special_tokens=[(f"{bos}", tokenizer.bos_token_id), (f"{eos}", tokenizer.eos_token_id)],
)
return tokenizer
class Generator:
def __init__(self, model: Model):
self._model = model
self._model.setup_caches(1)
self._text_tokenizer = load_llama3_tokenizer()
device = next(model.parameters()).device
mimi_weight = hf_hub_download(loaders.DEFAULT_REPO, loaders.MIMI_NAME)
mimi = loaders.get_mimi(mimi_weight, device=device)
num_codebooks = model.config.audio_num_codebooks
mimi.set_num_codebooks(num_codebooks)
self._num_codebooks = num_codebooks
self._audio_tokenizer = mimi
self.sample_rate = mimi.sample_rate
self.device = device
self._stream_buffer_size = 20
self.max_seq_len = 2048
self._cache = OrderedDict()
self._text_token_cache = {}
torch.set_num_threads(16)
torch.cuda.set_per_process_memory_fraction(0.95)
def _tokenize_text_segment(self, text: str, speaker: int) -> Tuple[torch.Tensor, torch.Tensor]:
"""
Tokenize text segment with caching optimization for reduced latency.
"""
# Check cache first
cache_key = f"{speaker}:{text}"
if not hasattr(self, '_text_token_cache'):
self._text_token_cache = {}
if cache_key in self._text_token_cache:
return self._text_token_cache[cache_key]
text_tokens = self._text_tokenizer.encode(f"[{speaker}]{text}")
text_frame = torch.zeros(len(text_tokens), self._num_codebooks+1, dtype=torch.long, device=self.device)
text_frame_mask = torch.zeros(len(text_tokens), self._num_codebooks+1, dtype=torch.bool, device=self.device)
text_frame[:, -1] = torch.tensor(text_tokens, device=self.device)
text_frame_mask[:, -1] = True
frame_tokens = [text_frame]
frame_masks = [text_frame_mask]
result = (torch.cat(frame_tokens, dim=0), torch.cat(frame_masks, dim=0))
self._text_token_cache[cache_key] = result
return result
def _tokenize_audio(self, audio: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
frame_tokens = []
frame_masks = []
# (K, T)
audio = audio.to(self.device)
audio_tokens = self._audio_tokenizer.encode(audio.unsqueeze(0).unsqueeze(0))[0]
# Limit to the number of codebooks set in MIMI
audio_tokens = audio_tokens[:self._num_codebooks, :]
# add EOS frame
eos_frame = torch.zeros(audio_tokens.size(0), 1).to(self.device)
audio_tokens = torch.cat([audio_tokens, eos_frame], dim=1)
audio_frame = torch.zeros(audio_tokens.size(1), self._num_codebooks+1).long().to(self.device)
audio_frame_mask = torch.zeros(audio_tokens.size(1), self._num_codebooks+1).bool().to(self.device)
audio_frame[:, :self._num_codebooks] = audio_tokens.transpose(0, 1)
audio_frame_mask[:, :self._num_codebooks] = True
frame_tokens.append(audio_frame)
frame_masks.append(audio_frame_mask)
return torch.cat(frame_tokens, dim=0), torch.cat(frame_masks, dim=0)
def _tokenize_segment(self, segment: Segment) -> Tuple[torch.Tensor, torch.Tensor]:
text_tokens, text_masks = self._tokenize_text_segment(segment.text, segment.speaker)
audio_tokens, audio_masks = self._tokenize_audio(segment.audio)
total_len = text_tokens.size(0) + audio_tokens.size(0)
if total_len > self.max_seq_len:
overflow = total_len - self.max_seq_len
if text_tokens.size(0) > overflow:
text_tokens = text_tokens[overflow:]
text_masks = text_masks[overflow:]
else:
audio_overflow = overflow - text_tokens.size(0)
text_tokens = text_tokens[0:0]
text_masks = text_masks[0:0]
audio_tokens = audio_tokens[audio_overflow:]
audio_masks = audio_masks[audio_overflow:]
return torch.cat([text_tokens, audio_tokens], dim=0), torch.cat([text_masks, audio_masks], dim=0)
@torch.inference_mode()
def _decode_frames(self, frames):
if not frames:
return torch.tensor([])
# Only use first N codebooks for faster decoding
frames_reduced = [frame[:, :self._num_codebooks//2] for frame in frames]
audio = self._audio_tokenizer.decode(torch.stack(frames_reduced).permute(1, 2, 0)).squeeze(0).squeeze(0)
return audio
@torch.inference_mode()
def generate_stream(
self,
text: str,
speaker: int,
context: List[Segment],
max_audio_length_ms: float = 90_000,
temperature: float = 0.7,
topk: int = 30,
on_chunk_generated: Optional[Callable[[torch.Tensor], None]] = None,
):
"""
Generate audio in a streaming fashion, optimized for lower latency to first chunk.
"""
if torch.cuda.is_available():
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.benchmark = True
torch.cuda.empty_cache()
torch.cuda.synchronize()
self._model.reset_caches()
max_generation_len = int(max_audio_length_ms / 80)
tokens, tokens_mask = [], []
initial_batch_size = 20
normal_batch_size = 20
initial_buffer_size = 20
normal_buffer_size = 20
batch_size = initial_batch_size
buffer_size = initial_buffer_size
first_chunk_delivered = False
context_start = time.time()
if context:
for segment in context:
segment_tokens, segment_tokens_mask = self._tokenize_segment(segment)
tokens.append(segment_tokens)
tokens_mask.append(segment_tokens_mask)
gen_segment_tokens, gen_segment_tokens_mask = self._tokenize_text_segment(text, speaker)
tokens.append(gen_segment_tokens)
tokens_mask.append(gen_segment_tokens_mask)
prompt_tokens = torch.cat(tokens, dim=0).long().to(self.device)
prompt_tokens_mask = torch.cat(tokens_mask, dim=0).bool().to(self.device)
max_seq_len = 2048
if prompt_tokens.size(0) > max_seq_len:
prompt_tokens = prompt_tokens[-max_seq_len:]
prompt_tokens_mask = prompt_tokens_mask[-max_seq_len:]
curr_tokens = prompt_tokens.unsqueeze(0)
curr_tokens_mask = prompt_tokens_mask.unsqueeze(0)
curr_pos = torch.arange(0, prompt_tokens.size(0)).unsqueeze(0).long().to(self.device)
expected_frame_count = buffer_size
frame_buffer = []
zeros_1_1 = torch.zeros(1, 1).long().to(self.device)
zeros_mask_1_1 = torch.zeros(1, 1).bool().to(self.device)
def update_tokens(sample):
nonlocal curr_tokens, curr_tokens_mask, curr_pos
ones = torch.ones_like(sample).bool()
curr_tokens = torch.cat([sample, zeros_1_1], dim=1).unsqueeze(1)
curr_tokens_mask = torch.cat([ones, zeros_mask_1_1], dim=1).unsqueeze(1)
curr_pos = curr_pos[:, -1:] + 1
with self._audio_tokenizer.streaming(1):
i = 0
generation_start = time.time()
while i < max_generation_len:
batch_end = min(i + batch_size, max_generation_len)
batch_size_actual = batch_end - i
batch_samples = []
for _ in range(batch_size_actual):
with torch.autocast(device_type=self.device.type, dtype=torch.bfloat16):
sample = self._model.generate_frame(curr_tokens, curr_tokens_mask, curr_pos, temperature, topk)
if torch.cuda.is_available() and hasattr(torch, "cuda") and hasattr(torch.cuda, "is_available"):
try:
torch.cuda.synchronize() # Force sync before checking
if sample.numel() == 0 or torch.isnan(sample).any():
print("Warning: Generated empty or NaN sample, stopping generation")
break
except:
print("Error checking tensor, stopping generation")
break
if torch.all(sample == 0):
break
batch_samples.append(sample)
update_tokens(sample)
if not batch_samples:
break
frame_buffer.extend(batch_samples)
i += len(batch_samples)
if len(frame_buffer) >= buffer_size:
frames_to_process = frame_buffer[:expected_frame_count]
# If we don't have enough frames, pad with zeros to match expected shape
if len(frames_to_process) < expected_frame_count:
# Create padding frames (zeros)
padding_frames = [
torch.zeros_like(frames_to_process[0])
for _ in range(expected_frame_count - len(frames_to_process))
]
# Combine actual frames with padding
frames_to_process = frames_to_process + padding_frames
frames_stacked = torch.stack(frames_to_process).permute(1, 2, 0)
audio_chunk = self._audio_tokenizer.decode(frames_stacked).squeeze(0).squeeze(0)
# Keep remaining frames for next iteration
frame_buffer = frame_buffer[expected_frame_count:]
# Process and yield the chunk
cpu_chunk = audio_chunk.cpu()
if on_chunk_generated:
on_chunk_generated(cpu_chunk)
# After first chunk is delivered, switch to normal batch and buffer sizes
if not first_chunk_delivered:
batch_size = normal_batch_size
buffer_size = normal_buffer_size
expected_frame_count = buffer_size
first_chunk_delivered = True
yield cpu_chunk
# Occasionally print progress and sync GPU
if i >= 100 and (i % 100 == 0):
if torch.cuda.is_available():
torch.cuda.synchronize()
print(f"Generated {i} frames ({i * 0.08:.2f}s of audio)")
# Process any remaining frames
if frame_buffer:
# Pad frame buffer if necessary
if len(frame_buffer) < expected_frame_count:
padding_frames = [
torch.zeros_like(frame_buffer[0])
for _ in range(expected_frame_count - len(frame_buffer))
]
frames_to_process = frame_buffer + padding_frames
else:
# Otherwise take as many frames as possible that are a multiple of expected_frame_count
frames_multiple = (len(frame_buffer) // expected_frame_count) * expected_frame_count
frames_to_process = frame_buffer[:frames_multiple]
frames_stacked = torch.stack(frames_to_process).permute(1, 2, 0)
audio_chunk = self._audio_tokenizer.decode(frames_stacked).squeeze(0).squeeze(0)
# Determine actual audio length (before padding)
actual_frames_percentage = min(len(frame_buffer), expected_frame_count) / expected_frame_count
actual_samples = int(audio_chunk.shape[0] * actual_frames_percentage)
# Return only the non-padded portion of audio if we added padding
if len(frame_buffer) < expected_frame_count:
audio_chunk = audio_chunk[:actual_samples]
cpu_chunk = audio_chunk.cpu()
if on_chunk_generated:
on_chunk_generated(cpu_chunk)
yield cpu_chunk
# Print final performance metrics
if torch.cuda.is_available():
torch.cuda.synchronize()
total_time = time.time() - generation_start
frames_generated = i
audio_seconds = frames_generated * 0.08
rtf = total_time / audio_seconds if audio_seconds > 0 else float('inf')
print(f"Total time: {total_time:.2f}s")
print(f"Generated {frames_generated} frames ({audio_seconds:.2f}s of audio)")
print(f"Real-time factor: {rtf:.3f}x (target: <1.0)")
@torch.inference_mode()
def generate(
self,
text: str,
speaker: int,
context: List[Segment],
max_audio_length_ms: float = 90_000,
temperature: float = 0.8,
topk: int = 40,
stream: bool = False,
output_file: Optional[str] = None,
):
"""
Generate audio with optional streaming and file output.
Args:
text: Text to generate audio for
speaker: Speaker ID
context: List of context segments
max_audio_length_ms: Maximum audio length in milliseconds
temperature: Sampling temperature
topk: Top-k sampling parameter
stream: Whether to use streaming generation
output_file: If provided and stream=True, output will be saved to this file
Returns:
torch.Tensor: Generated audio tensor
"""
if stream:
if output_file:
# Setup streaming to file
write_chunk, close_wav = stream_audio_to_wav(output_file, self.sample_rate)
# Collect chunks while streaming to file
audio_chunks = []
t1 = time.time()
for i, chunk in enumerate(self.generate_stream(
text, speaker, context, max_audio_length_ms, temperature, topk
)):
# Write to file
write_chunk(chunk)
# Store for return value
audio_chunks.append(chunk)
# Occasionally print progress
if i % 5 == 0:
print(f"Part {i+1} available after {time.time() - t1:.4f}s")
t1 = time.time()
# Close file
close_wav()
print(f"Streaming complete, WAV file saved to {output_file}")
else:
# Just collect chunks without file output
audio_chunks = []
for chunk in self.generate_stream(text, speaker, context, max_audio_length_ms, temperature, topk):
audio_chunks.append(chunk)
if not audio_chunks:
return torch.tensor([])
return torch.cat(audio_chunks)
# Non-streaming generation remains unchanged
if torch.cuda.is_available():
torch.cuda.empty_cache()
self._model.reset_caches()
max_generation_len = int(max_audio_length_ms / 80)
tokens, tokens_mask = [], []
for segment in context:
segment_tokens, segment_tokens_mask = self._tokenize_segment(segment)
tokens.append(segment_tokens)
tokens_mask.append(segment_tokens_mask)
gen_segment_tokens, gen_segment_tokens_mask = self._tokenize_text_segment(text, speaker)
tokens.append(gen_segment_tokens)
tokens_mask.append(gen_segment_tokens_mask)
prompt_tokens = torch.cat(tokens, dim=0).long().to(self.device)
prompt_tokens_mask = torch.cat(tokens_mask, dim=0).bool().to(self.device)
max_seq_len = 2048
if prompt_tokens.size(0) > max_seq_len:
prompt_tokens = prompt_tokens[-max_seq_len:]
prompt_tokens_mask = prompt_tokens_mask[-max_seq_len:]
curr_tokens = prompt_tokens.unsqueeze(0)
curr_tokens_mask = prompt_tokens_mask.unsqueeze(0)
curr_pos = torch.arange(0, prompt_tokens.size(0)).unsqueeze(0).long().to(self.device)
samples = []
with self._audio_tokenizer.streaming(1):
for _ in range(max_generation_len):
sample = self._model.generate_frame(curr_tokens, curr_tokens_mask, curr_pos, temperature, topk)
if torch.all(sample == 0):
break
samples.append(sample)
curr_tokens = torch.cat([sample, torch.zeros(1, 1).long().to(self.device)], dim=1).unsqueeze(1)
curr_tokens_mask = torch.cat(
[torch.ones_like(sample).bool(), torch.zeros(1, 1).bool().to(self.device)], dim=1
).unsqueeze(1)
curr_pos = curr_pos[:, -1:] + 1
if not samples:
return torch.tensor([])
return self._audio_tokenizer.decode(torch.stack(samples).permute(1, 2, 0)).squeeze(0).squeeze(0)
class AudioStreamWriter:
"""
Helper class for writing streaming audio to a file.
"""
def __init__(self, filename, sample_rate):
self.filename = filename
self.sample_rate = sample_rate
self.audio_chunks = []
self.lock = threading.Lock()
self.queue = queue.Queue()
self.running = True
# Start background writer thread
self.writer_thread = threading.Thread(target=self._writer_worker, daemon=True)
self.writer_thread.start()
def _writer_worker(self):
"""Background thread that handles audio chunk processing"""
buffer_chunks = []
last_flush_time = time.time()
while self.running or not self.queue.empty():
try:
# Get chunk with timeout to allow for regular checks
chunk = self.queue.get(timeout=0.2)
buffer_chunks.append(chunk)
# Periodically flush the buffer to the main list
current_time = time.time()
if len(buffer_chunks) >= 10 or (current_time - last_flush_time > 2.0 and buffer_chunks):
with self.lock:
self.audio_chunks.extend(buffer_chunks)
buffer_chunks = []
last_flush_time = current_time
except queue.Empty:
# If queue is empty but we have pending chunks, add them
if buffer_chunks:
with self.lock:
self.audio_chunks.extend(buffer_chunks)
buffer_chunks = []
last_flush_time = time.time()
# Final flush of any remaining chunks
if buffer_chunks:
with self.lock:
self.audio_chunks.extend(buffer_chunks)
def add_chunk(self, chunk):
"""Add an audio chunk to the buffer queue without blocking"""
try:
self.queue.put(chunk, timeout=0.1)
except queue.Full:
# If queue is full, add directly to avoid losing data
with self.lock:
self.audio_chunks.append(chunk)
def write_file(self):
"""Write all collected audio chunks to file and clean up"""
# Signal the background thread to stop
self.running = False
# Wait for the thread to finish with a timeout
self.writer_thread.join(timeout=3.0)
with self.lock:
if not self.audio_chunks:
return
# Concatenate all chunks
audio = torch.cat(self.audio_chunks)
# Save to file
torchaudio.save(self.filename, audio.unsqueeze(0).cpu(), self.sample_rate)
from safetensors.torch import load_file
import os
import torch
from models import Model, ModelArgs
from generator import Generator
def load_csm_1b_local(model_path: str, device: str = "cuda", audio_num_codebooks: int = 32):
"""
Load the CSM-1B model from a local checkpoint with extreme optimizations and warmup.
"""
import torch
import platform
from functools import lru_cache
from generator import Generator, Model, ModelArgs
# Enable all CUDA optimizations
torch.backends.cuda.matmul.allow_tf32 = True
if hasattr(torch.backends.cuda, 'enable_flash_sdp'):
torch.backends.cuda.enable_flash_sdp(True)
torch.backends.cudnn.benchmark = True
torch.backends.cudnn.enabled = True
print(f"Loading CSM-1B model from local checkpoint '{model_path}' with extreme optimizations...")
if torch.cuda.is_available():
torch.cuda.empty_cache()
torch.cuda.synchronize()
config = ModelArgs(
backbone_flavor="llama-1B",
decoder_flavor="llama-100M",
text_vocab_size=128256,
audio_vocab_size=2051,
audio_num_codebooks=audio_num_codebooks,
)
model = Model.from_pretrained(model_path)
model.eval()
dtype = torch.bfloat16 if torch.cuda.is_available() and torch.cuda.is_bf16_supported() else torch.float16
model.backbone = torch.compile(model.backbone,mode='reduce-overhead', fullgraph=True, backend='inductor')
model.decoder = torch.compile(model.decoder,mode='reduce-overhead', fullgraph=True, backend='inductor')
model.to(device=device, dtype=dtype)
print("Model compilation complete. Creating generator...")
generator = Generator(model)
generator._stream_buffer_size = 20
# Setup tokenization caching
generator._tokenization_cache = {}
original_tokenize_text = generator._tokenize_text_segment
@lru_cache(maxsize=2048)
def cached_tokenize_text_segment(text_str, speaker_int):
return original_tokenize_text(text_str, speaker_int)
generator._tokenize_text_segment = lambda text, speaker: cached_tokenize_text_segment(text, speaker)
# Perform warmup
warmup_generator(generator)
return generator
def warmup_generator(gen: Generator, warmup_text: str = "Hello, this is a comprehensive warmup text that will exercise the model's generation capabilities.", speaker_id: int = 0):
"""
Perform an extremely aggressive warmup to drastically reduce first-generation latency.
"""
print("Starting maximum-intensity warmup sequence...")
# Directly access and optimize the model's internal state
if hasattr(gen._model, 'backbone') and hasattr(gen._model.backbone, 'positional_embedding'):
# Force calculation of position embeddings to ensure they're cached
with torch.inference_mode():
positions = torch.arange(0, 2048).to(gen.device)
_ = gen._model.backbone.positional_embedding(positions)
# Pre-allocate CUDA memory to prevent fragmentation during generation
if torch.cuda.is_available():
print("Optimizing GPU memory allocation...")
# Try to reserve a large chunk of memory
try:
import math
reserved_memory = []
# Reserve multiple blocks of different sizes
for size_mb in [128, 256, 512, 256, 128, 64]:
size = int(size_mb * 1024 * 1024 / 4) # Convert MB to float32 elements
tensor_size = int(math.sqrt(size))
tensor = torch.ones((tensor_size, tensor_size), device=gen.device, dtype=torch.float32)
tensor = tensor * 1.0 # Force allocation
reserved_memory.append(tensor)
torch.cuda.synchronize()
# Now free the memory
for tensor in reserved_memory:
del tensor
reserved_memory = []
torch.cuda.empty_cache()
torch.cuda.synchronize()
except Exception as e:
print(f"Memory pre-allocation: {e}")
# Create multiple dummy audio segments with varying characteristics
print("Creating diverse audio contexts...")
audio_segments = []
# Create 3 different audio patterns
for i in range(3):
length = 24000 * (i + 1) # 1s, 2s, 3s
audio = torch.zeros(length).to(gen.device)
# Add different patterns to each segment
if i == 0:
# Sine wave pattern
import math
t = torch.linspace(0, 8 * math.pi, length).to(gen.device)
audio = torch.sin(t) * 0.1
elif i == 1:
# Random noise pattern
audio = torch.randn(length).to(gen.device) * 0.05
else:
# Pulse pattern
audio[::800] = 0.2
audio[::801] = -0.2
segment = Segment(
speaker=speaker_id,
text=f"Warmup segment {i+1} with {length/24000:.1f}s of audio.",
audio=audio
)
audio_segments.append(segment)
# Force compilation of critical model components
print("Forcing compilation of critical components...")
# Directly exercise the audio tokenizer with real data
with torch.inference_mode():
for segment in audio_segments:
# Force tokenization of both text and audio
gen._tokenize_segment(segment)
# Exercise the model's generation capabilities directly
with torch.inference_mode():
# Generate some sample frames to ensure model is compiled
dummy_tokens = torch.ones(1, 10, gen._num_codebooks+1).long().to(gen.device)
dummy_mask = torch.ones(1, 10, gen._num_codebooks+1).bool().to(gen.device)
dummy_pos = torch.arange(0, 10).unsqueeze(0).to(gen.device)
# Generate multiple frames with different parameters
for temp in [0.6, 0.7, 0.8]:
for topk in [20, 30, 40]:
_ = gen._model.generate_frame(dummy_tokens, dummy_mask, dummy_pos, temp, topk)
gen._text_token_cache.clear()
print("Running final generation with exact same setup as a real request...")
final_text = "This is the final warmup that exactly matches a real generation request."
# First tokenize the text - to fill the cache
gen._tokenize_text_segment(final_text, speaker_id)
try:
# Now run a complete generation with a single context segment
generate_streaming_audio(
generator=gen,
text=final_text,
speaker=speaker_id,
context=[audio_segments[0]], # Just one context segment
output_file="warmup_final.wav",
max_audio_length_ms=6000,
temperature=0.7,
topk=30,
play_audio=False
)
except Exception as e:
print(f"Final warmup run exception (ignorable): {e}")
# Force final synchronization and memory optimization
if torch.cuda.is_available():
print("Final GPU optimization...")
torch.cuda.synchronize()
torch.cuda.empty_cache()
try:
# Allocate a large tensor to force compaction
large_tensor = torch.empty(int(1e9//4), dtype=torch.float, device=gen.device)
# Immediately delete it
del large_tensor
except RuntimeError:
# Expected if there's not enough memory
pass
# Final cleanup
torch.cuda.empty_cache()
torch.cuda.synchronize()
print("Maximum-intensity warmup complete. First generation should now be MUCH faster.")
def load_csm_1b(device: str = "cuda") -> Generator:
"""
Load the CSM-1B model with extreme optimizations for real-time performance.
"""
# Enable all CUDA optimizations
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cuda.enable_flash_sdp(True)
torch.backends.cudnn.benchmark = True
torch.backends.cudnn.enabled = True
print("Loading CSM-1B model with extreme optimizations for real-time performance...")
if torch.cuda.is_available():
torch.cuda.empty_cache()
torch.cuda.synchronize()
model = Model.from_pretrained("sesame/csm-1b")
dtype = torch.bfloat16 if torch.cuda.is_available() and torch.cuda.is_bf16_supported() else torch.float16
model.backbone = torch.compile(model.backbone,mode='reduce-overhead', fullgraph=True, backend='inductor')
model.decoder = torch.compile(model.decoder,mode='reduce-overhead', fullgraph=True, backend='inductor')
model.to(device=device, dtype=dtype)
print("Model compilation complete. Creating generator...")
generator = Generator(model)
generator._stream_buffer_size = 20
generator._tokenization_cache = {}
from functools import lru_cache
# Patch the tokenize method with caching
original_tokenize_text = generator._tokenize_text_segment
@lru_cache(maxsize=2048)
def cached_tokenize_text_segment(text_str, speaker_int):
return original_tokenize_text(text_str, speaker_int)
generator._tokenize_text_segment = lambda text, speaker: cached_tokenize_text_segment(text, speaker)
warmup_generator(generator)
return generator
def stream_audio_to_wav(filename, sample_rate):
"""
Initialize a WAV writer for streaming audio chunks.
Args:
filename: Output WAV file path
sample_rate: Audio sample rate in Hz
Returns:
tuple: (write_chunk, close) functions for writing audio data and closing the file
"""
# Create a WAV file with the proper header
wav_file = wave.open(filename, 'wb')
wav_file.setnchannels(1) # Mono
wav_file.setsampwidth(2) # 16-bit
wav_file.setframerate(sample_rate)
def write_chunk(audio_chunk):
# Convert tensor to numpy and then to int16 PCM format
if isinstance(audio_chunk, torch.Tensor):
# Ensure it's on CPU and detached before converting to numpy
audio_np = audio_chunk.detach().cpu().numpy()
else:
audio_np = audio_chunk
# Normalize if needed (assuming audio is in [-1, 1] range)
if audio_np.max() <= 1.0 and audio_np.min() >= -1.0:
audio_int = (audio_np * 32767).astype(np.int16)
else:
audio_int = audio_np.astype(np.int16)
# Write to WAV file
wav_file.writeframes(audio_int.tobytes())
def close():
wav_file.close()
return write_chunk, close
def generate_streaming_audio(
generator: Generator,
text: str,
speaker: int,
context: List[Segment],
output_file: str,
max_audio_length_ms: float = 90_000,
temperature: float = 1.0,
topk: int = 50,
play_audio: bool = False,
):
"""
Generate audio with streaming output and comprehensive timing metrics.
Optimized for reduced first-chunk latency.
"""
# Initialize the streaming WAV writer
write_chunk, close_wav = stream_audio_to_wav(output_file, generator.sample_rate)
# Set up audio playback if requested
audio_queue = queue.Queue(maxsize=100) if play_audio else None
stop_event = threading.Event()
if play_audio:
try:
import sounddevice as sd
# Get available sample rates for default output device to check compatibility
device_info = sd.query_devices(kind='output')
supported_rate = device_info.get('default_samplerate', 44100)
need_resampling = abs(supported_rate - generator.sample_rate) > 100
if need_resampling:
try:
# Use resampling if sample rate doesn't match
import librosa
print(f"Resampling from {generator.sample_rate}Hz to {int(supported_rate)}Hz for playback")
def audio_playback_worker():
while not stop_event.is_set() or not audio_queue.empty():
try:
chunk = audio_queue.get(timeout=0.5)
if isinstance(chunk, torch.Tensor) and chunk.numel() == 0:
audio_queue.task_done()
continue
audio_np = chunk.numpy() if isinstance(chunk, torch.Tensor) else chunk
# Skip very short chunks (likely noise)
if len(audio_np) < 100:
audio_queue.task_done()
continue
# Resample to device's supported rate
resampled = librosa.resample(
audio_np,
orig_sr=generator.sample_rate,
target_sr=int(supported_rate)
)
sd.play(resampled, supported_rate, blocking=True)
# Add a small delay to ensure audio finishes playing
time.sleep(0.05)
audio_queue.task_done()
except queue.Empty:
# If queue empty but not stopping, keep trying
if not stop_event.is_set():
continue
else:
break
except Exception as e:
print(f"Playback error: {e}")
audio_queue.task_done()
except ImportError:
print("Librosa not found. Using direct playback which may cause sample rate warnings.")
need_resampling = False
if not need_resampling:
def audio_playback_worker():
while not stop_event.is_set() or not audio_queue.empty():
try:
chunk = audio_queue.get(timeout=0.5)
if isinstance(chunk, torch.Tensor) and chunk.numel() == 0:
audio_queue.task_done()
continue
audio_np = chunk.numpy() if isinstance(chunk, torch.Tensor) else chunk
# Skip very short chunks (likely noise)
if len(audio_np) < 100:
audio_queue.task_done()
continue
sd.play(audio_np, generator.sample_rate, blocking=True)
# Add a small delay to ensure audio finishes playing
time.sleep(0.05)
audio_queue.task_done()
except queue.Empty:
# If queue empty but not stopping, keep trying
if not stop_event.is_set():
continue
else:
break
except Exception as e:
print(f"Playback error: {e}")
audio_queue.task_done()
# Start playback thread
playback_thread = threading.Thread(target=audio_playback_worker, daemon=False)
playback_thread.start()
except ImportError:
print("sounddevice library not found. Install with 'pip install sounddevice' for real-time playback.")
play_audio = False
# Timing metrics
chunk_times = []
latency_to_first_chunk = None
total_audio_duration = 0
chunk_count = 0
# Function to handle each generated chunk
def on_chunk_generated(chunk):
nonlocal chunk_count, latency_to_first_chunk, total_audio_duration
current_time = time.time()
if chunk_count == 0:
latency_to_first_chunk = current_time - start_time
print(f"First chunk latency: {latency_to_first_chunk*1000:.1f}ms")
# Save chunk to WAV file
write_chunk(chunk)
# Update metrics
chunk_count += 1
chunk_duration = len(chunk) / generator.sample_rate
total_audio_duration += chunk_duration
chunk_times.append(current_time)
# Send to audio player if enabled
if play_audio and audio_queue is not None:
try:
audio_queue.put(chunk, timeout=1.0)
except queue.Full:
pass # Skip if queue is full to avoid blocking
if torch.cuda.is_available():
print("Preparing GPU for low-latency generation...")
torch.cuda.empty_cache()
torch.cuda.synchronize()
# Pre-allocate some GPU memory to avoid allocation during generation
dummy_tensors = []
for i in range(5):
dummy = torch.ones((100, 100), device=generator.device)
dummy = dummy + 1.0 # Force computation
dummy_tensors.append(dummy) # Keep reference to prevent deallocation
torch.cuda.synchronize()
# Set process priority to improve performance - use higher priority
try:
import psutil
process = psutil.Process()
if platform.system() == 'Windows':
process.nice(psutil.HIGH_PRIORITY_CLASS)
else:
process.nice(-1)
except (ImportError, PermissionError, psutil.AccessDenied):
pass
print(f"Starting audio generation for: '{text[:50]}{'...' if len(text) > 50 else ''}'")
start_time = time.time()
# Generate audio in chunks, catching possible errors
frame_count = 0
audio_chunks = [] # Store all chunks for possible use at the end
try:
for audio_chunk in generator.generate_stream(
text=text,
speaker=speaker,
context=context,