Skip to content

The platform for continuously improving AI agents.

4.5 KB
Raw Blame
"""vLLM /v1/systemone argv and the predict envelope DecisionEndpoint returns."""

from __future__ import annotations

import json
import math
from pathlib import Path

from modal_shared.serving.args import snap_lora_rank


def decision_vllm_command(
    *,
    model_path: str,
    served_name: str,
    adapter_path: str,
    max_lora_rank: int,
    max_model_len: int,
    port: int = 8000,
) -> list[str]:
    """One joint head overlaid on the base, then that artifact's LoRA."""
    rank = int(max_lora_rank or 0)
    config = Path(adapter_path) / "adapter_config.json"
    if config.is_file():
        stored = int(json.loads(config.read_text()).get("r") or 0)
        rank = max(rank, stored)
    rank = snap_lora_rank(rank or 16)
    return [
        "vllm",
        "serve",
        model_path,
        "--served-model-name",
        f"{served_name}--base",
        "--enable-lora",
        "--max-loras",
        "1",
        "--max-lora-rank",
        str(rank),
        "--lora-modules",
        f"{served_name}={adapter_path}",
        "--host",
        "0.0.0.0",
        "--port",
        str(port),
        "--max-model-len",
        str(max_model_len),
        "--gpu-memory-utilization",
        "0.90",
        "--enforce-eager",
        "--trust-remote-code",
        "--dtype",
        "bfloat16",
    ]


def decision_sglang_command(*, model_path: str, max_model_len: int, port: int = 8000) -> list[str]:
    """SGLang 0.5.23 loads a Kev pointer head from head.pt plus the adapter."""
    return [
        "sglang",
        "serve",
        "--model-path",
        model_path,
        "--context-length",
        str(max_model_len),
        "--host",
        "0.0.0.0",
        "--port",
        str(port),
    ]


def decision_base_vllm_command(
    *, model_path: str, served_name: str, max_model_len: int, port: int = 8000
) -> list[str]:
    """A catalog base whose decision head is already in the checkpoint."""
    return [
        "vllm",
        "serve",
        model_path,
        "--served-model-name",
        served_name,
        "--host",
        "0.0.0.0",
        "--port",
        str(port),
        "--max-model-len",
        str(max_model_len),
        "--gpu-memory-utilization",
        "0.90",
        "--enforce-eager",
        "--trust-remote-code",
        "--dtype",
        "bfloat16",
    ]


def systemone_question(request: dict) -> dict:
    kind = request["kind"]
    if kind == "noul":
        return {
            "type": "noul",
            "instructions": request["question"],
            "criteria": {"false": request["options"][0], "true": request["options"][1]},
        }
    if kind == "score":
        return {
            "type": "score",
            "instructions": request["question"],
            "criteria": list(request["options"]),
        }
    return {
        "type": "choice",
        "instructions": request["question"],
        "criteria": {option: option for option in request["options"]},
    }


def _labeled(options: list[str], raw: dict) -> list[float] | None:
    if all(option in raw for option in options):
        return [float(raw[option]) for option in options]
    folded = {str(key).casefold(): value for key, value in raw.items()}
    if all(option.casefold() in folded for option in options):
        return [float(folded[option.casefold()]) for option in options]
    return None


def probabilities_for(request: dict, answer: dict) -> list[float]:
    options = request["options"]
    raw = answer.get("probabilities")
    labeled = _labeled(options, raw) if isinstance(raw, dict) else None
    if labeled is not None:
        values = labeled
    elif isinstance(raw, list) and len(raw) == len(options):
        values = [float(item) for item in raw]
    elif request["kind"] == "noul" and "noul" in answer:
        # vLLM labels a noul answer yes/no. The criteria text is options[0]/options[1],
        # and options[1] is the positive class.
        positive = float(answer["noul"])
        values = [1 - positive, positive]
    else:
        raise ValueError("Invalid decision probabilities")
    if len(values) != len(options) or any(
        not math.isfinite(value) or not 0 <= value <= 1 for value in values
    ):
        raise ValueError("Invalid decision probabilities")
    if not math.isclose(sum(values), 1, abs_tol=1e-3):
        raise ValueError("Invalid decision probabilities")
    return values


def systemone_body(request: dict, served_name: str) -> dict:
    return {
        "model": served_name,
        "state": request["state"],
        "questions": {"0": systemone_question(request)},
    }