From f58862bd428819a31b8c15ac7dd67ff43ea9e162 Mon Sep 17 00:00:00 2001 From: Vincent Moens Date: Mon, 31 Oct 2022 10:37:11 +0000 Subject: [PATCH] [Version] Updating to torch 1.13 (#627) * init * new release updates * tutorials * amend * lint * lint * lint --- .circleci/config.yml | 2 +- .../scripts_gym_0_13/run_test.sh | 3 +- .../unittest/linux_stable/scripts/install.sh | 5 +-- .clang-format | 2 +- .github/workflows/wheels.yml | 15 +++---- README.md | 2 +- test/test_trainer.py | 2 + torchrl/trainers/trainers.py | 15 ++++--- tutorials/src/envs.py | 12 ++++++ tutorials/train_demo.ipynb | 41 +++++++++++++++++++ 10 files changed, 74 insertions(+), 25 deletions(-) create mode 100644 tutorials/src/envs.py create mode 100644 tutorials/train_demo.ipynb diff --git a/.circleci/config.yml b/.circleci/config.yml index e27f5619327..95865519aef 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -398,7 +398,7 @@ jobs: bash <(curl -s https://codecov.io/bash) -Z -F habitat-gpu - run: name: Post Process - command: docker run -t --gpus all -v $PWD:$PWD .circleci/unittest/linux_libs/scripts_habitat/post_process.sh + command: docker run -t --gpus all -v $PWD:$PWD -w $PWD "${image_name}" .circleci/unittest/linux_libs/scripts_habitat/post_process.sh - store_test_results: path: test-results diff --git a/.circleci/unittest/linux_olddeps/scripts_gym_0_13/run_test.sh b/.circleci/unittest/linux_olddeps/scripts_gym_0_13/run_test.sh index a56d64c3c45..5f0377ba540 100755 --- a/.circleci/unittest/linux_olddeps/scripts_gym_0_13/run_test.sh +++ b/.circleci/unittest/linux_olddeps/scripts_gym_0_13/run_test.sh @@ -22,7 +22,8 @@ export MKL_THREADING_LAYER=GNU coverage run -m pytest test/smoke_test.py -v --durations 20 coverage run -m pytest test/smoke_test_deps.py -v --durations 20 -k 'test_gym or test_dm_control_pixels or test_dm_control' -MUJOCO_GL=egl coverage run -m xvfb-run -a pytest --instafail -v --durations 20 +#MUJOCO_GL=egl coverage run -m xvfb-run -a pytest --instafail -v --durations 20 +MUJOCO_GL=egl coverage run -m pytest --instafail -v --durations 20 #pytest --instafail -v --durations 20 #python test/test_libs.py coverage xml -i diff --git a/.circleci/unittest/linux_stable/scripts/install.sh b/.circleci/unittest/linux_stable/scripts/install.sh index 69dfef80b20..f98dfab5d95 100755 --- a/.circleci/unittest/linux_stable/scripts/install.sh +++ b/.circleci/unittest/linux_stable/scripts/install.sh @@ -33,11 +33,8 @@ else pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113 fi -printf "Installing functorch\n" -pip3 install functorch - # smoke test -python -c "import functorch" +python -c "import torch;import functorch" printf "* Installing torchrl\n" printf "g++ version: " diff --git a/.clang-format b/.clang-format index 22fb9e69c3f..1eca2764d32 100644 --- a/.clang-format +++ b/.clang-format @@ -4,8 +4,8 @@ Language: Cpp AccessModifierOffset: -1 AlignAfterOpenBracket: Align AlignConsecutiveMacros: false -AlignConsecutiveAssignments: false AlignConsecutiveDeclarations: false +AlignConsecutiveAssignments: false AlignEscapedNewlines: Left AlignOperands: true AlignTrailingComments: true diff --git a/.github/workflows/wheels.yml b/.github/workflows/wheels.yml index dc80f55ba53..0c354fb09f2 100644 --- a/.github/workflows/wheels.yml +++ b/.github/workflows/wheels.yml @@ -18,11 +18,10 @@ jobs: steps: - name: Checkout torchrl uses: actions/checkout@v2 - - name: Install PyTorch 1.12 RC + - name: Install PyTorch RC run: | export PATH="/opt/python/${{ matrix.python_version[1] }}/bin:$PATH" - python3 -mpip install torch==1.12 ${{ matrix.cuda_support[1] }} - python3 -mpip install "git+https://github.com/pytorch/functorch.git@release/0.2" + python3 -mpip install torch ${{ matrix.cuda_support[1] }} - name: Build wheel run: | export PATH="/opt/python/${{ matrix.python_version[1] }}/bin:$PATH" @@ -60,10 +59,9 @@ jobs: architecture: x64 - name: Checkout torchrl uses: actions/checkout@v2 - - name: Install PyTorch 1.12 RC + - name: Install PyTorch RC run: | - python3 -mpip install torch==1.12 --extra-index-url https://download.pytorch.org/whl/cpu - python3 -mpip install "git+https://github.com/pytorch/functorch.git@release/0.2" + python3 -mpip install torch --extra-index-url https://download.pytorch.org/whl/cpu - name: Build wheel run: | export CC=clang CXX=clang++ @@ -95,10 +93,9 @@ jobs: architecture: x64 - name: Checkout torchrl uses: actions/checkout@v2 - - name: Install PyTorch 1.12 RC + - name: Install PyTorch RC run: | - python3 -mpip install torch==1.12 torchvision --extra-index-url https://download.pytorch.org/whl/cpu - python3 -mpip install "git+https://github.com/pytorch/functorch.git@release/0.2" + python3 -mpip install torch torchvision --extra-index-url https://download.pytorch.org/whl/cpu - name: Upgrade pip run: | python3 -mpip install --upgrade pip diff --git a/README.md b/README.md index 476ddb55ce8..0a188c01054 100644 --- a/README.md +++ b/README.md @@ -375,7 +375,7 @@ conda install pytorch torchvision torchaudio cudatoolkit=11.6 -c pytorch -c cond # For CPU-only build conda install pytorch torchvision cpuonly -c pytorch -# Functorch will be integrated in torch from 1.13. As of now, we still need the latest pip release +# For torch 1.12 (and not above), one should install functorch separately: pip3 install functorch ``` diff --git a/test/test_trainer.py b/test/test_trainer.py index 069ac1aaacc..30729b1562f 100644 --- a/test/test_trainer.py +++ b/test/test_trainer.py @@ -115,6 +115,8 @@ def test_selectkeys(self): assert key2 not in td_out.keys() def test_selectkeys_statedict(self): + if not _has_ts: + os.environ["CKPT_BACKEND"] = "torch" trainer = mocking_trainer() key1 = "first key" key2 = "second key" diff --git a/torchrl/trainers/trainers.py b/torchrl/trainers/trainers.py index 7973f07001d..34fed320d8e 100644 --- a/torchrl/trainers/trainers.py +++ b/torchrl/trainers/trainers.py @@ -17,14 +17,6 @@ from torch import nn, optim from torchrl._utils import KeyDependentDefaultDict - -try: - from tqdm import tqdm - - _has_tqdm = True -except ImportError: - _has_tqdm = False - from torchrl._utils import _CKPT_BACKEND from torchrl.collectors.collectors import _DataCollector from torchrl.data import ( @@ -40,6 +32,13 @@ from torchrl.objectives.common import LossModule from torchrl.trainers.loggers import Logger +try: + from tqdm import tqdm + + _has_tqdm = True +except ImportError: + _has_tqdm = False + try: from torchsnapshot import StateDict, Snapshot diff --git a/tutorials/src/envs.py b/tutorials/src/envs.py new file mode 100644 index 00000000000..ea406d97135 --- /dev/null +++ b/tutorials/src/envs.py @@ -0,0 +1,12 @@ +from gym.envs.classic_control.pendulum import PendulumEnv + + +class PendulumWithSafety(PendulumEnv): + def step(self, u): + out = super().step(u) + sin, cos, vel = out[0] + safe = sin > 0 and cos > 0 # some quadrant is considered safe + safety_cost = abs(sin) * float(sin <= 0) + abs(cos) * float(cos <= 0) + out[-1]["safety_cost"] = safety_cost + out[-1]["safe"] = safe + return out diff --git a/tutorials/train_demo.ipynb b/tutorials/train_demo.ipynb new file mode 100644 index 00000000000..7ec2b5f47ee --- /dev/null +++ b/tutorials/train_demo.ipynb @@ -0,0 +1,41 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "c2e7018e-62a9-4d3f-9e75-343e8910e981", + "metadata": {}, + "source": [ + "# TorchRL overview" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3c75a1ad-128c-4a8c-b387-7021dd6767a1", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.3" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}