publish.yml 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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
  7. name: Build wheels and deploy
  8. on:
  9. create:
  10. tags:
  11. - v*
  12. jobs:
  13. setup_release:
  14. name: Create Release
  15. runs-on: ubuntu-latest
  16. steps:
  17. - name: Get the tag version
  18. id: extract_branch
  19. run: echo ::set-output name=branch::${GITHUB_REF#refs/tags/}
  20. shell: bash
  21. - name: Create Release
  22. id: create_release
  23. uses: actions/create-release@v1
  24. env:
  25. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  26. with:
  27. tag_name: ${{ steps.extract_branch.outputs.branch }}
  28. release_name: ${{ steps.extract_branch.outputs.branch }}
  29. build_wheels:
  30. name: Build Wheel
  31. needs: setup_release
  32. runs-on: ${{ matrix.os }}
  33. strategy:
  34. fail-fast: false
  35. matrix:
  36. # Using ubuntu-20.04 instead of 22.04 for more compatibility (glibc). Ideally we'd use the
  37. # manylinux docker image, but I haven't figured out how to install CUDA on manylinux.
  38. os: [ubuntu-20.04]
  39. python-version: ['3.9', '3.10', '3.11', '3.12', '3.13']
  40. torch-version: ['2.1.2', '2.2.2', '2.3.1', '2.4.0', '2.5.1', '2.6.0.dev20241001']
  41. cuda-version: ['11.8.0', '12.3.2']
  42. # We need separate wheels that either uses C++11 ABI (-D_GLIBCXX_USE_CXX11_ABI) or not.
  43. # Pytorch wheels currently don't use it, but nvcr images have Pytorch compiled with C++11 ABI.
  44. # Without this we get import error (undefined symbol: _ZN3c105ErrorC2ENS_14SourceLocationESs)
  45. # when building without C++11 ABI and using it on nvcr images.
  46. cxx11_abi: ['FALSE', 'TRUE']
  47. exclude:
  48. # see https://github.com/pytorch/pytorch/blob/main/RELEASE.md#release-compatibility-matrix
  49. # Pytorch < 2.2 does not support Python 3.12
  50. - torch-version: '2.1.2'
  51. python-version: '3.12'
  52. # Pytorch < 2.5 does not support Python 3.13
  53. - torch-version: '2.1.2'
  54. python-version: '3.13'
  55. - torch-version: '2.2.2'
  56. python-version: '3.13'
  57. - torch-version: '2.3.1'
  58. python-version: '3.13'
  59. - torch-version: '2.4.0'
  60. python-version: '3.13'
  61. steps:
  62. - name: Checkout
  63. uses: actions/checkout@v4
  64. - name: Set up Python
  65. uses: actions/setup-python@v5
  66. with:
  67. python-version: ${{ matrix.python-version }}
  68. - name: Set CUDA and PyTorch versions
  69. run: |
  70. echo "MATRIX_CUDA_VERSION=$(echo ${{ matrix.cuda-version }} | awk -F \. {'print $1 $2'})" >> $GITHUB_ENV
  71. echo "MATRIX_TORCH_VERSION=$(echo ${{ matrix.torch-version }} | awk -F \. {'print $1 "." $2'})" >> $GITHUB_ENV
  72. echo "WHEEL_CUDA_VERSION=$(echo ${{ matrix.cuda-version }} | awk -F \. {'print $1'})" >> $GITHUB_ENV
  73. echo "MATRIX_PYTHON_VERSION=$(echo ${{ matrix.python-version }} | awk -F \. {'print $1 $2'})" >> $GITHUB_ENV
  74. - name: Free up disk space
  75. if: ${{ runner.os == 'Linux' }}
  76. # https://github.com/easimon/maximize-build-space/blob/master/action.yml
  77. # https://github.com/easimon/maximize-build-space/tree/test-report
  78. run: |
  79. sudo rm -rf /usr/share/dotnet
  80. sudo rm -rf /opt/ghc
  81. sudo rm -rf /opt/hostedtoolcache/CodeQL
  82. - name: Set up swap space
  83. if: runner.os == 'Linux'
  84. uses: pierotofy/set-swap-space@v1.0
  85. with:
  86. swap-size-gb: 10
  87. - name: Install CUDA ${{ matrix.cuda-version }}
  88. if: ${{ matrix.cuda-version != 'cpu' }}
  89. uses: Jimver/cuda-toolkit@v0.2.19
  90. id: cuda-toolkit
  91. with:
  92. cuda: ${{ matrix.cuda-version }}
  93. linux-local-args: '["--toolkit"]'
  94. # default method is "local", and we're hitting some error with caching for CUDA 11.8 and 12.1
  95. # method: ${{ (matrix.cuda-version == '11.8.0' || matrix.cuda-version == '12.1.0') && 'network' || 'local' }}
  96. method: 'network'
  97. sub-packages: '["nvcc"]'
  98. - name: Install PyTorch ${{ matrix.torch-version }}+cu${{ matrix.cuda-version }}
  99. run: |
  100. pip install --upgrade pip
  101. # For some reason torch 2.2.0 on python 3.12 errors saying no setuptools
  102. pip install setuptools==68.0.0
  103. # With python 3.13 and torch 2.5.1, unless we update typing-extensions, we get error
  104. # AttributeError: attribute '__default__' of 'typing.ParamSpec' objects is not writable
  105. pip install typing-extensions==4.12.2
  106. # We want to figure out the CUDA version to download pytorch
  107. # 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
  108. # see https://github.com/pytorch/pytorch/blob/main/RELEASE.md#release-compatibility-matrix
  109. # This code is ugly, maybe there's a better way to do this.
  110. export TORCH_CUDA_VERSION=$(python -c "from os import environ as env; \
  111. minv = {'2.1': 118, '2.2': 118, '2.3': 118, '2.4': 118, '2.5': 118, '2.6': 118}[env['MATRIX_TORCH_VERSION']]; \
  112. maxv = {'2.1': 121, '2.2': 121, '2.3': 121, '2.4': 124, '2.5': 124, '2.6': 124}[env['MATRIX_TORCH_VERSION']]; \
  113. print(minv if int(env['MATRIX_CUDA_VERSION']) < 120 else maxv)" \
  114. )
  115. if [[ ${{ matrix.torch-version }} == *"dev"* ]]; then
  116. # pip install --no-cache-dir --pre torch==${{ matrix.torch-version }} --index-url https://download.pytorch.org/whl/nightly/cu${TORCH_CUDA_VERSION}
  117. # Can't use --no-deps because we need cudnn etc.
  118. # Hard-coding this version of pytorch-triton for torch 2.6.0.dev20241001
  119. pip install jinja2
  120. pip install https://download.pytorch.org/whl/nightly/pytorch_triton-3.1.0%2Bcf34004b8a-cp${MATRIX_PYTHON_VERSION}-cp${MATRIX_PYTHON_VERSION}-linux_x86_64.whl
  121. pip install --no-cache-dir --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
  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. - name: Build wheel
  133. run: |
  134. # 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
  135. # https://github.com/pytorch/pytorch/blob/664058fa83f1d8eede5d66418abff6e20bd76ca8/torch/utils/cpp_extension.py#L810
  136. # However this still fails so I'm using a newer version of setuptools
  137. pip install setuptools==68.0.0
  138. pip install ninja packaging wheel
  139. export PATH=/usr/local/nvidia/bin:/usr/local/nvidia/lib64:$PATH
  140. export LD_LIBRARY_PATH=/usr/local/nvidia/lib64:/usr/local/cuda/lib64:$LD_LIBRARY_PATH
  141. # Limit MAX_JOBS otherwise the github runner goes OOM
  142. # nvcc 11.8 can compile with 2 jobs, but nvcc 12.3 goes OOM
  143. MAX_JOBS=$([ "$MATRIX_CUDA_VERSION" == "123" ] && echo 1 || echo 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${WHEEL_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
  148. - name: Log Built Wheels
  149. run: |
  150. ls dist
  151. - name: Get the tag version
  152. id: extract_branch
  153. run: echo ::set-output name=branch::${GITHUB_REF#refs/tags/}
  154. - name: Get Release with tag
  155. id: get_current_release
  156. uses: joutvhu/get-release@v1
  157. with:
  158. tag_name: ${{ steps.extract_branch.outputs.branch }}
  159. env:
  160. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  161. - name: Upload Release Asset
  162. id: upload_release_asset
  163. uses: actions/upload-release-asset@v1
  164. env:
  165. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  166. with:
  167. upload_url: ${{ steps.get_current_release.outputs.upload_url }}
  168. asset_path: ./dist/${{env.wheel_name}}
  169. asset_name: ${{env.wheel_name}}
  170. asset_content_type: application/*
  171. publish_package:
  172. name: Publish package
  173. needs: [build_wheels]
  174. runs-on: ubuntu-latest
  175. steps:
  176. - uses: actions/checkout@v4
  177. - uses: actions/setup-python@v5
  178. with:
  179. python-version: '3.10'
  180. - name: Install dependencies
  181. run: |
  182. pip install ninja packaging setuptools wheel twine
  183. # We don't want to download anything CUDA-related here
  184. pip install torch --index-url https://download.pytorch.org/whl/cpu
  185. - name: Build core package
  186. env:
  187. FLASH_ATTENTION_SKIP_CUDA_BUILD: "TRUE"
  188. run: |
  189. python setup.py sdist --dist-dir=dist
  190. - name: Deploy
  191. env:
  192. TWINE_USERNAME: "__token__"
  193. TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
  194. run: |
  195. python -m twine upload dist/*