Skip to main content

RLtools Python Interface

Open In Colab

A Python wrapper for RLtools (https://rl.tools). The only dependency is pybind11 to bind the C++ functions. Note that pybind11 requires the Python headers (which can e.g. be installed by apt install python3-dev on Ubuntu). The RLtools training code needs to be compiled at runtime because properties like the observation and action dimensions are not known at compile time. One of the fundamental principles of RLtools is that the sizes of all data structures and loops are known at compile-time so that the compiler can maximally reason about the code and heavily optimize it. Hence this wrapper takes an environment (Gymnasium interface) factory function as an input to infer the observation and action shapes and compile a bridge environment that is compatible with RLtools.

This wrapper is work in progress and for now just exposes the SAC training loop and does not allow much modification of hyperparameters etc. yet. Stay tuned.

Installation:

pip install rltools gymnasium

Note: On macOS you might have to install and accept the Xcode CLI license terms by sudo xcodebuild -license because we use clang in the background to compile RLtools operations just-in-time.

Example:

from rltools import SAC
import gymnasium as gym
from gymnasium.wrappers import RescaleAction

seed = 0xf00d
def env_factory():
    env = gym.make("Pendulum-v1")
    env = RescaleAction(env, -1, 1)
    env.reset(seed=seed)
    return env

sac = SAC(env_factory)
state = sac.State(seed)

finished = False
while not finished:
    finished = state.step()

Evaluating the Trained Policy

pip install gymnasium[classic-control]
env_replay = gym.make("Pendulum-v1", render_mode="human")

while True:
    observation, _ = env_replay.reset(seed=seed)
    finished = False
    while not finished:
        env_replay.render()
        action = state.action(observation)
        observation, reward, terminated, truncated, _ = env_replay.step(action)
        finished = terminated or truncated

Saving and Loading Checkpoints

# Save
with open("pendulum_sac_checkpoint.h", "w") as f:
    f.write(state.export_policy())
# Load
from rltools import load_checkpoint_from_path
policy = load_checkpoint_from_path("pendulum_sac_checkpoint.h")
action = policy.evaluate(observation) # Note that e.g. SAC's policies output mean and std (concatenated)

Custom C++ Enviroments

To get the maximum performance you should rewrite your environment in C++. Don't be scared it is actually quite straightforward and similar to creating a Gym environment. For an example of a custom pendulum environment see examples/custom_environment (just 105 lines of code).

Acceleration

On macOS the RLtools Python interface automatically uses Accelerate. To use MKL on linux you can install RLtools with the mkl option:

pip install rltools[mkl]

Windows

The RLtools Python interface also works on Windows but MKL is not integrated, yet. Please make sure to install Python from the installer from the Python website and not using the Windows Store Python version. The latter resides in a directory that requires admin privileges even for read access. Due to the just-in-time compilation of RLtools we need to be able to read the Python header and library files. After installing the right Python version the easies way to run RLtools is by opening the cloned folder in Visual Studio Code and launching the preconfigured targets. Make sure to start Visual Studio Code from the Visual Studio Prompt (e.g. Developer Command Prompt for VS 2022) by running code so that cl.exe (MSVC) is available in the environment.

Acknowledgements

Thanks a lot to @omtinez for letting us use the PyPI rltools handle! The PyPI rltools handle originally contained the v0.1.0, v0.2.0, and v0.2.1 releases of omtinez/rltools. Please refer to the source repository omtinez/rltools if you were looking for the original releases.

Metadata

Release files for rltools 2.2.1

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

Source distribution (sdist)

Source distribution for rltools 2.2.1
File Size Uploaded Provenance
rltools-2.2.1.tar.gz 326.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for rltools 2.2.1
File Interpreter ABI Platform
rltools-2.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 923.4 kB

Release files / rltools-2.2.1.tar.gz

Download URL rltools-2.2.1.tar.gz
Size 326.6 kB
Tags Source
SHA-256 checksum
How to use checksums
1ee45ae609380fc6a6c5b91e18ea023a3895d69fe148cd4cc25ace415da756b0
BLAKE2b-256 checksum
How to use checksums
4182c81992b7cdfc4e6541d2c139e474089d5f2a457d6075fe4fd21d5c669794
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / rltools-2.2.1-py3-none-any.whl

Download URL rltools-2.2.1-py3-none-any.whl
Size 596.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d36327057d57cdc290d1342165b4240b7db007896527a50f954c6966691dd8d7
BLAKE2b-256 checksum
How to use checksums
88861354c5df12f37a86d33c3805c401f15f12558d9e3fe6825038cff95de2c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Security for rltools 2.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

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