Multi-Environment¶
Classes for composing several different environments (and their vectorised variants) under a single unified handle.
MultiEnv holds a list of JaxEnv instances and dispatches via Python iteration. MultiVecEnv holds a dict of BatchedEnv instances (keyed by env name, auto-derived from a list if you prefer) and dispatches inside one jax.jit boundary.
envrax.multi_env.MultiEnv
¶
Container for multiple JaxEnv instances keyed by env name.
Holds one inner env per key and dispatches reset/step via a Python
loop. No outer jax.jit boundary is added — each inner env keeps its
own compile cycle (typically via JitWrapper). Use MultiVecEnv if
you need a single jitted dispatch over batched envs.
Accepts either a list (keys derived from each env's name via
_auto_key) or a dict (used as-is for explicit control).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
envs
|
List[JaxEnv] | Dict[str, JaxEnv]
|
Envs to wrap. When a list, keys are derived from |
required |
Source code in envrax/multi_env.py
| Python | |
|---|---|
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | |
envs
property
¶
The inner JaxEnv instances keyed by env name.
env_keys
property
¶
Ordered list of env-type keys.
n_envs
property
¶
Number of environments (M).
observation_spaces
property
¶
Per-env observation spaces.
action_spaces
property
¶
Per-env action spaces.
observation_shapes
property
¶
Per-env observation shapes.
action_shapes
property
¶
Per-env action shapes.
observation_sizes
property
¶
Per-env flat observation element counts (prod(shape)).
action_sizes
property
¶
Per-env flat action element counts (prod(shape)).
observation_dtypes
property
¶
Per-env observation dtypes.
action_dtypes
property
¶
Per-env action dtypes.
pad_dims()
¶
Return (max_action_size, max_observation_size) across envs.
Returns:
| Name | Type | Description |
|---|---|---|
action |
int
|
Largest flat action size. |
observation |
int
|
Largest flat observation size. |
Source code in envrax/multi_env.py
| Python | |
|---|---|
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
reset(rng)
¶
Reset all environments with independent PRNG sub-keys.
Splits rng deterministically. Same master key produces the same
per-env keys for full reproducibility.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rng
|
PRNGKey
|
JAX PRNG key |
required |
Returns:
| Name | Type | Description |
|---|---|---|
obs |
Dict[str, Array]
|
Per-env initial observations. |
states |
Dict[str, EnvState]
|
Per-env initial states. |
Source code in envrax/multi_env.py
| Python | |
|---|---|
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | |
step(states, actions)
¶
Step all environments simultaneously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
states
|
Dict[str, EnvState]
|
Per-env states from a previous reset or step. |
required |
actions
|
Dict[str, Array]
|
Per-env actions matching each env's action space. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
obs |
Dict[str, Array]
|
Per-env observations after the step. |
new_states |
Dict[str, EnvState]
|
Per-env updated states. |
rewards |
Dict[str, Array]
|
Per-env scalar rewards. |
dones |
Dict[str, Array]
|
Per-env terminal flags. |
infos |
Dict[str, Dict[str, Any]]
|
Per-env info dicts. |
Raises:
| Name | Type | Description |
|---|---|---|
key_mismatch
|
ValueError
|
If |
Source code in envrax/multi_env.py
| Python | |
|---|---|
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | |
compile(*, progress=True)
¶
Trigger XLA compilation for all JIT-wrapped environments.
Calls compile() on each inner env that is a JitWrapper.
Environments without JIT wrapping are silently skipped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
progress
|
bool
|
Show a |
True
|
Source code in envrax/multi_env.py
| Python | |
|---|---|
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | |
envrax.multi_vec_env.MultiVecEnv
¶
JAX-native container for multiple BatchedEnv instances keyed by env name.
State is a dict-of-pytrees (Dict[str, chex.ArrayTree]). The
cross-env-type dispatch runs as a Python loop at jax.jit trace time,
producing one XLA computation per call that dispatches one inner-kernel
per env type with no per-call Python overhead between them.
Accepts either a list (keys derived from each env's name via
_auto_key) or a dict (used as-is for explicit control).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
envs
|
List[BatchedEnv] | Dict[str, BatchedEnv]
|
Envs to wrap. When a list, keys are derived from |
required |
Source code in envrax/multi_vec_env.py
| Python | |
|---|---|
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | |
envs
property
¶
The inner BatchedEnv instances keyed by env name.
env_keys
property
¶
Ordered list of env-type keys.
n_envs
property
¶
Number of distinct env types (= number of BatchedEnv instances).
total_slots
property
¶
Total number of individual agent slots across all env types.
slots_per_env
property
¶
Per-env-type slot counts.
single_observation_spaces
property
¶
Per-env-type unbatched observation spaces.
single_action_spaces
property
¶
Per-env-type unbatched action spaces.
single_observation_shapes
property
¶
Per-env-type unbatched observation shapes.
single_action_shapes
property
¶
Per-env-type unbatched action shapes.
single_observation_sizes
property
¶
Per-env-type flat unbatched observation element counts.
single_action_sizes
property
¶
Per-env-type flat unbatched action element counts.
single_observation_dtypes
property
¶
Per-env-type unbatched observation dtypes.
single_action_dtypes
property
¶
Per-env-type unbatched action dtypes.
pad_dims()
¶
Return (max_action_size, max_observation_size) across env types.
Returns:
| Name | Type | Description |
|---|---|---|
action |
int
|
Largest flat action size across all env types. |
observation |
int
|
Largest flat observation size across all env types. |
Source code in envrax/multi_vec_env.py
| Python | |
|---|---|
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
reset(rng)
¶
Reset all env types with independent PRNG sub-keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rng
|
PRNGKey
|
JAX PRNG key |
required |
Returns:
| Name | Type | Description |
|---|---|---|
obs |
Dict[str, Array]
|
Per-env-type batched observations. |
states |
Dict[str, ArrayTree]
|
Per-env-type batched state pytrees. |
Source code in envrax/multi_vec_env.py
| Python | |
|---|---|
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | |
step(states, actions)
¶
Step all env types simultaneously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
states
|
Dict[str, ArrayTree]
|
Per-env-type batched states from a previous reset or step. |
required |
actions
|
Dict[str, Array]
|
Per-env-type batched actions. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
obs |
Dict[str, Array]
|
Per-env-type batched observations after the step. |
new_states |
Dict[str, ArrayTree]
|
Per-env-type updated batched states. |
rewards |
Dict[str, Array]
|
Per-env-type batched rewards. |
dones |
Dict[str, Array]
|
Per-env-type batched terminal flags. |
infos |
Dict[str, Dict[str, Any]]
|
Per-env-type batched info dicts. |
Raises:
| Name | Type | Description |
|---|---|---|
key_mismatch
|
ValueError
|
If |
Source code in envrax/multi_vec_env.py
| Python | |
|---|---|
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | |
slot_state(states, key, slot_idx)
¶
Extract the single-slot state pytree for one agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
states
|
Dict[str, ArrayTree]
|
Per-env-type batched states. |
required |
key
|
str
|
Env-type key in |
required |
slot_idx
|
int
|
Slot index in |
required |
Returns:
| Name | Type | Description |
|---|---|---|
single_state |
ArrayTree
|
Unbatched state pytree for the chosen slot. |
Source code in envrax/multi_vec_env.py
| Python | |
|---|---|
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | |
render_slot(states, key, slot_idx)
¶
Render a single slot as an RGB frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
states
|
Dict[str, ArrayTree]
|
Per-env-type batched states. |
required |
key
|
str
|
Env-type key in |
required |
slot_idx
|
int
|
Slot index in |
required |
Returns:
| Name | Type | Description |
|---|---|---|
frame |
ndarray
|
uint8 RGB array of shape |
Source code in envrax/multi_vec_env.py
| Python | |
|---|---|
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | |
compile(cache_dir=DEFAULT_CACHE_DIR, *, progress=True)
¶
Trigger XLA compilation for all inner envs and warm the multi-step jit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_dir
|
Path | str | None
|
XLA cache directory. Defaults to |
DEFAULT_CACHE_DIR
|
progress
|
bool
|
Show a |
True
|
Source code in envrax/multi_vec_env.py
| Python | |
|---|---|
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | |