From 190ca7233089a8ca1d53bc448d4cd5ff8719e5c1 Mon Sep 17 00:00:00 2001 From: Xuehai Pan Date: Fri, 10 May 2024 17:19:14 +0800 Subject: [PATCH] deps(torch): support PyTorch 2.0+ only (#215) --- .github/workflows/build.yml | 4 +-- .github/workflows/lint.yml | 2 +- .github/workflows/tests.yml | 2 +- .pre-commit-config.yaml | 4 +-- CHANGELOG.md | 2 +- CMakeLists.txt | 2 +- Dockerfile | 2 +- Makefile | 8 ++++-- README.md | 6 ++-- conda-recipe-minimal-cpu.yaml | 8 +++--- conda-recipe-minimal.yaml | 18 ++++++------ conda-recipe.yaml | 40 +++++++++++++------------- docs/conda-recipe.yaml | 16 +++++------ docs/requirements.txt | 2 +- docs/source/developer/contributing.rst | 8 +++--- docs/source/index.rst | 2 +- examples/requirements.txt | 4 +-- pyproject.toml | 19 ++++++------ requirements.txt | 4 +-- tests/helpers.py | 2 +- tests/requirements.txt | 14 ++++----- tutorials/requirements.txt | 6 ++-- 22 files changed, 90 insertions(+), 85 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 894442dd..57857770 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -37,8 +37,8 @@ concurrency: cancel-in-progress: ${{ github.event_name == 'pull_request' }} env: - CUDA_VERSION: "11.7" - TEST_TORCH_SPECS: "cpu cu116" + CUDA_VERSION: "12.1" + TEST_TORCH_SPECS: "cpu cu118" jobs: build: diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 62f96340..472d5967 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -16,7 +16,7 @@ concurrency: cancel-in-progress: ${{ github.event_name == 'pull_request' }} env: - CUDA_VERSION: "11.7" + CUDA_VERSION: "12.1" jobs: lint: diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 6e51ecad..24d06fe7 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -27,7 +27,7 @@ concurrency: cancel-in-progress: ${{ github.event_name == 'pull_request' }} env: - CUDA_VERSION: "11.7" + CUDA_VERSION: "12.1" jobs: test: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 8e501e70..621632c9 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -26,11 +26,11 @@ repos: - id: debug-statements - id: double-quote-string-fixer - repo: https://github.com/pre-commit/mirrors-clang-format - rev: v18.1.4 + rev: v18.1.5 hooks: - id: clang-format - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.4.3 + rev: v0.4.4 hooks: - id: ruff args: [--fix, --exit-non-zero-on-fix] diff --git a/CHANGELOG.md b/CHANGELOG.md index 315d24db..70b44628 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,7 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Removed -- +- Drop PyTorch 1.x support by [@XuehaiPan](https://github.com/XuehaiPan) in [#215](https://github.com/metaopt/torchopt/pull/215). ------ diff --git a/CMakeLists.txt b/CMakeLists.txt index bf17f38c..13814dee 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,7 +17,7 @@ cmake_minimum_required(VERSION 3.11) # for FetchContent project(torchopt LANGUAGES CXX) include(FetchContent) -set(PYBIND11_VERSION v2.11.1) +set(PYBIND11_VERSION v2.12.0) if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release) diff --git a/Dockerfile b/Dockerfile index 7295af74..246a81e9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,7 +7,7 @@ # $ docker build --target devel --tag torchopt-devel:latest . # -ARG cuda_docker_tag="11.7.1-cudnn8-devel-ubuntu22.04" +ARG cuda_docker_tag="12.1.0-cudnn8-devel-ubuntu22.04" FROM nvidia/cuda:"${cuda_docker_tag}" AS builder ENV DEBIAN_FRONTEND=noninteractive diff --git a/Makefile b/Makefile index 1c42fcd7..fd25d8d9 100644 --- a/Makefile +++ b/Makefile @@ -236,7 +236,11 @@ docker-devel: docker: docker-base docker-devel docker-run-base: docker-base - docker run --network=host --gpus=all -v /:/host -h ubuntu -it $(PROJECT_NAME):$(COMMIT_HASH) + docker run -it --network=host --gpus=all --shm-size=4gb -v /:/host -v "${PWD}:/workspace" \ + -h ubuntu -w /workspace $(PROJECT_NAME):$(COMMIT_HASH) docker-run-devel: docker-devel - docker run --network=host --gpus=all -v /:/host -h ubuntu -it $(PROJECT_NAME)-devel:$(COMMIT_HASH) + docker run -it --network=host --gpus=all --shm-size=4gb -v /:/host -v "${PWD}:/workspace" \ + -h ubuntu -w /workspace $(PROJECT_NAME)-devel:$(COMMIT_HASH) + +docker-run: docker-run-base diff --git a/README.md b/README.md index 2300f619..91d44a25 100644 --- a/README.md +++ b/README.md @@ -425,11 +425,11 @@ Then run the following command to install TorchOpt from PyPI ([![PyPI](https://i pip3 install torchopt ``` -If the minimum version of PyTorch is not satisfied, `pip` will install/upgrade it for you. Please be careful about the `torch` build for CPU / CUDA support (e.g. `cpu`, `cu116`, `cu117`). +If the minimum version of PyTorch is not satisfied, `pip` will install/upgrade it for you. Please be careful about the `torch` build for CPU / CUDA support (e.g. `cpu`, `cu118`, `cu121`). You may need to specify the extra index URL for the `torch` package: ```bash -pip3 install torchopt --extra-index-url https://download.pytorch.org/whl/cu117 +pip3 install torchopt --extra-index-url https://download.pytorch.org/whl/cu121 ``` See for more information about installing PyTorch. @@ -450,7 +450,7 @@ git clone https://github.com/metaopt/torchopt.git cd torchopt # You may need `CONDA_OVERRIDE_CUDA` if conda fails to detect the NVIDIA driver (e.g. in docker or WSL2) -CONDA_OVERRIDE_CUDA=11.7 conda env create --file conda-recipe-minimal.yaml +CONDA_OVERRIDE_CUDA=12.1 conda env create --file conda-recipe-minimal.yaml conda activate torchopt make install-editable # or run `pip3 install --no-build-isolation --editable .` diff --git a/conda-recipe-minimal-cpu.yaml b/conda-recipe-minimal-cpu.yaml index c872924d..dda60369 100644 --- a/conda-recipe-minimal-cpu.yaml +++ b/conda-recipe-minimal-cpu.yaml @@ -26,11 +26,11 @@ channels: - conda-forge dependencies: - - python = 3.10 + - python = 3.11 - pip # Learning - - pytorch::pytorch >= 1.13 # sync with project.dependencies + - pytorch::pytorch >= 2.0 # sync with project.dependencies - pytorch::torchvision - pytorch::pytorch-mutex = *=*cpu* - pip: @@ -40,10 +40,10 @@ dependencies: - cmake >= 3.11 - make - cxx-compiler - - pybind11 >= 2.10.1 + - pybind11 >= 2.11.1 # Misc - optree >= 0.4.1 - - typing-extensions >= 4.0.0 + - typing-extensions - numpy - python-graphviz diff --git a/conda-recipe-minimal.yaml b/conda-recipe-minimal.yaml index 39ff72cc..7e28d2ef 100644 --- a/conda-recipe-minimal.yaml +++ b/conda-recipe-minimal.yaml @@ -15,41 +15,41 @@ # # Create virtual environment with command: # -# $ CONDA_OVERRIDE_CUDA=11.7 conda env create --file conda-recipe-minimal.yaml +# $ CONDA_OVERRIDE_CUDA=12.1 conda env create --file conda-recipe-minimal.yaml # name: torchopt channels: - pytorch - - nvidia/label/cuda-11.7.1 + - nvidia/label/cuda-12.1.0 - defaults - conda-forge dependencies: - - python = 3.10 + - python = 3.11 - pip # Learning - - pytorch::pytorch >= 1.13 # sync with project.dependencies + - pytorch::pytorch >= 2.0 # sync with project.dependencies - pytorch::torchvision - pytorch::pytorch-mutex = *=*cuda* - pip: - torchviz # Device select - - nvidia/label/cuda-11.7.1::cuda-toolkit = 11.7 + - nvidia/label/cuda-12.1.0::cuda-toolkit = 12.1 # Build toolchain - cmake >= 3.11 - make - cxx-compiler - - nvidia/label/cuda-11.7.1::cuda-nvcc - - nvidia/label/cuda-11.7.1::cuda-cudart-dev - - pybind11 >= 2.10.1 + - nvidia/label/cuda-12.1.0::cuda-nvcc + - nvidia/label/cuda-12.1.0::cuda-cudart-dev + - pybind11 >= 2.11.1 # Misc - optree >= 0.4.1 - - typing-extensions >= 4.0.0 + - typing-extensions - numpy - python-graphviz diff --git a/conda-recipe.yaml b/conda-recipe.yaml index c82eb3c6..9753852b 100644 --- a/conda-recipe.yaml +++ b/conda-recipe.yaml @@ -15,49 +15,49 @@ # # Create virtual environment with command: # -# $ CONDA_OVERRIDE_CUDA=11.7 conda env create --file conda-recipe.yaml +# $ CONDA_OVERRIDE_CUDA=12.1 conda env create --file conda-recipe.yaml # name: torchopt channels: - pytorch - - nvidia/label/cuda-11.7.1 + - nvidia/label/cuda-12.1.0 - defaults - conda-forge dependencies: - - python = 3.10 + - python = 3.11 - pip # Learning - - pytorch::pytorch >= 1.13 # sync with project.dependencies + - pytorch::pytorch >= 2.0 # sync with project.dependencies - pytorch::torchvision - pytorch::pytorch-mutex = *=*cuda* - pip: - torchviz - sphinxcontrib-katex # for documentation - - jax # for tutorials - - jaxlib # for tutorials - - optax # for tutorials - - jaxopt # for tests + - conda-forge::jax # for tutorials + - conda-forge::jaxlib # for tutorials + - conda-forge::optax # for tutorials + - conda-forge::jaxopt # for tests - tensorboard # for examples # Device select - - nvidia/label/cuda-11.7.1::cuda-toolkit = 11.7 + - nvidia/label/cuda-12.1.0::cuda-toolkit = 12.1 # Build toolchain - cmake >= 3.11 - make - cxx-compiler - - nvidia/label/cuda-11.7.1::cuda-nvcc - - nvidia/label/cuda-11.7.1::cuda-cudart-dev + - nvidia/label/cuda-12.1.0::cuda-nvcc + - nvidia/label/cuda-12.1.0::cuda-cudart-dev - patchelf >= 0.14 - - pybind11 >= 2.10.1 + - pybind11 >= 2.11.1 # Misc - optree >= 0.4.1 - - typing-extensions >= 4.0.0 + - typing-extensions - numpy - matplotlib-base - seaborn @@ -83,10 +83,10 @@ dependencies: - pytest - pytest-cov - pytest-xdist - - isort >= 5.11.0 - - conda-forge::black-jupyter >= 22.6.0 - - pylint >= 2.15.0 - - mypy >= 0.990 + - isort + - conda-forge::black-jupyter + - pylint + - mypy - flake8 - flake8-bugbear - flake8-comprehensions @@ -96,8 +96,8 @@ dependencies: - ruff - doc8 - pydocstyle - - clang-format >= 14 - - clang-tools >= 14 # clang-tidy - - cpplint + - conda-forge::clang-format >= 14 + - conda-forge::clang-tools >= 14 # clang-tidy + - conda-forge::cpplint - conda-forge::pre-commit - conda-forge::identify diff --git a/docs/conda-recipe.yaml b/docs/conda-recipe.yaml index b6f1b580..d7d2f288 100644 --- a/docs/conda-recipe.yaml +++ b/docs/conda-recipe.yaml @@ -15,23 +15,23 @@ # # Create virtual environment with command: # -# $ CONDA_OVERRIDE_CUDA=11.7 conda env create --file docs/conda-recipe.yaml +# $ CONDA_OVERRIDE_CUDA=12.1 conda env create --file docs/conda-recipe.yaml # name: torchopt-docs channels: - pytorch - - nvidia/label/cuda-11.7.1 + - nvidia/label/cuda-12.1.0 - defaults - conda-forge dependencies: - - python = 3.10 + - python = 3.11 - pip # Learning - - pytorch::pytorch >= 1.13 # sync with project.dependencies + - pytorch::pytorch >= 2.0 # sync with project.dependencies - pytorch::cpuonly - pytorch::pytorch-mutex = *=*cpu* - pip: @@ -42,13 +42,13 @@ dependencies: - cmake >= 3.11 - make - cxx-compiler - - nvidia/label/cuda-11.7.1::cuda-nvcc - - nvidia/label/cuda-11.7.1::cuda-cudart-dev - - pybind11 >= 2.10.1 + - nvidia/label/cuda-12.1.0::cuda-nvcc + - nvidia/label/cuda-12.1.0::cuda-cudart-dev + - pybind11 >= 2.11.1 # Misc - optree >= 0.4.1 - - typing-extensions >= 4.0.0 + - typing-extensions - numpy - matplotlib-base - seaborn diff --git a/docs/requirements.txt b/docs/requirements.txt index 82bf2d91..c9631b75 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -1,6 +1,6 @@ --extra-index-url https://download.pytorch.org/whl/cpu # Sync with project.dependencies -torch >= 1.13 +torch >= 2.0 --requirement ../requirements.txt diff --git a/docs/source/developer/contributing.rst b/docs/source/developer/contributing.rst index 4e7dd355..e40a564a 100644 --- a/docs/source/developer/contributing.rst +++ b/docs/source/developer/contributing.rst @@ -17,7 +17,7 @@ Before contributing to TorchOpt, please follow the instructions below to setup. .. code-block:: bash # You may need `CONDA_OVERRIDE_CUDA` if conda fails to detect the NVIDIA driver (e.g. in docker or WSL2) - CONDA_OVERRIDE_CUDA=11.7 conda env create --file conda-recipe.yaml + CONDA_OVERRIDE_CUDA=12.1 conda env create --file conda-recipe.yaml conda activate torchopt @@ -91,14 +91,14 @@ To build compatible **manylinux2014** (:pep:`599`) wheels for distribution, you pip3 install --upgrade cibuildwheel - export TEST_TORCH_SPECS="cpu cu116" # `torch` builds for testing - export CUDA_VERSION="11.7" # version of `nvcc` for compilation + export TEST_TORCH_SPECS="cpu cu118" # `torch` builds for testing + export CUDA_VERSION="12.1" # version of `nvcc` for compilation python3 -m cibuildwheel --platform=linux --output-dir=wheelhouse --config-file=pyproject.toml It will install the CUDA compiler with ``CUDA_VERSION`` in the build container. Then build wheel binaries for all supported CPython versions. The outputs will be placed in the ``wheelhouse`` directory. To build a wheel for a specific CPython version, you can use the |CIBW_BUILD|_ environment variable. -For example, the following command will build a wheel for Python 3.7: +For example, the following command will build a wheel for Python 3.8: .. code-block:: bash diff --git a/docs/source/index.rst b/docs/source/index.rst index ec9a3749..83602090 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -42,7 +42,7 @@ You can use the following commands with `conda ` cd torchopt # You may need `CONDA_OVERRIDE_CUDA` if conda fails to detect the NVIDIA driver (e.g. in docker or WSL2) - CONDA_OVERRIDE_CUDA=11.7 conda env create --file conda-recipe-minimal.yaml + CONDA_OVERRIDE_CUDA=12.1 conda env create --file conda-recipe-minimal.yaml conda activate torchopt diff --git a/examples/requirements.txt b/examples/requirements.txt index 76bed365..48945c62 100644 --- a/examples/requirements.txt +++ b/examples/requirements.txt @@ -1,5 +1,5 @@ ---extra-index-url https://download.pytorch.org/whl/cu117 -torch >= 1.13 +--extra-index-url https://download.pytorch.org/whl/cu121 +torch >= 2.0 torchvision --requirement ../requirements.txt diff --git a/pyproject.toml b/pyproject.toml index f3a5645b..17d14b05 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,7 +2,7 @@ [build-system] # Sync with project.dependencies -requires = ["setuptools", "torch >= 1.13", "numpy", "pybind11 >= 2.10.1"] +requires = ["setuptools", "torch >= 2.0", "numpy", "pybind11 >= 2.11.1"] build-backend = "setuptools.build_meta" [project] @@ -51,11 +51,11 @@ classifiers = [ ] dependencies = [ # See also build-system.requires and project.requires-python - "torch >= 1.13", + "torch >= 2.0", "optree >= 0.4.1", "numpy", "graphviz", - "typing-extensions >= 4.0.0", + "typing-extensions", ] dynamic = ["version"] @@ -68,9 +68,9 @@ Documentation = "https://torchopt.readthedocs.io" [project.optional-dependencies] lint = [ "isort", - "black[jupyter] >= 22.6.0", - "pylint[spelling] >= 2.15.0", - "mypy >= 0.990", + "black[jupyter]", + "pylint[spelling]", + "mypy", "flake8", "flake8-bugbear", "flake8-comprehensions", @@ -88,7 +88,7 @@ test = [ "pytest", "pytest-cov", "pytest-xdist", - "jax[cpu] >= 0.3; platform_system != 'Windows'", + "jax[cpu] >= 0.4; platform_system != 'Windows'", "jaxopt; platform_system != 'Windows'", "optax; platform_system != 'Windows'", ] @@ -113,8 +113,8 @@ build-verbosity = 3 environment.USE_FP16 = "ON" environment.CUDACXX = "/usr/local/cuda/bin/nvcc" environment.TORCH_CUDA_ARCH_LIST = "Common" -environment.DEFAULT_CUDA_VERSION = "11.7" -environment.DEFAULT_TEST_TORCH_SPECS = "cpu cu116" +environment.DEFAULT_CUDA_VERSION = "12.1" +environment.DEFAULT_TEST_TORCH_SPECS = "cpu cu118" environment-pass = ["CUDA_VERSION", "TEST_TORCH_SPECS"] container-engine = "docker" test-extras = ["test"] @@ -316,4 +316,5 @@ filterwarnings = [ 'ignore:jax\.numpy\.DeviceArray is deprecated\. Use jax\.Array\.:DeprecationWarning', 'ignore:.*functorch.*deprecate.*:UserWarning', 'ignore:.*Apple Paravirtual device.*:UserWarning', + 'ignore:.*NVML.*:UserWarning', ] diff --git a/requirements.txt b/requirements.txt index 961ddf73..a5151c36 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,6 @@ # Sync with project.dependencies -torch >= 1.13 +torch >= 2.0 optree >= 0.4.1 numpy graphviz -typing-extensions >= 4.0.0 +typing-extensions diff --git a/tests/helpers.py b/tests/helpers.py index 1b624ce9..0dc415d4 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -65,7 +65,7 @@ def parametrize(**argvalues) -> pytest.mark.parametrize: argvalues = list(itertools.product(*tuple(map(argvalues.get, arguments)))) ids = tuple( - '-'.join(f'{arg}({val})' for arg, val in zip(arguments, values)) for values in argvalues + '-'.join(f'{arg}({val!r})' for arg, val in zip(arguments, values)) for values in argvalues ) return pytest.mark.parametrize(arguments, argvalues, ids=ids) diff --git a/tests/requirements.txt b/tests/requirements.txt index 397ac350..ee54732b 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -1,9 +1,9 @@ ---extra-index-url https://download.pytorch.org/whl/cu117 -torch >= 1.13 +--extra-index-url https://download.pytorch.org/whl/cu121 +torch >= 2.0 --requirement ../requirements.txt -jax[cpu] >= 0.3; platform_system != 'Windows' +jax[cpu] >= 0.4; platform_system != 'Windows' jaxopt; platform_system != 'Windows' optax < 0.1.8a0; platform_system != 'Windows' and python_version < '3.9' optax >= 0.1.8; platform_system != 'Windows' and python_version >= '3.9' @@ -11,10 +11,10 @@ optax >= 0.1.8; platform_system != 'Windows' and python_version >= '3.9' pytest pytest-cov pytest-xdist -isort >= 5.11.0 -black[jupyter] >= 22.6.0 -pylint[spelling] >= 2.15.0 -mypy >= 0.990 +isort +black[jupyter] +pylint[spelling] +mypy flake8 flake8-bugbear flake8-comprehensions diff --git a/tutorials/requirements.txt b/tutorials/requirements.txt index ff5a5c42..e8a3be95 100644 --- a/tutorials/requirements.txt +++ b/tutorials/requirements.txt @@ -1,11 +1,11 @@ ---extra-index-url https://download.pytorch.org/whl/cu117 +--extra-index-url https://download.pytorch.org/whl/cu121 # Sync with project.dependencies -torch >= 1.13 +torch >= 2.0 torchvision --requirement ../requirements.txt ipykernel -jax[cpu] >= 0.3 +jax[cpu] >= 0.4 jaxopt optax