This commit is contained in:
hailin 2025-07-01 11:46:41 +08:00
parent db00790ccb
commit d3bb573d22
1 changed files with 62 additions and 31 deletions

View File

@ -1,55 +1,86 @@
############################################################################### ###############################################################################
# Stage 0 — builderCUDA 12.6.1 + nvcc/gcc用来 # Stage 0 ─ builder-torch编译 PyTorch 2.7.1 (+cu126)
# 1) 下载 torch-2.7.1+cu124 / torchvision-0.22.1+cu124 wheel
# 2) 下载 flashinfer-python (torch2.7/cu124) wheel
# 3) 编译本地 sglang 源码并打 wheel
############################################################################### ###############################################################################
ARG CUDA_VERSION=12.6.1 ARG CUDA_VERSION=12.6.1
ARG CUINDEX=124 # 使用 cu124 官方 wheel FROM nvidia/cuda:${CUDA_VERSION}-devel-ubuntu22.04 AS builder-torch
FROM nvidia/cuda:${CUDA_VERSION}-devel-ubuntu22.04 AS builder
ARG MAX_JOBS=24 # 按机器核心数调整
ENV DEBIAN_FRONTEND=noninteractive \ ENV DEBIAN_FRONTEND=noninteractive \
PYTHONUNBUFFERED=1 LANG=C.UTF-8 LC_ALL=C.UTF-8 PYTHONUNBUFFERED=1 LANG=C.UTF-8 LC_ALL=C.UTF-8 \
USE_CUDA=1 USE_DISTRIBUTED=0 BUILD_TEST=0 TORCH_CUDA_ARCH_LIST="7.5;8.0;8.6;9.0"
RUN apt-get update && apt-get install -y --no-install-recommends \ RUN apt-get update && apt-get install -y --no-install-recommends \
python3 python3-pip python3-distutils build-essential git ca-certificates && \ python3 python3-pip python3-distutils \
python3 -m pip install --no-cache-dir --upgrade pip setuptools wheel git cmake ninja-build \
libopenblas-dev libopenmpi-dev libnccl-dev \
libjpeg-dev libpng-dev ca-certificates && \
python3 -m pip install --no-cache-dir --upgrade pip wheel setuptools sympy
# ---- ① 预下载 PyTorch / TorchVision cu124 wheel -------------------------- WORKDIR /opt
RUN python3 -m pip download --no-deps -d /tmp/wheels \ RUN git clone --recursive -b v2.7.1 https://github.com/pytorch/pytorch.git
--index-url https://download.pytorch.org/whl/cu${CUINDEX} \
torch==2.7.1 torchvision==0.22.1
# ---- ② 预下载 flashinfer (torch2.7 / cu124) ------------------------------ WORKDIR /opt/pytorch
RUN python3 -m pip download --no-deps -d /tmp/wheels \ ENV MAX_JOBS=${MAX_JOBS}
--find-links https://flashinfer.ai/whl/cu${CUINDEX}/torch2.7/flashinfer-python \ RUN python3 setup.py bdist_wheel # ≈5060 min 首编
flashinfer-python
# ---- ③ 安装依赖,供后续 sglang 编译 --------------------------------------
RUN python3 -m pip install --no-cache-dir /tmp/wheels/*
# ---- ④ COPY 本地 sglang 源码并编 wheel ------------------------------------
COPY ./sglang /sgl-workspace/sglang
WORKDIR /sgl-workspace/sglang/python
RUN python3 -m pip wheel '.[srt,openai]' --no-deps -w /tmp/wheels
############################################################################### ###############################################################################
# Stage 1 — runtime极简运行镜像无编译链离线装 wheel # Stage 1 ─ builder-extras用自编 Torch 装 TV / flashinfer / sglang并收集轮子
###############################################################################
ARG CUDA_VERSION=12.6.1
FROM nvidia/cuda:${CUDA_VERSION}-devel-ubuntu22.04 AS builder-extras
ENV DEBIAN_FRONTEND=noninteractive PYTHONUNBUFFERED=1 LANG=C.UTF-8 LC_ALL=C.UTF-8
RUN apt-get update && apt-get install -y --no-install-recommends \
python3 python3-pip python3-distutils \
git build-essential cmake ninja-build \
libjpeg-dev libpng-dev ca-certificates && \
python3 -m pip install --no-cache-dir --upgrade pip wheel setuptools
# ── 安装自编 torch 轮子 ──────────────────────────────────────────────────────
COPY --from=builder-torch /opt/pytorch/dist/torch-2.7.1+cu126*.whl /tmp/
RUN python3 -m pip install --no-cache-dir /tmp/torch-2.7.1+cu126*.whl && rm /tmp/*.whl
# ── 编译 torchvision 0.22.1 (依赖本地 torch) ────────────────────────────────
WORKDIR /opt
RUN git clone -b v0.22.1 https://github.com/pytorch/vision.git
WORKDIR /opt/vision
RUN python3 setup.py bdist_wheel
# ── 编译 flashinfer (主分支支持 torch 2.7 / cu126) ─────────────────────────
WORKDIR /opt
RUN git clone https://github.com/Dao-AILab/flashinfer.git
WORKDIR /opt/flashinfer/python
RUN python3 setup.py bdist_wheel
# ── 编译你本地 sglang 源码并打 wheel ───────────────────────────────────────
COPY ./sglang /sgl/sglang
WORKDIR /sgl/sglang/python
RUN python3 -m pip install ".[srt,openai]" --no-build-isolation && \
python3 -m pip wheel ".[srt,openai]" --no-deps -w /tmp/sg_wheels
# ── 收集所有 wheel 到 /wheels ──────────────────────────────────────────────
RUN mkdir -p /wheels && \
cp /opt/pytorch/dist/torch-2.7.1+cu126*.whl /wheels/ && \
cp /opt/vision/dist/torchvision-0.22.1+cu126*.whl /wheels/ && \
cp /opt/flashinfer/python/dist/flashinfer_python-*.whl /wheels/ && \
cp /tmp/sg_wheels/sglang-*.whl /wheels/
###############################################################################
# Stage 2 ─ runtime极简运行镜像仅离线安装 wheel
############################################################################### ###############################################################################
ARG CUDA_VERSION=12.6.1 ARG CUDA_VERSION=12.6.1
FROM nvidia/cuda:${CUDA_VERSION}-runtime-ubuntu22.04 FROM nvidia/cuda:${CUDA_VERSION}-runtime-ubuntu22.04
ENV DEBIAN_FRONTEND=noninteractive \ ENV DEBIAN_FRONTEND=noninteractive PYTHONUNBUFFERED=1 LANG=C.UTF-8 LC_ALL=C.UTF-8
PYTHONUNBUFFERED=1 LANG=C.UTF-8 LC_ALL=C.UTF-8
RUN apt-get update && apt-get install -y --no-install-recommends \ RUN apt-get update && apt-get install -y --no-install-recommends \
python3 python3-pip python3-distutils ca-certificates && \ python3 python3-pip python3-distutils ca-certificates && \
rm -rf /var/lib/apt/lists/* && \ rm -rf /var/lib/apt/lists/* && \
python3 -m pip install --no-cache-dir --upgrade pip python3 -m pip install --no-cache-dir --upgrade pip
# ---- 一次性离线安装 torch / TV / flashinfer / sglang ---------------------- COPY --from=builder-extras /wheels /tmp/wheels
COPY --from=builder /tmp/wheels /tmp/wheels
RUN python3 -m pip install --no-cache-dir /tmp/wheels/* && rm -rf /tmp/wheels RUN python3 -m pip install --no-cache-dir /tmp/wheels/* && rm -rf /tmp/wheels
# ---- 仅打印帮助CPU 机器也能跑) ---------------------------------------- # 仅做 CI / CPU 自检 —— 输出帮助后退出 0
CMD ["python3", "-m", "sglang.launch_server", "--help"] CMD ["python3", "-m", "sglang.launch_server", "--help"]