| """Disaggregated (multi-GPU async) GRPO rollout: launch a ``trl vllm-serve`` rollout server on |
| the inference GPUs and point the GRPO trainer at it (``vllm_mode="server"``), so generation for |
| the next batch overlaps the current optimizer step instead of time-sharing one GPU. |
| |
| verl ref (3D-HybridEngine / flexible device mapping + async rollout): |
| https://github.com/verl-project/verl |
| |
| The command/env builders here are PURE (no torch, no subprocess) so the launch contract is |
| unit-testable on CPU; only :func:`launch_vllm_server` / :func:`wait_for_server_health` / |
| :func:`detect_total_gpus` touch the system. The device split itself lives in |
| :mod:`autoslm.engine.rollout_bench` (``select_rollout_split``). |
| |
| TRL 1.6 server-mode contract (verified against trl/scripts/vllm_serve.py + trl/generation/ |
| vllm_client.py): the trainer's ``VLLMClient`` waits for the server (``vllm_server_timeout``), |
| opens a NCCL weight-sync group on ``vllm_group_port`` (default 51216), and on every generation |
| batch ``sync_weights()`` POSTs ``/update_named_param/`` then broadcasts the (PEFT-merged) weights |
| over NCCL — so weight sync is automatic; we only have to bring the server up first. |
| """ |
|
|
| from __future__ import annotations |
|
|
| import contextlib |
| import os |
| import subprocess |
| import time |
| import urllib.error |
| import urllib.request |
|
|
| from autoslm.engine.rollout_bench import RolloutSplit |
|
|
| |
| |
| |
| DEFAULT_SERVER_PORT = 8000 |
| DEFAULT_GROUP_PORT = 51216 |
| |
| DEFAULT_SERVER_GPU_UTIL = 0.90 |
|
|
|
|
| def server_port() -> int: |
| return int(os.environ.get("AUTOSLM_VLLM_SERVER_PORT", DEFAULT_SERVER_PORT)) |
|
|
|
|
| def group_port() -> int: |
| return int(os.environ.get("AUTOSLM_VLLM_GROUP_PORT", DEFAULT_GROUP_PORT)) |
|
|
|
|
| def server_base_url(port: int | None = None) -> str: |
| return f"http://127.0.0.1:{port or server_port()}" |
|
|
|
|
| def detect_total_gpus(env: dict | None = None) -> int: |
| """Number of GPUs physically on this node, WITHOUT initializing a torch CUDA context. |
| |
| ``torch.cuda.device_count()`` is avoided on the hot path because the disaggregated split must |
| set ``CUDA_VISIBLE_DEVICES`` on the trainer process *before* any CUDA context is created; |
| querying torch here could bind the context to all visible devices first. Prefer the explicit |
| ``AUTOSLM_GPU_COUNT`` the provisioner sets (= ``[gpu] count``), else count ``nvidia-smi -L``. |
| """ |
| env = env if env is not None else os.environ |
| |
| |
| |
| |
| |
| try: |
| out = subprocess.run(["nvidia-smi", "-L"], capture_output=True, text=True, timeout=20) |
| |
| |
| |
| print( |
| f"[rl][disagg][diag] nvidia-smi -L:\n{out.stdout.strip()}\n" |
| f"[rl][disagg][diag] CUDA_VISIBLE_DEVICES={env.get('CUDA_VISIBLE_DEVICES')!r} " |
| f"NVIDIA_VISIBLE_DEVICES={env.get('NVIDIA_VISIBLE_DEVICES')!r} " |
| f"AUTOSLM_GPU_COUNT={env.get('AUTOSLM_GPU_COUNT')!r}" |
| ) |
| lines = [ln for ln in out.stdout.splitlines() if ln.strip().startswith("GPU ")] |
| if lines: |
| real = len(lines) |
| hint = env.get("AUTOSLM_GPU_COUNT") |
| if hint and hint.isdigit() and int(hint) != real: |
| _note = ( |
| "The provider under-provisioned the node." |
| if real < int(hint) |
| else "The node exposes more GPUs than requested." |
| ) |
| print( |
| f"[rl][disagg] WARNING: [gpu] count requested {hint} but the container exposes " |
| f"{real} GPU(s) (nvidia-smi) — using {real}. {_note}" |
| ) |
| return real |
| except Exception: |
| pass |
| explicit = env.get("AUTOSLM_GPU_COUNT") |
| if explicit and explicit.isdigit() and int(explicit) >= 1: |
| return int(explicit) |
| try: |
| import torch |
|
|
| return int(torch.cuda.device_count()) |
| except Exception: |
| return 1 |
|
|
|
|
| def _cvd(devices: tuple[int, ...]) -> str: |
| """CUDA_VISIBLE_DEVICES string from GLOBAL physical device indices.""" |
| return ",".join(str(d) for d in devices) |
|
|
|
|
| def trainer_cuda_visible_devices(split: RolloutSplit) -> str: |
| """The CUDA_VISIBLE_DEVICES the trainer process must use (its train devices, global indices).""" |
| return _cvd(split.train_devices) |
|
|
|
|
| def build_vllm_serve_cmd( |
| model_id: str, |
| split: RolloutSplit, |
| *, |
| max_model_len: int, |
| port: int, |
| gpu_memory_util: float = DEFAULT_SERVER_GPU_UTIL, |
| quant: str = "bf16", |
| trl_bin: str | None = None, |
| trust_remote_code: bool = True, |
| enable_prefix_caching: bool = True, |
| kv_cache_dtype: str | None = None, |
| parallel: str = "tp", |
| extra: list[str] | None = None, |
| ) -> list[str]: |
| """The ``trl vllm-serve`` argv for the rollout server. |
| |
| PARALLELISM across the inference GPUs (after CUDA_VISIBLE_DEVICES pins the server to |
| ``split.infer_devices``, those cards re-index to 0..infer_gpus-1): |
| |
| * ``parallel="tp"`` (DEFAULT) -> ``--tensor_parallel_size infer_gpus``: shard ONE model across |
| the inference GPUs. For a generation-bound GRPO step the decode phase is memory-bandwidth |
| bound, so TP gives ~the aggregate HBM bandwidth of all inference cards (weights + KV split |
| across them) -> larger concurrent batches + faster decode, which is how higher inference |
| ratios (1:2, 1:3) clear the rollout bottleneck. Works for DENSE and MoE alike. |
| * ``parallel="dp"`` -> ``--data_parallel_size infer_gpus --tensor_parallel_size 1``: each |
| inference GPU is a FULL replica and the server load-balances across them. **vLLM REJECTS |
| offline data-parallel for DENSE models** ("Offline data parallel mode is not supported/useful |
| for dense models" — ParallelConfig validation), so this is ONLY usable for the MoE model |
| (Qwen3.6-35B-A3B), where DP shards experts. Do NOT default to it: every dense model in the |
| catalog (MiniCPM, the Qwen3.5 dense line) must use TP. |
| |
| ``max_model_len`` bounds the KV cache to the GRPO need (prompt+completion) so the server starts |
| on a consumer card instead of sizing for the model's full context. |
| |
| Only flags that ``trl vllm-serve``'s ScriptArguments actually defines are emitted (verified |
| against trl/scripts/vllm_serve.py): model, tensor_parallel_size, data_parallel_size, host, port, |
| gpu_memory_utilization, max_model_len, enable_prefix_caching, trust_remote_code. The CLI has NO |
| quantization/load_format option, so a ``4bit-qlora`` model is served bf16/auto on its OWN |
| inference card (in disaggregated mode the inference GPU holds ONLY the rollout, not a colocated |
| trainer, so the full-precision weights fit a big enough card). The trainer still loads the base |
| 4-bit (its model_init_kwargs are independent); TRL merges the LoRA into bf16 before each weight |
| sync, so trainer-4-bit + server-bf16 stays consistent. ``quant`` is accepted for signature |
| stability / sizing notes but does not change the server command. |
| """ |
| bin_ = trl_bin or os.environ.get("AUTOSLM_TRL_BIN", "trl") |
| if parallel == "dp": |
| |
| |
| par_flags = [ |
| "--data_parallel_size", |
| str(split.infer_gpus), |
| "--tensor_parallel_size", |
| "1", |
| ] |
| else: |
| |
| |
| par_flags = ["--tensor_parallel_size", str(split.infer_gpus)] |
| cmd = [ |
| bin_, |
| "vllm-serve", |
| "--model", |
| model_id, |
| *par_flags, |
| "--host", |
| "127.0.0.1", |
| "--port", |
| str(port), |
| "--gpu_memory_utilization", |
| str(gpu_memory_util), |
| "--max_model_len", |
| str(max_model_len), |
| ] |
| if trust_remote_code: |
| cmd += ["--trust_remote_code", "true"] |
| |
| |
| |
| |
| if enable_prefix_caching: |
| cmd += ["--enable_prefix_caching", "true"] |
| if kv_cache_dtype: |
| cmd += ["--kv_cache_dtype", kv_cache_dtype] |
| if extra: |
| cmd += list(extra) |
| return cmd |
|
|
|
|
| |
| |
| |
| TRL_VLLM_SERVE_FLAGS = frozenset( |
| { |
| "--model", |
| "--revision", |
| "--tensor_parallel_size", |
| "--data_parallel_size", |
| "--host", |
| "--port", |
| "--gpu_memory_utilization", |
| "--dtype", |
| "--max_model_len", |
| "--enable_prefix_caching", |
| "--enforce_eager", |
| "--kv_cache_dtype", |
| "--trust_remote_code", |
| "--log_level", |
| "--vllm_model_impl", |
| "--distributed_executor_backend", |
| "--speculative_config", |
| } |
| ) |
|
|
|
|
| def build_accelerate_launch_cmd( |
| split: RolloutSplit, |
| *, |
| worker_module: str = "autoslm.engine.worker", |
| mixed_precision: str = "bf16", |
| use_fsdp: bool = True, |
| python_bin: str | None = None, |
| ) -> list[str]: |
| """``accelerate launch`` argv that runs the GRPO trainer across the TRAIN devices (FSDP). |
| |
| For a train_gpus>1 ratio (2:1, 3:1, 2:2) the trainer must be a *distributed* process group, not |
| a single process spanning many GPUs (that silently becomes nn.DataParallel, which TRL's |
| weight-sync gather does not support). The disaggregated launcher brings the vLLM rollout server |
| up on the inference GPUs first, then re-execs the worker's RL phase under this command so |
| ``accelerate`` shards the trainer across ``split.train_gpus`` with FSDP and the trainer connects |
| to the already-running server (``AUTOSLM_RL_TRAINER_ONLY=1`` in the child env). |
| |
| ``--gpu_ids`` pins the child group to the GLOBAL train device indices (so it never touches the |
| inference card), and ``--num_processes`` == train_gpus (one rank per train GPU). FSDP (vs DDP) |
| is the default because it shards the base weights — required for the 35B-A3B whose base does not |
| fit one card replicated; for small models it is still correct, just less memory-critical. |
| """ |
| cmd = [ |
| "accelerate", |
| "launch", |
| "--num_machines", |
| "1", |
| "--num_processes", |
| str(split.train_gpus), |
| "--gpu_ids", |
| _cvd(split.train_devices), |
| "--mixed_precision", |
| mixed_precision, |
| "--dynamo_backend", |
| "no", |
| ] |
| if use_fsdp: |
| |
| |
| |
| |
| |
| cmd += [ |
| "--use_fsdp", |
| |
| |
| |
| |
| |
| |
| |
| |
| "--fsdp_sharding_strategy", |
| "SHARD_GRAD_OP", |
| "--fsdp_auto_wrap_policy", |
| "TRANSFORMER_BASED_WRAP", |
| |
| |
| "--fsdp_use_orig_params", |
| "true", |
| |
| |
| "--fsdp_state_dict_type", |
| "FULL_STATE_DICT", |
| ] |
| else: |
| |
| cmd.insert(2, "--multi_gpu") |
| cmd += ["-m", worker_module] |
| return cmd |
|
|
|
|
| def is_main_rank(env: dict | None = None) -> bool: |
| """True on the rank that must write artifacts (adapter/metrics/DONE/upload). |
| |
| Single-process paths (colocate, train_gpus==1) set no RANK -> "0" -> main, so existing behavior |
| is unchanged; under ``accelerate launch`` only RANK 0 writes, avoiding N concurrent HF uploads. |
| """ |
| env = env if env is not None else os.environ |
| return str(env.get("RANK", "0")) == "0" |
|
|
|
|
| def trainer_only_mode(env: dict | None = None) -> bool: |
| """True inside the accelerate-launched trainer child (server already up; skip the launcher).""" |
| env = env if env is not None else os.environ |
| return str(env.get("AUTOSLM_RL_TRAINER_ONLY", "")) in ("1", "true", "True") |
|
|
|
|
| def server_subprocess_env(base_env: dict, split: RolloutSplit) -> dict: |
| """Env for the vllm-serve subprocess: pin it to the GLOBAL inference device indices. |
| |
| Built from a COPY so the parent (trainer) can independently set its own CUDA_VISIBLE_DEVICES to |
| the train devices without affecting the already-launched server. The group port is exported so |
| the server's weight-sync extension and the trainer's client agree on the NCCL rendezvous. |
| """ |
| env = dict(base_env) |
| env["CUDA_VISIBLE_DEVICES"] = _cvd(split.infer_devices) |
| env.setdefault("AUTOSLM_VLLM_GROUP_PORT", str(group_port())) |
| |
| |
| |
| |
| |
| |
| env.setdefault("VLLM_WORKER_MULTIPROC_METHOD", "spawn") |
| return env |
|
|
|
|
| def _server_log_tail(log_path: str | None, n: int = 60) -> str: |
| """Last ``n`` lines of the rollout server's log (its model-load crash lives here). The TRL |
| server only binds its HTTP port AFTER its llm_worker child loads the model + reports ready |
| (trl/scripts/vllm_serve.py lifespan), so a load crash shows as 'connection refused' on the |
| port while the real error is ONLY in this log — surface it so error_rl.txt is diagnosable.""" |
| if not log_path: |
| return "" |
| try: |
| with open(log_path, errors="replace") as f: |
| lines = f.read().splitlines() |
| return "\n".join(lines[-n:]) |
| except Exception as e: |
| return f"(server log unreadable: {e})" |
|
|
|
|
| def wait_for_server_health( |
| port: int, |
| *, |
| timeout: float, |
| proc: subprocess.Popen | None = None, |
| log_path: str | None = None, |
| interval: float = 3.0, |
| on_wait=None, |
| on_wait_every: float = 60.0, |
| ) -> None: |
| """Block until the rollout server answers ``/health/`` 200, the timeout elapses, or the |
| subprocess dies (fail fast — a dead server would otherwise hang the trainer at first generation). |
| On any failure the server log tail is appended (the HTTP port stays unbound until the worker |
| finishes loading the model, so a load/OOM crash surfaces only as 'connection refused'). |
| |
| ``on_wait`` (called every ``on_wait_every`` s while waiting) lets the caller emit a liveness |
| heartbeat during a long boot: a BIG model (35B bf16 ~70 GB + tilelang/CUDA-graph JIT) can take |
| >20 min to bind the port, and the control plane's no-heartbeat STALL detector (~25 min) would |
| otherwise kill the run mid-boot even though it is healthy-progressing.""" |
| url = f"http://127.0.0.1:{port}/health/" |
| deadline = time.time() + timeout |
| last = None |
| |
| |
| |
| _next_ping = time.time() |
| while time.time() < deadline: |
| if on_wait is not None and time.time() >= _next_ping: |
| with contextlib.suppress(Exception): |
| on_wait() |
| _next_ping = time.time() + on_wait_every |
| |
| |
| |
| |
| if proc is not None and proc.poll() is not None: |
| raise RuntimeError( |
| f"vllm-serve exited (code {proc.returncode}) before becoming healthy.\n" |
| f"--- vllm-serve log tail ---\n{_server_log_tail(log_path)}" |
| ) |
| if time.time() >= deadline: |
| break |
| try: |
| with urllib.request.urlopen(url, timeout=interval) as r: |
| if 200 <= r.status < 300: |
| return |
| except urllib.error.HTTPError as e: |
| |
| |
| |
| |
| last = f"HTTP {e.code}" |
| except Exception as e: |
| last = str(e)[:80] |
| time.sleep(interval) |
| raise TimeoutError( |
| f"vllm-serve not healthy after {timeout:.0f}s (last: {last}).\n" |
| f"--- vllm-serve log tail ---\n{_server_log_tail(log_path)}" |
| ) |
|
|
|
|
| def launch_vllm_server( |
| cmd: list[str], env: dict, *, log_path: str = "/tmp/vllm_serve.log" |
| ) -> subprocess.Popen: |
| """Start the rollout server subprocess, streaming its stdout/stderr to ``log_path`` (so a |
| server-side load/OOM is recoverable from the run log on a rented node).""" |
| log = open(log_path, "wb") |
| print(f"[rl][disagg] launching rollout server: {' '.join(cmd)}") |
| print(f"[rl][disagg] server CUDA_VISIBLE_DEVICES={env.get('CUDA_VISIBLE_DEVICES')} log={log_path}") |
| return subprocess.Popen(cmd, env=env, stdout=log, stderr=subprocess.STDOUT) |
|
|
|
|
| def terminate_server(proc: subprocess.Popen | None, *, timeout: float = 15.0) -> None: |
| """Best-effort shutdown of the rollout server after training (free its GPU + port).""" |
| if proc is None or proc.poll() is not None: |
| return |
| try: |
| proc.terminate() |
| proc.wait(timeout=timeout) |
| except Exception: |
| with contextlib.suppress(Exception): |
| proc.kill() |
|
|