No project description provided
Project description
TorchRL
Disclaimer
This library is not officially released yet and is subject to change.
The features are available before an official release so that users and collaborators can get early access and provide feedback. No guarantee of stability, robustness or backward compatibility is provided.
TorchRL is an open-source Reinforcement Learning (RL) library for PyTorch.
It provides pytorch and python-first, low and high level abstractions for RL that are intended to be efficient, documented and properly tested. The code is aimed at supporting research in RL. Most of it is written in python in a highly modular way, such that researchers can easily swap components, transform them or write new ones with little effort.
This repo attempts to align with the existing pytorch ecosystem libraries in that it has a dataset pillar (torchrl/envs), transforms, models, data utilities (e.g. collectors and containers), etc. TorchRL aims at having as few dependencies as possible (python standard library, numpy and pytorch). Common environment libraries (e.g. OpenAI gym) are only optional.
On the low-level end, torchrl comes with a set of highly re-usable functionals for cost functions, returns and data processing.
On the high-level end, torchrl provides:
- multiprocess data collectors;
- efficient and generic replay buffers;
- TensorDict, a convenient data structure to pass data from one object to another without friction;
- An associated
TensorDictModule
class which is functorch-compatible! - interfaces for environments from common libraries (OpenAI gym, deepmind control lab, etc.) and wrappers for parallel execution, as well as a new pytorch-first class of tensor-specification class;
- environment transforms, which process and prepare the data coming out of the environments to be used by the agent;
- various tools for distributed learning (e.g. memory mapped tensors);
- various architectures and models (e.g. actor-critic);
- exploration wrappers and modules;
- various recipes to build models that correspond to the environment being deployed;
- a generic trainer class.
A series of examples are provided with an illustrative purpose:
and many more to come!
We also provide tutorials and demos that give a sense of what the library can do.
Installation
Create a conda environment where the packages will be installed.
Before installing anything, make sure you have the latest version of the ninja
library:
conda create --name torch_rl python=3.9
conda activate torch_rl
pip install ninja
Depending on the use of functorch that you want to make, you may want to install the latest (nightly) pytorch release or the latest stable version of pytorch:
Stable
# For CUDA 10.2
conda install pytorch torchvision cudatoolkit=10.2 -c pytorch
# For CUDA 11.3
conda install pytorch torchvision cudatoolkit=11.3 -c pytorch
# For CPU-only build
conda install pytorch torchvision cpuonly -c pytorch
pip install functorch
Nightly
# For CUDA 10.2
pip3 install --pre torch torchvision --extra-index-url https://download.pytorch.org/whl/nightly/cu102
# For CUDA 11.3
pip3 install --pre torch torchvision --extra-index-url https://download.pytorch.org/whl/nightly/cu113
# For CPU-only build
pip3 install --pre torch torchvision --extra-index-url https://download.pytorch.org/whl/nightly/cpu
and functorch
pip install "git+https://github.com/pytorch/functorch.git"
If the generation of this artifact in MacOs M1 doesn't work correctly or in the execution the message
(mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e'))
appears, then try
ARCHFLAGS="-arch arm64" pip install "git+https://github.com/pytorch/functorch.git"
Torchrl
You can install the latest release by using
pip install torchrl
This should work on linux and MacOs (not M1). For Windows and M1/M2 machines, one should install the library locally (see below).
To install extra dependencies, call
pip install "torchrl[atari,dm_control,gym_continuous,rendering,tests,utils]"
or a subset of these.
Alternatively, as the library is at an early stage, it may be wise to install it in develop mode as this will make it possible to pull the latest changes and benefit from them immediately. Start by cloning the repo:
git clone https://github.com/facebookresearch/rl
Go to the directory where you have cloned the torchrl repo and install it
cd /path/to/torchrl/
python setup.py develop
If the generation of this artifact in MacOs M1 doesn't work correctly or in the execution the message
(mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e'))
appears, then try
ARCHFLAGS="-arch arm64" python setup.py develop
To run a quick sanity check, leave that directory (e.g. by executing cd ~/
)
and try to import the library.
python -c "import torchrl"
This should not return any warning or error.
Optional dependencies
The following libraries can be installed depending on the usage one wants to make of torchrl:
# diverse
pip install tqdm tensorboard "hydra-core>=1.1" hydra-submitit-launcher
# rendering
pip install moviepy
# deepmind control suite
pip install dm_control
# gym, atari games
pip install gym "gym[accept-rom-license]" pygame gym_retro
# tests
pip install pytest pyyaml pytest-instafail
Troubleshooting
If a ModuleNotFoundError: No module named ‘torchrl._torchrl
errors occurs,
it means that the C++ extensions were not installed or not found.
One common reason might be that you are trying to import torchrl from within the
git repo location. Indeed the following code snippet should return an error if
torchrl has not been installed in develop
mode:
cd ~/path/to/rl/repo
python -c 'from torchrl.envs import GymEnv'
If this is the case, consider executing torchrl from another location.
On MacOs, we recommend installing XCode first. With Apple Silicon M1 chips, make sure you are using the arm64-built python (e.g. here). Running the following lines of code
wget https://raw.githubusercontent.com/pytorch/pytorch/master/torch/utils/collect_env.py
python collect_env.py
should display
OS: macOS *** (arm64)
and not
OS: macOS **** (x86_64)
Running examples
Examples are coded in a very similar way but the configuration may change from one algorithm to another (e.g. async/sync data collection, hyperparameters, ratio of model updates / frame etc.)
To train an algorithm it is therefore advised to use the predefined configurations that are found in the configs
sub-folder in each algorithm directory:
python examples/ppo/ppo.py --config=examples/ppo/configs/humanoid.txt
Note that using the config files requires the configargparse library.
One can also overwrite the config parameters using flags, e.g.
python examples/ppo/ppo.py --config=examples/ppo/configs/humanoid.txt --frame_skip=2 --collection_devices=cuda:1
Each example will write a tensorboard log in a dedicated folder, e.g. ppo_logging/...
.
Contributing
Internal collaborations to torchrl are welcome! Feel free to fork, submit issues and PRs. You can checkout the detailed contribution guide here.
Contributors are recommended to install pre-commit hooks (using pre-commit install
). pre-commit will check for linting related issues when the code is commited locally. You can disable th check by appending -n
to your commit command: git commit -m <commit message> -n
Upcoming features
In the near future, we plan to:
- provide tutorials on how to design new actors or environment wrappers;
- implement IMPALA (as a distributed RL example) and Meta-RL algorithms;
- improve the tests, documentation and nomenclature.
License
TorchRL is licensed under the MIT License. See LICENSE for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for torchrl-0.0.1b0-cp310-cp310-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7b78041f8a20ded4fa1f264694185b3259578dbd40fa894cae046fa77b331caf |
|
MD5 | c6683e5e3b3baf48adf5279ef51680f2 |
|
BLAKE2b-256 | 1b26b55feabe4123e4f0f50aef0093f442f8b00c9bd5f85d233fd63a2c70ef9c |
Hashes for torchrl-0.0.1b0-cp310-cp310-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3f5f1f022ecac8c92f22bc515e3a4ba54be8066137045e5ce60141ad2e216fb9 |
|
MD5 | a47587d63b1a490c0a9a619cb4a4cc67 |
|
BLAKE2b-256 | a1bbe189a0d1d989435ada1428025566e51cfffe05f3b9c59559fa9b3b7fe318 |
Hashes for torchrl-0.0.1b0-cp39-cp39-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8a19aef0fda171b0444588de908bdca04d7818eecf5ca8f44b8b31e40628a9ee |
|
MD5 | c2273f77b79f0da6afb84045a7014fe1 |
|
BLAKE2b-256 | db33212542a742b2d4c924a172579c851715170b5b2e5b0dd99dc1b3a0476542 |
Hashes for torchrl-0.0.1b0-cp39-cp39-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eef8444ef1cb627e930353dfb911dd1d850762e814bc2d989ff5e2de06039883 |
|
MD5 | f34b7621cfdcfe0de71eda55d10570f8 |
|
BLAKE2b-256 | dc99e4e400b764a5afc3880ca0613c9e64b41b08f6b27c72ace94dbffd93fc1f |
Hashes for torchrl-0.0.1b0-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce44f628f5b04e1da2ad8189a3b7c109568d7b0c69cb0eb0528f6e8017d8a7ae |
|
MD5 | 46d5ddbb17c8a4b5f8874a4b442edd72 |
|
BLAKE2b-256 | eb2c107fe3fc318f97997fce95073078bf6b7c75b0d4f99334f39e6af69ed227 |
Hashes for torchrl-0.0.1b0-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 215fc71f66456fbbcd829bf5a8c7d2fa3fcec20473c7082402195e023723ea37 |
|
MD5 | dd7fbfc16bf3b5eb80e3ba92b40bedc7 |
|
BLAKE2b-256 | 0b581ecf03bae456752e522f8744b593244aa3bd5a3149ed6554f52afa75c181 |
Hashes for torchrl-0.0.1b0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5f012e265881273a534f1c015b487692455a1e98da4f33a589e7532ca68bdac2 |
|
MD5 | 44e40dd87bce55042662574367429aaa |
|
BLAKE2b-256 | da60caee73d9767c776a552ad0004dcdb10936c3a1289a7d18854fb98d7104fd |
Hashes for torchrl-0.0.1b0-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dcaef226b823698e021723afb99a0750717c4c194d64f19fa895843bc80ff62c |
|
MD5 | 57b60f33024b6868bb201299d6514cdb |
|
BLAKE2b-256 | 6bfaae40c3738710c9f8c2422bbbb0c65aec9eba96d2cdc9735338c69013e01e |