| """FastAPI control plane for the managed Flash service. |
| |
| This is the operator-side component. It holds the |
| provider credentials (``RUNPOD_API_KEY``, ``HF_TOKEN``, ``PRIME_API_KEY`` — |
| the worker needs the last to ``prime env install`` the run's Prime Hub env) and |
| exposes the full run lifecycle to clients that authenticate with their freesolo API |
| key (verified against the freesolo backend) — clients never see provider credentials. |
| |
| Run state truth stays in the runner's JSON files; SQLite (server/db.py) holds |
| keys and run ownership. Runs the server owns are recovered on startup by re-attaching |
| to their persisted RunPod job handles. |
| """ |
|
|
| from __future__ import annotations |
|
|
| import contextlib |
| import os |
| import threading |
| import weakref |
|
|
| from flash import __version__ |
| from flash.catalog import public_model_rows |
| from flash.runner import ( |
| adapter_prefix, |
| cancel_run, |
| get_status, |
| mark_deployed, |
| mark_undeployed, |
| new_run_id, |
| runs_file_path, |
| submit_job, |
| ) |
| from flash.schema import ConfigError, spec_from_dict |
| from flash.serve.deploy import chat as serve_chat |
| from flash.serve.deploy import deploy_adapter, servable_gpu, undeploy_adapter |
| from flash.spec import JobSpec |
|
|
| from . import auth, db |
|
|
| _RECOVERABLE = {"queued", "provisioning", "running"} |
| |
| _DEPLOYABLE_STATES = {"done", "deployed"} |
|
|
|
|
| class _RunLock: |
| """A weak-referenceable mutex usable as a context manager. |
| |
| ``threading.Lock()`` returns a ``_thread.lock`` that does NOT support weak references, |
| so it can't live in a WeakValueDictionary directly — wrap it in a tiny object that does |
| (and acquire/release via ``with``). |
| """ |
|
|
| __slots__ = ("__weakref__", "_lock") |
|
|
| def __init__(self) -> None: |
| self._lock = threading.Lock() |
|
|
| def __enter__(self) -> _RunLock: |
| self._lock.acquire() |
| return self |
|
|
| def __exit__(self, *exc: object) -> None: |
| self._lock.release() |
|
|
|
|
| |
| |
| |
| |
| |
| _DEPLOY_LOCKS: weakref.WeakValueDictionary[str, _RunLock] = weakref.WeakValueDictionary() |
| _DEPLOY_LOCKS_GUARD = threading.Lock() |
|
|
|
|
| def _deploy_lock(run_id: str) -> _RunLock: |
| |
| |
| with _DEPLOY_LOCKS_GUARD: |
| lk = _DEPLOY_LOCKS.get(run_id) |
| if lk is None: |
| lk = _RunLock() |
| _DEPLOY_LOCKS[run_id] = lk |
| return lk |
|
|
|
|
| def recover_runs(log=None) -> None: |
| """Re-attach to in-flight runs after a server restart (per-run daemon threads).""" |
| from flash.runner import _gc_run_endpoints, _update, attach_run, resume_run |
|
|
| active: set[str] = set() |
| for row in db.all_runs(): |
| try: |
| status = get_status(row["run_id"]) |
| except FileNotFoundError: |
| continue |
| if status.state not in _RECOVERABLE: |
| continue |
| if status.remote: |
| |
| |
| |
| |
| active.add(status.run_id) |
| threading.Thread(target=lambda rid=row["run_id"]: attach_run(rid), daemon=True).start() |
| elif status.resume_seed_index is not None: |
| |
| |
| |
| |
| |
| active.add(status.run_id) |
| threading.Thread(target=lambda rid=row["run_id"]: resume_run(rid), daemon=True).start() |
| else: |
| |
| |
| |
| |
| with contextlib.suppress(Exception): |
| _gc_run_endpoints(JobSpec.from_dict(status.spec)) |
| _update(status.run_id, "failed", error="server restarted before job submission") |
| |
| |
| |
| |
| |
| |
| from flash.providers import configured_providers |
|
|
| for prov in configured_providers(): |
| with contextlib.suppress(Exception): |
| prov.sweep_orphans(active_labels=active) |
|
|
|
|
| def create_app(): |
| try: |
| from fastapi import Depends, FastAPI, Header, HTTPException |
| except ImportError as exc: |
| raise RuntimeError( |
| "the control plane needs the server extras: pip install 'flash[server]'" |
| ) from exc |
| from contextlib import asynccontextmanager |
|
|
| @asynccontextmanager |
| async def lifespan(app): |
| from flash.providers.preflight import check_run_preflight |
|
|
| check_run_preflight() |
| recover_runs() |
| yield |
|
|
| app = FastAPI(title="Flash Control Plane", version=__version__, lifespan=lifespan) |
|
|
| def require_key(authorization: str | None = Header(default=None)) -> dict: |
| key = auth.authenticate(authorization) |
| if key is None: |
| raise HTTPException( |
| status_code=401, |
| detail="invalid or missing API key; log in with `slm login` using your " |
| "freesolo API key", |
| ) |
| return key |
|
|
| def owned_run(run_id: str, key: dict): |
| """Load a run's status iff `key` owns it; 404 otherwise (don't leak existence).""" |
| if db.run_owner(run_id) != key["id"]: |
| raise HTTPException(status_code=404, detail=f"unknown run_id: {run_id}") |
| try: |
| return get_status(run_id) |
| except FileNotFoundError as exc: |
| raise HTTPException(status_code=404, detail=str(exc)) from exc |
|
|
| @app.get("/v1/health") |
| def health(): |
| return {"ok": True, "service": "flash", "version": __version__} |
|
|
| @app.get("/v1/me") |
| def me(key: dict = Depends(require_key)): |
| return { |
| "key_prefix": key["key_prefix"], |
| "email": key["email"], |
| "created_at": key["created_at"], |
| } |
|
|
| @app.get("/v1/models") |
| def models(_: dict = Depends(require_key)): |
| return {"models": public_model_rows()} |
|
|
| def _parse_spec(payload: dict, run_id: str) -> JobSpec: |
| spec_raw = payload.get("spec") or {} |
| env_raw = spec_raw.get("environment") or {} |
| if env_raw.get("path"): |
| raise HTTPException( |
| status_code=400, |
| detail="local environment paths are not supported on the managed service; " |
| "publish the environment to the Prime Hub (`slm env push`), then reference it " |
| 'by its Hub id (`[environment] id = "owner/name"`)', |
| ) |
| try: |
| return spec_from_dict(spec_raw, run_id=run_id) |
| except (ConfigError, ValueError) as exc: |
| raise HTTPException(status_code=400, detail=str(exc)) from exc |
|
|
| @app.post("/v1/runs") |
| def create_run(payload: dict, key: dict = Depends(require_key)): |
| spec = _parse_spec(payload, run_id=new_run_id()) |
| dry_run = bool(payload.get("dry_run", False)) |
| db.record_run(spec.run_id, key["id"], kind="train") |
| try: |
| status = submit_job(spec, dry_run=dry_run, background=True) |
| except Exception as exc: |
| db.delete_run(spec.run_id) |
| raise HTTPException(status_code=400, detail=str(exc)) from exc |
| return status.to_dict() |
|
|
| @app.get("/v1/runs") |
| def list_runs(key: dict = Depends(require_key)): |
| out = [] |
| for row in db.runs_for_key(key["id"]): |
| try: |
| out.append(get_status(row["run_id"]).to_dict()) |
| except FileNotFoundError: |
| continue |
| return {"runs": out} |
|
|
| @app.get("/v1/runs/{run_id}") |
| def run_status(run_id: str, key: dict = Depends(require_key)): |
| status = owned_run(run_id, key) |
| return status.to_dict() |
|
|
| @app.get("/v1/runs/{run_id}/logs") |
| def run_logs(run_id: str, offset: int = 0, key: dict = Depends(require_key)): |
| status = owned_run(run_id, key) |
| log_path = runs_file_path(run_id, ".log") |
| chunk, end = "", max(0, offset) |
| if os.path.exists(log_path): |
| with open(log_path) as f: |
| f.seek(end) |
| chunk = f.read() |
| end = f.tell() |
| return {"run_id": run_id, "logs": chunk, "offset": end, "state": status.state} |
|
|
| @app.post("/v1/runs/{run_id}/cancel") |
| def cancel(run_id: str, key: dict = Depends(require_key)): |
| owned_run(run_id, key) |
| return cancel_run(run_id).to_dict() |
|
|
| @app.post("/v1/runs/{run_id}/deploy") |
| def deploy(run_id: str, payload: dict | None = None, key: dict = Depends(require_key)): |
| payload = payload or {} |
| |
| |
| |
| with _deploy_lock(run_id): |
| status = owned_run(run_id, key) |
| spec = JobSpec.from_dict(status.spec) |
| dry_run = bool(payload.get("dry_run", False)) |
| if not dry_run and status.state not in _DEPLOYABLE_STATES: |
| raise HTTPException( |
| status_code=409, |
| detail=( |
| f"run {run_id} is {status.state!r}; only finished runs with " |
| "trained adapter artifacts can be deployed" |
| ), |
| ) |
| |
| |
| |
| if not dry_run and not spec.train.hf_repo: |
| raise HTTPException( |
| status_code=409, |
| detail=( |
| f"run {run_id} has no [train].hf_repo (legacy run); its adapter artifacts " |
| "cannot be located, so it cannot be deployed" |
| ), |
| ) |
| mode = payload.get("mode", "dev") |
| |
| |
| |
| prev_state = status.state |
| try: |
| dep = deploy_adapter( |
| run_id=run_id, |
| model=spec.model, |
| hf_repo=spec.train.hf_repo, |
| adapter_prefix=adapter_prefix(spec), |
| gpu_name=spec.gpu.type, |
| mode=mode, |
| idle_timeout_s=int(payload.get("idle_timeout_s", 300)), |
| dry_run=dry_run, |
| lora_rank=spec.train.lora_rank, |
| |
| thinking=spec.thinking, |
| ) |
| except Exception as exc: |
| if isinstance(exc, ValueError): |
| raise HTTPException(status_code=400, detail=str(exc)) from exc |
| raise |
| if not dry_run: |
| |
| |
| |
| marked = mark_deployed(run_id, dep.to_dict(), expect_state=prev_state) |
| if marked.state != "deployed": |
| with contextlib.suppress(Exception): |
| undeploy_adapter(run_id, gpu_name=servable_gpu(spec.gpu.type, spec.model)) |
| raise HTTPException( |
| status_code=409, |
| detail=f"run {run_id} became {marked.state!r} during deploy; aborted", |
| ) |
| return dep.to_dict() |
|
|
| @app.delete("/v1/runs/{run_id}/deploy") |
| def undeploy(run_id: str, key: dict = Depends(require_key)): |
| |
| |
| with _deploy_lock(run_id): |
| status = owned_run(run_id, key) |
| spec = JobSpec.from_dict(status.spec) |
| |
| |
| deployed_gpu = (status.deployment or {}).get("gpu") or spec.gpu.type |
| deleted = undeploy_adapter(run_id, gpu_name=deployed_gpu) |
| |
| |
| |
| |
| |
| dev_mode = (status.deployment or {}).get("mode", "dev") == "dev" |
| if status.deployment and (deleted or dev_mode): |
| mark_undeployed(run_id) |
| elif status.deployment and not deleted: |
| raise HTTPException( |
| status_code=502, |
| detail=f"could not delete the serving endpoint for {run_id}; it may still " |
| "be running — retry `slm undeploy`", |
| ) |
| return {"run_id": run_id, "deleted_endpoints": deleted} |
|
|
| @app.get("/v1/deployments") |
| def deployments(key: dict = Depends(require_key)): |
| out = [] |
| for row in db.runs_for_key(key["id"]): |
| try: |
| status = get_status(row["run_id"]) |
| except FileNotFoundError: |
| continue |
| if status.deployment and status.deployment.get("state") not in ( |
| "undeployed", |
| "dry_run", |
| ): |
| out.append(status.to_dict()) |
| return {"deployments": out} |
|
|
| @app.post("/v1/runs/{run_id}/chat") |
| def chat(run_id: str, payload: dict, key: dict = Depends(require_key)): |
| status = owned_run(run_id, key) |
| spec = JobSpec.from_dict(status.spec) |
| deployment = status.deployment or {} |
| |
| |
| |
| if status.state == "cancelled": |
| raise HTTPException( |
| status_code=409, detail=f"run {run_id} was cancelled; redeploy is not allowed" |
| ) |
| |
| |
| if deployment.get("state") in (None, "undeployed", "dry_run"): |
| raise HTTPException( |
| status_code=409, |
| detail=f"run {run_id} has no active deployment; `slm deploy {run_id}` first", |
| ) |
| |
| |
| if not spec.train.hf_repo: |
| raise HTTPException( |
| status_code=409, |
| detail=f"run {run_id} has no [train].hf_repo (legacy run); its adapter cannot be served", |
| ) |
| try: |
| return serve_chat( |
| run_id=run_id, |
| messages=payload.get("messages") or [], |
| model=spec.model, |
| hf_repo=spec.train.hf_repo, |
| adapter_prefix=adapter_prefix(spec), |
| |
| |
| |
| |
| gpu_name=deployment.get("gpu") or spec.gpu.type, |
| temperature=float(payload.get("temperature") or 0.0), |
| max_tokens=int(payload.get("max_tokens") or 512), |
| mode=deployment.get("mode", "dev"), |
| idle_timeout_s=int(deployment.get("idle_timeout_s", 300)), |
| lora_rank=spec.train.lora_rank, |
| |
| thinking=spec.thinking, |
| ) |
| except Exception as exc: |
| raise HTTPException(status_code=502, detail=f"inference failure: {exc}") from exc |
|
|
| return app |
|
|
|
|
| def run_server(host: str = "127.0.0.1", port: int = 8080) -> None: |
| try: |
| import uvicorn |
| except ImportError as exc: |
| raise RuntimeError( |
| "the control plane needs the server extras: pip install 'flash[server]'" |
| ) from exc |
| uvicorn.run(create_app(), host=host, port=port) |
|
|