Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Co-authored-by: conglu1997 <[email protected]>
Co-authored-by: philipjball <[email protected]>
  • Loading branch information
Cong Lu and philipjball committed May 14, 2023
0 parents commit b0500b2
Show file tree
Hide file tree
Showing 125 changed files with 7,542 additions and 0 deletions.
148 changes: 148 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
bin/
logs/
wandb/
online_logs/

fuse.cfg

*.ai

# Generation results
results/

ray/auth.json

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# Custom ignores
.idea/
.vscode/
*.DS_Store
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "synther/REDQ"]
path = synther/REDQ
url = https://github.com/watchernyu/REDQ
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Cong Lu, Philip J. Ball.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
94 changes: 94 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Synthetic Experience Replay

<p align="center">
<img src="figs/diffusion.png" />
</p>

Synthetic Experience Replay (SynthER) is a diffusion-based approach to arbitrarily upsample an RL agent's collected
experience, leading to large gains in sample efficiency and scaling benefits. We integrate SynthER into a variety of
offline and online algorithms in this codebase, including SAC, TD3+BC, IQL, EDAC, and CQL. For further details, please
see the paper:

**_Synthetic Experience Replay_**; Cong Lu*, Philip J. Ball*, Yee Whye Teh, Jack Parker-Holder.

<p align="center">
<a href=https://arxiv.org/abs/2303.06614>View on arXiv</a>
</p>

## Setup

To install, clone the repository and run the following:

```bash
git submodule update --init --recursive
pip install -r requirements.txt
```

## Running Instructions

### Offline RL

Diffusion model training (this automatically generates samples and saves them):

```bash
python3 synther/diffusion/train_diffuser.py --dataset halfcheetah-medium_replay-v2
```

Baseline without SynthER (e.g. on TD3+BC):

```bash
python3 synther/corl/algorithms/td3_bc.py --config synther/corl/yaml/td3_bc/halfcheetah/medium_replay_v2.yaml --checkpoints_path corl_logs/
```

Offline RL training with SynthER:

```bash
# Generating diffusion samples on the fly.
python3 synther/corl/algorithms/td3_bc.py --config synther/corl/yaml/td3_bc/halfcheetah/medium_replay_v2.yaml --checkpoints_path corl_logs/ --name SynthER --diffusion_path path/to/model-100000.pt

# Using saved diffusion samples.
python3 synther/corl/algorithms/td3_bc.py --config synther/corl/yaml/td3_bc/halfcheetah/medium_replay_v2.yaml --checkpoints_path corl_logs/ --name SynthER --diffusion_path path/to/samples.npz
```

### Online RL

Baselines (SAC, REDQ):

```bash
# SAC.
python3 synther/online/online_exp.py --env quadruped-walk-v0 --results_folder online_logs/ --exp_name SAC --gin_config_files 'config/online/sac.gin'

# REDQ.
python3 synther/online/online_exp.py --env quadruped-walk-v0 --results_folder online_logs/ --exp_name REDQ --gin_config_files 'config/online/redq.gin'
```

SynthER (SAC):

```bash
# DMC environments.
python3 synther/online/online_exp.py --env quadruped-walk-v0 --results_folder online_logs/ --exp_name SynthER --gin_config_files 'config/online/sac_synther_dmc.gin' --gin_params 'redq_sac.utd_ratio = 20' 'redq_sac.num_samples = 1000000'

# OpenAI environments (different gin config).
python3 synther/online/online_exp.py --env HalfCheetah-v2 --results_folder online_logs/ --exp_name SynthER --gin_config_files 'config/online/sac_synther_openai.gin' --gin_params 'redq_sac.utd_ratio = 20' 'redq_sac.num_samples = 1000000'
```

## Thinking of adding SynthER to your own algorithm?

Our codebase has everything you need for diffusion with low-dimensional data along with example integrations with RL algorithms.
For a custom use-case, we recommend starting from the training script and `SimpleDiffusionGenerator` class
in `synther/diffusion/train_diffuser.py`. You can modify the hyperparameters specified in `config/resmlp_denoiser.gin`
to suit your own needs.

## Acknowledgements

SynthER builds upon many works and open-source codebases in both diffusion modelling and reinforcement learning. We
would like to particularly thank the authors of:

- [denoising-diffusion-pytorch](https://github.com/lucidrains/denoising-diffusion-pytorch/tree/main/denoising_diffusion_pytorch)
- [REDQ](https://github.com/watchernyu/REDQ)
- [CORL](https://github.com/tinkoff-ai/CORL)

## Contact

Please contact [Cong Lu](mailto:[email protected]) or [Philip Ball](mailto:[email protected]) for any queries.
We welcome any suggestions or contributions!
6 changes: 6 additions & 0 deletions config/online/redq.gin
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# REDQ baseline.

include 'config/online/sac.gin'

redq_sac.utd_ratio = 20
redq_sac.num_Q = 10
7 changes: 7 additions & 0 deletions config/online/sac.gin
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SAC baseline.

redq_sac.start_steps = 5000
redq_sac.utd_ratio = 1
redq_sac.num_Q = 2
redq_sac.n_evals_per_epoch = 10
redq_sac.evaluate_bias = False
45 changes: 45 additions & 0 deletions config/online/sac_synther_dmc.gin
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# SAC (SynthER) for DMC environments.

include 'config/online/sac.gin'

# Higher UTD with synthetic data.
redq_sac.utd_ratio = 20

# SAC/REDQ diffusion args.
redq_sac.disable_diffusion = False
redq_sac.retrain_diffusion_every = 10000
redq_sac.num_samples = 200000
redq_sac.diffusion_sample_ratio = 0.5
redq_sac.diffusion_start = 0
redq_sac.skip_reward_norm = True

# Online setup.
construct_diffusion_model.denoising_network = @ResidualMLPDenoiser
construct_diffusion_model.normalizer_type = 'standard'

# No terminals in DMC.
modelled_terminals = False
redq_sac.model_terminals = %modelled_terminals
split_diffusion_samples.modelled_terminals = %modelled_terminals

# Diffusion denoising network.
ResidualMLPDenoiser.dim_t = 256
ResidualMLPDenoiser.mlp_width = 1024
ResidualMLPDenoiser.num_layers = 6
ResidualMLPDenoiser.learned_sinusoidal_cond = False
ResidualMLPDenoiser.random_fourier_features = True
ResidualMLPDenoiser.learned_sinusoidal_dim = 16
ResidualMLPDenoiser.activation = 'relu'
ResidualMLPDenoiser.layer_norm = False

# Diffusion training.
REDQTrainer.train_batch_size = 256
REDQTrainer.train_lr = 3e-4
REDQTrainer.lr_scheduler = "cosine"
REDQTrainer.weight_decay = 0
REDQTrainer.train_num_steps = 100000
# Don't save.
REDQTrainer.save_and_sample_every = 100000000

# Diffusion sampling.
SimpleDiffusionGenerator.num_sample_steps = 128
11 changes: 11 additions & 0 deletions config/online/sac_synther_openai.gin
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# SAC (SynthER) for OpenAI environments.

include 'config/online/sac_synther_dmc.gin'

# Normalize rewards.
redq_sac.skip_reward_norm = False

# Model terminals.
modelled_terminals = True
redq_sac.model_terminals = %modelled_terminals
split_diffusion_samples.modelled_terminals = %modelled_terminals
43 changes: 43 additions & 0 deletions config/resmlp_denoiser.gin
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Default Residual MLP configuration.

# Model the terminals and discretize them with threshold 0.5.
modelled_terminals = True
make_inputs.modelled_terminals = %modelled_terminals
split_diffusion_samples.modelled_terminals = %modelled_terminals
split_diffusion_samples.terminal_threshold = 0.5

construct_diffusion_model.normalizer_type = 'standard'
construct_diffusion_model.denoising_network = @ResidualMLPDenoiser
# No normalization on the terminals.
construct_diffusion_model.disable_terminal_norm = True

# Network.
ResidualMLPDenoiser.dim_t = 128
ResidualMLPDenoiser.mlp_width = 2048
ResidualMLPDenoiser.num_layers = 6
ResidualMLPDenoiser.learned_sinusoidal_cond = False
ResidualMLPDenoiser.random_fourier_features = True
ResidualMLPDenoiser.learned_sinusoidal_dim = 16
ResidualMLPDenoiser.activation = 'relu'
ResidualMLPDenoiser.layer_norm = False

# Diffusion Model.
ElucidatedDiffusion.num_sample_steps = 128
ElucidatedDiffusion.sigma_data = 1.0
ElucidatedDiffusion.S_churn = 80
ElucidatedDiffusion.S_tmin = 0.05
ElucidatedDiffusion.S_tmax = 50
ElucidatedDiffusion.S_noise = 1.003

# Training.
Trainer.train_batch_size = 1024
Trainer.small_batch_size = 256
Trainer.train_lr = 3e-4
Trainer.lr_scheduler = 'cosine'
Trainer.weight_decay = 0
Trainer.train_num_steps = 100000
Trainer.save_and_sample_every = 10000

# Sampling.
SimpleDiffusionGenerator.num_sample_steps = 128
SimpleDiffusionGenerator.sample_batch_size = 100000
6 changes: 6 additions & 0 deletions config/resmlp_w1024_denoiser.gin
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Default Residual MLP configuration (lower width for some datasets).

include 'config/resmlp_denoiser.gin'

# Network.
ResidualMLPDenoiser.mlp_width = 1024
Binary file added figs/diffusion.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit b0500b2

Please sign in to comment.