diff --git a/.github/scripts/install-torch-tensorrt.sh b/.github/scripts/install-torch-tensorrt.sh new file mode 100644 index 0000000000..2930421d5b --- /dev/null +++ b/.github/scripts/install-torch-tensorrt.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +set -eou pipefail +# Source conda so it's available to the script environment +source ${BUILD_ENV_FILE} +${CONDA_RUN} ${PIP_INSTALL_TORCH} torchvision pyyaml +export TRT_VERSION=$(${CONDA_RUN} python -c "import versions; versions.tensorrt_version()") +${CONDA_RUN} python -m pip install /opt/torch-tensorrt-builds/torch_tensorrt*+${CU_VERSION}*.whl tensorrt~=${TRT_VERSION} tensorrt-bindings~=${TRT_VERSION} --extra-index-url=https://pypi.ngc.nvidia.com + +echo -e "Running test script"; \ No newline at end of file diff --git a/.github/scripts/setup-env.sh b/.github/scripts/setup-env.sh new file mode 100644 index 0000000000..86e814faf5 --- /dev/null +++ b/.github/scripts/setup-env.sh @@ -0,0 +1,107 @@ +#!/usr/bin/env bash + +set -euxo pipefail + +# Prepare conda +set +x && eval "$($(which conda) shell.bash hook)" && set -x + +# Setup the OS_TYPE environment variable that should be used for conditions involving the OS below. +case $(uname) in + Linux) + OS_TYPE=linux + ;; + Darwin) + OS_TYPE=macos + ;; + MSYS*) + OS_TYPE=windows + ;; + *) + echo "Unknown OS type:" $(uname) + exit 1 + ;; +esac + +if [[ "${OS_TYPE}" == "macos" && $(uname -m) == x86_64 ]]; then + echo '::group::Uninstall system JPEG libraries on macOS' + # The x86 macOS runners, e.g. the GitHub Actions native "macos-12" runner, has some JPEG and PNG libraries + # installed by default that interfere with our build. We uninstall them here and use the one from conda below. + IMAGE_LIBS=$(brew list | grep -E "jpeg|png") + for lib in $IMAGE_LIBS; do + brew uninstall --ignore-dependencies --force "${lib}" + done + echo '::endgroup::' +fi + +echo '::group::Create build environment' +# See https://github.com/pytorch/vision/issues/7296 for ffmpeg +conda create \ + --name ci \ + --quiet --yes \ + python="${PYTHON_VERSION}" pip \ + ninja cmake \ + libpng jpeg \ + 'ffmpeg<4.3' +conda activate ci +pip install --progress-bar=off --upgrade setuptools + +# See https://github.com/pytorch/vision/issues/6790 +if [[ "${PYTHON_VERSION}" != "3.11" ]]; then + pip install --progress-bar=off av!=10.0.0 +fi + +echo '::endgroup::' + +if [[ "${OS_TYPE}" == windows && "${GPU_ARCH_TYPE}" == cuda ]]; then + echo '::group::Install VisualStudio CUDA extensions on Windows' + if [[ "${VC_YEAR:-}" == "2022" ]]; then + TARGET_DIR="/c/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/MSBuild/Microsoft/VC/v170/BuildCustomizations" + else + TARGET_DIR="/c/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/MSBuild/Microsoft/VC/v160/BuildCustomizations" + fi + mkdir -p "${TARGET_DIR}" + cp -r "${CUDA_HOME}/MSBuildExtensions/"* "${TARGET_DIR}" + echo '::endgroup::' +fi + +echo '::group::Install PyTorch' +# TODO: Can we maybe have this as environment variable in the job template? For example, `IS_RELEASE`. +if [[ (${GITHUB_EVENT_NAME} = 'pull_request' && (${GITHUB_BASE_REF} = 'release'*)) || (${GITHUB_REF} = 'refs/heads/release'*) ]]; then + CHANNEL=test +else + CHANNEL=nightly +fi + +pip install --progress-bar=off light-the-torch +ltt install --progress-bar=off \ + --pytorch-computation-backend="${GPU_ARCH_TYPE}${GPU_ARCH_VERSION}" \ + --pytorch-channel="${CHANNEL}" \ + torch + +if [[ $GPU_ARCH_TYPE == 'cuda' ]]; then + python -c "import torch; exit(not torch.cuda.is_available())" +fi +echo '::endgroup::' + +echo '::group::Install third party dependencies prior to TorchVision install' +# Installing with `easy_install`, e.g. `python setup.py install` or `python setup.py develop`, has some quirks when +# when pulling in third-party dependencies. For example: +# - On Windows, we often hit an SSL error although `pip` can install just fine. +# - It happily pulls in pre-releases, which can lead to more problems down the line. +# `pip` does not unless explicitly told to do so. +# Thus, we use `easy_install` to extract the third-party dependencies here and install them upfront with `pip`. +python setup.py egg_info +# The requires.txt cannot be used with `pip install -r` directly. The requirements are listed at the top and the +# optional dependencies come in non-standard syntax after a blank line. Thus, we just extract the header. +sed -e '/^$/,$d' *.egg-info/requires.txt | tee requirements.txt +pip install --progress-bar=off -r requirements.txt +echo '::endgroup::' + +echo '::group::Install TorchVision' +python setup.py develop +echo '::endgroup::' + +echo '::group::Collect environment information' +conda list +python -m torch.utils.collect_env +echo '::endgroup::' \ No newline at end of file diff --git a/.github/scripts/tests-torchscript-cpp.sh b/.github/scripts/tests-torchscript-cpp.sh new file mode 100644 index 0000000000..936d2a7c53 --- /dev/null +++ b/.github/scripts/tests-torchscript-cpp.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +set -euo pipefail + +./.github/scripts/setup-env.sh + +# Activate conda environment +eval "$($(which conda) shell.bash hook)" && conda deactivate && conda activate ci + +echo '::group::Install testing utilities' +pip install --progress-bar=off pytest pytest-mock pytest-cov +echo '::endgroup::' + +pytest --junit-xml="${RUNNER_TEST_RESULTS_DIR}/test-results.xml" -v --durations=25 \ No newline at end of file diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml new file mode 100644 index 0000000000..ae4cd671f0 --- /dev/null +++ b/.github/workflows/build-test.yml @@ -0,0 +1,205 @@ +name: Build and test linux wheels + +on: + pull_request: + push: + branches: + - nightly + - main + - release/* + tags: + # NOTE: Binary build pipelines should only get triggered on release candidate builds + # Release candidate tags look like: v1.11.0-rc1 + - v[0-9]+.[0-9]+.[0-9]+-rc[0-9]+ + workflow_dispatch: + +jobs: + generate-matrix: + uses: pytorch/test-infra/.github/workflows/generate_binary_build_matrix.yml@main + with: + package-type: wheel + os: linux + test-infra-repository: pytorch/test-infra + test-infra-ref: main + with-rocm: false + with-cpu: false + + build: + needs: generate-matrix + strategy: + fail-fast: false + matrix: + include: + - repository: pytorch/tensorrt + pre-script: packaging/pre_build_script.sh + env-var-script: packaging/env_vars.txt + post-script: "" + smoke-test-script: "" + package-name: torch_tensorrt + name: Build torch-tensorrt whl package + uses: pytorch/test-infra/.github/workflows/build_wheels_linux.yml@main + with: + repository: ${{ matrix.repository }} + ref: "" + test-infra-repository: pytorch/test-infra + test-infra-ref: main + build-matrix: ${{ needs.generate-matrix.outputs.matrix }} + pre-script: ${{ matrix.pre-script }} + env-var-script: ${{ matrix.env-var-script }} + post-script: ${{ matrix.post-script }} + package-name: ${{ matrix.package-name }} + smoke-test-script: ${{ matrix.smoke-test-script }} + trigger-event: ${{ github.event_name }} + secrets: + AWS_PYTORCH_UPLOADER_ACCESS_KEY_ID: ${{ secrets.AWS_PYTORCH_UPLOADER_ACCESS_KEY_ID }} + AWS_PYTORCH_UPLOADER_SECRET_ACCESS_KEY: ${{ secrets.AWS_PYTORCH_UPLOADER_SECRET_ACCESS_KEY }} + + # tests-py-torchscript-fe: + # name: Test torchscript frontend [Python] + # needs: [generate-matrix, build] + # strategy: + # fail-fast: false + # matrix: + # include: + # - repository: pytorch/tensorrt + # package-name: torch_tensorrt + # pre-script: packaging/pre_build_script.sh + # uses: pytorch/tensorrt/.github/workflows/linux-test.yml@gha-ci-infra + # with: + # job-name: tests-py-torchscript-fe + # repository: "pytorch/tensorrt" + # ref: "" + # test-infra-repository: pytorch/test-infra + # test-infra-ref: main + # build-matrix: ${{ needs.generate-matrix.outputs.matrix }} + # pre-script: ${{ matrix.pre-script }} + # script: | + # export USE_HOST_DEPS=1 + # pushd . + # cd tests/modules + # ${CONDA_RUN} python -m pip install -r requirements.txt + # ${CONDA_RUN} python hub.py + # popd + # pushd . + # cd tests/py/ts + # ${CONDA_RUN} python -m pip install --pre pytest timm transformers parameterized expecttest --use-deprecated=legacy-resolver + # ${CONDA_RUN} python -m pytest --junitxml=${RUNNER_TEST_RESULTS_DIR}/ts_api_test_results.xml api/ + # ${CONDA_RUN} python -m pytest --junitxml=${RUNNER_TEST_RESULTS_DIR}/ts_models_test_results.xml models/ + # ${CONDA_RUN} python -m pytest --junitxml=${RUNNER_TEST_RESULTS_DIR}/ts_integrations_test_results.xml integrations/ + # popd + + tests-py-dynamo-converters: + name: Test dynamo converters [Python] + needs: [generate-matrix, build] + strategy: + fail-fast: false + matrix: + include: + - repository: pytorch/tensorrt + package-name: torch_tensorrt + pre-script: packaging/pre_build_script.sh + uses: pytorch/tensorrt/.github/workflows/linux-test.yml@gha-ci-infra + with: + job-name: tests-py-dynamo-converters + repository: "pytorch/tensorrt" + ref: "" + test-infra-repository: pytorch/test-infra + test-infra-ref: main + build-matrix: ${{ needs.generate-matrix.outputs.matrix }} + pre-script: ${{ matrix.pre-script }} + script: | + export USE_HOST_DEPS=1 + pushd . + cd tests/modules + ${CONDA_RUN} python -m pip install -r requirements.txt + ${CONDA_RUN} python hub.py + popd + pushd . + cd tests/py/dynamo + ${CONDA_RUN} python -m pip install --pre pytest-xdist timm transformers parameterized expecttest --use-deprecated=legacy-resolver + ${CONDA_RUN} python -m pytest --junitxml=${RUNNER_TEST_RESULTS_DIR}/dynamo_converters_test_results.xml -n 10 conversion/ + popd + + tests-py-dynamo-fe: + name: Test dynamo frontend [Python] + needs: [generate-matrix, build] + strategy: + fail-fast: false + matrix: + include: + - repository: pytorch/tensorrt + package-name: torch_tensorrt + pre-script: packaging/pre_build_script.sh + uses: pytorch/tensorrt/.github/workflows/linux-test.yml@gha-ci-infra + with: + job-name: tests-py-dynamo-fe + repository: "pytorch/tensorrt" + ref: "" + test-infra-repository: pytorch/test-infra + test-infra-ref: main + build-matrix: ${{ needs.generate-matrix.outputs.matrix }} + pre-script: ${{ matrix.pre-script }} + script: | + export USE_HOST_DEPS=1 + pushd . + cd tests/py/dynamo + ${CONDA_RUN} python -m pip install --pre pytest timm transformers parameterized expecttest --use-deprecated=legacy-resolver + ${CONDA_RUN} python -m pytest --junitxml=${RUNNER_TEST_RESULTS_DIR}/dynamo_fe_test_results.xml --ir dynamo models/test_models_export.py + popd + + tests-py-torch-compile-be: + name: Test torch compile backend [Python] + needs: [generate-matrix, build] + strategy: + fail-fast: false + matrix: + include: + - repository: pytorch/tensorrt + package-name: torch_tensorrt + pre-script: packaging/pre_build_script.sh + uses: pytorch/tensorrt/.github/workflows/linux-test.yml@gha-ci-infra + with: + job-name: tests-py-torch-compile-be + repository: "pytorch/tensorrt" + ref: "" + test-infra-repository: pytorch/test-infra + test-infra-ref: main + build-matrix: ${{ needs.generate-matrix.outputs.matrix }} + pre-script: ${{ matrix.pre-script }} + script: | + export USE_HOST_DEPS=1 + pushd . + cd tests/py/dynamo + ${CONDA_RUN} python -m pip install --pre pytest-xdist timm transformers parameterized expecttest --use-deprecated=legacy-resolver + ${CONDA_RUN} python -m pytest -n 10 --junitxml=${RUNNER_TEST_RESULTS_DIR}/torch_compile_be_test_results.xml backend/ + ${CONDA_RUN} python -m pytest -n 4 --junitxml=${RUNNER_TEST_RESULTS_DIR}/torch_comple_be_e2e_test_results.xml --ir torch_compile models/test_models.py + popd + + tests-py-dynamo-core: + name: Test dynamo core [Python] + needs: [generate-matrix, build] + strategy: + fail-fast: false + matrix: + include: + - repository: pytorch/tensorrt + package-name: torch_tensorrt + pre-script: packaging/pre_build_script.sh + uses: pytorch/tensorrt/.github/workflows/linux-test.yml@gha-ci-infra + with: + job-name: tests-py-dynamo-core + repository: "pytorch/tensorrt" + ref: "" + test-infra-repository: pytorch/test-infra + test-infra-ref: main + build-matrix: ${{ needs.generate-matrix.outputs.matrix }} + pre-script: ${{ matrix.pre-script }} + script: | + export USE_HOST_DEPS=1 + pushd . + cd tests/py/dynamo + ${CONDA_RUN} python -m pip install --pre pytest-xdist timm transformers parameterized expecttest --use-deprecated=legacy-resolver + ${CONDA_RUN} python -m pytest -n 10 --junitxml=${RUNNER_TEST_RESULTS_DIR}/tests_py_dynamo_core_runtime_test_results.xml runtime/ + ${CONDA_RUN} python -m pytest -n 10 --junitxml=${RUNNER_TEST_RESULTS_DIR}/tests_py_dynamo_core_partitioning_test_results.xml partitioning/ + ${CONDA_RUN} python -m pytest -n 10 --junitxml=${RUNNER_TEST_RESULTS_DIR}/tests_py_dynamo_core_lowering_test_results.xml lowering/ + popd \ No newline at end of file diff --git a/.github/workflows/docgen.yml b/.github/workflows/docgen.yml index 47dd165d86..eabe615c44 100644 --- a/.github/workflows/docgen.yml +++ b/.github/workflows/docgen.yml @@ -10,32 +10,33 @@ on: jobs: build-docs: - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest container: image: docker.io/pytorch/manylinux-builder:cuda12.1 steps: - uses: actions/checkout@v3 with: ref: ${{github.head_ref}} + - name: Select python + run: | + export PATH=/opt/python/cp311-cp311/bin/:$PATH - name: Install base deps run: | - ./packaging/pre_build_script.sh + python3 -m pip install pyyaml + VERSION_SUFFIX=cu121 ./packaging/pre_build_script.sh - name: Get HEAD SHA id: vars run: echo "sha=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT - - name: Get Bazel version - id: bazel_info - run: echo "version=$(cat .bazelversion)" >> $GITHUB_OUTPUT - name: Build Python Package run: | - cp toolchains/ci_workspaces/WORKSPACE.x86_64.cu121.release.rhel WORKSPACE - python -m pip install pip<=23 - python -m pip install --pre -e . --extra-index-url https://download.pytorch.org/whl/nightly/cu121 + python3 -m pip install pip --upgrade + CUDA_HOME=/usr/local/cuda-12.1 CI_BUILD=1 python3 -m pip install --pre -e . --extra-index-url https://download.pytorch.org/whl/nightly/cu121 - name: Generate New Docs run: | cd docsrc - python -m pip install -r requirements.txt - python -c "import torch_tensorrt; print(torch_tensorrt.__version__)" + yum install -y doxygen pandoc + python3 -m pip install -r requirements.txt + python3 -c "import torch_tensorrt; print(torch_tensorrt.__version__)" make html cd .. - uses: stefanzweifel/git-auto-commit-action@v4 diff --git a/.github/workflows/linux-test.yml b/.github/workflows/linux-test.yml new file mode 100644 index 0000000000..a2b1392f4f --- /dev/null +++ b/.github/workflows/linux-test.yml @@ -0,0 +1,194 @@ +name: Test on Linux + +on: + workflow_call: + inputs: + job-name: + description: "Name of task" + default: "" + type: string + repository: + description: 'Repository to checkout, defaults to ""' + default: "" + type: string + ref: + description: 'Reference to checkout, defaults to "nightly"' + default: "nightly" + type: string + pre-script: + description: "Pre script to run prior to build" + default: "" + type: string + test-infra-repository: + description: "Test infra repository to use" + default: "pytorch/test-infra" + type: string + test-infra-ref: + description: "Test infra reference to use" + default: "" + type: string + build-matrix: + description: "Build matrix to utilize" + default: "" + type: string + architecture: + description: Architecture to build for x86_64 for default Linux, or aarch64 for Linux aarch64 builds + required: false + type: string + default: x86_64 + setup-miniconda: + description: Works as stated in actions/checkout, but the default value is recursive + required: false + type: boolean + default: true + script: + description: 'Script to utilize' + default: "python setup.py bdist_wheel" + type: string + continue-on-error: + description: "Prevents a job from failing when a step fails. Set to true to allow a job to pass when exec script step fails." + default: false + type: boolean + upload-artifact: + description: 'Name to give artifacts uploaded from ${RUNNER_ARTIFACT_DIR}' + default: '' + type: string + +jobs: + test: + strategy: + fail-fast: false + matrix: ${{ fromJSON(inputs.build-matrix) }} + env: + PYTHON_VERSION: ${{ matrix.python_version }} + PACKAGE_TYPE: wheel + REPOSITORY: ${{ inputs.repository }} + REF: ${{ inputs.ref }} + CU_VERSION: ${{ matrix.desired_cuda }} + SCRIPT: ${{ inputs.script }} + RUNNER_TEST_RESULTS_DIR: /tmp/test_results + name: ${{ inputs.job-name }}-${{ matrix.desired_cuda }} + runs-on: ${{ matrix.validation_runner }} + container: + image: ${{ matrix.container_image }} + options: ${{ matrix.gpu_arch_type == 'cuda' && '--gpus all' || ' ' }} + # If a build is taking longer than 60 minutes on these runners we need + # to have a conversation + timeout-minutes: 60 + steps: + - name: Clean workspace + run: | + set -euxo pipefail + echo "::group::Cleanup debug output" + rm -rfv "${GITHUB_WORKSPACE}" + mkdir -p "${GITHUB_WORKSPACE}" + echo "::endgroup::" + - uses: actions/checkout@v3 + with: + # Support the use case where we need to checkout someone's fork + repository: ${{ inputs.test-infra-repository }} + ref: ${{ inputs.test-infra-ref }} + path: test-infra + - name: Setup SSH + uses: ./test-infra/.github/actions/setup-ssh + with: + github-secret: ${{ github.token }} + - uses: ./test-infra/.github/actions/set-channel + - uses: ./test-infra/.github/actions/setup-binary-builds + with: + repository: ${{ inputs.repository }} + ref: ${{ inputs.ref }} + setup-miniconda: ${{ inputs.setup-miniconda }} + python-version: ${{ env.PYTHON_VERSION }} + - name: Run Pre-Script with Caching + if: ${{ inputs.pre-script != '' }} + uses: ./test-infra/.github/actions/run-script-with-cache + with: + cache-path: ${{ inputs.cache-path }} + cache-key: ${{ inputs.cache-key }} + repository: ${{ inputs.repository }} + script: ${{ inputs.pre-script }} + - name: Download artifacts + uses: actions/download-artifact@v3 + with: + name: ${{ env.ARTIFACT_NAME }} + path: /opt/torch-tensorrt-builds/ + # - name: Install torch and torch-tensorrt + # if: ${{ inputs.pre-script != '' }} + # uses: ./test-infra/.github/actions/run-script-with-cache + # with: + # repository: ${{ inputs.repository }} + # script: .github/scripts/install-torch-tensorrt.sh + - name: Pack script + continue-on-error: ${{ inputs.continue-on-error }} + working-directory: ${{ inputs.repository }} + env: + ALL_SECRETS: ${{ toJSON(secrets) }} + run: | + set -euxo pipefail + # shellcheck disable=SC2086 + source "${BUILD_ENV_FILE}" + { + echo "${SCRIPT}"; + } > "user_script" + cat .github/scripts/install-torch-tensorrt.sh user_script > exec_script + - name: Run Script + uses: ./test-infra/.github/actions/run-script-with-cache + with: + repository: ${{ inputs.repository }} + script: exec_script + - name: Surface failing tests + if: always() + uses: pmeier/pytest-results-action@v0.3.0 + with: + path: ${{ env.RUNNER_TEST_RESULTS_DIR }} + fail-on-empty: false + + - name: Prepare artifacts for upload + working-directory: ${{ inputs.repository }} + id: check-artifacts + env: + UPLOAD_ARTIFACT_NAME: ${{ inputs.upload-artifact }} + run: | + # Only do these steps if we actually want to upload an artifact + if [[ -n "${UPLOAD_ARTIFACT_NAME}" ]]; then + # If the default execution path is followed then we should get a wheel in the dist/ folder + # attempt to just grab whatever is in there and scoop it all up + if find "dist/" -name "*.whl" >/dev/null 2>/dev/null; then + mv -v dist/*.whl "${RUNNER_ARTIFACT_DIR}/" + fi + # Set to fail upload step if there are no files for upload and expected files for upload + echo 'if-no-files-found=error' >> "${GITHUB_OUTPUT}" + fi + + upload_docs=0 + # Check if there are things in the documentation folder to uplaod + if find "${RUNNER_DOCS_DIR}" -mindepth 1 -maxdepth 1 | read -r; then + # TODO: Add a check here to test if on ec2 because if we're not on ec2 then this + # upload will probably not work correctly + upload_docs=1 + fi + echo "upload-docs=${upload_docs}" >> "${GITHUB_OUTPUT}" + + - name: Upload artifacts to GitHub (if any) + uses: actions/upload-artifact@v3 + if: ${{ inputs.upload-artifact != '' }} + with: + name: ${{ inputs.upload-artifact }} + path: ${{ runner.temp }}/artifacts/ + if-no-files-found: ${{ steps.check-artifacts.outputs.if-no-files-found }} + + - name: Upload documentation to S3 (if any) + uses: seemethere/upload-artifact-s3@v5 + if: ${{ steps.check-artifacts.outputs.upload-docs == 1 && github.event.pull_request.number != '' }} + with: + retention-days: 14 + s3-bucket: doc-previews + if-no-files-found: error + path: ${{ env.RUNNER_DOCS_DIR }} + # ${{ env.repository }} is $OWNER/$REPO + s3-prefix: ${{ env.REPOSITORY }}/${{ github.event.pull_request.number }} + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}-${{ inputs.repository }}-${{ github.event_name == 'workflow_dispatch' }}-${{ inputs.job-name }} + cancel-in-progress: true \ No newline at end of file diff --git a/.github/workflows/nightlies.yml b/.github/workflows/nightlies.yml new file mode 100644 index 0000000000..31e3b034f6 --- /dev/null +++ b/.github/workflows/nightlies.yml @@ -0,0 +1,27 @@ +name: Cut Nightly + +on: + schedule: + - cron: '30 9 * * *' + workflow_dispatch: + +jobs: + cut_nightly: + runs-on: ubuntu-latest + timeout-minutes: 60 + steps: + - uses: actions/checkout@v3 + with: + ref: main + - name: Create nightly commit + shell: bash + run: | + git config --global user.email "torch-tensorrt.github.bot@nvidia.com" + git config --global user.name "Torch-TensorRT Github Bot" + git fetch origin nightly + HEAD_COMMIT_HASH=$(git rev-parse HEAD) + NIGHTLY_DATE=$(date +"%Y-%m-%d") + # shellcheck disable=SC1083 + NIGHTLY_RELEASE_COMMIT=$(git commit-tree -p FETCH_HEAD HEAD^{tree} -m "${NIGHTLY_DATE} nightly release (${HEAD_COMMIT_HASH})") + # shellcheck disable=SC1083 + git push -f origin "${NIGHTLY_RELEASE_COMMIT}:nightly" \ No newline at end of file diff --git a/WORKSPACE b/WORKSPACE index ae98bc949c..b24384dfe8 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -53,17 +53,15 @@ new_local_repository( http_archive( name = "libtorch", build_file = "@//third_party/libtorch:BUILD", - sha256 = "1ae8366aaf7af7f68f142ba644fe26c837c6fa8347ec6bd9ce605ac60e7f7e5e", strip_prefix = "libtorch", - urls = ["https://download.pytorch.org/libtorch/nightly/cu121/libtorch-cxx11-abi-shared-with-deps-2.1.0.dev20230703%2Bcu121.zip"], + urls = ["https://download.pytorch.org/libtorch/nightly/cu121/libtorch-cxx11-abi-shared-with-deps-latest.zip"], ) http_archive( name = "libtorch_pre_cxx11_abi", build_file = "@//third_party/libtorch:BUILD", - sha256 = "9add4832f4da9223866d85810820b816ab3319d5a227066101eeb6cbb76adb4b", strip_prefix = "libtorch", - urls = ["https://download.pytorch.org/libtorch/nightly/cu121/libtorch-shared-with-deps-2.1.0.dev20230703%2Bcu121.zip"], + urls = ["https://download.pytorch.org/libtorch/nightly/cu121/libtorch-shared-with-deps-latest.zip"], ) # Download these tarballs manually from the NVIDIA website diff --git a/core/runtime/TRTEngine.h b/core/runtime/TRTEngine.h index 1a2df1f2e3..3f165ee0c0 100644 --- a/core/runtime/TRTEngine.h +++ b/core/runtime/TRTEngine.h @@ -1,5 +1,6 @@ #pragma once #include +#include #include #include #include diff --git a/versions.yml b/dev_dep_versions.yml similarity index 100% rename from versions.yml rename to dev_dep_versions.yml diff --git a/packaging/env_vars.txt b/packaging/env_vars.txt new file mode 100644 index 0000000000..46f906b1ff --- /dev/null +++ b/packaging/env_vars.txt @@ -0,0 +1,2 @@ +export CI_BUILD="1" +export RELEASE="1" \ No newline at end of file diff --git a/packaging/post_build_script.sh b/packaging/post_build_script.sh new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packaging/pre_build_script.sh b/packaging/pre_build_script.sh old mode 100644 new mode 100755 index 49a3a187d5..18cd5d9fe2 --- a/packaging/pre_build_script.sh +++ b/packaging/pre_build_script.sh @@ -1,13 +1,16 @@ #!/bin/bash # Install dependencies +python3 -m pip install pyyaml TRT_VERSION=$(python3 -c "import versions; versions.tensorrt_version()") yum-config-manager --add-repo https://developer.download.nvidia.com/compute/cuda/repos/rhel7/x86_64/cuda-rhel7.repo yum check-update -yum install -y ninja-build tensorrt-${TRT_VERSION}.* +yum install -y ninja-build gettext tensorrt-${TRT_VERSION}.* wget https://github.com/bazelbuild/bazelisk/releases/download/v1.17.0/bazelisk-linux-amd64 \ && mv bazelisk-linux-amd64 /usr/bin/bazel \ && chmod +x /usr/bin/bazel -cp toolchains/ci_workspaces/WORKSPACE.x86_64.${VERSION_SUFFIX#*+}.release.rhel WORKSPACE +export TORCH_BUILD_NUMBER=$(python -c "import torch, urllib.parse as ul; print(ul.quote_plus(torch.__version__))") + +cat toolchains/ci_workspaces/WORKSPACE.x86_64.release.rhel.tmpl | envsubst > WORKSPACE export CI_BUILD=1 diff --git a/py/ci/build_whl.sh b/py/ci/build_whl.sh index 0998e87384..12f72a9351 100755 --- a/py/ci/build_whl.sh +++ b/py/ci/build_whl.sh @@ -6,11 +6,23 @@ export CXX=g++ export CUDA_HOME=/usr/local/cuda-12.1 export PROJECT_DIR=/workspace/project -cp -r $CUDA_HOME /usr/local/cuda +rm -rf /usr/local/cuda + +if [[ $CUDA_HOME == "/usr/local/cuda-12.1" ]]; then + cp -r /usr/local/cuda-11.8 /usr/local/cuda + cp -r /usr/local/cuda-12.0/ /usr/local/cuda/ + rsync -a /usr/local/cuda-12.1/ /usr/local/cuda/ + export CUDA_HOME=/usr/local/cuda +else + ln -s $CUDA_HOME /usr/local/cuda +fi build_wheel() { - $1/bin/python -m pip install --upgrade pip - $1/bin/python -m pip wheel . --config-setting="--build-option=--release" --config-setting="--build-option=--ci" -w dist + $1/bin/python -m pip install --upgrade pip setuptools + $1/bin/python -m pip install -r py/requirements.txt + #$1/bin/python -m pip wheel . -w dist + export BUILD_VERSION=$(cd ${PROJECT_DIR} && $1/bin/python3 -c "import versions; versions.torch_tensorrt_version_release()") + CI_BUILD=1 RELEASE=1 $1/bin/python setup.py bdist_wheel } patch_wheel() { @@ -86,7 +98,7 @@ libtorchtrt() { ${PY_DIR}/bin/python -m pip install setuptools wheel auditwheel bazel build //:libtorchtrt --platforms //toolchains:ci_rhel_x86_64_linux -c opt --noshow_progress CUDA_VERSION=$(cd ${PROJECT_DIR} && ${PY_DIR}/bin/python3 -c "import versions; versions.cuda_version()") - TORCHTRT_VERSION=$(cd ${PROJECT_DIR} && ${PY_DIR}/bin/python3 -c "import versions; versions.torch_tensorrt_version()") + TORCHTRT_VERSION=$(cd ${PROJECT_DIR} && ${PY_DIR}/bin/python3 -c "import versions; versions.torch_tensorrt_version_release()") TRT_VERSION=$(cd ${PROJECT_DIR} && ${PY_DIR}/bin/python3 -c "import versions; versions.tensorrt_version()") CUDNN_VERSION=$(cd ${PROJECT_DIR} && ${PY_DIR}/bin/python3 -c "import versions; versions.cudnn_version()") TORCH_VERSION=$(${PY_DIR}/bin/python -c "from torch import __version__;print(__version__.split('+')[0])") @@ -106,7 +118,7 @@ libtorchtrt_pre_cxx11_abi() { ${PY_DIR}/bin/python -m pip install setuptools wheel auditwheel bazel build //:libtorchtrt --config pre_cxx11_abi --platforms //toolchains:ci_rhel_x86_64_linux -c opt --noshow_progress CUDA_VERSION=$(cd ${PROJECT_DIR} && ${PY_DIR}/bin/python3 -c "import versions; versions.cuda_version()") - TORCHTRT_VERSION=$(cd ${PROJECT_DIR} && ${PY_DIR}/bin/python3 -c "import versions; versions.torch_tensorrt_version()") + TORCHTRT_VERSION=$(cd ${PROJECT_DIR} && ${PY_DIR}/bin/python3 -c "import versions; versions.torch_tensorrt_version_release()") TRT_VERSION=$(cd ${PROJECT_DIR} && ${PY_DIR}/bin/python3 -c "import versions; versions.tensorrt_version()") CUDNN_VERSION=$(cd ${PROJECT_DIR} && ${PY_DIR}/bin/python3 -c "import versions; versions.cudnn_version()") TORCH_VERSION=$(${PY_DIR}/bin/python -c "from torch import __version__;print(__version__.split('+')[0])") diff --git a/py/requirements.txt b/py/requirements.txt index 5c91e3684e..0116b23c4f 100644 --- a/py/requirements.txt +++ b/py/requirements.txt @@ -2,8 +2,8 @@ numpy packaging pybind11==2.6.2 --extra-index-url https://download.pytorch.org/whl/nightly/cu121 -torch==2.1.0.dev20230703+cu121 -torchvision==0.16.0.dev20230703+cu121 +torch>=2.1.0.dev,<2.2.0 +torchvision>=0.16.0.dev,<0.17.0 --extra-index-url https://pypi.ngc.nvidia.com tensorrt==8.6.1 pyyaml \ No newline at end of file diff --git a/py/torch_tensorrt/__init__.py b/py/torch_tensorrt/__init__.py index 11c5cc11ec..c24ef3897f 100644 --- a/py/torch_tensorrt/__init__.py +++ b/py/torch_tensorrt/__init__.py @@ -59,9 +59,7 @@ def _find_lib(name: str, paths: List[str]) -> str: ctypes.CDLL(_find_lib(lib, WIN_PATHS)) elif sys.platform.startswith("linux"): - LINUX_PATHS = [ - "/usr/local/cuda-12.1/lib64", - ] + LINUX_PATHS = ["/usr/local/cuda-12.1/lib64", "/usr/lib", "/usr/lib64"] if "LD_LIBRARY_PATH" in os.environ: LINUX_PATHS += os.environ["LD_LIBRARY_PATH"].split(os.path.pathsep) diff --git a/py/torch_tensorrt/_compile.py b/py/torch_tensorrt/_compile.py index 7106a4e6be..08e7f7d424 100644 --- a/py/torch_tensorrt/_compile.py +++ b/py/torch_tensorrt/_compile.py @@ -35,13 +35,13 @@ def _non_fx_input_interface( inputs: Sequence[Input | torch.Tensor | InputTensorSpec], ) -> TypeGuard[List[Input | torch.Tensor]]: - return all(isinstance(i, torch.Tensor | Input) for i in inputs) + return all(isinstance(i, (torch.Tensor, Input)) for i in inputs) def _fx_input_interface( inputs: Sequence[Input | torch.Tensor | InputTensorSpec], ) -> TypeGuard[List[InputTensorSpec | torch.Tensor]]: - return all(isinstance(i, torch.Tensor | InputTensorSpec) for i in inputs) + return all(isinstance(i, (torch.Tensor, InputTensorSpec)) for i in inputs) class _IRType(Enum): diff --git a/pyproject.toml b/pyproject.toml index 3f99213db9..3973f1fdb9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,8 +9,8 @@ requires = [ "typing-extensions>=4.7.0", "future>=0.18.3", "tensorrt>=8.6,<8.7", - #"torch >=2.0.0,<2.1.0", - "torch==2.1.0.dev20230703+cu121", + "torch >=2.1.0.dev,<2.2.0", + #"torch==2.1.0.dev20230731", "pybind11==2.6.2", "numpy", ] @@ -42,8 +42,8 @@ readme = {file = "py/README.md", content-type = "text/markdown"} requires-python = ">=3.8" keywords = ["pytorch", "torch", "tensorrt", "trt", "ai", "artificial intelligence", "ml", "machine learning", "dl", "deep learning", "compiler", "dynamo", "torchscript", "inference"] dependencies = [ - #"torch>=2.0.0,<2.1.0", - "torch==2.1.0.dev20230703+cu121", + "torch >=2.1.0.dev,<2.2.0", + #"torch==2.1.0.dev20230731", "tensorrt>=8.6,<8.7", "packaging>=23", "numpy", diff --git a/setup.py b/setup.py index 0964cc53fe..fec696bbce 100644 --- a/setup.py +++ b/setup.py @@ -18,35 +18,29 @@ from torch.utils import cpp_extension from wheel.bdist_wheel import bdist_wheel -dir_path = os.path.dirname(os.path.realpath(__file__)) + "/py" +import yaml +import re +import os +import subprocess -CXX11_ABI = False -JETPACK_VERSION = None -FX_ONLY = False -LEGACY = False -RELEASE = False -CI_RELEASE = False +from datetime import datetime +from pathlib import Path +from typing import List __version__: str = "0.0.0" __cuda_version__: str = "0.0" __cudnn_version__: str = "0.0" __tensorrt_version__: str = "0.0" - -def load_version_info(): - global __version__ - global __cuda_version__ - global __cudnn_version__ - global __tensorrt_version__ - with open("versions.yml", "r") as stream: - versions = yaml.safe_load(stream) - __version__ = versions["__version__"] - __cuda_version__ = versions["__cuda_version__"] - __cudnn_version__ = versions["__cudnn_version__"] - __tensorrt_version__ = versions["__tensorrt_version__"] +LEGACY_BASE_VERSION_SUFFIX_PATTERN = re.compile("a0$") -load_version_info() +def get_root_dir() -> Path: + return Path( + subprocess.check_output(["git", "rev-parse", "--show-toplevel"]) + .decode("ascii") + .strip() + ) def get_git_revision_short_hash() -> str: @@ -57,6 +51,45 @@ def get_git_revision_short_hash() -> str: ) +def get_base_version() -> str: + root = get_root_dir() + try: + dirty_version = open(root / "version.txt", "r").read().strip() + except FileNotFoundError: + print("# WARNING: Base version not found defaulting BUILD_VERSION to 0.1.0") + dirty_version = "0.1.0" + # Strips trailing a0 from version.txt, not too sure why it's there in the + # first place + return re.sub(LEGACY_BASE_VERSION_SUFFIX_PATTERN, "", dirty_version) + + +def load_dep_info(): + global __cuda_version__ + global __cudnn_version__ + global __tensorrt_version__ + with open("dev_dep_versions.yml", "r") as stream: + versions = yaml.safe_load(stream) + if (gpu_arch_version := os.environ.get("CU_VERSION")) is not None: + __cuda_version__ = ( + (gpu_arch_version[2:])[:-1] + "." + (gpu_arch_version[2:])[-1:] + ) + else: + __cuda_version__ = versions["__cuda_version__"] + __cudnn_version__ = versions["__cudnn_version__"] + __tensorrt_version__ = versions["__tensorrt_version__"] + + +load_dep_info() + +dir_path = str(get_root_dir()) + "/py" + +CXX11_ABI = False +JETPACK_VERSION = None +FX_ONLY = False +LEGACY = False +RELEASE = False +CI_BUILD = False + if "--fx-only" in sys.argv: FX_ONLY = True sys.argv.remove("--fx-only") @@ -65,16 +98,31 @@ def get_git_revision_short_hash() -> str: LEGACY = True sys.argv.remove("--legacy") -if "--release" not in sys.argv: - __version__ = __version__ + "+" + get_git_revision_short_hash() -else: +if "--release" in sys.argv: RELEASE = True sys.argv.remove("--release") +if (release_env_var := os.environ.get("RELEASE")) is not None: + if release_env_var == "1": + RELEASE = True + +if (gpu_arch_version := os.environ.get("CU_VERSION")) is None: + gpu_arch_version = f"cu{__cuda_version__.replace('.','')}" + + +if RELEASE: + __version__ = os.environ.get("BUILD_VERSION") +else: + __version__ = f"{get_base_version()}.dev0+{get_git_revision_short_hash()}" + if "--ci" in sys.argv: sys.argv.remove("--ci") if RELEASE: - CI_RELEASE = True + CI_BUILD = True + +if (ci_env_var := os.environ.get("CI_BUILD")) is not None: + if ci_env_var == "1": + CI_BUILD = True if "--use-cxx11-abi" in sys.argv: sys.argv.remove("--use-cxx11-abi") @@ -158,7 +206,7 @@ def build_libtorchtrt_pre_cxx11_abi(develop=True, use_dist_dir=True, cxx11_abi=F cmd.append("--platforms=//toolchains:jetpack_5.0") print("Jetpack version: 5.0") - if CI_RELEASE: + if CI_BUILD: cmd.append("--platforms=//toolchains:ci_rhel_x86_64_linux") print("CI based build") @@ -415,6 +463,7 @@ def run(self): dir_path + "/../bazel-TensorRT/external/tensorrt/include", dir_path + "/../bazel-tensorrt/external/tensorrt/include", dir_path + "/../", + "/usr/local/cuda", ], extra_compile_args=[ "-Wno-deprecated", diff --git a/toolchains/ci_workspaces/WORKSPACE.x86_64.release.rhel b/toolchains/ci_workspaces/WORKSPACE.x86_64.cu118.release.rhel similarity index 91% rename from toolchains/ci_workspaces/WORKSPACE.x86_64.release.rhel rename to toolchains/ci_workspaces/WORKSPACE.x86_64.cu118.release.rhel index 8d6ff234e8..79c7575d2b 100644 --- a/toolchains/ci_workspaces/WORKSPACE.x86_64.release.rhel +++ b/toolchains/ci_workspaces/WORKSPACE.x86_64.cu118.release.rhel @@ -43,7 +43,7 @@ local_repository( new_local_repository( name = "cuda", build_file = "@//third_party/cuda:BUILD", - path = "/usr/local/cuda-12.1", + path = "/usr/local/cuda-11.8", ) new_local_repository( @@ -58,17 +58,17 @@ new_local_repository( http_archive( name = "libtorch", build_file = "@//third_party/libtorch:BUILD", - sha256 = "1ae8366aaf7af7f68f142ba644fe26c837c6fa8347ec6bd9ce605ac60e7f7e5e", + sha256 = "174579a7ee2a506d063714160c5fc57da428f7935311ef511c8f19820eb14c86", strip_prefix = "libtorch", - urls = ["https://download.pytorch.org/libtorch/nightly/cu121/libtorch-cxx11-abi-shared-with-deps-2.1.0.dev20230703%2Bcu121.zip"], + urls = ["https://download.pytorch.org/libtorch/nightly/cu121/libtorch-cxx11-abi-shared-with-deps-2.1.0.dev20230731%2Bcu121.zip"], ) http_archive( name = "libtorch_pre_cxx11_abi", build_file = "@//third_party/libtorch:BUILD", - sha256 = "9add4832f4da9223866d85810820b816ab3319d5a227066101eeb6cbb76adb4b", + sha256 = "532217063c65354d5534211badadc9c370d889cb1c3fdb295c9b3d0f181bc0ba", strip_prefix = "libtorch", - urls = ["https://download.pytorch.org/libtorch/nightly/cu121/libtorch-shared-with-deps-2.1.0.dev20230703%2Bcu121.zip"], + urls = ["https://download.pytorch.org/libtorch/nightly/cu121/libtorch-shared-with-deps-2.1.0.dev20230731%2Bcu121.zip"], ) #################################################################################### diff --git a/toolchains/ci_workspaces/WORKSPACE.x86_64.release.rhel.tmpl b/toolchains/ci_workspaces/WORKSPACE.x86_64.release.rhel.tmpl new file mode 100644 index 0000000000..00b85034fe --- /dev/null +++ b/toolchains/ci_workspaces/WORKSPACE.x86_64.release.rhel.tmpl @@ -0,0 +1,101 @@ +workspace(name = "Torch-TensorRT") + +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + +http_archive( + name = "rules_python", + sha256 = "863ba0fa944319f7e3d695711427d9ad80ba92c6edd0b7c7443b84e904689539", + strip_prefix = "rules_python-0.22.0", + url = "https://github.com/bazelbuild/rules_python/releases/download/0.22.0/rules_python-0.22.0.tar.gz", +) + +load("@rules_python//python:repositories.bzl", "py_repositories") + +py_repositories() + +http_archive( + name = "rules_pkg", + sha256 = "8f9ee2dc10c1ae514ee599a8b42ed99fa262b757058f65ad3c384289ff70c4b8", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/rules_pkg/releases/download/0.9.1/rules_pkg-0.9.1.tar.gz", + "https://github.com/bazelbuild/rules_pkg/releases/download/0.9.1/rules_pkg-0.9.1.tar.gz", + ], +) + +load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies") + +rules_pkg_dependencies() + +http_archive( + name = "googletest", + sha256 = "755f9a39bc7205f5a0c428e920ddad092c33c8a1b46997def3f1d4a82aded6e1", + strip_prefix = "googletest-5ab508a01f9eb089207ee87fd547d290da39d015", + urls = ["https://github.com/google/googletest/archive/5ab508a01f9eb089207ee87fd547d290da39d015.zip"], +) + +# External dependency for torch_tensorrt if you already have precompiled binaries. +local_repository( + name = "torch_tensorrt", + path = "/opt/circleci/.pyenv/versions/3.10.9/lib/python3.10/site-packages/torch_tensorrt" +) + +# CUDA should be installed on the system locally +new_local_repository( + name = "cuda", + build_file = "@//third_party/cuda:BUILD", + path = "${CUDA_HOME}", +) + +new_local_repository( + name = "cublas", + build_file = "@//third_party/cublas:BUILD", + path = "/usr", +) +############################################################################################################# +# Tarballs and fetched dependencies (default - use in cases when building from precompiled bin and tarballs) +############################################################################################################# + +http_archive( + name = "libtorch", + build_file = "@//third_party/libtorch:BUILD", + strip_prefix = "libtorch", + urls = ["https://download.pytorch.org/libtorch/nightly/cu121/libtorch-cxx11-abi-shared-with-deps-latest.zip"], +) + +http_archive( + name = "libtorch_pre_cxx11_abi", + build_file = "@//third_party/libtorch:BUILD", + strip_prefix = "libtorch", + urls = ["https://download.pytorch.org/libtorch/nightly/cu121/libtorch-shared-with-deps-latest.zip"], +) + +#################################################################################### +# Locally installed dependencies (use in cases of custom dependencies or aarch64) +#################################################################################### + +new_local_repository( + name = "cudnn", + path = "/usr/", + build_file = "@//third_party/cudnn/local:BUILD" +) + +new_local_repository( + name = "tensorrt", + path = "/usr/", + build_file = "@//third_party/tensorrt/local:BUILD" +) + +# ######################################################################### +# # Testing Dependencies (optional - comment out on aarch64) +# ######################################################################### + +load("@rules_python//python:pip.bzl", "pip_parse") + +pip_parse( + name = "devtools_deps", + requirements = "//:requirements-dev.txt", +) + +load("@devtools_deps//:requirements.bzl", "install_deps") + +install_deps() diff --git a/toolchains/ci_workspaces/WORKSPACE.x86_64.release.ubuntu b/toolchains/ci_workspaces/WORKSPACE.x86_64.release.ubuntu index 60e29f2b60..745272d328 100644 --- a/toolchains/ci_workspaces/WORKSPACE.x86_64.release.ubuntu +++ b/toolchains/ci_workspaces/WORKSPACE.x86_64.release.ubuntu @@ -58,17 +58,17 @@ new_local_repository( http_archive( name = "libtorch", build_file = "@//third_party/libtorch:BUILD", - sha256 = "1ae8366aaf7af7f68f142ba644fe26c837c6fa8347ec6bd9ce605ac60e7f7e5e", + sha256 = "174579a7ee2a506d063714160c5fc57da428f7935311ef511c8f19820eb14c86", strip_prefix = "libtorch", - urls = ["https://download.pytorch.org/libtorch/nightly/cu121/libtorch-cxx11-abi-shared-with-deps-2.1.0.dev20230703%2Bcu121.zip"], + urls = ["https://download.pytorch.org/libtorch/nightly/cu121/libtorch-cxx11-abi-shared-with-deps-2.1.0.dev20230731%2Bcu121.zip"], ) http_archive( name = "libtorch_pre_cxx11_abi", build_file = "@//third_party/libtorch:BUILD", - sha256 = "9add4832f4da9223866d85810820b816ab3319d5a227066101eeb6cbb76adb4b", + sha256 = "532217063c65354d5534211badadc9c370d889cb1c3fdb295c9b3d0f181bc0ba", strip_prefix = "libtorch", - urls = ["https://download.pytorch.org/libtorch/nightly/cu121/libtorch-shared-with-deps-2.1.0.dev20230703%2Bcu121.zip"], + urls = ["https://download.pytorch.org/libtorch/nightly/cu121/libtorch-shared-with-deps-2.1.0.dev20230731%2Bcu121.zip"], ) #################################################################################### diff --git a/version.txt b/version.txt new file mode 100644 index 0000000000..32408126ec --- /dev/null +++ b/version.txt @@ -0,0 +1 @@ +2.0.0a0 \ No newline at end of file diff --git a/versions.py b/versions.py index f038aabb24..772737aab7 100644 --- a/versions.py +++ b/versions.py @@ -1,29 +1,141 @@ import yaml +import re +import os +import subprocess -__version__: str = "0.0.0" -__cuda_version__: str = "0.0" -__cudnn_version__: str = "0.0" -__tensorrt_version__: str = "0.0" +from datetime import datetime +from pathlib import Path +from typing import List +__version__ = "0.0.0" +__cuda_version__ = "0.0" +__cudnn_version__ = "0.0" +__tensorrt_version__ = "0.0" -def load_version_info(): - global __version__ + +LEADING_V_PATTERN = re.compile("^v") +TRAILING_RC_PATTERN = re.compile("-rc[0-9]*$") +LEGACY_BASE_VERSION_SUFFIX_PATTERN = re.compile("a0$") + + +class NoGitTagException(Exception): + pass + + +def get_root_dir() -> Path: + return Path( + subprocess.check_output(["git", "rev-parse", "--show-toplevel"]) + .decode("ascii") + .strip() + ) + + +def get_tag() -> str: + root = get_root_dir() + # We're on a tag + am_on_tag = ( + subprocess.run( + ["git", "describe", "--tags", "--exact"], + cwd=root, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + ).returncode + == 0 + ) + tag = "" + if am_on_tag: + dirty_tag = ( + subprocess.check_output(["git", "describe"], cwd=root) + .decode("ascii") + .strip() + ) + # Strip leading v that we typically do when we tag branches + # ie: v1.7.1 -> 1.7.1 + tag = re.sub(LEADING_V_PATTERN, "", dirty_tag) + # Strip trailing rc pattern + # ie: 1.7.1-rc1 -> 1.7.1 + tag = re.sub(TRAILING_RC_PATTERN, "", tag) + return tag + + +def get_base_version() -> str: + root = get_root_dir() + try: + dirty_version = open(root / "version.txt", "r").read().strip() + except FileNotFoundError: + print("# WARNING: Base version not found defaulting BUILD_VERSION to 0.1.0") + dirty_version = "0.1.0" + # Strips trailing a0 from version.txt, not too sure why it's there in the + # first place + return re.sub(LEGACY_BASE_VERSION_SUFFIX_PATTERN, "", dirty_version) + + +class PytorchVersion: + def __init__( + self, + gpu_arch_version: str, + no_build_suffix: bool, + base_build_version: str, + ) -> None: + self.gpu_arch_version = gpu_arch_version + self.no_build_suffix = no_build_suffix + if base_build_version == "": + base_build_version = get_base_version() + self.base_build_version = base_build_version + + def get_post_build_suffix(self) -> str: + if self.no_build_suffix or self.gpu_arch_version is None: + return "" + return f"+{self.gpu_arch_version}" + + def get_release_version(self) -> str: + if self.base_build_version: + return f"{self.base_build_version}{self.get_post_build_suffix()}" + if not get_tag(): + raise NoGitTagException( + "Not on a git tag, are you sure you want a release version?" + ) + return f"{get_tag()}{self.get_post_build_suffix()}" + + def get_nightly_version(self) -> str: + date_str = datetime.today().strftime("%Y%m%d") + build_suffix = self.get_post_build_suffix() + return f"{self.base_build_version}.dev{date_str}{build_suffix}" + + +def load_dep_info(): global __cuda_version__ global __cudnn_version__ global __tensorrt_version__ - with open("versions.yml", "r") as stream: + with open("dev_dep_versions.yml", "r") as stream: versions = yaml.safe_load(stream) - __version__ = versions["__version__"] - __cuda_version__ = versions["__cuda_version__"] + gpu_arch_version = os.environ.get("CU_VERSION") + if gpu_arch_version is not None: + __cuda_version__ = ( + (gpu_arch_version[2:])[:-1] + "." + (gpu_arch_version[2:])[-1:] + ) + else: + __cuda_version__ = versions["__cuda_version__"] __cudnn_version__ = versions["__cudnn_version__"] __tensorrt_version__ = versions["__tensorrt_version__"] -load_version_info() +load_dep_info() + +gpu_arch_version = os.environ.get("CU_VERSION") +version = PytorchVersion( + gpu_arch_version=gpu_arch_version, + no_build_suffix=False, + base_build_version=get_base_version(), +) + + +def torch_tensorrt_version_nightly(): + print(version.get_nightly_version()) -def torch_tensorrt_version(): - print(__version__) +def torch_tensorrt_version_release(): + print(version.get_release_version()) def cuda_version():