| """Curated model catalog for one-consumer-GPU LoRA jobs.""" |
|
|
| from __future__ import annotations |
|
|
| import math |
| from dataclasses import asdict, dataclass |
| from typing import Any |
|
|
| ALGORITHMS = ("sft", "grpo") |
|
|
|
|
| def normalize_algorithm(value: str) -> str: |
| """Canonical (lowercased, validated) algorithm name.""" |
| value = (value or "grpo").lower() |
| if value not in ALGORITHMS: |
| raise ValueError(f"unsupported algorithm: {value}; known: {', '.join(ALGORITHMS)}") |
| return value |
|
|
|
|
| |
| |
| |
| |
| |
| DEFAULT_GPU = "RTX 5090" |
|
|
|
|
| @dataclass(frozen=True) |
| class ModelInfo: |
| id: str |
| display_name: str |
| params: str |
| algos: tuple[str, ...] |
| min_vram_gb: int |
| quant: str = "bf16" |
| recommended_gpu: str = DEFAULT_GPU |
| |
| |
| |
| |
| grpo_min_vram_gb: int = 0 |
| notes: str = "" |
| |
| |
| |
| min_disk_gb: int = 0 |
| |
| |
| |
| |
| |
| |
| |
| thinking: str = "none" |
|
|
| def to_dict(self) -> dict[str, Any]: |
| return asdict(self) |
|
|
|
|
| |
| |
| |
| DEFAULT_MODEL = "Qwen/Qwen3.5-4B" |
|
|
| MODELS: dict[str, ModelInfo] = { |
| "openbmb/MiniCPM5-1B": ModelInfo( |
| id="openbmb/MiniCPM5-1B", |
| display_name="MiniCPM5 1B", |
| params="1.2B dense (Llama arch)", |
| algos=("sft", "grpo"), |
| min_vram_gb=12, |
| recommended_gpu="RTX 4090", |
| thinking="hybrid", |
| notes="On-device class SLM (131k ctx); standard Llama architecture.", |
| ), |
| |
| |
| |
| |
| |
| "Qwen/Qwen3.5-0.8B": ModelInfo( |
| id="Qwen/Qwen3.5-0.8B", |
| display_name="Qwen3.5 0.8B", |
| params="0.9B (text-only fine-tune)", |
| algos=("sft", "grpo"), |
| min_vram_gb=12, |
| recommended_gpu="RTX 4090", |
| thinking="hybrid", |
| notes="Smallest Qwen3.5; cheap smoke/dev runs with the modern arch.", |
| ), |
| "Qwen/Qwen3.5-2B": ModelInfo( |
| id="Qwen/Qwen3.5-2B", |
| display_name="Qwen3.5 2B", |
| params="2.3B (text-only fine-tune)", |
| algos=("sft", "grpo"), |
| min_vram_gb=16, |
| recommended_gpu="RTX 4090", |
| thinking="hybrid", |
| ), |
| "Qwen/Qwen3.5-4B": ModelInfo( |
| id="Qwen/Qwen3.5-4B", |
| display_name="Qwen3.5 4B", |
| params="4.7B (text-only fine-tune)", |
| algos=("sft", "grpo"), |
| min_vram_gb=32, |
| recommended_gpu="RTX 5090", |
| thinking="hybrid", |
| notes="Current-gen 4B. GRPO uses the sleep-mode memory recipe (hybrid arch needs " |
| "extra engine state-cache); fused DeltaNet kernels ship in the default stack.", |
| ), |
| "Qwen/Qwen3.5-9B": ModelInfo( |
| id="Qwen/Qwen3.5-9B", |
| display_name="Qwen3.5 9B", |
| params="9.7B (text-only fine-tune)", |
| algos=("sft", "grpo"), |
| min_vram_gb=16, |
| |
| |
| |
| |
| |
| |
| grpo_min_vram_gb=0, |
| quant="4bit-qlora", |
| recommended_gpu="RTX 5090", |
| thinking="hybrid", |
| notes="QLoRA (4-bit NF4 base + bf16 LoRA). GRPO's colocated vLLM rollout loads the " |
| "base 4-bit via bitsandbytes too, so both copies are 4-bit -> fits ~24-32 GB " |
| "instead of 80 GB bf16. ~near-lossless vs bf16 LoRA.", |
| ), |
| } |
|
|
|
|
| def list_models() -> list[ModelInfo]: |
| return sorted(MODELS.values(), key=lambda m: (m.min_vram_gb, m.id)) |
|
|
|
|
| def get_model(model_id: str) -> ModelInfo: |
| try: |
| return MODELS[model_id] |
| except KeyError as exc: |
| allowed = ", ".join(MODELS) |
| raise ValueError( |
| f"unsupported model {model_id!r}; choose one of: {allowed} — or set " |
| f'model_policy = "allow" in the config to run any HF model that fits the GPU ' |
| f"(open-model policy)" |
| ) from exc |
|
|
|
|
| def resolve_model( |
| model_id: str, |
| algorithm: str, |
| policy: str = "catalog", |
| gpu: str | None = None, |
| ) -> ModelInfo: |
| """Resolve a model under the configured policy. |
| |
| ``catalog`` (default): the model must be a curated catalog entry. |
| ``allow``: any HF model is accepted; a coarse VRAM-fit estimate (HF safetensors |
| metadata, no download) blocks only provably-impossible fits and warns on tight ones. |
| """ |
| algo = normalize_algorithm(algorithm) |
| if model_id in MODELS: |
| return validate_model_for_algorithm(model_id, algo) |
| if policy != "allow": |
| |
| return get_model(model_id) |
| return _resolve_open_model(model_id, algo, gpu) |
|
|
|
|
| def _resolve_open_model(model_id: str, algo: str, gpu: str | None) -> ModelInfo: |
| """Synthesize a ModelInfo for the open-model "allow" policy from a coarse VRAM-fit |
| estimate (HF safetensors metadata, no download). Blocks provably-impossible fits and |
| warns on tight ones. Isolates the engine.vram dependency + disk-floor heuristic from |
| the curated-catalog path in resolve_model.""" |
| from flash.engine.vram import check_fit |
|
|
| est = check_fit(model_id, algo, gpu or DEFAULT_GPU) |
| if est.verdict == "too_big": |
| raise ValueError( |
| f"{model_id} does not fit the requested GPU: {est.describe()}. " |
| f"Pick a smaller model or a larger supported GPU." |
| ) |
| if est.verdict in ("tight", "unknown"): |
| print(f"warning: open-model policy: {est.describe()}") |
| params = f"{est.params_b:.1f}B" if est.params_b else "unknown size" |
| |
| |
| |
| |
| |
| min_disk = int(est.params_b * 2) + 64 if est.params_b else 0 |
| return ModelInfo( |
| id=model_id, |
| display_name=model_id, |
| params=params, |
| algos=ALGORITHMS, |
| min_vram_gb=math.ceil(est.est_gb) if est.est_gb else 24, |
| min_disk_gb=min_disk, |
| recommended_gpu=gpu or DEFAULT_GPU, |
| thinking="unknown", |
| notes="unlisted model accepted via the open-model policy (not curated/validated)", |
| ) |
|
|
|
|
| def validate_model_for_algorithm(model_id: str, algorithm: str) -> ModelInfo: |
| info = get_model(model_id) |
| algo = normalize_algorithm(algorithm) |
| |
| |
| required = "grpo" if algo == "grpo" else "sft" |
| if required not in info.algos: |
| allowed = ", ".join(info.algos) |
| raise ValueError(f"{model_id} supports {allowed}, not {algo}") |
| return info |
|
|
|
|
| def public_model_rows() -> list[dict[str, Any]]: |
| return [m.to_dict() for m in list_models()] |
|
|