# syntax=docker/dockerfile:1.7
# DeepSeek-V4-Flash-0731 — eugr/spark-vllm-b12x overlay with the
# instanttensor-hybrid-draft-loader mod baked in.
#
# Base: eugr/spark-vllm-b12x — the prebuilt b12x-branch image published in
# NVIDIA forum comment #28 (https://forums.developer.nvidia.com/t/376220/28).
# Ubuntu 24.04 + system Python 3.12; vLLM is installed into the system
# site-packages (the mod's default /usr/local/lib/python3.12/dist-packages,
# but the path is resolved dynamically below so this is robust to a moved
# install).
#
# eugr's recipe applies `mods/instanttensor-hybrid-draft-loader` at
# run-recipe.sh time (it patches vLLM's model_loader get_model() to keep
# InstantTensor for the target while letting an embedded MTP/dspark draft
# use lazy safetensors). sm121 has no runtime mod-upload step for Docker
# recipes, so this overlay bakes the patch in at build time instead: the
# running container already has the hybrid loader, and the recipe carries
# no [mods] block.
#
# Build safety: the patcher refuses to patch an incompatible module (it
# anchors on vLLM's exact get_model signature and self-compiles the result).
# If a future eugr image bumps vLLM and that signature changes, `docker
# build` fails here instead of silently shipping an unpatched image.
#
# Build: cd docker-images/deepseek-v4-flash-eugr && bash build.sh
# (the sm121 launcher builds on demand and stamps sm121.checksum)

FROM eugr/spark-vllm-b12x

# Vendor the mod's patcher (vendored from eugr/spark-vllm-docker b12x branch,
# mods/instanttensor-hybrid-draft-loader/patch_model_loader.py) so the build is
# self-contained and the file is folded into the sm121 build checksum.
COPY patches/patch_model_loader.py /tmp/patch_model_loader.py

# Resolve the model_loader module path from Python itself (do not hardcode the
# dist-packages prefix), then check -> apply -> check, matching the mod's run.sh.
RUN set -eux; \
    TARGET="$(python3 -c 'import vllm, os; print(os.path.join(vllm.__path__[0], "model_executor/model_loader/__init__.py"))')"; \
    python3 /tmp/patch_model_loader.py --check "$TARGET"; \
    python3 /tmp/patch_model_loader.py "$TARGET"; \
    python3 /tmp/patch_model_loader.py --check "$TARGET"; \
    find "$(dirname "$TARGET")" -name '__pycache__' -type d -prune -exec rm -rf {} + 2>/dev/null || true; \
    rm -f /tmp/patch_model_loader.py
