# syntax=docker/dockerfile:1.7
# GLM-5.3-Flash NVFP4 (LibertAIDAI weight-only NVFP4-A16) on DGX Spark
# (SM121 / GB10), TP=2.
#
# Base: vllm/vllm-openai:glm53-flash-arm64-cu130 — vLLM's day-0 per-model
# image (arm64/cu130, glm5_next arch support baked in). It CANNOT serve this
# model on GB10 stock: glm5_next's NoPE-MLA (qk_rope_head_dim=0) + KDA linear
# attention + the sparse kpool indexer fail every attention backend the sm_121
# selector offers, and the auto-selected NVFP4 MoE backend silently corrupts
# output (degenerate token loops, zero errors).
#   * --moe-backend marlin (runtime, in the recipe) fixes the MoE corruption.
#   * The 4-part patch below fixes the MLA.
#
# The patch (patches/patch_mla.py) dresses GLM's NoPE-MLA (kv_lora 512, rope 0)
# in DeepSeek's fp8_ds_mla 512+64 layout and compacts the sm120 sparse-MLA
# topk table from 2176 to the compiled 2048. It is applied at build time;
# runtime activates the NoPE-pad path via VLLM_MLA_NOPE_PAD_ROPE=1 (recipe env).
#
# Provenance + measured numbers (24.74 code / 30.30 structured / 19.58 prose
# tok/s at MTP-5, 2x Spark, 256K needle-exact):
#   https://github.com/kingjones30/GLM-5.3-Flash-2x-DGX-Spark
#   https://forums.developer.nvidia.com/t/381433

ARG BASE_IMAGE
FROM ${BASE_IMAGE}

COPY patches/patch_mla.py /tmp/patch_mla.py
RUN set -eux; \
    python3 /tmp/patch_mla.py; \
    rm -f /tmp/patch_mla.py
