sgoodfriend commited on
Commit
5c3cdcb
1 Parent(s): 57c23a8

PPO playing MicrortsRandomEnemyShapedReward3-v1 from https://github.com/sgoodfriend/rl-algo-impls/tree/fb34ab86707f5e2db85e821ff7dbdc624072d640

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitignore +147 -0
  2. LICENSE +21 -0
  3. README.md +143 -0
  4. benchmark_publish.py +4 -0
  5. colab/colab_atari1.sh +4 -0
  6. colab/colab_atari2.sh +4 -0
  7. colab/colab_basic.sh +4 -0
  8. colab/colab_benchmark.ipynb +195 -0
  9. colab/colab_carracing.sh +4 -0
  10. colab/colab_enjoy.ipynb +198 -0
  11. colab/colab_pybullet.sh +4 -0
  12. colab/colab_train.ipynb +200 -0
  13. compare_runs.py +4 -0
  14. enjoy.py +4 -0
  15. environment.yml +12 -0
  16. huggingface_publish.py +4 -0
  17. optimize.py +4 -0
  18. pyproject.toml +69 -0
  19. replay.meta.json +1 -0
  20. replay.mp4 +0 -0
  21. rl_algo_impls/a2c/a2c.py +203 -0
  22. rl_algo_impls/a2c/optimize.py +77 -0
  23. rl_algo_impls/benchmark_publish.py +111 -0
  24. rl_algo_impls/compare_runs.py +199 -0
  25. rl_algo_impls/dqn/dqn.py +182 -0
  26. rl_algo_impls/dqn/policy.py +55 -0
  27. rl_algo_impls/dqn/q_net.py +41 -0
  28. rl_algo_impls/enjoy.py +35 -0
  29. rl_algo_impls/huggingface_publish.py +194 -0
  30. rl_algo_impls/hyperparams/a2c.yml +138 -0
  31. rl_algo_impls/hyperparams/dqn.yml +130 -0
  32. rl_algo_impls/hyperparams/ppo.yml +417 -0
  33. rl_algo_impls/hyperparams/vpg.yml +197 -0
  34. rl_algo_impls/optimize.py +442 -0
  35. rl_algo_impls/ppo/ppo.py +370 -0
  36. rl_algo_impls/publish/markdown_format.py +210 -0
  37. rl_algo_impls/runner/config.py +191 -0
  38. rl_algo_impls/runner/env.py +334 -0
  39. rl_algo_impls/runner/evaluate.py +103 -0
  40. rl_algo_impls/runner/running_utils.py +198 -0
  41. rl_algo_impls/runner/train.py +143 -0
  42. rl_algo_impls/shared/algorithm.py +39 -0
  43. rl_algo_impls/shared/callbacks/callback.py +11 -0
  44. rl_algo_impls/shared/callbacks/eval_callback.py +205 -0
  45. rl_algo_impls/shared/callbacks/optimize_callback.py +117 -0
  46. rl_algo_impls/shared/gae.py +94 -0
  47. rl_algo_impls/shared/module/feature_extractor.py +249 -0
  48. rl_algo_impls/shared/module/module.py +40 -0
  49. rl_algo_impls/shared/policy/actor.py +452 -0
  50. rl_algo_impls/shared/policy/critic.py +28 -0
.gitignore ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ pip-wheel-metadata/
24
+ share/python-wheels/
25
+ *.egg-info/
26
+ .installed.cfg
27
+ *.egg
28
+ MANIFEST
29
+
30
+ # PyInstaller
31
+ # Usually these files are written by a python script from a template
32
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
33
+ *.manifest
34
+ *.spec
35
+
36
+ # Installer logs
37
+ pip-log.txt
38
+ pip-delete-this-directory.txt
39
+
40
+ # Unit test / coverage reports
41
+ htmlcov/
42
+ .tox/
43
+ .nox/
44
+ .coverage
45
+ .coverage.*
46
+ .cache
47
+ nosetests.xml
48
+ coverage.xml
49
+ *.cover
50
+ *.py,cover
51
+ .hypothesis/
52
+ .pytest_cache/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ target/
76
+
77
+ # Jupyter Notebook
78
+ .ipynb_checkpoints
79
+
80
+ # IPython
81
+ profile_default/
82
+ ipython_config.py
83
+
84
+ # pyenv
85
+ .python-version
86
+
87
+ # pipenv
88
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
89
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
90
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
91
+ # install all needed dependencies.
92
+ #Pipfile.lock
93
+
94
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow
95
+ __pypackages__/
96
+
97
+ # Celery stuff
98
+ celerybeat-schedule
99
+ celerybeat.pid
100
+
101
+ # SageMath parsed files
102
+ *.sage.py
103
+
104
+ # Environments
105
+ .env
106
+ .venv
107
+ env/
108
+ venv/
109
+ ENV/
110
+ env.bak/
111
+ venv.bak/
112
+
113
+ # Spyder project settings
114
+ .spyderproject
115
+ .spyproject
116
+
117
+ # Rope project settings
118
+ .ropeproject
119
+
120
+ # mkdocs documentation
121
+ /site
122
+
123
+ # mypy
124
+ .mypy_cache/
125
+ .dmypy.json
126
+ dmypy.json
127
+
128
+ # Pyre type checker
129
+ .pyre/
130
+
131
+ # Logging into tensorboard and wandb
132
+ runs/*
133
+ wandb
134
+
135
+ # macOS
136
+ .DS_STORE
137
+
138
+ # Local scratch work
139
+ scratch/*
140
+
141
+ # vscode
142
+ .vscode/
143
+
144
+ # Don't bother tracking saved_models or videos
145
+ saved_models/*
146
+ downloaded_models/*
147
+ videos/*
LICENSE ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Scott Goodfriend
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
README.md ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: rl-algo-impls
3
+ tags:
4
+ - MicrortsRandomEnemyShapedReward3-v1
5
+ - ppo
6
+ - deep-reinforcement-learning
7
+ - reinforcement-learning
8
+ model-index:
9
+ - name: ppo
10
+ results:
11
+ - metrics:
12
+ - type: mean_reward
13
+ value: 5.29 +/- 0.38
14
+ name: mean_reward
15
+ task:
16
+ type: reinforcement-learning
17
+ name: reinforcement-learning
18
+ dataset:
19
+ name: MicrortsRandomEnemyShapedReward3-v1
20
+ type: MicrortsRandomEnemyShapedReward3-v1
21
+ ---
22
+ # **PPO** Agent playing **MicrortsRandomEnemyShapedReward3-v1**
23
+
24
+ This is a trained model of a **PPO** agent playing **MicrortsRandomEnemyShapedReward3-v1** using the [/sgoodfriend/rl-algo-impls](https://github.com/sgoodfriend/rl-algo-impls) repo.
25
+
26
+ All models trained at this commit can be found at https://api.wandb.ai/links/sgoodfriend/z3kioih3.
27
+
28
+ ## Training Results
29
+
30
+ This model was trained from 3 trainings of **PPO** agents using different initial seeds. These agents were trained by checking out [fb34ab8](https://github.com/sgoodfriend/rl-algo-impls/tree/fb34ab86707f5e2db85e821ff7dbdc624072d640). The best and last models were kept from each training. This submission has loaded the best models from each training, reevaluates them, and selects the best model from these latest evaluations (mean - std).
31
+
32
+ | algo | env | seed | reward_mean | reward_std | eval_episodes | best | wandb_url |
33
+ |:-------|:------------------------------------|-------:|--------------:|-------------:|----------------:|:-------|:-----------------------------------------------------------------------------|
34
+ | ppo | MicrortsRandomEnemyShapedReward3-v1 | 1 | 5.11875 | 0.304094 | 16 | | [wandb](https://wandb.ai/sgoodfriend/rl-algo-impls-benchmarks/runs/4xb7o6x6) |
35
+ | ppo | MicrortsRandomEnemyShapedReward3-v1 | 2 | 1.57281 | 9.87922 | 16 | | [wandb](https://wandb.ai/sgoodfriend/rl-algo-impls-benchmarks/runs/nyzpkp1k) |
36
+ | ppo | MicrortsRandomEnemyShapedReward3-v1 | 3 | 5.29124 | 0.381996 | 16 | * | [wandb](https://wandb.ai/sgoodfriend/rl-algo-impls-benchmarks/runs/71qt3jb7) |
37
+
38
+
39
+ ### Prerequisites: Weights & Biases (WandB)
40
+ Training and benchmarking assumes you have a Weights & Biases project to upload runs to.
41
+ By default training goes to a rl-algo-impls project while benchmarks go to
42
+ rl-algo-impls-benchmarks. During training and benchmarking runs, videos of the best
43
+ models and the model weights are uploaded to WandB.
44
+
45
+ Before doing anything below, you'll need to create a wandb account and run `wandb
46
+ login`.
47
+
48
+
49
+
50
+ ## Usage
51
+ /sgoodfriend/rl-algo-impls: https://github.com/sgoodfriend/rl-algo-impls
52
+
53
+ Note: While the model state dictionary and hyperaparameters are saved, the latest
54
+ implementation could be sufficiently different to not be able to reproduce similar
55
+ results. You might need to checkout the commit the agent was trained on:
56
+ [fb34ab8](https://github.com/sgoodfriend/rl-algo-impls/tree/fb34ab86707f5e2db85e821ff7dbdc624072d640).
57
+ ```
58
+ # Downloads the model, sets hyperparameters, and runs agent for 3 episodes
59
+ python enjoy.py --wandb-run-path=sgoodfriend/rl-algo-impls-benchmarks/71qt3jb7
60
+ ```
61
+
62
+ Setup hasn't been completely worked out yet, so you might be best served by using Google
63
+ Colab starting from the
64
+ [colab_enjoy.ipynb](https://github.com/sgoodfriend/rl-algo-impls/blob/main/colab_enjoy.ipynb)
65
+ notebook.
66
+
67
+
68
+
69
+ ## Training
70
+ If you want the highest chance to reproduce these results, you'll want to checkout the
71
+ commit the agent was trained on: [fb34ab8](https://github.com/sgoodfriend/rl-algo-impls/tree/fb34ab86707f5e2db85e821ff7dbdc624072d640). While
72
+ training is deterministic, different hardware will give different results.
73
+
74
+ ```
75
+ python train.py --algo ppo --env MicrortsRandomEnemyShapedReward3-v1 --seed 3
76
+ ```
77
+
78
+ Setup hasn't been completely worked out yet, so you might be best served by using Google
79
+ Colab starting from the
80
+ [colab_train.ipynb](https://github.com/sgoodfriend/rl-algo-impls/blob/main/colab_train.ipynb)
81
+ notebook.
82
+
83
+
84
+
85
+ ## Benchmarking (with Lambda Labs instance)
86
+ This and other models from https://api.wandb.ai/links/sgoodfriend/z3kioih3 were generated by running a script on a Lambda
87
+ Labs instance. In a Lambda Labs instance terminal:
88
+ ```
89
+ git clone git@github.com:sgoodfriend/rl-algo-impls.git
90
+ cd rl-algo-impls
91
+ bash ./lambda_labs/setup.sh
92
+ wandb login
93
+ bash ./lambda_labs/benchmark.sh [-a {"ppo a2c dqn vpg"}] [-e ENVS] [-j {6}] [-p {rl-algo-impls-benchmarks}] [-s {"1 2 3"}]
94
+ ```
95
+
96
+ ### Alternative: Google Colab Pro+
97
+ As an alternative,
98
+ [colab_benchmark.ipynb](https://github.com/sgoodfriend/rl-algo-impls/tree/main/benchmarks#:~:text=colab_benchmark.ipynb),
99
+ can be used. However, this requires a Google Colab Pro+ subscription and running across
100
+ 4 separate instances because otherwise running all jobs will exceed the 24-hour limit.
101
+
102
+
103
+
104
+ ## Hyperparameters
105
+ This isn't exactly the format of hyperparams in hyperparams/ppo.yml, but instead the Wandb Run Config. However, it's very
106
+ close and has some additional data:
107
+ ```
108
+ algo: ppo
109
+ algo_hyperparams:
110
+ batch_size: 256
111
+ clip_range: 0.1
112
+ clip_range_decay: none
113
+ clip_range_vf: 0.1
114
+ ent_coef: 0.01
115
+ learning_rate: 0.00025
116
+ learning_rate_decay: linear
117
+ n_epochs: 4
118
+ n_steps: 128
119
+ vf_coef: 0.5
120
+ device: auto
121
+ env: MicrortsRandomEnemyShapedReward3-v1-NoMask
122
+ env_hyperparams:
123
+ mask_actions: false
124
+ n_envs: 8
125
+ vec_env_class: sync
126
+ env_id: MicrortsRandomEnemyShapedReward3-v1
127
+ eval_params:
128
+ deterministic: false
129
+ n_timesteps: 2000000
130
+ policy_hyperparams:
131
+ activation_fn: relu
132
+ cnn_feature_dim: 128
133
+ cnn_style: microrts
134
+ seed: 3
135
+ use_deterministic_algorithms: true
136
+ wandb_entity: null
137
+ wandb_group: null
138
+ wandb_project_name: rl-algo-impls-benchmarks
139
+ wandb_tags:
140
+ - benchmark_fb34ab8
141
+ - host_150-230-34-91
142
+
143
+ ```
benchmark_publish.py ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ from rl_algo_impls.benchmark_publish import benchmark_publish
2
+
3
+ if __name__ == "__main__":
4
+ benchmark_publish()
colab/colab_atari1.sh ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ ALGO="ppo"
2
+ ENVS="PongNoFrameskip-v4 BreakoutNoFrameskip-v4"
3
+ BENCHMARK_MAX_PROCS="${BENCHMARK_MAX_PROCS:-3}"
4
+ bash scripts/train_loop.sh -a $ALGO -e "$ENVS" | xargs -I CMD -P $BENCHMARK_MAX_PROCS bash -c CMD
colab/colab_atari2.sh ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ ALGO="ppo"
2
+ ENVS="SpaceInvadersNoFrameskip-v4 QbertNoFrameskip-v4"
3
+ BENCHMARK_MAX_PROCS="${BENCHMARK_MAX_PROCS:-3}"
4
+ bash scripts/train_loop.sh -a $ALGO -e "$ENVS" | xargs -I CMD -P $BENCHMARK_MAX_PROCS bash -c CMD
colab/colab_basic.sh ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ ALGO="ppo"
2
+ ENVS="CartPole-v1 MountainCar-v0 MountainCarContinuous-v0 Acrobot-v1 LunarLander-v2"
3
+ BENCHMARK_MAX_PROCS="${BENCHMARK_MAX_PROCS:-3}"
4
+ bash scripts/train_loop.sh -a $ALGO -e "$ENVS" | xargs -I CMD -P $BENCHMARK_MAX_PROCS bash -c CMD
colab/colab_benchmark.ipynb ADDED
@@ -0,0 +1,195 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": [],
7
+ "machine_shape": "hm",
8
+ "authorship_tag": "ABX9TyOGIH7rqgasim3Sz7b1rpoE",
9
+ "include_colab_link": true
10
+ },
11
+ "kernelspec": {
12
+ "name": "python3",
13
+ "display_name": "Python 3"
14
+ },
15
+ "language_info": {
16
+ "name": "python"
17
+ },
18
+ "gpuClass": "standard",
19
+ "accelerator": "GPU"
20
+ },
21
+ "cells": [
22
+ {
23
+ "cell_type": "markdown",
24
+ "metadata": {
25
+ "id": "view-in-github",
26
+ "colab_type": "text"
27
+ },
28
+ "source": [
29
+ "<a href=\"https://colab.research.google.com/github/sgoodfriend/rl-algo-impls/blob/main/benchmarks/colab_benchmark.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
30
+ ]
31
+ },
32
+ {
33
+ "cell_type": "markdown",
34
+ "source": [
35
+ "# [sgoodfriend/rl-algo-impls](https://github.com/sgoodfriend/rl-algo-impls) in Google Colaboratory\n",
36
+ "## Parameters\n",
37
+ "\n",
38
+ "\n",
39
+ "1. Wandb\n",
40
+ "\n"
41
+ ],
42
+ "metadata": {
43
+ "id": "S-tXDWP8WTLc"
44
+ }
45
+ },
46
+ {
47
+ "cell_type": "code",
48
+ "source": [
49
+ "from getpass import getpass\n",
50
+ "import os\n",
51
+ "os.environ[\"WANDB_API_KEY\"] = getpass(\"Wandb API key to upload metrics, videos, and models: \")"
52
+ ],
53
+ "metadata": {
54
+ "id": "1ZtdYgxWNGwZ"
55
+ },
56
+ "execution_count": null,
57
+ "outputs": []
58
+ },
59
+ {
60
+ "cell_type": "markdown",
61
+ "source": [
62
+ "## Setup\n",
63
+ "Clone [sgoodfriend/rl-algo-impls](https://github.com/sgoodfriend/rl-algo-impls) "
64
+ ],
65
+ "metadata": {
66
+ "id": "bsG35Io0hmKG"
67
+ }
68
+ },
69
+ {
70
+ "cell_type": "code",
71
+ "source": [
72
+ "%%capture\n",
73
+ "!git clone https://github.com/sgoodfriend/rl-algo-impls.git"
74
+ ],
75
+ "metadata": {
76
+ "id": "k5ynTV25hdAf"
77
+ },
78
+ "execution_count": null,
79
+ "outputs": []
80
+ },
81
+ {
82
+ "cell_type": "markdown",
83
+ "source": [
84
+ "Installing the correct packages:\n",
85
+ "\n",
86
+ "While conda and poetry are generally used for package management, the mismatch in Python versions (3.10 in the project file vs 3.8 in Colab) makes using the package yml files difficult to use. For now, instead I'm going to specify the list of requirements manually below:"
87
+ ],
88
+ "metadata": {
89
+ "id": "jKxGok-ElYQ7"
90
+ }
91
+ },
92
+ {
93
+ "cell_type": "code",
94
+ "source": [
95
+ "%%capture\n",
96
+ "!apt install python-opengl\n",
97
+ "!apt install ffmpeg\n",
98
+ "!apt install xvfb\n",
99
+ "!apt install swig"
100
+ ],
101
+ "metadata": {
102
+ "id": "nn6EETTc2Ewf"
103
+ },
104
+ "execution_count": null,
105
+ "outputs": []
106
+ },
107
+ {
108
+ "cell_type": "code",
109
+ "source": [
110
+ "%%capture\n",
111
+ "%cd /content/rl-algo-impls\n",
112
+ "python -m pip install ."
113
+ ],
114
+ "metadata": {
115
+ "id": "AfZh9rH3yQii"
116
+ },
117
+ "execution_count": null,
118
+ "outputs": []
119
+ },
120
+ {
121
+ "cell_type": "markdown",
122
+ "source": [
123
+ "## Run Once Per Runtime"
124
+ ],
125
+ "metadata": {
126
+ "id": "4o5HOLjc4wq7"
127
+ }
128
+ },
129
+ {
130
+ "cell_type": "code",
131
+ "source": [
132
+ "import wandb\n",
133
+ "wandb.login()"
134
+ ],
135
+ "metadata": {
136
+ "id": "PCXa5tdS2qFX"
137
+ },
138
+ "execution_count": null,
139
+ "outputs": []
140
+ },
141
+ {
142
+ "cell_type": "markdown",
143
+ "source": [
144
+ "## Restart Session beteween runs"
145
+ ],
146
+ "metadata": {
147
+ "id": "AZBZfSUV43JQ"
148
+ }
149
+ },
150
+ {
151
+ "cell_type": "code",
152
+ "source": [
153
+ "%%capture\n",
154
+ "from pyvirtualdisplay import Display\n",
155
+ "\n",
156
+ "virtual_display = Display(visible=0, size=(1400, 900))\n",
157
+ "virtual_display.start()"
158
+ ],
159
+ "metadata": {
160
+ "id": "VzemeQJP2NO9"
161
+ },
162
+ "execution_count": null,
163
+ "outputs": []
164
+ },
165
+ {
166
+ "cell_type": "markdown",
167
+ "source": [
168
+ "The below 5 bash scripts train agents on environments with 3 seeds each:\n",
169
+ "- colab_basic.sh and colab_pybullet.sh test on a set of basic gym environments and 4 PyBullet environments. Running both together will likely take about 18 hours. This is likely to run into runtime limits for free Colab and Colab Pro, but is fine for Colab Pro+.\n",
170
+ "- colab_carracing.sh only trains 3 seeds on CarRacing-v0, which takes almost 22 hours on Colab Pro+ on high-RAM, standard GPU.\n",
171
+ "- colab_atari1.sh and colab_atari2.sh likely need to be run separately because each takes about 19 hours on high-RAM, standard GPU."
172
+ ],
173
+ "metadata": {
174
+ "id": "nSHfna0hLlO1"
175
+ }
176
+ },
177
+ {
178
+ "cell_type": "code",
179
+ "source": [
180
+ "%cd /content/rl-algo-impls\n",
181
+ "os.environ[\"BENCHMARK_MAX_PROCS\"] = str(1) # Can't reliably raise this to 2+, but would make it faster.\n",
182
+ "!./benchmarks/colab_basic.sh\n",
183
+ "!./benchmarks/colab_pybullet.sh\n",
184
+ "# !./benchmarks/colab_carracing.sh\n",
185
+ "# !./benchmarks/colab_atari1.sh\n",
186
+ "# !./benchmarks/colab_atari2.sh"
187
+ ],
188
+ "metadata": {
189
+ "id": "07aHYFH1zfXa"
190
+ },
191
+ "execution_count": null,
192
+ "outputs": []
193
+ }
194
+ ]
195
+ }
colab/colab_carracing.sh ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ ALGO="ppo"
2
+ ENVS="CarRacing-v0"
3
+ BENCHMARK_MAX_PROCS="${BENCHMARK_MAX_PROCS:-3}"
4
+ bash scripts/train_loop.sh -a $ALGO -e "$ENVS" | xargs -I CMD -P $BENCHMARK_MAX_PROCS bash -c CMD
colab/colab_enjoy.ipynb ADDED
@@ -0,0 +1,198 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": [],
7
+ "machine_shape": "hm",
8
+ "authorship_tag": "ABX9TyN6S7kyJKrM5x0OOiN+CgTc",
9
+ "include_colab_link": true
10
+ },
11
+ "kernelspec": {
12
+ "name": "python3",
13
+ "display_name": "Python 3"
14
+ },
15
+ "language_info": {
16
+ "name": "python"
17
+ },
18
+ "gpuClass": "standard",
19
+ "accelerator": "GPU"
20
+ },
21
+ "cells": [
22
+ {
23
+ "cell_type": "markdown",
24
+ "metadata": {
25
+ "id": "view-in-github",
26
+ "colab_type": "text"
27
+ },
28
+ "source": [
29
+ "<a href=\"https://colab.research.google.com/github/sgoodfriend/rl-algo-impls/blob/main/colab_enjoy.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
30
+ ]
31
+ },
32
+ {
33
+ "cell_type": "markdown",
34
+ "source": [
35
+ "# [sgoodfriend/rl-algo-impls](https://github.com/sgoodfriend/rl-algo-impls) in Google Colaboratory\n",
36
+ "## Parameters\n",
37
+ "\n",
38
+ "\n",
39
+ "1. Wandb\n",
40
+ "\n"
41
+ ],
42
+ "metadata": {
43
+ "id": "S-tXDWP8WTLc"
44
+ }
45
+ },
46
+ {
47
+ "cell_type": "code",
48
+ "source": [
49
+ "from getpass import getpass\n",
50
+ "import os\n",
51
+ "os.environ[\"WANDB_API_KEY\"] = getpass(\"Wandb API key to upload metrics, videos, and models: \")"
52
+ ],
53
+ "metadata": {
54
+ "id": "1ZtdYgxWNGwZ"
55
+ },
56
+ "execution_count": null,
57
+ "outputs": []
58
+ },
59
+ {
60
+ "cell_type": "markdown",
61
+ "source": [
62
+ "2. enjoy.py parameters"
63
+ ],
64
+ "metadata": {
65
+ "id": "ao0nAh3MOdN7"
66
+ }
67
+ },
68
+ {
69
+ "cell_type": "code",
70
+ "source": [
71
+ "WANDB_RUN_PATH=\"sgoodfriend/rl-algo-impls-benchmarks/rd0lisee\""
72
+ ],
73
+ "metadata": {
74
+ "id": "jKL_NFhVOjSc"
75
+ },
76
+ "execution_count": 2,
77
+ "outputs": []
78
+ },
79
+ {
80
+ "cell_type": "markdown",
81
+ "source": [
82
+ "## Setup\n",
83
+ "Clone [sgoodfriend/rl-algo-impls](https://github.com/sgoodfriend/rl-algo-impls) "
84
+ ],
85
+ "metadata": {
86
+ "id": "bsG35Io0hmKG"
87
+ }
88
+ },
89
+ {
90
+ "cell_type": "code",
91
+ "source": [
92
+ "%%capture\n",
93
+ "!git clone https://github.com/sgoodfriend/rl-algo-impls.git"
94
+ ],
95
+ "metadata": {
96
+ "id": "k5ynTV25hdAf"
97
+ },
98
+ "execution_count": 3,
99
+ "outputs": []
100
+ },
101
+ {
102
+ "cell_type": "markdown",
103
+ "source": [
104
+ "Installing the correct packages:\n",
105
+ "\n",
106
+ "While conda and poetry are generally used for package management, the mismatch in Python versions (3.10 in the project file vs 3.8 in Colab) makes using the package yml files difficult to use. For now, instead I'm going to specify the list of requirements manually below:"
107
+ ],
108
+ "metadata": {
109
+ "id": "jKxGok-ElYQ7"
110
+ }
111
+ },
112
+ {
113
+ "cell_type": "code",
114
+ "source": [
115
+ "%%capture\n",
116
+ "!apt install python-opengl\n",
117
+ "!apt install ffmpeg\n",
118
+ "!apt install xvfb\n",
119
+ "!apt install swig"
120
+ ],
121
+ "metadata": {
122
+ "id": "nn6EETTc2Ewf"
123
+ },
124
+ "execution_count": 4,
125
+ "outputs": []
126
+ },
127
+ {
128
+ "cell_type": "code",
129
+ "source": [
130
+ "%%capture\n",
131
+ "%cd /content/rl-algo-impls\n",
132
+ "python -m pip install ."
133
+ ],
134
+ "metadata": {
135
+ "id": "AfZh9rH3yQii"
136
+ },
137
+ "execution_count": 5,
138
+ "outputs": []
139
+ },
140
+ {
141
+ "cell_type": "markdown",
142
+ "source": [
143
+ "## Run Once Per Runtime"
144
+ ],
145
+ "metadata": {
146
+ "id": "4o5HOLjc4wq7"
147
+ }
148
+ },
149
+ {
150
+ "cell_type": "code",
151
+ "source": [
152
+ "import wandb\n",
153
+ "wandb.login()"
154
+ ],
155
+ "metadata": {
156
+ "id": "PCXa5tdS2qFX"
157
+ },
158
+ "execution_count": null,
159
+ "outputs": []
160
+ },
161
+ {
162
+ "cell_type": "markdown",
163
+ "source": [
164
+ "## Restart Session beteween runs"
165
+ ],
166
+ "metadata": {
167
+ "id": "AZBZfSUV43JQ"
168
+ }
169
+ },
170
+ {
171
+ "cell_type": "code",
172
+ "source": [
173
+ "%%capture\n",
174
+ "from pyvirtualdisplay import Display\n",
175
+ "\n",
176
+ "virtual_display = Display(visible=0, size=(1400, 900))\n",
177
+ "virtual_display.start()"
178
+ ],
179
+ "metadata": {
180
+ "id": "VzemeQJP2NO9"
181
+ },
182
+ "execution_count": 7,
183
+ "outputs": []
184
+ },
185
+ {
186
+ "cell_type": "code",
187
+ "source": [
188
+ "%cd /content/rl-algo-impls\n",
189
+ "!python enjoy.py --wandb-run-path={WANDB_RUN_PATH}"
190
+ ],
191
+ "metadata": {
192
+ "id": "07aHYFH1zfXa"
193
+ },
194
+ "execution_count": null,
195
+ "outputs": []
196
+ }
197
+ ]
198
+ }
colab/colab_pybullet.sh ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ ALGO="ppo"
2
+ ENVS="HalfCheetahBulletEnv-v0 AntBulletEnv-v0 HopperBulletEnv-v0 Walker2DBulletEnv-v0"
3
+ BENCHMARK_MAX_PROCS="${BENCHMARK_MAX_PROCS:-3}"
4
+ bash scripts/train_loop.sh -a $ALGO -e "$ENVS" | xargs -I CMD -P $BENCHMARK_MAX_PROCS bash -c CMD
colab/colab_train.ipynb ADDED
@@ -0,0 +1,200 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": [],
7
+ "machine_shape": "hm",
8
+ "authorship_tag": "ABX9TyMmemQnx6G7GOnn6XBdjgxY",
9
+ "include_colab_link": true
10
+ },
11
+ "kernelspec": {
12
+ "name": "python3",
13
+ "display_name": "Python 3"
14
+ },
15
+ "language_info": {
16
+ "name": "python"
17
+ },
18
+ "gpuClass": "standard",
19
+ "accelerator": "GPU"
20
+ },
21
+ "cells": [
22
+ {
23
+ "cell_type": "markdown",
24
+ "metadata": {
25
+ "id": "view-in-github",
26
+ "colab_type": "text"
27
+ },
28
+ "source": [
29
+ "<a href=\"https://colab.research.google.com/github/sgoodfriend/rl-algo-impls/blob/main/colab_train.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
30
+ ]
31
+ },
32
+ {
33
+ "cell_type": "markdown",
34
+ "source": [
35
+ "# [sgoodfriend/rl-algo-impls](https://github.com/sgoodfriend/rl-algo-impls) in Google Colaboratory\n",
36
+ "## Parameters\n",
37
+ "\n",
38
+ "\n",
39
+ "1. Wandb\n",
40
+ "\n"
41
+ ],
42
+ "metadata": {
43
+ "id": "S-tXDWP8WTLc"
44
+ }
45
+ },
46
+ {
47
+ "cell_type": "code",
48
+ "source": [
49
+ "from getpass import getpass\n",
50
+ "import os\n",
51
+ "os.environ[\"WANDB_API_KEY\"] = getpass(\"Wandb API key to upload metrics, videos, and models: \")"
52
+ ],
53
+ "metadata": {
54
+ "id": "1ZtdYgxWNGwZ"
55
+ },
56
+ "execution_count": null,
57
+ "outputs": []
58
+ },
59
+ {
60
+ "cell_type": "markdown",
61
+ "source": [
62
+ "2. train run parameters"
63
+ ],
64
+ "metadata": {
65
+ "id": "ao0nAh3MOdN7"
66
+ }
67
+ },
68
+ {
69
+ "cell_type": "code",
70
+ "source": [
71
+ "ALGO = \"ppo\"\n",
72
+ "ENV = \"CartPole-v1\"\n",
73
+ "SEED = 1"
74
+ ],
75
+ "metadata": {
76
+ "id": "jKL_NFhVOjSc"
77
+ },
78
+ "execution_count": null,
79
+ "outputs": []
80
+ },
81
+ {
82
+ "cell_type": "markdown",
83
+ "source": [
84
+ "## Setup\n",
85
+ "Clone [sgoodfriend/rl-algo-impls](https://github.com/sgoodfriend/rl-algo-impls) "
86
+ ],
87
+ "metadata": {
88
+ "id": "bsG35Io0hmKG"
89
+ }
90
+ },
91
+ {
92
+ "cell_type": "code",
93
+ "source": [
94
+ "%%capture\n",
95
+ "!git clone https://github.com/sgoodfriend/rl-algo-impls.git"
96
+ ],
97
+ "metadata": {
98
+ "id": "k5ynTV25hdAf"
99
+ },
100
+ "execution_count": null,
101
+ "outputs": []
102
+ },
103
+ {
104
+ "cell_type": "markdown",
105
+ "source": [
106
+ "Installing the correct packages:\n",
107
+ "\n",
108
+ "While conda and poetry are generally used for package management, the mismatch in Python versions (3.10 in the project file vs 3.8 in Colab) makes using the package yml files difficult to use. For now, instead I'm going to specify the list of requirements manually below:"
109
+ ],
110
+ "metadata": {
111
+ "id": "jKxGok-ElYQ7"
112
+ }
113
+ },
114
+ {
115
+ "cell_type": "code",
116
+ "source": [
117
+ "%%capture\n",
118
+ "!apt install python-opengl\n",
119
+ "!apt install ffmpeg\n",
120
+ "!apt install xvfb\n",
121
+ "!apt install swig"
122
+ ],
123
+ "metadata": {
124
+ "id": "nn6EETTc2Ewf"
125
+ },
126
+ "execution_count": null,
127
+ "outputs": []
128
+ },
129
+ {
130
+ "cell_type": "code",
131
+ "source": [
132
+ "%%capture\n",
133
+ "%cd /content/rl-algo-impls\n",
134
+ "python -m pip install ."
135
+ ],
136
+ "metadata": {
137
+ "id": "AfZh9rH3yQii"
138
+ },
139
+ "execution_count": null,
140
+ "outputs": []
141
+ },
142
+ {
143
+ "cell_type": "markdown",
144
+ "source": [
145
+ "## Run Once Per Runtime"
146
+ ],
147
+ "metadata": {
148
+ "id": "4o5HOLjc4wq7"
149
+ }
150
+ },
151
+ {
152
+ "cell_type": "code",
153
+ "source": [
154
+ "import wandb\n",
155
+ "wandb.login()"
156
+ ],
157
+ "metadata": {
158
+ "id": "PCXa5tdS2qFX"
159
+ },
160
+ "execution_count": null,
161
+ "outputs": []
162
+ },
163
+ {
164
+ "cell_type": "markdown",
165
+ "source": [
166
+ "## Restart Session beteween runs"
167
+ ],
168
+ "metadata": {
169
+ "id": "AZBZfSUV43JQ"
170
+ }
171
+ },
172
+ {
173
+ "cell_type": "code",
174
+ "source": [
175
+ "%%capture\n",
176
+ "from pyvirtualdisplay import Display\n",
177
+ "\n",
178
+ "virtual_display = Display(visible=0, size=(1400, 900))\n",
179
+ "virtual_display.start()"
180
+ ],
181
+ "metadata": {
182
+ "id": "VzemeQJP2NO9"
183
+ },
184
+ "execution_count": null,
185
+ "outputs": []
186
+ },
187
+ {
188
+ "cell_type": "code",
189
+ "source": [
190
+ "%cd /content/rl-algo-impls\n",
191
+ "!python train.py --algo {ALGO} --env {ENV} --seed {SEED}"
192
+ ],
193
+ "metadata": {
194
+ "id": "07aHYFH1zfXa"
195
+ },
196
+ "execution_count": null,
197
+ "outputs": []
198
+ }
199
+ ]
200
+ }
compare_runs.py ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ from rl_algo_impls.compare_runs import compare_runs
2
+
3
+ if __name__ == "__main__":
4
+ compare_runs()
enjoy.py ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ from rl_algo_impls.enjoy import enjoy
2
+
3
+ if __name__ == "__main__":
4
+ enjoy()
environment.yml ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: rl_algo_impls
2
+ channels:
3
+ - pytorch
4
+ - conda-forge
5
+ - nodefaults
6
+ dependencies:
7
+ - python>=3.8, <3.11
8
+ - mamba
9
+ - pip
10
+ - pytorch
11
+ - torchvision
12
+ - torchaudio
huggingface_publish.py ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ from rl_algo_impls.huggingface_publish import huggingface_publish
2
+
3
+ if __name__ == "__main__":
4
+ huggingface_publish()
optimize.py ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ from rl_algo_impls.optimize import optimize
2
+
3
+ if __name__ == "__main__":
4
+ optimize()
pyproject.toml ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [project]
2
+ name = "rl_algo_impls"
3
+ version = "0.0.7"
4
+ description = "Implementations of reinforcement learning algorithms"
5
+ authors = [
6
+ {name = "Scott Goodfriend", email = "goodfriend.scott@gmail.com"},
7
+ ]
8
+ license = {file = "LICENSE"}
9
+ readme = "README.md"
10
+ requires-python = ">= 3.8"
11
+ classifiers = [
12
+ "License :: OSI Approved :: MIT License",
13
+ "Development Status :: 3 - Alpha",
14
+ "Programming Language :: Python :: 3.8",
15
+ "Programming Language :: Python :: 3.9",
16
+ "Programming Language :: Python :: 3.10",
17
+ ]
18
+ dependencies = [
19
+ "cmake",
20
+ "swig",
21
+ "scipy",
22
+ "torch",
23
+ "torchvision",
24
+ "tensorboard >= 2.11.2, < 2.12",
25
+ "AutoROM.accept-rom-license >= 0.4.2, < 0.5",
26
+ "stable-baselines3[extra] >= 1.7.0, < 1.8",
27
+ "gym[box2d] >= 0.21.0, < 0.22",
28
+ "pyglet == 1.5.27",
29
+ "wandb",
30
+ "pyvirtualdisplay",
31
+ "pybullet",
32
+ "tabulate",
33
+ "huggingface-hub",
34
+ "optuna",
35
+ "dash",
36
+ "kaleido",
37
+ "PyYAML",
38
+ "scikit-learn",
39
+ ]
40
+
41
+ [tool.setuptools]
42
+ packages = ["rl_algo_impls"]
43
+
44
+ [project.optional-dependencies]
45
+ test = [
46
+ "pytest",
47
+ "black",
48
+ "mypy",
49
+ "flake8",
50
+ "flake8-bugbear",
51
+ "isort",
52
+ ]
53
+ procgen = [
54
+ "numexpr >= 2.8.4",
55
+ "gym3",
56
+ "glfw >= 1.12.0, < 1.13",
57
+ "procgen; platform_machine=='x86_64'",
58
+ ]
59
+ microrts = [
60
+ "numpy < 1.24.0", # Support for gym-microrts < 0.6.0
61
+ "gym-microrts == 0.2.0",
62
+ ]
63
+
64
+ [project.urls]
65
+ "Homepage" = "https://github.com/sgoodfriend/rl-algo-impls"
66
+
67
+ [build-system]
68
+ requires = ["setuptools==65.5.0", "setuptools-scm"]
69
+ build-backend = "setuptools.build_meta"
replay.meta.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"content_type": "video/mp4", "encoder_version": {"backend": "ffmpeg", "version": "b'ffmpeg version 4.2.7-0ubuntu0.1 Copyright (c) 2000-2022 the FFmpeg developers\\nbuilt with gcc 9 (Ubuntu 9.4.0-1ubuntu1~20.04.1)\\nconfiguration: --prefix=/usr --extra-version=0ubuntu0.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-avresample --disable-filter=resample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librsvg --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opencl --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-nvenc --enable-chromaprint --enable-frei0r --enable-libx264 --enable-shared\\nlibavutil 56. 31.100 / 56. 31.100\\nlibavcodec 58. 54.100 / 58. 54.100\\nlibavformat 58. 29.100 / 58. 29.100\\nlibavdevice 58. 8.100 / 58. 8.100\\nlibavfilter 7. 57.100 / 7. 57.100\\nlibavresample 4. 0. 0 / 4. 0. 0\\nlibswscale 5. 5.100 / 5. 5.100\\nlibswresample 3. 5.100 / 3. 5.100\\nlibpostproc 55. 5.100 / 55. 5.100\\n'", "cmdline": ["ffmpeg", "-nostats", "-loglevel", "error", "-y", "-f", "rawvideo", "-s:v", "640x640", "-pix_fmt", "rgb24", "-framerate", "50", "-i", "-", "-vf", "scale=trunc(iw/2)*2:trunc(ih/2)*2", "-vcodec", "libx264", "-pix_fmt", "yuv420p", "-r", "50", "/tmp/tmp5e8390t_/ppo-MicrortsRandomEnemyShapedReward3-v1-NoMask/replay.mp4"]}, "episode": {"r": 5.595859527587891, "l": 600, "t": 11.182363}}
replay.mp4 ADDED
Binary file (193 kB). View file
 
rl_algo_impls/a2c/a2c.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ import numpy as np
3
+ import torch
4
+ import torch.nn as nn
5
+ import torch.nn.functional as F
6
+
7
+ from time import perf_counter
8
+ from torch.utils.tensorboard.writer import SummaryWriter
9
+ from typing import Optional, TypeVar
10
+
11
+ from rl_algo_impls.shared.algorithm import Algorithm
12
+ from rl_algo_impls.shared.callbacks.callback import Callback
13
+ from rl_algo_impls.shared.gae import compute_advantages
14
+ from rl_algo_impls.shared.policy.on_policy import ActorCritic
15
+ from rl_algo_impls.shared.schedule import schedule, update_learning_rate
16
+ from rl_algo_impls.shared.stats import log_scalars
17
+ from rl_algo_impls.wrappers.vectorable_wrapper import (
18
+ VecEnv,
19
+ single_observation_space,
20
+ single_action_space,
21
+ )
22
+
23
+ A2CSelf = TypeVar("A2CSelf", bound="A2C")
24
+
25
+
26
+ class A2C(Algorithm):
27
+ def __init__(
28
+ self,
29
+ policy: ActorCritic,
30
+ env: VecEnv,
31
+ device: torch.device,
32
+ tb_writer: SummaryWriter,
33
+ learning_rate: float = 7e-4,
34
+ learning_rate_decay: str = "none",
35
+ n_steps: int = 5,
36
+ gamma: float = 0.99,
37
+ gae_lambda: float = 1.0,
38
+ ent_coef: float = 0.0,
39
+ ent_coef_decay: str = "none",
40
+ vf_coef: float = 0.5,
41
+ max_grad_norm: float = 0.5,
42
+ rms_prop_eps: float = 1e-5,
43
+ use_rms_prop: bool = True,
44
+ sde_sample_freq: int = -1,
45
+ normalize_advantage: bool = False,
46
+ ) -> None:
47
+ super().__init__(policy, env, device, tb_writer)
48
+ self.policy = policy
49
+
50
+ self.lr_schedule = schedule(learning_rate_decay, learning_rate)
51
+ if use_rms_prop:
52
+ self.optimizer = torch.optim.RMSprop(
53
+ policy.parameters(), lr=learning_rate, eps=rms_prop_eps
54
+ )
55
+ else:
56
+ self.optimizer = torch.optim.Adam(policy.parameters(), lr=learning_rate)
57
+
58
+ self.n_steps = n_steps
59
+
60
+ self.gamma = gamma
61
+ self.gae_lambda = gae_lambda
62
+
63
+ self.vf_coef = vf_coef
64
+ self.ent_coef_schedule = schedule(ent_coef_decay, ent_coef)
65
+ self.max_grad_norm = max_grad_norm
66
+
67
+ self.sde_sample_freq = sde_sample_freq
68
+ self.normalize_advantage = normalize_advantage
69
+
70
+ def learn(
71
+ self: A2CSelf,
72
+ train_timesteps: int,
73
+ callback: Optional[Callback] = None,
74
+ total_timesteps: Optional[int] = None,
75
+ start_timesteps: int = 0,
76
+ ) -> A2CSelf:
77
+ if total_timesteps is None:
78
+ total_timesteps = train_timesteps
79
+ assert start_timesteps + train_timesteps <= total_timesteps
80
+ epoch_dim = (self.n_steps, self.env.num_envs)
81
+ step_dim = (self.env.num_envs,)
82
+ obs_space = single_observation_space(self.env)
83
+ act_space = single_action_space(self.env)
84
+
85
+ obs = np.zeros(epoch_dim + obs_space.shape, dtype=obs_space.dtype)
86
+ actions = np.zeros(epoch_dim + act_space.shape, dtype=act_space.dtype)
87
+ rewards = np.zeros(epoch_dim, dtype=np.float32)
88
+ episode_starts = np.zeros(epoch_dim, dtype=np.bool8)
89
+ values = np.zeros(epoch_dim, dtype=np.float32)
90
+ logprobs = np.zeros(epoch_dim, dtype=np.float32)
91
+
92
+ next_obs = self.env.reset()
93
+ next_episode_starts = np.full(step_dim, True, dtype=np.bool8)
94
+
95
+ timesteps_elapsed = start_timesteps
96
+ while timesteps_elapsed < start_timesteps + train_timesteps:
97
+ start_time = perf_counter()
98
+
99
+ progress = timesteps_elapsed / total_timesteps
100
+ ent_coef = self.ent_coef_schedule(progress)
101
+ learning_rate = self.lr_schedule(progress)
102
+ update_learning_rate(self.optimizer, learning_rate)
103
+ log_scalars(
104
+ self.tb_writer,
105
+ "charts",
106
+ {
107
+ "ent_coef": ent_coef,
108
+ "learning_rate": learning_rate,
109
+ },
110
+ timesteps_elapsed,
111
+ )
112
+
113
+ self.policy.eval()
114
+ self.policy.reset_noise()
115
+ for s in range(self.n_steps):
116
+ timesteps_elapsed += self.env.num_envs
117
+ if self.sde_sample_freq > 0 and s > 0 and s % self.sde_sample_freq == 0:
118
+ self.policy.reset_noise()
119
+
120
+ obs[s] = next_obs
121
+ episode_starts[s] = next_episode_starts
122
+
123
+ actions[s], values[s], logprobs[s], clamped_action = self.policy.step(
124
+ next_obs
125
+ )
126
+ next_obs, rewards[s], next_episode_starts, _ = self.env.step(
127
+ clamped_action
128
+ )
129
+
130
+ advantages = compute_advantages(
131
+ rewards,
132
+ values,
133
+ episode_starts,
134
+ next_episode_starts,
135
+ next_obs,
136
+ self.policy,
137
+ self.gamma,
138
+ self.gae_lambda,
139
+ )
140
+ returns = advantages + values
141
+
142
+ b_obs = torch.tensor(obs.reshape((-1,) + obs_space.shape)).to(self.device)
143
+ b_actions = torch.tensor(actions.reshape((-1,) + act_space.shape)).to(
144
+ self.device
145
+ )
146
+ b_advantages = torch.tensor(advantages.reshape(-1)).to(self.device)
147
+ b_returns = torch.tensor(returns.reshape(-1)).to(self.device)
148
+
149
+ if self.normalize_advantage:
150
+ b_advantages = (b_advantages - b_advantages.mean()) / (
151
+ b_advantages.std() + 1e-8
152
+ )
153
+
154
+ self.policy.train()
155
+ logp_a, entropy, v = self.policy(b_obs, b_actions)
156
+
157
+ pi_loss = -(b_advantages * logp_a).mean()
158
+ value_loss = F.mse_loss(b_returns, v)
159
+ entropy_loss = -entropy.mean()
160
+
161
+ loss = pi_loss + self.vf_coef * value_loss + ent_coef * entropy_loss
162
+
163
+ self.optimizer.zero_grad()
164
+ loss.backward()
165
+ nn.utils.clip_grad_norm_(self.policy.parameters(), self.max_grad_norm)
166
+ self.optimizer.step()
167
+
168
+ y_pred = values.reshape(-1)
169
+ y_true = returns.reshape(-1)
170
+ var_y = np.var(y_true).item()
171
+ explained_var = (
172
+ np.nan if var_y == 0 else 1 - np.var(y_true - y_pred).item() / var_y
173
+ )
174
+
175
+ end_time = perf_counter()
176
+ rollout_steps = self.n_steps * self.env.num_envs
177
+ self.tb_writer.add_scalar(
178
+ "train/steps_per_second",
179
+ (rollout_steps) / (end_time - start_time),
180
+ timesteps_elapsed,
181
+ )
182
+
183
+ log_scalars(
184
+ self.tb_writer,
185
+ "losses",
186
+ {
187
+ "loss": loss.item(),
188
+ "pi_loss": pi_loss.item(),
189
+ "v_loss": value_loss.item(),
190
+ "entropy_loss": entropy_loss.item(),
191
+ "explained_var": explained_var,
192
+ },
193
+ timesteps_elapsed,
194
+ )
195
+
196
+ if callback:
197
+ if not callback.on_step(timesteps_elapsed=rollout_steps):
198
+ logging.info(
199
+ f"Callback terminated training at {timesteps_elapsed} timesteps"
200
+ )
201
+ break
202
+
203
+ return self
rl_algo_impls/a2c/optimize.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import optuna
2
+
3
+ from copy import deepcopy
4
+
5
+ from rl_algo_impls.runner.config import Config, Hyperparams, EnvHyperparams
6
+ from rl_algo_impls.runner.env import make_eval_env
7
+ from rl_algo_impls.shared.policy.optimize_on_policy import sample_on_policy_hyperparams
8
+ from rl_algo_impls.tuning.optimize_env import sample_env_hyperparams
9
+
10
+
11
+ def sample_params(
12
+ trial: optuna.Trial,
13
+ base_hyperparams: Hyperparams,
14
+ base_config: Config,
15
+ ) -> Hyperparams:
16
+ hyperparams = deepcopy(base_hyperparams)
17
+
18
+ base_env_hyperparams = EnvHyperparams(**hyperparams.env_hyperparams)
19
+ env = make_eval_env(base_config, base_env_hyperparams, override_n_envs=1)
20
+
21
+ # env_hyperparams
22
+ env_hyperparams = sample_env_hyperparams(trial, hyperparams.env_hyperparams, env)
23
+
24
+ # policy_hyperparams
25
+ policy_hyperparams = sample_on_policy_hyperparams(
26
+ trial, hyperparams.policy_hyperparams, env
27
+ )
28
+
29
+ # algo_hyperparams
30
+ algo_hyperparams = hyperparams.algo_hyperparams
31
+
32
+ learning_rate = trial.suggest_float("learning_rate", 1e-5, 2e-3, log=True)
33
+ learning_rate_decay = trial.suggest_categorical(
34
+ "learning_rate_decay", ["none", "linear"]
35
+ )
36
+ n_steps_exp = trial.suggest_int("n_steps_exp", 1, 10)
37
+ n_steps = 2**n_steps_exp
38
+ trial.set_user_attr("n_steps", n_steps)
39
+ gamma = 1.0 - trial.suggest_float("gamma_om", 1e-4, 1e-1, log=True)
40
+ trial.set_user_attr("gamma", gamma)
41
+ gae_lambda = 1 - trial.suggest_float("gae_lambda_om", 1e-4, 1e-1)
42
+ trial.set_user_attr("gae_lambda", gae_lambda)
43
+ ent_coef = trial.suggest_float("ent_coef", 1e-8, 2.5e-2, log=True)
44
+ ent_coef_decay = trial.suggest_categorical("ent_coef_decay", ["none", "linear"])
45
+ vf_coef = trial.suggest_float("vf_coef", 0.1, 0.7)
46
+ max_grad_norm = trial.suggest_float("max_grad_norm", 1e-1, 1e1, log=True)
47
+ use_rms_prop = trial.suggest_categorical("use_rms_prop", [True, False])
48
+ normalize_advantage = trial.suggest_categorical(
49
+ "normalize_advantage", [True, False]
50
+ )
51
+
52
+ algo_hyperparams.update(
53
+ {
54
+ "learning_rate": learning_rate,
55
+ "learning_rate_decay": learning_rate_decay,
56
+ "n_steps": n_steps,
57
+ "gamma": gamma,
58
+ "gae_lambda": gae_lambda,
59
+ "ent_coef": ent_coef,
60
+ "ent_coef_decay": ent_coef_decay,
61
+ "vf_coef": vf_coef,
62
+ "max_grad_norm": max_grad_norm,
63
+ "use_rms_prop": use_rms_prop,
64
+ "normalize_advantage": normalize_advantage,
65
+ }
66
+ )
67
+
68
+ if policy_hyperparams.get("use_sde", False):
69
+ sde_sample_freq = 2 ** trial.suggest_int("sde_sample_freq_exp", 0, n_steps_exp)
70
+ trial.set_user_attr("sde_sample_freq", sde_sample_freq)
71
+ algo_hyperparams["sde_sample_freq"] = sde_sample_freq
72
+ elif "sde_sample_freq" in algo_hyperparams:
73
+ del algo_hyperparams["sde_sample_freq"]
74
+
75
+ env.close()
76
+
77
+ return hyperparams
rl_algo_impls/benchmark_publish.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import subprocess
3
+ import wandb
4
+ import wandb.apis.public
5
+
6
+ from collections import defaultdict
7
+ from multiprocessing.pool import ThreadPool
8
+ from typing import List, NamedTuple
9
+
10
+
11
+ class RunGroup(NamedTuple):
12
+ algo: str
13
+ env_id: str
14
+
15
+
16
+ def benchmark_publish() -> None:
17
+ parser = argparse.ArgumentParser()
18
+ parser.add_argument(
19
+ "--wandb-project-name",
20
+ type=str,
21
+ default="rl-algo-impls-benchmarks",
22
+ help="WandB project name to load runs from",
23
+ )
24
+ parser.add_argument(
25
+ "--wandb-entity",
26
+ type=str,
27
+ default=None,
28
+ help="WandB team of project. None uses default entity",
29
+ )
30
+ parser.add_argument("--wandb-tags", type=str, nargs="+", help="WandB tags")
31
+ parser.add_argument("--wandb-report-url", type=str, help="Link to WandB report")
32
+ parser.add_argument(
33
+ "--envs", type=str, nargs="*", help="Optional filter down to these envs"
34
+ )
35
+ parser.add_argument(
36
+ "--exclude-envs",
37
+ type=str,
38
+ nargs="*",
39
+ help="Environments to exclude from publishing",
40
+ )
41
+ parser.add_argument(
42
+ "--huggingface-user",
43
+ type=str,
44
+ default=None,
45
+ help="Huggingface user or team to upload model cards. Defaults to huggingface-cli login user",
46
+ )
47
+ parser.add_argument(
48
+ "--pool-size",
49
+ type=int,
50
+ default=3,
51
+ help="How many publish jobs can run in parallel",
52
+ )
53
+ parser.add_argument(
54
+ "--virtual-display", action="store_true", help="Use headless virtual display"
55
+ )
56
+ # parser.set_defaults(
57
+ # wandb_tags=["benchmark_e47a44c", "host_129-146-2-230"],
58
+ # wandb_report_url="https://api.wandb.ai/links/sgoodfriend/v4wd7cp5",
59
+ # envs=[],
60
+ # exclude_envs=[],
61
+ # )
62
+ args = parser.parse_args()
63
+ print(args)
64
+
65
+ api = wandb.Api()
66
+ all_runs = api.runs(
67
+ f"{args.wandb_entity or api.default_entity}/{args.wandb_project_name}"
68
+ )
69
+
70
+ required_tags = set(args.wandb_tags)
71
+ runs: List[wandb.apis.public.Run] = [
72
+ r
73
+ for r in all_runs
74
+ if required_tags.issubset(set(r.config.get("wandb_tags", [])))
75
+ ]
76
+
77
+ runs_paths_by_group = defaultdict(list)
78
+ for r in runs:
79
+ if r.state != "finished":
80
+ continue
81
+ algo = r.config["algo"]
82
+ env = r.config["env"]
83
+ if args.envs and env not in args.envs:
84
+ continue
85
+ if args.exclude_envs and env in args.exclude_envs:
86
+ continue
87
+ run_group = RunGroup(algo, env)
88
+ runs_paths_by_group[run_group].append("/".join(r.path))
89
+
90
+ def run(run_paths: List[str]) -> None:
91
+ publish_args = ["python", "huggingface_publish.py"]
92
+ publish_args.append("--wandb-run-paths")
93
+ publish_args.extend(run_paths)
94
+ publish_args.append("--wandb-report-url")
95
+ publish_args.append(args.wandb_report_url)
96
+ if args.huggingface_user:
97
+ publish_args.append("--huggingface-user")
98
+ publish_args.append(args.huggingface_user)
99
+ if args.virtual_display:
100
+ publish_args.append("--virtual-display")
101
+ subprocess.run(publish_args)
102
+
103
+ tp = ThreadPool(args.pool_size)
104
+ for run_paths in runs_paths_by_group.values():
105
+ tp.apply_async(run, (run_paths,))
106
+ tp.close()
107
+ tp.join()
108
+
109
+
110
+ if __name__ == "__main__":
111
+ benchmark_publish()
rl_algo_impls/compare_runs.py ADDED
@@ -0,0 +1,199 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import itertools
3
+ import numpy as np
4
+ import pandas as pd
5
+ import wandb
6
+ import wandb.apis.public
7
+
8
+ from collections import defaultdict
9
+ from dataclasses import dataclass
10
+ from typing import Dict, Iterable, List, TypeVar
11
+
12
+ from rl_algo_impls.benchmark_publish import RunGroup
13
+
14
+
15
+ @dataclass
16
+ class Comparison:
17
+ control_values: List[float]
18
+ experiment_values: List[float]
19
+
20
+ def mean_diff_percentage(self) -> float:
21
+ return self._diff_percentage(
22
+ np.mean(self.control_values).item(), np.mean(self.experiment_values).item()
23
+ )
24
+
25
+ def median_diff_percentage(self) -> float:
26
+ return self._diff_percentage(
27
+ np.median(self.control_values).item(),
28
+ np.median(self.experiment_values).item(),
29
+ )
30
+
31
+ def _diff_percentage(self, c: float, e: float) -> float:
32
+ if c == e:
33
+ return 0
34
+ elif c == 0:
35
+ return float("inf") if e > 0 else float("-inf")
36
+ return 100 * (e - c) / c
37
+
38
+ def score(self) -> float:
39
+ return (
40
+ np.sum(
41
+ np.sign((self.mean_diff_percentage(), self.median_diff_percentage()))
42
+ ).item()
43
+ / 2
44
+ )
45
+
46
+
47
+ RunGroupRunsSelf = TypeVar("RunGroupRunsSelf", bound="RunGroupRuns")
48
+
49
+
50
+ class RunGroupRuns:
51
+ def __init__(
52
+ self,
53
+ run_group: RunGroup,
54
+ control: List[str],
55
+ experiment: List[str],
56
+ summary_stats: List[str] = ["best_eval", "eval", "train_rolling"],
57
+ summary_metrics: List[str] = ["mean", "result"],
58
+ ) -> None:
59
+ self.algo = run_group.algo
60
+ self.env = run_group.env_id
61
+ self.control = set(control)
62
+ self.experiment = set(experiment)
63
+
64
+ self.summary_stats = summary_stats
65
+ self.summary_metrics = summary_metrics
66
+
67
+ self.control_runs = []
68
+ self.experiment_runs = []
69
+
70
+ def add_run(self, run: wandb.apis.public.Run) -> None:
71
+ wandb_tags = set(run.config.get("wandb_tags", []))
72
+ if self.control & wandb_tags:
73
+ self.control_runs.append(run)
74
+ elif self.experiment & wandb_tags:
75
+ self.experiment_runs.append(run)
76
+
77
+ def comparisons_by_metric(self) -> Dict[str, Comparison]:
78
+ c_by_m = {}
79
+ for metric in (
80
+ f"{s}/{m}"
81
+ for s, m in itertools.product(self.summary_stats, self.summary_metrics)
82
+ ):
83
+ c_by_m[metric] = Comparison(
84
+ [c.summary[metric] for c in self.control_runs],
85
+ [e.summary[metric] for e in self.experiment_runs],
86
+ )
87
+ return c_by_m
88
+
89
+ @staticmethod
90
+ def data_frame(rows: Iterable[RunGroupRunsSelf]) -> pd.DataFrame:
91
+ results = defaultdict(list)
92
+ for r in rows:
93
+ if not r.control_runs or not r.experiment_runs:
94
+ continue
95
+ results["algo"].append(r.algo)
96
+ results["env"].append(r.env)
97
+ results["control"].append(r.control)
98
+ results["expierment"].append(r.experiment)
99
+ c_by_m = r.comparisons_by_metric()
100
+ results["score"].append(
101
+ sum(m.score() for m in c_by_m.values()) / len(c_by_m)
102
+ )
103
+ for m, c in c_by_m.items():
104
+ results[f"{m}_mean"].append(c.mean_diff_percentage())
105
+ results[f"{m}_median"].append(c.median_diff_percentage())
106
+ return pd.DataFrame(results)
107
+
108
+
109
+ def compare_runs() -> None:
110
+ parser = argparse.ArgumentParser()
111
+ parser.add_argument(
112
+ "-p",
113
+ "--wandb-project-name",
114
+ type=str,
115
+ default="rl-algo-impls-benchmarks",
116
+ help="WandB project name to load runs from",
117
+ )
118
+ parser.add_argument(
119
+ "--wandb-entity",
120
+ type=str,
121
+ default=None,
122
+ help="WandB team. None uses default entity",
123
+ )
124
+ parser.add_argument(
125
+ "-n",
126
+ "--wandb-hostname-tag",
127
+ type=str,
128
+ nargs="*",
129
+ help="WandB tags for hostname (i.e. host_192-9-145-26)",
130
+ )
131
+ parser.add_argument(
132
+ "-c",
133
+ "--wandb-control-tag",
134
+ type=str,
135
+ nargs="+",
136
+ help="WandB tag for control commit (i.e. benchmark_5598ebc)",
137
+ )
138
+ parser.add_argument(
139
+ "-e",
140
+ "--wandb-experiment-tag",
141
+ type=str,
142
+ nargs="+",
143
+ help="WandB tag for experiment commit (i.e. benchmark_5540e1f)",
144
+ )
145
+ parser.add_argument(
146
+ "--envs",
147
+ type=str,
148
+ nargs="*",
149
+ help="If specified, only compare these envs",
150
+ )
151
+ parser.add_argument(
152
+ "--exclude-envs",
153
+ type=str,
154
+ nargs="*",
155
+ help="Environments to exclude from comparison",
156
+ )
157
+ # parser.set_defaults(
158
+ # wandb_hostname_tag=["host_150-230-44-105", "host_155-248-214-128"],
159
+ # wandb_control_tag=["benchmark_fbc943f"],
160
+ # wandb_experiment_tag=["benchmark_f59bf74"],
161
+ # exclude_envs=[],
162
+ # )
163
+ args = parser.parse_args()
164
+ print(args)
165
+
166
+ api = wandb.Api()
167
+ all_runs = api.runs(
168
+ path=f"{args.wandb_entity or api.default_entity}/{args.wandb_project_name}",
169
+ order="+created_at",
170
+ )
171
+
172
+ runs_by_run_group: Dict[RunGroup, RunGroupRuns] = {}
173
+ wandb_hostname_tags = set(args.wandb_hostname_tag)
174
+ for r in all_runs:
175
+ if r.state != "finished":
176
+ continue
177
+ wandb_tags = set(r.config.get("wandb_tags", []))
178
+ if not wandb_tags or not wandb_hostname_tags & wandb_tags:
179
+ continue
180
+ rg = RunGroup(r.config["algo"], r.config.get("env_id") or r.config["env"])
181
+ if args.exclude_envs and rg.env_id in args.exclude_envs:
182
+ continue
183
+ if args.envs and rg.env_id not in args.envs:
184
+ continue
185
+ if rg not in runs_by_run_group:
186
+ runs_by_run_group[rg] = RunGroupRuns(
187
+ rg,
188
+ args.wandb_control_tag,
189
+ args.wandb_experiment_tag,
190
+ )
191
+ runs_by_run_group[rg].add_run(r)
192
+ df = RunGroupRuns.data_frame(runs_by_run_group.values()).round(decimals=2)
193
+ print(f"**Total Score: {sum(df.score)}**")
194
+ df.loc["mean"] = df.mean(numeric_only=True)
195
+ print(df.to_markdown())
196
+
197
+
198
+ if __name__ == "__main__":
199
+ compare_runs()
rl_algo_impls/dqn/dqn.py ADDED
@@ -0,0 +1,182 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import copy
2
+ import numpy as np
3
+ import random
4
+ import torch
5
+ import torch.nn as nn
6
+ import torch.nn.functional as F
7
+
8
+ from collections import deque
9
+ from torch.optim import Adam
10
+ from torch.utils.tensorboard.writer import SummaryWriter
11
+ from typing import NamedTuple, Optional, TypeVar
12
+
13
+ from rl_algo_impls.dqn.policy import DQNPolicy
14
+ from rl_algo_impls.shared.algorithm import Algorithm
15
+ from rl_algo_impls.shared.callbacks.callback import Callback
16
+ from rl_algo_impls.shared.schedule import linear_schedule
17
+ from rl_algo_impls.wrappers.vectorable_wrapper import VecEnv, VecEnvObs
18
+
19
+
20
+ class Transition(NamedTuple):
21
+ obs: np.ndarray
22
+ action: np.ndarray
23
+ reward: float
24
+ done: bool
25
+ next_obs: np.ndarray
26
+
27
+
28
+ class Batch(NamedTuple):
29
+ obs: np.ndarray
30
+ actions: np.ndarray
31
+ rewards: np.ndarray
32
+ dones: np.ndarray
33
+ next_obs: np.ndarray
34
+
35
+
36
+ class ReplayBuffer:
37
+ def __init__(self, num_envs: int, maxlen: int) -> None:
38
+ self.num_envs = num_envs
39
+ self.buffer = deque(maxlen=maxlen)
40
+
41
+ def add(
42
+ self,
43
+ obs: VecEnvObs,
44
+ action: np.ndarray,
45
+ reward: np.ndarray,
46
+ done: np.ndarray,
47
+ next_obs: VecEnvObs,
48
+ ) -> None:
49
+ assert isinstance(obs, np.ndarray)
50
+ assert isinstance(next_obs, np.ndarray)
51
+ for i in range(self.num_envs):
52
+ self.buffer.append(
53
+ Transition(obs[i], action[i], reward[i], done[i], next_obs[i])
54
+ )
55
+
56
+ def sample(self, batch_size: int) -> Batch:
57
+ ts = random.sample(self.buffer, batch_size)
58
+ return Batch(
59
+ obs=np.array([t.obs for t in ts]),
60
+ actions=np.array([t.action for t in ts]),
61
+ rewards=np.array([t.reward for t in ts]),
62
+ dones=np.array([t.done for t in ts]),
63
+ next_obs=np.array([t.next_obs for t in ts]),
64
+ )
65
+
66
+ def __len__(self) -> int:
67
+ return len(self.buffer)
68
+
69
+
70
+ DQNSelf = TypeVar("DQNSelf", bound="DQN")
71
+
72
+
73
+ class DQN(Algorithm):
74
+ def __init__(
75
+ self,
76
+ policy: DQNPolicy,
77
+ env: VecEnv,
78
+ device: torch.device,
79
+ tb_writer: SummaryWriter,
80
+ learning_rate: float = 1e-4,
81
+ buffer_size: int = 1_000_000,
82
+ learning_starts: int = 50_000,
83
+ batch_size: int = 32,
84
+ tau: float = 1.0,
85
+ gamma: float = 0.99,
86
+ train_freq: int = 4,
87
+ gradient_steps: int = 1,
88
+ target_update_interval: int = 10_000,
89
+ exploration_fraction: float = 0.1,
90
+ exploration_initial_eps: float = 1.0,
91
+ exploration_final_eps: float = 0.05,
92
+ max_grad_norm: float = 10.0,
93
+ ) -> None:
94
+ super().__init__(policy, env, device, tb_writer)
95
+ self.policy = policy
96
+
97
+ self.optimizer = Adam(self.policy.q_net.parameters(), lr=learning_rate)
98
+
99
+ self.target_q_net = copy.deepcopy(self.policy.q_net).to(self.device)
100
+ self.target_q_net.train(False)
101
+ self.tau = tau
102
+ self.target_update_interval = target_update_interval
103
+
104
+ self.replay_buffer = ReplayBuffer(self.env.num_envs, buffer_size)
105
+ self.batch_size = batch_size
106
+
107
+ self.learning_starts = learning_starts
108
+ self.train_freq = train_freq
109
+ self.gradient_steps = gradient_steps
110
+
111
+ self.gamma = gamma
112
+ self.exploration_eps_schedule = linear_schedule(
113
+ exploration_initial_eps,
114
+ exploration_final_eps,
115
+ end_fraction=exploration_fraction,
116
+ )
117
+
118
+ self.max_grad_norm = max_grad_norm
119
+
120
+ def learn(
121
+ self: DQNSelf, total_timesteps: int, callback: Optional[Callback] = None
122
+ ) -> DQNSelf:
123
+ self.policy.train(True)
124
+ obs = self.env.reset()
125
+ obs = self._collect_rollout(self.learning_starts, obs, 1)
126
+ learning_steps = total_timesteps - self.learning_starts
127
+ timesteps_elapsed = 0
128
+ steps_since_target_update = 0
129
+ while timesteps_elapsed < learning_steps:
130
+ progress = timesteps_elapsed / learning_steps
131
+ eps = self.exploration_eps_schedule(progress)
132
+ obs = self._collect_rollout(self.train_freq, obs, eps)
133
+ rollout_steps = self.train_freq
134
+ timesteps_elapsed += rollout_steps
135
+ for _ in range(
136
+ self.gradient_steps if self.gradient_steps > 0 else self.train_freq
137
+ ):
138
+ self.train()
139
+ steps_since_target_update += rollout_steps
140
+ if steps_since_target_update >= self.target_update_interval:
141
+ self._update_target()
142
+ steps_since_target_update = 0
143
+ if callback:
144
+ callback.on_step(timesteps_elapsed=rollout_steps)
145
+ return self
146
+
147
+ def train(self) -> None:
148
+ if len(self.replay_buffer) < self.batch_size:
149
+ return
150
+ o, a, r, d, next_o = self.replay_buffer.sample(self.batch_size)
151
+ o = torch.as_tensor(o, device=self.device)
152
+ a = torch.as_tensor(a, device=self.device).unsqueeze(1)
153
+ r = torch.as_tensor(r, dtype=torch.float32, device=self.device)
154
+ d = torch.as_tensor(d, dtype=torch.long, device=self.device)
155
+ next_o = torch.as_tensor(next_o, device=self.device)
156
+
157
+ with torch.no_grad():
158
+ target = r + (1 - d) * self.gamma * self.target_q_net(next_o).max(1).values
159
+ current = self.policy.q_net(o).gather(dim=1, index=a).squeeze(1)
160
+ loss = F.smooth_l1_loss(current, target)
161
+
162
+ self.optimizer.zero_grad()
163
+ loss.backward()
164
+ if self.max_grad_norm:
165
+ nn.utils.clip_grad_norm_(self.policy.q_net.parameters(), self.max_grad_norm)
166
+ self.optimizer.step()
167
+
168
+ def _collect_rollout(self, timesteps: int, obs: VecEnvObs, eps: float) -> VecEnvObs:
169
+ for _ in range(0, timesteps, self.env.num_envs):
170
+ action = self.policy.act(obs, eps, deterministic=False)
171
+ next_obs, reward, done, _ = self.env.step(action)
172
+ self.replay_buffer.add(obs, action, reward, done, next_obs)
173
+ obs = next_obs
174
+ return obs
175
+
176
+ def _update_target(self) -> None:
177
+ for target_param, param in zip(
178
+ self.target_q_net.parameters(), self.policy.q_net.parameters()
179
+ ):
180
+ target_param.data.copy_(
181
+ self.tau * param.data + (1 - self.tau) * target_param.data
182
+ )
rl_algo_impls/dqn/policy.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import os
3
+ import torch
4
+
5
+ from typing import Optional, Sequence, TypeVar
6
+
7
+ from rl_algo_impls.dqn.q_net import QNetwork
8
+ from rl_algo_impls.shared.policy.policy import Policy
9
+ from rl_algo_impls.wrappers.vectorable_wrapper import (
10
+ VecEnv,
11
+ VecEnvObs,
12
+ single_observation_space,
13
+ single_action_space,
14
+ )
15
+
16
+ DQNPolicySelf = TypeVar("DQNPolicySelf", bound="DQNPolicy")
17
+
18
+
19
+ class DQNPolicy(Policy):
20
+ def __init__(
21
+ self,
22
+ env: VecEnv,
23
+ hidden_sizes: Sequence[int] = [],
24
+ cnn_feature_dim: int = 512,
25
+ cnn_style: str = "nature",
26
+ cnn_layers_init_orthogonal: Optional[bool] = None,
27
+ impala_channels: Sequence[int] = (16, 32, 32),
28
+ **kwargs,
29
+ ) -> None:
30
+ super().__init__(env, **kwargs)
31
+ self.q_net = QNetwork(
32
+ single_observation_space(env),
33
+ single_action_space(env),
34
+ hidden_sizes,
35
+ cnn_feature_dim=cnn_feature_dim,
36
+ cnn_style=cnn_style,
37
+ cnn_layers_init_orthogonal=cnn_layers_init_orthogonal,
38
+ impala_channels=impala_channels,
39
+ )
40
+
41
+ def act(
42
+ self, obs: VecEnvObs, eps: float = 0, deterministic: bool = True
43
+ ) -> np.ndarray:
44
+ assert eps == 0 if deterministic else eps >= 0
45
+ if not deterministic and np.random.random() < eps:
46
+ return np.array(
47
+ [
48
+ single_action_space(self.env).sample()
49
+ for _ in range(self.env.num_envs)
50
+ ]
51
+ )
52
+ else:
53
+ o = self._as_tensor(obs)
54
+ with torch.no_grad():
55
+ return self.q_net(o).argmax(axis=1).cpu().numpy()
rl_algo_impls/dqn/q_net.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gym
2
+ import torch as th
3
+ import torch.nn as nn
4
+
5
+ from gym.spaces import Discrete
6
+ from typing import Optional, Sequence, Type
7
+
8
+ from rl_algo_impls.shared.module.feature_extractor import FeatureExtractor
9
+ from rl_algo_impls.shared.module.module import mlp
10
+
11
+
12
+ class QNetwork(nn.Module):
13
+ def __init__(
14
+ self,
15
+ observation_space: gym.Space,
16
+ action_space: gym.Space,
17
+ hidden_sizes: Sequence[int] = [],
18
+ activation: Type[nn.Module] = nn.ReLU, # Used by stable-baselines3
19
+ cnn_feature_dim: int = 512,
20
+ cnn_style: str = "nature",
21
+ cnn_layers_init_orthogonal: Optional[bool] = None,
22
+ impala_channels: Sequence[int] = (16, 32, 32),
23
+ ) -> None:
24
+ super().__init__()
25
+ assert isinstance(action_space, Discrete)
26
+ self._feature_extractor = FeatureExtractor(
27
+ observation_space,
28
+ activation,
29
+ cnn_feature_dim=cnn_feature_dim,
30
+ cnn_style=cnn_style,
31
+ cnn_layers_init_orthogonal=cnn_layers_init_orthogonal,
32
+ impala_channels=impala_channels,
33
+ )
34
+ layer_sizes = (
35
+ (self._feature_extractor.out_dim,) + tuple(hidden_sizes) + (action_space.n,)
36
+ )
37
+ self._fc = mlp(layer_sizes, activation)
38
+
39
+ def forward(self, obs: th.Tensor) -> th.Tensor:
40
+ x = self._feature_extractor(obs)
41
+ return self._fc(x)
rl_algo_impls/enjoy.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Support for PyTorch mps mode (https://pytorch.org/docs/stable/notes/mps.html)
2
+ import os
3
+
4
+ os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
5
+
6
+ from rl_algo_impls.runner.evaluate import EvalArgs, evaluate_model
7
+ from rl_algo_impls.runner.running_utils import base_parser
8
+
9
+
10
+ def enjoy() -> None:
11
+ parser = base_parser(multiple=False)
12
+ parser.add_argument("--render", default=True, type=bool)
13
+ parser.add_argument("--best", default=True, type=bool)
14
+ parser.add_argument("--n_envs", default=1, type=int)
15
+ parser.add_argument("--n_episodes", default=3, type=int)
16
+ parser.add_argument("--deterministic-eval", default=None, type=bool)
17
+ parser.add_argument(
18
+ "--no-print-returns", action="store_true", help="Limit printing"
19
+ )
20
+ # wandb-run-path overrides base RunArgs
21
+ parser.add_argument("--wandb-run-path", default=None, type=str)
22
+ parser.set_defaults(
23
+ algo=["ppo"],
24
+ wandb_run_path="sgoodfriend/rl-algo-impls/m5c1t7g5",
25
+ )
26
+ args = parser.parse_args()
27
+ args.algo = args.algo[0]
28
+ args.env = args.env[0]
29
+ args = EvalArgs(**vars(args))
30
+
31
+ evaluate_model(args, os.getcwd())
32
+
33
+
34
+ if __name__ == "__main__":
35
+ enjoy()
rl_algo_impls/huggingface_publish.py ADDED
@@ -0,0 +1,194 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
4
+
5
+ import argparse
6
+ import requests
7
+ import shutil
8
+ import subprocess
9
+ import tempfile
10
+ import wandb
11
+ import wandb.apis.public
12
+
13
+ from typing import List, Optional
14
+
15
+ from huggingface_hub.hf_api import HfApi, upload_folder
16
+ from huggingface_hub.repocard import metadata_save
17
+ from pyvirtualdisplay.display import Display
18
+
19
+ from rl_algo_impls.publish.markdown_format import EvalTableData, model_card_text
20
+ from rl_algo_impls.runner.config import EnvHyperparams
21
+ from rl_algo_impls.runner.evaluate import EvalArgs, evaluate_model
22
+ from rl_algo_impls.runner.env import make_eval_env
23
+ from rl_algo_impls.shared.callbacks.eval_callback import evaluate
24
+ from rl_algo_impls.wrappers.vec_episode_recorder import VecEpisodeRecorder
25
+
26
+
27
+ def publish(
28
+ wandb_run_paths: List[str],
29
+ wandb_report_url: str,
30
+ huggingface_user: Optional[str] = None,
31
+ huggingface_token: Optional[str] = None,
32
+ virtual_display: bool = False,
33
+ ) -> None:
34
+ if virtual_display:
35
+ display = Display(visible=False, size=(1400, 900))
36
+ display.start()
37
+
38
+ api = wandb.Api()
39
+ runs = [api.run(rp) for rp in wandb_run_paths]
40
+ algo = runs[0].config["algo"]
41
+ hyperparam_id = runs[0].config["env"]
42
+ evaluations = [
43
+ evaluate_model(
44
+ EvalArgs(
45
+ algo,
46
+ hyperparam_id,
47
+ seed=r.config.get("seed", None),
48
+ render=False,
49
+ best=True,
50
+ n_envs=None,
51
+ n_episodes=10,
52
+ no_print_returns=True,
53
+ wandb_run_path="/".join(r.path),
54
+ ),
55
+ os.getcwd(),
56
+ )
57
+ for r in runs
58
+ ]
59
+ run_metadata = requests.get(runs[0].file("wandb-metadata.json").url).json()
60
+ table_data = list(EvalTableData(r, e) for r, e in zip(runs, evaluations))
61
+ best_eval = sorted(
62
+ table_data, key=lambda d: d.evaluation.stats.score, reverse=True
63
+ )[0]
64
+
65
+ with tempfile.TemporaryDirectory() as tmpdirname:
66
+ _, (policy, stats, config) = best_eval
67
+
68
+ repo_name = config.model_name(include_seed=False)
69
+ repo_dir_path = os.path.join(tmpdirname, repo_name)
70
+ # Locally clone this repo to a temp directory
71
+ subprocess.run(["git", "clone", ".", repo_dir_path])
72
+ shutil.rmtree(os.path.join(repo_dir_path, ".git"))
73
+ model_path = config.model_dir_path(best=True, downloaded=True)
74
+ shutil.copytree(
75
+ model_path,
76
+ os.path.join(
77
+ repo_dir_path, "saved_models", config.model_dir_name(best=True)
78
+ ),
79
+ )
80
+
81
+ github_url = "https://github.com/sgoodfriend/rl-algo-impls"
82
+ commit_hash = run_metadata.get("git", {}).get("commit", None)
83
+ env_id = runs[0].config.get("env_id") or runs[0].config["env"]
84
+ card_text = model_card_text(
85
+ algo,
86
+ env_id,
87
+ github_url,
88
+ commit_hash,
89
+ wandb_report_url,
90
+ table_data,
91
+ best_eval,
92
+ )
93
+ readme_filepath = os.path.join(repo_dir_path, "README.md")
94
+ os.remove(readme_filepath)
95
+ with open(readme_filepath, "w") as f:
96
+ f.write(card_text)
97
+
98
+ metadata = {
99
+ "library_name": "rl-algo-impls",
100
+ "tags": [
101
+ env_id,
102
+ algo,
103
+ "deep-reinforcement-learning",
104
+ "reinforcement-learning",
105
+ ],
106
+ "model-index": [
107
+ {
108
+ "name": algo,
109
+ "results": [
110
+ {
111
+ "metrics": [
112
+ {
113
+ "type": "mean_reward",
114
+ "value": str(stats.score),
115
+ "name": "mean_reward",
116
+ }
117
+ ],
118
+ "task": {
119
+ "type": "reinforcement-learning",
120
+ "name": "reinforcement-learning",
121
+ },
122
+ "dataset": {
123
+ "name": env_id,
124
+ "type": env_id,
125
+ },
126
+ }
127
+ ],
128
+ }
129
+ ],
130
+ }
131
+ metadata_save(readme_filepath, metadata)
132
+
133
+ video_env = VecEpisodeRecorder(
134
+ make_eval_env(
135
+ config,
136
+ EnvHyperparams(**config.env_hyperparams),
137
+ override_n_envs=1,
138
+ normalize_load_path=model_path,
139
+ ),
140
+ os.path.join(repo_dir_path, "replay"),
141
+ max_video_length=3600,
142
+ )
143
+ evaluate(
144
+ video_env,
145
+ policy,
146
+ 1,
147
+ deterministic=config.eval_params.get("deterministic", True),
148
+ )
149
+
150
+ api = HfApi()
151
+ huggingface_user = huggingface_user or api.whoami()["name"]
152
+ huggingface_repo = f"{huggingface_user}/{repo_name}"
153
+ api.create_repo(
154
+ token=huggingface_token,
155
+ repo_id=huggingface_repo,
156
+ private=False,
157
+ exist_ok=True,
158
+ )
159
+ repo_url = upload_folder(
160
+ repo_id=huggingface_repo,
161
+ folder_path=repo_dir_path,
162
+ path_in_repo="",
163
+ commit_message=f"{algo.upper()} playing {env_id} from {github_url}/tree/{commit_hash}",
164
+ token=huggingface_token,
165
+ delete_patterns="*",
166
+ )
167
+ print(f"Pushed model to the hub: {repo_url}")
168
+
169
+
170
+ def huggingface_publish():
171
+ parser = argparse.ArgumentParser()
172
+ parser.add_argument(
173
+ "--wandb-run-paths",
174
+ type=str,
175
+ nargs="+",
176
+ help="Run paths of the form entity/project/run_id",
177
+ )
178
+ parser.add_argument("--wandb-report-url", type=str, help="Link to WandB report")
179
+ parser.add_argument(
180
+ "--huggingface-user",
181
+ type=str,
182
+ help="Huggingface user or team to upload model cards",
183
+ default=None,
184
+ )
185
+ parser.add_argument(
186
+ "--virtual-display", action="store_true", help="Use headless virtual display"
187
+ )
188
+ args = parser.parse_args()
189
+ print(args)
190
+ publish(**vars(args))
191
+
192
+
193
+ if __name__ == "__main__":
194
+ huggingface_publish()
rl_algo_impls/hyperparams/a2c.yml ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ CartPole-v1: &cartpole-defaults
2
+ n_timesteps: !!float 5e5
3
+ env_hyperparams:
4
+ n_envs: 8
5
+
6
+ CartPole-v0:
7
+ <<: *cartpole-defaults
8
+
9
+ MountainCar-v0:
10
+ n_timesteps: !!float 1e6
11
+ env_hyperparams:
12
+ n_envs: 16
13
+ normalize: true
14
+
15
+ MountainCarContinuous-v0:
16
+ n_timesteps: !!float 1e5
17
+ env_hyperparams:
18
+ n_envs: 4
19
+ normalize: true
20
+ # policy_hyperparams:
21
+ # use_sde: true
22
+ # log_std_init: 0.0
23
+ # init_layers_orthogonal: false
24
+ algo_hyperparams:
25
+ n_steps: 100
26
+ sde_sample_freq: 16
27
+
28
+ Acrobot-v1:
29
+ n_timesteps: !!float 5e5
30
+ env_hyperparams:
31
+ normalize: true
32
+ n_envs: 16
33
+
34
+ # Tuned
35
+ LunarLander-v2:
36
+ device: cpu
37
+ n_timesteps: !!float 1e6
38
+ env_hyperparams:
39
+ n_envs: 4
40
+ normalize: true
41
+ algo_hyperparams:
42
+ n_steps: 2
43
+ gamma: 0.9955517404308908
44
+ gae_lambda: 0.9875340918797773
45
+ learning_rate: 0.0013814130817068916
46
+ learning_rate_decay: linear
47
+ ent_coef: !!float 3.388369146384422e-7
48
+ ent_coef_decay: none
49
+ max_grad_norm: 3.33982095073364
50
+ normalize_advantage: true
51
+ vf_coef: 0.1667838310548184
52
+
53
+ BipedalWalker-v3:
54
+ n_timesteps: !!float 5e6
55
+ env_hyperparams:
56
+ n_envs: 16
57
+ normalize: true
58
+ policy_hyperparams:
59
+ use_sde: true
60
+ log_std_init: -2
61
+ init_layers_orthogonal: false
62
+ algo_hyperparams:
63
+ ent_coef: 0
64
+ max_grad_norm: 0.5
65
+ n_steps: 8
66
+ gae_lambda: 0.9
67
+ vf_coef: 0.4
68
+ gamma: 0.99
69
+ learning_rate: !!float 9.6e-4
70
+ learning_rate_decay: linear
71
+
72
+ HalfCheetahBulletEnv-v0: &pybullet-defaults
73
+ n_timesteps: !!float 2e6
74
+ env_hyperparams:
75
+ n_envs: 4
76
+ normalize: true
77
+ policy_hyperparams:
78
+ use_sde: true
79
+ log_std_init: -2
80
+ init_layers_orthogonal: false
81
+ algo_hyperparams: &pybullet-algo-defaults
82
+ n_steps: 8
83
+ ent_coef: 0
84
+ max_grad_norm: 0.5
85
+ gae_lambda: 0.9
86
+ gamma: 0.99
87
+ vf_coef: 0.4
88
+ learning_rate: !!float 9.6e-4
89
+ learning_rate_decay: linear
90
+
91
+ AntBulletEnv-v0:
92
+ <<: *pybullet-defaults
93
+
94
+ Walker2DBulletEnv-v0:
95
+ <<: *pybullet-defaults
96
+
97
+ HopperBulletEnv-v0:
98
+ <<: *pybullet-defaults
99
+
100
+ CarRacing-v0:
101
+ n_timesteps: !!float 4e6
102
+ env_hyperparams:
103
+ n_envs: 8
104
+ frame_stack: 4
105
+ normalize: true
106
+ normalize_kwargs:
107
+ norm_obs: false
108
+ norm_reward: true
109
+ policy_hyperparams:
110
+ use_sde: true
111
+ log_std_init: -2
112
+ init_layers_orthogonal: false
113
+ activation_fn: relu
114
+ share_features_extractor: false
115
+ cnn_feature_dim: 256
116
+ hidden_sizes: [256]
117
+ algo_hyperparams:
118
+ n_steps: 512
119
+ learning_rate: !!float 1.62e-5
120
+ gamma: 0.997
121
+ gae_lambda: 0.975
122
+ ent_coef: 0
123
+ sde_sample_freq: 128
124
+ vf_coef: 0.64
125
+
126
+ _atari: &atari-defaults
127
+ n_timesteps: !!float 1e7
128
+ env_hyperparams: &atari-env-defaults
129
+ n_envs: 16
130
+ frame_stack: 4
131
+ no_reward_timeout_steps: 1000
132
+ no_reward_fire_steps: 500
133
+ vec_env_class: async
134
+ policy_hyperparams: &atari-policy-defaults
135
+ activation_fn: relu
136
+ algo_hyperparams:
137
+ ent_coef: 0.01
138
+ vf_coef: 0.25
rl_algo_impls/hyperparams/dqn.yml ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ CartPole-v1: &cartpole-defaults
2
+ n_timesteps: !!float 5e4
3
+ env_hyperparams:
4
+ rolling_length: 50
5
+ policy_hyperparams:
6
+ hidden_sizes: [256, 256]
7
+ algo_hyperparams:
8
+ learning_rate: !!float 2.3e-3
9
+ batch_size: 64
10
+ buffer_size: 100000
11
+ learning_starts: 1000
12
+ gamma: 0.99
13
+ target_update_interval: 10
14
+ train_freq: 256
15
+ gradient_steps: 128
16
+ exploration_fraction: 0.16
17
+ exploration_final_eps: 0.04
18
+ eval_params:
19
+ step_freq: !!float 1e4
20
+
21
+ CartPole-v0:
22
+ <<: *cartpole-defaults
23
+ n_timesteps: !!float 4e4
24
+
25
+ MountainCar-v0:
26
+ n_timesteps: !!float 1.2e5
27
+ env_hyperparams:
28
+ rolling_length: 50
29
+ policy_hyperparams:
30
+ hidden_sizes: [256, 256]
31
+ algo_hyperparams:
32
+ learning_rate: !!float 4e-3
33
+ batch_size: 128
34
+ buffer_size: 10000
35
+ learning_starts: 1000
36
+ gamma: 0.98
37
+ target_update_interval: 600
38
+ train_freq: 16
39
+ gradient_steps: 8
40
+ exploration_fraction: 0.2
41
+ exploration_final_eps: 0.07
42
+
43
+ Acrobot-v1:
44
+ n_timesteps: !!float 1e5
45
+ env_hyperparams:
46
+ rolling_length: 50
47
+ policy_hyperparams:
48
+ hidden_sizes: [256, 256]
49
+ algo_hyperparams:
50
+ learning_rate: !!float 6.3e-4
51
+ batch_size: 128
52
+ buffer_size: 50000
53
+ learning_starts: 0
54
+ gamma: 0.99
55
+ target_update_interval: 250
56
+ train_freq: 4
57
+ gradient_steps: -1
58
+ exploration_fraction: 0.12
59
+ exploration_final_eps: 0.1
60
+
61
+ LunarLander-v2:
62
+ n_timesteps: !!float 5e5
63
+ env_hyperparams:
64
+ rolling_length: 50
65
+ policy_hyperparams:
66
+ hidden_sizes: [256, 256]
67
+ algo_hyperparams:
68
+ learning_rate: !!float 1e-4
69
+ batch_size: 256
70
+ buffer_size: 100000
71
+ learning_starts: 10000
72
+ gamma: 0.99
73
+ target_update_interval: 250
74
+ train_freq: 8
75
+ gradient_steps: -1
76
+ exploration_fraction: 0.12
77
+ exploration_final_eps: 0.1
78
+ max_grad_norm: 0.5
79
+ eval_params:
80
+ step_freq: 25_000
81
+
82
+ _atari: &atari-defaults
83
+ n_timesteps: !!float 1e7
84
+ env_hyperparams:
85
+ frame_stack: 4
86
+ no_reward_timeout_steps: 1_000
87
+ no_reward_fire_steps: 500
88
+ n_envs: 8
89
+ vec_env_class: async
90
+ algo_hyperparams:
91
+ buffer_size: 100000
92
+ learning_rate: !!float 1e-4
93
+ batch_size: 32
94
+ learning_starts: 100000
95
+ target_update_interval: 1000
96
+ train_freq: 8
97
+ gradient_steps: 2
98
+ exploration_fraction: 0.1
99
+ exploration_final_eps: 0.01
100
+ eval_params:
101
+ deterministic: false
102
+
103
+ PongNoFrameskip-v4:
104
+ <<: *atari-defaults
105
+ n_timesteps: !!float 2.5e6
106
+
107
+ _impala-atari: &impala-atari-defaults
108
+ <<: *atari-defaults
109
+ policy_hyperparams:
110
+ cnn_style: impala
111
+ cnn_feature_dim: 256
112
+ init_layers_orthogonal: true
113
+ cnn_layers_init_orthogonal: false
114
+
115
+ impala-PongNoFrameskip-v4:
116
+ <<: *impala-atari-defaults
117
+ env_id: PongNoFrameskip-v4
118
+ n_timesteps: !!float 2.5e6
119
+
120
+ impala-BreakoutNoFrameskip-v4:
121
+ <<: *impala-atari-defaults
122
+ env_id: BreakoutNoFrameskip-v4
123
+
124
+ impala-SpaceInvadersNoFrameskip-v4:
125
+ <<: *impala-atari-defaults
126
+ env_id: SpaceInvadersNoFrameskip-v4
127
+
128
+ impala-QbertNoFrameskip-v4:
129
+ <<: *impala-atari-defaults
130
+ env_id: QbertNoFrameskip-v4
rl_algo_impls/hyperparams/ppo.yml ADDED
@@ -0,0 +1,417 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ CartPole-v1: &cartpole-defaults
2
+ n_timesteps: !!float 1e5
3
+ env_hyperparams:
4
+ n_envs: 8
5
+ algo_hyperparams:
6
+ n_steps: 32
7
+ batch_size: 256
8
+ n_epochs: 20
9
+ gae_lambda: 0.8
10
+ gamma: 0.98
11
+ ent_coef: 0.0
12
+ learning_rate: 0.001
13
+ learning_rate_decay: linear
14
+ clip_range: 0.2
15
+ clip_range_decay: linear
16
+ eval_params:
17
+ step_freq: !!float 2.5e4
18
+
19
+ CartPole-v0:
20
+ <<: *cartpole-defaults
21
+ n_timesteps: !!float 5e4
22
+
23
+ MountainCar-v0:
24
+ n_timesteps: !!float 1e6
25
+ env_hyperparams:
26
+ normalize: true
27
+ n_envs: 16
28
+ algo_hyperparams:
29
+ n_steps: 16
30
+ n_epochs: 4
31
+ gae_lambda: 0.98
32
+ gamma: 0.99
33
+ ent_coef: 0.0
34
+
35
+ MountainCarContinuous-v0:
36
+ n_timesteps: !!float 1e5
37
+ env_hyperparams:
38
+ normalize: true
39
+ n_envs: 4
40
+ # policy_hyperparams:
41
+ # init_layers_orthogonal: false
42
+ # log_std_init: -3.29
43
+ # use_sde: true
44
+ algo_hyperparams:
45
+ n_steps: 512
46
+ batch_size: 256
47
+ n_epochs: 10
48
+ learning_rate: !!float 7.77e-5
49
+ ent_coef: 0.01 # 0.00429
50
+ ent_coef_decay: linear
51
+ clip_range: 0.1
52
+ gae_lambda: 0.9
53
+ max_grad_norm: 5
54
+ vf_coef: 0.19
55
+ eval_params:
56
+ step_freq: 5000
57
+
58
+ Acrobot-v1:
59
+ n_timesteps: !!float 1e6
60
+ env_hyperparams:
61
+ n_envs: 16
62
+ normalize: true
63
+ algo_hyperparams:
64
+ n_steps: 256
65
+ n_epochs: 4
66
+ gae_lambda: 0.94
67
+ gamma: 0.99
68
+ ent_coef: 0.0
69
+
70
+ LunarLander-v2:
71
+ n_timesteps: !!float 4e6
72
+ env_hyperparams:
73
+ n_envs: 16
74
+ algo_hyperparams:
75
+ n_steps: 1024
76
+ batch_size: 64
77
+ n_epochs: 4
78
+ gae_lambda: 0.98
79
+ gamma: 0.999
80
+ learning_rate: !!float 5e-4
81
+ learning_rate_decay: linear
82
+ clip_range: 0.2
83
+ clip_range_decay: linear
84
+ ent_coef: 0.01
85
+ normalize_advantage: false
86
+
87
+ BipedalWalker-v3:
88
+ n_timesteps: !!float 10e6
89
+ env_hyperparams:
90
+ n_envs: 16
91
+ normalize: true
92
+ algo_hyperparams:
93
+ n_steps: 2048
94
+ batch_size: 64
95
+ gae_lambda: 0.95
96
+ gamma: 0.99
97
+ n_epochs: 10
98
+ ent_coef: 0.001
99
+ learning_rate: !!float 2.5e-4
100
+ learning_rate_decay: linear
101
+ clip_range: 0.2
102
+ clip_range_decay: linear
103
+
104
+ CarRacing-v0: &carracing-defaults
105
+ n_timesteps: !!float 4e6
106
+ env_hyperparams:
107
+ n_envs: 8
108
+ frame_stack: 4
109
+ policy_hyperparams: &carracing-policy-defaults
110
+ use_sde: true
111
+ log_std_init: -2
112
+ init_layers_orthogonal: false
113
+ activation_fn: relu
114
+ share_features_extractor: false
115
+ cnn_feature_dim: 256
116
+ hidden_sizes: [256]
117
+ algo_hyperparams:
118
+ n_steps: 512
119
+ batch_size: 128
120
+ n_epochs: 10
121
+ learning_rate: !!float 1e-4
122
+ learning_rate_decay: linear
123
+ gamma: 0.99
124
+ gae_lambda: 0.95
125
+ ent_coef: 0.0
126
+ sde_sample_freq: 4
127
+ max_grad_norm: 0.5
128
+ vf_coef: 0.5
129
+ clip_range: 0.2
130
+
131
+ impala-CarRacing-v0:
132
+ <<: *carracing-defaults
133
+ env_id: CarRacing-v0
134
+ policy_hyperparams:
135
+ <<: *carracing-policy-defaults
136
+ cnn_style: impala
137
+ init_layers_orthogonal: true
138
+ cnn_layers_init_orthogonal: false
139
+ hidden_sizes: []
140
+
141
+ # BreakoutNoFrameskip-v4
142
+ # PongNoFrameskip-v4
143
+ # SpaceInvadersNoFrameskip-v4
144
+ # QbertNoFrameskip-v4
145
+ _atari: &atari-defaults
146
+ n_timesteps: !!float 1e7
147
+ env_hyperparams: &atari-env-defaults
148
+ n_envs: 8
149
+ frame_stack: 4
150
+ no_reward_timeout_steps: 1000
151
+ no_reward_fire_steps: 500
152
+ vec_env_class: async
153
+ policy_hyperparams: &atari-policy-defaults
154
+ activation_fn: relu
155
+ algo_hyperparams: &atari-algo-defaults
156
+ n_steps: 128
157
+ batch_size: 256
158
+ n_epochs: 4
159
+ learning_rate: !!float 2.5e-4
160
+ learning_rate_decay: linear
161
+ clip_range: 0.1
162
+ clip_range_decay: linear
163
+ vf_coef: 0.5
164
+ ent_coef: 0.01
165
+ eval_params:
166
+ deterministic: false
167
+
168
+ _norm-rewards-atari: &norm-rewards-atari-default
169
+ <<: *atari-defaults
170
+ env_hyperparams:
171
+ <<: *atari-env-defaults
172
+ clip_atari_rewards: false
173
+ normalize: true
174
+ normalize_kwargs:
175
+ norm_obs: false
176
+ norm_reward: true
177
+
178
+ norm-rewards-BreakoutNoFrameskip-v4:
179
+ <<: *norm-rewards-atari-default
180
+ env_id: BreakoutNoFrameskip-v4
181
+
182
+ debug-PongNoFrameskip-v4:
183
+ <<: *atari-defaults
184
+ device: cpu
185
+ env_id: PongNoFrameskip-v4
186
+ env_hyperparams:
187
+ <<: *atari-env-defaults
188
+ vec_env_class: sync
189
+
190
+ _impala-atari: &impala-atari-defaults
191
+ <<: *atari-defaults
192
+ policy_hyperparams:
193
+ <<: *atari-policy-defaults
194
+ cnn_style: impala
195
+ cnn_feature_dim: 256
196
+ init_layers_orthogonal: true
197
+ cnn_layers_init_orthogonal: false
198
+
199
+ impala-PongNoFrameskip-v4:
200
+ <<: *impala-atari-defaults
201
+ env_id: PongNoFrameskip-v4
202
+
203
+ impala-BreakoutNoFrameskip-v4:
204
+ <<: *impala-atari-defaults
205
+ env_id: BreakoutNoFrameskip-v4
206
+
207
+ impala-SpaceInvadersNoFrameskip-v4:
208
+ <<: *impala-atari-defaults
209
+ env_id: SpaceInvadersNoFrameskip-v4
210
+
211
+ impala-QbertNoFrameskip-v4:
212
+ <<: *impala-atari-defaults
213
+ env_id: QbertNoFrameskip-v4
214
+
215
+ _microrts: &microrts-defaults
216
+ <<: *atari-defaults
217
+ n_timesteps: !!float 2e6
218
+ env_hyperparams: &microrts-env-defaults
219
+ n_envs: 8
220
+ vec_env_class: sync
221
+ mask_actions: true
222
+ policy_hyperparams:
223
+ <<: *atari-policy-defaults
224
+ cnn_style: microrts
225
+ cnn_feature_dim: 128
226
+ algo_hyperparams:
227
+ <<: *atari-algo-defaults
228
+ clip_range_decay: none
229
+ clip_range_vf: 0.1
230
+
231
+ _no-mask-microrts: &no-mask-microrts-defaults
232
+ <<: *microrts-defaults
233
+ env_hyperparams:
234
+ <<: *microrts-env-defaults
235
+ mask_actions: false
236
+
237
+ MicrortsMining-v1-NoMask:
238
+ <<: *no-mask-microrts-defaults
239
+ env_id: MicrortsMining-v1
240
+
241
+ MicrortsAttackShapedReward-v1-NoMask:
242
+ <<: *no-mask-microrts-defaults
243
+ env_id: MicrortsAttackShapedReward-v1
244
+
245
+ MicrortsRandomEnemyShapedReward3-v1-NoMask:
246
+ <<: *no-mask-microrts-defaults
247
+ env_id: MicrortsRandomEnemyShapedReward3-v1
248
+
249
+ HalfCheetahBulletEnv-v0: &pybullet-defaults
250
+ n_timesteps: !!float 2e6
251
+ env_hyperparams: &pybullet-env-defaults
252
+ n_envs: 16
253
+ normalize: true
254
+ policy_hyperparams: &pybullet-policy-defaults
255
+ pi_hidden_sizes: [256, 256]
256
+ v_hidden_sizes: [256, 256]
257
+ activation_fn: relu
258
+ algo_hyperparams: &pybullet-algo-defaults
259
+ n_steps: 512
260
+ batch_size: 128
261
+ n_epochs: 20
262
+ gamma: 0.99
263
+ gae_lambda: 0.9
264
+ ent_coef: 0.0
265
+ max_grad_norm: 0.5
266
+ vf_coef: 0.5
267
+ learning_rate: !!float 3e-5
268
+ clip_range: 0.4
269
+
270
+ AntBulletEnv-v0:
271
+ <<: *pybullet-defaults
272
+ policy_hyperparams:
273
+ <<: *pybullet-policy-defaults
274
+ algo_hyperparams:
275
+ <<: *pybullet-algo-defaults
276
+
277
+ Walker2DBulletEnv-v0:
278
+ <<: *pybullet-defaults
279
+ algo_hyperparams:
280
+ <<: *pybullet-algo-defaults
281
+ clip_range_decay: linear
282
+
283
+ HopperBulletEnv-v0:
284
+ <<: *pybullet-defaults
285
+ algo_hyperparams:
286
+ <<: *pybullet-algo-defaults
287
+ clip_range_decay: linear
288
+
289
+ HumanoidBulletEnv-v0:
290
+ <<: *pybullet-defaults
291
+ n_timesteps: !!float 1e7
292
+ env_hyperparams:
293
+ <<: *pybullet-env-defaults
294
+ n_envs: 8
295
+ policy_hyperparams:
296
+ <<: *pybullet-policy-defaults
297
+ # log_std_init: -1
298
+ algo_hyperparams:
299
+ <<: *pybullet-algo-defaults
300
+ n_steps: 2048
301
+ batch_size: 64
302
+ n_epochs: 10
303
+ gae_lambda: 0.95
304
+ learning_rate: !!float 2.5e-4
305
+ clip_range: 0.2
306
+
307
+ _procgen: &procgen-defaults
308
+ env_hyperparams: &procgen-env-defaults
309
+ env_type: procgen
310
+ n_envs: 64
311
+ # grayscale: false
312
+ # frame_stack: 4
313
+ normalize: true # procgen only normalizes reward
314
+ make_kwargs: &procgen-make-kwargs-defaults
315
+ num_threads: 8
316
+ policy_hyperparams: &procgen-policy-defaults
317
+ activation_fn: relu
318
+ cnn_style: impala
319
+ cnn_feature_dim: 256
320
+ init_layers_orthogonal: true
321
+ cnn_layers_init_orthogonal: false
322
+ algo_hyperparams: &procgen-algo-defaults
323
+ gamma: 0.999
324
+ gae_lambda: 0.95
325
+ n_steps: 256
326
+ batch_size: 2048
327
+ n_epochs: 3
328
+ ent_coef: 0.01
329
+ clip_range: 0.2
330
+ # clip_range_decay: linear
331
+ clip_range_vf: 0.2
332
+ learning_rate: !!float 5e-4
333
+ # learning_rate_decay: linear
334
+ vf_coef: 0.5
335
+ eval_params: &procgen-eval-defaults
336
+ ignore_first_episode: true
337
+ # deterministic: false
338
+ step_freq: !!float 1e5
339
+
340
+ _procgen-easy: &procgen-easy-defaults
341
+ <<: *procgen-defaults
342
+ n_timesteps: !!float 25e6
343
+ env_hyperparams: &procgen-easy-env-defaults
344
+ <<: *procgen-env-defaults
345
+ make_kwargs:
346
+ <<: *procgen-make-kwargs-defaults
347
+ distribution_mode: easy
348
+
349
+ procgen-coinrun-easy: &coinrun-easy-defaults
350
+ <<: *procgen-easy-defaults
351
+ env_id: coinrun
352
+
353
+ debug-procgen-coinrun:
354
+ <<: *coinrun-easy-defaults
355
+ device: cpu
356
+
357
+ procgen-starpilot-easy:
358
+ <<: *procgen-easy-defaults
359
+ env_id: starpilot
360
+
361
+ procgen-bossfight-easy:
362
+ <<: *procgen-easy-defaults
363
+ env_id: bossfight
364
+
365
+ procgen-bigfish-easy:
366
+ <<: *procgen-easy-defaults
367
+ env_id: bigfish
368
+
369
+ _procgen-hard: &procgen-hard-defaults
370
+ <<: *procgen-defaults
371
+ n_timesteps: !!float 200e6
372
+ env_hyperparams: &procgen-hard-env-defaults
373
+ <<: *procgen-env-defaults
374
+ n_envs: 256
375
+ make_kwargs:
376
+ <<: *procgen-make-kwargs-defaults
377
+ distribution_mode: hard
378
+ algo_hyperparams: &procgen-hard-algo-defaults
379
+ <<: *procgen-algo-defaults
380
+ batch_size: 8192
381
+ clip_range_decay: linear
382
+ learning_rate_decay: linear
383
+ eval_params:
384
+ <<: *procgen-eval-defaults
385
+ step_freq: !!float 5e5
386
+
387
+ procgen-starpilot-hard: &procgen-starpilot-hard-defaults
388
+ <<: *procgen-hard-defaults
389
+ env_id: starpilot
390
+
391
+ procgen-starpilot-hard-2xIMPALA:
392
+ <<: *procgen-starpilot-hard-defaults
393
+ policy_hyperparams:
394
+ <<: *procgen-policy-defaults
395
+ impala_channels: [32, 64, 64]
396
+ algo_hyperparams:
397
+ <<: *procgen-hard-algo-defaults
398
+ learning_rate: !!float 3.3e-4
399
+
400
+ procgen-starpilot-hard-2xIMPALA-fat:
401
+ <<: *procgen-starpilot-hard-defaults
402
+ policy_hyperparams:
403
+ <<: *procgen-policy-defaults
404
+ impala_channels: [32, 64, 64]
405
+ cnn_feature_dim: 512
406
+ algo_hyperparams:
407
+ <<: *procgen-hard-algo-defaults
408
+ learning_rate: !!float 2.5e-4
409
+
410
+ procgen-starpilot-hard-4xIMPALA:
411
+ <<: *procgen-starpilot-hard-defaults
412
+ policy_hyperparams:
413
+ <<: *procgen-policy-defaults
414
+ impala_channels: [64, 128, 128]
415
+ algo_hyperparams:
416
+ <<: *procgen-hard-algo-defaults
417
+ learning_rate: !!float 2.1e-4
rl_algo_impls/hyperparams/vpg.yml ADDED
@@ -0,0 +1,197 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ CartPole-v1: &cartpole-defaults
2
+ n_timesteps: !!float 4e5
3
+ algo_hyperparams:
4
+ n_steps: 4096
5
+ pi_lr: 0.01
6
+ gamma: 0.99
7
+ gae_lambda: 1
8
+ val_lr: 0.01
9
+ train_v_iters: 80
10
+ eval_params:
11
+ step_freq: !!float 2.5e4
12
+
13
+ CartPole-v0:
14
+ <<: *cartpole-defaults
15
+ n_timesteps: !!float 1e5
16
+ algo_hyperparams:
17
+ n_steps: 1024
18
+ pi_lr: 0.01
19
+ gamma: 0.99
20
+ gae_lambda: 1
21
+ val_lr: 0.01
22
+ train_v_iters: 80
23
+
24
+ MountainCar-v0:
25
+ n_timesteps: !!float 1e6
26
+ env_hyperparams:
27
+ normalize: true
28
+ n_envs: 16
29
+ algo_hyperparams:
30
+ n_steps: 200
31
+ pi_lr: 0.005
32
+ gamma: 0.99
33
+ gae_lambda: 0.97
34
+ val_lr: 0.01
35
+ train_v_iters: 80
36
+ max_grad_norm: 0.5
37
+
38
+ MountainCarContinuous-v0:
39
+ n_timesteps: !!float 3e5
40
+ env_hyperparams:
41
+ normalize: true
42
+ n_envs: 4
43
+ # policy_hyperparams:
44
+ # init_layers_orthogonal: false
45
+ # log_std_init: -3.29
46
+ # use_sde: true
47
+ algo_hyperparams:
48
+ n_steps: 1000
49
+ pi_lr: !!float 5e-4
50
+ gamma: 0.99
51
+ gae_lambda: 0.9
52
+ val_lr: !!float 1e-3
53
+ train_v_iters: 80
54
+ max_grad_norm: 5
55
+ eval_params:
56
+ step_freq: 5000
57
+
58
+ Acrobot-v1:
59
+ n_timesteps: !!float 2e5
60
+ algo_hyperparams:
61
+ n_steps: 2048
62
+ pi_lr: 0.005
63
+ gamma: 0.99
64
+ gae_lambda: 0.97
65
+ val_lr: 0.01
66
+ train_v_iters: 80
67
+ max_grad_norm: 0.5
68
+
69
+ LunarLander-v2:
70
+ n_timesteps: !!float 4e6
71
+ policy_hyperparams:
72
+ hidden_sizes: [256, 256]
73
+ algo_hyperparams:
74
+ n_steps: 2048
75
+ pi_lr: 0.0001
76
+ gamma: 0.999
77
+ gae_lambda: 0.97
78
+ val_lr: 0.0001
79
+ train_v_iters: 80
80
+ max_grad_norm: 0.5
81
+ eval_params:
82
+ deterministic: false
83
+
84
+ BipedalWalker-v3:
85
+ n_timesteps: !!float 10e6
86
+ env_hyperparams:
87
+ n_envs: 16
88
+ normalize: true
89
+ policy_hyperparams:
90
+ hidden_sizes: [256, 256]
91
+ algo_hyperparams:
92
+ n_steps: 1600
93
+ gae_lambda: 0.95
94
+ gamma: 0.99
95
+ pi_lr: !!float 1e-4
96
+ val_lr: !!float 1e-4
97
+ train_v_iters: 80
98
+ max_grad_norm: 0.5
99
+ eval_params:
100
+ deterministic: false
101
+
102
+ CarRacing-v0:
103
+ n_timesteps: !!float 4e6
104
+ env_hyperparams:
105
+ frame_stack: 4
106
+ n_envs: 4
107
+ vec_env_class: sync
108
+ policy_hyperparams:
109
+ use_sde: true
110
+ log_std_init: -2
111
+ init_layers_orthogonal: false
112
+ activation_fn: relu
113
+ cnn_feature_dim: 256
114
+ hidden_sizes: [256]
115
+ algo_hyperparams:
116
+ n_steps: 1000
117
+ pi_lr: !!float 5e-5
118
+ gamma: 0.99
119
+ gae_lambda: 0.95
120
+ val_lr: !!float 1e-4
121
+ train_v_iters: 40
122
+ max_grad_norm: 0.5
123
+ sde_sample_freq: 4
124
+
125
+ HalfCheetahBulletEnv-v0: &pybullet-defaults
126
+ n_timesteps: !!float 2e6
127
+ env_hyperparams: &pybullet-env-defaults
128
+ normalize: true
129
+ policy_hyperparams: &pybullet-policy-defaults
130
+ hidden_sizes: [256, 256]
131
+ algo_hyperparams: &pybullet-algo-defaults
132
+ n_steps: 4000
133
+ pi_lr: !!float 3e-4
134
+ gamma: 0.99
135
+ gae_lambda: 0.97
136
+ val_lr: !!float 1e-3
137
+ train_v_iters: 80
138
+ max_grad_norm: 0.5
139
+
140
+ AntBulletEnv-v0:
141
+ <<: *pybullet-defaults
142
+ policy_hyperparams:
143
+ <<: *pybullet-policy-defaults
144
+ hidden_sizes: [400, 300]
145
+ algo_hyperparams:
146
+ <<: *pybullet-algo-defaults
147
+ pi_lr: !!float 7e-4
148
+ val_lr: !!float 7e-3
149
+
150
+ HopperBulletEnv-v0:
151
+ <<: *pybullet-defaults
152
+
153
+ Walker2DBulletEnv-v0:
154
+ <<: *pybullet-defaults
155
+
156
+ FrozenLake-v1:
157
+ n_timesteps: !!float 8e5
158
+ env_params:
159
+ make_kwargs:
160
+ map_name: 8x8
161
+ is_slippery: true
162
+ policy_hyperparams:
163
+ hidden_sizes: [64]
164
+ algo_hyperparams:
165
+ n_steps: 2048
166
+ pi_lr: 0.01
167
+ gamma: 0.99
168
+ gae_lambda: 0.98
169
+ val_lr: 0.01
170
+ train_v_iters: 80
171
+ max_grad_norm: 0.5
172
+ eval_params:
173
+ step_freq: !!float 5e4
174
+ n_episodes: 10
175
+ save_best: true
176
+
177
+ _atari: &atari-defaults
178
+ n_timesteps: !!float 10e6
179
+ env_hyperparams:
180
+ n_envs: 2
181
+ frame_stack: 4
182
+ no_reward_timeout_steps: 1000
183
+ no_reward_fire_steps: 500
184
+ vec_env_class: async
185
+ policy_hyperparams:
186
+ activation_fn: relu
187
+ algo_hyperparams:
188
+ n_steps: 3072
189
+ pi_lr: !!float 5e-5
190
+ gamma: 0.99
191
+ gae_lambda: 0.95
192
+ val_lr: !!float 1e-4
193
+ train_v_iters: 80
194
+ max_grad_norm: 0.5
195
+ ent_coef: 0.01
196
+ eval_params:
197
+ deterministic: false
rl_algo_impls/optimize.py ADDED
@@ -0,0 +1,442 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import dataclasses
2
+ import gc
3
+ import inspect
4
+ import logging
5
+ import numpy as np
6
+ import optuna
7
+ import os
8
+ import torch
9
+ import wandb
10
+
11
+ from dataclasses import asdict, dataclass
12
+ from optuna.pruners import HyperbandPruner
13
+ from optuna.samplers import TPESampler
14
+ from optuna.visualization import plot_optimization_history, plot_param_importances
15
+ from torch.utils.tensorboard.writer import SummaryWriter
16
+ from typing import Callable, List, NamedTuple, Optional, Sequence, Union
17
+
18
+ from rl_algo_impls.a2c.optimize import sample_params as a2c_sample_params
19
+ from rl_algo_impls.runner.config import Config, EnvHyperparams, RunArgs
20
+ from rl_algo_impls.runner.env import make_env, make_eval_env
21
+ from rl_algo_impls.runner.running_utils import (
22
+ base_parser,
23
+ load_hyperparams,
24
+ set_seeds,
25
+ get_device,
26
+ make_policy,
27
+ ALGOS,
28
+ hparam_dict,
29
+ )
30
+ from rl_algo_impls.shared.callbacks.optimize_callback import (
31
+ Evaluation,
32
+ OptimizeCallback,
33
+ evaluation,
34
+ )
35
+ from rl_algo_impls.shared.stats import EpisodesStats
36
+
37
+
38
+ @dataclass
39
+ class StudyArgs:
40
+ load_study: bool
41
+ study_name: Optional[str] = None
42
+ storage_path: Optional[str] = None
43
+ n_trials: int = 100
44
+ n_jobs: int = 1
45
+ n_evaluations: int = 4
46
+ n_eval_envs: int = 8
47
+ n_eval_episodes: int = 16
48
+ timeout: Union[int, float, None] = None
49
+ wandb_project_name: Optional[str] = None
50
+ wandb_entity: Optional[str] = None
51
+ wandb_tags: Sequence[str] = dataclasses.field(default_factory=list)
52
+ wandb_group: Optional[str] = None
53
+ virtual_display: bool = False
54
+
55
+
56
+ class Args(NamedTuple):
57
+ train_args: Sequence[RunArgs]
58
+ study_args: StudyArgs
59
+
60
+
61
+ def parse_args() -> Args:
62
+ parser = base_parser()
63
+ parser.add_argument(
64
+ "--load-study",
65
+ action="store_true",
66
+ help="Load a preexisting study, useful for parallelization",
67
+ )
68
+ parser.add_argument("--study-name", type=str, help="Optuna study name")
69
+ parser.add_argument(
70
+ "--storage-path",
71
+ type=str,
72
+ help="Path of database for Optuna to persist to",
73
+ )
74
+ parser.add_argument(
75
+ "--wandb-project-name",
76
+ type=str,
77
+ default="rl-algo-impls-tuning",
78
+ help="WandB project name to upload tuning data to. If none, won't upload",
79
+ )
80
+ parser.add_argument(
81
+ "--wandb-entity",
82
+ type=str,
83
+ help="WandB team. None uses the default entity",
84
+ )
85
+ parser.add_argument(
86
+ "--wandb-tags", type=str, nargs="*", help="WandB tags to add to run"
87
+ )
88
+ parser.add_argument(
89
+ "--wandb-group", type=str, help="WandB group to group trials under"
90
+ )
91
+ parser.add_argument(
92
+ "--n-trials", type=int, default=100, help="Maximum number of trials"
93
+ )
94
+ parser.add_argument(
95
+ "--n-jobs", type=int, default=1, help="Number of jobs to run in parallel"
96
+ )
97
+ parser.add_argument(
98
+ "--n-evaluations",
99
+ type=int,
100
+ default=4,
101
+ help="Number of evaluations during the training",
102
+ )
103
+ parser.add_argument(
104
+ "--n-eval-envs",
105
+ type=int,
106
+ default=8,
107
+ help="Number of envs in vectorized eval environment",
108
+ )
109
+ parser.add_argument(
110
+ "--n-eval-episodes",
111
+ type=int,
112
+ default=16,
113
+ help="Number of episodes to complete for evaluation",
114
+ )
115
+ parser.add_argument("--timeout", type=int, help="Seconds to timeout optimization")
116
+ parser.add_argument(
117
+ "--virtual-display", action="store_true", help="Use headless virtual display"
118
+ )
119
+ # parser.set_defaults(
120
+ # algo=["a2c"],
121
+ # env=["CartPole-v1"],
122
+ # seed=[100, 200, 300],
123
+ # n_trials=5,
124
+ # virtual_display=True,
125
+ # )
126
+ train_dict, study_dict = {}, {}
127
+ for k, v in vars(parser.parse_args()).items():
128
+ if k in inspect.signature(StudyArgs).parameters:
129
+ study_dict[k] = v
130
+ else:
131
+ train_dict[k] = v
132
+
133
+ study_args = StudyArgs(**study_dict)
134
+ # Hyperparameter tuning across algos and envs not supported
135
+ assert len(train_dict["algo"]) == 1
136
+ assert len(train_dict["env"]) == 1
137
+ train_args = RunArgs.expand_from_dict(train_dict)
138
+
139
+ if not all((study_args.study_name, study_args.storage_path)):
140
+ hyperparams = load_hyperparams(train_args[0].algo, train_args[0].env)
141
+ config = Config(train_args[0], hyperparams, os.getcwd())
142
+ if study_args.study_name is None:
143
+ study_args.study_name = config.run_name(include_seed=False)
144
+ if study_args.storage_path is None:
145
+ study_args.storage_path = (
146
+ f"sqlite:///{os.path.join(config.runs_dir, 'tuning.db')}"
147
+ )
148
+ # Default set group name to study name
149
+ study_args.wandb_group = study_args.wandb_group or study_args.study_name
150
+
151
+ return Args(train_args, study_args)
152
+
153
+
154
+ def objective_fn(
155
+ args: Sequence[RunArgs], study_args: StudyArgs
156
+ ) -> Callable[[optuna.Trial], float]:
157
+ def objective(trial: optuna.Trial) -> float:
158
+ if len(args) == 1:
159
+ return simple_optimize(trial, args[0], study_args)
160
+ else:
161
+ return stepwise_optimize(trial, args, study_args)
162
+
163
+ return objective
164
+
165
+
166
+ def simple_optimize(trial: optuna.Trial, args: RunArgs, study_args: StudyArgs) -> float:
167
+ base_hyperparams = load_hyperparams(args.algo, args.env)
168
+ base_config = Config(args, base_hyperparams, os.getcwd())
169
+ if args.algo == "a2c":
170
+ hyperparams = a2c_sample_params(trial, base_hyperparams, base_config)
171
+ else:
172
+ raise ValueError(f"Optimizing {args.algo} isn't supported")
173
+ config = Config(args, hyperparams, os.getcwd())
174
+
175
+ wandb_enabled = bool(study_args.wandb_project_name)
176
+ if wandb_enabled:
177
+ wandb.init(
178
+ project=study_args.wandb_project_name,
179
+ entity=study_args.wandb_entity,
180
+ config=asdict(hyperparams),
181
+ name=f"{config.model_name()}-{str(trial.number)}",
182
+ tags=study_args.wandb_tags,
183
+ group=study_args.wandb_group,
184
+ sync_tensorboard=True,
185
+ monitor_gym=True,
186
+ save_code=True,
187
+ reinit=True,
188
+ )
189
+ wandb.config.update(args)
190
+
191
+ tb_writer = SummaryWriter(config.tensorboard_summary_path)
192
+ set_seeds(args.seed, args.use_deterministic_algorithms)
193
+
194
+ env = make_env(
195
+ config, EnvHyperparams(**config.env_hyperparams), tb_writer=tb_writer
196
+ )
197
+ device = get_device(config, env)
198
+ policy = make_policy(args.algo, env, device, **config.policy_hyperparams)
199
+ algo = ALGOS[args.algo](policy, env, device, tb_writer, **config.algo_hyperparams)
200
+
201
+ eval_env = make_eval_env(
202
+ config,
203
+ EnvHyperparams(**config.env_hyperparams),
204
+ override_n_envs=study_args.n_eval_envs,
205
+ )
206
+ callback = OptimizeCallback(
207
+ policy,
208
+ eval_env,
209
+ trial,
210
+ tb_writer,
211
+ step_freq=config.n_timesteps // study_args.n_evaluations,
212
+ n_episodes=study_args.n_eval_episodes,
213
+ deterministic=config.eval_params.get("deterministic", True),
214
+ )
215
+ try:
216
+ algo.learn(config.n_timesteps, callback=callback)
217
+
218
+ if not callback.is_pruned:
219
+ callback.evaluate()
220
+ if not callback.is_pruned:
221
+ policy.save(config.model_dir_path(best=False))
222
+
223
+ eval_stat: EpisodesStats = callback.last_eval_stat # type: ignore
224
+ train_stat: EpisodesStats = callback.last_train_stat # type: ignore
225
+
226
+ tb_writer.add_hparams(
227
+ hparam_dict(hyperparams, vars(args)),
228
+ {
229
+ "hparam/last_mean": eval_stat.score.mean,
230
+ "hparam/last_result": eval_stat.score.mean - eval_stat.score.std,
231
+ "hparam/train_mean": train_stat.score.mean,
232
+ "hparam/train_result": train_stat.score.mean - train_stat.score.std,
233
+ "hparam/score": callback.last_score,
234
+ "hparam/is_pruned": callback.is_pruned,
235
+ },
236
+ None,
237
+ config.run_name(),
238
+ )
239
+ tb_writer.close()
240
+
241
+ if wandb_enabled:
242
+ wandb.run.summary["state"] = "Pruned" if callback.is_pruned else "Complete"
243
+ wandb.finish(quiet=True)
244
+
245
+ if callback.is_pruned:
246
+ raise optuna.exceptions.TrialPruned()
247
+
248
+ return callback.last_score
249
+ except AssertionError as e:
250
+ logging.warning(e)
251
+ return np.nan
252
+ finally:
253
+ env.close()
254
+ eval_env.close()
255
+ gc.collect()
256
+ torch.cuda.empty_cache()
257
+
258
+
259
+ def stepwise_optimize(
260
+ trial: optuna.Trial, args: Sequence[RunArgs], study_args: StudyArgs
261
+ ) -> float:
262
+ algo = args[0].algo
263
+ env_id = args[0].env
264
+ base_hyperparams = load_hyperparams(algo, env_id)
265
+ base_config = Config(args[0], base_hyperparams, os.getcwd())
266
+ if algo == "a2c":
267
+ hyperparams = a2c_sample_params(trial, base_hyperparams, base_config)
268
+ else:
269
+ raise ValueError(f"Optimizing {algo} isn't supported")
270
+
271
+ wandb_enabled = bool(study_args.wandb_project_name)
272
+ if wandb_enabled:
273
+ wandb.init(
274
+ project=study_args.wandb_project_name,
275
+ entity=study_args.wandb_entity,
276
+ config=asdict(hyperparams),
277
+ name=f"{str(trial.number)}-S{base_config.seed()}",
278
+ tags=study_args.wandb_tags,
279
+ group=study_args.wandb_group,
280
+ save_code=True,
281
+ reinit=True,
282
+ )
283
+
284
+ score = -np.inf
285
+
286
+ for i in range(study_args.n_evaluations):
287
+ evaluations: List[Evaluation] = []
288
+
289
+ for arg in args:
290
+ config = Config(arg, hyperparams, os.getcwd())
291
+
292
+ tb_writer = SummaryWriter(config.tensorboard_summary_path)
293
+ set_seeds(arg.seed, arg.use_deterministic_algorithms)
294
+
295
+ env = make_env(
296
+ config,
297
+ EnvHyperparams(**config.env_hyperparams),
298
+ normalize_load_path=config.model_dir_path() if i > 0 else None,
299
+ tb_writer=tb_writer,
300
+ )
301
+ device = get_device(config, env)
302
+ policy = make_policy(arg.algo, env, device, **config.policy_hyperparams)
303
+ if i > 0:
304
+ policy.load(config.model_dir_path())
305
+ algo = ALGOS[arg.algo](
306
+ policy, env, device, tb_writer, **config.algo_hyperparams
307
+ )
308
+
309
+ eval_env = make_eval_env(
310
+ config,
311
+ EnvHyperparams(**config.env_hyperparams),
312
+ normalize_load_path=config.model_dir_path() if i > 0 else None,
313
+ override_n_envs=study_args.n_eval_envs,
314
+ )
315
+
316
+ start_timesteps = int(i * config.n_timesteps / study_args.n_evaluations)
317
+ train_timesteps = (
318
+ int((i + 1) * config.n_timesteps / study_args.n_evaluations)
319
+ - start_timesteps
320
+ )
321
+
322
+ try:
323
+ algo.learn(
324
+ train_timesteps,
325
+ callback=None,
326
+ total_timesteps=config.n_timesteps,
327
+ start_timesteps=start_timesteps,
328
+ )
329
+
330
+ evaluations.append(
331
+ evaluation(
332
+ policy,
333
+ eval_env,
334
+ tb_writer,
335
+ study_args.n_eval_episodes,
336
+ config.eval_params.get("deterministic", True),
337
+ start_timesteps + train_timesteps,
338
+ )
339
+ )
340
+
341
+ policy.save(config.model_dir_path())
342
+
343
+ tb_writer.close()
344
+
345
+ except AssertionError as e:
346
+ logging.warning(e)
347
+ if wandb_enabled:
348
+ wandb_finish("Error")
349
+ return np.nan
350
+ finally:
351
+ env.close()
352
+ eval_env.close()
353
+ gc.collect()
354
+ torch.cuda.empty_cache()
355
+
356
+ d = {}
357
+ for idx, e in enumerate(evaluations):
358
+ d[f"{idx}/eval_mean"] = e.eval_stat.score.mean
359
+ d[f"{idx}/train_mean"] = e.train_stat.score.mean
360
+ d[f"{idx}/score"] = e.score
361
+ d["eval"] = np.mean([e.eval_stat.score.mean for e in evaluations]).item()
362
+ d["train"] = np.mean([e.train_stat.score.mean for e in evaluations]).item()
363
+ score = np.mean([e.score for e in evaluations]).item()
364
+ d["score"] = score
365
+
366
+ step = i + 1
367
+ wandb.log(d, step=step)
368
+
369
+ print(f"Trial #{trial.number} Step {step} Score: {round(score, 2)}")
370
+ trial.report(score, step)
371
+ if trial.should_prune():
372
+ if wandb_enabled:
373
+ wandb_finish("Pruned")
374
+ raise optuna.exceptions.TrialPruned()
375
+
376
+ if wandb_enabled:
377
+ wandb_finish("Complete")
378
+ return score
379
+
380
+
381
+ def wandb_finish(state: str) -> None:
382
+ wandb.run.summary["state"] = state
383
+ wandb.finish(quiet=True)
384
+
385
+
386
+ def optimize() -> None:
387
+ from pyvirtualdisplay.display import Display
388
+
389
+ train_args, study_args = parse_args()
390
+ if study_args.virtual_display:
391
+ virtual_display = Display(visible=False, size=(1400, 900))
392
+ virtual_display.start()
393
+
394
+ sampler = TPESampler(**TPESampler.hyperopt_parameters())
395
+ pruner = HyperbandPruner()
396
+ if study_args.load_study:
397
+ assert study_args.study_name
398
+ assert study_args.storage_path
399
+ study = optuna.load_study(
400
+ study_name=study_args.study_name,
401
+ storage=study_args.storage_path,
402
+ sampler=sampler,
403
+ pruner=pruner,
404
+ )
405
+ else:
406
+ study = optuna.create_study(
407
+ study_name=study_args.study_name,
408
+ storage=study_args.storage_path,
409
+ sampler=sampler,
410
+ pruner=pruner,
411
+ direction="maximize",
412
+ )
413
+
414
+ try:
415
+ study.optimize(
416
+ objective_fn(train_args, study_args),
417
+ n_trials=study_args.n_trials,
418
+ n_jobs=study_args.n_jobs,
419
+ timeout=study_args.timeout,
420
+ )
421
+ except KeyboardInterrupt:
422
+ pass
423
+
424
+ best = study.best_trial
425
+ print(f"Best Trial Value: {best.value}")
426
+ print("Attributes:")
427
+ for key, value in list(best.params.items()) + list(best.user_attrs.items()):
428
+ print(f" {key}: {value}")
429
+
430
+ df = study.trials_dataframe()
431
+ df = df[df.state == "COMPLETE"].sort_values(by=["value"], ascending=False)
432
+ print(df.to_markdown(index=False))
433
+
434
+ fig1 = plot_optimization_history(study)
435
+ fig1.write_image("opt_history.png")
436
+
437
+ fig2 = plot_param_importances(study)
438
+ fig2.write_image("param_importances.png")
439
+
440
+
441
+ if __name__ == "__main__":
442
+ optimize()
rl_algo_impls/ppo/ppo.py ADDED
@@ -0,0 +1,370 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ import numpy as np
3
+ import torch
4
+ import torch.nn as nn
5
+
6
+ from dataclasses import asdict, dataclass
7
+ from time import perf_counter
8
+ from torch.optim import Adam
9
+ from torch.utils.tensorboard.writer import SummaryWriter
10
+ from typing import List, Optional, NamedTuple, TypeVar
11
+
12
+ from rl_algo_impls.shared.algorithm import Algorithm
13
+ from rl_algo_impls.shared.callbacks.callback import Callback
14
+ from rl_algo_impls.shared.gae import (
15
+ compute_advantages,
16
+ )
17
+ from rl_algo_impls.shared.policy.on_policy import ActorCritic
18
+ from rl_algo_impls.shared.schedule import (
19
+ schedule,
20
+ update_learning_rate,
21
+ )
22
+ from rl_algo_impls.shared.stats import log_scalars
23
+ from rl_algo_impls.wrappers.action_mask_wrapper import ActionMaskWrapper
24
+ from rl_algo_impls.wrappers.vectorable_wrapper import (
25
+ VecEnv,
26
+ find_wrapper,
27
+ single_observation_space,
28
+ single_action_space,
29
+ )
30
+
31
+
32
+ class TrainStepStats(NamedTuple):
33
+ loss: float
34
+ pi_loss: float
35
+ v_loss: float
36
+ entropy_loss: float
37
+ approx_kl: float
38
+ clipped_frac: float
39
+ val_clipped_frac: float
40
+
41
+
42
+ @dataclass
43
+ class TrainStats:
44
+ loss: float
45
+ pi_loss: float
46
+ v_loss: float
47
+ entropy_loss: float
48
+ approx_kl: float
49
+ clipped_frac: float
50
+ val_clipped_frac: float
51
+ explained_var: float
52
+
53
+ def __init__(self, step_stats: List[TrainStepStats], explained_var: float) -> None:
54
+ self.loss = np.mean([s.loss for s in step_stats]).item()
55
+ self.pi_loss = np.mean([s.pi_loss for s in step_stats]).item()
56
+ self.v_loss = np.mean([s.v_loss for s in step_stats]).item()
57
+ self.entropy_loss = np.mean([s.entropy_loss for s in step_stats]).item()
58
+ self.approx_kl = np.mean([s.approx_kl for s in step_stats]).item()
59
+ self.clipped_frac = np.mean([s.clipped_frac for s in step_stats]).item()
60
+ self.val_clipped_frac = np.mean([s.val_clipped_frac for s in step_stats]).item()
61
+ self.explained_var = explained_var
62
+
63
+ def write_to_tensorboard(self, tb_writer: SummaryWriter, global_step: int) -> None:
64
+ for name, value in asdict(self).items():
65
+ tb_writer.add_scalar(f"losses/{name}", value, global_step=global_step)
66
+
67
+ def __repr__(self) -> str:
68
+ return " | ".join(
69
+ [
70
+ f"Loss: {round(self.loss, 2)}",
71
+ f"Pi L: {round(self.pi_loss, 2)}",
72
+ f"V L: {round(self.v_loss, 2)}",
73
+ f"E L: {round(self.entropy_loss, 2)}",
74
+ f"Apx KL Div: {round(self.approx_kl, 2)}",
75
+ f"Clip Frac: {round(self.clipped_frac, 2)}",
76
+ f"Val Clip Frac: {round(self.val_clipped_frac, 2)}",
77
+ ]
78
+ )
79
+
80
+
81
+ PPOSelf = TypeVar("PPOSelf", bound="PPO")
82
+
83
+
84
+ class PPO(Algorithm):
85
+ def __init__(
86
+ self,
87
+ policy: ActorCritic,
88
+ env: VecEnv,
89
+ device: torch.device,
90
+ tb_writer: SummaryWriter,
91
+ learning_rate: float = 3e-4,
92
+ learning_rate_decay: str = "none",
93
+ n_steps: int = 2048,
94
+ batch_size: int = 64,
95
+ n_epochs: int = 10,
96
+ gamma: float = 0.99,
97
+ gae_lambda: float = 0.95,
98
+ clip_range: float = 0.2,
99
+ clip_range_decay: str = "none",
100
+ clip_range_vf: Optional[float] = None,
101
+ clip_range_vf_decay: str = "none",
102
+ normalize_advantage: bool = True,
103
+ ent_coef: float = 0.0,
104
+ ent_coef_decay: str = "none",
105
+ vf_coef: float = 0.5,
106
+ ppo2_vf_coef_halving: bool = True,
107
+ max_grad_norm: float = 0.5,
108
+ sde_sample_freq: int = -1,
109
+ ) -> None:
110
+ super().__init__(policy, env, device, tb_writer)
111
+ self.policy = policy
112
+ self.action_masker = find_wrapper(env, ActionMaskWrapper)
113
+
114
+ self.gamma = gamma
115
+ self.gae_lambda = gae_lambda
116
+ self.optimizer = Adam(self.policy.parameters(), lr=learning_rate, eps=1e-7)
117
+ self.lr_schedule = schedule(learning_rate_decay, learning_rate)
118
+ self.max_grad_norm = max_grad_norm
119
+ self.clip_range_schedule = schedule(clip_range_decay, clip_range)
120
+ self.clip_range_vf_schedule = None
121
+ if clip_range_vf:
122
+ self.clip_range_vf_schedule = schedule(clip_range_vf_decay, clip_range_vf)
123
+
124
+ if normalize_advantage:
125
+ assert (
126
+ env.num_envs * n_steps > 1 and batch_size > 1
127
+ ), f"Each minibatch must be larger than 1 to support normalization"
128
+ self.normalize_advantage = normalize_advantage
129
+
130
+ self.ent_coef_schedule = schedule(ent_coef_decay, ent_coef)
131
+ self.vf_coef = vf_coef
132
+ self.ppo2_vf_coef_halving = ppo2_vf_coef_halving
133
+
134
+ self.n_steps = n_steps
135
+ self.batch_size = batch_size
136
+ self.n_epochs = n_epochs
137
+ self.sde_sample_freq = sde_sample_freq
138
+
139
+ def learn(
140
+ self: PPOSelf,
141
+ train_timesteps: int,
142
+ callback: Optional[Callback] = None,
143
+ total_timesteps: Optional[int] = None,
144
+ start_timesteps: int = 0,
145
+ ) -> PPOSelf:
146
+ if total_timesteps is None:
147
+ total_timesteps = train_timesteps
148
+ assert start_timesteps + train_timesteps <= total_timesteps
149
+
150
+ epoch_dim = (self.n_steps, self.env.num_envs)
151
+ step_dim = (self.env.num_envs,)
152
+ obs_space = single_observation_space(self.env)
153
+ act_space = single_action_space(self.env)
154
+
155
+ next_obs = self.env.reset()
156
+ next_action_masks = (
157
+ self.action_masker.action_masks() if self.action_masker else None
158
+ )
159
+ next_episode_starts = np.full(step_dim, True, dtype=np.bool8)
160
+
161
+ obs = np.zeros(epoch_dim + obs_space.shape, dtype=obs_space.dtype) # type: ignore
162
+ actions = np.zeros(epoch_dim + act_space.shape, dtype=act_space.dtype) # type: ignore
163
+ rewards = np.zeros(epoch_dim, dtype=np.float32)
164
+ episode_starts = np.zeros(epoch_dim, dtype=np.bool8)
165
+ values = np.zeros(epoch_dim, dtype=np.float32)
166
+ logprobs = np.zeros(epoch_dim, dtype=np.float32)
167
+ action_masks = (
168
+ np.zeros(
169
+ (self.n_steps,) + next_action_masks.shape, dtype=next_action_masks.dtype
170
+ )
171
+ if next_action_masks is not None
172
+ else None
173
+ )
174
+
175
+ timesteps_elapsed = start_timesteps
176
+ while timesteps_elapsed < start_timesteps + train_timesteps:
177
+ start_time = perf_counter()
178
+
179
+ progress = timesteps_elapsed / total_timesteps
180
+ ent_coef = self.ent_coef_schedule(progress)
181
+ learning_rate = self.lr_schedule(progress)
182
+ update_learning_rate(self.optimizer, learning_rate)
183
+ pi_clip = self.clip_range_schedule(progress)
184
+ chart_scalars = {
185
+ "learning_rate": self.optimizer.param_groups[0]["lr"],
186
+ "ent_coef": ent_coef,
187
+ "pi_clip": pi_clip,
188
+ }
189
+ if self.clip_range_vf_schedule:
190
+ v_clip = self.clip_range_vf_schedule(progress)
191
+ chart_scalars["v_clip"] = v_clip
192
+ else:
193
+ v_clip = None
194
+ log_scalars(self.tb_writer, "charts", chart_scalars, timesteps_elapsed)
195
+
196
+ self.policy.eval()
197
+ self.policy.reset_noise()
198
+ for s in range(self.n_steps):
199
+ timesteps_elapsed += self.env.num_envs
200
+ if self.sde_sample_freq > 0 and s > 0 and s % self.sde_sample_freq == 0:
201
+ self.policy.reset_noise()
202
+
203
+ obs[s] = next_obs
204
+ episode_starts[s] = next_episode_starts
205
+ if action_masks is not None:
206
+ action_masks[s] = next_action_masks
207
+
208
+ (
209
+ actions[s],
210
+ values[s],
211
+ logprobs[s],
212
+ clamped_action,
213
+ ) = self.policy.step(next_obs, action_masks=next_action_masks)
214
+ next_obs, rewards[s], next_episode_starts, _ = self.env.step(
215
+ clamped_action
216
+ )
217
+ next_action_masks = (
218
+ self.action_masker.action_masks() if self.action_masker else None
219
+ )
220
+
221
+ self.policy.train()
222
+
223
+ advantages = compute_advantages(
224
+ rewards,
225
+ values,
226
+ episode_starts,
227
+ next_episode_starts,
228
+ next_obs,
229
+ self.policy,
230
+ self.gamma,
231
+ self.gae_lambda,
232
+ )
233
+ returns = advantages + values
234
+
235
+ b_obs = torch.tensor(obs.reshape((-1,) + obs_space.shape)).to(self.device) # type: ignore
236
+ b_actions = torch.tensor(actions.reshape((-1,) + act_space.shape)).to( # type: ignore
237
+ self.device
238
+ )
239
+ b_logprobs = torch.tensor(logprobs.reshape(-1)).to(self.device)
240
+ b_action_masks = (
241
+ torch.tensor(action_masks.reshape((-1,) + next_action_masks.shape[1:])).to( # type: ignore
242
+ self.device
243
+ )
244
+ if action_masks is not None
245
+ else None
246
+ )
247
+
248
+ b_advantages = torch.tensor(advantages.reshape(-1)).to(self.device)
249
+
250
+ y_pred = values.reshape(-1)
251
+ b_values = torch.tensor(y_pred).to(self.device)
252
+ y_true = returns.reshape(-1)
253
+ b_returns = torch.tensor(y_true).to(self.device)
254
+
255
+ step_stats = []
256
+ for _ in range(self.n_epochs):
257
+ b_idxs = torch.randperm(len(b_obs))
258
+ # Only record last epoch's stats
259
+ step_stats.clear()
260
+ for i in range(0, len(b_obs), self.batch_size):
261
+ self.policy.reset_noise(self.batch_size)
262
+
263
+ mb_idxs = b_idxs[i : i + self.batch_size]
264
+
265
+ mb_obs = b_obs[mb_idxs]
266
+ mb_actions = b_actions[mb_idxs]
267
+ mb_values = b_values[mb_idxs]
268
+ mb_logprobs = b_logprobs[mb_idxs]
269
+ mb_action_masks = (
270
+ b_action_masks[mb_idxs] if b_action_masks is not None else None
271
+ )
272
+
273
+ mb_adv = b_advantages[mb_idxs]
274
+ if self.normalize_advantage:
275
+ mb_adv = (mb_adv - mb_adv.mean()) / (mb_adv.std() + 1e-8)
276
+ mb_returns = b_returns[mb_idxs]
277
+
278
+ new_logprobs, entropy, new_values = self.policy(
279
+ mb_obs, mb_actions, action_masks=mb_action_masks
280
+ )
281
+
282
+ logratio = new_logprobs - mb_logprobs
283
+ ratio = torch.exp(logratio)
284
+ clipped_ratio = torch.clamp(ratio, min=1 - pi_clip, max=1 + pi_clip)
285
+ pi_loss = torch.max(
286
+ -ratio * mb_adv, -clipped_ratio * mb_adv
287
+ ).mean()
288
+
289
+ v_loss_unclipped = (new_values - mb_returns) ** 2
290
+ if v_clip:
291
+ v_loss_clipped = (
292
+ mb_values
293
+ + torch.clamp(new_values - mb_values, -v_clip, v_clip)
294
+ - mb_returns
295
+ ) ** 2
296
+ v_loss = torch.max(v_loss_unclipped, v_loss_clipped).mean()
297
+ else:
298
+ v_loss = v_loss_unclipped.mean()
299
+
300
+ if self.ppo2_vf_coef_halving:
301
+ v_loss *= 0.5
302
+
303
+ entropy_loss = -entropy.mean()
304
+
305
+ loss = pi_loss + ent_coef * entropy_loss + self.vf_coef * v_loss
306
+
307
+ self.optimizer.zero_grad()
308
+ loss.backward()
309
+ nn.utils.clip_grad_norm_(
310
+ self.policy.parameters(), self.max_grad_norm
311
+ )
312
+ self.optimizer.step()
313
+
314
+ with torch.no_grad():
315
+ approx_kl = ((ratio - 1) - logratio).mean().cpu().numpy().item()
316
+ clipped_frac = (
317
+ ((ratio - 1).abs() > pi_clip)
318
+ .float()
319
+ .mean()
320
+ .cpu()
321
+ .numpy()
322
+ .item()
323
+ )
324
+ val_clipped_frac = (
325
+ ((new_values - mb_values).abs() > v_clip)
326
+ .float()
327
+ .mean()
328
+ .cpu()
329
+ .numpy()
330
+ .item()
331
+ if v_clip
332
+ else 0
333
+ )
334
+
335
+ step_stats.append(
336
+ TrainStepStats(
337
+ loss.item(),
338
+ pi_loss.item(),
339
+ v_loss.item(),
340
+ entropy_loss.item(),
341
+ approx_kl,
342
+ clipped_frac,
343
+ val_clipped_frac,
344
+ )
345
+ )
346
+
347
+ var_y = np.var(y_true).item()
348
+ explained_var = (
349
+ np.nan if var_y == 0 else 1 - np.var(y_true - y_pred).item() / var_y
350
+ )
351
+ TrainStats(step_stats, explained_var).write_to_tensorboard(
352
+ self.tb_writer, timesteps_elapsed
353
+ )
354
+
355
+ end_time = perf_counter()
356
+ rollout_steps = self.n_steps * self.env.num_envs
357
+ self.tb_writer.add_scalar(
358
+ "train/steps_per_second",
359
+ rollout_steps / (end_time - start_time),
360
+ timesteps_elapsed,
361
+ )
362
+
363
+ if callback:
364
+ if not callback.on_step(timesteps_elapsed=rollout_steps):
365
+ logging.info(
366
+ f"Callback terminated training at {timesteps_elapsed} timesteps"
367
+ )
368
+ break
369
+
370
+ return self
rl_algo_impls/publish/markdown_format.py ADDED
@@ -0,0 +1,210 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import pandas as pd
3
+ import wandb.apis.public
4
+ import yaml
5
+
6
+ from collections import defaultdict
7
+ from dataclasses import dataclass, asdict
8
+ from typing import Any, Dict, Iterable, List, NamedTuple, Optional, TypeVar
9
+ from urllib.parse import urlparse
10
+
11
+ from rl_algo_impls.runner.evaluate import Evaluation
12
+
13
+ EvaluationRowSelf = TypeVar("EvaluationRowSelf", bound="EvaluationRow")
14
+
15
+
16
+ @dataclass
17
+ class EvaluationRow:
18
+ algo: str
19
+ env: str
20
+ seed: Optional[int]
21
+ reward_mean: float
22
+ reward_std: float
23
+ eval_episodes: int
24
+ best: str
25
+ wandb_url: str
26
+
27
+ @staticmethod
28
+ def data_frame(rows: List[EvaluationRowSelf]) -> pd.DataFrame:
29
+ results = defaultdict(list)
30
+ for r in rows:
31
+ for k, v in asdict(r).items():
32
+ results[k].append(v)
33
+ return pd.DataFrame(results)
34
+
35
+
36
+ class EvalTableData(NamedTuple):
37
+ run: wandb.apis.public.Run
38
+ evaluation: Evaluation
39
+
40
+
41
+ def evaluation_table(table_data: Iterable[EvalTableData]) -> str:
42
+ best_stats = sorted(
43
+ [d.evaluation.stats for d in table_data], key=lambda r: r.score, reverse=True
44
+ )[0]
45
+ table_data = sorted(table_data, key=lambda d: d.evaluation.config.seed() or 0)
46
+ rows = [
47
+ EvaluationRow(
48
+ config.algo,
49
+ config.env_id,
50
+ config.seed(),
51
+ stats.score.mean,
52
+ stats.score.std,
53
+ len(stats),
54
+ "*" if stats == best_stats else "",
55
+ f"[wandb]({r.url})",
56
+ )
57
+ for (r, (_, stats, config)) in table_data
58
+ ]
59
+ df = EvaluationRow.data_frame(rows)
60
+ return df.to_markdown(index=False)
61
+
62
+
63
+ def github_project_link(github_url: str) -> str:
64
+ return f"[{urlparse(github_url).path}]({github_url})"
65
+
66
+
67
+ def header_section(algo: str, env: str, github_url: str, wandb_report_url: str) -> str:
68
+ algo_caps = algo.upper()
69
+ lines = [
70
+ f"# **{algo_caps}** Agent playing **{env}**",
71
+ f"This is a trained model of a **{algo_caps}** agent playing **{env}** using "
72
+ f"the {github_project_link(github_url)} repo.",
73
+ f"All models trained at this commit can be found at {wandb_report_url}.",
74
+ ]
75
+ return "\n\n".join(lines)
76
+
77
+
78
+ def github_tree_link(github_url: str, commit_hash: Optional[str]) -> str:
79
+ if not commit_hash:
80
+ return github_project_link(github_url)
81
+ return f"[{commit_hash[:7]}]({github_url}/tree/{commit_hash})"
82
+
83
+
84
+ def results_section(
85
+ table_data: List[EvalTableData], algo: str, github_url: str, commit_hash: str
86
+ ) -> str:
87
+ # type: ignore
88
+ lines = [
89
+ "## Training Results",
90
+ f"This model was trained from {len(table_data)} trainings of **{algo.upper()}** "
91
+ + "agents using different initial seeds. "
92
+ + f"These agents were trained by checking out "
93
+ + f"{github_tree_link(github_url, commit_hash)}. "
94
+ + "The best and last models were kept from each training. "
95
+ + "This submission has loaded the best models from each training, reevaluates "
96
+ + "them, and selects the best model from these latest evaluations (mean - std).",
97
+ ]
98
+ lines.append(evaluation_table(table_data))
99
+ return "\n\n".join(lines)
100
+
101
+
102
+ def prerequisites_section() -> str:
103
+ return """
104
+ ### Prerequisites: Weights & Biases (WandB)
105
+ Training and benchmarking assumes you have a Weights & Biases project to upload runs to.
106
+ By default training goes to a rl-algo-impls project while benchmarks go to
107
+ rl-algo-impls-benchmarks. During training and benchmarking runs, videos of the best
108
+ models and the model weights are uploaded to WandB.
109
+
110
+ Before doing anything below, you'll need to create a wandb account and run `wandb
111
+ login`.
112
+ """
113
+
114
+
115
+ def usage_section(github_url: str, run_path: str, commit_hash: str) -> str:
116
+ return f"""
117
+ ## Usage
118
+ {urlparse(github_url).path}: {github_url}
119
+
120
+ Note: While the model state dictionary and hyperaparameters are saved, the latest
121
+ implementation could be sufficiently different to not be able to reproduce similar
122
+ results. You might need to checkout the commit the agent was trained on:
123
+ {github_tree_link(github_url, commit_hash)}.
124
+ ```
125
+ # Downloads the model, sets hyperparameters, and runs agent for 3 episodes
126
+ python enjoy.py --wandb-run-path={run_path}
127
+ ```
128
+
129
+ Setup hasn't been completely worked out yet, so you might be best served by using Google
130
+ Colab starting from the
131
+ [colab_enjoy.ipynb](https://github.com/sgoodfriend/rl-algo-impls/blob/main/colab_enjoy.ipynb)
132
+ notebook.
133
+ """
134
+
135
+
136
+ def training_setion(
137
+ github_url: str, commit_hash: str, algo: str, env: str, seed: Optional[int]
138
+ ) -> str:
139
+ return f"""
140
+ ## Training
141
+ If you want the highest chance to reproduce these results, you'll want to checkout the
142
+ commit the agent was trained on: {github_tree_link(github_url, commit_hash)}. While
143
+ training is deterministic, different hardware will give different results.
144
+
145
+ ```
146
+ python train.py --algo {algo} --env {env} {'--seed ' + str(seed) if seed is not None else ''}
147
+ ```
148
+
149
+ Setup hasn't been completely worked out yet, so you might be best served by using Google
150
+ Colab starting from the
151
+ [colab_train.ipynb](https://github.com/sgoodfriend/rl-algo-impls/blob/main/colab_train.ipynb)
152
+ notebook.
153
+ """
154
+
155
+
156
+ def benchmarking_section(report_url: str) -> str:
157
+ return f"""
158
+ ## Benchmarking (with Lambda Labs instance)
159
+ This and other models from {report_url} were generated by running a script on a Lambda
160
+ Labs instance. In a Lambda Labs instance terminal:
161
+ ```
162
+ git clone git@github.com:sgoodfriend/rl-algo-impls.git
163
+ cd rl-algo-impls
164
+ bash ./lambda_labs/setup.sh
165
+ wandb login
166
+ bash ./lambda_labs/benchmark.sh [-a {{"ppo a2c dqn vpg"}}] [-e ENVS] [-j {{6}}] [-p {{rl-algo-impls-benchmarks}}] [-s {{"1 2 3"}}]
167
+ ```
168
+
169
+ ### Alternative: Google Colab Pro+
170
+ As an alternative,
171
+ [colab_benchmark.ipynb](https://github.com/sgoodfriend/rl-algo-impls/tree/main/benchmarks#:~:text=colab_benchmark.ipynb),
172
+ can be used. However, this requires a Google Colab Pro+ subscription and running across
173
+ 4 separate instances because otherwise running all jobs will exceed the 24-hour limit.
174
+ """
175
+
176
+
177
+ def hyperparams_section(run_config: Dict[str, Any]) -> str:
178
+ return f"""
179
+ ## Hyperparameters
180
+ This isn't exactly the format of hyperparams in {os.path.join("hyperparams",
181
+ run_config["algo"] + ".yml")}, but instead the Wandb Run Config. However, it's very
182
+ close and has some additional data:
183
+ ```
184
+ {yaml.dump(run_config)}
185
+ ```
186
+ """
187
+
188
+
189
+ def model_card_text(
190
+ algo: str,
191
+ env: str,
192
+ github_url: str,
193
+ commit_hash: str,
194
+ wandb_report_url: str,
195
+ table_data: List[EvalTableData],
196
+ best_eval: EvalTableData,
197
+ ) -> str:
198
+ run, (_, _, config) = best_eval
199
+ run_path = "/".join(run.path)
200
+ return "\n\n".join(
201
+ [
202
+ header_section(algo, env, github_url, wandb_report_url),
203
+ results_section(table_data, algo, github_url, commit_hash),
204
+ prerequisites_section(),
205
+ usage_section(github_url, run_path, commit_hash),
206
+ training_setion(github_url, commit_hash, algo, env, config.seed()),
207
+ benchmarking_section(wandb_report_url),
208
+ hyperparams_section(run.config),
209
+ ]
210
+ )
rl_algo_impls/runner/config.py ADDED
@@ -0,0 +1,191 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import dataclasses
2
+ import inspect
3
+ import itertools
4
+ import os
5
+
6
+ from datetime import datetime
7
+ from dataclasses import dataclass
8
+ from typing import Any, Dict, List, Optional, Type, TypeVar, Union
9
+
10
+
11
+ RunArgsSelf = TypeVar("RunArgsSelf", bound="RunArgs")
12
+
13
+
14
+ @dataclass
15
+ class RunArgs:
16
+ algo: str
17
+ env: str
18
+ seed: Optional[int] = None
19
+ use_deterministic_algorithms: bool = True
20
+
21
+ @classmethod
22
+ def expand_from_dict(
23
+ cls: Type[RunArgsSelf], d: Dict[str, Any]
24
+ ) -> List[RunArgsSelf]:
25
+ maybe_listify = lambda v: [v] if isinstance(v, str) or isinstance(v, int) else v
26
+ algos = maybe_listify(d["algo"])
27
+ envs = maybe_listify(d["env"])
28
+ seeds = maybe_listify(d["seed"])
29
+ args = []
30
+ for algo, env, seed in itertools.product(algos, envs, seeds):
31
+ _d = d.copy()
32
+ _d.update({"algo": algo, "env": env, "seed": seed})
33
+ args.append(cls(**_d))
34
+ return args
35
+
36
+
37
+ @dataclass
38
+ class EnvHyperparams:
39
+ env_type: str = "gymvec"
40
+ n_envs: int = 1
41
+ frame_stack: int = 1
42
+ make_kwargs: Optional[Dict[str, Any]] = None
43
+ no_reward_timeout_steps: Optional[int] = None
44
+ no_reward_fire_steps: Optional[int] = None
45
+ vec_env_class: str = "sync"
46
+ normalize: bool = False
47
+ normalize_kwargs: Optional[Dict[str, Any]] = None
48
+ rolling_length: int = 100
49
+ train_record_video: bool = False
50
+ video_step_interval: Union[int, float] = 1_000_000
51
+ initial_steps_to_truncate: Optional[int] = None
52
+ clip_atari_rewards: bool = True
53
+ normalize_type: Optional[str] = None
54
+ mask_actions: bool = False
55
+
56
+
57
+ HyperparamsSelf = TypeVar("HyperparamsSelf", bound="Hyperparams")
58
+
59
+
60
+ @dataclass
61
+ class Hyperparams:
62
+ device: str = "auto"
63
+ n_timesteps: Union[int, float] = 100_000
64
+ env_hyperparams: Dict[str, Any] = dataclasses.field(default_factory=dict)
65
+ policy_hyperparams: Dict[str, Any] = dataclasses.field(default_factory=dict)
66
+ algo_hyperparams: Dict[str, Any] = dataclasses.field(default_factory=dict)
67
+ eval_params: Dict[str, Any] = dataclasses.field(default_factory=dict)
68
+ env_id: Optional[str] = None
69
+
70
+ @classmethod
71
+ def from_dict_with_extra_fields(
72
+ cls: Type[HyperparamsSelf], d: Dict[str, Any]
73
+ ) -> HyperparamsSelf:
74
+ return cls(
75
+ **{k: v for k, v in d.items() if k in inspect.signature(cls).parameters}
76
+ )
77
+
78
+
79
+ @dataclass
80
+ class Config:
81
+ args: RunArgs
82
+ hyperparams: Hyperparams
83
+ root_dir: str
84
+ run_id: str = datetime.now().isoformat()
85
+
86
+ def seed(self, training: bool = True) -> Optional[int]:
87
+ seed = self.args.seed
88
+ if training or seed is None:
89
+ return seed
90
+ return seed + self.env_hyperparams.get("n_envs", 1)
91
+
92
+ @property
93
+ def device(self) -> str:
94
+ return self.hyperparams.device
95
+
96
+ @property
97
+ def n_timesteps(self) -> int:
98
+ return int(self.hyperparams.n_timesteps)
99
+
100
+ @property
101
+ def env_hyperparams(self) -> Dict[str, Any]:
102
+ return self.hyperparams.env_hyperparams
103
+
104
+ @property
105
+ def policy_hyperparams(self) -> Dict[str, Any]:
106
+ return self.hyperparams.policy_hyperparams
107
+
108
+ @property
109
+ def algo_hyperparams(self) -> Dict[str, Any]:
110
+ return self.hyperparams.algo_hyperparams
111
+
112
+ @property
113
+ def eval_params(self) -> Dict[str, Any]:
114
+ return self.hyperparams.eval_params
115
+
116
+ @property
117
+ def algo(self) -> str:
118
+ return self.args.algo
119
+
120
+ @property
121
+ def env_id(self) -> str:
122
+ return self.hyperparams.env_id or self.args.env
123
+
124
+ def model_name(self, include_seed: bool = True) -> str:
125
+ # Use arg env name instead of environment name
126
+ parts = [self.algo, self.args.env]
127
+ if include_seed and self.args.seed is not None:
128
+ parts.append(f"S{self.args.seed}")
129
+
130
+ # Assume that the custom arg name already has the necessary information
131
+ if not self.hyperparams.env_id:
132
+ make_kwargs = self.env_hyperparams.get("make_kwargs", {})
133
+ if make_kwargs:
134
+ for k, v in make_kwargs.items():
135
+ if type(v) == bool and v:
136
+ parts.append(k)
137
+ elif type(v) == int and v:
138
+ parts.append(f"{k}{v}")
139
+ else:
140
+ parts.append(str(v))
141
+
142
+ return "-".join(parts)
143
+
144
+ def run_name(self, include_seed: bool = True) -> str:
145
+ parts = [self.model_name(include_seed=include_seed), self.run_id]
146
+ return "-".join(parts)
147
+
148
+ @property
149
+ def saved_models_dir(self) -> str:
150
+ return os.path.join(self.root_dir, "saved_models")
151
+
152
+ @property
153
+ def downloaded_models_dir(self) -> str:
154
+ return os.path.join(self.root_dir, "downloaded_models")
155
+
156
+ def model_dir_name(
157
+ self,
158
+ best: bool = False,
159
+ extension: str = "",
160
+ ) -> str:
161
+ return self.model_name() + ("-best" if best else "") + extension
162
+
163
+ def model_dir_path(self, best: bool = False, downloaded: bool = False) -> str:
164
+ return os.path.join(
165
+ self.saved_models_dir if not downloaded else self.downloaded_models_dir,
166
+ self.model_dir_name(best=best),
167
+ )
168
+
169
+ @property
170
+ def runs_dir(self) -> str:
171
+ return os.path.join(self.root_dir, "runs")
172
+
173
+ @property
174
+ def tensorboard_summary_path(self) -> str:
175
+ return os.path.join(self.runs_dir, self.run_name())
176
+
177
+ @property
178
+ def logs_path(self) -> str:
179
+ return os.path.join(self.runs_dir, f"log.yml")
180
+
181
+ @property
182
+ def videos_dir(self) -> str:
183
+ return os.path.join(self.root_dir, "videos")
184
+
185
+ @property
186
+ def video_prefix(self) -> str:
187
+ return os.path.join(self.videos_dir, self.model_name())
188
+
189
+ @property
190
+ def best_videos_dir(self) -> str:
191
+ return os.path.join(self.videos_dir, f"{self.model_name()}-best")
rl_algo_impls/runner/env.py ADDED
@@ -0,0 +1,334 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gym
2
+ import numpy as np
3
+ import os
4
+
5
+ from dataclasses import asdict, astuple
6
+ from gym.vector.async_vector_env import AsyncVectorEnv
7
+ from gym.vector.sync_vector_env import SyncVectorEnv
8
+ from gym.wrappers.resize_observation import ResizeObservation
9
+ from gym.wrappers.gray_scale_observation import GrayScaleObservation
10
+ from gym.wrappers.frame_stack import FrameStack
11
+ from stable_baselines3.common.atari_wrappers import (
12
+ MaxAndSkipEnv,
13
+ NoopResetEnv,
14
+ )
15
+ from stable_baselines3.common.vec_env.dummy_vec_env import DummyVecEnv
16
+ from stable_baselines3.common.vec_env.subproc_vec_env import SubprocVecEnv
17
+ from stable_baselines3.common.vec_env.vec_normalize import VecNormalize
18
+ from torch.utils.tensorboard.writer import SummaryWriter
19
+ from typing import Callable, Optional
20
+
21
+ from rl_algo_impls.runner.config import Config, EnvHyperparams
22
+ from rl_algo_impls.shared.policy.policy import VEC_NORMALIZE_FILENAME
23
+ from rl_algo_impls.wrappers.action_mask_wrapper import ActionMaskWrapper
24
+ from rl_algo_impls.wrappers.atari_wrappers import (
25
+ EpisodicLifeEnv,
26
+ FireOnLifeStarttEnv,
27
+ ClipRewardEnv,
28
+ )
29
+ from rl_algo_impls.wrappers.episode_record_video import EpisodeRecordVideo
30
+ from rl_algo_impls.wrappers.episode_stats_writer import EpisodeStatsWriter
31
+ from rl_algo_impls.wrappers.hwc_to_chw_observation import HwcToChwObservation
32
+ from rl_algo_impls.wrappers.initial_step_truncate_wrapper import (
33
+ InitialStepTruncateWrapper,
34
+ )
35
+ from rl_algo_impls.wrappers.is_vector_env import IsVectorEnv
36
+ from rl_algo_impls.wrappers.no_reward_timeout import NoRewardTimeout
37
+ from rl_algo_impls.wrappers.noop_env_seed import NoopEnvSeed
38
+ from rl_algo_impls.wrappers.normalize import NormalizeObservation, NormalizeReward
39
+ from rl_algo_impls.wrappers.sync_vector_env_render_compat import (
40
+ SyncVectorEnvRenderCompat,
41
+ )
42
+ from rl_algo_impls.wrappers.vectorable_wrapper import VecEnv
43
+ from rl_algo_impls.wrappers.video_compat_wrapper import VideoCompatWrapper
44
+
45
+
46
+ def make_env(
47
+ config: Config,
48
+ hparams: EnvHyperparams,
49
+ training: bool = True,
50
+ render: bool = False,
51
+ normalize_load_path: Optional[str] = None,
52
+ tb_writer: Optional[SummaryWriter] = None,
53
+ ) -> VecEnv:
54
+ if hparams.env_type == "procgen":
55
+ return _make_procgen_env(
56
+ config,
57
+ hparams,
58
+ training=training,
59
+ render=render,
60
+ normalize_load_path=normalize_load_path,
61
+ tb_writer=tb_writer,
62
+ )
63
+ elif hparams.env_type in {"sb3vec", "gymvec"}:
64
+ return _make_vec_env(
65
+ config,
66
+ hparams,
67
+ training=training,
68
+ render=render,
69
+ normalize_load_path=normalize_load_path,
70
+ tb_writer=tb_writer,
71
+ )
72
+ else:
73
+ raise ValueError(f"env_type {hparams.env_type} not supported")
74
+
75
+
76
+ def make_eval_env(
77
+ config: Config,
78
+ hparams: EnvHyperparams,
79
+ override_n_envs: Optional[int] = None,
80
+ **kwargs,
81
+ ) -> VecEnv:
82
+ kwargs = kwargs.copy()
83
+ kwargs["training"] = False
84
+ if override_n_envs is not None:
85
+ hparams_kwargs = asdict(hparams)
86
+ hparams_kwargs["n_envs"] = override_n_envs
87
+ if override_n_envs == 1:
88
+ hparams_kwargs["vec_env_class"] = "sync"
89
+ hparams = EnvHyperparams(**hparams_kwargs)
90
+ return make_env(config, hparams, **kwargs)
91
+
92
+
93
+ def _make_vec_env(
94
+ config: Config,
95
+ hparams: EnvHyperparams,
96
+ training: bool = True,
97
+ render: bool = False,
98
+ normalize_load_path: Optional[str] = None,
99
+ tb_writer: Optional[SummaryWriter] = None,
100
+ ) -> VecEnv:
101
+ (
102
+ env_type,
103
+ n_envs,
104
+ frame_stack,
105
+ make_kwargs,
106
+ no_reward_timeout_steps,
107
+ no_reward_fire_steps,
108
+ vec_env_class,
109
+ normalize,
110
+ normalize_kwargs,
111
+ rolling_length,
112
+ train_record_video,
113
+ video_step_interval,
114
+ initial_steps_to_truncate,
115
+ clip_atari_rewards,
116
+ normalize_type,
117
+ mask_actions,
118
+ ) = astuple(hparams)
119
+
120
+ import_for_env_id(config.env_id)
121
+
122
+ seed = config.seed(training=training)
123
+
124
+ make_kwargs = make_kwargs.copy() if make_kwargs is not None else {}
125
+ if is_bullet_env(config) and render:
126
+ make_kwargs["render"] = True
127
+ if is_car_racing(config):
128
+ make_kwargs["verbose"] = 0
129
+ if is_gym_procgen(config) and not render:
130
+ make_kwargs["render_mode"] = "rgb_array"
131
+
132
+ def make(idx: int) -> Callable[[], gym.Env]:
133
+ def _make() -> gym.Env:
134
+ env = gym.make(config.env_id, **make_kwargs)
135
+ env = gym.wrappers.RecordEpisodeStatistics(env)
136
+ env = VideoCompatWrapper(env)
137
+ if training and train_record_video and idx == 0:
138
+ env = EpisodeRecordVideo(
139
+ env,
140
+ config.video_prefix,
141
+ step_increment=n_envs,
142
+ video_step_interval=int(video_step_interval),
143
+ )
144
+ if training and initial_steps_to_truncate:
145
+ env = InitialStepTruncateWrapper(
146
+ env, idx * initial_steps_to_truncate // n_envs
147
+ )
148
+ if is_atari(config): # type: ignore
149
+ env = NoopResetEnv(env, noop_max=30)
150
+ env = MaxAndSkipEnv(env, skip=4)
151
+ env = EpisodicLifeEnv(env, training=training)
152
+ action_meanings = env.unwrapped.get_action_meanings()
153
+ if "FIRE" in action_meanings: # type: ignore
154
+ env = FireOnLifeStarttEnv(env, action_meanings.index("FIRE"))
155
+ if clip_atari_rewards:
156
+ env = ClipRewardEnv(env, training=training)
157
+ env = ResizeObservation(env, (84, 84))
158
+ env = GrayScaleObservation(env, keep_dim=False)
159
+ env = FrameStack(env, frame_stack)
160
+ elif is_car_racing(config):
161
+ env = ResizeObservation(env, (64, 64))
162
+ env = GrayScaleObservation(env, keep_dim=False)
163
+ env = FrameStack(env, frame_stack)
164
+ elif is_gym_procgen(config):
165
+ # env = GrayScaleObservation(env, keep_dim=False)
166
+ env = NoopEnvSeed(env)
167
+ env = HwcToChwObservation(env)
168
+ if frame_stack > 1:
169
+ env = FrameStack(env, frame_stack)
170
+ elif is_microrts(config):
171
+ env = HwcToChwObservation(env)
172
+
173
+ if no_reward_timeout_steps:
174
+ env = NoRewardTimeout(
175
+ env, no_reward_timeout_steps, n_fire_steps=no_reward_fire_steps
176
+ )
177
+
178
+ if seed is not None:
179
+ env.seed(seed + idx)
180
+ env.action_space.seed(seed + idx)
181
+ env.observation_space.seed(seed + idx)
182
+
183
+ return env
184
+
185
+ return _make
186
+
187
+ if env_type == "sb3vec":
188
+ VecEnvClass = {"sync": DummyVecEnv, "async": SubprocVecEnv}[vec_env_class]
189
+ elif env_type == "gymvec":
190
+ VecEnvClass = {"sync": SyncVectorEnv, "async": AsyncVectorEnv}[vec_env_class]
191
+ else:
192
+ raise ValueError(f"env_type {env_type} unsupported")
193
+ envs = VecEnvClass([make(i) for i in range(n_envs)])
194
+ if env_type == "gymvec" and vec_env_class == "sync":
195
+ envs = SyncVectorEnvRenderCompat(envs)
196
+ if env_type == "sb3vec":
197
+ envs = IsVectorEnv(envs)
198
+ if mask_actions:
199
+ envs = ActionMaskWrapper(envs)
200
+ if training:
201
+ assert tb_writer
202
+ envs = EpisodeStatsWriter(
203
+ envs, tb_writer, training=training, rolling_length=rolling_length
204
+ )
205
+ if normalize:
206
+ if normalize_type is None:
207
+ normalize_type = "sb3" if env_type == "sb3vec" else "gymlike"
208
+ normalize_kwargs = normalize_kwargs or {}
209
+ if normalize_type == "sb3":
210
+ if normalize_load_path:
211
+ envs = VecNormalize.load(
212
+ os.path.join(normalize_load_path, VEC_NORMALIZE_FILENAME),
213
+ envs, # type: ignore
214
+ )
215
+ else:
216
+ envs = VecNormalize(
217
+ envs, # type: ignore
218
+ training=training,
219
+ **normalize_kwargs,
220
+ )
221
+ if not training:
222
+ envs.norm_reward = False
223
+ elif normalize_type == "gymlike":
224
+ if normalize_kwargs.get("norm_obs", True):
225
+ envs = NormalizeObservation(
226
+ envs, training=training, clip=normalize_kwargs.get("clip_obs", 10.0)
227
+ )
228
+ if training and normalize_kwargs.get("norm_reward", True):
229
+ envs = NormalizeReward(
230
+ envs,
231
+ training=training,
232
+ clip=normalize_kwargs.get("clip_reward", 10.0),
233
+ )
234
+ else:
235
+ raise ValueError(
236
+ f"normalize_type {normalize_type} not supported (sb3 or gymlike)"
237
+ )
238
+ return envs
239
+
240
+
241
+ def _make_procgen_env(
242
+ config: Config,
243
+ hparams: EnvHyperparams,
244
+ training: bool = True,
245
+ render: bool = False,
246
+ normalize_load_path: Optional[str] = None,
247
+ tb_writer: Optional[SummaryWriter] = None,
248
+ ) -> VecEnv:
249
+ from gym3 import ViewerWrapper, ExtractDictObWrapper
250
+ from procgen.env import ProcgenGym3Env, ToBaselinesVecEnv
251
+
252
+ (
253
+ _, # env_type
254
+ n_envs,
255
+ _, # frame_stack
256
+ make_kwargs,
257
+ _, # no_reward_timeout_steps
258
+ _, # no_reward_fire_steps
259
+ _, # vec_env_class
260
+ normalize,
261
+ normalize_kwargs,
262
+ rolling_length,
263
+ _, # train_record_video
264
+ _, # video_step_interval
265
+ _, # initial_steps_to_truncate
266
+ _, # clip_atari_rewards
267
+ _, # normalize_type
268
+ _, # mask_actions
269
+ ) = astuple(hparams)
270
+
271
+ seed = config.seed(training=training)
272
+
273
+ make_kwargs = make_kwargs or {}
274
+ make_kwargs["render_mode"] = "rgb_array"
275
+ if seed is not None:
276
+ make_kwargs["rand_seed"] = seed
277
+
278
+ envs = ProcgenGym3Env(n_envs, config.env_id, **make_kwargs)
279
+ envs = ExtractDictObWrapper(envs, key="rgb")
280
+ if render:
281
+ envs = ViewerWrapper(envs, info_key="rgb")
282
+ envs = ToBaselinesVecEnv(envs)
283
+ envs = IsVectorEnv(envs)
284
+ # TODO: Handle Grayscale and/or FrameStack
285
+ envs = HwcToChwObservation(envs)
286
+
287
+ envs = gym.wrappers.RecordEpisodeStatistics(envs)
288
+
289
+ if seed is not None:
290
+ envs.action_space.seed(seed)
291
+ envs.observation_space.seed(seed)
292
+
293
+ if training:
294
+ assert tb_writer
295
+ envs = EpisodeStatsWriter(
296
+ envs, tb_writer, training=training, rolling_length=rolling_length
297
+ )
298
+ if normalize and training:
299
+ normalize_kwargs = normalize_kwargs or {}
300
+ envs = gym.wrappers.NormalizeReward(envs)
301
+ clip_obs = normalize_kwargs.get("clip_reward", 10.0)
302
+ envs = gym.wrappers.TransformReward(
303
+ envs, lambda r: np.clip(r, -clip_obs, clip_obs)
304
+ )
305
+
306
+ return envs # type: ignore
307
+
308
+
309
+ def import_for_env_id(env_id: str) -> None:
310
+ if "BulletEnv" in env_id:
311
+ import pybullet_envs
312
+ if "Microrts" in env_id:
313
+ import gym_microrts
314
+
315
+
316
+ def is_atari(config: Config) -> bool:
317
+ spec = gym.spec(config.env_id)
318
+ return "AtariEnv" in str(spec.entry_point)
319
+
320
+
321
+ def is_bullet_env(config: Config) -> bool:
322
+ return "BulletEnv" in config.env_id
323
+
324
+
325
+ def is_car_racing(config: Config) -> bool:
326
+ return "CarRacing" in config.env_id
327
+
328
+
329
+ def is_gym_procgen(config: Config) -> bool:
330
+ return "procgen" in config.env_id
331
+
332
+
333
+ def is_microrts(config: Config) -> bool:
334
+ return "Microrts" in config.env_id
rl_algo_impls/runner/evaluate.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import shutil
3
+
4
+ from dataclasses import dataclass
5
+ from typing import NamedTuple, Optional
6
+
7
+ from rl_algo_impls.runner.env import make_eval_env
8
+ from rl_algo_impls.runner.config import Config, EnvHyperparams, Hyperparams, RunArgs
9
+ from rl_algo_impls.runner.running_utils import (
10
+ load_hyperparams,
11
+ set_seeds,
12
+ get_device,
13
+ make_policy,
14
+ )
15
+ from rl_algo_impls.shared.callbacks.eval_callback import evaluate
16
+ from rl_algo_impls.shared.policy.policy import Policy
17
+ from rl_algo_impls.shared.stats import EpisodesStats
18
+
19
+
20
+ @dataclass
21
+ class EvalArgs(RunArgs):
22
+ render: bool = True
23
+ best: bool = True
24
+ n_envs: Optional[int] = 1
25
+ n_episodes: int = 3
26
+ deterministic_eval: Optional[bool] = None
27
+ no_print_returns: bool = False
28
+ wandb_run_path: Optional[str] = None
29
+
30
+
31
+ class Evaluation(NamedTuple):
32
+ policy: Policy
33
+ stats: EpisodesStats
34
+ config: Config
35
+
36
+
37
+ def evaluate_model(args: EvalArgs, root_dir: str) -> Evaluation:
38
+ if args.wandb_run_path:
39
+ import wandb
40
+
41
+ api = wandb.Api()
42
+ run = api.run(args.wandb_run_path)
43
+ params = run.config
44
+
45
+ args.algo = params["algo"]
46
+ args.env = params["env"]
47
+ args.seed = params.get("seed", None)
48
+ args.use_deterministic_algorithms = params.get(
49
+ "use_deterministic_algorithms", True
50
+ )
51
+
52
+ config = Config(args, Hyperparams.from_dict_with_extra_fields(params), root_dir)
53
+ model_path = config.model_dir_path(best=args.best, downloaded=True)
54
+
55
+ model_archive_name = config.model_dir_name(best=args.best, extension=".zip")
56
+ run.file(model_archive_name).download()
57
+ if os.path.isdir(model_path):
58
+ shutil.rmtree(model_path)
59
+ shutil.unpack_archive(model_archive_name, model_path)
60
+ os.remove(model_archive_name)
61
+ else:
62
+ hyperparams = load_hyperparams(args.algo, args.env)
63
+
64
+ config = Config(args, hyperparams, root_dir)
65
+ model_path = config.model_dir_path(best=args.best)
66
+
67
+ print(args)
68
+
69
+ set_seeds(args.seed, args.use_deterministic_algorithms)
70
+
71
+ env = make_eval_env(
72
+ config,
73
+ EnvHyperparams(**config.env_hyperparams),
74
+ override_n_envs=args.n_envs,
75
+ render=args.render,
76
+ normalize_load_path=model_path,
77
+ )
78
+ device = get_device(config, env)
79
+ policy = make_policy(
80
+ args.algo,
81
+ env,
82
+ device,
83
+ load_path=model_path,
84
+ **config.policy_hyperparams,
85
+ ).eval()
86
+
87
+ deterministic = (
88
+ args.deterministic_eval
89
+ if args.deterministic_eval is not None
90
+ else config.eval_params.get("deterministic", True)
91
+ )
92
+ return Evaluation(
93
+ policy,
94
+ evaluate(
95
+ env,
96
+ policy,
97
+ args.n_episodes,
98
+ render=args.render,
99
+ deterministic=deterministic,
100
+ print_returns=not args.no_print_returns,
101
+ ),
102
+ config,
103
+ )
rl_algo_impls/runner/running_utils.py ADDED
@@ -0,0 +1,198 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import gym
3
+ import json
4
+ import matplotlib.pyplot as plt
5
+ import numpy as np
6
+ import os
7
+ import random
8
+ import torch
9
+ import torch.backends.cudnn
10
+ import yaml
11
+
12
+ from dataclasses import asdict
13
+ from gym.spaces import Box, Discrete
14
+ from pathlib import Path
15
+ from torch.utils.tensorboard.writer import SummaryWriter
16
+ from typing import Dict, Optional, Type, Union
17
+
18
+ from rl_algo_impls.runner.config import Config, Hyperparams
19
+ from rl_algo_impls.runner.env import import_for_env_id, is_microrts
20
+ from rl_algo_impls.shared.algorithm import Algorithm
21
+ from rl_algo_impls.shared.callbacks.eval_callback import EvalCallback
22
+ from rl_algo_impls.shared.policy.on_policy import ActorCritic
23
+ from rl_algo_impls.shared.policy.policy import Policy
24
+
25
+ from rl_algo_impls.a2c.a2c import A2C
26
+ from rl_algo_impls.dqn.dqn import DQN
27
+ from rl_algo_impls.dqn.policy import DQNPolicy
28
+ from rl_algo_impls.ppo.ppo import PPO
29
+ from rl_algo_impls.vpg.vpg import VanillaPolicyGradient
30
+ from rl_algo_impls.vpg.policy import VPGActorCritic
31
+ from rl_algo_impls.wrappers.vectorable_wrapper import VecEnv, single_observation_space
32
+
33
+ ALGOS: Dict[str, Type[Algorithm]] = {
34
+ "dqn": DQN,
35
+ "vpg": VanillaPolicyGradient,
36
+ "ppo": PPO,
37
+ "a2c": A2C,
38
+ }
39
+ POLICIES: Dict[str, Type[Policy]] = {
40
+ "dqn": DQNPolicy,
41
+ "vpg": VPGActorCritic,
42
+ "ppo": ActorCritic,
43
+ "a2c": ActorCritic,
44
+ }
45
+
46
+ HYPERPARAMS_PATH = "hyperparams"
47
+
48
+
49
+ def base_parser(multiple: bool = True) -> argparse.ArgumentParser:
50
+ parser = argparse.ArgumentParser()
51
+ parser.add_argument(
52
+ "--algo",
53
+ default=["dqn"],
54
+ type=str,
55
+ choices=list(ALGOS.keys()),
56
+ nargs="+" if multiple else 1,
57
+ help="Abbreviation(s) of algorithm(s)",
58
+ )
59
+ parser.add_argument(
60
+ "--env",
61
+ default=["CartPole-v1"],
62
+ type=str,
63
+ nargs="+" if multiple else 1,
64
+ help="Name of environment(s) in gym",
65
+ )
66
+ parser.add_argument(
67
+ "--seed",
68
+ default=[1],
69
+ type=int,
70
+ nargs="*" if multiple else "?",
71
+ help="Seeds to run experiment. Unset will do one run with no set seed",
72
+ )
73
+ return parser
74
+
75
+
76
+ def load_hyperparams(algo: str, env_id: str) -> Hyperparams:
77
+ root_path = Path(__file__).parent.parent
78
+ hyperparams_path = os.path.join(root_path, HYPERPARAMS_PATH, f"{algo}.yml")
79
+ with open(hyperparams_path, "r") as f:
80
+ hyperparams_dict = yaml.safe_load(f)
81
+
82
+ if env_id in hyperparams_dict:
83
+ return Hyperparams(**hyperparams_dict[env_id])
84
+
85
+ import_for_env_id(env_id)
86
+ spec = gym.spec(env_id)
87
+ entry_point_name = str(spec.entry_point) # type: ignore
88
+ if "AtariEnv" in entry_point_name and "_atari" in hyperparams_dict:
89
+ return Hyperparams(**hyperparams_dict["_atari"])
90
+ elif "gym_microrts" in entry_point_name and "_microrts" in hyperparams_dict:
91
+ return Hyperparams(**hyperparams_dict["_microrts"])
92
+ else:
93
+ raise ValueError(f"{env_id} not specified in {algo} hyperparameters file")
94
+
95
+
96
+ def get_device(config: Config, env: VecEnv) -> torch.device:
97
+ device = config.device
98
+ # cuda by default
99
+ if device == "auto":
100
+ device = "cuda"
101
+ # Apple MPS is a second choice (sometimes)
102
+ if device == "cuda" and not torch.cuda.is_available():
103
+ device = "mps"
104
+ # If no MPS, fallback to cpu
105
+ if device == "mps" and not torch.backends.mps.is_available():
106
+ device = "cpu"
107
+ # Simple environments like Discreet and 1-D Boxes might also be better
108
+ # served with the CPU.
109
+ if device == "mps":
110
+ obs_space = single_observation_space(env)
111
+ if isinstance(obs_space, Discrete):
112
+ device = "cpu"
113
+ elif isinstance(obs_space, Box) and len(obs_space.shape) == 1:
114
+ device = "cpu"
115
+ if is_microrts(config):
116
+ device = "cpu"
117
+ print(f"Device: {device}")
118
+ return torch.device(device)
119
+
120
+
121
+ def set_seeds(seed: Optional[int], use_deterministic_algorithms: bool) -> None:
122
+ if seed is None:
123
+ return
124
+ random.seed(seed)
125
+ np.random.seed(seed)
126
+ torch.manual_seed(seed)
127
+ torch.backends.cudnn.benchmark = False
128
+ torch.use_deterministic_algorithms(use_deterministic_algorithms)
129
+ os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":4096:8"
130
+ # Stop warning and it would introduce stochasticity if I was using TF
131
+ os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0"
132
+
133
+
134
+ def make_policy(
135
+ algo: str,
136
+ env: VecEnv,
137
+ device: torch.device,
138
+ load_path: Optional[str] = None,
139
+ **kwargs,
140
+ ) -> Policy:
141
+ policy = POLICIES[algo](env, **kwargs).to(device)
142
+ if load_path:
143
+ policy.load(load_path)
144
+ return policy
145
+
146
+
147
+ def plot_eval_callback(callback: EvalCallback, tb_writer: SummaryWriter, run_name: str):
148
+ figure = plt.figure()
149
+ cumulative_steps = [
150
+ (idx + 1) * callback.step_freq for idx in range(len(callback.stats))
151
+ ]
152
+ plt.plot(
153
+ cumulative_steps,
154
+ [s.score.mean for s in callback.stats],
155
+ "b-",
156
+ label="mean",
157
+ )
158
+ plt.plot(
159
+ cumulative_steps,
160
+ [s.score.mean - s.score.std for s in callback.stats],
161
+ "g--",
162
+ label="mean-std",
163
+ )
164
+ plt.fill_between(
165
+ cumulative_steps,
166
+ [s.score.min for s in callback.stats], # type: ignore
167
+ [s.score.max for s in callback.stats], # type: ignore
168
+ facecolor="cyan",
169
+ label="range",
170
+ )
171
+ plt.xlabel("Steps")
172
+ plt.ylabel("Score")
173
+ plt.legend()
174
+ plt.title(f"Eval {run_name}")
175
+ tb_writer.add_figure("eval", figure)
176
+
177
+
178
+ Scalar = Union[bool, str, float, int, None]
179
+
180
+
181
+ def hparam_dict(
182
+ hyperparams: Hyperparams, args: Dict[str, Union[Scalar, list]]
183
+ ) -> Dict[str, Scalar]:
184
+ flattened = args.copy()
185
+ for k, v in flattened.items():
186
+ if isinstance(v, list):
187
+ flattened[k] = json.dumps(v)
188
+ for k, v in asdict(hyperparams).items():
189
+ if isinstance(v, dict):
190
+ for sk, sv in v.items():
191
+ key = f"{k}/{sk}"
192
+ if isinstance(sv, dict) or isinstance(sv, list):
193
+ flattened[key] = str(sv)
194
+ else:
195
+ flattened[key] = sv
196
+ else:
197
+ flattened[k] = v # type: ignore
198
+ return flattened # type: ignore
rl_algo_impls/runner/train.py ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Support for PyTorch mps mode (https://pytorch.org/docs/stable/notes/mps.html)
2
+ import os
3
+
4
+ os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
5
+
6
+ import dataclasses
7
+ import shutil
8
+ import wandb
9
+ import yaml
10
+
11
+ from dataclasses import asdict, dataclass
12
+ from torch.utils.tensorboard.writer import SummaryWriter
13
+ from typing import Any, Dict, Optional, Sequence
14
+
15
+ from rl_algo_impls.shared.callbacks.eval_callback import EvalCallback
16
+ from rl_algo_impls.runner.config import Config, EnvHyperparams, RunArgs
17
+ from rl_algo_impls.runner.env import make_env, make_eval_env
18
+ from rl_algo_impls.runner.running_utils import (
19
+ ALGOS,
20
+ load_hyperparams,
21
+ set_seeds,
22
+ get_device,
23
+ make_policy,
24
+ plot_eval_callback,
25
+ hparam_dict,
26
+ )
27
+ from rl_algo_impls.shared.stats import EpisodesStats
28
+
29
+
30
+ @dataclass
31
+ class TrainArgs(RunArgs):
32
+ wandb_project_name: Optional[str] = None
33
+ wandb_entity: Optional[str] = None
34
+ wandb_tags: Sequence[str] = dataclasses.field(default_factory=list)
35
+ wandb_group: Optional[str] = None
36
+
37
+
38
+ def train(args: TrainArgs):
39
+ print(args)
40
+ hyperparams = load_hyperparams(args.algo, args.env)
41
+ print(hyperparams)
42
+ config = Config(args, hyperparams, os.getcwd())
43
+
44
+ wandb_enabled = args.wandb_project_name
45
+ if wandb_enabled:
46
+ wandb.tensorboard.patch(
47
+ root_logdir=config.tensorboard_summary_path, pytorch=True
48
+ )
49
+ wandb.init(
50
+ project=args.wandb_project_name,
51
+ entity=args.wandb_entity,
52
+ config=asdict(hyperparams),
53
+ name=config.run_name(),
54
+ monitor_gym=True,
55
+ save_code=True,
56
+ tags=args.wandb_tags,
57
+ group=args.wandb_group,
58
+ )
59
+ wandb.config.update(args)
60
+
61
+ tb_writer = SummaryWriter(config.tensorboard_summary_path)
62
+
63
+ set_seeds(args.seed, args.use_deterministic_algorithms)
64
+
65
+ env = make_env(
66
+ config, EnvHyperparams(**config.env_hyperparams), tb_writer=tb_writer
67
+ )
68
+ device = get_device(config, env)
69
+ policy = make_policy(args.algo, env, device, **config.policy_hyperparams)
70
+ algo = ALGOS[args.algo](policy, env, device, tb_writer, **config.algo_hyperparams)
71
+
72
+ num_parameters = policy.num_parameters()
73
+ num_trainable_parameters = policy.num_trainable_parameters()
74
+ if wandb_enabled:
75
+ wandb.run.summary["num_parameters"] = num_parameters
76
+ wandb.run.summary["num_trainable_parameters"] = num_trainable_parameters
77
+ else:
78
+ print(
79
+ f"num_parameters = {num_parameters} ; "
80
+ f"num_trainable_parameters = {num_trainable_parameters}"
81
+ )
82
+
83
+ eval_env = make_eval_env(config, EnvHyperparams(**config.env_hyperparams))
84
+ record_best_videos = config.eval_params.get("record_best_videos", True)
85
+ callback = EvalCallback(
86
+ policy,
87
+ eval_env,
88
+ tb_writer,
89
+ best_model_path=config.model_dir_path(best=True),
90
+ **config.eval_params,
91
+ video_env=make_eval_env(
92
+ config, EnvHyperparams(**config.env_hyperparams), override_n_envs=1
93
+ )
94
+ if record_best_videos
95
+ else None,
96
+ best_video_dir=config.best_videos_dir,
97
+ )
98
+ algo.learn(config.n_timesteps, callback=callback)
99
+
100
+ policy.save(config.model_dir_path(best=False))
101
+
102
+ eval_stats = callback.evaluate(n_episodes=10, print_returns=True)
103
+
104
+ plot_eval_callback(callback, tb_writer, config.run_name())
105
+
106
+ log_dict: Dict[str, Any] = {
107
+ "eval": eval_stats._asdict(),
108
+ }
109
+ if callback.best:
110
+ log_dict["best_eval"] = callback.best._asdict()
111
+ log_dict.update(asdict(hyperparams))
112
+ log_dict.update(vars(args))
113
+ with open(config.logs_path, "a") as f:
114
+ yaml.dump({config.run_name(): log_dict}, f)
115
+
116
+ best_eval_stats: EpisodesStats = callback.best # type: ignore
117
+ tb_writer.add_hparams(
118
+ hparam_dict(hyperparams, vars(args)),
119
+ {
120
+ "hparam/best_mean": best_eval_stats.score.mean,
121
+ "hparam/best_result": best_eval_stats.score.mean
122
+ - best_eval_stats.score.std,
123
+ "hparam/last_mean": eval_stats.score.mean,
124
+ "hparam/last_result": eval_stats.score.mean - eval_stats.score.std,
125
+ },
126
+ None,
127
+ config.run_name(),
128
+ )
129
+
130
+ tb_writer.close()
131
+
132
+ if wandb_enabled:
133
+ shutil.make_archive(
134
+ os.path.join(wandb.run.dir, config.model_dir_name()),
135
+ "zip",
136
+ config.model_dir_path(),
137
+ )
138
+ shutil.make_archive(
139
+ os.path.join(wandb.run.dir, config.model_dir_name(best=True)),
140
+ "zip",
141
+ config.model_dir_path(best=True),
142
+ )
143
+ wandb.finish()
rl_algo_impls/shared/algorithm.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gym
2
+ import torch
3
+
4
+ from abc import ABC, abstractmethod
5
+ from torch.utils.tensorboard.writer import SummaryWriter
6
+ from typing import Optional, TypeVar
7
+
8
+ from rl_algo_impls.shared.callbacks.callback import Callback
9
+ from rl_algo_impls.shared.policy.policy import Policy
10
+ from rl_algo_impls.wrappers.vectorable_wrapper import VecEnv
11
+
12
+ AlgorithmSelf = TypeVar("AlgorithmSelf", bound="Algorithm")
13
+
14
+
15
+ class Algorithm(ABC):
16
+ @abstractmethod
17
+ def __init__(
18
+ self,
19
+ policy: Policy,
20
+ env: VecEnv,
21
+ device: torch.device,
22
+ tb_writer: SummaryWriter,
23
+ **kwargs,
24
+ ) -> None:
25
+ super().__init__()
26
+ self.policy = policy
27
+ self.env = env
28
+ self.device = device
29
+ self.tb_writer = tb_writer
30
+
31
+ @abstractmethod
32
+ def learn(
33
+ self: AlgorithmSelf,
34
+ train_timesteps: int,
35
+ callback: Optional[Callback] = None,
36
+ total_timesteps: Optional[int] = None,
37
+ start_timesteps: int = 0,
38
+ ) -> AlgorithmSelf:
39
+ ...
rl_algo_impls/shared/callbacks/callback.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from abc import ABC
2
+
3
+
4
+ class Callback(ABC):
5
+ def __init__(self) -> None:
6
+ super().__init__()
7
+ self.timesteps_elapsed = 0
8
+
9
+ def on_step(self, timesteps_elapsed: int = 1) -> bool:
10
+ self.timesteps_elapsed += timesteps_elapsed
11
+ return True
rl_algo_impls/shared/callbacks/eval_callback.py ADDED
@@ -0,0 +1,205 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import itertools
2
+ import numpy as np
3
+ import os
4
+
5
+ from time import perf_counter
6
+ from torch.utils.tensorboard.writer import SummaryWriter
7
+ from typing import List, Optional, Union
8
+
9
+ from rl_algo_impls.shared.callbacks.callback import Callback
10
+ from rl_algo_impls.shared.policy.policy import Policy
11
+ from rl_algo_impls.shared.stats import Episode, EpisodeAccumulator, EpisodesStats
12
+ from rl_algo_impls.wrappers.action_mask_wrapper import ActionMaskWrapper
13
+ from rl_algo_impls.wrappers.vec_episode_recorder import VecEpisodeRecorder
14
+ from rl_algo_impls.wrappers.vectorable_wrapper import VecEnv, find_wrapper
15
+
16
+
17
+ class EvaluateAccumulator(EpisodeAccumulator):
18
+ def __init__(
19
+ self,
20
+ num_envs: int,
21
+ goal_episodes: int,
22
+ print_returns: bool = True,
23
+ ignore_first_episode: bool = False,
24
+ ):
25
+ super().__init__(num_envs)
26
+ self.completed_episodes_by_env_idx = [[] for _ in range(num_envs)]
27
+ self.goal_episodes_per_env = int(np.ceil(goal_episodes / num_envs))
28
+ self.print_returns = print_returns
29
+ if ignore_first_episode:
30
+ first_done = set()
31
+
32
+ def should_record_done(idx: int) -> bool:
33
+ has_done_first_episode = idx in first_done
34
+ first_done.add(idx)
35
+ return has_done_first_episode
36
+
37
+ self.should_record_done = should_record_done
38
+ else:
39
+ self.should_record_done = lambda idx: True
40
+
41
+ def on_done(self, ep_idx: int, episode: Episode) -> None:
42
+ if (
43
+ self.should_record_done(ep_idx)
44
+ and len(self.completed_episodes_by_env_idx[ep_idx])
45
+ >= self.goal_episodes_per_env
46
+ ):
47
+ return
48
+ self.completed_episodes_by_env_idx[ep_idx].append(episode)
49
+ if self.print_returns:
50
+ print(
51
+ f"Episode {len(self)} | "
52
+ f"Score {episode.score} | "
53
+ f"Length {episode.length}"
54
+ )
55
+
56
+ def __len__(self) -> int:
57
+ return sum(len(ce) for ce in self.completed_episodes_by_env_idx)
58
+
59
+ @property
60
+ def episodes(self) -> List[Episode]:
61
+ return list(itertools.chain(*self.completed_episodes_by_env_idx))
62
+
63
+ def is_done(self) -> bool:
64
+ return all(
65
+ len(ce) == self.goal_episodes_per_env
66
+ for ce in self.completed_episodes_by_env_idx
67
+ )
68
+
69
+
70
+ def evaluate(
71
+ env: VecEnv,
72
+ policy: Policy,
73
+ n_episodes: int,
74
+ render: bool = False,
75
+ deterministic: bool = True,
76
+ print_returns: bool = True,
77
+ ignore_first_episode: bool = False,
78
+ ) -> EpisodesStats:
79
+ policy.sync_normalization(env)
80
+ policy.eval()
81
+
82
+ episodes = EvaluateAccumulator(
83
+ env.num_envs, n_episodes, print_returns, ignore_first_episode
84
+ )
85
+
86
+ obs = env.reset()
87
+ action_masker = find_wrapper(env, ActionMaskWrapper)
88
+ while not episodes.is_done():
89
+ act = policy.act(
90
+ obs,
91
+ deterministic=deterministic,
92
+ action_masks=action_masker.action_masks() if action_masker else None,
93
+ )
94
+ obs, rew, done, _ = env.step(act)
95
+ episodes.step(rew, done)
96
+ if render:
97
+ env.render()
98
+ stats = EpisodesStats(episodes.episodes)
99
+ if print_returns:
100
+ print(stats)
101
+ return stats
102
+
103
+
104
+ class EvalCallback(Callback):
105
+ def __init__(
106
+ self,
107
+ policy: Policy,
108
+ env: VecEnv,
109
+ tb_writer: SummaryWriter,
110
+ best_model_path: Optional[str] = None,
111
+ step_freq: Union[int, float] = 50_000,
112
+ n_episodes: int = 10,
113
+ save_best: bool = True,
114
+ deterministic: bool = True,
115
+ record_best_videos: bool = True,
116
+ video_env: Optional[VecEnv] = None,
117
+ best_video_dir: Optional[str] = None,
118
+ max_video_length: int = 3600,
119
+ ignore_first_episode: bool = False,
120
+ ) -> None:
121
+ super().__init__()
122
+ self.policy = policy
123
+ self.env = env
124
+ self.tb_writer = tb_writer
125
+ self.best_model_path = best_model_path
126
+ self.step_freq = int(step_freq)
127
+ self.n_episodes = n_episodes
128
+ self.save_best = save_best
129
+ self.deterministic = deterministic
130
+ self.stats: List[EpisodesStats] = []
131
+ self.best = None
132
+
133
+ self.record_best_videos = record_best_videos
134
+ assert video_env or not record_best_videos
135
+ self.video_env = video_env
136
+ assert best_video_dir or not record_best_videos
137
+ self.best_video_dir = best_video_dir
138
+ if best_video_dir:
139
+ os.makedirs(best_video_dir, exist_ok=True)
140
+ self.max_video_length = max_video_length
141
+ self.best_video_base_path = None
142
+
143
+ self.ignore_first_episode = ignore_first_episode
144
+
145
+ def on_step(self, timesteps_elapsed: int = 1) -> bool:
146
+ super().on_step(timesteps_elapsed)
147
+ if self.timesteps_elapsed // self.step_freq >= len(self.stats):
148
+ self.evaluate()
149
+ return True
150
+
151
+ def evaluate(
152
+ self, n_episodes: Optional[int] = None, print_returns: Optional[bool] = None
153
+ ) -> EpisodesStats:
154
+ start_time = perf_counter()
155
+ eval_stat = evaluate(
156
+ self.env,
157
+ self.policy,
158
+ n_episodes or self.n_episodes,
159
+ deterministic=self.deterministic,
160
+ print_returns=print_returns or False,
161
+ ignore_first_episode=self.ignore_first_episode,
162
+ )
163
+ end_time = perf_counter()
164
+ self.tb_writer.add_scalar(
165
+ "eval/steps_per_second",
166
+ eval_stat.length.sum() / (end_time - start_time),
167
+ self.timesteps_elapsed,
168
+ )
169
+ self.policy.train(True)
170
+ print(f"Eval Timesteps: {self.timesteps_elapsed} | {eval_stat}")
171
+
172
+ self.stats.append(eval_stat)
173
+
174
+ if not self.best or eval_stat >= self.best:
175
+ strictly_better = not self.best or eval_stat > self.best
176
+ self.best = eval_stat
177
+ if self.save_best:
178
+ assert self.best_model_path
179
+ self.policy.save(self.best_model_path)
180
+ print("Saved best model")
181
+ self.best.write_to_tensorboard(
182
+ self.tb_writer, "best_eval", self.timesteps_elapsed
183
+ )
184
+ if strictly_better and self.record_best_videos:
185
+ assert self.video_env and self.best_video_dir
186
+ self.best_video_base_path = os.path.join(
187
+ self.best_video_dir, str(self.timesteps_elapsed)
188
+ )
189
+ video_wrapped = VecEpisodeRecorder(
190
+ self.video_env,
191
+ self.best_video_base_path,
192
+ max_video_length=self.max_video_length,
193
+ )
194
+ video_stats = evaluate(
195
+ video_wrapped,
196
+ self.policy,
197
+ 1,
198
+ deterministic=self.deterministic,
199
+ print_returns=False,
200
+ )
201
+ print(f"Saved best video: {video_stats}")
202
+
203
+ eval_stat.write_to_tensorboard(self.tb_writer, "eval", self.timesteps_elapsed)
204
+
205
+ return eval_stat
rl_algo_impls/shared/callbacks/optimize_callback.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import optuna
3
+
4
+ from time import perf_counter
5
+ from torch.utils.tensorboard.writer import SummaryWriter
6
+ from typing import NamedTuple, Union
7
+
8
+ from rl_algo_impls.shared.callbacks.callback import Callback
9
+ from rl_algo_impls.shared.callbacks.eval_callback import evaluate
10
+ from rl_algo_impls.shared.policy.policy import Policy
11
+ from rl_algo_impls.shared.stats import EpisodesStats
12
+ from rl_algo_impls.wrappers.episode_stats_writer import EpisodeStatsWriter
13
+ from rl_algo_impls.wrappers.vectorable_wrapper import VecEnv, find_wrapper
14
+
15
+
16
+ class Evaluation(NamedTuple):
17
+ eval_stat: EpisodesStats
18
+ train_stat: EpisodesStats
19
+ score: float
20
+
21
+
22
+ class OptimizeCallback(Callback):
23
+ def __init__(
24
+ self,
25
+ policy: Policy,
26
+ env: VecEnv,
27
+ trial: optuna.Trial,
28
+ tb_writer: SummaryWriter,
29
+ step_freq: Union[int, float] = 50_000,
30
+ n_episodes: int = 10,
31
+ deterministic: bool = True,
32
+ ) -> None:
33
+ super().__init__()
34
+ self.policy = policy
35
+ self.env = env
36
+ self.trial = trial
37
+ self.tb_writer = tb_writer
38
+ self.step_freq = step_freq
39
+ self.n_episodes = n_episodes
40
+ self.deterministic = deterministic
41
+
42
+ stats_writer = find_wrapper(policy.env, EpisodeStatsWriter)
43
+ assert stats_writer
44
+ self.stats_writer = stats_writer
45
+
46
+ self.eval_step = 1
47
+ self.is_pruned = False
48
+ self.last_eval_stat = None
49
+ self.last_train_stat = None
50
+ self.last_score = -np.inf
51
+
52
+ def on_step(self, timesteps_elapsed: int = 1) -> bool:
53
+ super().on_step(timesteps_elapsed)
54
+ if self.timesteps_elapsed >= self.eval_step * self.step_freq:
55
+ self.evaluate()
56
+ return not self.is_pruned
57
+ return True
58
+
59
+ def evaluate(self) -> None:
60
+ self.last_eval_stat, self.last_train_stat, score = evaluation(
61
+ self.policy,
62
+ self.env,
63
+ self.tb_writer,
64
+ self.n_episodes,
65
+ self.deterministic,
66
+ self.timesteps_elapsed,
67
+ )
68
+ self.last_score = score
69
+
70
+ self.trial.report(score, self.eval_step)
71
+ if self.trial.should_prune():
72
+ self.is_pruned = True
73
+
74
+ self.eval_step += 1
75
+
76
+
77
+ def evaluation(
78
+ policy: Policy,
79
+ env: VecEnv,
80
+ tb_writer: SummaryWriter,
81
+ n_episodes: int,
82
+ deterministic: bool,
83
+ timesteps_elapsed: int,
84
+ ) -> Evaluation:
85
+ start_time = perf_counter()
86
+ eval_stat = evaluate(
87
+ env,
88
+ policy,
89
+ n_episodes,
90
+ deterministic=deterministic,
91
+ print_returns=False,
92
+ )
93
+ end_time = perf_counter()
94
+ tb_writer.add_scalar(
95
+ "eval/steps_per_second",
96
+ eval_stat.length.sum() / (end_time - start_time),
97
+ timesteps_elapsed,
98
+ )
99
+ policy.train()
100
+ print(f"Eval Timesteps: {timesteps_elapsed} | {eval_stat}")
101
+ eval_stat.write_to_tensorboard(tb_writer, "eval", timesteps_elapsed)
102
+
103
+ stats_writer = find_wrapper(policy.env, EpisodeStatsWriter)
104
+ assert stats_writer
105
+
106
+ train_stat = EpisodesStats(stats_writer.episodes)
107
+ print(f" Train Stat: {train_stat}")
108
+
109
+ score = (eval_stat.score.mean + train_stat.score.mean) / 2
110
+ print(f" Score: {round(score, 2)}")
111
+ tb_writer.add_scalar(
112
+ "eval/score",
113
+ score,
114
+ timesteps_elapsed,
115
+ )
116
+
117
+ return Evaluation(eval_stat, train_stat, score)
rl_algo_impls/shared/gae.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import torch
3
+
4
+ from typing import NamedTuple, Sequence
5
+
6
+ from rl_algo_impls.shared.policy.on_policy import OnPolicy
7
+ from rl_algo_impls.shared.trajectory import Trajectory
8
+ from rl_algo_impls.wrappers.vectorable_wrapper import VecEnvObs
9
+
10
+
11
+ class RtgAdvantage(NamedTuple):
12
+ rewards_to_go: torch.Tensor
13
+ advantage: torch.Tensor
14
+
15
+
16
+ def discounted_cumsum(x: np.ndarray, gamma: float) -> np.ndarray:
17
+ dc = x.copy()
18
+ for i in reversed(range(len(x) - 1)):
19
+ dc[i] += gamma * dc[i + 1]
20
+ return dc
21
+
22
+
23
+ def compute_advantage_from_trajectories(
24
+ trajectories: Sequence[Trajectory],
25
+ policy: OnPolicy,
26
+ gamma: float,
27
+ gae_lambda: float,
28
+ device: torch.device,
29
+ ) -> torch.Tensor:
30
+ advantage = []
31
+ for traj in trajectories:
32
+ last_val = 0
33
+ if not traj.terminated and traj.next_obs is not None:
34
+ last_val = policy.value(traj.next_obs)
35
+ rew = np.append(np.array(traj.rew), last_val)
36
+ v = np.append(np.array(traj.v), last_val)
37
+ deltas = rew[:-1] + gamma * v[1:] - v[:-1]
38
+ advantage.append(discounted_cumsum(deltas, gamma * gae_lambda))
39
+ return torch.as_tensor(
40
+ np.concatenate(advantage), dtype=torch.float32, device=device
41
+ )
42
+
43
+
44
+ def compute_rtg_and_advantage_from_trajectories(
45
+ trajectories: Sequence[Trajectory],
46
+ policy: OnPolicy,
47
+ gamma: float,
48
+ gae_lambda: float,
49
+ device: torch.device,
50
+ ) -> RtgAdvantage:
51
+ rewards_to_go = []
52
+ advantages = []
53
+ for traj in trajectories:
54
+ last_val = 0
55
+ if not traj.terminated and traj.next_obs is not None:
56
+ last_val = policy.value(traj.next_obs)
57
+ rew = np.append(np.array(traj.rew), last_val)
58
+ v = np.append(np.array(traj.v), last_val)
59
+ deltas = rew[:-1] + gamma * v[1:] - v[:-1]
60
+ adv = discounted_cumsum(deltas, gamma * gae_lambda)
61
+ advantages.append(adv)
62
+ rewards_to_go.append(v[:-1] + adv)
63
+ return RtgAdvantage(
64
+ torch.as_tensor(
65
+ np.concatenate(rewards_to_go), dtype=torch.float32, device=device
66
+ ),
67
+ torch.as_tensor(np.concatenate(advantages), dtype=torch.float32, device=device),
68
+ )
69
+
70
+
71
+ def compute_advantages(
72
+ rewards: np.ndarray,
73
+ values: np.ndarray,
74
+ episode_starts: np.ndarray,
75
+ next_episode_starts: np.ndarray,
76
+ next_obs: VecEnvObs,
77
+ policy: OnPolicy,
78
+ gamma: float,
79
+ gae_lambda: float,
80
+ ) -> np.ndarray:
81
+ advantages = np.zeros_like(rewards)
82
+ last_gae_lam = 0
83
+ n_steps = advantages.shape[0]
84
+ for t in reversed(range(n_steps)):
85
+ if t == n_steps - 1:
86
+ next_nonterminal = 1.0 - next_episode_starts
87
+ next_value = policy.value(next_obs)
88
+ else:
89
+ next_nonterminal = 1.0 - episode_starts[t + 1]
90
+ next_value = values[t + 1]
91
+ delta = rewards[t] + gamma * next_value * next_nonterminal - values[t]
92
+ last_gae_lam = delta + gamma * gae_lambda * next_nonterminal * last_gae_lam
93
+ advantages[t] = last_gae_lam
94
+ return advantages
rl_algo_impls/shared/module/feature_extractor.py ADDED
@@ -0,0 +1,249 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gym
2
+ import numpy as np
3
+ import torch
4
+ import torch.nn as nn
5
+ import torch.nn.functional as F
6
+
7
+ from abc import ABC, abstractmethod
8
+ from gym.spaces import Box, Discrete
9
+ from stable_baselines3.common.preprocessing import get_flattened_obs_dim
10
+ from typing import Dict, Optional, Sequence, Type
11
+
12
+ from rl_algo_impls.shared.module.module import layer_init
13
+
14
+
15
+ class CnnFeatureExtractor(nn.Module, ABC):
16
+ @abstractmethod
17
+ def __init__(
18
+ self,
19
+ in_channels: int,
20
+ activation: Type[nn.Module] = nn.ReLU,
21
+ init_layers_orthogonal: Optional[bool] = None,
22
+ **kwargs,
23
+ ) -> None:
24
+ super().__init__()
25
+
26
+
27
+ class NatureCnn(CnnFeatureExtractor):
28
+ """
29
+ CNN from DQN Nature paper: Mnih, Volodymyr, et al.
30
+ "Human-level control through deep reinforcement learning."
31
+ Nature 518.7540 (2015): 529-533.
32
+ """
33
+
34
+ def __init__(
35
+ self,
36
+ in_channels: int,
37
+ activation: Type[nn.Module] = nn.ReLU,
38
+ init_layers_orthogonal: Optional[bool] = None,
39
+ **kwargs,
40
+ ) -> None:
41
+ if init_layers_orthogonal is None:
42
+ init_layers_orthogonal = True
43
+ super().__init__(in_channels, activation, init_layers_orthogonal)
44
+ self.cnn = nn.Sequential(
45
+ layer_init(
46
+ nn.Conv2d(in_channels, 32, kernel_size=8, stride=4),
47
+ init_layers_orthogonal,
48
+ ),
49
+ activation(),
50
+ layer_init(
51
+ nn.Conv2d(32, 64, kernel_size=4, stride=2),
52
+ init_layers_orthogonal,
53
+ ),
54
+ activation(),
55
+ layer_init(
56
+ nn.Conv2d(64, 64, kernel_size=3, stride=1),
57
+ init_layers_orthogonal,
58
+ ),
59
+ activation(),
60
+ nn.Flatten(),
61
+ )
62
+
63
+ def forward(self, obs: torch.Tensor) -> torch.Tensor:
64
+ return self.cnn(obs)
65
+
66
+
67
+ class ResidualBlock(nn.Module):
68
+ def __init__(
69
+ self,
70
+ channels: int,
71
+ activation: Type[nn.Module] = nn.ReLU,
72
+ init_layers_orthogonal: bool = False,
73
+ ) -> None:
74
+ super().__init__()
75
+ self.residual = nn.Sequential(
76
+ activation(),
77
+ layer_init(
78
+ nn.Conv2d(channels, channels, 3, padding=1), init_layers_orthogonal
79
+ ),
80
+ activation(),
81
+ layer_init(
82
+ nn.Conv2d(channels, channels, 3, padding=1), init_layers_orthogonal
83
+ ),
84
+ )
85
+
86
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
87
+ return x + self.residual(x)
88
+
89
+
90
+ class ConvSequence(nn.Module):
91
+ def __init__(
92
+ self,
93
+ in_channels: int,
94
+ out_channels: int,
95
+ activation: Type[nn.Module] = nn.ReLU,
96
+ init_layers_orthogonal: bool = False,
97
+ ) -> None:
98
+ super().__init__()
99
+ self.seq = nn.Sequential(
100
+ layer_init(
101
+ nn.Conv2d(in_channels, out_channels, 3, padding=1),
102
+ init_layers_orthogonal,
103
+ ),
104
+ nn.MaxPool2d(3, stride=2, padding=1),
105
+ ResidualBlock(out_channels, activation, init_layers_orthogonal),
106
+ ResidualBlock(out_channels, activation, init_layers_orthogonal),
107
+ )
108
+
109
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
110
+ return self.seq(x)
111
+
112
+
113
+ class ImpalaCnn(CnnFeatureExtractor):
114
+ """
115
+ IMPALA-style CNN architecture
116
+ """
117
+
118
+ def __init__(
119
+ self,
120
+ in_channels: int,
121
+ activation: Type[nn.Module] = nn.ReLU,
122
+ init_layers_orthogonal: Optional[bool] = None,
123
+ impala_channels: Sequence[int] = (16, 32, 32),
124
+ **kwargs,
125
+ ) -> None:
126
+ if init_layers_orthogonal is None:
127
+ init_layers_orthogonal = False
128
+ super().__init__(in_channels, activation, init_layers_orthogonal)
129
+ sequences = []
130
+ for out_channels in impala_channels:
131
+ sequences.append(
132
+ ConvSequence(
133
+ in_channels, out_channels, activation, init_layers_orthogonal
134
+ )
135
+ )
136
+ in_channels = out_channels
137
+ sequences.extend(
138
+ [
139
+ activation(),
140
+ nn.Flatten(),
141
+ ]
142
+ )
143
+ self.seq = nn.Sequential(*sequences)
144
+
145
+ def forward(self, obs: torch.Tensor) -> torch.Tensor:
146
+ return self.seq(obs)
147
+
148
+
149
+ class MicrortsCnn(CnnFeatureExtractor):
150
+ """
151
+ Base CNN architecture for Gym-MicroRTS
152
+ """
153
+
154
+ def __init__(
155
+ self,
156
+ in_channels: int,
157
+ activation: Type[nn.Module] = nn.ReLU,
158
+ init_layers_orthogonal: Optional[bool] = None,
159
+ **kwargs,
160
+ ) -> None:
161
+ if init_layers_orthogonal is None:
162
+ init_layers_orthogonal = True
163
+ super().__init__(in_channels, activation, init_layers_orthogonal)
164
+ self.cnn = nn.Sequential(
165
+ layer_init(
166
+ nn.Conv2d(in_channels, 16, kernel_size=3, stride=2),
167
+ init_layers_orthogonal,
168
+ ),
169
+ activation(),
170
+ layer_init(nn.Conv2d(16, 32, kernel_size=2), init_layers_orthogonal),
171
+ activation(),
172
+ nn.Flatten(),
173
+ )
174
+
175
+ def forward(self, obs: torch.Tensor) -> torch.Tensor:
176
+ return self.cnn(obs)
177
+
178
+
179
+ CNN_EXTRACTORS_BY_STYLE: Dict[str, Type[CnnFeatureExtractor]] = {
180
+ "nature": NatureCnn,
181
+ "impala": ImpalaCnn,
182
+ "microrts": MicrortsCnn,
183
+ }
184
+
185
+
186
+ class FeatureExtractor(nn.Module):
187
+ def __init__(
188
+ self,
189
+ obs_space: gym.Space,
190
+ activation: Type[nn.Module],
191
+ init_layers_orthogonal: bool = False,
192
+ cnn_feature_dim: int = 512,
193
+ cnn_style: str = "nature",
194
+ cnn_layers_init_orthogonal: Optional[bool] = None,
195
+ impala_channels: Sequence[int] = (16, 32, 32),
196
+ ) -> None:
197
+ super().__init__()
198
+ if isinstance(obs_space, Box):
199
+ # Conv2D: (channels, height, width)
200
+ if len(obs_space.shape) == 3:
201
+ cnn = CNN_EXTRACTORS_BY_STYLE[cnn_style](
202
+ obs_space.shape[0],
203
+ activation,
204
+ init_layers_orthogonal=cnn_layers_init_orthogonal,
205
+ impala_channels=impala_channels,
206
+ )
207
+
208
+ range_size = np.max(obs_space.high) - np.min(obs_space.low)
209
+
210
+ def preprocess(obs: torch.Tensor) -> torch.Tensor:
211
+ if len(obs.shape) == 3:
212
+ obs = obs.unsqueeze(0)
213
+ return obs.float() / range_size
214
+
215
+ with torch.no_grad():
216
+ cnn_out = cnn(preprocess(torch.as_tensor(obs_space.sample())))
217
+ self.preprocess = preprocess
218
+ self.feature_extractor = nn.Sequential(
219
+ cnn,
220
+ layer_init(
221
+ nn.Linear(cnn_out.shape[1], cnn_feature_dim),
222
+ init_layers_orthogonal,
223
+ ),
224
+ activation(),
225
+ )
226
+ self.out_dim = cnn_feature_dim
227
+ elif len(obs_space.shape) == 1:
228
+
229
+ def preprocess(obs: torch.Tensor) -> torch.Tensor:
230
+ if len(obs.shape) == 1:
231
+ obs = obs.unsqueeze(0)
232
+ return obs.float()
233
+
234
+ self.preprocess = preprocess
235
+ self.feature_extractor = nn.Flatten()
236
+ self.out_dim = get_flattened_obs_dim(obs_space)
237
+ else:
238
+ raise ValueError(f"Unsupported observation space: {obs_space}")
239
+ elif isinstance(obs_space, Discrete):
240
+ self.preprocess = lambda x: F.one_hot(x, obs_space.n).float()
241
+ self.feature_extractor = nn.Flatten()
242
+ self.out_dim = obs_space.n
243
+ else:
244
+ raise NotImplementedError
245
+
246
+ def forward(self, obs: torch.Tensor) -> torch.Tensor:
247
+ if self.preprocess:
248
+ obs = self.preprocess(obs)
249
+ return self.feature_extractor(obs)
rl_algo_impls/shared/module/module.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import torch.nn as nn
3
+
4
+ from typing import Sequence, Type
5
+
6
+
7
+ def mlp(
8
+ layer_sizes: Sequence[int],
9
+ activation: Type[nn.Module],
10
+ output_activation: Type[nn.Module] = nn.Identity,
11
+ init_layers_orthogonal: bool = False,
12
+ final_layer_gain: float = np.sqrt(2),
13
+ ) -> nn.Module:
14
+ layers = []
15
+ for i in range(len(layer_sizes) - 2):
16
+ layers.append(
17
+ layer_init(
18
+ nn.Linear(layer_sizes[i], layer_sizes[i + 1]), init_layers_orthogonal
19
+ )
20
+ )
21
+ layers.append(activation())
22
+ layers.append(
23
+ layer_init(
24
+ nn.Linear(layer_sizes[-2], layer_sizes[-1]),
25
+ init_layers_orthogonal,
26
+ std=final_layer_gain,
27
+ )
28
+ )
29
+ layers.append(output_activation())
30
+ return nn.Sequential(*layers)
31
+
32
+
33
+ def layer_init(
34
+ layer: nn.Module, init_layers_orthogonal: bool, std: float = np.sqrt(2)
35
+ ) -> nn.Module:
36
+ if not init_layers_orthogonal:
37
+ return layer
38
+ nn.init.orthogonal_(layer.weight, std) # type: ignore
39
+ nn.init.constant_(layer.bias, 0.0) # type: ignore
40
+ return layer
rl_algo_impls/shared/policy/actor.py ADDED
@@ -0,0 +1,452 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gym
2
+ import numpy as np
3
+ import torch
4
+ import torch.nn as nn
5
+
6
+ from abc import ABC, abstractmethod
7
+ from gym.spaces import Box, Discrete, MultiDiscrete
8
+ from numpy.typing import NDArray
9
+ from torch.distributions import Categorical, Distribution, Normal, constraints
10
+ from typing import Dict, NamedTuple, Optional, Sequence, Type, TypeVar, Union
11
+
12
+ from rl_algo_impls.shared.module.module import mlp
13
+
14
+
15
+ class PiForward(NamedTuple):
16
+ pi: Distribution
17
+ logp_a: Optional[torch.Tensor]
18
+ entropy: Optional[torch.Tensor]
19
+
20
+
21
+ class Actor(nn.Module, ABC):
22
+ @abstractmethod
23
+ def forward(
24
+ self,
25
+ obs: torch.Tensor,
26
+ actions: Optional[torch.Tensor] = None,
27
+ action_masks: Optional[torch.Tensor] = None,
28
+ ) -> PiForward:
29
+ ...
30
+
31
+
32
+ class CategoricalActorHead(Actor):
33
+ def __init__(
34
+ self,
35
+ act_dim: int,
36
+ hidden_sizes: Sequence[int] = (32,),
37
+ activation: Type[nn.Module] = nn.Tanh,
38
+ init_layers_orthogonal: bool = True,
39
+ ) -> None:
40
+ super().__init__()
41
+ layer_sizes = tuple(hidden_sizes) + (act_dim,)
42
+ self._fc = mlp(
43
+ layer_sizes,
44
+ activation,
45
+ init_layers_orthogonal=init_layers_orthogonal,
46
+ final_layer_gain=0.01,
47
+ )
48
+
49
+ def forward(
50
+ self,
51
+ obs: torch.Tensor,
52
+ actions: Optional[torch.Tensor] = None,
53
+ action_masks: Optional[torch.Tensor] = None,
54
+ ) -> PiForward:
55
+ logits = self._fc(obs)
56
+ pi = MaskedCategorical(logits=logits, mask=action_masks)
57
+ logp_a = None
58
+ entropy = None
59
+ if actions is not None:
60
+ logp_a = pi.log_prob(actions)
61
+ entropy = pi.entropy()
62
+ return PiForward(pi, logp_a, entropy)
63
+
64
+
65
+ class MultiCategorical(Distribution):
66
+ def __init__(
67
+ self,
68
+ nvec: NDArray[np.int64],
69
+ probs=None,
70
+ logits=None,
71
+ validate_args=None,
72
+ masks: Optional[torch.Tensor] = None,
73
+ ):
74
+ # Either probs or logits should be set
75
+ assert (probs is None) != (logits is None)
76
+ masks_split = (
77
+ torch.split(masks, nvec.tolist(), dim=1)
78
+ if masks is not None
79
+ else [None] * len(nvec)
80
+ )
81
+ if probs:
82
+ self.dists = [
83
+ MaskedCategorical(probs=p, validate_args=validate_args, mask=m)
84
+ for p, m in zip(torch.split(probs, nvec.tolist(), dim=1), masks_split)
85
+ ]
86
+ param = probs
87
+ else:
88
+ assert logits is not None
89
+ self.dists = [
90
+ MaskedCategorical(logits=lg, validate_args=validate_args, mask=m)
91
+ for lg, m in zip(torch.split(logits, nvec.tolist(), dim=1), masks_split)
92
+ ]
93
+ param = logits
94
+ batch_shape = param.size()[:-1] if param.ndimension() > 1 else torch.Size()
95
+ super().__init__(batch_shape=batch_shape, validate_args=validate_args)
96
+
97
+ def log_prob(self, action: torch.Tensor) -> torch.Tensor:
98
+ prob_stack = torch.stack(
99
+ [c.log_prob(a) for a, c in zip(action.T, self.dists)], dim=-1
100
+ )
101
+ return prob_stack.sum(dim=-1)
102
+
103
+ def entropy(self):
104
+ return torch.stack([c.entropy() for c in self.dists], dim=-1).sum(dim=-1)
105
+
106
+ def sample(self, sample_shape: torch.Size = torch.Size()):
107
+ return torch.stack([c.sample(sample_shape) for c in self.dists], dim=-1)
108
+
109
+ @property
110
+ def arg_constraints(self) -> Dict[str, constraints.Constraint]:
111
+ # Constraints handled by child distributions in dist
112
+ return {}
113
+
114
+
115
+ class MaskedCategorical(Categorical):
116
+ def __init__(
117
+ self,
118
+ probs=None,
119
+ logits=None,
120
+ validate_args=None,
121
+ mask: Optional[torch.Tensor] = None,
122
+ ):
123
+ if mask is not None:
124
+ assert logits is not None, "mask requires logits and not probs"
125
+ logits = torch.where(mask, logits, -1e8)
126
+ self.mask = mask
127
+ super().__init__(probs, logits, validate_args)
128
+
129
+ def entropy(self) -> torch.Tensor:
130
+ if self.mask is None:
131
+ return super().entropy()
132
+ # If mask set, then use approximation for entropy
133
+ p_log_p = self.logits * self.probs
134
+ masked = torch.where(self.mask, p_log_p, 0)
135
+ return -masked.sum(-1)
136
+
137
+
138
+ class MultiDiscreteActorHead(Actor):
139
+ def __init__(
140
+ self,
141
+ nvec: NDArray[np.int64],
142
+ hidden_sizes: Sequence[int] = (32,),
143
+ activation: Type[nn.Module] = nn.ReLU,
144
+ init_layers_orthogonal: bool = True,
145
+ ) -> None:
146
+ super().__init__()
147
+ self.nvec = nvec
148
+ layer_sizes = tuple(hidden_sizes) + (nvec.sum(),)
149
+ self._fc = mlp(
150
+ layer_sizes,
151
+ activation,
152
+ init_layers_orthogonal=init_layers_orthogonal,
153
+ final_layer_gain=0.01,
154
+ )
155
+
156
+ def forward(
157
+ self,
158
+ obs: torch.Tensor,
159
+ actions: Optional[torch.Tensor] = None,
160
+ action_masks: Optional[torch.Tensor] = None,
161
+ ) -> PiForward:
162
+ logits = self._fc(obs)
163
+ pi = MultiCategorical(self.nvec, logits=logits, masks=action_masks)
164
+ logp_a = None
165
+ entropy = None
166
+ if actions is not None:
167
+ logp_a = pi.log_prob(actions)
168
+ entropy = pi.entropy()
169
+ return PiForward(pi, logp_a, entropy)
170
+
171
+
172
+ class GaussianDistribution(Normal):
173
+ def log_prob(self, a: torch.Tensor) -> torch.Tensor:
174
+ return super().log_prob(a).sum(axis=-1)
175
+
176
+ def sample(self) -> torch.Tensor:
177
+ return self.rsample()
178
+
179
+
180
+ class GaussianActorHead(Actor):
181
+ def __init__(
182
+ self,
183
+ act_dim: int,
184
+ hidden_sizes: Sequence[int] = (32,),
185
+ activation: Type[nn.Module] = nn.Tanh,
186
+ init_layers_orthogonal: bool = True,
187
+ log_std_init: float = -0.5,
188
+ ) -> None:
189
+ super().__init__()
190
+ layer_sizes = tuple(hidden_sizes) + (act_dim,)
191
+ self.mu_net = mlp(
192
+ layer_sizes,
193
+ activation,
194
+ init_layers_orthogonal=init_layers_orthogonal,
195
+ final_layer_gain=0.01,
196
+ )
197
+ self.log_std = nn.Parameter(
198
+ torch.ones(act_dim, dtype=torch.float32) * log_std_init
199
+ )
200
+
201
+ def _distribution(self, obs: torch.Tensor) -> Distribution:
202
+ mu = self.mu_net(obs)
203
+ std = torch.exp(self.log_std)
204
+ return GaussianDistribution(mu, std)
205
+
206
+ def forward(
207
+ self,
208
+ obs: torch.Tensor,
209
+ actions: Optional[torch.Tensor] = None,
210
+ action_masks: Optional[torch.Tensor] = None,
211
+ ) -> PiForward:
212
+ assert (
213
+ not action_masks
214
+ ), f"{self.__class__.__name__} does not support action_masks"
215
+ pi = self._distribution(obs)
216
+ logp_a = None
217
+ entropy = None
218
+ if actions is not None:
219
+ logp_a = pi.log_prob(actions)
220
+ entropy = pi.entropy()
221
+ return PiForward(pi, logp_a, entropy)
222
+
223
+
224
+ class TanhBijector:
225
+ def __init__(self, epsilon: float = 1e-6) -> None:
226
+ self.epsilon = epsilon
227
+
228
+ @staticmethod
229
+ def forward(x: torch.Tensor) -> torch.Tensor:
230
+ return torch.tanh(x)
231
+
232
+ @staticmethod
233
+ def inverse(y: torch.Tensor) -> torch.Tensor:
234
+ eps = torch.finfo(y.dtype).eps
235
+ clamped_y = y.clamp(min=-1.0 + eps, max=1.0 - eps)
236
+ return torch.atanh(clamped_y)
237
+
238
+ def log_prob_correction(self, x: torch.Tensor) -> torch.Tensor:
239
+ return torch.log(1.0 - torch.tanh(x) ** 2 + self.epsilon)
240
+
241
+
242
+ def sum_independent_dims(tensor: torch.Tensor) -> torch.Tensor:
243
+ if len(tensor.shape) > 1:
244
+ return tensor.sum(dim=1)
245
+ return tensor.sum()
246
+
247
+
248
+ class StateDependentNoiseDistribution(Normal):
249
+ def __init__(
250
+ self,
251
+ loc,
252
+ scale,
253
+ latent_sde: torch.Tensor,
254
+ exploration_mat: torch.Tensor,
255
+ exploration_matrices: torch.Tensor,
256
+ bijector: Optional[TanhBijector] = None,
257
+ validate_args=None,
258
+ ):
259
+ super().__init__(loc, scale, validate_args)
260
+ self.latent_sde = latent_sde
261
+ self.exploration_mat = exploration_mat
262
+ self.exploration_matrices = exploration_matrices
263
+ self.bijector = bijector
264
+
265
+ def log_prob(self, a: torch.Tensor) -> torch.Tensor:
266
+ gaussian_a = self.bijector.inverse(a) if self.bijector else a
267
+ log_prob = sum_independent_dims(super().log_prob(gaussian_a))
268
+ if self.bijector:
269
+ log_prob -= torch.sum(self.bijector.log_prob_correction(gaussian_a), dim=1)
270
+ return log_prob
271
+
272
+ def sample(self) -> torch.Tensor:
273
+ noise = self._get_noise()
274
+ actions = self.mean + noise
275
+ return self.bijector.forward(actions) if self.bijector else actions
276
+
277
+ def _get_noise(self) -> torch.Tensor:
278
+ if len(self.latent_sde) == 1 or len(self.latent_sde) != len(
279
+ self.exploration_matrices
280
+ ):
281
+ return torch.mm(self.latent_sde, self.exploration_mat)
282
+ # (batch_size, n_features) -> (batch_size, 1, n_features)
283
+ latent_sde = self.latent_sde.unsqueeze(dim=1)
284
+ # (batch_size, 1, n_actions)
285
+ noise = torch.bmm(latent_sde, self.exploration_matrices)
286
+ return noise.squeeze(dim=1)
287
+
288
+ @property
289
+ def mode(self) -> torch.Tensor:
290
+ mean = super().mode
291
+ return self.bijector.forward(mean) if self.bijector else mean
292
+
293
+
294
+ StateDependentNoiseActorHeadSelf = TypeVar(
295
+ "StateDependentNoiseActorHeadSelf", bound="StateDependentNoiseActorHead"
296
+ )
297
+
298
+
299
+ class StateDependentNoiseActorHead(Actor):
300
+ def __init__(
301
+ self,
302
+ act_dim: int,
303
+ hidden_sizes: Sequence[int] = (32,),
304
+ activation: Type[nn.Module] = nn.Tanh,
305
+ init_layers_orthogonal: bool = True,
306
+ log_std_init: float = -0.5,
307
+ full_std: bool = True,
308
+ squash_output: bool = False,
309
+ learn_std: bool = False,
310
+ ) -> None:
311
+ super().__init__()
312
+ self.act_dim = act_dim
313
+ layer_sizes = tuple(hidden_sizes) + (self.act_dim,)
314
+ if len(layer_sizes) == 2:
315
+ self.latent_net = nn.Identity()
316
+ elif len(layer_sizes) > 2:
317
+ self.latent_net = mlp(
318
+ layer_sizes[:-1],
319
+ activation,
320
+ output_activation=activation,
321
+ init_layers_orthogonal=init_layers_orthogonal,
322
+ )
323
+ else:
324
+ raise ValueError("hidden_sizes must be of at least length 1")
325
+ self.mu_net = mlp(
326
+ layer_sizes[-2:],
327
+ activation,
328
+ init_layers_orthogonal=init_layers_orthogonal,
329
+ final_layer_gain=0.01,
330
+ )
331
+ self.full_std = full_std
332
+ std_dim = (hidden_sizes[-1], act_dim if self.full_std else 1)
333
+ self.log_std = nn.Parameter(
334
+ torch.ones(std_dim, dtype=torch.float32) * log_std_init
335
+ )
336
+ self.bijector = TanhBijector() if squash_output else None
337
+ self.learn_std = learn_std
338
+ self.device = None
339
+
340
+ self.exploration_mat = None
341
+ self.exploration_matrices = None
342
+ self.sample_weights()
343
+
344
+ def to(
345
+ self: StateDependentNoiseActorHeadSelf,
346
+ device: Optional[torch.device] = None,
347
+ dtype: Optional[Union[torch.dtype, str]] = None,
348
+ non_blocking: bool = False,
349
+ ) -> StateDependentNoiseActorHeadSelf:
350
+ super().to(device, dtype, non_blocking)
351
+ self.device = device
352
+ return self
353
+
354
+ def _distribution(self, obs: torch.Tensor) -> Distribution:
355
+ latent = self.latent_net(obs)
356
+ mu = self.mu_net(latent)
357
+ latent_sde = latent if self.learn_std else latent.detach()
358
+ variance = torch.mm(latent_sde**2, self._get_std() ** 2)
359
+ assert self.exploration_mat is not None
360
+ assert self.exploration_matrices is not None
361
+ return StateDependentNoiseDistribution(
362
+ mu,
363
+ torch.sqrt(variance + 1e-6),
364
+ latent_sde,
365
+ self.exploration_mat,
366
+ self.exploration_matrices,
367
+ self.bijector,
368
+ )
369
+
370
+ def _get_std(self) -> torch.Tensor:
371
+ std = torch.exp(self.log_std)
372
+ if self.full_std:
373
+ return std
374
+ ones = torch.ones(self.log_std.shape[0], self.act_dim)
375
+ if self.device:
376
+ ones = ones.to(self.device)
377
+ return ones * std
378
+
379
+ def forward(
380
+ self,
381
+ obs: torch.Tensor,
382
+ actions: Optional[torch.Tensor] = None,
383
+ action_masks: Optional[torch.Tensor] = None,
384
+ ) -> PiForward:
385
+ assert (
386
+ not action_masks
387
+ ), f"{self.__class__.__name__} does not support action_masks"
388
+ pi = self._distribution(obs)
389
+ logp_a = None
390
+ entropy = None
391
+ if actions is not None:
392
+ logp_a = pi.log_prob(actions)
393
+ entropy = -logp_a if self.bijector else sum_independent_dims(pi.entropy())
394
+ return PiForward(pi, logp_a, entropy)
395
+
396
+ def sample_weights(self, batch_size: int = 1) -> None:
397
+ std = self._get_std()
398
+ weights_dist = Normal(torch.zeros_like(std), std)
399
+ # Reparametrization trick to pass gradients
400
+ self.exploration_mat = weights_dist.rsample()
401
+ self.exploration_matrices = weights_dist.rsample(torch.Size((batch_size,)))
402
+
403
+
404
+ def actor_head(
405
+ action_space: gym.Space,
406
+ hidden_sizes: Sequence[int],
407
+ init_layers_orthogonal: bool,
408
+ activation: Type[nn.Module],
409
+ log_std_init: float = -0.5,
410
+ use_sde: bool = False,
411
+ full_std: bool = True,
412
+ squash_output: bool = False,
413
+ ) -> Actor:
414
+ assert not use_sde or isinstance(
415
+ action_space, Box
416
+ ), "use_sde only valid if Box action_space"
417
+ assert not squash_output or use_sde, "squash_output only valid if use_sde"
418
+ if isinstance(action_space, Discrete):
419
+ return CategoricalActorHead(
420
+ action_space.n,
421
+ hidden_sizes=hidden_sizes,
422
+ activation=activation,
423
+ init_layers_orthogonal=init_layers_orthogonal,
424
+ )
425
+ elif isinstance(action_space, Box):
426
+ if use_sde:
427
+ return StateDependentNoiseActorHead(
428
+ action_space.shape[0],
429
+ hidden_sizes=hidden_sizes,
430
+ activation=activation,
431
+ init_layers_orthogonal=init_layers_orthogonal,
432
+ log_std_init=log_std_init,
433
+ full_std=full_std,
434
+ squash_output=squash_output,
435
+ )
436
+ else:
437
+ return GaussianActorHead(
438
+ action_space.shape[0],
439
+ hidden_sizes=hidden_sizes,
440
+ activation=activation,
441
+ init_layers_orthogonal=init_layers_orthogonal,
442
+ log_std_init=log_std_init,
443
+ )
444
+ elif isinstance(action_space, MultiDiscrete):
445
+ return MultiDiscreteActorHead(
446
+ action_space.nvec,
447
+ hidden_sizes=hidden_sizes,
448
+ activation=activation,
449
+ init_layers_orthogonal=init_layers_orthogonal,
450
+ )
451
+ else:
452
+ raise ValueError(f"Unsupported action space: {action_space}")
rl_algo_impls/shared/policy/critic.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gym
2
+ import torch
3
+ import torch.nn as nn
4
+
5
+ from typing import Sequence, Type
6
+
7
+ from rl_algo_impls.shared.module.module import mlp
8
+
9
+
10
+ class CriticHead(nn.Module):
11
+ def __init__(
12
+ self,
13
+ hidden_sizes: Sequence[int] = (32,),
14
+ activation: Type[nn.Module] = nn.Tanh,
15
+ init_layers_orthogonal: bool = True,
16
+ ) -> None:
17
+ super().__init__()
18
+ layer_sizes = tuple(hidden_sizes) + (1,)
19
+ self._fc = mlp(
20
+ layer_sizes,
21
+ activation,
22
+ init_layers_orthogonal=init_layers_orthogonal,
23
+ final_layer_gain=1.0,
24
+ )
25
+
26
+ def forward(self, obs: torch.Tensor) -> torch.Tensor:
27
+ v = self._fc(obs)
28
+ return v.squeeze(-1)