publish.yml 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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.2.2', '2.3.1', '2.4.0', '2.5.1', '2.6.0']
  41. cuda-version: ['12.4.1']
  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.5 does not support Python 3.13
  50. - torch-version: '2.2.2'
  51. python-version: '3.13'
  52. - torch-version: '2.3.1'
  53. python-version: '3.13'
  54. - torch-version: '2.4.0'
  55. python-version: '3.13'
  56. steps:
  57. - name: Checkout
  58. uses: actions/checkout@v4
  59. - name: Set up Python
  60. uses: actions/setup-python@v5
  61. with:
  62. python-version: ${{ matrix.python-version }}
  63. - name: Set CUDA and PyTorch versions
  64. run: |
  65. echo "MATRIX_CUDA_VERSION=$(echo ${{ matrix.cuda-version }} | awk -F \. {'print $1 $2'})" >> $GITHUB_ENV
  66. echo "MATRIX_TORCH_VERSION=$(echo ${{ matrix.torch-version }} | awk -F \. {'print $1 "." $2'})" >> $GITHUB_ENV
  67. echo "WHEEL_CUDA_VERSION=$(echo ${{ matrix.cuda-version }} | awk -F \. {'print $1'})" >> $GITHUB_ENV
  68. echo "MATRIX_PYTHON_VERSION=$(echo ${{ matrix.python-version }} | awk -F \. {'print $1 $2'})" >> $GITHUB_ENV
  69. - name: Free up disk space
  70. if: ${{ runner.os == 'Linux' }}
  71. # https://github.com/easimon/maximize-build-space/blob/master/action.yml
  72. # https://github.com/easimon/maximize-build-space/tree/test-report
  73. run: |
  74. sudo rm -rf /usr/share/dotnet
  75. sudo rm -rf /opt/ghc
  76. sudo rm -rf /opt/hostedtoolcache/CodeQL
  77. - name: Set up swap space
  78. if: runner.os == 'Linux'
  79. uses: pierotofy/set-swap-space@v1.0
  80. with:
  81. swap-size-gb: 10
  82. - name: Install CUDA ${{ matrix.cuda-version }}
  83. if: ${{ matrix.cuda-version != 'cpu' }}
  84. uses: Jimver/cuda-toolkit@v0.2.19
  85. id: cuda-toolkit
  86. with:
  87. cuda: ${{ matrix.cuda-version }}
  88. linux-local-args: '["--toolkit"]'
  89. # default method is "local", and we're hitting some error with caching for CUDA 11.8 and 12.1
  90. # method: ${{ (matrix.cuda-version == '11.8.0' || matrix.cuda-version == '12.1.0') && 'network' || 'local' }}
  91. method: 'network'
  92. sub-packages: '["nvcc"]'
  93. - name: Install PyTorch ${{ matrix.torch-version }}+cu${{ matrix.cuda-version }}
  94. run: |
  95. pip install --upgrade pip
  96. # For some reason torch 2.2.0 on python 3.12 errors saying no setuptools
  97. pip install setuptools==75.8.0
  98. # With python 3.13 and torch 2.5.1, unless we update typing-extensions, we get error
  99. # AttributeError: attribute '__default__' of 'typing.ParamSpec' objects is not writable
  100. pip install typing-extensions==4.12.2
  101. # We want to figure out the CUDA version to download pytorch
  102. # 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
  103. # see https://github.com/pytorch/pytorch/blob/main/RELEASE.md#release-compatibility-matrix
  104. # This code is ugly, maybe there's a better way to do this.
  105. export TORCH_CUDA_VERSION=$(python -c "from os import environ as env; \
  106. minv = {'2.2': 118, '2.3': 118, '2.4': 118, '2.5': 118, '2.6': 118}[env['MATRIX_TORCH_VERSION']]; \
  107. maxv = {'2.2': 121, '2.3': 121, '2.4': 124, '2.5': 124, '2.6': 124}[env['MATRIX_TORCH_VERSION']]; \
  108. print(minv if int(env['MATRIX_CUDA_VERSION']) < 120 else maxv)" \
  109. )
  110. if [[ ${{ matrix.torch-version }} == *"dev"* ]]; then
  111. # pip install --no-cache-dir --pre torch==${{ matrix.torch-version }} --index-url https://download.pytorch.org/whl/nightly/cu${TORCH_CUDA_VERSION}
  112. # Can't use --no-deps because we need cudnn etc.
  113. # Hard-coding this version of pytorch-triton for torch 2.6.0.dev20241001
  114. pip install jinja2
  115. 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
  116. 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
  117. else
  118. pip install --no-cache-dir torch==${{ matrix.torch-version }} --index-url https://download.pytorch.org/whl/cu${TORCH_CUDA_VERSION}
  119. fi
  120. nvcc --version
  121. python --version
  122. python -c "import torch; print('PyTorch:', torch.__version__)"
  123. python -c "import torch; print('CUDA:', torch.version.cuda)"
  124. python -c "from torch.utils import cpp_extension; print (cpp_extension.CUDA_HOME)"
  125. shell:
  126. bash
  127. - name: Build wheel
  128. run: |
  129. # 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
  130. # https://github.com/pytorch/pytorch/blob/664058fa83f1d8eede5d66418abff6e20bd76ca8/torch/utils/cpp_extension.py#L810
  131. # However this still fails so I'm using a newer version of setuptools
  132. pip install setuptools==75.8.0
  133. pip install ninja packaging wheel
  134. export PATH=/usr/local/nvidia/bin:/usr/local/nvidia/lib64:$PATH
  135. export LD_LIBRARY_PATH=/usr/local/nvidia/lib64:/usr/local/cuda/lib64:$LD_LIBRARY_PATH
  136. # Limit MAX_JOBS otherwise the github runner goes OOM
  137. # nvcc 11.8 can compile with 2 jobs, but nvcc 12.3 goes OOM
  138. 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
  139. tmpname=cu${WHEEL_CUDA_VERSION}torch${MATRIX_TORCH_VERSION}cxx11abi${{ matrix.cxx11_abi }}
  140. wheel_name=$(ls dist/*whl | xargs -n 1 basename | sed "s/-/+$tmpname-/2")
  141. ls dist/*whl |xargs -I {} mv {} dist/${wheel_name}
  142. echo "wheel_name=${wheel_name}" >> $GITHUB_ENV
  143. - name: Log Built Wheels
  144. run: |
  145. ls dist
  146. - name: Get the tag version
  147. id: extract_branch
  148. run: echo ::set-output name=branch::${GITHUB_REF#refs/tags/}
  149. - name: Get Release with tag
  150. id: get_current_release
  151. uses: joutvhu/get-release@v1
  152. with:
  153. tag_name: ${{ steps.extract_branch.outputs.branch }}
  154. env:
  155. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  156. - name: Upload Release Asset
  157. id: upload_release_asset
  158. uses: actions/upload-release-asset@v1
  159. env:
  160. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  161. with:
  162. upload_url: ${{ steps.get_current_release.outputs.upload_url }}
  163. asset_path: ./dist/${{env.wheel_name}}
  164. asset_name: ${{env.wheel_name}}
  165. asset_content_type: application/*
  166. publish_package:
  167. name: Publish package
  168. needs: [build_wheels]
  169. runs-on: ubuntu-latest
  170. steps:
  171. - uses: actions/checkout@v4
  172. - uses: actions/setup-python@v5
  173. with:
  174. python-version: '3.10'
  175. - name: Install dependencies
  176. run: |
  177. pip install ninja packaging wheel twine
  178. # Install latest setuptools with support for pypi metadata 2.2 (improved compat w/ uv)
  179. pip install setuptools==75.8.0
  180. # We don't want to download anything CUDA-related here
  181. pip install torch --index-url https://download.pytorch.org/whl/cpu
  182. - name: Build core package
  183. env:
  184. FLASH_ATTENTION_SKIP_CUDA_BUILD: "TRUE"
  185. run: |
  186. python setup.py sdist --dist-dir=dist
  187. - name: Deploy
  188. env:
  189. TWINE_USERNAME: "__token__"
  190. TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
  191. run: |
  192. python -m twine upload dist/*