Vector execution: autoreset modes change which rows are transitions
Trace pinned Gymnasium vector reset semantics, final observations, reset-only slots, and per-environment recurrent state.
On this page
The question for today
What you’ll understand
- Distinguish SAME_STEP and NEXT_STEP autoreset outputs in Gymnasium 1.1.1.
- Recover final observations using the correct info masks.
- Exclude reset-only slots from transition and learner accounting.
A vector step returns batched arrays, but not every returned observation necessarily belongs to the same kind of event. Gymnasium 1.1.1 supports next-step, same-step, and disabled autoreset modes, and algorithm or wrapper compatibility must be checked. We use its pinned SyncVectorEnv implementation for exact behavior. A parallel vector executor can have different scheduling costs, and another release or wrapper can use different final-observation fields. Store the selected reset mode with the rollout schema instead of guessing it from tensor shapes.
The ordinary observation slot depends on reset mode
In the pinned SAME_STEP mode, an ending environment first produces its final transition, then immediately resets. The returned ordinary observation for that slot is the new initial observation, while final_obs and final_info carry the old ending transition’s data in the information dictionary. Vector info entries have associated presence masks, including _final_obs, so a collector must check whether a field exists for each environment rather than interpret an unfilled object-array entry as a valid observation. Rewards and ending flags still describe the action that ended the old episode.
In NEXT_STEP mode, the ending step returns its actual final observation. On the following vector call, that slot resets instead of executing the supplied action and returns zero reward with false ending flags. That reset-only output must not become a fabricated zero-reward transition from the old final state. Other vector slots may execute real actions during the same call. DISABLED mode instead requires correctly resetting ended environments through the supported interface before stepping them again. These distinctions affect sample counts, hidden-state handling, and bootstrap inputs.
Separate value continuation from sequence continuation
At an externally truncated transition, a continuing-task value can bootstrap from the old final observation. The subsequent reset episode must still begin a new sequence with its appropriate recurrent state. A value mask that retains truncation bootstrapping therefore cannot also serve blindly as the recurrent or trace-continuation mask. At a collector-only fragment boundary, similarly retain a suitable tail value while stopping the local backward recursion. Preserve which boundary was encountered so that a sequence assembler cannot join unrelated episodes simply because their numeric environment-slot indices match.
A recurrent critic may need the old trajectory’s hidden context to value the final observation, while the next episode needs reset state. Evaluate or store those contexts before overwriting them. A collector that resets every hidden state whenever any slot ends also corrupts the continuing environments in the batch. Test mixed batches containing ordinary, terminated, truncated, and reset-only slots, with clearly tagged episode identifiers. Correct vectorization is a per-slot state machine, not a global Boolean reset applied to the entire batch.
Measure useful transitions rather than raw vector calls
Vectorization can amortize policy inference while introducing a slowest-slot barrier, reset costs, serialization, or interprocess overhead. A serial SyncVectorEnv does not parallelize simulator execution merely because observations are batched. Async execution can overlap independent work but still needs correct action-to-observation matching and bounded memory. Count valid action-executing transitions and completed episodes separately from vector calls. Otherwise a next-step autoreset configuration can appear to collect more training data simply by counting reset-only outputs as experiences.
Reset the probability lab, explicitly set p=0.75 and n=20, predict the mean valid-slot count in a hypothetical independent binary model, then Play or Step. Compare a separate Reset with p=0.5 and n=20 explicitly set. The means are fifteen and ten. Real vector reset events depend on episode lengths and are generally not independent identical trials. Playback highlights exact binomial outcomes, not actual environments or sampled resets; this toy model only distinguishes total slots from assumed useful-transition counts.
Work it through
The old final observation must survive same-step reset
In a SAME_STEP two-slot fixture, slot A externally truncates with reward one, final-observation value five, and reset-observation value 100. Slot B continues with reward two and next value three. Use discount 0.9 and valid continuing-task value estimates.
For slot A, retrieve the old final observation using the pinned final_obs field and its _final_obs presence mask. The ordinary returned observation is the reset observation and is not the bootstrap input for this transition.
Slot A’s target is 1+0.9×5=5.5. Substituting the reset value would incorrectly give 91. Stop its trace before the new episode while retaining the permitted final-state bootstrap.
Slot B’s target is 2+0.9×3=4.7, using its ordinary continuing next observation. Its hidden state and episode identity continue independently from slot A’s reset.
The correct targets are 5.5 and 4.7. A single batched observation tensor contains different event semantics that the collector must resolve per slot.
The living notebook
Explore the model
Interactive lab · probability
Useful slots are a stated counting hypothesis
Use independent Reset → explicitly set BOTH p and n → predict → Play/Step for (.75,20) and (.5,20), giving means fifteen and ten. The lab assumes independent Bernoulli slots and has no vector environment, reset mode, hidden state, or final-observation adapter.
- Mean · np
- 5
- Variance · np(1−p)
- 2.5
- Total probability
- 1
These are exact binomial probabilities for independent trials with one fixed success probability. Playback tours the distribution; it does not resample outcomes. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forA vector-call count is not automatically a valid-transition count.
Pause & reason
Check your understanding
Your turn
Try it yourself
A NEXT_STEP vector call has eight slots, two of which perform reset-only work. How many valid action transitions should enter the learner? If a truncated old transition had reward two, discount 0.8, and final value four, what is its continuation target?
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Count actual action execution, then bootstrap from the old final observation rather than a reset-only output.
Compare with the solution
Six valid transitions enter the learner from that call. The old truncated transition’s target is 2+0.8×4=5.2 under the continuing-task assumptions. Its trace cannot run into the reset episode. Preserve per-slot episode identity and hidden-state handling, and do not infer valid transition count from the eight-element return shape. Another vector implementation or reset mode must be checked against its own pinned schema and semantics.
Keep the idea
Take it with you
- Pin autoreset mode and recover final observations with presence masks.
- Use different masks for value bootstrap and sequence continuation.
- Count actual transitions and maintain state independently for each environment slot.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.