Multi-Environment¶
Classes for composing several different environments (and their vectorised variants) under a single unified handle.
envrax.multi_env.MultiEnv
¶
Manages M heterogeneous JaxEnv instances as a single unit.
Useful for holding M different JaxEnvs — with potentially different
classes, configs, and shapes.
Use .class_groups to identify which indices share a class for
downstream batching of same-shape observations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
envs
|
List[JaxEnv]
|
List of already-constructed environments. |
required |
Source code in envrax/multi_env.py
| Python | |
|---|---|
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 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 | |
num_envs
property
¶
Number of environments (M).
envs
property
¶
The inner environment instances.
observation_spaces
property
¶
Per-env observation spaces.
action_spaces
property
¶
Per-env action spaces.
class_groups
property
¶
Env class name → list of indices.
Useful for downstream code that wants to batch operations across envs of the same class (e.g. stacking observations with matching shapes).
reset(rng)
¶
Reset all M environments with independent PRNG keys.
Splits rng into M sub-keys 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 |
|---|---|---|
observations |
List[Array]
|
Per-env initial observations |
states |
List[EnvState]
|
Per-env initial states |
Source code in envrax/multi_env.py
| Python | |
|---|---|
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 | |
step(states, actions)
¶
Step all M environments simultaneously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
states
|
List[EnvState]
|
Per-env states from a previous reset or step |
required |
actions
|
List[Array]
|
Per-env actions matching each env's action space |
required |
Returns:
| Name | Type | Description |
|---|---|---|
observations |
List[Array]
|
Per-env observations after the step |
new_states |
List[EnvState]
|
Per-env updated states |
rewards |
List[Array]
|
Per-env scalar rewards |
dones |
List[Array]
|
Per-env terminal flags |
infos |
List[Dict[str, Any]]
|
Per-env info dicts |
Raises:
| Name | Type | Description |
|---|---|---|
length_mismatch
|
ValueError
|
If |
Source code in envrax/multi_env.py
| Python | |
|---|---|
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 | |
reset_at(idx, rng)
¶
Reset a single environment by index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
idx
|
int
|
Index of the environment to reset |
required |
rng
|
PRNGKey
|
JAX PRNG key for the reset |
required |
Returns:
| Name | Type | Description |
|---|---|---|
obs |
Array
|
Initial observation |
state |
EnvState
|
Initial state |
Source code in envrax/multi_env.py
| Python | |
|---|---|
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
step_at(idx, state, action)
¶
Step a single environment by index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
idx
|
int
|
Index of the environment to step |
required |
state
|
EnvState
|
Current state of the environment |
required |
action
|
Array
|
Action to take |
required |
Returns:
| Name | Type | Description |
|---|---|---|
obs |
Array
|
Observation after the step |
new_state |
EnvState
|
Updated state |
reward |
Array
|
Scalar reward |
done |
Array
|
Terminal flag |
info |
Dict[str, Any]
|
Info dict |
Source code in envrax/multi_env.py
| Python | |
|---|---|
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 | |
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 | |
|---|---|
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | |
envrax.multi_vec_env.MultiVecEnv
¶
Manages M heterogeneous VecEnv instances as a single unit.
Useful for holding M different VecEnvs — with potentially different
classes, configs, and shapes.
Use .class_groups to identify which indices share an inner env
class for downstream batching.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vec_envs
|
List[VecEnv]
|
List of already-constructed vectorised environments. |
required |
Source code in envrax/multi_vec_env.py
| Python | |
|---|---|
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 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 | |
num_envs
property
¶
Number of VecEnv groups (M).
total_envs
property
¶
Total number of individual environments across all groups.
vec_envs
property
¶
The inner VecEnv instances.
observation_spaces
property
¶
Per-group batched observation spaces.
action_spaces
property
¶
Per-group batched action spaces.
single_observation_spaces
property
¶
Per-group unbatched observation spaces.
single_action_spaces
property
¶
Per-group unbatched action spaces.
class_groups
property
¶
Inner env class name → list of VecEnv indices.
Useful for downstream code that wants to batch operations across groups sharing the same inner env class.
reset(rng)
¶
Reset all M vectorised environment groups with independent PRNG keys.
Each VecEnv receives one sub-key and splits it internally across
its own parallel copies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rng
|
PRNGKey
|
JAX PRNG key |
required |
Returns:
| Name | Type | Description |
|---|---|---|
observations |
List[Array]
|
Per-group batched observations |
states |
List[EnvState]
|
Per-group batched states |
Source code in envrax/multi_vec_env.py
| Python | |
|---|---|
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 | |
step(states, actions)
¶
Step all M vectorised environment groups simultaneously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
states
|
List[EnvState]
|
Per-group batched states from a previous reset or step |
required |
actions
|
List[Array]
|
Per-group batched actions |
required |
Returns:
| Name | Type | Description |
|---|---|---|
observations |
List[Array]
|
Per-group batched observations after the step |
new_states |
List[EnvState]
|
Per-group updated batched states |
rewards |
List[Array]
|
Per-group batched rewards |
dones |
List[Array]
|
Per-group batched terminal flags |
infos |
List[Dict[str, Any]]
|
Per-group batched info dicts |
Raises:
| Name | Type | Description |
|---|---|---|
length_mismatch
|
ValueError
|
If |
Source code in envrax/multi_vec_env.py
| Python | |
|---|---|
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 | |
reset_at(idx, rng)
¶
Reset a single VecEnv group by index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
idx
|
int
|
Index of the |
required |
rng
|
PRNGKey
|
JAX PRNG key |
required |
Returns:
| Name | Type | Description |
|---|---|---|
obs |
Array
|
Batched initial observations for this group |
state |
EnvState
|
Batched initial state for this group |
Source code in envrax/multi_vec_env.py
| Python | |
|---|---|
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
step_at(idx, state, action)
¶
Step a single VecEnv group by index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
idx
|
int
|
Index of the |
required |
state
|
EnvState
|
Batched state for this group |
required |
action
|
Array
|
Batched action for this group |
required |
Returns:
| Name | Type | Description |
|---|---|---|
obs |
Array
|
Batched observations after the step |
new_state |
EnvState
|
Updated batched state |
reward |
Array
|
Batched rewards |
done |
Array
|
Batched terminal flags |
info |
Dict[str, Any]
|
Batched info dict |
Source code in envrax/multi_vec_env.py
| Python | |
|---|---|
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 | |
compile(*, progress=True)
¶
Trigger XLA compilation for all inner VecEnv instances.
Calls compile() on each VecEnv, which runs a dummy
reset + step to populate the XLA cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
progress
|
bool
|
Show a |
True
|
Source code in envrax/multi_vec_env.py
| Python | |
|---|---|
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | |