#!/usr/bin/env bash
# Adapted from the Thor launch scripts inspected on 2026-11-10.
# Run on the Thor after downloading the specified model revision to HF cache.
set -euo pipefail
PROFILE="${1:-flash-next}"
CACHE_ROOT="${QWEN_CACHE_ROOT:-${HOME}/.cache}"
case "$PROFILE" in
  flash-next)
    IMAGE="vllm/vllm-openai@sha256:cca10003856866925653fb4ec45309df385dc15f802dbd47c289eb9c5720f197"
    MODEL="nvidia/Qwen3.8-Flash-Next-NVFP4"
    REVISION="fc694b54fb0174e0913e6adf86691ef85a4ead47"
    NAME="qwen38-flash-next"
    EXTRA=(--engram-config '{"disk_offload": true}' --mamba-cache-mode align
      --gpu-memory-utilization 0.75 --tool-call-parser qwen3_xml)
    ;;
  qwen35)
    IMAGE="vllm/vllm-openai:v0.28.0"
    MODEL="nvidia/Qwen3.5-122B-A10B-NVFP4"
    REVISION="98915d837c4e7c87ac8296d02e89de19b3207e6d"
    NAME="qwen35-122b"
    EXTRA=(--quantization modelopt_fp4 --tensor-parallel-size 1
      --gpu-memory-utilization 0.72 --attention-backend FLASHINFER
      --tool-call-parser qwen3_coder)
    ;;
  *) echo 'Usage: bash serve.sh [flash-next|qwen35]' >&2; exit 2 ;;
esac
mkdir -p "$CACHE_ROOT/huggingface" "$CACHE_ROOT/vllm"
docker run -d --name "$NAME" --runtime nvidia --network host --ipc host \
  -e HF_HUB_OFFLINE=1 \
  -v "$CACHE_ROOT/huggingface:/root/.cache/huggingface" \
  -v "$CACHE_ROOT/vllm:/root/.cache/vllm" \
  --entrypoint vllm "$IMAGE" serve "$MODEL" --revision "$REVISION" \
  --served-model-name "$NAME" --host 127.0.0.1 --port 8000 \
  --trust-remote-code --language-model-only --kv-cache-dtype fp8 \
  --max-model-len 65536 --max-num-seqs 2 --enable-prefix-caching \
  --reasoning-parser qwen3 --enable-auto-tool-choice "${EXTRA[@]}"
