-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathgeneration_utils.py
More file actions
557 lines (466 loc) · 17.8 KB
/
generation_utils.py
File metadata and controls
557 lines (466 loc) · 17.8 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
import argparse
import json
import re
from pathlib import Path
from typing import Any, Dict, Iterator, List, Optional, Tuple
import soundfile as sf
import torch
import torchaudio
def normalize_text(text: str) -> str:
text = re.sub(r"\[(\d+)\]", r"[S\1]", text)
remove_chars = "【】《》()『』「」" '"-_“”~~‘’'
segments = re.split(r"(?=\[S\d+\])", text.replace("\n", " "))
processed_parts = []
for seg in segments:
seg = seg.strip()
if not seg:
continue
m = re.match(r"^(\[S\d+\])\s*(.*)", seg)
tag, content = m.groups() if m else ("", seg)
content = re.sub(f"[{re.escape(remove_chars)}]", "", content)
content = re.sub(r"哈{2,}", "[笑]", content)
content = re.sub(r"\b(ha(\s*ha)+)\b", "[laugh]", content, flags=re.IGNORECASE)
content = content.replace("——", ",")
content = content.replace("……", ",")
content = content.replace("...", ",")
content = content.replace("⸺", ",")
content = content.replace("―", ",")
content = content.replace("—", ",")
content = content.replace("…", ",")
internal_punct_map = str.maketrans(
{";": ",", ";": ",", ":": ",", ":": ",", "、": ","}
)
content = content.translate(internal_punct_map)
content = content.strip()
content = re.sub(r"([,。?!,.?!])[,。?!,.?!]+", r"\1", content)
if len(content) > 1:
last_ch = (
"。"
if content[-1] == ","
else ("." if content[-1] == "," else content[-1])
)
body = content[:-1].replace("。", ",")
content = body + last_ch
processed_parts.append({"tag": tag, "content": content})
if not processed_parts:
return ""
merged_lines = []
current_tag = processed_parts[0]["tag"]
current_content = [processed_parts[0]["content"]]
for part in processed_parts[1:]:
if part["tag"] == current_tag and current_tag:
current_content.append(part["content"])
else:
merged_lines.append(f"{current_tag}{''.join(current_content)}".strip())
current_tag = part["tag"]
current_content = [part["content"]]
merged_lines.append(f"{current_tag}{''.join(current_content)}".strip())
return "".join(merged_lines).replace("‘", "'").replace("’", "'")
def streaming_jsonl_reader(
jsonl_path: str,
rank: int = 0,
world_size: int = 1,
skip_invalid_json: bool = False,
) -> Iterator[Tuple[int, Dict[str, Any]]]:
if world_size < 1:
raise ValueError("`world_size` must be >= 1.")
if rank < 0 or rank >= world_size:
raise ValueError("`rank` must satisfy 0 <= rank < world_size.")
with open(jsonl_path, "r", encoding="utf-8") as f:
for line_no, line in enumerate(f, start=1):
line = line.strip()
if not line:
continue
if (line_no - 1) % world_size != rank:
continue
try:
sample = json.loads(line)
except json.JSONDecodeError as exc:
if skip_invalid_json:
print(
f"[WARN] jsonl line {line_no} skipped: invalid json ({exc.msg})"
)
continue
raise ValueError(
f"jsonl line {line_no}: invalid json ({exc.msg})"
) from exc
yield line_no, sample
def _to_abs_path_str(path_str: str) -> str:
return str(Path(path_str).expanduser().resolve())
def _abspath_record_paths(record: Dict[str, Any]) -> Dict[str, Any]:
path_key_pattern = re.compile(
r"^(output_audio|prompt_audio(?:_speaker\d+)?|.*_path)$"
)
for key, value in list(record.items()):
if value is None:
continue
if not isinstance(value, str):
continue
if path_key_pattern.fullmatch(str(key)):
record[key] = _to_abs_path_str(value)
return record
def _resolve_path(maybe_path: str, base_path: Optional[str]) -> str:
if base_path is None:
return maybe_path
p = Path(maybe_path)
if p.is_absolute():
return str(p)
return str(Path(base_path) / p)
def _make_output_record(raw_sample: Dict[str, Any], sample_id: str) -> Dict[str, Any]:
record = dict(raw_sample)
base_path = record.pop("base_path", None)
record["id"] = sample_id
for k, v in list(record.items()):
if v is None:
continue
if re.fullmatch(r"prompt_audio(?:_speaker\d+)?", str(k)) and isinstance(v, str):
record[k] = _resolve_path(str(v), base_path)
return _abspath_record_paths(record)
def _write_jsonl_line(f, obj: Dict[str, Any]) -> None:
f.write(json.dumps(obj, ensure_ascii=False) + "\n")
def _merge_consecutive_speaker_tags(text: str) -> str:
segments = re.split(r"(?=\[S\d+\])", text)
if not segments:
return text
merged_parts: List[str] = []
current_tag = None
for seg in segments:
seg = seg.strip()
if not seg:
continue
m = re.match(r"^(\[S\d+\])\s*(.*)", seg, re.DOTALL)
if m:
tag, content = m.groups()
if tag == current_tag:
merged_parts.append(content)
else:
current_tag = tag
merged_parts.append(f"{tag}{content}")
else:
merged_parts.append(seg)
return "".join(merged_parts)
def _load_generation_config(model_path: str) -> Dict[str, Any]:
generation_config_path = Path(model_path) / "generation_config.json"
if not generation_config_path.is_file():
return {}
try:
with open(generation_config_path, "r", encoding="utf-8") as f:
cfg = json.load(f)
except Exception as exc:
print(f"Warning: failed to read {generation_config_path}: {exc}")
return {}
if not isinstance(cfg, dict):
return {}
return cfg
def resolve_sampling_args(args: argparse.Namespace) -> None:
fallback: Dict[str, Any] = {
"max_new_tokens": 8192,
"temperature": 1.1,
"top_p": 0.9,
"top_k": 50,
"repetition_penalty": 1.1,
}
generation_cfg = _load_generation_config(args.model_path)
for key, default_value in fallback.items():
cli_value = getattr(args, key)
if cli_value is not None:
continue
if key in generation_cfg:
try:
if key in {"top_k", "max_new_tokens"}:
setattr(args, key, int(generation_cfg[key]))
else:
setattr(args, key, float(generation_cfg[key]))
continue
except (TypeError, ValueError):
pass
setattr(args, key, default_value)
def _load_mono_wav(wav_path: str) -> Tuple[torch.Tensor, int]:
audio, sr = sf.read(wav_path, dtype="float32", always_2d=True)
wav = torch.from_numpy(audio).transpose(0, 1).contiguous()
if wav.ndim != 2:
raise ValueError(
f"Expect wav tensor rank=2, got shape={tuple(wav.shape)} from {wav_path}"
)
if wav.shape[0] > 1:
wav = wav.mean(dim=0, keepdim=True)
return wav.contiguous(), int(sr)
def _maybe_resample(wav: torch.Tensor, orig_sr: int, target_sr: int) -> torch.Tensor:
if orig_sr == target_sr:
return wav
return torchaudio.functional.resample(
waveform=wav,
orig_freq=orig_sr,
new_freq=target_sr,
)
def _preprocess_prompt_wavs(
loaded_wavs: List[Tuple[torch.Tensor, int]],
target_sr: int,
sample_rate_normalize_enabled: bool,
) -> List[torch.Tensor]:
if sample_rate_normalize_enabled:
min_sr = min(sr for _, sr in loaded_wavs)
else:
min_sr = None
wav_list: List[torch.Tensor] = []
for wav, sr in loaded_wavs:
if sample_rate_normalize_enabled:
wav = _maybe_resample(wav, sr, int(min_sr))
sr = int(min_sr)
wav = _maybe_resample(wav, sr, target_sr)
wav_list.append(wav)
return wav_list
def _collect_speaker_fields(
sample: Dict[str, Any],
base_path: Optional[str],
) -> Tuple[Dict[int, str], Dict[int, str], List[int]]:
audio_map: Dict[int, str] = {}
text_map: Dict[int, str] = {}
for k, v in sample.items():
if v is None:
continue
key = str(k)
value = str(v).strip()
if not value:
continue
m_audio = re.fullmatch(r"prompt_audio_speaker(\d+)", key)
if m_audio:
speaker_id = int(m_audio.group(1))
audio_map[speaker_id] = _resolve_path(value, base_path)
continue
m_text = re.fullmatch(r"prompt_text_speaker(\d+)", key)
if m_text:
speaker_id = int(m_text.group(1))
text_map[speaker_id] = value
speaker_ids = sorted(set(audio_map.keys()) & set(text_map.keys()))
return audio_map, text_map, speaker_ids
def _build_prefixed_text(
text: str, text_map: Dict[int, str], speaker_ids: List[int]
) -> str:
parts: List[str] = []
for speaker_id in speaker_ids:
cur_text = text_map[speaker_id]
tag = f"[S{speaker_id}]"
if not cur_text.lstrip().startswith(tag):
cur_text = f"{tag}{cur_text}"
parts.append(cur_text)
return _merge_consecutive_speaker_tags("".join(parts) + text)
def _encode_concat_prompt_audio(
processor: Any,
audio_map: Dict[int, str],
speaker_ids: List[int],
target_sr: int,
sample_rate_normalize_enabled: bool,
) -> torch.Tensor:
loaded_wavs: List[Tuple[torch.Tensor, int]] = []
for speaker_id in speaker_ids:
loaded_wavs.append(_load_mono_wav(audio_map[speaker_id]))
wav_list = _preprocess_prompt_wavs(
loaded_wavs=loaded_wavs,
target_sr=target_sr,
sample_rate_normalize_enabled=sample_rate_normalize_enabled,
)
wav = torch.cat(wav_list, dim=-1)
return processor.encode_audios_from_wav([wav], sampling_rate=target_sr)[0]
def _encode_references(
processor: Any,
audio_map: Dict[int, str],
speaker_ids: List[int],
target_sr: int,
sample_rate_normalize_enabled: bool,
) -> List[Optional[torch.Tensor]]:
loaded_wavs: List[Tuple[torch.Tensor, int]] = []
ordered_ids: List[int] = []
for speaker_id in speaker_ids:
loaded_wavs.append(_load_mono_wav(audio_map[speaker_id]))
ordered_ids.append(speaker_id)
wav_list = _preprocess_prompt_wavs(
loaded_wavs=loaded_wavs,
target_sr=target_sr,
sample_rate_normalize_enabled=sample_rate_normalize_enabled,
)
encoded_list = processor.encode_audios_from_wav(wav_list, sampling_rate=target_sr)
encoded_map = {
speaker_id: tokens for speaker_id, tokens in zip(ordered_ids, encoded_list)
}
max_speaker_id = max(speaker_ids)
return [encoded_map.get(speaker_id) for speaker_id in range(1, max_speaker_id + 1)]
def prepare_sample(
line_no: int,
raw_sample: Dict[str, Any],
mode: str,
processor: Any,
target_sr: int,
text_normalize_enabled: bool,
sample_rate_normalize_enabled: bool,
) -> Tuple[str, Dict[str, Any], List[Dict[str, Any]]]:
sample = dict(raw_sample)
sample_id = f"{line_no:06d}"
output_record = _make_output_record(raw_sample, sample_id)
if sample.get("text") is None:
raise ValueError(f"jsonl line {line_no}: missing `text`")
text = str(sample["text"])
if text_normalize_enabled:
text = normalize_text(text)
base_path = sample.get("base_path")
audio_map, text_map, speaker_ids = _collect_speaker_fields(sample, base_path)
if mode != "generation" and len(speaker_ids) == 0:
raise ValueError(
f"jsonl line {line_no}: mode={mode} requires at least one paired "
"`prompt_audio_speakerN` + `prompt_text_speakerN`."
)
if mode == "generation":
conversation = [processor.build_user_message(text=text)]
return sample_id, output_record, conversation
if mode in ("continuation", "voice_clone_and_continuation"):
text = _build_prefixed_text(
text=text, text_map=text_map, speaker_ids=speaker_ids
)
if text_normalize_enabled:
text = normalize_text(text)
if mode == "continuation":
prompt_audio = _encode_concat_prompt_audio(
processor=processor,
audio_map=audio_map,
speaker_ids=speaker_ids,
target_sr=target_sr,
sample_rate_normalize_enabled=sample_rate_normalize_enabled,
)
conversation = [
processor.build_user_message(text=text),
processor.build_assistant_message(audio_codes_list=[prompt_audio]),
]
return sample_id, output_record, conversation
if mode == "voice_clone":
reference = _encode_references(
processor=processor,
audio_map=audio_map,
speaker_ids=speaker_ids,
target_sr=target_sr,
sample_rate_normalize_enabled=sample_rate_normalize_enabled,
)
conversation = [processor.build_user_message(text=text, reference=reference)]
return sample_id, output_record, conversation
if mode == "voice_clone_and_continuation":
reference = _encode_references(
processor=processor,
audio_map=audio_map,
speaker_ids=speaker_ids,
target_sr=target_sr,
sample_rate_normalize_enabled=sample_rate_normalize_enabled,
)
prompt_audio = _encode_concat_prompt_audio(
processor=processor,
audio_map=audio_map,
speaker_ids=speaker_ids,
target_sr=target_sr,
sample_rate_normalize_enabled=sample_rate_normalize_enabled,
)
conversation = [
processor.build_user_message(text=text, reference=reference),
processor.build_assistant_message(audio_codes_list=[prompt_audio]),
]
return sample_id, output_record, conversation
raise ValueError(f"Unexpected mode: {mode}")
def run_infer_batch(
batch_data: List[Tuple[str, Dict[str, Any], List[Dict[str, Any]]]],
model: Any,
processor: Any,
mode: str,
device: str,
max_new_tokens: int,
temperature: float,
top_p: float,
top_k: int,
repetition_penalty: float,
save_dir: Path,
out_fp,
) -> None:
sample_ids, output_records, conversations = zip(*batch_data)
input_batch = processor(
list(conversations),
mode=(
"continuation"
if mode in ("continuation", "voice_clone_and_continuation")
else "generation"
),
)
outputs = model.generate(
input_ids=input_batch["input_ids"].to(device),
attention_mask=input_batch["attention_mask"].to(device),
max_new_tokens=max_new_tokens,
audio_temperature=temperature,
audio_top_p=top_p,
audio_top_k=top_k,
audio_repetition_penalty=repetition_penalty,
)
messages = processor.decode(outputs)
sampling_rate = int(processor.model_config.sampling_rate)
for sample_id, output_record, message in zip(sample_ids, output_records, messages):
record = dict(output_record)
if message is None or len(message.audio_codes_list) == 0:
record["output_audio"] = None
record["duration"] = 0.0
record = _abspath_record_paths(record)
_write_jsonl_line(out_fp, record)
continue
wav_segments: List[torch.Tensor] = []
for wav in message.audio_codes_list:
if not isinstance(wav, torch.Tensor):
continue
wav_segments.append(
wav.detach().to(dtype=torch.float32, device="cpu").reshape(-1)
)
if len(wav_segments) == 0:
record["output_audio"] = None
record["duration"] = 0.0
record = _abspath_record_paths(record)
_write_jsonl_line(out_fp, record)
continue
merged_wav = torch.cat(wav_segments, dim=0)
save_path = (save_dir / f"{sample_id}.wav").resolve()
sf.write(str(save_path), merged_wav.numpy(), sampling_rate)
record["output_audio"] = str(save_path)
record["duration"] = float(merged_wav.shape[-1] / sampling_rate)
record = _abspath_record_paths(record)
_write_jsonl_line(out_fp, record)
def _record_sort_key(record: Dict[str, Any]) -> Tuple[int, str]:
raw_id = record.get("id", "")
try:
numeric_id = int(str(raw_id))
except (TypeError, ValueError):
numeric_id = 10**18
return numeric_id, str(raw_id)
def merge_rank_jsonl_files(
output_jsonl_path: Path,
rank_jsonl_paths: List[Path],
) -> None:
import heapq
existing_paths = [p for p in rank_jsonl_paths if p.is_file()]
if len(existing_paths) == 0:
output_jsonl_path.parent.mkdir(parents=True, exist_ok=True)
with open(output_jsonl_path, "w", encoding="utf-8"):
pass
return
fps = [open(p, "r", encoding="utf-8") for p in existing_paths]
try:
heap: List[Tuple[Tuple[int, str], int, Dict[str, Any]]] = []
for idx, fp in enumerate(fps):
line = fp.readline()
if not line:
continue
obj = json.loads(line)
heapq.heappush(heap, (_record_sort_key(obj), idx, obj))
output_jsonl_path.parent.mkdir(parents=True, exist_ok=True)
with open(output_jsonl_path, "w", encoding="utf-8") as out_fp:
while heap:
_, file_idx, obj = heapq.heappop(heap)
_write_jsonl_line(out_fp, _abspath_record_paths(obj))
line = fps[file_idx].readline()
if not line:
continue
next_obj = json.loads(line)
heapq.heappush(heap, (_record_sort_key(next_obj), file_idx, next_obj))
finally:
for fp in fps:
fp.close()