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

Uploaded CPython 3.13Windows x86-64

twisterl-0.4.1-cp313-cp313-manylinux_2_34_x86_64.whl (455.5 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.34+ x86-64

twisterl-0.4.1-cp313-cp313-macosx_11_0_arm64.whl (411.7 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

twisterl-0.4.1-cp312-cp312-win_amd64.whl (318.3 kB view details)

Uploaded CPython 3.12Windows x86-64

twisterl-0.4.1-cp312-cp312-manylinux_2_34_x86_64.whl (456.6 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

twisterl-0.4.1-cp312-cp312-macosx_11_0_arm64.whl (411.9 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

twisterl-0.4.1-cp312-cp312-macosx_10_12_x86_64.whl (430.0 kB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

twisterl-0.4.1-cp311-cp311-win_amd64.whl (318.4 kB view details)

Uploaded CPython 3.11Windows x86-64

twisterl-0.4.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.4.1-cp311-cp311-macosx_11_0_arm64.whl (412.6 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

twisterl-0.4.1-cp311-cp311-macosx_10_12_x86_64.whl (430.4 kB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

twisterl-0.4.1-cp310-cp310-win_amd64.whl (318.2 kB view details)

Uploaded CPython 3.10Windows x86-64

twisterl-0.4.1-cp310-cp310-manylinux_2_34_x86_64.whl (455.8 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.34+ x86-64

twisterl-0.4.1-cp310-cp310-macosx_11_0_arm64.whl (412.7 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

twisterl-0.4.1-cp310-cp310-macosx_10_12_x86_64.whl (430.5 kB view details)

Uploaded CPython 3.10macOS 10.12+ x86-64

twisterl-0.4.1-cp39-cp39-win_amd64.whl (319.7 kB view details)

Uploaded CPython 3.9Windows x86-64

twisterl-0.4.1-cp39-cp39-manylinux_2_34_x86_64.whl (457.1 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.34+ x86-64

twisterl-0.4.1-cp39-cp39-macosx_11_0_arm64.whl (414.4 kB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

twisterl-0.4.1-cp39-cp39-macosx_10_12_x86_64.whl (432.4 kB view details)

Uploaded CPython 3.9macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: twisterl-0.4.1-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 317.8 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.4.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 436bcefb4e9f2c8e9561b9ecd2d3d1b67c3abb8613cac7e3914ba6d8faac2ee0
MD5 3371cc9e3411178b54ee08f140a2a343
BLAKE2b-256 4861f86ef0f666f249fe9422e1bf7e2478fcb4b40281ae7749a56b3006a9b1ac

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for twisterl-0.4.1-cp313-cp313-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 aad265c834a303d8ab63e6756c02ba14e8f29eb254bd34e2b47f3a5d49f9b2cc
MD5 cda9009b30aa8907845f967b8d5fdc1a
BLAKE2b-256 3eb05d641695fe23042541ce73534c971719727c8331cce4d412fcf31509a5ec

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for twisterl-0.4.1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 29c573af896d97463b769c82e178152da4557683bed6b3b6c6b05acda7fdab8e
MD5 43e7d4d441c97e3b97288b2e803e2e14
BLAKE2b-256 f58ae85a7ffbe158061ff1e42e983c36b34646b611418d57dcb17a804afb6636

See more details on using hashes here.

File details

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

File metadata

  • Download URL: twisterl-0.4.1-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 318.3 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.4.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 94ed14c55d03773e7522daaedbc1fad85d3f9d1f0c132cbacaa8afdd40df789c
MD5 f174b1fff50f3f35fa9081849bb8bda4
BLAKE2b-256 bbef7f753bbc8dd64e18e69bbf6933c982050db0a876cc0b9062525ead594c6d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for twisterl-0.4.1-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 22bb7cc00a6287e05fe1b05c92bece1a4485ee7a0e8d26b7a057b536cb08bc64
MD5 19d82c8391b5eecc391f3d2e68940b4b
BLAKE2b-256 44bf431c73f09323c31ece1387631bb1d29d8e8d508e005f9781f12e2c2e1ec8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for twisterl-0.4.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4b3816670c006e42455659a5b9cde9c147a6380dac537fc3f1b878436847b84e
MD5 497eaeba74570c30fe10849816c1654a
BLAKE2b-256 4b8b68557ae57e775c5e14b49261f0e2a35e1d3508847d6f894d22bbcece4702

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for twisterl-0.4.1-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 0036a566741ec6e657b8b3a9b1d7a85e3a212b55617b072d196dc88385a21c36
MD5 60c840b7354e6cc088bee2cfce371142
BLAKE2b-256 fb65fb765f4ea546def159f13564a4a0681722cd4f4006d551b644670d81d768

See more details on using hashes here.

File details

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

File metadata

  • Download URL: twisterl-0.4.1-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 318.4 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.4.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 52c9c23292e7fbe5103361c76188a54c254dc47a97576388bf85e93f04f0bf8c
MD5 39703da0354fc4ad3fae7787baa48748
BLAKE2b-256 97347e7616d448326c41c3084f0f809e01deb06d468a876cc412a16011e3b342

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for twisterl-0.4.1-cp311-cp311-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 9b603851629c7e7a3171e27cea705372282184a272dcda136650b95f71e8d7fe
MD5 b6dc40e596b99e91b80368f55cec16f0
BLAKE2b-256 307758eb418d35e97b6d60df1aab443d2d3ffe7cf78a1847abc98cc577937816

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for twisterl-0.4.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b9f55a0db0f073acc17a42b25bab6e0dac31f66f5369d3143222d7f33d46b2d5
MD5 b28ad96d93d52d1a6455763a63deaff0
BLAKE2b-256 d35caf1699fccc6115d4a010cc201bbf5e25a96a45ca09175fd6383a22d3af2d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for twisterl-0.4.1-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 d5edaa8ba62eca8c9744b44ecfd3baa431ba028bdb0b50fffcace27ebe9a74fe
MD5 0710a9000d88b5391b23db9403047d3b
BLAKE2b-256 3b708fb2796433d1c6efe0a19dc1e43279024c9f3055a89a7e191d0e4298de3d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: twisterl-0.4.1-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 318.2 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.4.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 459b140e34031164e2764bdb31c644d8a0893fc6f32c585130d401951b3985e0
MD5 3df1b45900aad02f9ff0ba554a3a404d
BLAKE2b-256 091208c54f80663272a69c2d1152d5c1ffc7faa84426c4170622dcc15c33224d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for twisterl-0.4.1-cp310-cp310-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 08efd92607df6cced153d997b74cfe14ea4110887de94ed93bd9144e8227004f
MD5 d2b131c9a8a267c0ec4d180d90c7409e
BLAKE2b-256 c4fdbb20094feac6454423543eedf1fe0f876b559540dde4b71b5ba0bc418dd2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for twisterl-0.4.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0acb88c6ce87b67016d33d190d8d8f8a4fb425b1048e39ce1b740c8ca8380fd3
MD5 24b775224d00384b5e9e68817596a193
BLAKE2b-256 c9df939a110739d85b64e01596a39cdbbed131fbd3e1d54ed316a808fcdb8cf1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for twisterl-0.4.1-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 760d8f978bc01a413cc55ff366329106a69b09dabf7dfb7de7597dd0a9903ca5
MD5 137b764ed7f743982de461c7f0ea15a0
BLAKE2b-256 6e7fa3b1545408ddad65d0bf60ae2051b87814ce16913a04a25b8b0be7abf5a6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: twisterl-0.4.1-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 319.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.4.1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 eb47a7fd34fde8c4f1ee1a293da790e38e48cd64111af4114e5214effca720b5
MD5 c46a4d010a2a82792196b78e9d490f84
BLAKE2b-256 ff838b20f09f5104c8aa7126637de9617575ff1cc6b52a53c57c1e6a0ca92d50

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for twisterl-0.4.1-cp39-cp39-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 bd89111b06d32453aeee67a8bf22aeb4367d4a1fb7c24fa088885534d50cd790
MD5 80c361df992514d547f16bbeebafdccc
BLAKE2b-256 bf092ea9345fea6225f0c77c3b7faca7230185f86f052340d88af43e4c6f8ba3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for twisterl-0.4.1-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 118135afcdc8b6ffd6af439342e43cf6f6ad7f51190e40eddbc6b00f57673681
MD5 02b6c3d8bb70cb1cc906f2dbd4f6388b
BLAKE2b-256 532bd8dc1962e36e5becdd052f6a186adea8cb2d9ffbfc6296035bf8c6085801

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for twisterl-0.4.1-cp39-cp39-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 eee6d0ab3767a5508bf1f74e768ed8eba8b5600d6554cef2244d6d697927e4f7
MD5 6748c4f8604ee4fbdc5bce243e94616d
BLAKE2b-256 75b994133c5f14524c68e806151b38218e08d2234098786a585badd7743676ff

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