# syntax=docker/dockerfile:1.7
# dsv41-flash + B12X MoE backend (Aiden's overlay #1, aidenle.com
# 2026-09-11): thin FROM wrapper over the sm121-built dsv41-flash:local
# image that adds the b12x kernel package (pip wheel, pure python + a
# small JIT-compiled native) and the persistent JIT cache env.
#
# What changes at runtime (recipe dsv41-flash-4x-b12x):
#   --moe-backend b12x          (vLLM selects B12X_MXFP4_MXFP8 experts)
#   B12X_CACHE_DIR (below)      (kernel JIT persists per node via the
#                                recipe's extra_mount, like the other JITs)
#
# Their measured effect (Aiden, boots B/C, same stack otherwise):
#   decode +9% (counting 81->89-93), prefill at 9K +22% (1,932 -> 2,362).

ARG BASE_IMAGE
FROM ${BASE_IMAGE}

ARG B12X_VERSION

# Persistent kernel-JIT location (the recipe bind-mounts a per-node dir).
ENV B12X_CACHE_DIR=/root/.cache/b12x

RUN set -eux; \
    pip install --no-cache-dir "b12x==${B12X_VERSION}"; \
    python3 -c "import b12x; print('b12x import OK')"; \
    # Pre-build the JIT-compiled native so first serves never hit the
    # compiler (mirror of the sparkring-shared bridge, but our image ships
    # a real /usr/local/cuda-13.0/lib64 with libcudart.so — no symlink hack).
    python3 - <<'PY'
import importlib
try:
    mod = importlib.import_module("b12x.loader._native")
    mod.load()
    print("b12x native pre-built")
except ModuleNotFoundError:
    print("b12x.loader._native not present in this b12x version — nothing to pre-build")
PY
