PufferLib Documentation
repository·4.0·Indexed 24 days ago
https://github.com/pufferai/pufferlibA fast reinforcement learning library designed for training small, superhuman models. PufferLib provides integrated learning algorithms, hyperparameter tuning, and simulation methods. It includes specialized environment support for NetHack (via fast-nle), Overcooked (with configurable layouts and reward systems), and Craftax (featuring a native C implementation transition for high-performance step execution).
What's inside PufferLib
- PufferLib is a high-performance reinforcement learning library designed to train small, superhuman models rapidly. It includes custom learning algorithms, hyperparameter tuning, and simulation methods developed through internal research. The library is open-source and optimized for speed and usability in reinforcement learning workflows.
Understand the Craftax Ocean Environment Implementation
4.0The
ocean/craftax/environment is a full Craftax Ocean implementation with a symbolic observation size of8268and an action count of43.Current Architecture:
- Reset: The
c_resetfunction is implemented natively in C. It generates the full 9-floor world state (including maps, item maps, mob maps, light maps, ladders, chest flags, and scalar reset fields) and encodes them into a flat symbolic observation. - Step: The
c_stepfunction is currently a Python/JAX proxy. It acquires the Python GIL, calls the JAXCraftax-Symbolic-v1implementation, and copies the resulting float32 observation, reward, terminal flag, and terminal achievement log into PufferLib-owned buffers. - State Alignment: After a native reset, the first delegated step performs an internal proxy reset to ensure the JAX-owned step state remains aligned with the native reset observation.
Note on Performance: Because
c_stepstill allocates through Python/JAX and serializes on the GIL, training throughput is currently limited and not yet optimized for high-performance targets.- Reset: The
Set JAX Platform for Craftax Environments
4.0To avoid conflicts with shared GPUs when running environment steps, ensure theJAX_PLATFORM_NAMEenvironment variable is set tocpubefore importing JAX.Verify Craftax parity with the stress battery
4.0To ensure correctness between JAX and native implementations, use the stress battery in
tests/craftax_parity_stress.py. The battery executes the following configurations:- 64 seeds $\times$ 10,000 steps with
mixedpolicy. - 16 seeds $\times$ 30,000 steps with
descendpolicy. - 32 seeds $\times$ 5,000 steps with
suicidepolicy. - 16 seeds $\times$ 5,000 steps with
combatpolicy.
All stress cases use
atol=1e-5for observations and rewards and require exact terminal matching.- 64 seeds $\times$ 10,000 steps with
Watch a NetHack policy
4.0To watch a trained policy, first build the demo binary using the
--fastflag, then run the./nethackbinary.To play specific weights, set the
NH_WEIGHTSenvironment variable to the path of your checkpoint file.Binary Arguments:
[steps]: Number of steps to run.[ms_per_frame]: Milliseconds per frame. Setting this to0runs the environment headless.
Environment Variables:
NH_WEIGHTS: Path to the weights file (e.g.,checkpoints/nethack/<run>/<step>.bin).NH_SEED: Set this to replay a specific run.
Example usage:
./build.sh nethack --fast ./nethack NH_WEIGHTS=checkpoints/nethack/<run>/<step>.bin ./nethackUnderstand the Craftax Native Step Integration Roadmap
4.0The Craftax environment is transitioning from a Python/JAX proxy-based step execution to a native C implementation (
c_step). This transition is being done via standalone subsystem ports that are verified against JAX for parity before being integrated.Current Status (as of 2026-04-18):
- Completed: Native reset (PRNG, noise, world gen), simple subsystems, medium subsystems, crafting/placement subsystems, and the
do_actionsubsystem. - Remaining Tasks:
- Native ports for
update_mobsandspawn_mobs. - Native bookkeeping for rewards, terminal states, timesteps, light-levels, RNG, and achievement-deltas.
- Integration of all standalone ports into a single native
c_stepbehind an explicit switch. - Removal of the Python/JAX proxy from the normal step path.
- Restoring production vector sizes in
config/ocean/craftax.ini.
- Native ports for
Note: Until the integration is complete, the live Ocean environment still delegates steps to the Python/JAX proxy, and rendering remains a no-op.
- Completed: Native reset (PRNG, noise, world gen), simple subsystems, medium subsystems, crafting/placement subsystems, and the
Train a NetHack policy
4.0Train a policy for the NetHack environment using the
puffer traincommand. Reward coefficients and hyperparameters are configured inconfig/nethack.ini.puffer train nethackUse deterministic action policies in the Craftax parity harness
4.0The standalone parity harness (
tests/craftax_parity.py) supports several deterministic action policies for testing beyond uniform random exploration:uniform: Original random action stream.combat: Biases towardDO, arrows, fireballs, and iceballs when meaningful; otherwise moves toward live mobs.descend: Uses mirrored state to push toward down ladders, clear blocked levels via combat, and exercise placement/crafting.suicide: Steers into lava, water, mob-occupied, or projectile-heavy danger.boss: Warm up with downward navigation, then repeatedly attempts descent while routing toward ladders.mixed: Round-robins the above policies every 500 steps.
When a divergence is detected, the harness reports the policy, seed, step, action, reward delta, terminal delta, first symbolic-observation field, suspected subsystem, and the last 10 actions.
Build and Run the Overcooked Environment
4.0To use the Overcooked environment, you must first build it using the provided setup script, then you can run tests or standalone demos.
Build
python setup.py build_overcooked --inplaceRun Tests
python pufferlib/ocean/overcooked/overcooked.pyRun Standalone Demos
Run the
./overcookedexecutable followed by the layout name:./overcooked cramped_room ./overcooked asymmetric_advantages ./overcooked forced_coordination ./overcooked coordination_ring ./overcooked counter_circuit# Build the environment python setup.py build_overcooked --inplace # Run standalone test python pufferlib/ocean/overcooked/overcooked.py # Run standalone demo with specific layout ./overcooked cramped_room ./overcooked asymmetric_advantages ./overcooked forced_coordination ./overcooked coordination_ring ./overcooked counter_circuitSetup the NetHack environment
4.0To use the NetHack environment, install the current package in editable mode and run the build script to clone and build the
fast-nlevendor and the training backend.Note: Run these commands from the repository root. The engine expects data at
vendor/fast-nle/build/dat. You can override this location by setting theNETHACKDIRenvironment variable.pip install -e . ./build.sh nethackConfigure the Overcooked Environment
4.0Initialize the
Overcookedenvironment using the Python wrapper. You can specify the number of parallel environments, the layout, the number of agents, and various reward configurations to customize the training signal.Key configuration parameters include:
num_envs: Number of parallel environments.layout: The name of the map layout (e.g.,cramped_room,asymmetric_advantages).num_agents: Number of agents per environment.render_mode: Set to enable rendering.reward_*: Various keys to adjust the weight of main and intermediate rewards (e.g.,reward_dish_served_whole_team,reward_pot_started,reward_ingredient_picked).
env = Overcooked( num_envs=1, # Number of parallel environments layout="cramped_room", # Layout name (see Available Layouts) num_agents=2, # Agents per environment render_mode=None, # Set to enable rendering log_interval=128, # Steps between log aggregation grid_size=32, # Render tile size in pixels # Reward configuration (from config/ocean/overcooked.ini) reward_dish_served_whole_team=1.0, # Shared reward for correct dish reward_dish_served_agent=0.0, # Bonus for serving agent reward_pot_started=0.15, # Starting correct recipe reward_ingredient_added=0.15, # Adding onion to pot reward_ingredient_picked=0.05, # Picking up ingredient reward_soup_plated=0.20, # Plating cooked soup reward_wrong_dish_served=0.0, # Serving incorrect dish reward_step_penalty=0.0, # Per-step penalty )Configure Craftax Environment via INI
4.0The environment configuration is managed via
config/ocean/craftax.ini.Warning: Currently, this configuration uses a small proxy-friendly vector size to accommodate the Python/JAX step proxy. Once the step logic is fully ported to native C, this vector size should be increased.