Skip to content

Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.

Container: split image into dependency, extension and code layers

YanissAmz81ab1c5669a9
diff --git a/doc/container.md b/doc/container.md
index 68232f2..d588437 100644
--- a/doc/container.md
+++ b/doc/container.md
@@ -27,7 +27,7 @@ docker build -f docker/Dockerfile --build-arg MODEL=glm -t kyojin-glm . # ca
docker build -f docker/Dockerfile --build-arg MODEL=mimo -t kyojin-mimo .
```
-No GPU needed. First build: 22 minutes on a 32-thread machine at 18 MB/s download (about 14 GB of wheels); the other two tags take 17 s. The torch wheel is a nightly, so two builds on different days can carry different versions. `podman build -f docker/Dockerfile` is untested.
+No GPU needed. First build: 12.5 minutes measured here (32 threads; about 14 GB of wheels; the extension alone takes 7). The other two tags take under a second: every layer but the last `MODEL` one is shared. A rebuild after a Python-only edit takes 3 s (only the last code layer, ~26 MB uncompressed, is re-pushed; users re-download MBs, not ~3 GB). Only a native-source edit (`exllamav3/exllamav3_ext/`, `setup.py`, `build.sh`, `tools/strix_halo/`) rebuilds the 136 MB extension layer. The torch and SDK versions are pinned in the Dockerfile (`TORCH_VERSION`, `ROCM_SDK_VERSION`), so two builds carry the same versions until the pins are bumped deliberately. `podman build -f docker/Dockerfile` is untested.
## Run with docker (tested)
diff --git a/docker/Dockerfile b/docker/Dockerfile
index 0b3db56..b6b71df 100644
--- a/docker/Dockerfile
+++ b/docker/Dockerfile
@@ -1,13 +1,27 @@
+# syntax=docker/dockerfile:1
# Kyojin server image for AMD Strix Halo (gfx1151). Works with docker and podman.
# docker build -f docker/Dockerfile -t kyojin-qwen . # default model: qwen
# docker build -f docker/Dockerfile --build-arg MODEL=glm -t kyojin-glm . # same layers, other baked defaults
# docker build -f docker/Dockerfile --build-arg MODEL=mimo -t kyojin-mimo .
# Follows the README Quickstart: ROCm torch + rocm-sdk-devel from AMD's gfx1151 index, requirements.txt,
# `rocm-sdk init`, tools/strix_halo/env.sh, build.sh. The models are NOT in the image: mount them.
+#
+# Layering (single stage, heaviest first so upgrades stay small):
+# A: OS + venv + pinned torch/rocm-sdk-devel + requirements.txt + `rocm-sdk init` + SDK prune.
+# Depends only on requirements.txt and the TORCH_/ROCM_ ARGs below. Python-source edits never touch it.
+# B: native extension only (setup.py/build.sh/pyproject.toml, exllamav3_ext sources, the two files
+# setup.py loads by path, build scripts). Rebuilds (~10 min) only when native sources change.
+# C: the rest of the tree (python, tools, docker/). Python-only releases change only this layer (MBs).
+# MODEL stays last: qwen/glm/mimo share every other layer.
FROM ubuntu:24.04
SHELL ["/bin/bash", "-c"]
-ENV DEBIAN_FRONTEND=noninteractive PIP_NO_CACHE_DIR=1 PYTHONUNBUFFERED=1
+ENV DEBIAN_FRONTEND=noninteractive PYTHONUNBUFFERED=1
+# Pinned nightly versions (same set as the tested 1.5 images): bump deliberately, then re-verify
+# `docker run --rm --entrypoint python IMG -c "import torch, exllamav3_ext"`.
+ARG TORCH_VERSION=2.12.0a0+rocm7.13.0a20260411
+ARG ROCM_SDK_VERSION=7.14.0a20260612
+ARG ROCM_INDEX_URL=https://rocm.nightlies.amd.com/v2/gfx1151/
# Single stage on purpose: a second stage would copy the ~14 GB tree again and double the disk peak.
# g++, libc6-dev, python3-dev stay: the servers compile a few HIP kernels (hipcc) and Triton kernels at start.
RUN apt-get update && apt-get install -y --no-install-recommends \
@@ -17,22 +31,39 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
WORKDIR /opt/kyojin
RUN python3 -m venv .venv
ENV PATH=/opt/kyojin/.venv/bin:$PATH
-COPY requirements.txt .
-COPY . .
-# One layer on purpose: the pip downloads and the unpruned SDK tree never reach an image layer.
-# The SDK (hipcc, headers) stays, pruned of test binaries, profiler data and client samples.
-RUN pip install --pre torch rocm-sdk-devel --index-url https://rocm.nightlies.amd.com/v2/gfx1151/ \
+# --- Layer A: heavy dependencies. Only requirements.txt and the version ARGs invalidate it. ---
+# pip downloads live in a BuildKit cache mount, never in an image layer; the unpruned SDK tree is pruned
+# in the same RUN. The SDK (hipcc, headers) stays, pruned of test binaries, profiler data and client samples.
+COPY requirements.txt ./
+RUN --mount=type=cache,target=/root/.cache/pip \
+ pip install --pre "torch==${TORCH_VERSION}" "rocm-sdk-devel==${ROCM_SDK_VERSION}" --index-url "${ROCM_INDEX_URL}" \
&& pip install -r requirements.txt \
&& rocm-sdk init \
&& export EXL3_ROCM_SDK=$(rocm-sdk path --root) \
- && source tools/strix_halo/env.sh \
- && ./build.sh \
- && python -c "import torch, exllamav3_ext" \
- && rm -rf build .rebuild.* .sdk-deps /root/.cache \
- && cd "$EXL3_ROCM_SDK" && rm -rf clients share/rocprofiler-sdk share/hipblaslt share/rocdecode share/rocsparse bin/*gtest* bin/*-test bin/*-bench bin/*-gemm-tune bin/*test*
+ && cd "$EXL3_ROCM_SDK" && rm -rf clients share/rocprofiler-sdk share/hipblaslt share/rocdecode share/rocsparse bin/*gtest* bin/*-test bin/*-bench bin/*-gemm-tune bin/*test* \
+ && rm -rf /tmp/*
ENV EXL3_ROCM_SDK=/opt/kyojin/.venv/lib/python3.12/site-packages/_rocm_sdk_devel \
EXL3_HIPCC=/opt/kyojin/.venv/lib/python3.12/site-packages/_rocm_sdk_devel/bin/hipcc \
EXL3_HSA_LIB=/opt/kyojin/.venv/lib/python3.12/site-packages/_rocm_sdk_devel/lib/libhsa-runtime64.so.1
+# --- Layer B: native extension. Only native sources and build scripts invalidate it. ---
+# setup.py loads exllamav3/exllamav3_ext/build_config.py and exllamav3/util/arch_list.py by file path
+# (no top-level exllamav3 import) and reads the version from exllamav3/version.py; build.sh runs
+# tools/strix_halo/rebuild.sh, which sources tools/strix_halo/env.sh. Nothing else is read at build time.
+COPY setup.py build.sh pyproject.toml ./
+COPY exllamav3/exllamav3_ext/ ./exllamav3/exllamav3_ext/
+COPY exllamav3/version.py ./exllamav3/version.py
+COPY exllamav3/__init__.py ./exllamav3/__init__.py
+COPY exllamav3/util/arch_list.py ./exllamav3/util/arch_list.py
+COPY exllamav3/util/__init__.py ./exllamav3/util/__init__.py
+COPY tools/strix_halo/env.sh tools/strix_halo/rebuild.sh ./tools/strix_halo/
+RUN --mount=type=cache,target=/root/.cache/pip \
+ source tools/strix_halo/env.sh \
+ && ./build.sh \
+ && python -c "import torch, exllamav3_ext" \
+ && rm -rf build .rebuild.* .sdk-deps /tmp/*
+# --- Layer C: everything else (python, tools, docker/). Python-only edits land here. ---
+COPY . ./
+RUN python -c "import torch, exllamav3_ext"
# Last on purpose: the only layer that differs between the qwen, glm and mimo images. Defaults per model live in
# docker/kyojin_args.py (context, sampling, hub repository); MODEL only picks which set the image starts with.
ARG MODEL=qwen