| """On-GPU fine-tuning worker (RunPod or Vast.ai). Modes: sft | rl. |
| |
| This module runs on the provisioned GPU (RunPod or Vast.ai) launched by the selected |
| ``autoslm.providers`` backend. It uses the shared recipe (``autoslm.engine.recipe``) so |
| SFT targets and RL rewards are rendered and scored consistently. |
| |
| Artifacts (adapter, metrics.json, heartbeat.json, checkpoints) are streamed to a |
| Hugging Face dataset repo. HF checkpoints give preemption resilience: if a worker is |
| recycled mid-run we resume from the latest uploaded checkpoint. Metrics are also |
| returned directly to the caller by the launching provider. |
| |
| Core environment variables (set by the launching provider / runner): |
| RUN_MODE sft|rl |
| SEED int |
| HF_REPO Hugging Face dataset repo for artifacts, populated per-run from the |
| JobSpec's [train] hf_repo by whichever provider launches the worker |
| HF_TOKEN |
| RUN_ID unique id for this run (namespacing in the repo) |
| |
| The AUTOSLM_*/RL_*/SFT_* env vars are A/B overrides documented at their use sites; the |
| JobSpec [train] table is the source of truth for per-run knobs. |
| """ |
|
|
| from __future__ import annotations |
|
|
| import contextlib |
| import json |
| import os |
| import random |
| import sys |
| import threading |
| import time |
| import traceback |
|
|
| from autoslm.engine.accounting import RunMetrics |
|
|
| |
| from autoslm.engine.recipe import RECIPE |
| from autoslm.envs.registry import load_environment |
| from autoslm.spec import load_job_spec_from_env |
|
|
| HF_REPO = os.environ.get("HF_REPO", "") |
| RUN_ID = os.environ.get("RUN_ID", "local") |
| SEED = int(os.environ.get("SEED", "0")) |
| RUN_MODE = os.environ.get("RUN_MODE", "sft") |
| JOB_SPEC = load_job_spec_from_env() |
| |
| PHASE = os.environ.get( |
| "PHASE", |
| JOB_SPEC.phase if JOB_SPEC else (RUN_MODE if RUN_MODE in ("sft", "rl") else "sft"), |
| ) |
|
|
|
|
| def _load_active_env(): |
| """Load the run's verifiers environment from the JobSpec; require an explicit env. |
| |
| There is no default/builtin environment (verifiers-only): a run MUST name a verifiers/ |
| Prime Hub env id. Failing here (instead of falling back to some default) prevents a paid |
| worker from training/evaluating the wrong task. |
| """ |
| if JOB_SPEC is None: |
| |
| |
| |
| return None |
| env_id = JOB_SPEC.environment.id |
| if not env_id: |
| |
| |
| |
| raise RuntimeError( |
| "JobSpec sets no environment: provide [environment] id (a verifiers/Prime Hub " |
| "slug, e.g. 'owner/name')." |
| ) |
| return load_environment(env_id, JOB_SPEC.environment.params) |
|
|
|
|
| ACTIVE_ENV = _load_active_env() |
|
|
|
|
| def require_active_env(): |
| """Return the run's loaded environment, or raise a CLEAR error when there is none. |
| |
| ``ACTIVE_ENV`` is None on the no-JobSpec path (the module is imported with no |
| AUTOSLM_JOB_SPEC_JSON/PATH, e.g. a misconfigured worker launch). Every train/eval consumer |
| needs a real env; without this guard the first ``ACTIVE_ENV.<attr>`` access dies with an |
| opaque ``AttributeError: 'NoneType' object has no attribute ...``. Fail loudly with an |
| actionable message instead — mirrors the explicit RuntimeError raised when a JobSpec is |
| present but names no environment. |
| """ |
| if ACTIVE_ENV is None: |
| raise RuntimeError( |
| "no environment is loaded: this worker was started without a JobSpec " |
| "(AUTOSLM_JOB_SPEC_JSON / AUTOSLM_JOB_SPEC_PATH is unset). A train/eval run must " |
| "carry a JobSpec naming [environment] id (a verifiers/Prime Hub slug, e.g. " |
| "'owner/name')." |
| ) |
| return ACTIVE_ENV |
|
|
|
|
| |
| |
| THINKING = JOB_SPEC.thinking if JOB_SPEC else False |
|
|
|
|
| |
| |
| |
| def error_artifact_name(mode: str) -> str: |
| """Per-mode error filename (e.g. error_sft.txt) so a run's traceback is uploaded |
| under a stable name even though heartbeat.json is single-file/overwritten.""" |
| return f"error_{mode}.txt" |
|
|
|
|
| def hf_api(): |
| from huggingface_hub import HfApi |
|
|
| return HfApi(token=os.environ.get("HF_TOKEN")) |
|
|
|
|
| def hf_prefix() -> str: |
| return f"{PHASE}/{RUN_ID}/seed{SEED}" |
|
|
|
|
| def _hf_upload(do_upload, repo_subpath: str, required: bool, label: str) -> None: |
| """Shared HF upload loop for files/folders: HF_REPO guard + retry/raise-or-warn. |
| |
| ``required=True`` (completion artifacts DONE/metrics.json, the trained adapter) retries |
| and finally raises: a swallowed upload failure would make the control plane mark a |
| finished run failed/retried, or mark the run done while deployment can never download |
| the missing adapter. Optional artifacts (generations, logs) only warn. |
| """ |
| if not HF_REPO: |
| return |
| attempts = 3 if required else 1 |
| for attempt in range(attempts): |
| try: |
| do_upload() |
| return |
| except Exception as e: |
| if required and attempt + 1 < attempts: |
| print(f"{label} retry {attempt + 1}/{attempts}: {e}") |
| time.sleep(5 * (attempt + 1)) |
| continue |
| if required: |
| raise RuntimeError(f"required upload of {repo_subpath!r} failed: {e}") from e |
| print(f"{label} warn:", e) |
| return |
|
|
|
|
| def hf_upload_file(local_path: str, repo_subpath: str, required: bool = False): |
| """Upload one file to the run's HF prefix.""" |
| _hf_upload( |
| lambda: hf_api().upload_file( |
| path_or_fileobj=local_path, |
| path_in_repo=f"{hf_prefix()}/{repo_subpath}", |
| repo_id=HF_REPO, |
| repo_type="dataset", |
| ), |
| repo_subpath, |
| required, |
| "hf_upload_file", |
| ) |
|
|
|
|
| def hf_upload_folder(local_dir: str, repo_subpath: str, required: bool = False): |
| """Upload a folder to the run's HF prefix.""" |
| _hf_upload( |
| lambda: hf_api().upload_folder( |
| folder_path=local_dir, |
| path_in_repo=f"{hf_prefix()}/{repo_subpath}", |
| repo_id=HF_REPO, |
| repo_type="dataset", |
| ), |
| repo_subpath, |
| required, |
| "hf_upload_folder", |
| ) |
|
|
|
|
| def hf_resume_checkpoint() -> str | None: |
| """Latest streamed trainer checkpoint for this run (or None). |
| |
| Checkpoints are uploaded DURING the run by ``make_checkpoint_upload_callback`` as |
| ``<prefix>/checkpoint/checkpoint-<step>/``; a replacement worker downloads the |
| newest one so a mid-run preemption costs at most one save interval. |
| """ |
| if not HF_REPO: |
| return None |
| try: |
| from huggingface_hub import snapshot_download |
|
|
| snapshot_download( |
| repo_id=HF_REPO, |
| repo_type="dataset", |
| allow_patterns=[f"{hf_prefix()}/checkpoint/**"], |
| local_dir="/tmp/resume", |
| token=os.environ.get("HF_TOKEN"), |
| ) |
| base = os.path.join("/tmp/resume", hf_prefix(), "checkpoint") |
| if not os.path.isdir(base): |
| return None |
| cands = [d for d in os.listdir(base) if d.startswith("checkpoint-")] |
| if not cands: |
| return None |
| latest = max(cands, key=lambda d: int(d.split("-")[-1])) |
| path = os.path.join(base, latest) |
| print(f"[resume] found streamed checkpoint: {path}") |
| return path |
| except Exception as e: |
| print("hf_resume_checkpoint warn:", e) |
| return None |
|
|
|
|
| def prefetch_model(model_id: str) -> float: |
| """Pull the model weights into the local HF cache up front; return seconds spent. |
| |
| The trainer/vLLM would download lazily anyway — doing it explicitly (a) makes the |
| download a first-class, timed stage in the heartbeat stream (the cold-start metric |
| the speed work optimizes), and (b) fails fast with a clear disk/network error |
| instead of dying inside trainer construction. Idempotent: a warm cache costs ~0 s. |
| """ |
| from huggingface_hub import snapshot_download |
|
|
| t0 = time.time() |
| try: |
| snapshot_download( |
| repo_id=model_id, |
| |
| ignore_patterns=["*.pth", "*.gguf", "original/*", "*.onnx", "*.msgpack", "*.h5"], |
| ) |
| except Exception as e: |
| |
| |
| print("prefetch_model warn:", e) |
| secs = round(time.time() - t0, 1) |
| heartbeat( |
| "model_prefetched", |
| model=model_id, |
| download_seconds=secs, |
| hf_transfer=os.environ.get("HF_HUB_ENABLE_HF_TRANSFER", ""), |
| ) |
| return secs |
|
|
|
|
| def make_checkpoint_upload_callback(): |
| """Stream each trainer save to HF so preemption loses <= one save interval. |
| |
| Uploads run in a background thread (the train loop never blocks on the network); |
| older checkpoints are deleted in the same commit. If an upload is still in flight |
| when the next save fires, the new save is skipped (the following one catches up). |
| """ |
| import threading |
|
|
| from transformers import TrainerCallback |
|
|
| lock = threading.Lock() |
|
|
| class _CheckpointUpload(TrainerCallback): |
| def on_save(self, args, state, control, **kwargs): |
| if not HF_REPO: |
| return |
| step = int(state.global_step) |
| ckpt_dir = os.path.join(args.output_dir, f"checkpoint-{step}") |
| if not os.path.isdir(ckpt_dir): |
| return |
| if not lock.acquire(blocking=False): |
| print(f"[ckpt] upload busy; skipping step {step}") |
| return |
|
|
| def _upload(): |
| try: |
| hf_api().upload_folder( |
| folder_path=ckpt_dir, |
| path_in_repo=f"{hf_prefix()}/checkpoint/checkpoint-{step}", |
| repo_id=HF_REPO, |
| repo_type="dataset", |
| delete_patterns=[f"{hf_prefix()}/checkpoint/**"], |
| ) |
| heartbeat("checkpoint_uploaded", step=step) |
| except Exception as e: |
| print("ckpt upload warn:", e) |
| finally: |
| lock.release() |
|
|
| threading.Thread(target=_upload, daemon=True).start() |
|
|
| return _CheckpointUpload() |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| _HB_LAST_UPLOAD = 0.0 |
| _HB_MIN_INTERVAL_S = 60.0 |
| _HB_THROTTLED_STAGES = frozenset({"rl_step"}) |
| |
| _HB_TERMINAL_STAGES = frozenset({"done", "already_done"}) |
| _HB_TERMINAL_ONLY = False |
| |
| |
| |
| _HB_TERMINAL_ONLY_INTERVAL_S = 600.0 |
|
|
|
|
| |
| |
| |
| |
| _HB_LOCK = threading.Lock() |
| |
| |
| |
| |
| _HB_UPLOAD_LOCK = threading.Lock() |
|
|
|
|
| def heartbeat(stage: str, **kw): |
| global _HB_LAST_UPLOAD |
| payload = { |
| "stage": stage, |
| "ts": time.time(), |
| "run_id": RUN_ID, |
| "mode": RUN_MODE, |
| "seed": SEED, |
| **kw, |
| } |
| os.makedirs("/tmp/hb", exist_ok=True) |
| p = "/tmp/hb/heartbeat.json" |
| |
| |
| |
| with _HB_LOCK: |
| |
| tmp = p + f".{os.getpid()}.{threading.get_ident()}.tmp" |
| snapshot = json.dumps(payload) |
| with open(tmp, "w") as f: |
| f.write(snapshot) |
| os.replace(tmp, p) |
| now = time.time() |
| if stage in _HB_TERMINAL_STAGES or stage.startswith("error_"): |
| upload_due = True |
| elif _HB_TERMINAL_ONLY: |
| |
| |
| |
| upload_due = ( |
| _HB_LAST_UPLOAD == 0.0 or (now - _HB_LAST_UPLOAD) >= _HB_TERMINAL_ONLY_INTERVAL_S |
| ) |
| else: |
| throttled = stage in _HB_THROTTLED_STAGES |
| upload_due = not throttled or (now - _HB_LAST_UPLOAD) >= _HB_MIN_INTERVAL_S |
| if upload_due: |
| _HB_LAST_UPLOAD = now |
| if upload_due: |
| |
| |
| |
| |
| with _HB_UPLOAD_LOCK: |
| up = p + f".{os.getpid()}.{threading.get_ident()}.upload.tmp" |
| with open(up, "w") as f: |
| f.write(snapshot) |
| try: |
| hf_upload_file(up, "heartbeat.json") |
| finally: |
| with contextlib.suppress(OSError): |
| os.remove(up) |
| print("HEARTBEAT", json.dumps(payload)) |
|
|
|
|
| |
| |
| |
| |
| |
| def render_prompt(tokenizer, item) -> str: |
| item = item if isinstance(item, dict) else {"question": item} |
| msgs = require_active_env().prompt_messages(item) |
| return tokenizer.apply_chat_template( |
| msgs, tokenize=False, add_generation_prompt=True, enable_thinking=THINKING |
| ) |
|
|
|
|
| def strip_think(completion: str | None) -> str | None: |
| """Drop <think>...</think> reasoning before the environment grades/rewards a |
| thinking-mode completion. |
| |
| - closed block(s): keep only the text after the LAST </think>. This also covers |
| always-thinking templates that pre-open <think> inside the generation prompt, |
| whose completions contain </think> with no opening tag. |
| - unclosed <think> (completion budget exhausted): keep only the pre-think text |
| (usually empty), so answer extraction fails and the completion scores 0 — |
| deliberate reward pressure to close thinking within budget, and it keeps a |
| last-number fallback from matching numbers inside the reasoning. |
| - no tags: unchanged. |
| """ |
| if completion is None: |
| return None |
| if "</think>" in completion: |
| return completion.rsplit("</think>", 1)[1] |
| if "<think>" in completion: |
| return completion.split("<think>", 1)[0] |
| return completion |
|
|
|
|
| def graded_text(completion: str | None) -> str | None: |
| """What the env grader/reward sees: thinking runs strip <think> blocks first (a |
| completion whose reasoning never closes grades 0 — see strip_think). Applied once |
| here, before ACTIVE_ENV.grade/reward, so it works for every environment.""" |
| return strip_think(completion) if THINKING else completion |
|
|
|
|
| def _patch_peft_weight_converter_compat() -> None: |
| """peft 0.19.1 x transformers 5.6-5.10: make MoE adapter loading work. |
| |
| peft's ``build_peft_weight_mapping`` reconstructs transformers ``WeightConverter`` |
| objects passing ``distributed_operation=`` / ``quantization_operation=`` — kwargs |
| the WeightConverter in transformers <5.11 doesn't accept (init=False dataclass |
| fields), so loading a LoRA adapter onto any arch WITH weight conversions dies with |
| ``TypeError: unexpected keyword argument 'distributed_operation'`` (observed on a |
| weight-converting checkpoint eval). The |
| worker can't take transformers>=5.11 (vllm 0.19.1 compat), so accept-and-drop |
| unknown kwargs; on a single GPU those fields are unused. No-op once signatures |
| match. |
| """ |
| import inspect |
|
|
| try: |
| from transformers import core_model_loading as cml |
| except Exception: |
| return |
| converter = getattr(cml, "WeightConverter", None) |
| if converter is None or getattr(converter, "_autoslm_compat", False): |
| return |
| accepted = set(inspect.signature(converter.__init__).parameters) |
| if "distributed_operation" in accepted: |
| return |
| orig_init = converter.__init__ |
|
|
| def _compat_init(self, *args, **kwargs): |
| dropped = [k for k in kwargs if k not in accepted] |
| for k in dropped: |
| kwargs.pop(k) |
| orig_init(self, *args, **kwargs) |
|
|
| converter.__init__ = _compat_init |
| converter._autoslm_compat = True |
| print("[compat] WeightConverter patched (peft<->transformers signature drift)") |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| _VL_EXCLUDE_SEGMENTS = ("visual", "vision_tower", "multi_modal_projector", "mtp") |
|
|
|
|
| def lora_exclude_modules(model_id: str) -> str | None: |
| """Regex (peft fullmatch semantics) excluding vision-tower modules from LoRA. |
| |
| Returns None when no exclusion is needed (pure text architectures). NOTE: peft's |
| list-form exclude_modules uses suffix matching (like target_modules), which does |
| NOT match leaf modules under 'visual.*' — a regex string is required. |
| """ |
| excludes = { |
| "qwen3_5": _VL_EXCLUDE_SEGMENTS, |
| "qwen3_5_moe": _VL_EXCLUDE_SEGMENTS, |
| "qwen3_6": _VL_EXCLUDE_SEGMENTS, |
| } |
| try: |
| from transformers import AutoConfig |
|
|
| cfg = AutoConfig.from_pretrained(model_id, trust_remote_code=True) |
| model_type = getattr(cfg, "model_type", "") or "" |
| except Exception as e: |
| print("lora_exclude_modules: config probe failed:", e) |
| return None |
| segments = excludes.get(model_type) |
| if not segments: |
| return None |
| alt = "|".join(segments) |
| return rf"(^|.*\.)({alt})(\..*|$)" |
|
|
|
|
| def is_vl_checkpoint(model_id: str) -> bool: |
| """True for natively-multimodal checkpoints we train/serve text-only (Qwen3.5/3.6).""" |
| return bool(lora_exclude_modules(model_id)) |
|
|
|
|
| def vllm_language_model_only_kwargs(model_id: str) -> dict: |
| """Engine kwargs to skip the vision tower for VL checkpoints (vLLM >= 0.19). |
| |
| Besides wasting VRAM, the vision tower's attention path hardcodes vLLM's bundled |
| flash-attn, whose PTX needs a newer driver JIT than many RTX 5090 hosts have |
| ("PTX compiled with unsupported toolchain") — text-only loading sidesteps it and |
| is the officially supported way to run Qwen3.5 as a pure LLM. |
| """ |
| return {"language_model_only": True} if is_vl_checkpoint(model_id) else {} |
|
|
|
|
| def _attn_impl_for_capability(major: int, minor: int) -> str | None: |
| """Map a CUDA compute capability to the trainer ``attn_implementation``. |
| |
| Attention uses PyTorch SDPA (its flash/efficient backend is already selected automatically |
| on Ampere/Ada/Hopper) — the HF Kernels-Hub FA path is disabled because the torch2.10- |
| compatible ``kernels`` versions break transformers' import. So: |
| sm120 (Blackwell consumer 5090/RTX Pro) -> "sdpa" (forced to the cuDNN backend at train |
| time — its default SDPA can fall to the slow math kernel); all other archs -> None (let |
| transformers pick SDPA, which already flash-backs on Ampere/Ada/Hopper). The big LoRA |
| win comes from the Liger fused kernels, not the attention path. Pure function (no torch) |
| so it's unit-testable on CPU; override the whole thing with AUTOSLM_ATTN_IMPL. |
| """ |
| if major == 12: |
| return "sdpa" |
| return None |
|
|
|
|
| def _flash_attn_available() -> bool: |
| """True when the ``flash_attn`` wheel is importable (our worker image builds it from source). |
| |
| Gates the packing default: TRL's ``packing_strategy='bfd'`` produces flattened/padding-free |
| batches whose example boundaries are carried by ``position_ids`` and enforced ONLY by an |
| attention impl that honors them (FlashAttention-2 varlen / flex_attention). Under plain SDPA, |
| packed examples attend ACROSS boundaries (silent quality loss). find_spec only — no import side |
| effects (and no CUDA init).""" |
| try: |
| import importlib.util |
|
|
| return importlib.util.find_spec("flash_attn") is not None |
| except Exception: |
| return False |
|
|
|
|
| def optimal_attn_impl() -> str | None: |
| """Best ``attn_implementation`` for the live GPU (None = leave transformers' default).""" |
| try: |
| import torch |
|
|
| if not torch.cuda.is_available(): |
| return None |
| major, minor = torch.cuda.get_device_capability(0) |
| except Exception as e: |
| print("optimal_attn_impl probe failed:", e) |
| return None |
| impl = _attn_impl_for_capability(major, minor) |
| if impl: |
| print(f"[attn] sm{major}{minor} -> attn_implementation={impl}") |
| return impl |
|
|
|
|
| |
| |
| |
| |
| |
| |
| _LIGER_MIN_PARAMS = 3e9 |
|
|
|
|
| def _estimate_params(cfg) -> float: |
| """Rough param count from a HF config: embeddings (+untied lm_head) + transformer blocks. |
| For multimodal checkpoints (e.g. Qwen3.5-VL) the LM dims live under ``text_config`` — read it |
| when the top-level dims are absent, else the gate underestimates and wrongly disables the |
| memory path (GC/Liger) for the 4B/9B tiers.""" |
| tc = getattr(cfg, "text_config", None) |
| src = cfg if getattr(cfg, "hidden_size", 0) else (tc or cfg) |
| h = getattr(src, "hidden_size", 0) or 0 |
| v = getattr(src, "vocab_size", 0) or getattr(cfg, "vocab_size", 0) or 0 |
| n = getattr(src, "num_hidden_layers", 0) or 0 |
| tied = getattr(src, "tie_word_embeddings", getattr(cfg, "tie_word_embeddings", False)) |
| emb = v * h * (1 if tied else 2) |
| blocks = n * 12 * h * h |
| return float(emb + blocks) |
|
|
|
|
| def _liger_default_for_model(model_id: str) -> bool: |
| """Default Liger ON only for models large enough that fused-CE's memory win pays off |
| (≥ _LIGER_MIN_PARAMS, ~3B). 1B-class models measured net-negative -> default OFF.""" |
| try: |
| from transformers import AutoConfig |
|
|
| cfg = AutoConfig.from_pretrained(model_id, trust_remote_code=True) |
| return _estimate_params(cfg) >= _LIGER_MIN_PARAMS |
| except Exception as e: |
| print("liger model-size probe failed (default off):", e) |
| return False |
|
|
|
|
| def liger_on(default_on: bool) -> bool: |
| """Whether to enable a Liger kernel path. ``default_on`` is the model-size decision (on only |
| for models large enough that fused-CE's memory win pays off; 1B-class is a measured net loss). |
| Even when on, require a CUDA GPU AND that ``liger_kernel`` is importable — the local |
| ``autoslm-train[gpu]`` extra doesn't ship it, so blindly setting use_liger_kernel would crash a |
| local GPU run. No GPU / absent -> off.""" |
| if not default_on: |
| return False |
| try: |
| import importlib.util |
|
|
| import torch |
|
|
| return bool( |
| torch.cuda.is_available() and importlib.util.find_spec("liger_kernel") is not None |
| ) |
| except Exception: |
| return False |
|
|
|
|
| def setup_perf_backends() -> None: |
| """Universal, arch-agnostic throughput knobs — safe on every CUDA arch, no JIT/compile cost. |
| |
| - TF32 for fp32 matmuls/cuDNN (Ampere+): the residual fp32 ops in a bf16 LoRA run (some |
| norms, the optimizer's fp32 master step, any fp32 GEMM) run on the TF32 tensor cores at |
| ~no accuracy cost. No-op on pre-Ampere. |
| """ |
| try: |
| import torch |
|
|
| if not torch.cuda.is_available(): |
| return |
| torch.set_float32_matmul_precision("high") |
| torch.backends.cuda.matmul.allow_tf32 = True |
| torch.backends.cudnn.allow_tf32 = True |
| print("[perf] TF32 matmul/cuDNN enabled") |
| except Exception as e: |
| print("setup_perf_backends skipped:", e) |
|
|
|
|
| def finalize_alloc_conf_for_sleep() -> None: |
| """Sync the CUDA allocator conf with the worker's RESOLVED vLLM sleep default. |
| |
| The launcher (providers/*/train.py build_worker_env) must pick PYTORCH_ALLOC_CONF before this |
| process starts, but it can't always know the GRPO sleep decision: for a small model with |
| RL_VLLM_SLEEP unset the worker resolves sleep OFF (the speed default), yet the launcher |
| conservatively assumes sleep ON and picks the non-expandable conf (safe, but fragments a long |
| colocate run). When the launcher cedes the decision (it sets AUTOSLM_ALLOC_AUTO=1 — only when |
| it applied a DEFAULT, never an operator override), we resolve the same sleep default here (we |
| have the model config + GPU) and, if sleep is OFF, switch to expandable_segments — which only |
| crashes WITH sleep on, a case we've just ruled out. PYTORCH_ALLOC_CONF is read lazily at the |
| first CUDA allocation, so this must run before any allocation (it does — called at boot).""" |
| if os.environ.get("AUTOSLM_ALLOC_AUTO") != "1": |
| return |
| try: |
| model_id = os.environ.get("BENCH_HF_MODEL", "") |
| |
| |
| _spec_len = 0 |
| try: |
| if JOB_SPEC and JOB_SPEC.train and JOB_SPEC.train.max_length: |
| _spec_len = int(JOB_SPEC.train.max_length) |
| except Exception: |
| _spec_len = 0 |
| ctx = int(_spec_len or 0) |
| if not _memory_mode(model_id, ctx): |
| conf = "expandable_segments:True" |
| os.environ["PYTORCH_ALLOC_CONF"] = conf |
| os.environ["PYTORCH_CUDA_ALLOC_CONF"] = conf |
| print(f"[alloc] sleep resolves OFF -> {conf} (anti-fragmentation, matches worker gate)") |
| else: |
| print("[alloc] sleep resolves ON -> keeping launcher's non-expandable conf") |
| except Exception as e: |
| print("[alloc] auto-conf skipped:", e) |
|
|
|
|
| def _remove_fla_from_disk() -> tuple[list[str], bool]: |
| """Physically delete every importable ``fla`` package dir from the worker's REAL sys.path. |
| |
| Loops until ``find_spec('fla')`` is clean (removing one copy can expose another further down |
| the path) and invalidates import caches so transformers' is_fla_available() probe sees it |
| gone. ``pip uninstall`` alone is unreliable here — it targets one site-packages but the base |
| image bakes ``fla`` into another dir on the path (and can report success while leaving the |
| package dir). Returns ``(removed_dirs, still_importable)``. Used by the Hopper auto-drop. |
| """ |
| import importlib |
| import importlib.util |
| import shutil |
|
|
| removed: list[str] = [] |
| for _ in range(6): |
| importlib.invalidate_caches() |
| spec = importlib.util.find_spec("fla") |
| if spec is None: |
| break |
| |
| locs = list(getattr(spec, "submodule_search_locations", None) or []) |
| if not locs and spec.origin: |
| locs = [os.path.dirname(spec.origin)] |
| progressed = False |
| for loc in locs: |
| if loc and os.path.isdir(loc) and os.path.basename(loc.rstrip("/")) == "fla": |
| try: |
| shutil.rmtree(loc) |
| removed.append(loc) |
| progressed = True |
| except Exception as e: |
| print(f"[fla] could not remove {loc}: {e}", flush=True) |
| if not progressed: |
| break |
| importlib.invalidate_caches() |
| return removed, importlib.util.find_spec("fla") is not None |
|
|
|
|
| |
| |
| |
| _LONG_CONTEXT_TOKENS = 2048 |
|
|
|
|
| def _memory_mode(model_id: str, max_length: int = 0) -> bool: |
| """Whether to default the memory-saving features (Liger, grad-checkpointing, vLLM sleep) ON: |
| a large model (fused-CE memory win) OR a long context (activations/KV dominate). Small model + |
| short context -> off (optimize for speed).""" |
| if max_length and max_length >= _LONG_CONTEXT_TOKENS: |
| return True |
| return _liger_default_for_model(model_id) |
|
|
|
|
| def grad_checkpointing_on(model_id: str, max_length: int = 0) -> bool: |
| """Gradient checkpointing recomputes the forward in backward (~25% slower) to save activation |
| memory — a MEMORY feature, not speed. ON for large models / long context that need the |
| headroom; OFF for small+short runs that fit without it (the speed win).""" |
| return _memory_mode(model_id, max_length) |
|
|
|
|
| def fused_optim_name() -> str: |
| """TRL/HF ``optim`` value: 8-bit paged AdamW (bitsandbytes int8 optimizer state paged to host |
| RAM). It fits a smaller/cheaper GPU and is the better default across the catalog.""" |
| return "paged_adamw_8bit" |
|
|
|
|
| def _reset_peak_gpu() -> None: |
| """Reset the CUDA peak-memory counter so a subsequent ``_peak_gpu_gb`` measures only the work |
| that follows (e.g. the train loop, isolating the optimizer-state A/B from setup/model load).""" |
| try: |
| import torch |
|
|
| if torch.cuda.is_available(): |
| torch.cuda.reset_peak_memory_stats() |
| except Exception: |
| pass |
|
|
|
|
| def _peak_gpu_gb() -> float: |
| """Peak torch-allocated CUDA memory (GB) since the last reset; 0.0 if no CUDA. Note: bnb paged |
| 8-bit optimizer state lives in unified/managed memory outside torch's caching allocator and is |
| NOT counted here — so this OVERSTATES the 8-bit saving. _GpuPeakSampler measures the true |
| device footprint (incl. bnb managed pages) for the honest A/B number.""" |
| try: |
| import torch |
|
|
| if torch.cuda.is_available(): |
| return round(torch.cuda.max_memory_allocated() / 1e9, 3) |
| except Exception: |
| pass |
| return 0.0 |
|
|
|
|
| class _GpuPeakSampler: |
| """Background sampler of true device memory (GB) = (total - free) from cuda.mem_get_info, which |
| DOES include bitsandbytes managed/paged optimizer pages while they're GPU-resident (torch's |
| max_memory_allocated does not). This is the honest peak for the fp32-vs-8-bit optimizer A/B.""" |
|
|
| def __init__(self, interval: float = 0.25): |
| self.interval = interval |
| self.peak_used = 0 |
| self._stop = False |
| self._thread = None |
|
|
| def _run(self): |
| import torch |
|
|
| while not self._stop: |
| try: |
| free, total = torch.cuda.mem_get_info() |
| self.peak_used = max(self.peak_used, total - free) |
| except Exception: |
| pass |
| time.sleep(self.interval) |
|
|
| def start(self): |
| try: |
| import threading |
|
|
| import torch |
|
|
| if not torch.cuda.is_available(): |
| return self |
| self._thread = threading.Thread(target=self._run, daemon=True) |
| self._thread.start() |
| except Exception: |
| pass |
| return self |
|
|
| def stop_gb(self) -> float: |
| self._stop = True |
| if self._thread is not None: |
| self._thread.join(timeout=2) |
| return round(self.peak_used / 1e9, 3) |
|
|
|
|
| |
| |
| |
| _LORAPLUS_8BIT_DEFAULT = True |
|
|
|
|
| def loraplus_optimizer_cls(optim_name: str): |
| """Optimizer class for the LoRA+ ``create_optimizer`` override (returns ``(cls, extra_kwargs)``). |
| |
| The LoRA+ override has to *build* the optimizer itself (PEFT splits the LoRA A/B matrices into |
| separate param groups with different LRs), so it cannot inherit TRL's ``optim=`` string — it has |
| to choose a concrete class. Historically it always built a full-precision ``torch.optim.AdamW``, |
| which silently discarded the catalog's ``paged_adamw_8bit`` setting whenever LoRA+ was on. |
| |
| PEFT's ``create_loraplus_optimizer`` accepts ANY ``optimizer_cls`` — including bitsandbytes 8-bit |
| optimizers (it registers embedding overrides with bnb's ``GlobalOptimManager`` to keep them |
| 32-bit) — so LoRA+ and the 8-bit paged optimizer state CAN coexist. |
| |
| Whether the 8-bit combination is the DEFAULT is gated by ``_LORAPLUS_8BIT_DEFAULT`` (set from the |
| on-GPU A/B, see that constant). When enabled, an ``8bit`` ``optim`` value selects |
| ``bnb.optim.PagedAdamW8bit``; otherwise (or for a non-8-bit ``optim``) LoRA+ keeps fp32 AdamW. |
| Either way ``AUTOSLM_LORAPLUS_8BIT`` forces the choice (1 -> 8-bit, 0 -> fp32), so the |
| combination is always available on demand. Falls back to fp32 AdamW if bitsandbytes is missing.""" |
| import torch as _torch |
|
|
| want_8bit = _LORAPLUS_8BIT_DEFAULT and "8bit" in (optim_name or "") |
| override = os.environ.get("AUTOSLM_LORAPLUS_8BIT") |
| if override is not None: |
| want_8bit = override.strip().lower() in ("1", "true", "yes", "on") |
| if want_8bit: |
| try: |
| import bitsandbytes as bnb |
|
|
| return bnb.optim.PagedAdamW8bit, {} |
| except Exception as e: |
| print(f"[lora+] bitsandbytes 8-bit optimizer unavailable ({e}); using fp32 AdamW") |
| return _torch.optim.AdamW, {} |
|
|
|
|
| def wandb_report_to() -> list[str]: |
| """TRL/HF ``report_to`` targets. Restores the W&B logging the legacy freesolo training path had |
| but the autoslm migration dropped: report to W&B whenever WANDB_API_KEY is present. No key -> [] |
| (silent, the metrics.json artifact is still the source of truth). Sets a default project so runs |
| land in one place.""" |
| if not os.environ.get("WANDB_API_KEY"): |
| return [] |
| import importlib.util |
|
|
| if importlib.util.find_spec("wandb") is None: |
| print("[wandb] WANDB_API_KEY set but the wandb package is missing; skipping W&B logging") |
| return [] |
| os.environ.setdefault("WANDB_PROJECT", "autoslm") |
| return ["wandb"] |
|
|
|
|
| def wandb_run_name() -> str: |
| """Stable, human-readable W&B run name tying the dashboard run to the AutoSLM run id.""" |
| return f"autoslm-{PHASE}-{RUN_ID}-seed{SEED}" |
|
|
|
|
| def wandb_run_info() -> dict: |
| """The live W&B run's {url, id, project} if W&B is active, else {}. Recorded in metrics.json so |
| the W&B run is verifiable + the freesolo agent's `wandb_runs` / the SDK's link_wandb can point at |
| the real dashboard URL — the link the autoslm migration otherwise dropped. Never raises.""" |
| try: |
| import wandb |
|
|
| run = getattr(wandb, "run", None) |
| if run is None: |
| return {} |
| return { |
| "wandb_url": getattr(run, "url", None), |
| "wandb_id": getattr(run, "id", None), |
| "wandb_project": getattr(run, "project", None), |
| } |
| except Exception: |
| return {} |
|
|
|
|
| def _sdpa_cudnn_ctx(attn_impl: str | None): |
| """Context forcing the cuDNN SDPA backend (real Blackwell-consumer kernels) when we fell |
| back to plain SDPA on sm120; a no-op context otherwise. Best-effort.""" |
| if attn_impl != "sdpa": |
| return contextlib.nullcontext() |
| try: |
| from torch.nn.attention import SDPBackend, sdpa_kernel |
|
|
| |
| |
| |
| |
| |
| |
| return sdpa_kernel( |
| [ |
| SDPBackend.CUDNN_ATTENTION, |
| SDPBackend.FLASH_ATTENTION, |
| SDPBackend.EFFICIENT_ATTENTION, |
| SDPBackend.MATH, |
| ], |
| set_priority=True, |
| ) |
| except Exception as e: |
| print("[attn] cuDNN SDPA backend unavailable, using default SDPA:", e) |
| return contextlib.nullcontext() |
|
|
|
|
| def patch_vllm_language_model_only(model_id: str) -> bool: |
| """Force ``language_model_only=True`` on vLLM engines created by third-party code |
| (TRL's colocated GRPO rollout engine) for VL checkpoints. Returns True if patched.""" |
| extra = vllm_language_model_only_kwargs(model_id) |
| if not extra: |
| return False |
| try: |
| import vllm |
|
|
| if getattr(vllm.LLM.__init__, "_autoslm_lmo_patched", False): |
| return True |
| orig = vllm.LLM.__init__ |
|
|
| def patched(self, *args, **kwargs): |
| kwargs.setdefault("language_model_only", True) |
| return orig(self, *args, **kwargs) |
|
|
| patched._autoslm_lmo_patched = True |
| vllm.LLM.__init__ = patched |
| print(f"[vllm] language_model_only patch active for {model_id}") |
| return True |
| except Exception as e: |
| print("patch_vllm_language_model_only warn:", e) |
| return False |
|
|
|
|
| def make_lora(model_id: str | None = None): |
| """LoRA config. We target 'all-linear' (every nn.Linear) rather than a hardcoded |
| q/k/v/o list: it is architecture-agnostic, so the same recipe works for the dense |
| default (Qwen3-4B-Instruct-2507) and for newer models with extra projection |
| types (e.g. the Qwen3.5 hybrid Gated-DeltaNet) without missing any adapters. |
| For natively-multimodal checkpoints the vision tower is excluded (see |
| ``lora_exclude_modules``).""" |
| from peft import LoraConfig |
|
|
| |
| |
| |
| |
| _parts = [ |
| t.strip() for t in os.environ.get("LORA_TARGETS", "all-linear").split(",") if t.strip() |
| ] |
| |
| |
| |
| targets = "all-linear" if (not _parts or _parts == ["all-linear"]) else _parts |
| rank = JOB_SPEC.train.lora_rank if JOB_SPEC else RECIPE.lora.rank |
| alpha = JOB_SPEC.train.lora_alpha if JOB_SPEC else RECIPE.lora.alpha |
| kwargs = { |
| "r": rank, |
| "lora_alpha": alpha, |
| "lora_dropout": RECIPE.lora.dropout, |
| "target_modules": targets, |
| "task_type": "CAUSAL_LM", |
| } |
| |
| |
| |
| |
| |
| kwargs["init_lora_weights"] = "pissa_niter_16" |
| |
| kwargs["use_rslora"] = True |
| print("[lora] init_lora_weights=pissa_niter_16, rsLoRA scaling enabled") |
| if model_id and targets == "all-linear": |
| exclude = lora_exclude_modules(model_id) |
| if exclude: |
| kwargs["exclude_modules"] = exclude |
| print(f"[lora] excluding modules for {model_id}: {exclude}") |
| return LoraConfig(**kwargs) |
|
|
|
|
| def model_quant(model_id: str) -> str: |
| """Quantization tier for this model: catalog entry > AUTOSLM_QUANT env > bf16.""" |
| env_q = os.environ.get("AUTOSLM_QUANT") |
| if env_q: |
| return env_q |
| try: |
| from autoslm.catalog import MODELS |
|
|
| info = MODELS.get(model_id) |
| if info is not None: |
| return info.quant |
| except Exception as e: |
| print("model_quant: catalog probe failed:", e) |
| return "bf16" |
|
|
|
|
| def qlora_model_init_kwargs() -> dict: |
| """Model-load kwargs for the 4-bit QLoRA tier: bf16 compute + a bitsandbytes NF4 |
| (double-quant) config so the frozen base loads in 4-bit and only the LoRA adapter trains.""" |
| import torch |
| from transformers import BitsAndBytesConfig |
|
|
| return { |
| "dtype": torch.bfloat16, |
| "quantization_config": BitsAndBytesConfig( |
| load_in_4bit=True, |
| bnb_4bit_compute_dtype=torch.bfloat16, |
| bnb_4bit_quant_type="nf4", |
| bnb_4bit_use_double_quant=True, |
| ), |
| } |
|
|
|
|
| def require_vllm_for_rollout_func(use_rollout_func: bool, use_vllm: bool, model_id: str) -> None: |
| """Fail fast when a multi-turn GRPO run needs colocated vLLM but it's disabled. |
| |
| The multi-turn rollout closure (``multiturn_rollout.build_rollout_func``) drives generation |
| through ``trainer.vllm_generation.llm``. TRL only creates that engine when ``use_vllm`` is |
| True, so with vLLM disabled (catalog ``grpo_use_vllm=False`` or ``RL_USE_VLLM=0``) the rollout |
| would AttributeError at the first turn. Reject the combination up front with an actionable |
| message instead of crashing deep in training. |
| """ |
| if use_rollout_func and not use_vllm: |
| raise RuntimeError( |
| f"multi-turn GRPO needs colocated vLLM, which is disabled for {model_id} " |
| "(grpo_use_vllm=False / RL_USE_VLLM=0). Use a single-turn environment for this " |
| "model, or a model tier that keeps vLLM enabled for rollouts." |
| ) |
|
|
|
|
| def run_sft(): |
| from datasets import Dataset |
| from transformers import AutoTokenizer |
| from trl import SFTConfig as TRLSFTConfig |
| from trl import SFTTrainer |
|
|
| require_active_env() |
| t_start = time.time() |
| heartbeat("sft_start") |
| |
| |
| |
| if getattr(ACTIVE_ENV, "multi_turn", False): |
| print( |
| "[sft][warn] this is a multi-turn / tool verifiers environment, but SFT only fits " |
| "the single assistant target per row (tool/env turns are ignored). The model will be " |
| "trained on collapsed single-turn targets; multi-turn SFT is not supported. Use a " |
| "single-turn environment, or expect a single-turn-only fit." |
| ) |
| wait_for_gpu() |
| setup_perf_backends() |
| model_id = JOB_SPEC.model if JOB_SPEC else RECIPE.hf_model_id |
| download_seconds = prefetch_model(model_id) |
| tok = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True) |
| if tok.pad_token is None: |
| tok.pad_token = tok.eos_token |
|
|
| |
| train = ACTIVE_ENV.dataset("train") |
| rng = random.Random(SEED) |
| rng.shuffle(train) |
| max_examples = int( |
| JOB_SPEC.train.max_examples or 0 |
| if JOB_SPEC and JOB_SPEC.train and JOB_SPEC.train.max_examples is not None |
| else 0 |
| ) |
| if max_examples > 0: |
| train = train[:max_examples] |
| texts = [] |
| for ex in train: |
| msgs = [ |
| *ACTIVE_ENV.prompt_messages(ex), |
| {"role": "assistant", "content": ACTIVE_ENV.sft_target(ex)}, |
| ] |
| texts.append( |
| { |
| "text": tok.apply_chat_template( |
| msgs, tokenize=False, add_generation_prompt=False, enable_thinking=THINKING |
| ) |
| } |
| ) |
| if THINKING and not any("<think>" in t["text"] for t in texts[:256]): |
| print( |
| "WARN: thinking mode is ON but no sampled SFT target contains a <think> " |
| "trace — training on non-reasoning targets teaches the model to SKIP " |
| "thinking. Use a dataset with reasoning traces, or set thinking = false." |
| ) |
| ds = Dataset.from_list(texts) |
|
|
| setup_seconds = time.time() - t_start |
| heartbeat("sft_model_load", setup_seconds=setup_seconds) |
|
|
| default_epochs = ( |
| JOB_SPEC.train.epochs |
| if JOB_SPEC and JOB_SPEC.train.epochs is not None |
| else RECIPE.sft.num_epochs |
| ) |
| epochs = int(os.environ.get("SFT_EPOCHS", str(default_epochs))) |
| |
| _t = JOB_SPEC.train if JOB_SPEC else None |
| per_device_bs = int(os.environ.get("SFT_PER_DEVICE_BS", "4")) |
| |
| |
| |
| |
| effective_batch = ( |
| _t.batch_size if _t and _t.batch_size is not None else RECIPE.sft.effective_batch |
| ) |
| per_device_bs = max(1, min(per_device_bs, effective_batch)) |
| grad_accum = max(1, -(-effective_batch // per_device_bs)) |
| sft_lr = _t.learning_rate if _t and _t.learning_rate is not None else RECIPE.sft.learning_rate |
| sft_max_len = ( |
| _t.max_length |
| if _t and _t.max_length is not None |
| else (RECIPE.sft.max_seq_len_thinking if THINKING else RECIPE.sft.max_seq_len) |
| ) |
| sft_save_default = _t.save_every if _t and _t.save_every is not None else 50 |
| out_dir = f"/tmp/sft_seed{SEED}" |
| resume_ckpt = hf_resume_checkpoint() |
|
|
| |
| max_steps = int(_t.max_steps or 0 if _t and _t.max_steps is not None else 0) |
| cfg_kwargs = { |
| "output_dir": out_dir, |
| "num_train_epochs": epochs, |
| "per_device_train_batch_size": per_device_bs, |
| "gradient_accumulation_steps": grad_accum, |
| "learning_rate": sft_lr, |
| "warmup_ratio": RECIPE.sft.warmup_frac, |
| "logging_steps": 10, |
| "save_steps": sft_save_default, |
| "save_total_limit": 1, |
| |
| |
| |
| "save_only_model": True, |
| "max_length": sft_max_len, |
| "bf16": True, |
| "report_to": wandb_report_to(), |
| "run_name": wandb_run_name(), |
| |
| |
| |
| |
| "dataloader_num_workers": 4, |
| "dataloader_pin_memory": True, |
| "dataloader_persistent_workers": True, |
| "seed": SEED, |
| "gradient_checkpointing": grad_checkpointing_on(model_id, sft_max_len), |
| |
| "gradient_checkpointing_kwargs": {"use_reentrant": False}, |
| "completion_only_loss": False, |
| |
| "optim": fused_optim_name(), |
| } |
| if max_steps > 0: |
| cfg_kwargs["max_steps"] = max_steps |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| _packing_env = os.environ.get("SFT_PACKING") |
| _want_packing = (_packing_env or "1") not in ("0", "false", "False") |
| _packing_forced = _packing_env not in (None, "") |
| _fa_ok = _flash_attn_available() |
| if _want_packing and _fa_ok: |
| cfg_kwargs["packing"] = True |
| print("[sft] example packing enabled (FA2 varlen; SFT_PACKING=0 to disable)") |
| elif _want_packing and _packing_forced: |
| cfg_kwargs["packing"] = True |
| print( |
| "[sft] WARNING: packing forced without FA2 — 'bfd' boundaries need varlen; " |
| "examples may cross-contaminate under SDPA. Set SFT_PACKING=0 to disable." |
| ) |
| elif _want_packing: |
| print( |
| "[sft] packing SKIPPED: no boundary-correct attn backend (flash-attn absent on torch " |
| "2.10). Bake flash-attn into the worker image to enable FA2 varlen packing." |
| ) |
| |
| |
| if liger_on(_memory_mode(model_id, sft_max_len)): |
| cfg_kwargs["use_liger_kernel"] = True |
| print("[sft] liger fused kernels enabled") |
| _attn = optimal_attn_impl() |
| |
| |
| |
| if cfg_kwargs.get("packing") and _fa_ok: |
| _attn = "flash_attention_2" |
| print("[sft] attn_implementation=flash_attention_2 (packing boundary-correct varlen)") |
| quant = model_quant(model_id) |
| if quant == "4bit-qlora": |
| |
| _patch_peft_weight_converter_compat() |
| mik = qlora_model_init_kwargs() |
| print(f"[sft] loading {model_id} in 4-bit (QLoRA tier)") |
| else: |
| |
| |
| |
| |
| mik = {"dtype": "bfloat16", "device_map": None} |
| if _attn: |
| mik["attn_implementation"] = _attn |
| cfg_kwargs["model_init_kwargs"] = mik |
| cfg = TRLSFTConfig(**cfg_kwargs) |
|
|
| |
| |
| |
| |
| |
| _lp_ratio = 16 |
| _SFT = SFTTrainer |
| if _lp_ratio > 1: |
|
|
| class _SFT(SFTTrainer): |
| def create_optimizer(self): |
| if self.optimizer is None: |
| try: |
| from peft.optimizers import create_loraplus_optimizer |
|
|
| |
| |
| opt_cls, extra = loraplus_optimizer_cls(str(self.args.optim)) |
| |
| |
| |
| try: |
| self.optimizer = create_loraplus_optimizer( |
| model=self.model, |
| optimizer_cls=opt_cls, |
| optimizer_kwargs={"lr": self.args.learning_rate, **extra}, |
| loraplus_lr_ratio=_lp_ratio, |
| ) |
| except TypeError: |
| self.optimizer = create_loraplus_optimizer( |
| model=self.model, |
| optimizer_cls=opt_cls, |
| lr=self.args.learning_rate, |
| loraplus_lr_ratio=_lp_ratio, |
| **extra, |
| ) |
| print( |
| f"[lora+] optimizer enabled (B-matrix LR ratio={_lp_ratio}, " |
| f"cls={opt_cls.__name__})" |
| ) |
| return self.optimizer |
| except Exception as e: |
| print("[lora+] setup failed, falling back to default optimizer:", e) |
| return super().create_optimizer() |
|
|
| |
| |
| trainer = _SFT( |
| model=model_id, |
| args=cfg, |
| train_dataset=ds, |
| peft_config=make_lora(model_id), |
| processing_class=tok, |
| callbacks=[make_checkpoint_upload_callback()], |
| ) |
|
|
| _reset_peak_gpu() |
| _gpu_sampler = _GpuPeakSampler().start() |
| t_train = time.time() |
| with _sdpa_cudnn_ctx(_attn): |
| trainer.train(resume_from_checkpoint=resume_ckpt) |
| train_wall = time.time() - t_train |
| sft_peak_gpu_gb = _peak_gpu_gb() |
| sft_device_peak_gpu_gb = _gpu_sampler.stop_gb() |
|
|
| adapter_dir = f"{out_dir}/adapter" |
| trainer.model.save_pretrained(adapter_dir) |
| tok.save_pretrained(adapter_dir) |
| hf_upload_folder(adapter_dir, "adapter", required=True) |
| heartbeat("sft_trained", train_wall=train_wall) |
|
|
| |
| train_tokens = int(sum(len(tok(t["text"])["input_ids"]) for t in texts) * epochs) |
|
|
| |
| write_train_meta( |
| phase="sft", |
| adapter_dir=adapter_dir, |
| model_id=model_id, |
| train_wall=train_wall, |
| setup_seconds=setup_seconds, |
| train_tokens=train_tokens, |
| generated_tokens=0, |
| notes={ |
| "epochs": epochs, |
| "resumed": bool(resume_ckpt), |
| "download_seconds": download_seconds, |
| "hf_transfer": os.environ.get("HF_HUB_ENABLE_HF_TRANSFER", ""), |
| "thinking": THINKING, |
| |
| |
| |
| |
| "loss_curve": _metric_curve(trainer, "loss"), |
| |
| |
| "peak_gpu_gb": sft_peak_gpu_gb, |
| |
| |
| "device_peak_gpu_gb": sft_device_peak_gpu_gb, |
| "loraplus_optim": loraplus_optimizer_cls(fused_optim_name())[0].__name__, |
| **wandb_run_info(), |
| }, |
| ) |
| free_gpu(trainer) |
|
|
|
|
| |
| |
| |
| def compute_grpo_batching(prompts_per_step: int, group_size: int, per_device_comps: int) -> dict: |
| """Translate an intended ``prompts_per_step`` into a TRL GRPO batch configuration. |
| |
| TRL's GRPO batch sizing is denominated in **completions (prompt-completion pairs), not |
| prompts**. The number of *unique prompts* optimized per step is |
| |
| (per_device_train_batch_size * gradient_accumulation_steps * num_processes) |
| / num_generations |
| |
| So to actually optimize ``prompts_per_step`` prompts per step, the global *completion* |
| batch must equal ``prompts_per_step * group_size``. We keep ``per_device`` small (it, |
| not grad-accum, sets peak VRAM) and put the rest in gradient accumulation. |
| |
| The bug this fixes: ``grad_accum = prompts_per_step // per_device`` treated |
| ``per_device_train_batch_size`` as a *prompt* count, omitting the ``* group_size`` |
| factor, so a run intended as 64 prompts/step actually optimized only |
| ``64 / group_size = 8`` prompts/step (an 8x smaller effective batch). |
| """ |
| import math |
|
|
| group_size = max(1, int(group_size)) |
| prompts_per_step = max(1, int(prompts_per_step)) |
| per_device = max(1, int(per_device_comps)) |
| target_comps = prompts_per_step * group_size |
| |
| |
| |
| per_device = max(1, min(per_device, target_comps)) |
| grad_accum = max(1, target_comps // per_device) |
| |
| |
| |
| |
| |
| |
| accum_step = group_size // math.gcd(per_device, group_size) |
| grad_accum = ((grad_accum + accum_step - 1) // accum_step) * accum_step |
| generations_per_step = per_device * grad_accum |
| unique_prompts_per_step = generations_per_step // group_size |
| return { |
| "per_device_train_batch_size": per_device, |
| "gradient_accumulation_steps": grad_accum, |
| "generations_per_step": generations_per_step, |
| "unique_prompts_per_step": unique_prompts_per_step, |
| |
| "divisible_by_group": (generations_per_step % group_size == 0), |
| } |
|
|
|
|
| def rl_per_device_comps( |
| completion_len: int = 0, |
| vocab: int = 152_000, |
| *, |
| use_vllm: bool = True, |
| params_b: float | None = None, |
| ) -> int: |
| """Per-device *completion* micro-batch for GRPO (TRL counts completions, not prompts). |
| |
| This, not grad-accum, sets peak trainer VRAM: the logprob pass materializes fp32 logits |
| of shape [per_device, completion_len, vocab]. At Qwen's ~152k vocab a long completion is |
| enormous (measured: per_device 8 x 4096 tok x 152k x 4 B = ~20 GiB single alloc -> OOMs |
| a small card). So we MEMORY-CAP per_device to a logits budget (RL_LOGITS_BUDGET_GB, |
| default 6) for the given completion length, then push the difference into grad-accum |
| (compute_grpo_batching) so the effective batch is unchanged. This keeps long-completion |
| GRPO on a cheaper GPU. RL_PER_DEVICE_PROMPTS forces an explicit value. |
| |
| The logits budget is NOT the whole story: the per-device forward also holds the model's |
| attention/activation memory (the Qwen3.5 GDN/FLA kernels peak per micro-batch even with |
| grad checkpointing), which the logits term can't see. Under colocated vLLM (the rollout |
| engine + its card-sized KV pool + a 2nd weight copy share the GPU) that activation peak is |
| what OOMs a small card -- and Liger, which fuses away the logits, does NOT touch it. |
| MEASURED: Qwen3.5-2B (width ~1.41) group8 seq2048 OOMs a 32 GB card at per_device=8 but |
| TRAINS at 4. So for colocate, additionally cap per_device to the live card's VRAM scaled |
| by model width (~sqrt(params)): ~vram_gb/8 at 2B-width, tightened for wider models (4B/9B). |
| """ |
| base = int(os.environ.get("RL_PER_DEVICE_PROMPTS", "2" if THINKING else "8")) |
| if "RL_PER_DEVICE_PROMPTS" in os.environ: |
| |
| return max(1, base) |
| if completion_len > 0: |
| budget = float(os.environ.get("RL_LOGITS_BUDGET_GB", "6")) * 1e9 |
| cap = max(1, int(budget / (max(1, completion_len) * vocab * 4))) |
| base = min(base, cap) |
| if use_vllm: |
| try: |
| import torch |
|
|
| if torch.cuda.is_available(): |
| vram_gb = torch.cuda.get_device_properties(0).total_memory / (1024**3) |
| width = (max(float(params_b), 0.1) ** 0.5) if params_b else 1.41 |
| act_cap = max(1, int(vram_gb / (7.5 * (width / 1.41)))) |
| base = min(base, act_cap) |
| except Exception as e: |
| print("rl_per_device_comps colocate cap probe failed (keeping logits cap):", e) |
| return max(1, base) |
|
|
|
|
| def make_reward_heartbeat_callback(): |
| """A TRL/transformers callback that streams the per-step mean reward to the HF heartbeat |
| channel, giving the worker a live RL signal (no pod log API) and recording a |
| ``reward_history``. Built lazily so the module imports without transformers installed.""" |
| from transformers import TrainerCallback |
|
|
| class _RewardHeartbeat(TrainerCallback): |
| def __init__(self): |
| self.reward_history = [] |
|
|
| def on_log(self, args, state, control, logs=None, **kwargs): |
| if not logs: |
| return |
| r = logs.get("reward") |
| if r is None: |
| return |
| try: |
| r = float(r) |
| except (TypeError, ValueError): |
| return |
| self.reward_history.append(r) |
| step = int(getattr(state, "global_step", len(self.reward_history))) |
| heartbeat("rl_step", step=step, reward=r, reward_last=self.reward_history[-8:]) |
|
|
| return _RewardHeartbeat() |
|
|
|
|
| def grpo_overrides() -> dict: |
| """The GRPO recipe knobs, read off the job spec's ``[train]`` table (``TrainSpec``). |
| A field left unset (None) is omitted here so the recipe default applies downstream. |
| |
| Knobs: group_size, temperature, max_tokens (completion budget), kl_penalty_coef (the KL |
| beta), advantage_clip (centered-advantage clip), and thinking_length_penalty_coef |
| (a per-<think>-token reward deduction). These live in ``[train]`` — NOT in |
| ``[environment.params]``, which is forwarded verbatim to the verifiers env loader.""" |
| if not JOB_SPEC: |
| return {} |
| train = JOB_SPEC.train |
| cfg = { |
| "group_size": train.group_size, |
| "temperature": train.temperature, |
| "max_tokens": train.max_tokens, |
| "kl_penalty_coef": train.kl_penalty_coef, |
| "advantage_clip": train.advantage_clip, |
| "thinking_length_penalty_coef": train.thinking_length_penalty_coef, |
| } |
| return {k: v for k, v in cfg.items() if v is not None} |
|
|
|
|
| def think_token_count(completion: str | None, tokenizer) -> int: |
| """Number of tokens inside the completion's <think>...</think> span (0 if none). |
| |
| Used for the thinking-length reward deduction: long reasoning is penalized in |
| proportion to the tokens it spent, mirroring the SDK's thinking_length_penalty_coef. |
| """ |
| if not completion or "<think>" not in completion: |
| return 0 |
| after = completion.split("<think>", 1)[1] |
| think_text = after.split("</think>", 1)[0] if "</think>" in after else after |
| if not think_text: |
| return 0 |
| return len(tokenizer(think_text, add_special_tokens=False)["input_ids"]) |
|
|
|
|
| def _init_adapter_model(model_id: str): |
| """Base model + the ``train.init_from_adapter`` adapter loaded as a trainable |
| PeftModel, or the plain ``model_id`` string + a fresh LoRA when it is unset. |
| |
| GRPO continuing an SFT adapter: TRL trains the LOADED adapter (peft_config=None) |
| instead of attaching a fresh one.""" |
| prefix = JOB_SPEC.train.init_from_adapter if JOB_SPEC else "" |
| if not prefix: |
| return model_id, make_lora(model_id) |
| adir = _download_adapter(prefix) |
| if not adir: |
| |
| |
| |
| raise RuntimeError( |
| f"train.init_from_adapter={prefix!r} could not be downloaded from the artifact " |
| "store (wrong/missing prefix or no access); refusing to silently start GRPO from " |
| "the base model. Fix the adapter prefix / HF credentials, or omit " |
| "init_from_adapter to train a fresh LoRA." |
| ) |
| from peft import PeftModel |
| from transformers import AutoModelForCausalLM |
|
|
| print(f"[init-adapter] initializing LoRA from {prefix}") |
| |
| |
| |
| if model_quant(model_id) == "4bit-qlora": |
| _patch_peft_weight_converter_compat() |
| _attn = optimal_attn_impl() |
| _mik = qlora_model_init_kwargs() |
| if _attn: |
| _mik["attn_implementation"] = _attn |
| base = AutoModelForCausalLM.from_pretrained( |
| model_id, |
| trust_remote_code=True, |
| **_mik, |
| ) |
| else: |
| _attn = optimal_attn_impl() |
| base = AutoModelForCausalLM.from_pretrained( |
| model_id, |
| dtype="bfloat16", |
| trust_remote_code=True, |
| **({"attn_implementation": _attn} if _attn else {}), |
| ) |
| model = PeftModel.from_pretrained(base, adir, is_trainable=True) |
| return model, None |
|
|
|
|
| def run_rl(): |
| from datasets import Dataset |
| from transformers import AutoTokenizer |
| from trl import GRPOConfig, GRPOTrainer |
|
|
| require_active_env() |
| t_start = time.time() |
| heartbeat("rl_start") |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| is_tool_env = getattr(ACTIVE_ENV, "is_tool_env", False) |
| is_multi_turn = getattr(ACTIVE_ENV, "multi_turn", False) |
| conversational = is_multi_turn |
| wait_for_gpu() |
| setup_perf_backends() |
| model_id = JOB_SPEC.model if JOB_SPEC else RECIPE.hf_model_id |
| |
| quant = model_quant(model_id) |
| download_seconds = prefetch_model(model_id) |
| rl = RECIPE.rl |
| steps = int(os.environ.get("RL_STEPS", str(rl.num_steps))) |
| |
| |
| |
| |
| |
| gcfg = grpo_overrides() |
| _t = JOB_SPEC.train if JOB_SPEC else None |
| |
| |
| prompts_per_step = int(_t.batch_size if _t and _t.batch_size is not None else rl.prompts_per_step) |
| group_size = int(gcfg.get("group_size") or rl.group_size) |
| |
| |
| _gcfg_temp = gcfg.get("temperature") |
| _temperature = float(_gcfg_temp if _gcfg_temp is not None else rl.sampling_temperature) |
| _kl_beta = float(gcfg.get("kl_penalty_coef") or 0.0) |
| _adv_clip = float(gcfg.get("advantage_clip") or 0.0) |
| _think_penalty = float(gcfg.get("thinking_length_penalty_coef") or 0.0) |
| |
| |
| |
| |
| _sleep_env = os.environ.get("RL_VLLM_SLEEP") |
| if _sleep_env is not None: |
| sleep_mode = _sleep_env not in ("0", "false", "False") |
| else: |
| |
| |
| |
| _grpo_ctx = int(_t.max_length if _t and _t.max_length else 0) |
| sleep_mode = _memory_mode(model_id, _grpo_ctx) |
| |
| |
| use_vllm = True |
| print("[rl] rollout backend: colocated vLLM") |
| from autoslm.catalog import MODELS as _CATALOG |
|
|
| _info = _CATALOG.get(model_id) |
| tok = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True) |
| if tok.pad_token is None: |
| tok.pad_token = tok.eos_token |
|
|
| train = ACTIVE_ENV.dataset("train") |
| rng = random.Random(SEED) |
| rng.shuffle(train) |
| if conversational: |
| |
| |
| prompts = [{"prompt": ACTIVE_ENV.prompt_messages(ex), "example": ex} for ex in train] |
| else: |
| prompts = [{"prompt": render_prompt(tok, ex), "example": ex} for ex in train] |
| |
| |
| |
| |
| _max_completion = int( |
| gcfg.get("max_tokens") |
| or (rl.max_completion_len_thinking if THINKING else rl.max_completion_len) |
| ) |
| |
| |
| |
| |
| _train_ctx = _t.max_length if (_t and _t.max_length) else 0 |
| vllm_max_len = int(_train_ctx or max(1024, rl.max_prompt_len + _max_completion)) |
| |
| |
| |
| if vllm_max_len <= _max_completion: |
| raise ValueError( |
| f"engine length {vllm_max_len} leaves no room for the {_max_completion}-token " |
| "completion; raise [train].max_length or lower [train].max_tokens" |
| ) |
| prompt_budget = vllm_max_len - _max_completion |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| _oai_tools = ( |
| getattr(getattr(ACTIVE_ENV, "_env", None), "oai_tools", None) if is_tool_env else None |
| ) |
|
|
| def _prompt_tokens(p) -> int: |
| if conversational: |
| |
| |
| kw = {"tools": _oai_tools} if _oai_tools else {} |
| try: |
| text = tok.apply_chat_template( |
| p["prompt"], |
| add_generation_prompt=True, |
| tokenize=False, |
| enable_thinking=THINKING, |
| **kw, |
| ) |
| except Exception as exc: |
| |
| |
| |
| |
| raise RuntimeError( |
| "failed to render a conversational prompt with this model's chat template " |
| f"(fix the model/template or the env's prompts): {exc}" |
| ) from exc |
| return len(tok(text, add_special_tokens=False).input_ids) |
| return len(tok(p["prompt"], add_special_tokens=False).input_ids) |
|
|
| kept = [p for p in prompts if 0 < _prompt_tokens(p) <= prompt_budget] |
| if len(kept) < len(prompts): |
| print( |
| f"[rl] dropped {len(prompts) - len(kept)} prompts over the {prompt_budget}-token " |
| f"prompt budget (engine {vllm_max_len} - completion {_max_completion})" |
| ) |
| if not kept: |
| raise ValueError( |
| f"every training prompt exceeds the {prompt_budget}-token prompt budget (engine " |
| f"{vllm_max_len} - completion {_max_completion}); raise [train].max_length, lower " |
| "[train].max_tokens, or shorten the environment's prompts" |
| ) |
| prompts = kept |
| ds = Dataset.from_list(prompts) |
|
|
| def reward_fn(completions, **kwargs): |
| |
| |
| if kwargs.get("reward") is not None: |
| return [float(r) for r in kwargs["reward"]] |
| |
| |
| examples = kwargs.get("example") |
| rewards = [] |
| for comp, ex in zip(completions, examples, strict=False): |
| if isinstance(comp, list): |
| |
| |
| rewards.append(ACTIVE_ENV.reward_from_messages(comp, ex)) |
| continue |
| r = ACTIVE_ENV.reward(graded_text(comp), ex) |
| if _think_penalty > 0 and THINKING: |
| r -= _think_penalty * think_token_count(comp, tok) |
| rewards.append(r) |
| return rewards |
|
|
| |
| |
| |
| |
| from autoslm.engine.vram import fetch_hf_params_b, params_b_from_str |
|
|
| _params_b = params_b_from_str(getattr(_info, "params", None)) if _info else None |
| |
| |
| |
| |
| if _params_b is None: |
| _params_b = fetch_hf_params_b(model_id) |
| per_device_comps = rl_per_device_comps(_max_completion, use_vllm=use_vllm, params_b=_params_b) |
| batching = compute_grpo_batching(prompts_per_step, group_size, per_device_comps) |
| if not batching["divisible_by_group"]: |
| print("WARN: generation batch not divisible by group size; check RL_PER_DEVICE_PROMPTS") |
| print( |
| f"[rl] GRPO batching: per_device={batching['per_device_train_batch_size']} " |
| f"grad_accum={batching['gradient_accumulation_steps']} " |
| f"generations/step={batching['generations_per_step']} " |
| f"unique_prompts/step={batching['unique_prompts_per_step']} " |
| f"(target prompts/step={prompts_per_step}, group={group_size}, sleep={sleep_mode})" |
| ) |
| out_dir = f"/tmp/rl_seed{SEED}" |
| resume_ckpt = hf_resume_checkpoint() |
|
|
| grpo_kwargs = { |
| "output_dir": out_dir, |
| "learning_rate": ( |
| _t.learning_rate if _t and _t.learning_rate is not None else rl.learning_rate |
| ), |
| "per_device_train_batch_size": batching["per_device_train_batch_size"], |
| "gradient_accumulation_steps": batching["gradient_accumulation_steps"], |
| "num_generations": group_size, |
| |
| |
| |
| "max_completion_length": _max_completion, |
| "max_steps": steps, |
| "temperature": _temperature, |
| "top_p": rl.sampling_top_p, |
| "use_vllm": use_vllm, |
| "logging_steps": 1, |
| "save_steps": _t.save_every if _t and _t.save_every is not None else 20, |
| "save_total_limit": 1, |
| |
| |
| "save_only_model": True, |
| "bf16": True, |
| "report_to": wandb_report_to(), |
| "run_name": wandb_run_name(), |
| "seed": SEED, |
| "gradient_checkpointing": grad_checkpointing_on(model_id, vllm_max_len), |
| |
| |
| "gradient_checkpointing_kwargs": {"use_reentrant": False}, |
| |
| |
| |
| |
| |
| |
| "lr_scheduler_type": "constant", |
| "warmup_ratio": 0.0, |
| "beta": _kl_beta, |
| "scale_rewards": "none", |
| "loss_type": "dr_grpo", |
| |
| |
| "optim": fused_optim_name(), |
| } |
| |
| |
| |
| |
| |
| |
| |
| if liger_on(True): |
| grpo_kwargs["use_liger_kernel"] = True |
| print("[rl] liger fused GRPO loss enabled") |
| if use_vllm: |
| |
| |
| |
| |
| grpo_kwargs.update( |
| vllm_mode="colocate", |
| vllm_max_model_length=vllm_max_len, |
| vllm_gpu_memory_utilization=float(os.environ.get("RL_VLLM_GPU_UTIL", "0.45")), |
| vllm_enable_sleep_mode=sleep_mode, |
| ) |
| |
| |
| _grpo_fields = set(getattr(GRPOConfig, "__dataclass_fields__", {})) |
|
|
| def _set_vllm_field(names, value, label): |
| for _f in names: |
| if _f in _grpo_fields: |
| grpo_kwargs[_f] = value |
| print(f"[rl] {label} ({_f}={value})") |
| return True |
| return False |
|
|
| |
| |
| |
| try: |
| import torch as _torch |
|
|
| _want_fp8 = _torch.cuda.get_device_capability() >= (8, 9) |
| except Exception: |
| _want_fp8 = False |
| if _want_fp8: |
| _set_vllm_field(("vllm_kv_cache_dtype", "kv_cache_dtype"), "fp8", "fp8 KV cache") |
| |
| |
| |
| |
| _set_vllm_field( |
| ("vllm_enable_prefix_caching", "enable_prefix_caching"), |
| True, |
| "vLLM prefix caching (shared GRPO prompt KV reuse)", |
| ) |
| _set_vllm_field( |
| ("vllm_enable_chunked_prefill", "enable_chunked_prefill"), |
| True, |
| "vLLM chunked prefill", |
| ) |
| _set_vllm_field( |
| ("vllm_compilation_config", "compilation_config"), |
| {"cudagraph_mode": "FULL_AND_PIECEWISE"}, |
| "vLLM cudagraph_mode (verl rollout default)", |
| ) |
| |
| |
| |
| |
| init_model, init_peft = _init_adapter_model(model_id) |
| if init_peft is not None: |
| |
| |
| |
| |
| |
| |
| _attn = optimal_attn_impl() |
| if quant == "4bit-qlora": |
| _patch_peft_weight_converter_compat() |
| grpo_kwargs["model_init_kwargs"] = qlora_model_init_kwargs() |
| _vllm_note = "; vLLM rollout -> bitsandbytes" if use_vllm else "" |
| print(f"[rl] loading {model_id} in 4-bit (QLoRA tier){_vllm_note}") |
| else: |
| grpo_kwargs["model_init_kwargs"] = {"dtype": "bfloat16"} |
| if _attn: |
| grpo_kwargs["model_init_kwargs"]["attn_implementation"] = _attn |
| else: |
| _attn = optimal_attn_impl() |
| |
| |
| |
| if _t and _t.stop_sequences: |
| grpo_kwargs["generation_kwargs"] = {"stop": list(_t.stop_sequences)} |
| |
| |
| |
| if _adv_clip > 0: |
| print(f"[rl] advantage_clip={_adv_clip} recorded; TRL centers advantages (no value clip)") |
| |
| |
| |
| |
| |
| |
| import dataclasses as _dc |
|
|
| try: |
| _grpo_fields = {f.name for f in _dc.fields(GRPOConfig)} |
| except TypeError: |
| _grpo_fields = set() |
| if "num_iterations" in _grpo_fields: |
| grpo_kwargs["num_iterations"] = 2 |
| print("[rl] rollout amortization: num_iterations=2 (reuse each generation batch)") |
| cfg = GRPOConfig(**grpo_kwargs) |
| setup_seconds = time.time() - t_start |
| heartbeat("rl_train_start", setup_seconds=setup_seconds) |
|
|
| |
| |
| |
| |
| if use_vllm: |
| patch_vllm_language_model_only(model_id) |
| hb_cb = make_reward_heartbeat_callback() |
| |
| |
| |
| extra_trainer_kwargs: dict = {} |
| tools = ACTIVE_ENV.tools() if is_tool_env else [] |
| |
| |
| if is_tool_env and not tools: |
| print("[rl][warn] tool env exposes no tools — using the multi-turn rollout_func path") |
| use_rollout_func = is_multi_turn and not (is_tool_env and tools) |
| require_vllm_for_rollout_func(use_rollout_func, use_vllm, model_id) |
| if is_tool_env and tools: |
| extra_trainer_kwargs["tools"] = tools |
| print(f"[rl] tool env: handing {len(tools)} tool(s) to TRL's native tool loop") |
| if use_rollout_func: |
| from autoslm.engine.multiturn_rollout import ( |
| build_examples_index, |
| build_rollout_func, |
| index_collisions, |
| ) |
|
|
| examples_by_key = build_examples_index(train, ACTIVE_ENV.prompt_messages) |
| ncol = index_collisions(train, ACTIVE_ENV.prompt_messages) |
| if ncol: |
| print( |
| f"[rl][warn] {ncol} duplicate prompt(s) collide in the reward index; the shared " |
| "prompt scores against the last example's answer/info" |
| ) |
| extra_trainer_kwargs["rollout_func"] = build_rollout_func( |
| active_env=ACTIVE_ENV, |
| tok=tok, |
| examples_by_key=examples_by_key, |
| max_completion=_max_completion, |
| max_turns=getattr(ACTIVE_ENV, "max_turns", 10), |
| temperature=_temperature, |
| top_p=rl.sampling_top_p, |
| stop=(list(_t.stop_sequences) if _t and _t.stop_sequences else None), |
| thinking=THINKING, |
| engine_max_len=vllm_max_len, |
| ) |
| print("[rl] multi-turn env: driving the turn loop via rollout_func") |
| trainer = GRPOTrainer( |
| model=init_model, |
| args=cfg, |
| train_dataset=ds, |
| reward_funcs=reward_fn, |
| peft_config=init_peft, |
| processing_class=tok, |
| callbacks=[hb_cb, make_checkpoint_upload_callback()], |
| **extra_trainer_kwargs, |
| ) |
| t_train = time.time() |
| with _sdpa_cudnn_ctx(_attn): |
| trainer.train(resume_from_checkpoint=resume_ckpt) |
| train_wall = time.time() - t_train |
| reward_history = list(getattr(hb_cb, "reward_history", [])) |
|
|
| adapter_dir = f"{out_dir}/adapter" |
| trainer.model.save_pretrained(adapter_dir) |
| tok.save_pretrained(adapter_dir) |
| hf_upload_folder(adapter_dir, "adapter", required=True) |
| heartbeat("rl_trained", train_wall=train_wall) |
|
|
| |
| |
| |
| gen_tokens = steps * batching["unique_prompts_per_step"] * group_size * _max_completion |
| write_train_meta( |
| phase="rl", |
| adapter_dir=adapter_dir, |
| model_id=model_id, |
| train_wall=train_wall, |
| setup_seconds=setup_seconds, |
| train_tokens=0, |
| generated_tokens=gen_tokens, |
| notes={ |
| "steps": steps, |
| "resumed": bool(resume_ckpt), |
| "download_seconds": download_seconds, |
| "hf_transfer": os.environ.get("HF_HUB_ENABLE_HF_TRANSFER", ""), |
| "reward_history": reward_history, |
| "loss_curve": _metric_curve(trainer, "loss"), |
| **wandb_run_info(), |
| "gen_tokens_is_upper_bound": True, |
| "thinking": THINKING, |
| "max_completion_len": _max_completion, |
| "prompts_per_step": batching["unique_prompts_per_step"], |
| "generations_per_step": batching["generations_per_step"], |
| "group_size": group_size, |
| "per_device_train_batch_size": batching["per_device_train_batch_size"], |
| "gradient_accumulation_steps": batching["gradient_accumulation_steps"], |
| "grpo_recipe": { |
| "lr_scheduler": "constant", |
| "beta": _kl_beta, |
| "scale_rewards": "none", |
| "loss_type": "dr_grpo", |
| "temperature": _temperature, |
| "advantage_clip": _adv_clip, |
| "thinking_length_penalty_coef": _think_penalty, |
| "init_from_adapter": JOB_SPEC.train.init_from_adapter if JOB_SPEC else "", |
| }, |
| }, |
| ) |
| free_gpu(trainer) |
|
|
|
|
| |
| |
| |
| def gpu_diagnostics() -> dict: |
| """Collect CUDA/driver diagnostics to pin down GPU init failures on rented nodes.""" |
| diag = {} |
| try: |
| import torch |
|
|
| diag["torch"] = torch.__version__ |
| diag["torch_cuda"] = torch.version.cuda |
| diag["cuda_available"] = torch.cuda.is_available() |
| try: |
| diag["device_count"] = torch.cuda.device_count() |
| diag["device_name"] = torch.cuda.get_device_name(0) |
| except Exception as e: |
| diag["device_query_err"] = str(e)[:160] |
| except Exception as e: |
| diag["torch_import_err"] = str(e)[:160] |
| try: |
| import subprocess |
|
|
| out = subprocess.run( |
| ["nvidia-smi", "--query-gpu=driver_version,name,memory.total", "--format=csv,noheader"], |
| capture_output=True, |
| text=True, |
| timeout=20, |
| ) |
| diag["nvidia_smi"] = (out.stdout or out.stderr).strip()[:200] |
| except Exception as e: |
| diag["nvidia_smi_err"] = str(e)[:160] |
| return diag |
|
|
|
|
| def wait_for_gpu(max_tries=12, sleep_s=10): |
| """Rented nodes sometimes report 'CUDA device not ready' transiently at startup. |
| Poll a trivial CUDA op until it succeeds before doing real work; raise if never ready.""" |
| import time as _t |
|
|
| last = None |
| for i in range(max_tries): |
| try: |
| import torch |
|
|
| if torch.cuda.is_available(): |
| |
| _ = torch.zeros(8, device="cuda") + 1 |
| torch.cuda.synchronize() |
| print(f"GPU ready after {i} retries: {torch.cuda.get_device_name(0)}") |
| return True |
| last = "cuda not available" |
| except Exception as e: |
| last = str(e)[:160] |
| print(f"GPU not ready (try {i + 1}/{max_tries}): {last}; sleeping {sleep_s}s") |
| _t.sleep(sleep_s) |
| raise RuntimeError(f"GPU never became ready after {max_tries} tries: {last}") |
|
|
|
|
| def free_gpu(trainer=None): |
| try: |
| import gc |
|
|
| import torch |
|
|
| try: |
| if trainer is not None and hasattr(trainer, "model"): |
| trainer.model = None |
| except Exception: |
| |
| pass |
| gc.collect() |
| if torch.cuda.is_available(): |
| torch.cuda.empty_cache() |
| except Exception as e: |
| print("free_gpu warn:", e) |
|
|
|
|
| def _metric_curve(trainer, key: str, cap: int = 400) -> list: |
| """The logged values of `key` (e.g. 'loss' or 'reward') from the trainer's log history, |
| rounded + capped. Lets metrics.json carry the convergence/reward curve for an A/B without |
| relying on a checkpoint's trainer_state.json (only written on save_steps) or the console |
| (only uploaded on failure). Never raises.""" |
| try: |
| vals = [round(float(h[key]), 4) for h in trainer.state.log_history if key in h] |
| return vals[:cap] |
| except Exception: |
| return [] |
|
|
|
|
| def write_train_meta( |
| phase, adapter_dir, model_id, train_wall, setup_seconds, train_tokens, generated_tokens, notes |
| ): |
| meta = { |
| "phase": phase, |
| "adapter_dir": adapter_dir, |
| "model_id": model_id, |
| "train_wall": train_wall, |
| "setup_seconds": setup_seconds, |
| "train_tokens": train_tokens, |
| "generated_tokens": generated_tokens, |
| "notes": notes or {}, |
| } |
| with open("/tmp/train_meta.json", "w") as f: |
| json.dump(meta, f) |
| hf_upload_file("/tmp/train_meta.json", "train_meta.json") |
| heartbeat( |
| f"{phase}_train_done", |
| **{k: meta[k] for k in ("train_wall", "train_tokens", "generated_tokens")}, |
| ) |
| |
| |
| |
| m = RunMetrics( |
| |
| |
| |
| arm=os.environ.get("AUTOSLM_ARM", "runpod"), |
| phase=phase, |
| seed=SEED, |
| model_id=model_id, |
| wall_seconds=train_wall, |
| setup_seconds=setup_seconds, |
| train_throughput_toks_per_s=( |
| (generated_tokens or train_tokens) / train_wall if train_wall else 0.0 |
| ), |
| train_tokens=train_tokens, |
| generated_tokens=generated_tokens, |
| notes={ |
| **(notes or {}), |
| "renderer": "autoslm_env", |
| "thinking": THINKING, |
| "train_wall": train_wall, |
| "model_id": model_id, |
| "environment": ACTIVE_ENV.id, |
| "job_spec": JOB_SPEC.to_dict() if JOB_SPEC else None, |
| }, |
| ) |
| _finalize(m, adapter_dir) |
|
|
|
|
| def _download_adapter(adapter_prefix: str | None) -> str | None: |
| if not (adapter_prefix and HF_REPO): |
| return None |
| from huggingface_hub import snapshot_download |
|
|
| snapshot_download( |
| repo_id=HF_REPO, |
| repo_type="dataset", |
| allow_patterns=[f"{adapter_prefix}/adapter/*"], |
| local_dir="/tmp/evdl", |
| token=os.environ.get("HF_TOKEN"), |
| ) |
| adir = os.path.join("/tmp/evdl", adapter_prefix, "adapter") |
| return adir if os.path.isdir(adir) else None |
|
|
|
|
| def _finalize(metrics: RunMetrics, adapter_dir: str): |
| metrics.save("/tmp/metrics.json") |
| |
| hf_upload_file("/tmp/metrics.json", "metrics.json", required=True) |
| |
| with open("/tmp/DONE", "w") as f: |
| f.write(str(time.time())) |
| hf_upload_file("/tmp/DONE", "DONE", required=True) |
| heartbeat("done") |
| print("NODE DONE:", metrics.to_json()) |
|
|
|
|
| def _drop_fla_on_hopper() -> None: |
| """Remove flash-linear-attention when running on a Hopper GPU (sm90, H100). |
| |
| fla's gated chunk_bwd Triton kernel is miscomputed on Hopper with Triton>=3.4 and |
| HARD-RAISES (fla #640), so every gated-delta (Qwen3.5/3.6 family) GRPO backward crashes. |
| The worker base image BAKES fla in, and per-run installs (extra_pip / `prime env install`) |
| can pull it back, so the only reliable place to drop it is HERE: in the worker process, |
| after all installs and BEFORE any model import. transformers then uses the correct |
| pure-PyTorch delta rule (2-3x slower but it RUNS). Runs on BOTH substrates (RunPod and |
| Vast both exec this module). importlib caches are invalidated so the later |
| is_fla_available() probe sees it gone. Ampere/Ada/Blackwell keep fla for the speedup. |
| """ |
| import importlib.util |
| import subprocess |
|
|
| try: |
| import torch |
|
|
| if not (torch.cuda.is_available() and torch.cuda.get_device_capability()[0] == 9): |
| return |
| except Exception: |
| return |
|
|
| if importlib.util.find_spec("fla") is None: |
| return |
| |
| |
| subprocess.run( |
| [sys.executable, "-m", "pip", "uninstall", "-y", "flash-linear-attention"], check=False |
| ) |
| removed, still = _remove_fla_from_disk() |
| print( |
| f"[hopper] fla removed {removed or 'nothing'} (still_importable={still}) -> " |
| f"{'WARNING fla remains' if still else 'pure-PyTorch delta rule'} (fla #640)", |
| flush=True, |
| ) |
|
|
|
|
| def main(): |
| |
| |
| |
| try: |
| |
| |
| |
| |
| if HF_REPO: |
| from huggingface_hub import hf_hub_download |
|
|
| try: |
| hf_hub_download( |
| repo_id=HF_REPO, |
| repo_type="dataset", |
| filename=f"{hf_prefix()}/DONE", |
| token=os.environ.get("HF_TOKEN"), |
| ) |
| done = True |
| except Exception: |
| done = False |
| if done: |
| print("Run already complete (DONE present); returning persisted metrics.") |
| heartbeat("already_done") |
| try: |
| got = hf_hub_download( |
| repo_id=HF_REPO, |
| repo_type="dataset", |
| filename=f"{hf_prefix()}/metrics.json", |
| token=os.environ.get("HF_TOKEN"), |
| ) |
| import shutil |
|
|
| shutil.copy(got, "/tmp/metrics.json") |
| sys.stdout.flush() |
| os._exit(0) |
| except Exception as e: |
| raise SystemExit(f"DONE present but metrics.json unavailable: {e}") from e |
| |
| _drop_fla_on_hopper() |
| heartbeat("boot") |
| finalize_alloc_conf_for_sleep() |
| |
| modes = { |
| "sft": run_sft, |
| "rl": run_rl, |
| } |
| handler = modes.get(RUN_MODE) |
| if handler is None: |
| raise SystemExit(f"unknown RUN_MODE {RUN_MODE}; known: {sorted(modes)}") |
| handler() |
| |
| |
| |
| |
| |
| |
| |
| sys.stdout.flush() |
| sys.stderr.flush() |
| os._exit(0) |
| except Exception as e: |
| tb = traceback.format_exc() |
| traceback.print_exc() |
| |
| |
| |
| try: |
| err_name = error_artifact_name(RUN_MODE) |
| err_path = f"/tmp/{err_name}" |
| with open(err_path, "w") as f: |
| f.write(tb) |
| hf_upload_file(err_path, err_name) |
| except Exception as up_err: |
| print("error-upload warn:", up_err) |
| try: |
| heartbeat(f"error_{RUN_MODE}", error=str(e)[:500], diag=gpu_diagnostics()) |
| except Exception: |
| heartbeat(f"error_{RUN_MODE}", error=str(e)[:500]) |
| |
| time.sleep(10) |
| raise |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|