Learn how to use PufferLib with Gym and Gymnasium environments
PufferLib provides seamless integration with both legacy Gym and modern Gymnasium environments. This guide shows you how to get started with basic environment usage, vectorization, and rendering.
Gymnasium is the modern successor to OpenAI Gym. PufferLib makes it easy to wrap Gymnasium environments into the PufferEnv format for high-performance training.
The GymnasiumPufferEnv wrapper converts your Gymnasium environment into a PufferEnv that supports vectorized operations and efficient batch processing.
PufferLib’s vectorization capabilities allow you to run multiple environments in parallel for faster data collection. The library supports multiple backends for different use cases.
For production training, use the multiprocessing backend to run environments in parallel across multiple CPU cores.
vecenv = pufferlib.vector.make( SamplePufferEnv, num_envs=2, num_workers=2, batch_size=1, backend=pufferlib.vector.Multiprocessing)# Asynchronous API for maximum throughputvecenv.async_reset()o, r, d, t, i, env_ids, masks = vecenv.recv()actions = vecenv.action_space.sample()print('Actions:', actions)vecenv.send(actions)# New observations are ready while other envs run in the backgroundo, r, d, t, i, env_ids, masks = vecenv.recv()print('Observations:', o)vecenv.close()
Make sure num_envs divides num_workers, and both should divide batch_size evenly. PufferLib will raise an APIUsageError if these constraints are violated.
PufferLib environments support rendering for visualization and debugging. Here’s an example using the built-in Breakout environment:
from pufferlib.ocean.breakout import breakoutenv = breakout.Breakout()env.reset()while True: env.step(env.action_space.sample()) frame = env.render() # Display or save the frame as needed
The render() method returns an RGB array that you can display using your preferred visualization library (matplotlib, OpenCV, etc.).