build.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env bash
  2. # Build GLM-5.2 NF3-Hybrid Docker image for DGX Spark (aarch64/sm_121a)
  3. #
  4. # Build harness: m9e/blackwell-llm-docker → build-and-copy.sh
  5. # https://github.com/m9e/blackwell-llm-docker
  6. #
  7. # Pins:
  8. # vLLM: local-inference-lab/vllm @ dev/eldritch-enlightenment 45c1582e9b80ba83e71c3a6458e71da4736fbdc4
  9. # b12x: voipmonitor/b12x @ f3686b555d639823b276c2080f173145eed7f007
  10. # CUDA: 13.2 / torch 2.12.0+cu132
  11. # arch: TORCH_CUDA_ARCH_LIST=12.1a, CUTE_DSL_ARCH=sm_121a
  12. # FlashInfer: 0.6.14 aarch64 wheels
  13. #
  14. # Usage:
  15. # cd recipes/glm52-nf3-hybrid
  16. # ./build.sh [--tag vllm-nf3-hybrid:probe]
  17. set -euo pipefail
  18. TAG="${1:-vllm-nf3-hybrid:probe}"
  19. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  20. # ── Prerequisites ──────────────────────────────────────────────────────────
  21. command -v git >/dev/null 2>&1 || { echo "Need git"; exit 1; }
  22. command -v docker >/dev/null 2>&1 || { echo "Need docker"; exit 1; }
  23. BUILD_ROOT="$(mktemp -d)"
  24. trap 'rm -rf "$BUILD_ROOT"' EXIT
  25. echo "==> Build root: $BUILD_ROOT"
  26. # ── Clone build harness ────────────────────────────────────────────────────
  27. HARNESS_REPO="https://github.com/m9e/blackwell-llm-docker"
  28. echo "==> Cloning build harness from $HARNESS_REPO ..."
  29. git clone --depth=1 "$HARNESS_REPO" "$BUILD_ROOT/harness"
  30. # ── Build base image using build-and-copy.sh ───────────────────────────────
  31. # build-and-copy.sh clones vLLM and b12x, applies patches, builds, and
  32. # copies the artifacts (wheels + venv overlay) into a Docker image.
  33. echo "==> Building base image with build-and-copy.sh ..."
  34. cd "$BUILD_ROOT/harness"
  35. # The harness vLLM ref includes NF3-hybrid loader, DCP ag_rs dispatch,
  36. # low-bit MLA KV-cache dtypes (nvfp4_ds_mla, nf3_ds_mla), MTP/spec-decode
  37. # cherries, and the patched b12x_sparse_indexer_warmup.py.
  38. ./build-and-copy.sh \
  39. --vllm-repo "https://github.com/local-inference-lab/vllm" \
  40. --vllm-ref "45c1582e9b80ba83e71c3a6458e71da4736fbdc4" \
  41. --b12x-repo "https://github.com/voipmonitor/b12x" \
  42. --b12x-ref "f3686b555d639823b276c2080f173145eed7f007" \
  43. --gpu-arch "12.1a" \
  44. -t "$TAG" \
  45. --clean
  46. # The harness emits a Docker image tagged $TAG (no --push flag; local only).
  47. # ── Bake NF3 v2 overlay + CuteDSL runtime ─────────────────────────────────
  48. echo "==> Baking NF3 v2 overlay into $TAG ..."
  49. # Create a Dockerfile that adds the layer on top of the base.
  50. cat > "$BUILD_ROOT/Dockerfile.bake" << 'DOCKERFILE'
  51. ARG BASE_TAG
  52. FROM ${BASE_TAG}
  53. # NF3 v2 overlay files (hybrid loader, NF3/NVFP4 kernels, replan, mxfp8 tier)
  54. COPY overlay/hybrid_loader.py /usr/local/lib/python3.12/dist-packages/
  55. COPY overlay/nf3_kernel.py /usr/local/lib/python3.12/dist-packages/
  56. COPY overlay/nvfp4_kernel.py /usr/local/lib/python3.12/dist-packages/
  57. COPY overlay/nf3_replan.py /usr/local/lib/python3.12/dist-packages/
  58. COPY overlay/mxfp8_tier.json /usr/local/lib/python3.12/dist-packages/
  59. # Install CuteDSL runtime (NF3 kernel dependency)
  60. RUN pip install --no-cache-dir \
  61. nvidia-cutlass-dsl==4.5.2 \
  62. nvidia-cutlass-dsl-libs-cu13==4.5.2
  63. # Install FlashInfer 0.6.14 aarch64 wheel
  64. # Build from source for aarch64/sm_121a or use published wheel.
  65. # Published aarch64 wheels are available at flashinfer.ai/whl.
  66. RUN pip install --no-cache-dir \
  67. --find-links https://flashinfer.ai/whl/cu124/flashinfer_pypi/ \
  68. flashinfer==0.6.14
  69. # Ensure CuteDSL .pth path hook is present
  70. RUN python3 -c "import cutlass; print(f'CuteDSL OK: {cutlass.__version__}')"
  71. # Set arch envs so JIT compile targets sm_121a
  72. ENV TORCH_CUDA_ARCH_LIST=12.1a
  73. ENV CUTE_DSL_ARCH=sm_121a
  74. ENV FLASHINFER_CUDA_ARCH_LIST=12.1a
  75. # Verify imports
  76. RUN python3 -c "
  77. from hybrid_loader import NF3HybridLoader
  78. from nf3_kernel import NF3Kernel
  79. from nvfp4_kernel import NVFP4Kernel
  80. from nf3_replan import NF3Replan
  81. print('NF3 hybrid overlay OK')
  82. "
  83. DOCKERFILE
  84. # Build the overlay layer
  85. docker build \
  86. -f "$BUILD_ROOT/Dockerfile.bake" \
  87. --build-arg BASE_TAG="$TAG" \
  88. -t "$TAG" \
  89. "$SCRIPT_DIR"
  90. echo "==> Done: $TAG"
  91. echo "To push: docker push $TAG (or use as local image)"