[CI] Add prebuild command option, set prebuild command option for CI to build flash attention (#156236)

Build flash attention separately in build using 2 jobs since it OOMs on more, then the rest of the job uses 6
Pull Request resolved: https://github.com/pytorch/pytorch/pull/156236
Approved by: https://github.com/malfet
This commit is contained in:
Catherine Lee
2025-06-27 11:09:08 -07:00
committed by PyTorch MergeBot
parent 3ed4384f5b
commit f40efde2a4
2 changed files with 19 additions and 4 deletions

View File

@ -198,10 +198,8 @@ fi
# We only build FlashAttention files for CUDA 8.0+, and they require large amounts of # We only build FlashAttention files for CUDA 8.0+, and they require large amounts of
# memory to build and will OOM # memory to build and will OOM
if [[ "$BUILD_ENVIRONMENT" == *cuda* ]] && [[ 1 -eq $(echo "${TORCH_CUDA_ARCH_LIST} >= 8.0" | bc) ]] && [ -z "$MAX_JOBS_OVERRIDE" ]; then if [[ "$BUILD_ENVIRONMENT" == *cuda* ]] && [[ 1 -eq $(echo "${TORCH_CUDA_ARCH_LIST} >= 8.0" | bc) ]]; then
echo "WARNING: FlashAttention files require large amounts of memory to build and will OOM" export BUILD_CUSTOM_STEP="ninja -C build flash_attention -j 2"
echo "Setting MAX_JOBS=(nproc-2)/3 to reduce memory usage"
export MAX_JOBS="$(( $(nproc --ignore=2) / 3 ))"
fi fi
if [[ "${BUILD_ENVIRONMENT}" == *clang* ]]; then if [[ "${BUILD_ENVIRONMENT}" == *clang* ]]; then

View File

@ -2,6 +2,7 @@ from __future__ import annotations
import os import os
import platform import platform
import subprocess
from .optional_submodules import checkout_nccl from .optional_submodules import checkout_nccl
from .setup_helpers.cmake import CMake, USE_NINJA from .setup_helpers.cmake import CMake, USE_NINJA
@ -98,4 +99,20 @@ def build_pytorch(
) )
if cmake_only: if cmake_only:
return return
build_custom_step = os.getenv("BUILD_CUSTOM_STEP")
if build_custom_step:
try:
output = subprocess.check_output(
build_custom_step,
shell=True,
stderr=subprocess.STDOUT,
text=True,
)
print("Command output:")
print(output)
except subprocess.CalledProcessError as e:
print("Command failed with return code:", e.returncode)
print("Output (stdout and stderr):")
print(e.output)
raise
cmake.build(my_env) cmake.build(my_env)