Skip to content

Add HistoryWrapper to stack past observations and actions - #686

Open
MaxonT wants to merge 1 commit into
google:mainfrom
MaxonT:contrib/514-auto
Open

MaxonT wants to merge 1 commit into
google:mainfrom
MaxonT:contrib/514-auto

Conversation

@MaxonT

@MaxonT MaxonT commented Nov 28, 2026

Copy link
Copy Markdown

A wrapper that keeps a context window of past observations and actions is what the feature request for brax asks for, and the patch for it is attached for you to look over.

Fixes #514

Thanks to erikfrey for marking this as a good first issue; it made it easy to pick up. This adds HistoryWrapper(env, context_length) to brax/envs/wrappers/training.py.

The wrapped observation is the flattened last context_length pairs of (observation, action just taken), oldest first. The pair returned by reset has a zero action, and steps before the start of the episode are zero padded. observation_size becomes context_length * (observation_size + action_size).

I kept the history inside state.obs rather than in state.info. That way AutoResetWrapper, which swaps state.obs for the stored reset observation when an episode ends, also resets the history, and nothing from the previous episode leaks into the next one. The consequence is that the wrapper goes on the base env, before training.wrap (or VmapWrapper), and it only supports a flat observation: a dict observation raises a ValueError. It works on batched states too, since it only indexes the last two axes.

Tests added in training_test.py: the unbatched case (shapes, zero padding, ordering of the stored actions, dropping the oldest frame), a batched run through training.wrap with a short episode that checks the history is reset after auto reset, and the context_length < 1 error. They pass (4 passed in training_test.py, ant env, macOS, CPU jax).

Not verified: the rest of the test suite, GPU/TPU, and training an agent on top of the wrapped env. I have not exported it from brax.envs or wired it into envs.create; happy to do that if you would prefer.

Signed-off-by: Ym <173550318+MaxonT@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] Env Wrapper for specifying a context length of past obs + actions

1 participant