Skip to content

Commit 961cfbd

Browse files
committed
Github action for build
1 parent f638e65 commit 961cfbd

File tree

6 files changed

+183
-158
lines changed

6 files changed

+183
-158
lines changed

.github/scripts/build.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
python_executable=python$1
4+
cuda_home=/usr/local/cuda-$2
5+
6+
# Update paths
7+
PATH=${cuda_home}/bin:$PATH
8+
LD_LIBRARY_PATH=${cuda_home}/lib64:$LD_LIBRARY_PATH
9+
10+
# Install requirements
11+
$python_executable -m pip install wheel packaging
12+
13+
# Limit the number of parallel jobs to avoid OOM
14+
export MAX_JOBS=2
15+
# Make sure release wheels are built for the following architectures
16+
export TORCH_CUDA_ARCH_LIST="8.0 8.6 8.9 9.0+PTX"
17+
# Build
18+
$python_executable setup.py bdist_wheel --dist-dir=dist

.github/scripts/create_release.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Uses Github's API to create the release and wait for result.
2+
// We use a JS script since github CLI doesn't provide a way to wait for the release's creation and returns immediately.
3+
4+
module.exports = async (github, context, core) => {
5+
try {
6+
const response = await github.rest.repos.createRelease({
7+
draft: false,
8+
generate_release_notes: true,
9+
name: process.env.RELEASE_TAG,
10+
owner: context.repo.owner,
11+
prerelease: true,
12+
repo: context.repo.repo,
13+
tag_name: process.env.RELEASE_TAG,
14+
});
15+
16+
core.setOutput('upload_url', response.data.upload_url);
17+
} catch (error) {
18+
core.setFailed(error.message);
19+
}
20+
}

.github/scripts/cuda-install.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
3+
# Replace '.' with '-' ex: 11.8 -> 11-8
4+
cuda_version=$(echo $1 | tr "." "-")
5+
# Removes '-' and '.' ex: ubuntu-20.04 -> ubuntu2004
6+
OS=$(echo $2 | tr -d ".\-")
7+
8+
# Installs CUDA
9+
wget -nv https://developer.download.nvidia.com/compute/cuda/repos/${OS}/x86_64/cuda-keyring_1.1-1_all.deb
10+
sudo dpkg -i cuda-keyring_1.1-1_all.deb
11+
rm cuda-keyring_1.1-1_all.deb
12+
sudo apt -qq update
13+
sudo apt -y install cuda-${cuda_version} cuda-nvcc-${cuda_version} cuda-libraries-dev-${cuda_version}
14+
sudo apt clean
15+
16+
# Test nvcc
17+
PATH=/usr/local/cuda-$1/bin:${PATH}
18+
nvcc --version
19+
20+
# Log gcc, g++, c++ versions
21+
gcc --version
22+
g++ --version
23+
c++ --version

.github/scripts/env.sh

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/bash
2+
3+
# This file installs common linux environment tools
4+
5+
export LANG C.UTF-8
6+
7+
# python_version=$1
8+
9+
sudo apt-get update && \
10+
sudo apt-get install -y --no-install-recommends \
11+
software-properties-common \
12+
13+
sudo apt-get install -y --no-install-recommends \
14+
build-essential \
15+
apt-utils \
16+
ca-certificates \
17+
wget \
18+
git \
19+
vim \
20+
libssl-dev \
21+
curl \
22+
unzip \
23+
unrar \
24+
cmake \
25+
net-tools \
26+
sudo \
27+
autotools-dev \
28+
rsync \
29+
jq \
30+
openssh-server \
31+
tmux \
32+
screen \
33+
htop \
34+
pdsh \
35+
openssh-client \
36+
lshw \
37+
dmidecode \
38+
util-linux \
39+
automake \
40+
autoconf \
41+
libtool \
42+
net-tools \
43+
pciutils \
44+
libpci-dev \
45+
libaio-dev \
46+
libcap2 \
47+
libtinfo5 \
48+
fakeroot \
49+
devscripts \
50+
debhelper \
51+
nfs-common
52+
53+
# Remove github bloat files to free up disk space
54+
sudo rm -rf "/usr/local/share/boost"
55+
sudo rm -rf "$AGENT_TOOLSDIRECTORY"
56+
sudo rm -rf "/usr/share/dotnet"

.github/scripts/pytorch-install.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
3+
python_executable=python$1
4+
pytorch_version=$2
5+
cuda_version=$3
6+
7+
# Install torch
8+
$python_executable -m pip install numpy pyyaml scipy ipython mkl mkl-include ninja cython typing pandas typing-extensions dataclasses setuptools && conda clean -ya
9+
$python_executable -m pip install torch==${pytorch_version}+cu${cuda_version//./} --extra-index-url https://download.pytorch.org/whl/cu${cuda_version//./}
10+
11+
# Print version information
12+
$python_executable --version
13+
$python_executable -c "import torch; print('PyTorch:', torch.__version__)"
14+
$python_executable -c "import torch; print('CUDA:', torch.version.cuda)"
15+
$python_executable -c "from torch.utils import cpp_extension; print (cpp_extension.CUDA_HOME)"

.github/workflows/publish.yml

Lines changed: 51 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -1,206 +1,99 @@
1-
# This workflow will:
2-
# - Create a new Github release
3-
# - Build wheels for supported architectures
4-
# - Deploy the wheels to the Github release
5-
# - Release the static code to PyPi
6-
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries
1+
# This workflow will upload a Python Package to Release asset
2+
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions
73

8-
name: Build wheels and deploy
4+
name: Create Release
95

106
on:
11-
create:
7+
push:
128
tags:
139
- v*
1410

15-
jobs:
11+
# Needed to create release and upload assets
12+
permissions:
13+
contents: write
1614

17-
setup_release:
15+
jobs:
16+
release:
17+
# Retrieve tag and create release
1818
name: Create Release
1919
runs-on: ubuntu-latest
20+
outputs:
21+
upload_url: ${{ steps.create_release.outputs.upload_url }}
2022
steps:
21-
- name: Get the tag version
22-
id: extract_branch
23-
run: echo ::set-output name=branch::${GITHUB_REF#refs/tags/}
23+
- name: Checkout
24+
uses: actions/checkout@v3
25+
26+
- name: Extract branch info
2427
shell: bash
28+
run: |
29+
echo "release_tag=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
2530
2631
- name: Create Release
2732
id: create_release
28-
uses: actions/create-release@v1
33+
uses: "actions/github-script@v6"
2934
env:
30-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
35+
RELEASE_TAG: ${{ env.release_tag }}
3136
with:
32-
tag_name: ${{ steps.extract_branch.outputs.branch }}
33-
release_name: ${{ steps.extract_branch.outputs.branch }}
37+
github-token: "${{ secrets.GITHUB_TOKEN }}"
38+
script: |
39+
const script = require('.github/workflows/scripts/create_release.js')
40+
await script(github, context, core)
3441
35-
build_wheels:
42+
wheel:
3643
name: Build Wheel
37-
needs: setup_release
3844
runs-on: ${{ matrix.os }}
45+
needs: release
3946

4047
strategy:
4148
fail-fast: false
4249
matrix:
43-
# Using ubuntu-20.04 instead of 22.04 for more compatibility (glibc). Ideally we'd use the
44-
# manylinux docker image, but I haven't figured out how to install CUDA on manylinux.
45-
os: [ubuntu-20.04]
50+
os: ['ubuntu-20.04']
4651
python-version: ['3.8', '3.9', '3.10', '3.11']
47-
torch-version: ['2.3.0']
48-
cuda-version: ['11.8.0', '12.1.1']
49-
# We need separate wheels that either uses C++11 ABI (-D_GLIBCXX_USE_CXX11_ABI) or not.
50-
# Pytorch wheels currently don't use it, but nvcr images have Pytorch compiled with C++11 ABI.
51-
# Without this we get import error (undefined symbol: _ZN3c105ErrorC2ENS_14SourceLocationESs)
52-
# when building without C++11 ABI and using it on nvcr images.
53-
cxx11_abi: ['FALSE', 'TRUE']
52+
pytorch-version: ['2.3.0'] # Must be the most recent version that meets requirements-cuda.txt.
53+
cuda-version: ['11.8', '12.1']
5454

5555
steps:
5656
- name: Checkout
5757
uses: actions/checkout@v3
5858

59-
- name: Set up Python
60-
uses: actions/setup-python@v4
61-
with:
62-
python-version: ${{ matrix.python-version }}
59+
- name: Setup ccache
60+
uses: hendrikmuhs/ccache-action@v1.2
6361

64-
- name: Set CUDA and PyTorch versions
65-
run: |
66-
echo "MATRIX_CUDA_VERSION=$(echo ${{ matrix.cuda-version }} | awk -F \. {'print $1 $2'})" >> $GITHUB_ENV
67-
echo "MATRIX_TORCH_VERSION=$(echo ${{ matrix.torch-version }} | awk -F \. {'print $1 "." $2'})" >> $GITHUB_ENV
68-
echo "MATRIX_PYTHON_VERSION=$(echo ${{ matrix.python-version }} | awk -F \. {'print $1 $2'})" >> $GITHUB_ENV
69-
70-
- name: Free up disk space
62+
- name: Set up Linux Env
7163
if: ${{ runner.os == 'Linux' }}
72-
# https://github.com/easimon/maximize-build-space/blob/master/action.yml
73-
# https://github.com/easimon/maximize-build-space/tree/test-report
7464
run: |
75-
sudo rm -rf /usr/share/dotnet
76-
sudo rm -rf /opt/ghc
77-
sudo rm -rf /opt/hostedtoolcache/CodeQL
65+
bash -x .github/workflows/scripts/env.sh
7866
79-
- name: Set up swap space
80-
if: runner.os == 'Linux'
81-
uses: pierotofy/set-swap-space@v1.0
67+
- name: Set up Python
68+
uses: actions/setup-python@v4
8269
with:
83-
swap-size-gb: 10
70+
python-version: ${{ matrix.python-version }}
8471

8572
- name: Install CUDA ${{ matrix.cuda-version }}
86-
if: ${{ matrix.cuda-version != 'cpu' }}
87-
uses: Jimver/cuda-toolkit@v0.2.14
88-
id: cuda-toolkit
89-
with:
90-
cuda: ${{ matrix.cuda-version }}
91-
linux-local-args: '["--toolkit"]'
92-
# default method is "local", and we're hitting some error with caching for CUDA 11.8 and 12.1
93-
# method: ${{ (matrix.cuda-version == '11.8.0' || matrix.cuda-version == '12.1.0') && 'network' || 'local' }}
94-
method: 'network'
95-
# We need the cuda libraries (e.g. cuSparse, cuSolver) for compiling PyTorch extensions,
96-
# not just nvcc
97-
# sub-packages: '["nvcc"]'
98-
99-
- name: Install PyTorch ${{ matrix.torch-version }}+cu${{ matrix.cuda-version }}
100-
run: |
101-
pip install --upgrade pip
102-
# If we don't install before installing Pytorch, we get error for torch 2.0.1
103-
# ERROR: Could not find a version that satisfies the requirement setuptools>=40.8.0 (from versions: none)
104-
pip install lit
105-
# We want to figure out the CUDA version to download pytorch
106-
# e.g. we can have system CUDA version being 11.7 but if torch==1.12 then we need to download the wheel from cu116
107-
# see https://github.com/pytorch/pytorch/blob/main/RELEASE.md#release-compatibility-matrix
108-
# This code is ugly, maybe there's a better way to do this.
109-
export TORCH_CUDA_VERSION=$(python -c "from os import environ as env; \
110-
minv = {'1.12': 113, '1.13': 116, '2.0': 117, '2.1': 118, '2.2': 118, '2.3': 118}[env['MATRIX_TORCH_VERSION']]; \
111-
maxv = {'1.12': 116, '1.13': 117, '2.0': 118, '2.1': 121, '2.2': 121, '2.3': 121}[env['MATRIX_TORCH_VERSION']]; \
112-
print(max(min(int(env['MATRIX_CUDA_VERSION']), maxv), minv))" \
113-
)
114-
if [[ ${{ matrix.torch-version }} == *"dev"* ]]; then
115-
if [[ ${MATRIX_TORCH_VERSION} == "2.2" ]]; then
116-
# --no-deps because we can't install old versions of pytorch-triton
117-
pip install typing-extensions jinja2
118-
pip install --no-cache-dir --no-deps --pre https://download.pytorch.org/whl/nightly/cu${TORCH_CUDA_VERSION}/torch-${{ matrix.torch-version }}%2Bcu${TORCH_CUDA_VERSION}-cp${MATRIX_PYTHON_VERSION}-cp${MATRIX_PYTHON_VERSION}-linux_x86_64.whl
119-
else
120-
pip install --no-cache-dir --pre torch==${{ matrix.torch-version }} --index-url https://download.pytorch.org/whl/nightly/cu${TORCH_CUDA_VERSION}
121-
fi
122-
else
123-
pip install --no-cache-dir torch==${{ matrix.torch-version }} --index-url https://download.pytorch.org/whl/cu${TORCH_CUDA_VERSION}
124-
fi
125-
nvcc --version
126-
python --version
127-
python -c "import torch; print('PyTorch:', torch.__version__)"
128-
python -c "import torch; print('CUDA:', torch.version.cuda)"
129-
python -c "from torch.utils import cpp_extension; print (cpp_extension.CUDA_HOME)"
130-
shell:
131-
bash
132-
133-
- name: Build wheel
13473
run: |
135-
# We want setuptools >= 49.6.0 otherwise we can't compile the extension if system CUDA version is 11.7 and pytorch cuda version is 11.6
136-
# https://github.com/pytorch/pytorch/blob/664058fa83f1d8eede5d66418abff6e20bd76ca8/torch/utils/cpp_extension.py#L810
137-
# However this still fails so I'm using a newer version of setuptools
138-
pip install setuptools==68.0.0
139-
pip install ninja packaging wheel
140-
export PATH=/usr/local/nvidia/bin:/usr/local/nvidia/lib64:$PATH
141-
export LD_LIBRARY_PATH=/usr/local/nvidia/lib64:/usr/local/cuda/lib64:$LD_LIBRARY_PATH
142-
# Limit MAX_JOBS otherwise the github runner goes OOM
143-
MAX_JOBS=2 FLASH_ATTENTION_FORCE_BUILD="TRUE" FLASH_ATTENTION_FORCE_CXX11_ABI=${{ matrix.cxx11_abi}} python setup.py bdist_wheel --dist-dir=dist
144-
tmpname=cu${MATRIX_CUDA_VERSION}torch${MATRIX_TORCH_VERSION}cxx11abi${{ matrix.cxx11_abi }}
145-
wheel_name=$(ls dist/*whl | xargs -n 1 basename | sed "s/-/+$tmpname-/2")
146-
ls dist/*whl |xargs -I {} mv {} dist/${wheel_name}
147-
echo "wheel_name=${wheel_name}" >> $GITHUB_ENV
74+
bash -x .github/workflows/scripts/cuda-install.sh ${{ matrix.cuda-version }} ${{ matrix.os }}
14875
149-
- name: Log Built Wheels
76+
- name: Install PyTorch ${{ matrix.pytorch-version }} with CUDA ${{ matrix.cuda-version }}
15077
run: |
151-
ls dist
78+
bash -x .github/workflows/scripts/pytorch-install.sh ${{ matrix.python-version }} ${{ matrix.pytorch-version }} ${{ matrix.cuda-version }}
15279
153-
- name: Get the tag version
154-
id: extract_branch
155-
run: echo ::set-output name=branch::${GITHUB_REF#refs/tags/}
156-
157-
- name: Get Release with tag
158-
id: get_current_release
159-
uses: joutvhu/get-release@v1
160-
with:
161-
tag_name: ${{ steps.extract_branch.outputs.branch }}
80+
- name: Build wheel
81+
shell: bash
16282
env:
163-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
83+
CMAKE_BUILD_TYPE: Release # do not compile with debug symbol to reduce wheel size
84+
run: |
85+
bash -x .github/workflows/scripts/build.sh ${{ matrix.python-version }} ${{ matrix.cuda-version }}
86+
wheel_name=$(ls dist/*whl | xargs -n 1 basename)
87+
asset_name=${wheel_name//"linux"/"manylinux1"}
88+
echo "wheel_name=${wheel_name}" >> $GITHUB_ENV
89+
echo "asset_name=${asset_name}" >> $GITHUB_ENV
16490
16591
- name: Upload Release Asset
166-
id: upload_release_asset
16792
uses: actions/upload-release-asset@v1
16893
env:
16994
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
17095
with:
171-
upload_url: ${{ steps.get_current_release.outputs.upload_url }}
172-
asset_path: ./dist/${{env.wheel_name}}
173-
asset_name: ${{env.wheel_name}}
96+
upload_url: ${{ needs.release.outputs.upload_url }}
97+
asset_path: ./dist/${{ env.wheel_name }}
98+
asset_name: ${{ env.asset_name }}
17499
asset_content_type: application/*
175-
176-
publish_package:
177-
name: Publish package
178-
needs: [build_wheels]
179-
180-
runs-on: ubuntu-latest
181-
182-
steps:
183-
- uses: actions/checkout@v3
184-
185-
- uses: actions/setup-python@v4
186-
with:
187-
python-version: '3.10'
188-
189-
- name: Install dependencies
190-
run: |
191-
pip install ninja packaging setuptools wheel twine
192-
# We don't want to download anything CUDA-related here
193-
pip install torch --index-url https://download.pytorch.org/whl/cpu
194-
195-
- name: Build core package
196-
env:
197-
FLASH_ATTENTION_SKIP_CUDA_BUILD: "TRUE"
198-
run: |
199-
python setup.py sdist --dist-dir=dist
200-
201-
- name: Deploy
202-
env:
203-
TWINE_USERNAME: "__token__"
204-
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
205-
run: |
206-
python -m twine upload dist/*

0 commit comments

Comments
 (0)