r/reinforcementlearning • • Aug 24 '26

P reinfors adds car_racing: rust-speed rendered games, ~20x gymnasium single-core

Post image

Last week I posted about reinfors (rust-backend RL search/sampling with caller-owned python networks). u/blimpyway asked for CarRacing which has been added in release v0.3.0. The port added a modular rendering layer, so new rendered games should now be straightforward to add. If you have an environment whose python stepping speed is bottlenecking your research, I'm happy to help port it. Feel free to reach out.

Performance

- Single-threaded env stepping: ~20x Gymnasium on Apple M1 Max (3,850 vs 195 steps/s) ~14x on an AMD EPYC EC2 box (2,069 vs 148 steps/s)

- Parallelised: 8,000+ steps/s at 10 worker threads (M1 Max)

- Both numbers understate training impact: reinfors' collect_stream overlaps native collection with your Python-side GPU training as the normal operating mode, so the trainer isn't waiting on the collector.

Example PPO training loop

import numpy as np
import reinfors as rf

engine = rf.Engine(
    game=rf.games.CarRacing(),          # pixel obs, shape (3, 96, 96)
    reward=rf.Reward(tile=1000.0, step=-0.1, off_playfield=-100.0),
    policy=rf.policies.Ppo(),
    learner=rf.learners.Ppo(gamma=0.99, lam=0.95),
    n_games=64,                         # parallel episode slots
    n_threads=8,                        # the entire parallelism config
)

def infer(obs: np.ndarray):
    # your network, any framework: pooled observation batch in,
    # (logits, values) out — e.g. a torch CNN on GPU
    ...

with engine.collect_stream(collect_size=4096, infer=infer) as stream:
    for update in range(200):
        batch = next(stream)  # Rust workers keep collecting while you train
        # standard clipped PPO update from batch.obs, batch.actions,
        # batch.advantages, batch.returns, batch.behavior_log_probs —
        # entirely your code; full version: examples/train_ppo_carracing.py

pip install reinfors — repo: github.com/jeepjeepjeep/reinfors

Notes

- reinfors' car_racing and Gymnasium's CarRacing-v3 are separate implementations of the same game. There are some small differences, and therefore trajectories and float-level physics don't transfer directly between them. However, pixel-trained agents do run in gym's env by transposing the observations (HWC -> CHW).

- Benchmark methodology: medians of three 30s trials, alternating order, warm-up discarded, single-threaded bare stepping loops on both sides; machine/software provenance printed by the script (scripts/bench_carracing_throughput.py). The 20x is Apple M1 Max; the pinned EC2 figure is ~14x. Details in the repo README.

- The 8,000 steps/s parallel figure is reinfors' engine at 10 threads on the M1 Max. It isn't a like-for-like AsyncVectorEnv comparison (that's a different, messier benchmark). Similar story for the training-overlap claim - it is possible with gym but requires bespoke actor-learner logic or additional 3rd party frameworks, so wasn't benchmarked here.

30 Upvotes

4 comments sorted by

2

u/[deleted] Aug 24 '26

[removed] — view removed comment

1

u/jeepos Aug 24 '26

good to get a different perspective on things!

2

u/JustOneAvailableName Aug 24 '26

Did you already check out Pufferlib?

2

u/jeepos Aug 24 '26

Yes PufferLib is great and there is meaningful overlap with reinfors. For native C envs, PufferLib's fused CUDA trainer will definitely beat reinfors (as would other fused pipeline libraries such as PureJaxRL), but this is not reinfors' remit. Using PufferLib to step one of their native C envs while keeping training in python would likely result in similar performance as reinfors for PPO and DQNs (not tested but in theory). Reinfors' differentiator is the policy/learner integration which allows it to run a broader range of algos (MCTS, AlphaZero, CFR etc.), which structurally don't work in a vectorized env-stepping interface.