Skip to main content

flashrl

flashrl does RL with millions of steps/second 💨 while being tiny: ~200 lines of code

🛠️ pip install flashrl or clone the repo & pip install -r requirements.txt

  • If cloned (or if envs changed), compile: python setup.py build_ext --inplace

💡 flashrl will always be tiny: Read the code (+paste into LLM) to understand it!

Quick Start 🚀

flashrl uses a Learner that holds an env and a model (default: Policy with LSTM)

import flashrl as frl

learn = frl.Learner(frl.envs.Pong(n_agents=2**14))
curves = learn.fit(40, steps=16, desc='done')
frl.print_curve(curves['loss'], label='loss')
frl.play(learn.env, learn.model, fps=8)
learn.env.close()

.fit does RL with ~10 million steps: 40 iterations × 16 steps × 2**14 agents!

Run it yourself via python train.py and play against the AI 🪄

Click here, to read a tiny doc 📑

Learner takes the arguments

  • env: RL environment
  • model: A Policy model
  • device: Per default picks mps or cuda if available else cpu
  • dtype: Per default torch.bfloat16 if device is cuda else torch.float32
  • compile_no_lstm: Speedup via torch.compile if model has no lstm
  • **kwargs: Passed to the Policy, e.g. hidden_size or lstm

Learner.fit takes the arguments

  • iters: Number of iterations
  • steps: Number of steps in rollout
  • desc: Progress bar description (e.g. 'reward')
  • log: If True, tensorboard logging is enabled
    • run tensorboard --logdir=runsand visit http://localhost:6006 in the browser!
  • stop_func: Function that stops training if it returns True e.g.
...
def stop(kl, **kwargs):
  return kl > .1

curves = learn.fit(40, steps=16, stop_func=stop)
...
  • lr, anneal_lr & args of ppo after bs: Hyperparameters

The most important functions in flashrl/utils.py are

  • print_curve: Visualizes the loss across the iters
  • play: Plays the environment in the terminal and takes
    • model: A Policy model
    • playable: If True, allows you to act (or decide to let the model act)
    • steps: Number of steps
    • fps: Frames per second
    • obs: Argument of the env that should be rendered as observations
    • dump: If True, no frame refresh -> Frames accumulate in the terminal
    • idx: Agent index between 0 and n_agents (default: 0)

Environments 🕹️

Each env is one Cython(=.pyx) file in flashrl/envs. That's it!

To add custom envs, use grid.pyx, pong.pyx or multigrid.pyx as a template:

  • grid.pyx for single-agent envs (~110 LOC)
  • pong.pyx for 1 vs 1 agent envs (~150 LOC)
  • multigrid.pyx for multi-agent envs (~190 LOC)
Grid Pong MultiGrid
Agent must reach goal Agent must score Agent must reach goal first
grid pong multigrid

Acknowledgements 🙌

I want to thank

and last but not least...

Release files for flashrl 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for flashrl 0.2.1
File
flashrl-0.2.1-cp313-cp313-win_amd64.whl CPython 3.13 CPython 3.13 Windows x86-64 Details
flashrl-0.2.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.17+ x86-64 Details
flashrl-0.2.1-cp313-cp313-macosx_10_13_universal2.whl CPython 3.13 CPython 3.13 macOS 10.13+ universal2 (ARM64, x86-64) Details
flashrl-0.2.1-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details
flashrl-0.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.17+ x86-64 Details
flashrl-0.2.1-cp312-cp312-macosx_10_13_universal2.whl CPython 3.12 CPython 3.12 macOS 10.13+ universal2 (ARM64, x86-64) Details
flashrl-0.2.1-cp311-cp311-win_amd64.whl CPython 3.11 CPython 3.11 Windows x86-64 Details
flashrl-0.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.17+ x86-64 Details
flashrl-0.2.1-cp311-cp311-macosx_10_9_universal2.whl CPython 3.11 CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64) Details
flashrl-0.2.1-cp310-cp310-win_amd64.whl CPython 3.10 CPython 3.10 Windows x86-64 Details
flashrl-0.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.17+ x86-64 Details
flashrl-0.2.1-cp310-cp310-macosx_10_9_universal2.whl CPython 3.10 CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64) Details

Total release size: 13.2 MB

Release files / flashrl-0.2.1-cp313-cp313-win_amd64.whl

Download URL flashrl-0.2.1-cp313-cp313-win_amd64.whl
Size 694.6 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
56ff9314164007fbf92d88bc41e595b3a92239ac84d005e44a0b477d1609016b
BLAKE2b-256 checksum
How to use checksums
1831a94be16246bd695bc1a55de8bddeb44268edf5c81ad6623f659e69eaa87b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.3

Release files / flashrl-0.2.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL flashrl-0.2.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 1.7 MB
Tags CPython 3.13 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
f9af705b7b1662a3d5ab3b8e1b69d8ae16747020fc753b90f5b5ec1a0e87924a
BLAKE2b-256 checksum
How to use checksums
cc5aa28e3af1e200199d857056696296088c04eceac536a8b23a8cd39ed80861
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.3

Release files / flashrl-0.2.1-cp313-cp313-macosx_10_13_universal2.whl

Download URL flashrl-0.2.1-cp313-cp313-macosx_10_13_universal2.whl
Size 961.5 kB
Tags CPython 3.13 macOS 10.13+ universal2 (ARM64, x86-64)
SHA-256 checksum
How to use checksums
d24bc5ee9a7409a96479e1acb23cfae78855342e9be6dbbfbfb066a93a705a3e
BLAKE2b-256 checksum
How to use checksums
f3243e84a6dee6e6bf888b2768fdb56a738e65f856d3c877dc3510b74cc26229
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.3

Release files / flashrl-0.2.1-cp312-cp312-win_amd64.whl

Download URL flashrl-0.2.1-cp312-cp312-win_amd64.whl
Size 694.8 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
d9030ffa184d85e8211d61bb00c07904c4d95a50572e8f32f05e6fe72b900f98
BLAKE2b-256 checksum
How to use checksums
21b5491ab3bab7f72680b2c30a4314919bf896c4f2e8ed720432c5ed4d5ffded
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.3

Release files / flashrl-0.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL flashrl-0.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 1.7 MB
Tags CPython 3.12 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
04bd1cbc528ce8e7d12da12fdd3a7871835a704c60e0294aa81ed98b8a51a6a4
BLAKE2b-256 checksum
How to use checksums
9cd5d0b38ef177e560dc8686d41a1b790b7a9bdb24fbf2e25d9439b671719186
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.3

Release files / flashrl-0.2.1-cp312-cp312-macosx_10_13_universal2.whl

Download URL flashrl-0.2.1-cp312-cp312-macosx_10_13_universal2.whl
Size 965.7 kB
Tags CPython 3.12 macOS 10.13+ universal2 (ARM64, x86-64)
SHA-256 checksum
How to use checksums
39b1d39387b4974d72796f111771f2e2aff68d5da432a936b25b53384eda671d
BLAKE2b-256 checksum
How to use checksums
131a032237c0b26ab9391438dfbe69a7d6ab05bdd9cc839a36b91fd234d30834
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.3

Release files / flashrl-0.2.1-cp311-cp311-win_amd64.whl

Download URL flashrl-0.2.1-cp311-cp311-win_amd64.whl
Size 689.1 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
2df43113f20e8cb61bf691d8abef464d59f8aa81be65f52516ffb306a8ea005c
BLAKE2b-256 checksum
How to use checksums
507b82a23af23407008cbde43a645e723abc230d2a866136414abfc654fedb4e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.3

Release files / flashrl-0.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL flashrl-0.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 1.6 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
88ea838f4c41eb16eda2f153dc16e6d6b3d0ea596c27432fdf971f51f390e9aa
BLAKE2b-256 checksum
How to use checksums
47d066845654e40e38d995f5195f2b9a1296df8899e0fa4b87dbb8da40bb11e8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.3

Release files / flashrl-0.2.1-cp311-cp311-macosx_10_9_universal2.whl

Download URL flashrl-0.2.1-cp311-cp311-macosx_10_9_universal2.whl
Size 960.7 kB
Tags CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64)
SHA-256 checksum
How to use checksums
d6ca678c31ce7d1b443117c8ba78d2249fc1bfa00155d76a6a6cdd00a8ce7555
BLAKE2b-256 checksum
How to use checksums
38a39d24be919239b82b7c02a7fc35b2b0e3c594dbceabda3241465bfb00eeed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.3

Release files / flashrl-0.2.1-cp310-cp310-win_amd64.whl

Download URL flashrl-0.2.1-cp310-cp310-win_amd64.whl
Size 689.0 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
2efbb5759d76b8ab116061c19633ef0a2b371634c27c839c954e147d100d85ca
BLAKE2b-256 checksum
How to use checksums
d139d1cf206ab47aa3974143a0031e8281af7f254ae50400bb6d828253be5bc8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.3

Release files / flashrl-0.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL flashrl-0.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 1.6 MB
Tags CPython 3.10 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
ebe75e4cd4838ca3c248485837703a3b19223ad8fa6654bc00a9b0fef60dbacf
BLAKE2b-256 checksum
How to use checksums
aea22fce780b2bdae5ebcb9e47ef1d3a5788a1a0b7ec3ccfe5c1de0d6b934fdd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.3

Release files / flashrl-0.2.1-cp310-cp310-macosx_10_9_universal2.whl

Download URL flashrl-0.2.1-cp310-cp310-macosx_10_9_universal2.whl
Size 960.0 kB
Tags CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64)
SHA-256 checksum
How to use checksums
2c41e7d2df4df5845d3a14e95748a7809959f8e329bb6c93166b5c8f14b8ed1b
BLAKE2b-256 checksum
How to use checksums
f6e23459be0242e75d4d687e48037024c2d3294fb84a13401d3fd609bf6b10e9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.2.1 This release

12 release files

0.2.0

12 release files

0.1.2

12 release files

0.1.1

12 release files

0.0.5

4 release files

0.0.4

4 release files

0.0.3

4 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page