Skip to content

Commit 5977cce

Browse files
authored
Feat/Add Sebulba (#105)
* feat: add sebulba ppo system and reorganise for future sebulba systems
1 parent 1a86c09 commit 5977cce

85 files changed

Lines changed: 2188 additions & 124 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,20 @@
3232

3333
## Welcome to Stoix! 🏛️
3434

35-
Stoix provides simplified code for quickly iterating on ideas in single-agent reinforcement learning with useful implementations of popular single-agent RL algorithms in JAX allowing for easy parallelisation across devices with JAX's `pmap`. All implementations are fully compilable with JAX's `jit` thus making training and environment execution very fast. However, this requires environments written in JAX. Algorithms and their default hyperparameters have not been hyper-optimised for any specific environment and are useful as a starting point for research and/or for initial baselines.
35+
Stoix provides simplified code for quickly iterating on ideas in single-agent reinforcement learning with useful implementations of popular single-agent RL algorithms in JAX allowing for easy parallelisation across devices with JAX's `pmap`. All implementations are fully compiled with JAX's `jit` thus making training and environment execution very fast. However, this does require environments written in JAX. For environments not written in JAX, Stoix offers Sebulba systems (see below). Algorithms and their default hyperparameters have not been hyper-optimised for any specific environment and are useful as a starting point for research and/or for initial baselines.
3636

3737
To join us in these efforts, please feel free to reach out, raise issues or read our [contribution guidelines](#contributing-) (or just star 🌟 to stay up to date with the latest developments)!
3838

39-
Stoix is fully in JAX with substantial speed improvement compared to other popular libraries. We currently provide native support for the [Jumanji][jumanji] environment API and wrappers for popular JAX-based RL environments.
39+
Stoix is fully in JAX with substantial speed improvement compared to other popular libraries. We currently provide native support for the [Jumanji][jumanji] environment API and wrappers for popular RL environments.
40+
41+
## System Design Paradigms
42+
Stoix offers two primary system design paradigms (Podracer Architectures) to cater to different research and deployment needs:
43+
44+
- **Anakin:** Traditional Stoix implementations are fully end-to-end compiled with JAX, focusing on speed and simplicity with native JAX environments. This design paradigm is ideal for setups where all components, including environments, can be optimized using JAX, leveraging the full power of JAX's pmap and jit. For an illustration of the Anakin architecture, see this [figure](docs/images/anakin_arch.jpg) from the [Mava](mava) technical report.
45+
46+
- **Sebulba:** The Sebulba system introduces flexibility by allowing different devices to be assigned specifically for learning and acting. In this setup, acting devices serve as inference servers for multiple parallel environments, which can be written in any framework, not just JAX. This enables Stoix to be used with a broader range of environments while still benefiting from JAX's speed. For an illustration of the Sebulba architecture, see this [animation](docs/images/sebulba_arch.gif) from the [InstaDeep Sebulba implementation](https://github.com/instadeepai/sebulba/).
47+
48+
Not all implementations have both Anakin and Sebulba implementations but effort has gone into making the two implementations as similar as possible to allow easy conversion.
4049

4150
## Code Philosophy 🧘
4251

@@ -47,9 +56,11 @@ The current code in Stoix was initially **largely** taken and subsequently adapt
4756
### Stoix TLDR
4857
1. **Algorithms:** Stoix offers easily hackable, single-file implementations of popular algorithms in pure JAX. You can vectorize algorithm training on a single device using `vmap` as well as distribute training across multiple devices with `pmap` (or both). Multi-host support (i.e., vmap/pmap over multiple devices **and** machines) is coming soon! All implementations include checkpointing to save and resume parameters and training runs.
4958

50-
2. **Hydra Config System:** Leverage the Hydra configuration system for efficient and consistent management of experiments, network architectures, and environments. Hydra facilitates the easy addition of new hyperparameters and supports multi-runs and Optuna hyperparameter optimization. No more need to create large bash scripts to run a series of experiments with differing hyperparameters, network architectures or environments.
59+
2. **System Designs:** Choose between Anakin systems for fully JAX-optimized workflows or Sebulba systems for flexibility with non-JAX environments.
60+
61+
3. **Hydra Config System:** Leverage the Hydra configuration system for efficient and consistent management of experiments, network architectures, and environments. Hydra facilitates the easy addition of new hyperparameters and supports multi-runs and Optuna hyperparameter optimization. No more need to create large bash scripts to run a series of experiments with differing hyperparameters, network architectures or environments.
5162

52-
3. **Advanced Logging:** Stoix features advanced and configurable logging, ready for output to the terminal, TensorBoard, and other ML tracking dashboards (WandB and Neptune). It also supports logging experiments in JSON format ready for statistical tests and generating RLiable plots (see the plotting notebook). This enables statistically confident comparisons of algorithms natively.
63+
4. **Advanced Logging:** Stoix features advanced and configurable logging, ready for output to the terminal, TensorBoard, and other ML tracking dashboards (WandB and Neptune). It also supports logging experiments in JSON format ready for statistical tests and generating RLiable plots (see the plotting notebook). This enables statistically confident comparisons of algorithms natively.
5364

5465
Stoix currently offers the following building blocks for Single-Agent RL research:
5566

@@ -78,14 +89,17 @@ Stoix currently offers the following building blocks for Single-Agent RL researc
7889
- **Sampled Alpha/Mu-Zero** - [Paper](https://arxiv.org/abs/2104.06303)
7990

8091
### Environment Wrappers 🍬
81-
Stoix offers wrappers for [Gymnax][gymnax], [Jumanji][jumanji], [Brax][brax], [XMinigrid][xminigrid], [Craftax][craftax], [POPJym][popjym], [Navix][navix] and even [JAXMarl][jaxmarl] (although using Centralised Controllers).
92+
Stoix offers wrappers for:
93+
94+
- **JAX environments:** [Gymnax][gymnax], [Jumanji][jumanji], [Brax][brax], [XMinigrid][xminigrid], [Craftax][craftax], [POPJym][popjym], [Navix][navix] and even [JAXMarl][jaxmarl] (although using Centralised Controllers).
95+
- **Non-JAX environments:** [Envpool][envpool] and [Gymnasium][gymnasium].
8296

8397
### Statistically Robust Evaluation 🧪
8498
Stoix natively supports logging to json files which adhere to the standard suggested by [Gorsane et al. (2022)][toward_standard_eval]. This enables easy downstream experiment plotting and aggregation using the tools found in the [MARL-eval][marl_eval] library.
8599

86100
## Performance and Speed 🚀
87101

88-
As the code in Stoix (at the time of creation) was in essence a port of [Mava][mava], for further speed comparisons we point to their repo. Additionally, we refer to the PureJaxRL blog post [here](https://chrislu.page/blog/meta-disco/) where the speed benefits of end-to-end JAX systems are discussed.
102+
As the code in Stoix (at the time of creation) was in essence a port of [Mava][mava], for further speed comparisons we point to their repo. Additionally, we refer to the PureJaxRL blog post [here](https://chrislu.page/blog/meta-disco/) where the speed benefits of end-to-end JAX systems are discussed. Lastly, we point to the Podracer architectures paper [here][anakin_paper] where these ideas were first discussed and benchmarked.
89103

90104
Below we provide some plots illustrating that Stoix performs equally to that of [PureJaxRL][purejaxrl] but with the added benefit of the code being already set up for `pmap` distribution over devices as well as the other features provided (algorithm implementations, logging, config system, etc).
91105
<p align="center">
@@ -118,14 +132,22 @@ we advise users to explicitly install the correct JAX version (see the [official
118132

119133
To get started with training your first Stoix system, simply run one of the system files. e.g.,
120134

135+
For an Anakin system:
136+
137+
```bash
138+
python stoix/systems/ppo/anakin/ff_ppo.py
139+
```
140+
141+
or for a Sebulba system:
142+
121143
```bash
122-
python stoix/systems/ppo/ff_ppo.py
144+
python stoix/systems/ppo/sebulba/ff_ppo.py arch=sebulba env=envpool/pong network=visual_resnet
123145
```
124146

125-
Stoix makes use of Hydra for config management. In order to see our default system configs please see the `stoix/configs/` directory. A benefit of Hydra is that configs can either be set in config yaml files or overwritten from the terminal on the fly. For an example of running a system on the CartPole environment, the above code can simply be adapted as follows:
147+
Stoix makes use of Hydra for config management. In order to see our default system configs please see the `stoix/configs/` directory. A benefit of Hydra is that configs can either be set in config yaml files or overwritten from the terminal on the fly. For an example of running a system on the CartPole environment and changing any hyperparameters, the above code can simply be adapted as follows:
126148

127149
```bash
128-
python stoix/systems/ppo/ff_ppo.py env=gymnax/cartpole
150+
python stoix/systems/ppo/ff_ppo.py env=gymnax/cartpole system.rollout_length=32 system.decay_learning_rates=True
129151
```
130152

131153
Additionally, certain implementations such as Dueling DQN are decided by the network architecture but the underlying algorithm stays the same. For example, if you wanted to run Dueling DQN you would simply do:
@@ -146,6 +168,8 @@ python stoix/systems/q_learning/ff_c51.py network=mlp_dueling_c51
146168

147169
2. Due to the way Stoix is set up, you are not guaranteed to run for exactly the number of timesteps you set. A warning is given at the beginning of a run on the actual number of timesteps that will be run. This value will always be less than or equal to the specified sample budget. To get the exact number of transitions to run, ensure that the number of timesteps is divisible by the rollout length * total_num_envs and additionally ensure that the number of evaluations spaced out throughout training perfectly divide the number of updates to be performed. To see the exact calculation, see the file total_timestep_checker.py. This will give an indication of how the actual number of timesteps is calculated and how you can easily set it up to run the exact amount you desire. Its relatively trivial to do so but it is important to keep in mind.
148170

171+
3. Optimising the performance and speed for Sebulba systems can be a little tricky as you need to balance the pipeline size, the number of actor threads, etc so keep this in mind when applying an algorithm to a new problem.
172+
149173
## Contributing 🤝
150174

151175
Please read our [contributing docs](docs/CONTRIBUTING.md) for details on how to submit pull requests, our Contributor License Agreement and community guidelines.
@@ -217,5 +241,7 @@ We would like to thank the authors and developers of [Mava](mava) as this was es
217241
[craftax]: https://github.com/MichaelTMatthews/Craftax
218242
[popjym]: https://github.com/FLAIROx/popjym
219243
[navix]: https://github.com/epignatelli/navix
244+
[envpool]: https://github.com/sail-sg/envpool/
245+
[gymnasium]: https://github.com/Farama-Foundation/Gymnasium
220246

221247
Disclaimer: This is not an official InstaDeep product nor is any of the work putforward associated with InstaDeep in any official capacity.

docs/images/anakin_arch.jpg

413 KB
Loading

docs/images/sebulba_arch.gif

1.69 MB
Loading

requirements/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ chex
33
colorama
44
craftax
55
distrax @ git+https://github.com/google-deepmind/distrax # distrax release doesn't support jax > 0.4.13
6+
envpool
67
flashbax @ git+https://github.com/instadeepai/flashbax
78
flax
9+
gymnasium
810
gymnax>=0.0.6
911
huggingface_hub
1012
hydra-core==1.3.2

stoix/base_types.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import TYPE_CHECKING, Any, Callable, Dict, Generic, Tuple, TypeVar
1+
from typing import TYPE_CHECKING, Any, Callable, Dict, Generic, Optional, Tuple, TypeVar
22

33
import chex
44
from distrax import DistributionLike
@@ -36,7 +36,7 @@ class Observation(NamedTuple):
3636

3737
agent_view: chex.Array # (num_obs_features,)
3838
action_mask: chex.Array # (num_actions,)
39-
step_count: chex.Array # (,)
39+
step_count: Optional[chex.Array] = None # (,)
4040

4141

4242
class ObservationGlobalState(NamedTuple):
@@ -106,8 +106,18 @@ class ActorCriticHiddenStates(NamedTuple):
106106
critic_hidden_state: HiddenState
107107

108108

109-
class LearnerState(NamedTuple):
110-
"""State of the learner."""
109+
class CoreLearnerState(NamedTuple):
110+
"""Base state of the learner. Can be used for both on-policy and off-policy learners.
111+
Mainly used for sebulba systems since we dont store env state."""
112+
113+
params: Parameters
114+
opt_states: OptStates
115+
key: chex.PRNGKey
116+
timestep: TimeStep
117+
118+
119+
class OnPolicyLearnerState(NamedTuple):
120+
"""State of the learner. Used for on-policy learners."""
111121

112122
params: Parameters
113123
opt_states: OptStates
@@ -146,6 +156,9 @@ class OnlineAndTarget(NamedTuple):
146156
StoixState = TypeVar(
147157
"StoixState",
148158
)
159+
StoixTransition = TypeVar(
160+
"StoixTransition",
161+
)
149162

150163

151164
class ExperimentOutput(NamedTuple, Generic[StoixState]):
@@ -158,6 +171,7 @@ class ExperimentOutput(NamedTuple, Generic[StoixState]):
158171

159172
RNNObservation: TypeAlias = Tuple[Observation, Done]
160173
LearnerFn = Callable[[StoixState], ExperimentOutput[StoixState]]
174+
SebulbaLearnerFn = Callable[[StoixState, StoixTransition], ExperimentOutput[StoixState]]
161175
EvalFn = Callable[[FrozenDict, chex.PRNGKey], ExperimentOutput[StoixState]]
162176

163177
ActorApply = Callable[..., DistributionLike]
@@ -174,3 +188,6 @@ class ExperimentOutput(NamedTuple, Generic[StoixState]):
174188
[FrozenDict, HiddenState, RNNObservation, chex.PRNGKey], Tuple[HiddenState, chex.Array]
175189
]
176190
RecCriticApply = Callable[[FrozenDict, HiddenState, RNNObservation], Tuple[HiddenState, Value]]
191+
192+
193+
EnvFactory = Callable[[int], Any]

stoix/configs/arch/anakin.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# --- Anakin config ---
2-
2+
architecture_name: anakin
33
# --- Training ---
44
seed: 42 # RNG seed.
55
update_batch_size: 1 # Number of vectorised gradient updates per device.

stoix/configs/arch/sebulba.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# --- Sebulba config ---
2+
architecture_name : sebulba
3+
# --- Training ---
4+
seed: 42 # RNG seed.
5+
total_num_envs: 1024 # Total Number of vectorised environments across all actors. Needs to be divisible by the number of actor devices and actors per device.
6+
total_timesteps: 1e7 # Set the total environment steps.
7+
# If unspecified, it's derived from num_updates; otherwise, num_updates adjusts based on this value.
8+
num_updates: ~ # Number of updates
9+
10+
# Define the number of actors per device and which devices to use.
11+
actor:
12+
device_ids: [0,1] # Define which devices to use for the actors.
13+
actor_per_device: 2 # number of different threads per actor device.
14+
15+
# Define which devices to use for the learner.
16+
learner:
17+
device_ids: [2,3] # Define which devices to use for the learner.
18+
19+
# Size of the queue for the pipeline where actors push data and the learner pulls data.
20+
pipeline_queue_size: 10
21+
22+
# --- Evaluation ---
23+
evaluation_greedy: False # Evaluate the policy greedily. If True the policy will select
24+
# an action which corresponds to the greatest logit. If false, the policy will sample
25+
# from the logits.
26+
num_eval_episodes: 128 # Number of episodes to evaluate per evaluation.
27+
num_evaluation: 20 # Number of evenly spaced evaluations to perform during training.
28+
absolute_metric: True # Whether the absolute metric should be computed. For more details
29+
# on the absolute metric please see: https://arxiv.org/abs/2209.10485

stoix/configs/default_ff_awr.yaml renamed to stoix/configs/default/anakin/default_ff_awr.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ defaults:
55
- network: mlp
66
- env: gymnax/cartpole
77
- _self_
8+
9+
hydra:
10+
searchpath:
11+
- file://stoix/configs

stoix/configs/default_ff_awr_continuous.yaml renamed to stoix/configs/default/anakin/default_ff_awr_continuous.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ defaults:
55
- network: mlp_continuous
66
- env: brax/ant
77
- _self_
8+
9+
hydra:
10+
searchpath:
11+
- file://stoix/configs

stoix/configs/default_ff_az.yaml renamed to stoix/configs/default/anakin/default_ff_az.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ defaults:
55
- network: mlp
66
- env: gymnax/cartpole
77
- _self_
8+
9+
hydra:
10+
searchpath:
11+
- file://stoix/configs

0 commit comments

Comments
 (0)