| """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 _coerce_str_map(value: Any) -> dict[str, str]: |
| """Coerce a loosely-typed spec field into a ``dict[str, str]``. |
| |
| A malformed persisted spec (or programmatic caller) can set a mapping field to a non-dict; |
| `.items()` on that would crash `from_dict` with AttributeError. Treat a non-dict as empty, |
| mirroring how the other nested fields tolerate missing/garbage input. |
| """ |
| if not isinstance(value, dict): |
| return {} |
| return {str(k): str(v) for k, v in value.items()} |
|
|
|
|
| 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: |
| |
| |
| id: str = "" |
| params: dict[str, Any] = field(default_factory=dict) |
| |
| |
| |
| 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,) |
| |
| |
| init_from_adapter: str = "" |
| |
| |
| |
| hf_repo: str = "" |
| |
| |
| |
| |
| learning_rate: float | None = None |
| batch_size: int | None = None |
| max_length: int | None = None |
| save_every: int | None = None |
| |
| |
| max_steps: int | None = None |
| max_examples: int | None = None |
| |
| |
| |
| |
| |
| |
| 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, ...] = () |
| |
| |
| |
| |
| |
| |
| eval_every_steps: int | None = None |
| |
| |
| |
| eval_examples: int | None = None |
|
|
|
|
| @dataclass(frozen=True) |
| class GpuSpec: |
| type: str = DEFAULT_GPU |
| |
| |
| provider: str = "auto" |
| |
| |
| |
| |
| requested: str = "" |
| |
| |
| allow_unvalidated: bool | None = None |
| disk_gb: int = 60 |
| max_wall_seconds: int = 24 * 3600 |
| |
| |
| max_retries: int = 2 |
| |
| |
| |
| |
| |
| |
| network_volume: str | None = None |
| network_volume_gb: int = 100 |
| datacenter: str | None = None |
|
|
|
|
| @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" |
| |
| |
| |
| |
| |
| worker_env: dict[str, str] = field(default_factory=dict) |
| |
| model_policy: str = "catalog" |
| |
| |
| |
| thinking: bool = False |
|
|
| @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 {} |
| |
| |
| 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")), |
| max_steps=_opt_int(train.get("max_steps")), |
| max_examples=_opt_int(train.get("max_examples")), |
| 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")), |
| eval_every_steps=_opt_int(train.get("eval_every_steps")), |
| eval_examples=_opt_int(train.get("eval_examples")), |
| ), |
| 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"), |
| worker_env=_coerce_str_map(data.get("worker_env")), |
| model_policy=data.get("model_policy", "catalog"), |
| thinking=_coerce_bool(data.get("thinking", False)), |
| ) |
|
|
| @classmethod |
| def from_json(cls, raw: str) -> JobSpec: |
| return cls.from_dict(json.loads(raw)) |
|
|
|
|
| def load_job_spec_from_env() -> JobSpec | None: |
| """Load FLASH_JOB_SPEC_JSON or FLASH_JOB_SPEC_PATH if present on a worker node.""" |
| raw = os.environ.get("FLASH_JOB_SPEC_JSON") |
| if raw: |
| return JobSpec.from_json(raw) |
| path = os.environ.get("FLASH_JOB_SPEC_PATH") |
| if path and os.path.exists(path): |
| with open(path) as f: |
| return JobSpec.from_json(f.read()) |
| return None |
|
|