Skip to main content

Minimal RL in Rust

Project description

TwisteRL

TwisteRL

A minimalistic, high-performance Reinforcement Learning framework implemented in Rust.

The current version is a Proof of Concept, stay tuned for future releases!

Install

pip install .

Use

Training

python -m twisterl.train --config examples/ppo_puzzle8_v1.json

This example trains a model to play the popular "8 puzzle":

|8|7|5|
|3|2| |
|4|6|1|

where numbers have to be shifted around through the empty slot until they are in order.

This model can be trained on a single CPU in under 1 minute (no GPU required!). A larger version (4x4) is available: examples/ppo_puzzle15_v1.json.

Inference

Check the notebook example here!

Creating your own environment

The examples/grid_world custom environment example here shows how to implement an environment in Rust and expose it to Python with PyO3. You can use it as a template:

  1. Create a new crate

    cargo new --lib examples/my_env
    
  2. Add dependencies in examples/my_env/Cargo.toml:

    [package]
    name = "my_env"
    version = "0.1.0"
    edition = "2021"
    
    [lib]
    name = "my_env"
    crate-type = ["cdylib"]
    
    [dependencies]
    pyo3 = { version = "0.20", features = ["extension-module"] }
    twisterl = { path = "path/to/twisterl/rust", features = ["python_bindings"] }
    # Or using the official crate:
    # twisterl = { version = "a.b.c", features = ["python_bindings"] }
    
  3. Implement the environment by defining a struct and implementing twisterl::rl::env::Env for it. Provide logic for reset, step, observe, reward, etc.

In inference, twisterRL algorithms track the actions applied to the environment externally. If you need the environment itself to track them, implement the track_solution and solution methods in the Env trait.

  1. Expose it to Python using PyBaseEnv:

    use pyo3::prelude::*;
    use twisterl::python_interface::env::PyBaseEnv;
    
    #[pyclass(name = "MyEnv", extends = PyBaseEnv)]
    struct PyMyEnv;
    
    #[pymethods]
    impl PyMyEnv {
        #[new]
        fn new(...) -> (Self, PyBaseEnv) {
            let env = MyEnv::new(...);
            (PyMyEnv, PyBaseEnv { env: Box::new(env) })
        }
    }
    
  2. Add a pyproject.toml describing the Python package so maturin can build a wheel.

  3. Build and install the module:

    pip install .
    
  4. Use it from Python:

    import my_env
    env = my_env.MyEnv(...)
    obs = env.reset()
    

Refer to grid_world for a complete working example.

Checkpoint Format

TwisteRL uses safetensors as the default checkpoint format for model weights. Safetensors provides:

  • Security: No arbitrary code execution (unlike pickle-based .pt files)
  • Speed: Zero-copy loading for faster model initialization
  • HuggingFace compatibility: Standard format for Hub models

Legacy .pt checkpoints are still supported for backward compatibility but will log a warning. To convert existing checkpoints:

from twisterl.utils import convert_pt_to_safetensors

convert_pt_to_safetensors("model.pt")  # Creates model.safetensors

Documentation

🚀 Key Features

  • High-Performance Core: RL episode loop implemented in Rust for faster training and inference
  • Inference-Ready: Easy compilation and bundling of models with environments into portable binaries for inference
  • Modular Design: Support for multiple algorithms (PPO, AlphaZero) with interchangeable training and inference
  • Language Interoperability: Core in Rust with Python interface
  • Symmetry-Aware Training via Twists: Environments can expose observation/action permutations (“twists”) so policies automatically exploit device or puzzle symmetries for faster learning.

🏗️ Current State (PoC)

  • Hybrid rust-python implementation:
    • Data collection and inference in Rust
    • Training in Python (PyTorch)
  • Supported algorithms:
    • PPO (Proximal Policy Optimization)
    • AlphaZero
  • Focus on discrete observation and action spaces
  • Support for native Rust environments and for Python environments through a wrapper

🚧 Roadmap

Upcoming Features (Alpha Version)

  • Full training in Rust
  • Extended support for:
    • Continuous observation spaces
    • Continuous action spaces
    • Custom policy architectures
  • Native WebAssembly environment support
  • Streamlined policy+environment bundle export to WebAssembly
  • Comprehensive Python interface
  • Enhanced documentation and test coverage

💎 Future Possibilities

  • WebAssembly environment repository
  • Browser-based environment and agent visualization
  • Interactive web demonstrations
  • Serverless distributed training

🎮 Use Cases

Currently used in:

Perfect for:

  • Puzzle-like optimization problems
  • Any scenario requiring fast, production performance RL inference

🔧 Current Limitations

  • Limited to discrete observation and action spaces
  • Python environments may create performance bottlenecks
  • Documentation and testing coverage is currently minimal
  • WebAssembly support is in development

🤝 Contributing

We're in early development stages and welcome contributions! Stay tuned for more detailed contribution guidelines.

📄 Note

This project is currently in PoC stage. While functional, it's under active development and the API may change significantly.

📜 License

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

twisterl-0.5.1-cp314-cp314-win_amd64.whl (317.9 kB view details)

Uploaded CPython 3.14Windows x86-64

twisterl-0.5.1-cp314-cp314-manylinux_2_34_x86_64.whl (455.5 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.34+ x86-64

twisterl-0.5.1-cp314-cp314-macosx_11_0_arm64.whl (412.4 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

twisterl-0.5.1-cp313-cp313-win_amd64.whl (320.3 kB view details)

Uploaded CPython 3.13Windows x86-64

twisterl-0.5.1-cp313-cp313-manylinux_2_34_x86_64.whl (459.4 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.34+ x86-64

twisterl-0.5.1-cp313-cp313-macosx_11_0_arm64.whl (417.3 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

twisterl-0.5.1-cp312-cp312-win_amd64.whl (320.5 kB view details)

Uploaded CPython 3.12Windows x86-64

twisterl-0.5.1-cp312-cp312-manylinux_2_34_x86_64.whl (459.4 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

twisterl-0.5.1-cp312-cp312-macosx_11_0_arm64.whl (417.1 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

twisterl-0.5.1-cp312-cp312-macosx_10_12_x86_64.whl (433.4 kB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

twisterl-0.5.1-cp311-cp311-win_amd64.whl (316.8 kB view details)

Uploaded CPython 3.11Windows x86-64

twisterl-0.5.1-cp311-cp311-manylinux_2_34_x86_64.whl (455.9 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.34+ x86-64

twisterl-0.5.1-cp311-cp311-macosx_11_0_arm64.whl (413.3 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

twisterl-0.5.1-cp311-cp311-macosx_10_12_x86_64.whl (428.0 kB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

twisterl-0.5.1-cp310-cp310-win_amd64.whl (315.8 kB view details)

Uploaded CPython 3.10Windows x86-64

twisterl-0.5.1-cp310-cp310-manylinux_2_34_x86_64.whl (455.0 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.34+ x86-64

twisterl-0.5.1-cp310-cp310-macosx_11_0_arm64.whl (413.1 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

twisterl-0.5.1-cp310-cp310-macosx_10_12_x86_64.whl (428.0 kB view details)

Uploaded CPython 3.10macOS 10.12+ x86-64

File details

Details for the file twisterl-0.5.1-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: twisterl-0.5.1-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 317.9 kB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for twisterl-0.5.1-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 e6d6014b1f4865ced40d6b3c02b61b26c84bfd31e3b0a5c08686c3b08cd40f9d
MD5 38965935fc6194abbefd6555ca151e03
BLAKE2b-256 c606e264c04acb0590f289903207624b71e66002173881c62d896069fdd50b35

See more details on using hashes here.

File details

Details for the file twisterl-0.5.1-cp314-cp314-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for twisterl-0.5.1-cp314-cp314-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 c8b93afd0ec343f41d30e0a19981e18221e0a82c718254530d72cf600ad685bf
MD5 c7f395f35d9c0595c72718efbffa8bc5
BLAKE2b-256 94e69440622e1f6a7a3e0914f80d6b3b0784ffc435b34079d362848841ea197b

See more details on using hashes here.

File details

Details for the file twisterl-0.5.1-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for twisterl-0.5.1-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1517cf089d7214bf73e3c88f4fac5c52c0e99cbddf78fc5577a0c94f00453a79
MD5 43649f3e8eb2d6c8b6895cda328a5ed9
BLAKE2b-256 1cf1ca13e971cac1ccafbdbecfab1bd006de4f4ab4fe9c1e9142b73b5b3c9b65

See more details on using hashes here.

File details

Details for the file twisterl-0.5.1-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: twisterl-0.5.1-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 320.3 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for twisterl-0.5.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 3ca81767eadc492480d5ce1202f9020bce204db4512eb84335b50a387a1f5318
MD5 5fa235021f286aab1a2a82da2fe68f94
BLAKE2b-256 6d948bab31443bdccf5fd5b4932846a31c2763004eeecc1c3c49f609ae0bd549

See more details on using hashes here.

File details

Details for the file twisterl-0.5.1-cp313-cp313-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for twisterl-0.5.1-cp313-cp313-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 f91a5558e5bd3a2806b79baf7ddadf42bb833fd7a59f6966131019298985f7fd
MD5 8284a53576b7a61fd6de8f179db3e55a
BLAKE2b-256 408992c6962be9f31876c5f0c48767c9490da0cf7a87a61675afa4925702db0b

See more details on using hashes here.

File details

Details for the file twisterl-0.5.1-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for twisterl-0.5.1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 fa7c3f66858aabed02c22f064556e83fe15dd725fe2d231a11e6bdf153273a4c
MD5 cc45000d4a07a413f53424185e3bd458
BLAKE2b-256 f7fa1957aab3afdd7ae2047244c6cee8e96c0ed386523c0435bf0e14fbd5b4cc

See more details on using hashes here.

File details

Details for the file twisterl-0.5.1-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: twisterl-0.5.1-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 320.5 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for twisterl-0.5.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 5506b263f0bee9d2b7f9a600f301f0a5caf5030c2c2ad163dc56866d14b9366a
MD5 d6df199e8b4fef2bf62ea32799a34212
BLAKE2b-256 05a160bf2f1eef5376a4e16570aa60ba694796a90b58fa8abcc38f68b2fc63c3

See more details on using hashes here.

File details

Details for the file twisterl-0.5.1-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for twisterl-0.5.1-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 3a504cad5394cc3f4781be8488e696fb7053a816e1741e46ea9d257cf70dc7b4
MD5 3d5f05197f6f7a29896447c0e8b6f264
BLAKE2b-256 8bc5a94f8dc86c3e3c737e7f2de904fea25feb5a68a3ed6444d8697b37c925ac

See more details on using hashes here.

File details

Details for the file twisterl-0.5.1-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for twisterl-0.5.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9933f850472f4ba67418c9a4fcb3ac38bb6f139b226664d47d8723e4aaa988b8
MD5 35cfdbdc92b3dbccff31cb026f444cc0
BLAKE2b-256 48d0737de29d302c7d4ba36de997ee8c8f37a24d96f165d81f9281656933b26c

See more details on using hashes here.

File details

Details for the file twisterl-0.5.1-cp312-cp312-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for twisterl-0.5.1-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 41ba0fe7ab1a08ec5484cd1eccdec53051d7c8aab6045adced533cbf0d85cb0f
MD5 be67be0da10072d71c334ab02cbca4eb
BLAKE2b-256 ffa3e88456d3d9d2e07ebc9db7161d182c3883314e8540996851f4ab31b4c0bc

See more details on using hashes here.

File details

Details for the file twisterl-0.5.1-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: twisterl-0.5.1-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 316.8 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for twisterl-0.5.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 c72b3d692e5213376916fbe12ba1853d3756a4d17f5455a49da65b63e1e48a04
MD5 88fa373091eb3dd7830269b566011149
BLAKE2b-256 f54142706adb8214d1ca344df0ddb76802a0d06f25a25cc8a2aeec42fea41bd4

See more details on using hashes here.

File details

Details for the file twisterl-0.5.1-cp311-cp311-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for twisterl-0.5.1-cp311-cp311-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 b947b18835a1fc6bcb60c8cd6790c04a1824c416143eb12ba78982b8b8a23555
MD5 9c901dcfef49922a676d1fe62e3cb507
BLAKE2b-256 19fd636312f1b8fa038c01ed72a07d8d387e065b7289a5cb917c9f42e2a1595c

See more details on using hashes here.

File details

Details for the file twisterl-0.5.1-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for twisterl-0.5.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 af08560447ec0e9b6915ec9dfc8f404b0c7ce18d0aac0dcc4f66c9d5db84feca
MD5 e85861c160e30a307e5f6fc3ae7fceee
BLAKE2b-256 2c98f1b7cda575a135889fdab0e843a2c6c8de2f793cf54444df32d5e092569d

See more details on using hashes here.

File details

Details for the file twisterl-0.5.1-cp311-cp311-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for twisterl-0.5.1-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 54fe58fac3f012a754762bfb001868499cd20e4892d46b497d51fda1bc0ea2bf
MD5 343a4f1746839f3a35adaa45982afbe0
BLAKE2b-256 cd3537ca853d218173be0969132b7ad8b08455916126df934972712ff4585aad

See more details on using hashes here.

File details

Details for the file twisterl-0.5.1-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: twisterl-0.5.1-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 315.8 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for twisterl-0.5.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 4f4c0dde9ae1038c3b62d8e65bb0679e31e98d281148e4852ef5f31d1292ffa3
MD5 5a8b653bb041149bbc475b254c9ec383
BLAKE2b-256 c1106f411fcd9ff64be9b08b12a696f6570a6f2d6e48b5027a2d0c5492aee1ed

See more details on using hashes here.

File details

Details for the file twisterl-0.5.1-cp310-cp310-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for twisterl-0.5.1-cp310-cp310-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 bb82d9e9a54ac85db611a4ec2c6f17a1645a83c14605646de555fc0009982839
MD5 23fa96532e8fc55780ce31a9168ea234
BLAKE2b-256 9f2ff82fe29e24745cc9d2eae4ff783a96b5ad61261999e889aebb8feeb1249a

See more details on using hashes here.

File details

Details for the file twisterl-0.5.1-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for twisterl-0.5.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c1b1d213938e3ff8ae9f93168b3be10cf5f222c3d79809eb7826d39919201ba4
MD5 2ae0d3835ca8ef213179ea5182bfe2d9
BLAKE2b-256 945fce16a6eba8a19f8ce97a3ff5420c9b7c655e6b58da500671e70ad477be53

See more details on using hashes here.

File details

Details for the file twisterl-0.5.1-cp310-cp310-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for twisterl-0.5.1-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 e9ac89abaa3a5289899159f139167e1da042000dc2022086c34f0901f92ded1a
MD5 522998a5d3708d16a2c9617a385735da
BLAKE2b-256 928d243962b1ef50c3600cdbf02ab49919963d35c7de75305f66801a9fb65e14

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page