File size: 12,393 Bytes
d0768a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Launch multiple preprocessing workers in parallel while preserving the ability
to resume from previous runs. Each worker operates on a disjoint shard of the
manifest and writes outputs to a temporary worker directory; once finished, the
results are merged back into the main output directory and the worker assets are
removed.
"""

from __future__ import annotations

import argparse
import json
import math
import os
import shutil
import subprocess
import sys
import tempfile
from pathlib import Path
from typing import Dict, Iterable, List, Optional, Sequence, Tuple

import time


MANIFEST_TRAIN = "train_manifest.jsonl"
MANIFEST_VAL = "val_manifest.jsonl"
STATS_FILE = "stats.json"
FEATURE_SUBDIRS = ["codes", "condition", "emo_vec", "text_ids"]


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(
        description="Run IndexTTS2 preprocessing across multiple worker processes."
    )
    parser.add_argument("--manifest", type=Path, required=True, help="Source dataset manifest JSONL.")
    parser.add_argument("--output-dir", type=Path, required=True, help="Destination directory.")
    parser.add_argument("--tokenizer", type=Path, required=True, help="SentencePiece model path.")
    parser.add_argument(
        "--config",
        type=Path,
        default=Path("checkpoints/config_finetune.yaml"),
        help="Model config YAML (default: checkpoints/config_finetune.yaml).",
    )
    parser.add_argument(
        "--gpt-checkpoint",
        type=Path,
        default=Path("checkpoints/gpt.pth"),
        help="Base GPT checkpoint (default: checkpoints/gpt.pth).",
    )
    parser.add_argument("--language", type=str, default="ja", help="Language hint for normaliser.")
    parser.add_argument("--device", type=str, default="cuda", help="Device string (default: cuda).")
    parser.add_argument("--batch-size", type=int, default=4, help="Batch size per worker.")
    parser.add_argument("--workers", type=int, default=8, help="DataLoader worker threads per process.")
    parser.add_argument(
        "--num-processes",
        type=int,
        default=4,
        help="Number of parallel preprocessing processes to launch.",
    )
    parser.add_argument(
        "--launch-delay",
        type=float,
        default=0.0,
        help="Seconds to sleep between launching each worker process (default: 0).",
    )
    parser.add_argument(
        "--val-ratio",
        type=float,
        default=0.01,
        help="Validation ratio passed to preprocess_data.py (default: 0.01).",
    )
    parser.add_argument(
        "--skip-existing",
        action="store_true",
        help="Forward --skip-existing to each worker.",
    )
    parser.add_argument(
        "--max-samples",
        type=int,
        default=0,
        help="Maximum samples to process overall (0 = all remaining).",
    )
    parser.add_argument(
        "--extra-args",
        nargs=argparse.REMAINDER,
        help="Additional arguments forwarded to preprocess_data.py after a '--'.",
    )
    parser.add_argument(
        "--hf-cache-dir",
        type=Path,
        default=None,
        help="Directory to use for Hugging Face caches/offline assets (default: ./hf_cache).",
    )
    return parser.parse_args()


def cache_has_required_assets(cache_dir: Path) -> bool:
    required = [
        cache_dir / "models--facebook--seamless-m4t-medium",
        cache_dir / "models--amphion--MaskGCT",
    ]
    return all(path.exists() for path in required)


def merge_manifest_shards(base_path: Path, pattern: str, target: Path) -> None:
    shard_files = sorted(base_path.glob(pattern))
    if not shard_files:
        return
    target.parent.mkdir(parents=True, exist_ok=True)
    with target.open("a", encoding="utf-8") as dst:
        for shard in shard_files:
            with shard.open("r", encoding="utf-8") as src:
                shutil.copyfileobj(src, dst)
            shard.unlink()


def move_feature_tree(source_dir: Path, dest_dir: Path) -> None:
    for sub in FEATURE_SUBDIRS:
        src_sub = source_dir / sub
        if not src_sub.exists():
            continue
        dst_sub = dest_dir / sub
        dst_sub.mkdir(parents=True, exist_ok=True)
        for path in src_sub.iterdir():
            target_path = dst_sub / path.name
            if target_path.exists():
                # Already present; assume prior run completed this sample.
                path.unlink()
            else:
                shutil.move(str(path), str(target_path))


def consolidate_previous_shards(output_dir: Path) -> None:
    # Merge stray worker manifests if the previous run exited early.
    merge_manifest_shards(output_dir, "train_manifest.worker_*.jsonl", output_dir / MANIFEST_TRAIN)
    merge_manifest_shards(output_dir, "val_manifest.worker_*.jsonl", output_dir / MANIFEST_VAL)
    for worker_dir in sorted(output_dir.glob("worker_*")):
        merge_worker_results(worker_dir, output_dir)


def load_processed_ids(manifest_paths: Iterable[Path]) -> set[str]:
    ids: set[str] = set()
    for path in manifest_paths:
        if not path.exists():
            continue
        with path.open("r", encoding="utf-8") as handle:
            for line in handle:
                if not line.strip():
                    continue
                try:
                    record = json.loads(line)
                except json.JSONDecodeError:
                    continue
                sample_id = record.get("id")
                if sample_id:
                    ids.add(sample_id)
    return ids


def remaining_manifest_entries(
    manifest: Path,
    processed_ids: set[str],
    max_samples: int,
) -> List[str]:
    remaining: List[str] = []
    with manifest.open("r", encoding="utf-8") as handle:
        for line in handle:
            if not line.strip():
                continue
            try:
                record = json.loads(line)
            except json.JSONDecodeError:
                continue
            sample_id = record.get("id")
            if not sample_id or sample_id in processed_ids:
                continue
            remaining.append(line)
            if max_samples and len(remaining) >= max_samples:
                break
    return remaining


def write_chunks(
    lines: Sequence[str],
    num_chunks: int,
    work_dir: Path,
    prefix: str,
) -> List[tuple[Path, int]]:
    work_dir.mkdir(parents=True, exist_ok=True)
    chunks: List[tuple[Path, int]] = []
    if not lines:
        return chunks
    chunk_size = math.ceil(len(lines) / num_chunks)
    for idx in range(num_chunks):
        start = idx * chunk_size
        if start >= len(lines):
            break
        end = min(len(lines), start + chunk_size)
        chunk_path = work_dir / f"{prefix}_chunk_{idx:02d}.jsonl"
        with chunk_path.open("w", encoding="utf-8") as handle:
            handle.writelines(lines[start:end])
        chunks.append((chunk_path, end - start))
    return chunks


def launch_worker(
    chunk_manifest: Path,
    worker_output: Path,
    args: argparse.Namespace,
    hf_env: Dict[str, str],
) -> subprocess.Popen:
    cmd = [
        sys.executable,
        "-u",
        "tools/preprocess_data.py",
        "--manifest",
        str(chunk_manifest),
        "--output-dir",
        str(worker_output),
        "--tokenizer",
        str(args.tokenizer),
        "--config",
        str(args.config),
        "--gpt-checkpoint",
        str(args.gpt_checkpoint),
        "--language",
        args.language,
        "--device",
        args.device,
        "--batch-size",
        str(args.batch_size),
        "--workers",
        str(args.workers),
        "--val-ratio",
        str(args.val_ratio),
    ]
    if args.skip_existing:
        cmd.append("--skip-existing")
    if args.extra_args:
        cmd.append("--")
        cmd.extend(args.extra_args)
    env = os.environ.copy()
    for key, value in hf_env.items():
        env.setdefault(key, value)
    return subprocess.Popen(cmd, env=env)


def append_and_remove(source: Path, destination: Path) -> None:
    if not source.exists():
        return
    destination.parent.mkdir(parents=True, exist_ok=True)
    with destination.open("a", encoding="utf-8") as dst, source.open("r", encoding="utf-8") as src:
        shutil.copyfileobj(src, dst)
    source.unlink()


def merge_worker_results(worker_dir: Path, main_output: Path) -> None:
    append_and_remove(worker_dir / MANIFEST_TRAIN, main_output / MANIFEST_TRAIN)
    append_and_remove(worker_dir / MANIFEST_VAL, main_output / MANIFEST_VAL)
    move_feature_tree(worker_dir, main_output)
    stats_path = worker_dir / STATS_FILE
    if stats_path.exists():
        stats_path.unlink()
    shutil.rmtree(worker_dir)


def main() -> None:
    args = parse_args()
    output_dir = args.output_dir.expanduser().resolve()
    output_dir.mkdir(parents=True, exist_ok=True)

    hf_cache_dir = (
        args.hf_cache_dir.expanduser().resolve()
        if args.hf_cache_dir
        else (Path.cwd() / "hf_cache")
    )
    hf_cache_dir.mkdir(parents=True, exist_ok=True)
    hf_env = {
        "HF_HOME": str(hf_cache_dir),
        "HF_HUB_CACHE": str(hf_cache_dir),
        "HF_DATASETS_CACHE": str(hf_cache_dir),
    }
    if cache_has_required_assets(hf_cache_dir):
        hf_env["HF_HUB_OFFLINE"] = "1"
        hf_env["TRANSFORMERS_OFFLINE"] = "1"
    else:
        print("[preprocess_multiproc] HF cache missing SeamlessM4T/MaskGCT weights; running in online mode to populate cache.")

    consolidate_previous_shards(output_dir)

    processed_ids = load_processed_ids(
        [
            output_dir / MANIFEST_TRAIN,
            output_dir / MANIFEST_VAL,
        ]
    )

    manifest_path = args.manifest.expanduser().resolve()
    remaining_lines = remaining_manifest_entries(manifest_path, processed_ids, args.max_samples)
    if not remaining_lines:
        print("No remaining samples. Nothing to do.")
        return

    num_processes = min(max(1, args.num_processes), len(remaining_lines))
    chunk_root = manifest_path.parent.parent / "_preprocess_chunks"
    chunk_root.mkdir(parents=True, exist_ok=True)
    chunk_prefix = manifest_path.parent.name
    chunks = write_chunks(remaining_lines, num_processes, chunk_root, chunk_prefix)
    if not chunks:
        print("Failed to create chunk manifests.")
        return

    total_samples = sum(count for _, count in chunks)
    print(
        f"[preprocess_multiproc] Remaining samples: {len(remaining_lines)} "
        f"(assigned {total_samples} across {len(chunks)} workers)."
    )
    for idx, (chunk_path, count) in enumerate(chunks):
        print(f"[preprocess_multiproc] Worker {idx:02d} -> {chunk_path} ({count} samples)")

    processes: List[subprocess.Popen] = []
    worker_dirs: List[Path] = []
    try:
        for idx, (chunk_path, count) in enumerate(chunks):
            worker_dir = output_dir / f"worker_{idx:02d}"
            worker_dir.mkdir(parents=True, exist_ok=True)
            print(
                f"[preprocess_multiproc] Launching worker {idx:02d} "
                f"({count} samples) -> dir {worker_dir}"
            )
            proc = launch_worker(chunk_path, worker_dir, args, hf_env)
            processes.append(proc)
            worker_dirs.append(worker_dir)
            if idx < len(chunks) - 1 and args.launch_delay > 0:
                print(
                    f"[preprocess_multiproc] Sleeping {args.launch_delay:.2f}s before launching next worker..."
                )
                time.sleep(args.launch_delay)

        return_codes = [proc.wait() for proc in processes]
        if any(code != 0 for code in return_codes):
            for idx, code in enumerate(return_codes):
                if code != 0:
                    print(f"Worker {idx} exited with code {code}", file=sys.stderr)
            raise RuntimeError("One or more workers failed. Check logs above.")

        for worker_dir in worker_dirs:
            merge_worker_results(worker_dir, output_dir)

    finally:
        for chunk_path, _ in chunks:
            if chunk_path.exists():
                chunk_path.unlink()
        try:
            if chunk_root.exists() and not any(chunk_root.iterdir()):
                chunk_root.rmdir()
        except OSError:
            pass

    print("All workers completed successfully.")


if __name__ == "__main__":
    main()