|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -eo pipefail |
| 3 | +trap 'echo "[kimi-k3-efa] FATAL: setup failed at line $LINENO: $BASH_COMMAND" >&2' ERR |
| 4 | + |
| 5 | +source /infmax-workspace/benchmarks/check_env.sh |
| 6 | +check_env_vars KIMI_EFA_BUILD_CACHE LD_LIBRARY_PATH KV_OFFLOAD_BACKEND_VERSION |
| 7 | + |
| 8 | +efa_version=1.51.0 |
| 9 | +# Same digest as vLLM docker/Dockerfile.efa EFA_INSTALLER_SHA256. |
| 10 | +efa_sha256=83993044fa4df3dc2f8b5c2f7051d61b9689441be023a2c92ea51d3f53f2465c |
| 11 | +mc_rev=b46254dea9515ea6cc05fe4ec220545835582edd # commit of kvcache-ai/Mooncake tag v0.3.14-pre |
| 12 | + |
| 13 | +mc_version="0.3.14.dev0+g${mc_rev:0:8}" |
| 14 | +if [[ "$KV_OFFLOAD_BACKEND_VERSION" != "$mc_version" ]]; then |
| 15 | + echo "[kimi-k3-efa] FATAL: the master config's Mooncake ${KV_OFFLOAD_BACKEND_VERSION} is not the built ${mc_version}" >&2 |
| 16 | + exit 1 |
| 17 | +fi |
| 18 | +cache="$KIMI_EFA_BUILD_CACHE/mooncake-${mc_rev:0:8}-efa${efa_version}-v1" |
| 19 | +echo "[kimi-k3-efa] Mooncake ${mc_version}, cache ${cache}" |
| 20 | + |
| 21 | +export DEBIAN_FRONTEND=noninteractive |
| 22 | +# Mooncake build dependencies, plus the runtime libraries its wheel leaves to the system |
| 23 | +# (libibverbs, libnuma, libhwloc, libcurl, libnl). libibverbs-dev also brings libnl-3 and |
| 24 | +# libnl-route-3, which the installer's dpkg-only libfabric install needs, and libhwloc15 is the |
| 25 | +# only dependency of the NGC aws-ofi-nccl package. The NVIDIA CUDA apt mirror is intermittently |
| 26 | +# mid-sync (index size mismatch), so both the index update and the install are retried. |
| 27 | +apt_ok=0 |
| 28 | +for attempt in 1 2 3 4 5; do |
| 29 | + if apt-get update -qq && |
| 30 | + apt-get install -y -qq --no-install-recommends \ |
| 31 | + build-essential ca-certificates curl git libboost-filesystem-dev libboost-system-dev \ |
| 32 | + libcurl4-openssl-dev libgflags-dev libgoogle-glog-dev libhwloc15 libhwloc-dev libibverbs-dev \ |
| 33 | + libjsoncpp-dev libmsgpack-dev libnuma-dev libprotobuf-dev libssl-dev libxxhash-dev \ |
| 34 | + libyaml-cpp-dev libzstd-dev numactl patchelf pkg-config protobuf-compiler \ |
| 35 | + python-is-python3 cuda-nvml-dev-13-0; then |
| 36 | + apt_ok=1 |
| 37 | + break |
| 38 | + fi |
| 39 | + echo "[kimi-k3-efa] apt attempt $attempt failed" >&2 |
| 40 | + sleep $((attempt * 15)) |
| 41 | +done |
| 42 | +if [[ "$apt_ok" != 1 ]]; then |
| 43 | + echo "[kimi-k3-efa] FATAL: apt-get failed 5 times" >&2 |
| 44 | + exit 1 |
| 45 | +fi |
| 46 | + |
| 47 | +task_dir=$(mktemp -d /tmp/kimi-k3-efa.XXXXXX) |
| 48 | + |
| 49 | +# cached_wheel <url> <sha256>: one container downloads the wheel into the shared cache under a lock and keeps it |
| 50 | +# once its digest matches; the others wait and find the verified file. Prints the cached path. |
| 51 | +cached_wheel() { |
| 52 | + local url=$1 sha256=$2 |
| 53 | + local name=${url##*/} |
| 54 | + local dir="$KIMI_EFA_BUILD_CACHE/wheels" |
| 55 | + mkdir -p "$dir" |
| 56 | + exec 6>"$dir/$name.lock" |
| 57 | + flock 6 |
| 58 | + for attempt in 1 2 3 4 5; do |
| 59 | + if printf '%s %s\n' "$sha256" "$dir/$name" | sha256sum -c --status - 2>/dev/null; then |
| 60 | + break |
| 61 | + fi |
| 62 | + curl -fsSL --retry 3 --retry-all-errors "$url" -o "$task_dir/$name" || true |
| 63 | + if printf '%s %s\n' "$sha256" "$task_dir/$name" | sha256sum -c --status -; then |
| 64 | + mv -f "$task_dir/$name" "$dir/$name.tmp.${HOSTNAME}.$$" |
| 65 | + mv -f "$dir/$name.tmp.${HOSTNAME}.$$" "$dir/$name" |
| 66 | + else |
| 67 | + echo "[kimi-k3-efa] $name failed SHA256 check (attempt $attempt)" >&2 |
| 68 | + sleep $((attempt * 10)) |
| 69 | + fi |
| 70 | + done |
| 71 | + printf '%s %s\n' "$sha256" "$dir/$name" | sha256sum -c - >&2 || return 1 |
| 72 | + flock -u 6 |
| 73 | + echo "$dir/$name" |
| 74 | +} |
| 75 | +# The installer tarball is kept in the shared cache once its digest matches. One container downloads it |
| 76 | +# under the lock; the others wait and then find the verified file, which is never replaced afterwards. |
| 77 | +efa_dl="$KIMI_EFA_BUILD_CACHE/dl" |
| 78 | +efa_tgz="$efa_dl/aws-efa-installer-${efa_version}.tar.gz" |
| 79 | +mkdir -p "$efa_dl" |
| 80 | +exec 7>"$efa_dl/aws-efa-installer-${efa_version}.lock" |
| 81 | +flock 7 |
| 82 | +if ! printf '%s %s\n' "$efa_sha256" "$efa_tgz" | sha256sum -c --status - 2>/dev/null; then |
| 83 | + curl -fsSL --retry 5 --retry-all-errors \ |
| 84 | + "https://efa-installer.amazonaws.com/aws-efa-installer-${efa_version}.tar.gz" \ |
| 85 | + -o "$task_dir/efa.tar.gz" |
| 86 | + printf '%s %s\n' "$efa_sha256" "$task_dir/efa.tar.gz" | sha256sum -c - |
| 87 | + mv -f "$task_dir/efa.tar.gz" "$efa_tgz.tmp.${HOSTNAME}.$$" |
| 88 | + mv -f "$efa_tgz.tmp.${HOSTNAME}.$$" "$efa_tgz" |
| 89 | +fi |
| 90 | +flock -u 7 |
| 91 | +printf '%s %s\n' "$efa_sha256" "$efa_tgz" | sha256sum -c - |
| 92 | +tar -xzf "$efa_tgz" -C "$task_dir" |
| 93 | +# Same flags as vLLM docker/Dockerfile.efa: libfabric (with headers and fi_info), the NGC |
| 94 | +# aws-ofi-nccl plugin, no rdma-core, no MPI. --build-ngc implies --skip-kmod --skip-limit-conf |
| 95 | +# --no-verify --yes. |
| 96 | +( |
| 97 | + cd "$task_dir/aws-efa-installer" |
| 98 | + ./efa_installer.sh --skip-mpi --skip-rdma-core --build-ngc |
| 99 | +) |
| 100 | +ldconfig |
| 101 | +# The NGC plugin package has no libnccl-net.so alias; the disaggregated recipe sets NCCL_NET_PLUGIN=none. |
| 102 | +echo "[kimi-k3-efa] /opt/amazon/ofi-nccl/lib: $(ls /opt/amazon/ofi-nccl/lib 2>/dev/null | paste -sd' ' -)" |
| 103 | + |
| 104 | +mkdir -p "$cache" |
| 105 | +exec 9>"$cache/build.lock" |
| 106 | +flock 9 |
| 107 | +if [ ! -f "$cache/ready" ]; then |
| 108 | + python3 -m pip install --no-cache-dir \ |
| 109 | + 'cmake==4.4.4' 'build==1.6.1' 'auditwheel==6.8.2' \ |
| 110 | + 'wheel==0.48.0' 'setuptools==84.0.0' 'pybind11==3.1.0' |
| 111 | + git clone https://github.com/kvcache-ai/Mooncake.git "$task_dir/Mooncake" |
| 112 | + git -C "$task_dir/Mooncake" checkout --detach "$mc_rev" |
| 113 | + git -C "$task_dir/Mooncake" submodule update --init --recursive |
| 114 | + ( |
| 115 | + cd "$task_dir/Mooncake" |
| 116 | + # Since this tag the EFA transport dlopens libfabric at run time, so the build only needs the |
| 117 | + # installer's headers. |
| 118 | + cmake -S . -B build -DCMAKE_BUILD_TYPE=Release \ |
| 119 | + -DCMAKE_BUILD_WITH_INSTALL_RPATH=ON '-DCMAKE_INSTALL_RPATH=$ORIGIN' \ |
| 120 | + -DCMAKE_PREFIX_PATH=/opt/amazon/efa \ |
| 121 | + -DPython3_EXECUTABLE="$(command -v python3)" \ |
| 122 | + -DUSE_EFA=ON -DUSE_CUDA=ON -DUSE_ETCD=OFF \ |
| 123 | + -DWITH_STORE_RUST=OFF -DWITH_EP=OFF -DBUILD_UNIT_TESTS=OFF \ |
| 124 | + -DBUILD_BENCHMARK=OFF -DBUILD_EXAMPLES=ON |
| 125 | + cmake --build build --parallel 12 --target \ |
| 126 | + engine store mooncake_master mooncake_client transfer_engine_bench build_nvlink_allocator |
| 127 | + sed -i "s/^version = .*/version = \"${mc_version}\"/" mooncake-wheel/pyproject.toml |
| 128 | + mkdir -p "$task_dir/wheels" |
| 129 | + EFA_CU13_BUILD=1 OUTPUT_DIR="$task_dir/wheels" bash scripts/build_wheel.sh |
| 130 | + ) |
| 131 | + # Exactly one wheel, and nothing left over from an earlier failed attempt in this cache. |
| 132 | + wheels=("$task_dir/wheels/"*.whl) |
| 133 | + if [[ ${#wheels[@]} -ne 1 || ! -f "${wheels[0]}" ]]; then |
| 134 | + echo "[kimi-k3-efa] FATAL: expected one wheel, got: ${wheels[*]}" >&2 |
| 135 | + exit 1 |
| 136 | + fi |
| 137 | + rm -f "$cache/"*.whl |
| 138 | + cp "${wheels[0]}" "$cache/" |
| 139 | + touch "$cache/ready" |
| 140 | +fi |
| 141 | +python3 -m pip uninstall -y \ |
| 142 | + mooncake-transfer-engine mooncake-transfer-engine-cuda13 mooncake-transfer-engine-efa-cuda13 |
| 143 | +python3 -m pip install --no-deps "$cache/"*.whl |
| 144 | +flock -u 9 |
| 145 | + |
| 146 | +nixl_version=1.5.2 |
| 147 | +# The cp312 wheel matches the image's Python 3.12. |
| 148 | +py_tag=$(python3 -c 'import sys; print(f"cp{sys.version_info[0]}{sys.version_info[1]}")') |
| 149 | +if [[ "$py_tag" != cp312 ]]; then |
| 150 | + echo "[kimi-k3-efa] FATAL: Python ${py_tag}, the pinned nixl-cu13 wheel is cp312" >&2 |
| 151 | + exit 1 |
| 152 | +fi |
| 153 | +nixl_whl=$(cached_wheel \ |
| 154 | + "https://files.pythonhosted.org/packages/31/90/cfcad1475c05c759a47f14f8edd30ebe77db2b7fae31bcdfa098fa2cfa46/nixl_cu13-${nixl_version}-cp312-cp312-manylinux_2_28_x86_64.whl" \ |
| 155 | + 591eda3f14b72769590cd71b6b46fc5e483ef1b5a90a29a8ff464310c9cf6af2) |
| 156 | +echo "[kimi-k3-efa] NIXL ${nixl_version} from PyPI ($(basename "$nixl_whl"))" |
| 157 | +python3 -m pip uninstall -y nixl-cu13 |
| 158 | +python3 -m pip install --no-deps "$nixl_whl" |
| 159 | + |
| 160 | +fi_version=$(/opt/amazon/efa/bin/fi_info --version) |
| 161 | +echo "$fi_version" |
| 162 | +if ! grep -qx 'libfabric: 2.7.0amzn2.0' <<<"$fi_version"; then |
| 163 | + echo "[kimi-k3-efa] FATAL: expected libfabric 2.7.0amzn2.0 from EFA installer ${efa_version}" >&2 |
| 164 | + exit 1 |
| 165 | +fi |
| 166 | +/opt/amazon/efa/bin/fi_info -p efa -t FI_EP_RDM |
| 167 | + |
| 168 | +# The libraries the Mooncake wheel leaves to the system must resolve. libcuda comes from the |
| 169 | +# driver and is absent in containers without a GPU, so it is not checked here. |
| 170 | +mc_dir=$(python3 -c 'import mooncake, os; print(os.path.dirname(mooncake.__file__))') |
| 171 | +missing=$( |
| 172 | + for so in "$mc_dir"/engine*.so "$mc_dir"/_store*.so "$mc_dir"/mooncake_master; do |
| 173 | + if [[ ! -e "$so" ]]; then |
| 174 | + echo "$(basename "$so"): not in the wheel" |
| 175 | + continue |
| 176 | + fi |
| 177 | + ldd "$so" | grep 'not found' | grep -v 'libcuda\.so' | sed "s|^|$(basename "$so"): |" |
| 178 | + done || true |
| 179 | +) |
| 180 | +if [[ -n "$missing" ]]; then |
| 181 | + echo "[kimi-k3-efa] FATAL: Mooncake wheel files or libraries missing:" >&2 |
| 182 | + echo "$missing" >&2 |
| 183 | + exit 1 |
| 184 | +fi |
| 185 | +# vLLM Dockerfile.efa checks that NIXL's LIBFABRIC plugin loads the EFA libfabric. |
| 186 | +nixl_plugin=$(find /usr/local/lib -path "*nixl_cu13*" -name libplugin_LIBFABRIC.so -print -quit) |
| 187 | +nixl_fabric="" |
| 188 | +if [[ -n "$nixl_plugin" ]]; then |
| 189 | + nixl_fabric=$(ldd "$nixl_plugin" | awk '$1 == "libfabric.so.1" {print $3}') |
| 190 | +fi |
| 191 | +if [[ -z "$nixl_plugin" || "$nixl_fabric" != /opt/amazon/efa/lib/* ]]; then |
| 192 | + echo "[kimi-k3-efa] FATAL: NIXL LIBFABRIC plugin ${nixl_plugin:-<none>} does not load the EFA libfabric (${nixl_fabric:-<none>})" >&2 |
| 193 | + exit 1 |
| 194 | +fi |
| 195 | +echo "[kimi-k3-efa] NIXL LIBFABRIC plugin ${nixl_plugin:-<none under /usr/local/lib>} -> ${nixl_fabric:-<no libfabric.so.1 entry>}" |
| 196 | + |
| 197 | +python3 - "$mc_version" "$cache" "$efa_version" "$nixl_version" <<'PY' |
| 198 | +import ctypes |
| 199 | +import importlib.metadata |
| 200 | +import sys |
| 201 | +
|
| 202 | +mc_version, cache, efa_version, nixl_version = sys.argv[1:] |
| 203 | +fabric = ctypes.CDLL("libfabric.so.1") |
| 204 | +fabric.fi_version.restype = ctypes.c_uint32 |
| 205 | +api = fabric.fi_version() |
| 206 | +assert api == (2 << 16 | 7), f"libfabric API {api >> 16}.{api & 0xFFFF}, expected 2.7" |
| 207 | +with open("/proc/self/maps") as maps: |
| 208 | + path = next(line.split()[-1] for line in maps if "libfabric.so" in line) |
| 209 | +assert path.startswith("/opt/amazon/efa/lib/"), f"libfabric.so.1 loaded from {path}" |
| 210 | +with open(path, "rb") as lib: |
| 211 | + # libfabric 2.7.0amzn2.0 sets CU_CTX_SYNC_MEMOPS on the primary context when a VMM |
| 212 | + # allocation rejects the pointer attribute; 2.6 had neither the string nor the call. |
| 213 | + assert b"CU_CTX_SYNC_MEMOPS" in lib.read(), f"{path} lacks the CUDA VMM sync-memops fallback" |
| 214 | +installed = importlib.metadata.version("mooncake-transfer-engine-efa-cuda13") |
| 215 | +assert installed == mc_version, f"mooncake-transfer-engine-efa-cuda13 {installed}, expected {mc_version}" |
| 216 | +from mooncake.store import MooncakeDistributedStore, ReplicateConfig # noqa: E402,F401 |
| 217 | +import mooncake.engine # noqa: E402,F401 |
| 218 | +
|
| 219 | +nixl = importlib.metadata.version("nixl-cu13") |
| 220 | +assert nixl == nixl_version, f"nixl-cu13 {nixl}, expected {nixl_version}" |
| 221 | +from nixl._api import nixl_agent, nixl_agent_config # noqa: E402,F401 |
| 222 | +print(f"[kimi-k3-efa] ACTIVE efa-installer {efa_version} libfabric 2.7 ({path}) " |
| 223 | + f"mooncake {installed} nixl {nixl} cache {cache}", flush=True) |
| 224 | +PY |
0 commit comments