Skip to content

The platform for continuously improving AI agents.

9.5 KB
Raw Blame
"""Immutable train/serve stacks. FamilySpec points here; never edit a live pin set.

Add a model:
  1. Existing stack works → FamilySpec patterns (+ models.json). Stop.
  2. Need new Unsloth/torch/transformers/vLLM pins → new stack key, new Image,
     generate Function/Cls, canary, then point only that family at it.
  3. Never change pip/docker pins on an existing key. Clone → new key.

Deployed Modal names equal the stack id (``sft_u2026_7``, ``L4_vllm``). One
Function per Image — families sharing pins share the Function.

Stdlib only — imported by Django, Modal, and tests.
"""

from __future__ import annotations

from dataclasses import dataclass

# --- train stacks (FamilySpec.train_image) ---------------------------------

TRAIN_U2026_7 = "u2026_7"
TRAIN_U2026_8_TF510 = "u2026_8_tf510"
TRAIN_U2026_8_TF515 = "u2026_8_tf515"
TRAIN_U2026_8_GPOS = "u2026_8_gptoss"
TRAIN_U2026_8_18 = "u2026_8_18"
TRAIN_U2026_9_2 = "u2026_9_2"
TRAIN_U2026_10_7 = "u2026_10_7"
# Gemma 4. 2026.8.5 on this family's old image resolved transformers to 5.24 and
# died reading head_dim. 2026.11.9 with transformers held below 5.15 trained.
TRAIN_U2026_11_9 = "u2026_11_9"
# Inkling2Loop landed in transformers 5.24. The 2026.11.9 image caps below 5.15.
TRAIN_INKLING = "inkling_174067b"
# Qwen4 MoE needs transformers 5.26. Every released Unsloth wheel caps below that.
TRAIN_QWEN4 = "qwen4_56397f9"
TRAIN_DECISION = "u2026_10_3_decision"
# Kev's trainer is not Unsloth and refuses torch 2.10. Own image, own key.
TRAIN_KEV = "kev_2026_11"
KEV_REV = "051142bf849c2c6ad3f23e17a7b5aea2686d9a91"

# Chat images eligible as a native decision foundation.
NATIVE_FOUNDATION_STACKS = frozenset({TRAIN_U2026_8_18, TRAIN_U2026_10_7})

TRAIN_STACKS: tuple[str, ...] = (
    TRAIN_U2026_7,
    TRAIN_U2026_8_TF510,
    TRAIN_U2026_8_TF515,
    TRAIN_U2026_8_GPOS,
    TRAIN_U2026_8_18,
    TRAIN_U2026_9_2,
    TRAIN_U2026_10_7,
    TRAIN_U2026_11_9,
    TRAIN_INKLING,
    TRAIN_QWEN4,
    TRAIN_DECISION,
    TRAIN_KEV,
)

# Historical FamilySpec / models.json keys. Lookup must normalize.
TRAIN_STACK_ALIASES: dict[str, str] = {
    "default": TRAIN_U2026_8_18,
    "gemma4": TRAIN_U2026_11_9,
    "nemotron35": TRAIN_U2026_8_TF510,
    "muse": TRAIN_U2026_8_TF515,
    "gpt_oss": TRAIN_U2026_8_GPOS,
    TRAIN_U2026_7: TRAIN_U2026_7,
    TRAIN_U2026_8_TF510: TRAIN_U2026_8_TF510,
    TRAIN_U2026_8_TF515: TRAIN_U2026_8_TF515,
    TRAIN_U2026_8_GPOS: TRAIN_U2026_8_GPOS,
    TRAIN_U2026_8_18: TRAIN_U2026_8_18,
    TRAIN_U2026_9_2: TRAIN_U2026_9_2,
    TRAIN_U2026_10_7: TRAIN_U2026_10_7,
    TRAIN_U2026_11_9: TRAIN_U2026_11_9,
}

# Deployed Modal Function = sft_{stack}. One name per Image.
TRAIN_FUNCTION_NAMES: dict[str, str] = {s: f"sft_{s}" for s in TRAIN_STACKS}

TRAIN_STACK_PINS: dict[str, str] = {
    TRAIN_DECISION: "unsloth==2026.10.3 / unsloth_zoo==2026.10.3 / torch==2.10.0 / transformers==5.17.0 / trl==1.13.0",
    TRAIN_KEV: "torch==2.8.0 / transformers==5.17.0 / peft==0.22.0 / huggingface_hub==1.33.0 / kev@051142bf",
    TRAIN_U2026_7: "unsloth[cu128-torch270]==2026.7.5 / torch==2.7.0 / transformers>=5.2 / trl v1.10.0",
    TRAIN_U2026_8_TF510: "unsloth[cu128-torch2100]==2026.8.5 / torch==2.10.0 / transformers>=5.10.2 / trl v1.10.0",
    TRAIN_U2026_8_TF515: "unsloth[cu128-torch2100]==2026.8.5 / torch==2.10.0 / transformers>=5.15 / trl v1.10.0",
    TRAIN_U2026_8_GPOS: "unsloth[cu128-torch2100]==2026.8.5 / torch==2.10.0 / Unsloth transformers pin / kernels>=0.12,<0.15 / trl v1.10.0",
    TRAIN_U2026_8_18: "unsloth[cu128-torch2100]==2026.8.18 / unsloth_zoo==2026.8.12 / torch==2.10.0 / transformers>=5.10.2 / trl v1.10.0",
    TRAIN_U2026_9_2: "unsloth[cu128-torch2100]==2026.9.2 / unsloth_zoo==2026.9.1 / torch==2.10.0 / transformers>=5.10.2 / trl v1.10.0",
    TRAIN_U2026_10_7: "unsloth[cu128-torch2100]==2026.10.7 / unsloth_zoo==2026.10.7 / torch==2.10.0 / transformers==5.17.0 / trl v1.10.0",
    TRAIN_U2026_11_9: "unsloth[cu128-torch2100]==2026.11.9 / unsloth_zoo==2026.11.9 / torch==2.10.0 / transformers>=5.10.2,<5.15 / trl v1.10.0",
    TRAIN_INKLING: "unsloth@174067b17e794edd9f39bec0f1b182d9790fcbec / unsloth_zoo==2026.11.9 / torch==2.10.0 / transformers==5.24.1 / compressed-tensors>=0.15.0 / trl v1.10.0",
    TRAIN_QWEN4: "unsloth@56397f9d7b06177d035239ebed233a17646e5d42 / unsloth_zoo@12361e94e592c794d5b90032cac55babc61deaa5 / torch==2.10.0 / transformers==5.26.0 / trl v1.10.0",
}


def normalize_train_stack(key: str | None) -> str:
    raw = (key or "").strip()
    if not raw:
        return TRAIN_U2026_8_18
    if raw in TRAIN_STACKS:
        return raw
    return TRAIN_STACK_ALIASES.get(raw) or TRAIN_STACK_ALIASES.get(raw.lower()) or TRAIN_U2026_8_18


def train_function_name(stack_or_alias: str | None) -> str:
    return TRAIN_FUNCTION_NAMES[normalize_train_stack(stack_or_alias)]


# --- serve stacks (FamilySpec.serve_image / routing headers) ---------------
# vLLM 0.33.0 is the shared pool and the dedicated pool. SGLang is the shared
# pool for the adapter bake-off winners only. LFM3 has its own image.

SERVE_VLLM = "vllm"
SERVE_SGLANG = "sglang"
SERVE_SGLANG_LFM3 = "sglang_lfm3"
# Kev's pointer head is served by SGLang 0.5.23. 0.5.22 does not load it.
SERVE_SGLANG_KEV = "sglang_kev"
# Inkling2Loop is not in v0.5.22. The vendor image is the one that loads it.
SERVE_SGLANG_INKLING = "sglang_inkling"
# Qwen4 MoE is not in v0.5.22. This digest is lmsysorg/sglang:qwen4 (linux/amd64).
SERVE_SGLANG_QWEN4 = "sglang_qwen4"

SERVE_STACKS: tuple[str, ...] = (
    SERVE_VLLM,
    SERVE_SGLANG,
    SERVE_SGLANG_LFM3,
    SERVE_SGLANG_KEV,
    SERVE_SGLANG_INKLING,
    SERVE_SGLANG_QWEN4,
)

SERVE_STACK_PINS: dict[str, str] = {
    SERVE_VLLM: "vllm/vllm-openai:v0.33.0 + runai-model-streamer>=0.15.7",
    SERVE_SGLANG: "lmsysorg/sglang:v0.5.22",
    SERVE_SGLANG_LFM3: "lmsysorg/sglang:dev-lfm3",
    SERVE_SGLANG_KEV: "lmsysorg/sglang:v0.5.23",
    SERVE_SGLANG_INKLING: "lmsysorg/sglang:inkling-2-mini",
    SERVE_SGLANG_QWEN4: "lmsysorg/sglang@sha256:1a2931d9cb1e2815cc08b05b9c92a83a0e319ccf9519514dc31ccb2affc8165f",
}

# (gpu_type, serve_image) pairs we deploy. Cls name is ``{gpu}_{serve_image}``
# with hyphens stripped from gpu (A100-80GB → A10080GB_vllm).
_SERVERLESS_GPUS = ("L4", "L40S", "A100-80GB", "H200", "B200", "B300")
WORKER_ALLOWED: frozenset[tuple[str, str]] = frozenset(
    (gpu, image) for image in SERVE_STACKS for gpu in _SERVERLESS_GPUS
)


def _gpu_token(gpu_type: str) -> str:
    return gpu_type.replace("-", "")


def worker_cls_name(
    gpu_type: str,
    serve_image: str = SERVE_VLLM,
    default: str | None = None,
    *,
    enable_lora: bool = False,
) -> str:
    image = serve_image or SERVE_VLLM
    if (gpu_type, image) not in WORKER_ALLOWED:
        if default is not None and image == SERVE_VLLM:
            return default
        raise ValueError(f"no inference worker for gpu={gpu_type!r} serve_image={image!r}")
    return f"{_gpu_token(gpu_type)}_{image}" + ("_lora" if enable_lora else "")


WORKER_CLS: dict[tuple[str, str], str] = {pair: worker_cls_name(*pair) for pair in WORKER_ALLOWED}

GPU_CLASS_MAP: dict[str, str] = {
    gpu: cls for (gpu, image), cls in WORKER_CLS.items() if image == SERVE_VLLM
}

SERVE_CLASS_MAP: dict[tuple[str, str], str] = {
    (gpu, image): cls for (gpu, image), cls in WORKER_CLS.items() if image != SERVE_VLLM
}

# MiB reserved for the vLLM process. Dedicated quotes use the same table.
WORKER_CPU = 8
WORKER_MEMORY_MIB: dict[str, int] = {
    "L4": 131072,
    "L40S": 196608,
    "A100-80GB": 131072,
    "H100": 131072,
    "H200": 196608,
    "B200": 262144,
    "B300": 393216,
}

# scaledown bucket + default max_model_len per GPU (used when generating Cls).
GPU_TIER: dict[str, tuple[str, int]] = {
    "L4": ("short", 8192),
    "L40S": ("short", 16384),
    "A100-80GB": ("large", 16384),
    # Dedicated only. Serverless workers are still gated by WORKER_ALLOWED.
    "H100": ("large", 16384),
    "H200": ("large", 32768),
    "B200": ("large", 16384),
    "B300": ("large", 32768),
}


# --- canaries (prove stacks, not the catalog) ------------------------------


@dataclass(frozen=True)
class TrainCanary:
    stack: str
    hf_model_id: str
    training_type: str  # "Lora" | "Full" — both Unsloth
    params_b: float
    gate: str  # "pr" | "nightly"


TRAIN_CANARIES: tuple[TrainCanary, ...] = (
    TrainCanary(TRAIN_DECISION, "Qwen/Qwen3-0.6B", "Lora", 0.6, "pr"),
    # LoRA loads the Unsloth Qwen3.5 0.8B checkpoint this stack trains in production.
    # Full stays on dense Qwen3-0.6B, which shares the image.
    TrainCanary(TRAIN_U2026_10_7, "unsloth/Qwen3.5-0.8B", "Lora", 0.8, "pr"),
    TrainCanary(TRAIN_U2026_10_7, "Qwen/Qwen3-0.6B", "Full", 0.6, "pr"),
    TrainCanary(TRAIN_U2026_9_2, "Qwen/Qwen3-0.6B", "Lora", 0.6, "pr"),
    TrainCanary(TRAIN_U2026_9_2, "Qwen/Qwen3-0.6B", "Full", 0.6, "pr"),
    TrainCanary(TRAIN_U2026_8_18, "LiquidAI/LFM2.5-230M", "Lora", 0.23, "pr"),
    TrainCanary(TRAIN_U2026_8_18, "LiquidAI/LFM2.5-230M", "Full", 0.23, "pr"),
    # Bolt stays on tf510. Gemma 4 moved to 2026.11.9.
    TrainCanary(TRAIN_U2026_8_TF510, "nvidia/NVIDIA-Nemotron-3.5-Bolt-E4B-BF16", "Lora", 8.0, "pr"),
    TrainCanary(TRAIN_U2026_11_9, "unsloth/gemma-4-E2B-it", "Lora", 5.1, "pr"),
    TrainCanary(TRAIN_INKLING, "unsloth/Inkling-2-Mini", "Lora", 26.4, "nightly"),
    TrainCanary(TRAIN_QWEN4, "Qwen/Qwen4-35B-A3B", "Lora", 35.0, "nightly"),
    TrainCanary(TRAIN_U2026_7, "Qwen/Qwen3-0.6B", "Lora", 0.6, "nightly"),
    TrainCanary(TRAIN_U2026_8_TF515, "unsloth/Muse-Glimmer-30B", "Lora", 30.0, "nightly"),
    TrainCanary(
        TRAIN_U2026_8_GPOS, "unsloth/gpt-oss-20b-unsloth-bnb-4bit", "Lora", 21.5, "nightly"
    ),
)