File size: 7,599 Bytes
21c7db9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Create/update the private Hugging Face Space used for PolyGuard training."""

from __future__ import annotations

import argparse
import os
from pathlib import Path
import shutil
import sys

from huggingface_hub import HfApi


ROOT = Path(__file__).resolve().parents[1]


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description="Deploy the PolyGuard remote training Space.")
    parser.add_argument("--repo-id", default="TheJackBright/polyguard-openenv-training-full")
    parser.add_argument("--artifact-repo-id", default="TheJackBright/polyguard-openenv-training-full-artifacts")
    parser.add_argument("--model-id", default="Qwen/Qwen2.5-0.5B-Instruct")
    parser.add_argument(
        "--model-sweep",
        default="Qwen/Qwen2.5-0.5B-Instruct,Qwen/Qwen2.5-1.5B-Instruct,Qwen/Qwen2.5-3B-Instruct",
    )
    parser.add_argument(
        "--training-mode",
        choices=["full", "sft-baseline"],
        default="full",
        help="Run the full SFT+GRPO sweep or an SFT-only baseline sweep.",
    )
    parser.add_argument("--sft-epochs", type=int, default=2)
    parser.add_argument(
        "--sft-epoch-sweep",
        default="",
        help="Optional comma-separated per-model SFT epochs, aligned to --model-sweep.",
    )
    parser.add_argument("--grpo-epochs", type=float, default=1.0)
    parser.add_argument("--sft-max-steps", type=int, default=0)
    parser.add_argument(
        "--sft-max-step-sweep",
        default="",
        help="Optional comma-separated per-model SFT max steps, aligned to --model-sweep.",
    )
    parser.add_argument(
        "--sft-batch-size-sweep",
        default="",
        help="Optional comma-separated per-model SFT batch sizes, aligned to --model-sweep.",
    )
    parser.add_argument("--grpo-max-steps", type=int, default=0)
    parser.add_argument("--grpo-max-prompts", type=int, default=0)
    parser.add_argument("--grpo-num-generations", type=int, default=2)
    parser.add_argument("--reuse-remote-grpo", action="store_true")
    parser.add_argument("--hardware", default="a10g-large")
    parser.add_argument("--sleep-time", type=int, default=3600)
    parser.add_argument("--bundle-dir", default="/tmp/polyguard-openenv-training-space")
    parser.add_argument("--public", action="store_true")
    parser.add_argument("--skip-upload", action="store_true")
    parser.add_argument("--bundle-only", action="store_true")
    return parser.parse_args()


def _ignore(_dir: str, names: list[str]) -> set[str]:
    ignored = {
        ".git",
        ".venv",
        "__pycache__",
        ".pytest_cache",
        ".mypy_cache",
        ".ruff_cache",
        "outputs",
        "checkpoints",
        "submission_bundle",
        "hf_artifacts",
        "polyguard_rl.egg-info",
        "dist",
        "build",
    }
    return {
        name
        for name in names
        if name in ignored
        or name.endswith(".pyc")
        or name == "node_modules"
        or name == ".DS_Store"
    }


def build_bundle(bundle_dir: Path) -> None:
    if bundle_dir.exists():
        shutil.rmtree(bundle_dir)
    shutil.copytree(ROOT, bundle_dir, ignore=_ignore)
    shutil.copy2(ROOT / "app" / "hf_space" / "Dockerfile", bundle_dir / "Dockerfile")
    project_readme = bundle_dir / "PROJECT_README.md"
    if (bundle_dir / "README.md").exists():
        (bundle_dir / "README.md").replace(project_readme)
    (bundle_dir / "README.md").write_text(
        """---
title: PolyGuard HF Training
sdk: docker
app_port: 7860
pinned: false
---

# PolyGuard HF Training

Private Docker Space for running PolyGuard SFT/GRPO training on Hugging Face hardware.

The original project README is included as `PROJECT_README.md`.
""",
        encoding="utf-8",
    )


def main() -> None:
    args = parse_args()
    bundle_dir = Path(args.bundle_dir)
    build_bundle(bundle_dir)
    if args.bundle_only:
        print(f"bundle_dir={bundle_dir}")
        return

    token = os.getenv("HF_TOKEN")
    api = HfApi(token=token)
    whoami = api.whoami(token=token)
    username = str(whoami.get("name") or whoami.get("fullname") or "")
    if username and not args.repo_id.startswith(f"{username}/"):
        print(f"[deploy_training_space] authenticated as {username}; target={args.repo_id}")

    space_variables = [
        {"key": "POLYGUARD_MODEL_ID", "value": args.model_id},
        {"key": "POLYGUARD_MODEL_SWEEP", "value": args.model_sweep},
        {"key": "POLYGUARD_TRAINING_MODE", "value": args.training_mode},
        {"key": "POLYGUARD_OFFLINE_MODE", "value": "false"},
        {"key": "POLYGUARD_AUTORUN", "value": "1"},
        {"key": "POLYGUARD_ARTIFACT_REPO_ID", "value": args.artifact_repo_id},
        {"key": "POLYGUARD_SPACE_REPO_ID", "value": args.repo_id},
        {"key": "POLYGUARD_SFT_EPOCHS", "value": str(args.sft_epochs)},
        {"key": "POLYGUARD_SFT_EPOCH_SWEEP", "value": args.sft_epoch_sweep},
        {"key": "POLYGUARD_GRPO_EPOCHS", "value": str(args.grpo_epochs)},
        {"key": "POLYGUARD_SFT_MAX_STEPS", "value": str(args.sft_max_steps)},
        {"key": "POLYGUARD_SFT_MAX_STEP_SWEEP", "value": args.sft_max_step_sweep},
        {"key": "POLYGUARD_SFT_BATCH_SIZE_SWEEP", "value": args.sft_batch_size_sweep},
        {"key": "POLYGUARD_GRPO_MAX_STEPS", "value": str(args.grpo_max_steps)},
        {"key": "POLYGUARD_GRPO_MAX_PROMPTS", "value": str(args.grpo_max_prompts)},
        {"key": "POLYGUARD_GRPO_NUM_GENERATIONS", "value": str(args.grpo_num_generations)},
        {"key": "POLYGUARD_REUSE_REMOTE_GRPO", "value": "true" if args.reuse_remote_grpo else "false"},
        {"key": "POLYGUARD_INCREMENTAL_UPLOAD", "value": "true"},
        {"key": "POLYGUARD_UPLOAD_AFTER_EACH_STAGE", "value": "true"},
        {"key": "POLYGUARD_LOG_UPLOAD_INTERVAL_SECONDS", "value": "180"},
    ]
    space_secrets = [{"key": "HF_TOKEN", "value": token}] if token else None

    api.create_repo(repo_id=args.artifact_repo_id, repo_type="model", private=True, exist_ok=True)
    api.create_repo(
        repo_id=args.repo_id,
        repo_type="space",
        space_sdk="docker",
        private=not args.public,
        exist_ok=True,
        space_hardware=args.hardware,
        space_sleep_time=args.sleep_time,
        space_variables=space_variables,
        space_secrets=space_secrets,
    )

    for variable in space_variables:
        api.add_space_variable(repo_id=args.repo_id, key=variable["key"], value=variable["value"])
    if token:
        api.add_space_secret(repo_id=args.repo_id, key="HF_TOKEN", value=token)
    if not args.skip_upload:
        api.upload_folder(
            repo_id=args.repo_id,
            repo_type="space",
            folder_path=str(bundle_dir),
            commit_message="Deploy PolyGuard HF training Space",
            ignore_patterns=[
                ".git/*",
                ".venv/*",
                "**/node_modules/*",
                "outputs/*",
                "checkpoints/*",
                "submission_bundle/*",
                "hf_artifacts/*",
                "**/__pycache__/*",
                "*.pyc",
            ],
        )

    try:
        api.request_space_hardware(repo_id=args.repo_id, hardware=args.hardware, sleep_time=args.sleep_time)
    except Exception as exc:  # noqa: BLE001
        print(f"hardware_request_warning={exc}", file=sys.stderr)

    print(f"space_url=https://huggingface.co/spaces/{args.repo_id}")
    print(f"artifact_repo=https://huggingface.co/{args.artifact_repo_id}")
    print(f"bundle_dir={bundle_dir}")


if __name__ == "__main__":
    main()