Skip to content

Commit 5a57f43

Browse files
committed
fix
1 parent 2cc5a5b commit 5a57f43

File tree

1 file changed

+54
-14
lines changed

1 file changed

+54
-14
lines changed

Dockerfile

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ RUN chmod 777 -R /tmp && apt-get update && DEBIAN_FRONTEND=noninteractive apt-ge
1717
git \
1818
cmake \
1919
ninja-build \
20-
build-essential && \
20+
build-essential \
21+
ccache && \
2122
rm -rf /var/lib/apt/lists/*
2223

2324
# Install Mambaforge
@@ -43,8 +44,16 @@ WORKDIR /workspace
4344
# Install PyTorch with CUDA support
4445
RUN pip install torch==2.7.1
4546

46-
# Install build dependencies
47-
RUN pip install --upgrade pip setuptools wheel build scikit-build-core[pyproject] pybind11 ninja
47+
# Install build dependencies + 构建加速工具
48+
RUN pip install --upgrade pip setuptools wheel build scikit-build-core[pyproject] pybind11 ninja psutil
49+
50+
# 🚀 设置ccache编译缓存加速
51+
ENV CCACHE_DIR=/tmp/ccache \
52+
CCACHE_MAXSIZE=2G \
53+
CCACHE_COMPRESS=true \
54+
CC="ccache gcc" \
55+
CXX="ccache g++"
56+
RUN ccache --set-config=max_size=2G
4857

4958
# Copy source code to container
5059
COPY . .
@@ -59,40 +68,71 @@ RUN python -c "import torch; print(f'PyTorch installed at: {torch.__path__[0]}')
5968
ENV FLASH_ATTENTION_FORCE_BUILD=TRUE \
6069
FLASH_ATTENTION_DISABLE_BACKWARD=TRUE \
6170
CUDA_HOME=/usr/local/cuda \
62-
CUDA_ROOT=/usr/local/cuda
71+
CUDA_ROOT=/usr/local/cuda \
72+
CCACHE_DISABLE=0
6373

6474
# 🎯 关键修复:设置 CMAKE_PREFIX_PATH 让 CMake 找到 PyTorch
6575
RUN TORCH_CMAKE_PATH=$(python -c "import torch; print(torch.utils.cmake_prefix_path)") && \
6676
echo "export CMAKE_PREFIX_PATH=$TORCH_CMAKE_PATH:\$CMAKE_PREFIX_PATH" >> ~/.bashrc && \
6777
echo "CMAKE_PREFIX_PATH=$TORCH_CMAKE_PATH" >> /etc/environment
6878

79+
# 🚀 GitHub Actions优化:智能设置并行度(针对2核7GB限制)
80+
RUN python -c "
81+
import os, psutil
82+
# GitHub Actions runner: 2核心,7GB内存
83+
cpu_cores = min(2, os.cpu_count())
84+
available_memory_gb = min(7, psutil.virtual_memory().available / (1024**3))
85+
# 保守策略:每个job约3GB内存
86+
memory_jobs = max(1, int(available_memory_gb / 3))
87+
# 选择安全的并行度
88+
optimal_jobs = min(cpu_cores, memory_jobs, 2)
89+
nvcc_threads = optimal_jobs
90+
print(f'🎯 CI优化: MAX_JOBS={optimal_jobs}, NVCC_THREADS={nvcc_threads}')
91+
print(f'💾 估算资源: {available_memory_gb:.1f}GB, {cpu_cores}核')
92+
with open('/etc/environment', 'a') as f:
93+
f.write(f'MAX_JOBS={optimal_jobs}\n')
94+
f.write(f'NVCC_THREADS={nvcc_threads}\n')
95+
"
96+
6997
# Create output directory
7098
RUN mkdir -p /out
7199

100+
101+
72102
# Build lightllm-kernel package (main project)
73-
# 🎯 关键:在构建时设置 CMAKE_PREFIX_PATH,让 CMake 找到 PyTorch
74103
RUN echo "🔧 Building lightllm-kernel package..." && \
75104
echo "📋 Verifying PyTorch installation..." && \
76105
python -c "import torch; print(f'PyTorch version: {torch.__version__}'); print(f'CUDA available: {torch.cuda.is_available()}'); print(f'CMake prefix path: {torch.utils.cmake_prefix_path}')" && \
106+
eval $(cat /etc/environment | xargs -I {} echo export {}) && \
77107
TORCH_CMAKE_PATH=$(python -c "import torch; print(torch.utils.cmake_prefix_path)") && \
78108
echo "🔧 Setting CMAKE_PREFIX_PATH to: $TORCH_CMAKE_PATH" && \
109+
echo "🚀 Using optimized settings: MAX_JOBS=$MAX_JOBS, NVCC_THREADS=$NVCC_THREADS" && \
79110
CMAKE_PREFIX_PATH="$TORCH_CMAKE_PATH:$CMAKE_PREFIX_PATH" python -m build --wheel --outdir /out/ && \
80111
echo "✅ lightllm-kernel build completed"
81112

82-
# Build flash_attn_3 package (hopper)
83-
RUN echo "🔧 Building flash_attn_3 package..." && \
113+
# Build flash_attn_3 package (hopper) - 源码优化构建
114+
RUN echo "🔧 Building flash_attn_3 from source with optimizations..." && \
84115
cd flash-attention/hopper && \
85-
MAX_JOBS=1 NVCC_THREADS=2 FLASH_ATTN_CUDA_ARCHS="90" python setup.py bdist_wheel && \
116+
eval $(cat /etc/environment | xargs -I {} echo export {}) && \
117+
echo "🚀 Optimized settings: MAX_JOBS=$MAX_JOBS, NVCC_THREADS=$NVCC_THREADS" && \
118+
echo "⏰ GitHub Actions: Building within 6h time limit..." && \
119+
MAX_JOBS=$MAX_JOBS NVCC_THREADS=$NVCC_THREADS FLASH_ATTN_CUDA_ARCHS=90 python setup.py bdist_wheel && \
86120
cp dist/*.whl /out/ && \
87-
echo "✅ flash_attn_3 build completed"
121+
echo "✅ flash_attn_3 optimized source build completed"
122+
123+
# 显示编译缓存统计(如果可用)
124+
RUN ccache --show-stats 2>/dev/null || echo "💾 ccache stats not available"
88125

89-
# Verify all wheels are built
126+
# Verify all wheels are built (源码构建验证)
90127
RUN echo "📦 Final wheel packages:" && \
91128
ls -la /out/ && \
92129
WHEEL_COUNT=$(ls -1 /out/*.whl | wc -l) && \
93-
echo "Total wheels built: $WHEEL_COUNT" && \
130+
echo "🎯 Total wheels built: $WHEEL_COUNT" && \
94131
if [ "$WHEEL_COUNT" -ne 2 ]; then \
95-
echo "❌ Error: Expected 2 wheels, found $WHEEL_COUNT" && exit 1; \
132+
echo "❌ ERROR: Expected 2 wheels (lightllm-kernel + flash_attn_3), found $WHEEL_COUNT" && \
133+
echo "📋 Debug info:" && ls -la /out/ && \
134+
exit 1; \
96135
else \
97-
echo "✅ Successfully built all wheel packages"; \
98-
fi
136+
echo "🎉 SUCCESS: All wheels built from optimized source compilation!"; \
137+
fi && \
138+
echo "🕒 Optimized build completed within GitHub Actions time limit!"

0 commit comments

Comments
 (0)