File size: 10,855 Bytes
cb2775c | 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 | """Structured job specification shared by CLI/API/runner and GPU workers."""
from __future__ import annotations
import json
import os
from dataclasses import asdict, dataclass, field
from typing import Any
from .catalog import DEFAULT_GPU, DEFAULT_MODEL, normalize_algorithm
_FALSE_STRINGS = {"", "0", "false", "no", "off", "none"}
def _str_tuple(value: Any) -> tuple[str, ...]:
"""Normalize a string-or-list knob (e.g. stop_sequences) to a tuple of strings.
A bare string is ONE element — never iterated into characters ("</s>" must not become
('<','/','s','>')). None and empty strings -> () (no stop configured); empty entries
in a list are dropped."""
if value is None:
return ()
if isinstance(value, str):
return (value,) if value else ()
return tuple(s for s in (str(x) for x in value) if s)
def _coerce_bool(value: Any) -> bool:
"""Parse a bool from loosely-typed spec sources (JSON/env/persisted dicts).
bool(...) on a string is truthy for ANY non-empty string, so "false"/"0" would
wrongly become True; treat the usual falsey strings as False.
"""
if isinstance(value, str):
return value.strip().lower() not in _FALSE_STRINGS
return bool(value)
def _opt_int(value: Any) -> int | None:
"""Parse an optional int from a loosely-typed spec source; None stays None.
Rejects JSON booleans: ``bool`` is an ``int`` subclass in Python, so ``int(True)`` would
silently coerce a stray boolean train knob to 1 (and ``False`` to 0). Mirrors the
bool rejection in schema._train_int — a bool is a type error, not a number.
"""
if value is None:
return None
if isinstance(value, bool):
raise TypeError(f"expected a number, got bool {value!r}")
return int(value)
def _opt_float(value: Any) -> float | None:
"""Parse an optional float from a loosely-typed spec source; None stays None.
Rejects JSON booleans (``bool`` is an ``int`` subclass) so a stray boolean train knob is
not silently coerced to 0.0/1.0; mirrors the bool rejection in schema._train_float.
"""
if value is None:
return None
if isinstance(value, bool):
raise TypeError(f"expected a number, got bool {value!r}")
return float(value)
@dataclass(frozen=True)
class EnvironmentSpec:
# Verifiers/Prime Hub env slug ("owner/name") or installed/local env id. No default:
# a run must name an environment explicitly (validated in schema / the worker).
id: str = ""
params: dict[str, Any] = field(default_factory=dict)
# Pip requirements the GPU worker needs for this environment (verifiers/Hub envs).
# Filled in client-side from the local install manifest so the managed control
# plane never depends on client-local state; empty means "derive on the server".
pip: tuple[str, ...] = ()
@dataclass(frozen=True)
class TrainSpec:
steps: int | None = None
epochs: int | None = None
lora_rank: int = 32
lora_alpha: int = 64
seeds: tuple[int, ...] = (0,)
# Artifact-store adapter prefix (``<phase>/<run_id>/seed<N>``) to initialize the
# LoRA from instead of training fresh — e.g. a GRPO run continuing an SFT adapter.
init_from_adapter: str = ""
# Per-run HuggingFace artifact repo ("owner/name") for this run's adapter/checkpoint/
# code storage AND serving. REQUIRED (validated in schema._validate_spec); there is no
# operator-wide default. The operator's HUGGINGFACE_TOKEN must have write access to it.
hf_repo: str = ""
# Optimizer/batching knobs (SFT + GRPO). None -> the worker's tuned recipe default.
# batch_size is the GLOBAL/effective batch (SFT: grad-accum is sized to hit it; GRPO:
# prompts per optimizer step). max_length is the SFT max sequence length. save_every
# is the checkpoint interval in optimizer steps.
learning_rate: float | None = None
batch_size: int | None = None
max_length: int | None = None
save_every: int | None = None
# GRPO recipe knobs (datums parity), shipped by the SDK in [train] (NOT in
# [environment.params], which is forwarded verbatim to the verifiers env loader).
# None/() -> recipe default. group_size = completions per prompt; temperature = rollout
# sampling temp; max_tokens = completion budget; kl_penalty_coef = KL beta;
# advantage_clip = centered-advantage clamp; thinking_length_penalty_coef =
# per-<think>-token reward deduction; stop_sequences = rollout stop strings.
group_size: int | None = None
temperature: float | None = None
max_tokens: int | None = None
kl_penalty_coef: float | None = None
advantage_clip: float | None = None
thinking_length_penalty_coef: float | None = None
stop_sequences: tuple[str, ...] = ()
@dataclass(frozen=True)
class GpuSpec:
type: str = DEFAULT_GPU
# GPU substrate: "auto" (cheapest across providers at submit time), "runpod", or
# "vast" (verified datacenters only).
provider: str = "auto"
# The raw user gpu.type input ("cheapest"/"auto" or a concrete class), always set
# by config parsing. The runner re-allocates the class at submit time iff
# this is a policy word — ``type`` is then just the parse-time provisional; a
# concrete ``requested`` pins the class and the allocator only picks the provider.
requested: str = ""
# Carried into the submit-time allocator (None -> AUTOSLM_GPU_ALLOW_UNVALIDATED).
allow_unvalidated: bool | None = None
disk_gb: int = 60
max_wall_seconds: int = 24 * 3600
# Auto-resubmit budget for infra-shaped failures (worker loss / stall / timeout);
# each retry resumes from the latest streamed checkpoint.
max_retries: int = 2
# OPT-IN persistent RunPod network volume mounted at /runpod-volume, used as a
# cross-run HF model cache (repeat runs skip the model download). Trade-offs: it
# pins the run to the volume's datacenter (smaller GPU pool — usually the bigger
# cost) and the volume bills monthly while it exists. Off (None) by default.
# RunPod-specific: network_volume/datacenter are read only by the RunPod provider
# and ignored by Vast (which rents single-GPU instances with no network volume).
network_volume: str | None = None
network_volume_gb: int = 100
datacenter: str | None = None # e.g. "EU-RO-1"; required pool pin for the volume
@dataclass(frozen=True)
class JobSpec:
model: str = DEFAULT_MODEL
algorithm: str = "grpo"
environment: EnvironmentSpec = field(default_factory=EnvironmentSpec)
train: TrainSpec = field(default_factory=TrainSpec)
gpu: GpuSpec = field(default_factory=GpuSpec)
run_id: str = "local"
# "catalog" (curated models only) or "allow" (any HF model that fits the GPU).
model_policy: str = "catalog"
# Thinking/reasoning mode (thinking-capable models only). One flag per run, consumed
# identically by SFT rendering, RL rollouts, and serving (decoding parity). ON by default.
thinking: bool = True
@property
def phase(self) -> str:
return "rl" if self.algorithm == "grpo" else self.algorithm
def to_dict(self) -> dict[str, Any]:
return asdict(self)
def to_json(self) -> str:
return json.dumps(self.to_dict(), sort_keys=True)
@classmethod
def from_dict(cls, data: dict[str, Any]) -> JobSpec:
env = data.get("environment") or {}
# Defense-in-depth: a stale/older payload may still carry a local `path`. The worker only
# runs published Hub env ids, so reject it here rather than silently dropping it.
if isinstance(env, dict) and env.get("path"):
raise ValueError(
"local environment paths are no longer supported; the worker only runs "
"published Hub env ids"
)
train = data.get("train") or {}
gpu = data.get("gpu") or {}
return cls(
model=data.get("model", cls.model),
algorithm=normalize_algorithm(data.get("algorithm", cls.algorithm)),
environment=EnvironmentSpec(
id=env.get("id", ""),
params=dict(env.get("params") or {}),
pip=tuple(str(p) for p in env.get("pip") or ()),
),
train=TrainSpec(
steps=_opt_int(train.get("steps")),
epochs=_opt_int(train.get("epochs")),
lora_rank=int(train.get("lora_rank", 32)),
lora_alpha=int(train.get("lora_alpha", 64)),
seeds=tuple(int(s) for s in train.get("seeds", (0,))),
init_from_adapter=str(train.get("init_from_adapter") or ""),
hf_repo=str(train.get("hf_repo") or ""),
learning_rate=_opt_float(train.get("learning_rate")),
batch_size=_opt_int(train.get("batch_size")),
max_length=_opt_int(train.get("max_length")),
save_every=_opt_int(train.get("save_every")),
group_size=_opt_int(train.get("group_size")),
temperature=_opt_float(train.get("temperature")),
max_tokens=_opt_int(train.get("max_tokens")),
kl_penalty_coef=_opt_float(train.get("kl_penalty_coef")),
advantage_clip=_opt_float(train.get("advantage_clip")),
thinking_length_penalty_coef=_opt_float(train.get("thinking_length_penalty_coef")),
stop_sequences=_str_tuple(train.get("stop_sequences")),
),
gpu=GpuSpec(
type=gpu.get("type", DEFAULT_GPU),
provider=gpu.get("provider", "auto"),
requested=gpu.get("requested", ""),
allow_unvalidated=gpu.get("allow_unvalidated"),
disk_gb=int(gpu.get("disk_gb", 60)),
max_wall_seconds=int(gpu.get("max_wall_seconds", 24 * 3600)),
max_retries=int(gpu.get("max_retries", 2)),
network_volume=gpu.get("network_volume"),
network_volume_gb=int(gpu.get("network_volume_gb", 100)),
datacenter=gpu.get("datacenter"),
),
run_id=data.get("run_id", "local"),
model_policy=data.get("model_policy", "catalog"),
thinking=_coerce_bool(data.get("thinking", True)),
)
@classmethod
def from_json(cls, raw: str) -> JobSpec:
return cls.from_dict(json.loads(raw))
def load_job_spec_from_env() -> JobSpec | None:
"""Load AUTOSLM_JOB_SPEC_JSON or AUTOSLM_JOB_SPEC_PATH if present on a worker node."""
raw = os.environ.get("AUTOSLM_JOB_SPEC_JSON")
if raw:
return JobSpec.from_json(raw)
path = os.environ.get("AUTOSLM_JOB_SPEC_PATH")
if path and os.path.exists(path):
with open(path) as f:
return JobSpec.from_json(f.read())
return None
|