|
1 | | -# Dreamer example |
| 1 | +# Dreamer V1 |
2 | 2 |
|
3 | | -## Note: |
4 | | -This example is not included in the benchmarked results of the current release (v0.3). The intention is to include it in the |
5 | | -benchmarking of future releases, to ensure that it can be successfully run with the release code and that the |
6 | | -results are consistent. For now, be aware that this additional check has not been performed in the case of this |
7 | | -specific example. |
| 3 | +This is an implementation of the Dreamer algorithm from the paper |
| 4 | +["Dream to Control: Learning Behaviors by Latent Imagination"](https://arxiv.org/abs/1912.01603) (Hafner et al., ICLR 2020). |
| 5 | + |
| 6 | +Dreamer is a model-based reinforcement learning algorithm that: |
| 7 | +1. Learns a **world model** (RSSM) from experience |
| 8 | +2. **Imagines** future trajectories in latent space |
| 9 | +3. Trains **actor and critic** using analytic gradients through the imagined rollouts |
| 10 | + |
| 11 | +## Setup |
| 12 | + |
| 13 | +### Dependencies |
| 14 | + |
| 15 | +```bash |
| 16 | +# Create virtual environment |
| 17 | +uv venv torchrl --python 3.12 |
| 18 | +source torchrl/bin/activate |
| 19 | + |
| 20 | +# Install PyTorch (adjust for your CUDA version) |
| 21 | +uv pip install --pre torch torchvision --index-url https://download.pytorch.org/whl/nightly/cu128 |
| 22 | + |
| 23 | +# Install TorchRL and TensorDict |
| 24 | +uv pip install tensordict torchrl |
| 25 | + |
| 26 | +# Install additional dependencies |
| 27 | +uv pip install mujoco dm_control wandb tqdm hydra-core |
| 28 | +``` |
| 29 | + |
| 30 | +### System Dependencies (for MuJoCo rendering) |
| 31 | + |
| 32 | +```bash |
| 33 | +apt-get update && apt-get install -y \ |
| 34 | + libegl1 \ |
| 35 | + libgl1 \ |
| 36 | + libgles2 \ |
| 37 | + libglvnd0 |
| 38 | +``` |
| 39 | + |
| 40 | +### Environment Variables |
| 41 | + |
| 42 | +```bash |
| 43 | +export MUJOCO_GL=egl |
| 44 | +export MUJOCO_EGL_DEVICE_ID=0 |
| 45 | +``` |
| 46 | + |
| 47 | +## Running |
| 48 | + |
| 49 | +```bash |
| 50 | +python dreamer.py |
| 51 | +``` |
| 52 | + |
| 53 | +### Configuration |
| 54 | + |
| 55 | +The default configuration trains on DMControl's `cheetah-run` task. You can override settings via command line: |
| 56 | + |
| 57 | +```bash |
| 58 | +# Different environment |
| 59 | +python dreamer.py env.name=walker env.task=walk |
| 60 | + |
| 61 | +# Mixed precision options: false, true (=bfloat16), float16, bfloat16 |
| 62 | +python dreamer.py optimization.autocast=bfloat16 # default |
| 63 | +python dreamer.py optimization.autocast=float16 # for older GPUs |
| 64 | +python dreamer.py optimization.autocast=false # disable autocast |
| 65 | + |
| 66 | +# Adjust batch size |
| 67 | +python dreamer.py replay_buffer.batch_size=1000 |
| 68 | +``` |
| 69 | + |
| 70 | +## Known Caveats |
| 71 | + |
| 72 | +### 1. Mixed Precision (Autocast) Compatibility |
| 73 | + |
| 74 | +Some GPU/cuBLAS combinations have issues with `bfloat16` autocast, resulting in: |
| 75 | +``` |
| 76 | +RuntimeError: CUDA error: CUBLAS_STATUS_INVALID_VALUE when calling cublasGemmEx |
| 77 | +``` |
| 78 | + |
| 79 | +**Solutions:** |
| 80 | +- Try float16: `optimization.autocast=float16` |
| 81 | +- Or disable autocast entirely: `optimization.autocast=false` |
| 82 | + |
| 83 | +Note: Ensure your PyTorch CUDA version matches your driver. For example, with CUDA 13.0: |
| 84 | +```bash |
| 85 | +uv pip install --pre torch torchvision --index-url https://download.pytorch.org/whl/nightly/cu130 |
| 86 | +``` |
| 87 | + |
| 88 | +### 2. Benchmarking Status |
| 89 | + |
| 90 | +This implementation has not been fully benchmarked against the original paper's results. |
| 91 | +Performance may differ from published numbers. |
| 92 | + |
| 93 | +### 3. Video Logging |
| 94 | + |
| 95 | +To enable video logging of both real and imagined rollouts: |
| 96 | +```bash |
| 97 | +python dreamer.py logger.video=True |
| 98 | +``` |
| 99 | + |
| 100 | +This requires additional setup for rendering and significantly increases computation time. |
| 101 | + |
| 102 | +## Architecture Overview |
| 103 | + |
| 104 | +``` |
| 105 | +World Model: |
| 106 | + - ObsEncoder: pixels -> encoded_latents |
| 107 | + - RSSMPrior: (state, belief, action) -> next_belief, prior_dist |
| 108 | + - RSSMPosterior: (belief, encoded_latents) -> posterior_dist, state |
| 109 | + - ObsDecoder: (state, belief) -> reconstructed_pixels |
| 110 | + - RewardModel: (state, belief) -> predicted_reward |
| 111 | +
|
| 112 | +Actor: (state, belief) -> action_distribution |
| 113 | +Critic: (state, belief) -> state_value |
| 114 | +``` |
| 115 | + |
| 116 | +## Training Loop |
| 117 | + |
| 118 | +1. **Collect** real experience from environment |
| 119 | +2. **Train world model** on sequences from replay buffer (KL + reconstruction + reward loss) |
| 120 | +3. **Imagine** trajectories starting from encoded real states |
| 121 | +4. **Train actor** to maximize imagined returns (gradients flow through dynamics) |
| 122 | +5. **Train critic** to predict lambda returns on imagined trajectories |
| 123 | + |
| 124 | +## References |
| 125 | + |
| 126 | +- Original Paper: [Dream to Control: Learning Behaviors by Latent Imagination](https://arxiv.org/abs/1912.01603) |
| 127 | +- PlaNet (predecessor): [Learning Latent Dynamics for Planning from Pixels](https://arxiv.org/abs/1811.04551) |
| 128 | +- DreamerV2: [Mastering Atari with Discrete World Models](https://arxiv.org/abs/2010.02193) |
| 129 | +- DreamerV3: [Mastering Diverse Domains through World Models](https://arxiv.org/abs/2301.04104) |
0 commit comments