diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6d86c06 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.vscode/ +SD/ +xformers/ +/*.whl diff --git a/README.md b/README.md new file mode 100644 index 0000000..0584fcc --- /dev/null +++ b/README.md @@ -0,0 +1,23 @@ +# xformers +다양한 Python 버전과 PyTorch 를 사용해 미리 컴파일한 [xformers](https://github.com/facebookresearch/xformers) 패키지 + +## 빌드 스크립트 +`docker.sh` 로 개별 빌드하거나 `docker-batch.sh` 로 여러 버전 빌드할 수 있음 + +도커 (또는 포드만) 없어도 `build.sh` 로 도커 없이도 돌릴 수 있긴 함 +데비안 계열 배포판 안쓰고 있거나 잡다한 패키지 깔리는거 싫으면 도커 강추 + +### 환경 변수 +- `TRACE` - `1` 값으로 설정해 `xtrace` 켜기 (기본 값: `0`) +- `PYTHON_VERSION` - 사용할 파이썬 버전 (기본 값: `3.8`) +- `PIP_TORCH_INDEX` - PyTorch 패키지 인덱스 주소 (기본 값: `https://download.pytorch.org/whl/cu116`) +- `PIP_TORCH_PACKAGE` - PyTorch 패키지 명 (기본 값: `torch`) +- `TORCH_CUDA_ARCH_LIST` - `xformers` 에서 컴파일할 목표 GPU 버전 (기본 값: `6.0;6.1;6.2;7.0;7.2;7.5;8.0;8.6`, [버전 목록](https://en.wikipedia.org/wiki/CUDA#GPUs_supported)) +- `NVCC_FLAGS` - NVCC 플래그 (기본 값: [`--use_fast_math -DXFORMERS_MEM_EFF_ATTENTION_DISABLE_BACKWARD`](https://github.com/facebookresearch/xformers/pull/482)) +- `MAX_JOBS` - `ninja` 에서 사용할 스레드 수 (기본 값: `현재 스레드 수 - 2`) +- `DOCKER_IMAGE` - 빌드에 사용할 도커 이미지 (기본 값: `docker.io/nvidia/cuda:11.6.0-devel-ubuntu18.04`) + +### 예시 +```sh +$ TRACE=1 PYTHON_VERSION="3.10" ./docker.sh && mv xformers/dist/*.whl . +``` diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..0f00d2b --- /dev/null +++ b/build.sh @@ -0,0 +1,51 @@ +#!/usr/bin/env bash +set -o errexit +set -o nounset +set -o pipefail +shopt -s globstar +shopt -s nullglob +[[ "${TRACE-0}" == "1" ]] && set -o xtrace + +# install requirements +apt update && apt install -y software-properties-common +add-apt-repository -y ppa:deadsnakes/ppa +apt install -y \ + git \ + python3-pip \ + python${PYTHON_VERSION} \ + python${PYTHON_VERSION}-venv \ + python${PYTHON_VERSION}-distutils + +# venv is safer than update-alternatives +python${PYTHON_VERSION} -m venv /venv +source /venv/bin/activate + +# checky check :) +pip -V + +# setup xformers repository +if [[ ! -d "xformers/.git" ]]; then + git clone --depth=1 https://github.com/facebookresearch/xformers.git +fi + +pushd xformers + + # update repository + git pull + git submodule update --init --recursive + + # install python dependencies + pip install \ + --upgrade \ + --extra-index-url "${PIP_TORCH_INDEX}" \ + "${PIP_TORCH_PACKAGE}" \ + ninja wheel \ + -r requirements.txt + + # just in case + python setup.py clean + + # let's hope nothing bad happen... + python setup.py build bdist_wheel --universal + +popd diff --git a/docker-batch.sh b/docker-batch.sh new file mode 100755 index 0000000..6f8d7b4 --- /dev/null +++ b/docker-batch.sh @@ -0,0 +1,42 @@ +#!/usr/bin/env bash +set -o errexit +set -o nounset +set -o pipefail +shopt -s globstar +shopt -s nullglob +[[ "${TRACE-0}" == "1" ]] && set -o xtrace + +cuda_versions=( + "cu113" + "cu116" +) + +declare -A cuda_images +cuda_images["cu113"]="docker.io/nvidia/cuda:11.3.0-devel-ubuntu18.04" +cuda_images["cu116"]="docker.io/nvidia/cuda:11.6.0-devel-ubuntu18.04" + +# xformers supports 3.7 to 3.10 +# https://github.com/facebookresearch/xformers/blob/e163309908ed7a76847ce46c79b238b49fd7d341/setup.py#L335-L338 +python_versions=( + "3.7" + "3.8" + "3.9" + "3.10" +) + +for cuda_version in "${cuda_versions[@]}"; do + dist_dir="wheels/${cuda_version}" + mkdir -p "${dist_dir}" + + for python_version in "${python_versions[@]}"; do + echo "build xformers with ${cuda_version} and python${python_version}" + + PYTHON_VERSION="${python_version}" \ + PIP_TORCH_INDEX="https://download.pytorch.org/whl/${cuda_version}" \ + PIP_TORCH_PACKAGE="torch==1.12.1+${cuda_version}" \ + DOCKER_IMAGE="${cuda_images["$cuda_version"]}" \ + "$(dirname "$0")/docker.sh" + + mv xformers/dist/*.whl "${dist_dir}/" + done +done \ No newline at end of file diff --git a/docker.sh b/docker.sh new file mode 100755 index 0000000..71c5e34 --- /dev/null +++ b/docker.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +set -o errexit +set -o nounset +set -o pipefail +shopt -s globstar +shopt -s nullglob +[[ "${TRACE-0}" == "1" ]] && set -o xtrace + +docker run \ + --rm \ + -e TRACE="${TRACE-0}" \ + -e PYTHON_VERSION="${PYTHON_VERSION:-"3.8"}" \ + -e PIP_TORCH_INDEX="${PIP_TORCH_INDEX:-"https://download.pytorch.org/whl/cu116"}" \ + -e PIP_TORCH_PACKAGE="${PIP_TORCH_PACKAGE:-"torch"}" \ + -e TORCH_CUDA_ARCH_LIST="${TORCH_CUDA_ARCH_LIST:-"6.0;6.1;6.2;7.0;7.2;7.5;8.0;8.6"}" \ + -e NVCC_FLAGS="${NVCC_FLAGS:-"--use_fast_math"}" \ + -e MAX_JOBS="${MAX_JOBS:-$(($(nproc) - 2))}" \ + -e FORCE_CUDA=1 \ + -e XFORMERS_DISABLE_FLASH_ATTN=1 \ + -e DEBIAN_FRONTEND=noninteractive \ + -v "$(dirname "$0")/build.sh:/build.sh:ro" \ + -v "$(pwd):/workspace" -w /workspace \ + --entrypoint /build.sh \ + "${DOCKER_IMAGE:-"docker.io/nvidia/cuda:11.6.0-devel-ubuntu18.04"}"