File size: 3,340 Bytes
4198d45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
"""Run released pre-decoders on the five T0-T4 simulated noise tasks."""

from __future__ import annotations

import argparse
import sys
from pathlib import Path

CODE_ROOT = Path(__file__).resolve().parents[1]
if str(CODE_ROOT) not in sys.path:
    sys.path.insert(0, str(CODE_ROOT))

from scripts.qadapt_example_utils import (  # noqa: E402
    InferenceJob,
    TASK_CONFIGS,
    add_common_inference_args,
    build_paired_command,
    parse_gpus,
    run_jobs,
)


TASK_BY_ID = {
    f"T{index}": (task_key, config_name)
    for index, (task_key, config_name) in enumerate(TASK_CONFIGS)
}


def parse_distances(value: str) -> list[int]:
    result = [int(item.strip()) for item in value.split(",") if item.strip()]
    if not result or result != sorted(set(result)):
        raise argparse.ArgumentTypeError(
            "distances must be a non-empty, increasing comma-separated list"
        )
    return result


def parse_tasks(value: str) -> list[str]:
    result = [item.strip().upper() for item in value.split(",") if item.strip()]
    if not result or len(result) != len(set(result)):
        raise argparse.ArgumentTypeError(
            "tasks must be a non-empty comma-separated subset of T0,T1,T2,T3,T4"
        )
    unknown = [item for item in result if item not in TASK_BY_ID]
    if unknown:
        raise argparse.ArgumentTypeError(f"unknown task(s): {','.join(unknown)}")
    return result


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        "--distances",
        type=parse_distances,
        default=[9],
        help=(
            "Comma-separated distances. Use 7,9 with --tasks T0 for the "
            "paper's mapped-noise geometry; the default is release coverage at d=9."
        ),
    )
    parser.add_argument(
        "--tasks",
        type=parse_tasks,
        default=list(TASK_BY_ID),
        help="Comma-separated task subset; defaults to T0,T1,T2,T3,T4.",
    )
    parser.add_argument("--n-rounds", type=int, default=9)
    add_common_inference_args(
        parser,
        default_output_dir=Path("outputs/examples/released_models/t0_t4"),
    )
    return parser.parse_args()


def main() -> None:
    args = parse_args()
    jobs = []
    for distance in args.distances:
        for task_id in args.tasks:
            task_key, config_name = TASK_BY_ID[task_id]
            label = f"d{distance}_{task_key}"
            output_path = args.output_dir / f"d{distance}" / f"{task_key}.json"
            jobs.append(
                InferenceJob(
                    label=label,
                    command=build_paired_command(
                        args,
                        config_name=config_name,
                        output_path=output_path,
                        distance=distance,
                        n_rounds=args.n_rounds,
                    ),
                    output_path=output_path,
                )
            )
    run_jobs(
        jobs,
        gpus=parse_gpus(args.gpus),
        parallelism=args.parallelism,
        resume=args.resume,
        dry_run=args.dry_run,
    )


if __name__ == "__main__":
    main()