#
# 1Cat-LLM (formerly 1Cat-vLLM) v1.5.1 — V100/SM70-optimized vLLM fork
#
# Build context:
#   cp 1cat_vllm-1.5.1-cp312-cp312-linux_x86_64.whl ./
#   sha256sum 1cat_vllm-1.5.1-cp312-cp312-linux_x86_64.whl   # must match WHEEL_SHA256
#   docker buildx build --platform linux/amd64 -t 1cat-llm:1.5.1 .
#
# Run (single V100):
#   docker run -d --gpus all --ipc=host -p 8000:8000 \
#     -v /path/to/models:/models \
#     -v vllm-cache:/var/lib/vllm \
#     -e MODEL=/models/<your-model> \
#     1cat-llm:1.5.1
#
# Stack (exactly as pinned by the v1.5.1 release notes):
#   Python 3.12.3 / PyTorch 2.10.0+cu128 / CUDA 12.8 / cuDNN 9
#   (the wheel is cp312-linux_x86_64 only — no PyPI package exists)

FROM pytorch/pytorch:2.10.0-cuda12.8-cudnn9-runtime

ARG WHEEL_NAME=1cat_vllm-1.5.1-cp312-cp312-linux_x86_64.whl
# Digest of the v1.5.1 release asset (GitHub asset SHA256)
ARG WHEEL_SHA256=757f2200ba6bd20f7e3e5c8fc68c7e05461df6d284baf676ac0c16ce19434959

# PEP 668: the Debian bookworm userland in the pytorch image marks system
# Python as "externally managed" — allow system-wide pip installs.
ENV PIP_BREAK_SYSTEM_PACKAGES=1 \
    PIP_NO_CACHE_DIR=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1

# build-essential/cmake/ninja: needed if a model path requires tilelang JIT
# (nvcc is NOT in this image — mount host CUDA 12.8 with
#  -v /usr/local/cuda-12.8:/usr/local/cuda:ro for those models)
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        build-essential \
        ca-certificates \
        cmake \
        curl \
        ninja-build \
        vim-tiny \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /opt/1cat

# Install the wheel from the build context (local copy, hash-verified).
# If you'd rather fetch at build time, replace the COPY+sha256sum lines with:
#   curl -fL --retry 3 -o /opt/1cat/1cat_vllm.whl \
#     "https://github.com/1CatAI/1Cat-vLLM/releases/download/v1.5.1/${WHEEL_NAME}"
#   echo "${WHEEL_SHA256}  /opt/1cat/1cat_vllm.whl" | sha256sum -c -
COPY ${WHEEL_NAME} ./${WHEEL_NAME}
RUN set -eux; \
    echo "${WHEEL_SHA256}  /opt/1cat/${WHEEL_NAME}" | sha256sum -c -; \
    pip install --break-system-packages /opt/1cat/${WHEEL_NAME}; \
    rm -f /opt/1cat/${WHEEL_NAME}; \
    # sanity: fork is importable and reports 1.5.1
    python -c "import vllm, importlib.metadata as md; v = md.version('1cat-vllm'); assert v == '1.5.1', v; print('1Cat-LLM', v)"

# ---- Runtime config (all overridable at `docker run` time) ----------------
# v1.5.1 release profile defaults (from the release notes):
#   max-model-len 262144 | prefill budget 8192 | max-num-seqs 4
#   gpu-memory-utilization 0.80 | kv e4m3 (NVFP4 checkpoints) | prefix caching
#   async scheduling | FULL_AND_PIECEWISE cudagraphs | FLASH_ATTN_V100 backend
ENV TENSOR_PARALLEL_SIZE=1 \
    MAX_MODEL_LEN=262144 \
    MAX_NUM_SEQS=4 \
    MAX_NUM_BATCHED_TOKENS=8192 \
    GPU_MEMORY_UTILIZATION=0.80 \
    KV_CACHE_DTYPE=e4m3 \
    MODEL=""

# FP8/FP16 checkpoints: set KV_CACHE_DTYPE=fp8_e5m2 (or auto) at run time;
# if a model rejects the V100 attention backend, pass --no-enable-prefix-caching
# or your own extra CLI args via ENTRYPOINT overrides below.

# /var/lib/vllm = vLLM compile/JIT cache (mount a volume here to keep it
# across container recreation); /models = mount your weights here.
VOLUME ["/models", "/var/lib/vllm"]
ENV VLLM_CACHE_ROOT=/var/lib/vllm

EXPOSE 8000

HEALTHCHECK --interval=60s --timeout=10s --start-period=300s --retries=5 \
    CMD python -c "import urllib.request,sys; sys.exit(0 if urllib.request.urlopen('http://127.0.0.1:8000/health', timeout=5).status==200 else 1)" || exit 1

ENTRYPOINT ["/opt/1cat/serve.sh"]
CMD [""]

# ---------------------------------------------------------------------------
# serve.sh — 1.5.1 profile defaults + env overrides + pass-through extra args
# ---------------------------------------------------------------------------
RUN vllm serve "${MODEL}" \
  --served-model-name "${SERVED_NAME:-qwen3.8-27b-awq}" \
  --port "${PORT:-8000}" \
  --tensor-parallel-size "${TENSOR_PARALLEL_SIZE:-1}" \
  --max-model-len "${MAX_MODEL_LEN:-262144}" \
  --max-num-seqs "${MAX_NUM_SEQS:-4}" \
  --max-num-batched-tokens "${MAX_NUM_BATCHED_TOKENS:-8192}" \
  --gpu-memory-utilization "${GPU_MEMORY_UTILIZATION:-0.80}" \
  --kv-cache-dtype "${KV_CACHE_DTYPE:-e4m3}" \
  --enable-prefix-caching \
  --enable-auto-tool-choice \
  --tool-call-parser qwen3_coder \
  --enable-chunked-prefill
)
