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.0-cp313-cp313-win_amd64.whl (320.5 kB view details)

Uploaded CPython 3.13Windows x86-64

twisterl-0.5.0-cp313-cp313-manylinux_2_34_x86_64.whl (458.7 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.34+ x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

twisterl-0.5.0-cp312-cp312-win_amd64.whl (320.6 kB view details)

Uploaded CPython 3.12Windows x86-64

twisterl-0.5.0-cp312-cp312-manylinux_2_34_x86_64.whl (458.7 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

twisterl-0.5.0-cp312-cp312-macosx_11_0_arm64.whl (416.9 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

twisterl-0.5.0-cp312-cp312-macosx_10_12_x86_64.whl (433.1 kB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

twisterl-0.5.0-cp311-cp311-win_amd64.whl (317.0 kB view details)

Uploaded CPython 3.11Windows x86-64

twisterl-0.5.0-cp311-cp311-manylinux_2_34_x86_64.whl (455.7 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.34+ x86-64

twisterl-0.5.0-cp311-cp311-macosx_11_0_arm64.whl (412.9 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

twisterl-0.5.0-cp311-cp311-macosx_10_12_x86_64.whl (427.7 kB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

twisterl-0.5.0-cp310-cp310-win_amd64.whl (316.0 kB view details)

Uploaded CPython 3.10Windows x86-64

twisterl-0.5.0-cp310-cp310-manylinux_2_34_x86_64.whl (454.9 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.34+ x86-64

twisterl-0.5.0-cp310-cp310-macosx_11_0_arm64.whl (413.0 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

twisterl-0.5.0-cp310-cp310-macosx_10_12_x86_64.whl (427.8 kB view details)

Uploaded CPython 3.10macOS 10.12+ x86-64

twisterl-0.5.0-cp39-cp39-win_amd64.whl (317.7 kB view details)

Uploaded CPython 3.9Windows x86-64

twisterl-0.5.0-cp39-cp39-manylinux_2_34_x86_64.whl (456.2 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.34+ x86-64

twisterl-0.5.0-cp39-cp39-macosx_11_0_arm64.whl (414.5 kB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

twisterl-0.5.0-cp39-cp39-macosx_10_12_x86_64.whl (429.3 kB view details)

Uploaded CPython 3.9macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: twisterl-0.5.0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 320.5 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.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 c85e939df1f3b93d0e31199d7d139aac0f70b7e7d64ed77b88093ce3dae53112
MD5 ad02a750fe2efb33f9a53c32b2103b99
BLAKE2b-256 e58a2cb68bf2b20e7e75118d84599c29005e43da3152f30fb333b74c50e8b453

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for twisterl-0.5.0-cp313-cp313-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 a52bcbaca49e3d215806b37e26002f091f279cc26de734b2c8ecd39c05270c26
MD5 65d0f3200c447a5384e402b6463d44bc
BLAKE2b-256 636204e18c9c0720144faf3d3a5b5151c291f88c3c020119581728e66d35a4d5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for twisterl-0.5.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c224459c6336f365c7e5981eeac1c226495f073178f501f6e51148d44b930e6c
MD5 160e2e8270fda3c178a216b069884821
BLAKE2b-256 c316db3183d19c36381d8c4436b4dd728948ab122d5f4b6a4beba9e726dea98b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: twisterl-0.5.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 320.6 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.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 8a9b2dc10bb83cbe7170e1d28e1c0e28e9df441e5b0b9a0b2ef97a3c72dd319f
MD5 5d08bd612bec2410682c65e18bfc15f6
BLAKE2b-256 1d20685a17c541cbf0a1130412d166d6c04f4f4b738de1f15ef1eed408198d93

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for twisterl-0.5.0-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 dfb2f456d1ceb5949761a76a5a02e32d01a4268e63ca6768ae808cb8910ef7ad
MD5 59d78792e4ee7c0dc943d51332ab83b4
BLAKE2b-256 f29e01f5eff53dadd47d45c4818a2f7331ae2f1325f062a67f536e568cf26100

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for twisterl-0.5.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 549e626c5c43f2f7c978fa8aa73b022fbbe05fef41fad203c07d8a03c8617c0b
MD5 7cdddc326652588fb81c9471f4e24754
BLAKE2b-256 4d92e98d9845ef00685d2a7067f55a238aacf077bbcfde23661aad4059f62350

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for twisterl-0.5.0-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 3d4cbfb81ef73b51d7f6f4ac5f508b69e2e3894e60c5da6ebb7d6e124f97cdf0
MD5 b21ee93a022cfe5e4deabd761dd50a00
BLAKE2b-256 ef90ad3edeeb1f1d75d19180ddc9a7a9b38f90721d4057e29bbbe929d6e67870

See more details on using hashes here.

File details

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

File metadata

  • Download URL: twisterl-0.5.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 317.0 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.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 234a41f3ebbb6bbf8c772e9b7a2ef63e5ae59039df8bdca5e4b8938f25d19bce
MD5 ab860abc8bf6ee6738f4c898066eab4e
BLAKE2b-256 88d362143bb87e1778afa0dadd16b484e591dba88768758d0dcbb2e64be69010

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for twisterl-0.5.0-cp311-cp311-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 e077f464415bc56029f865d9a4e26d2fdfa3a3ff5314fc4e95f91b0b5c4f76f7
MD5 18e14b7b7d932d04de237ef9086ae2bb
BLAKE2b-256 689dcb6592b380f40574ba1a6a33b700c49111eed708d3df5bedc65ff86efc01

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for twisterl-0.5.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 765fab67a550443c7eba60e69d271a9c2d20367ec820957f8f9de1134e35e60b
MD5 7b68723b850427f658ee93f710afceae
BLAKE2b-256 6f4278ac07729ee0135022eaf5875f5421b137dd3e2345f4042f9794d11eb9de

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for twisterl-0.5.0-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 43223508546262fd8c440b5b13f00e5c6035a988f548a18038afc8107f449639
MD5 f8aaa5b5f9f4e38d1b30ca8d660da0ba
BLAKE2b-256 7078baea9e306a430ee60450be3d27494d838fdf3190c7a00d0feca358465d5c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: twisterl-0.5.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 316.0 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.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 f1d807159ba3dd3d4b7e2c02c897884cfbc77ae99d4298928bcbe01c63dd030c
MD5 c4444c3057f362eac780093edd1af12f
BLAKE2b-256 16e1678d60a642477ff2d4c14198444a17372d6cbccc44e7261b76f7deb38404

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for twisterl-0.5.0-cp310-cp310-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 9012ca1f08c991b74b5ffb4e4c4b19b51854a6b8249332032ccd3b1efd75ef20
MD5 e8e8bf9371ba6342097166a39891724a
BLAKE2b-256 2986240935c225e772d50e32fb202878e4754000fb74468c0ed63ce2d14638a0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for twisterl-0.5.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b45f51adc29022aac1904938bc2041cacd5baf215a2d5eb936fd341512736487
MD5 9dae140db8555d2f4dc0b7bcec81c89b
BLAKE2b-256 7b0e6880c6907d2c707f26f4840fa3164bcea0af76680ba1620000013b70f072

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for twisterl-0.5.0-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 2eb34c42c0c3774d7478dd2b6ba04f07723aa9b16807ea918ae495dc82107af8
MD5 b6c33ac76087f6f07088f7d22170b73c
BLAKE2b-256 b67d8f32e17bce75a7408327a71aa0722795f70733222d4cded70b907561da21

See more details on using hashes here.

File details

Details for the file twisterl-0.5.0-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: twisterl-0.5.0-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 317.7 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.13

File hashes

Hashes for twisterl-0.5.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 aa07f6bdc1f8ae5a96aa118ad0ca5ed7e208b276797bb581c8c3a04962fd26fa
MD5 22deffa6c5d60e5c748819cb7ac82f09
BLAKE2b-256 3f6cac40c75adf66db8fd8f9c89aa54ff04b17ad1cc95edc51a51e2df916854b

See more details on using hashes here.

File details

Details for the file twisterl-0.5.0-cp39-cp39-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for twisterl-0.5.0-cp39-cp39-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 aff52c782d23d675e25bc84a0f8667f06f30266d32ffe2882fe7c4202513b5dc
MD5 62eafc321598b34db37ca775c652b638
BLAKE2b-256 b6ed0cd323ff65c9e9870f5f9bd50ddcfc3957653c2967375f37096702d2d80b

See more details on using hashes here.

File details

Details for the file twisterl-0.5.0-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for twisterl-0.5.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 547fc853d45164b0d00d10a01b39d96f4d02b68ffa9f8d4db07aef85be1928ee
MD5 49484f890384fb3627c7f935054af1f4
BLAKE2b-256 05519a83b1375ed5b0105078c675d2c19ddcf2f86e880dd12186f31cd9fce387

See more details on using hashes here.

File details

Details for the file twisterl-0.5.0-cp39-cp39-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for twisterl-0.5.0-cp39-cp39-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 d61fd3d79c2e338934b11bb82e530c5f42bd668facdebececde4d06462f0fc3e
MD5 742996337a18411f9d3c11e1154c1512
BLAKE2b-256 019b1f8d76319677b57fb8df006eea308874c05e9033dd3d0d7a532589cb9aae

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