A Python wrapper for RLtools
Project description
TinyRL
A Python wrapper for RLtools (https://rl.tools). PyTorch is only used for its utils that allow convenient wrapping of C++ code to compile RLtools. 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 tinyrl gymnasium
Example:
from tinyrl import SAC
import gymnasium as gym
seed = 0xf00d
def env_factory():
env = gym.make("Pendulum-v1")
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 tinyrl 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 TinyRL automatically uses Accelerate. To use MKL on linux you can install TinyRL with the mkl
option:
pip install tinyrl[mkl]
Windows
TinyRL 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 TinyRL 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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file tinyrl-0.4.3.tar.gz
.
File metadata
- Download URL: tinyrl-0.4.3.tar.gz
- Upload date:
- Size: 145.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1efa724236e1ed1be8b32447d71af1aa9288d25a0cec3483c16b877b0320169f |
|
MD5 | 63cc1b504f9765420b710092c0abbac4 |
|
BLAKE2b-256 | 2b81794b3f5fef25e6600829d5854fa051d5c73da302fac5806e7678fd31d903 |
File details
Details for the file tinyrl-0.4.3-py3-none-any.whl
.
File metadata
- Download URL: tinyrl-0.4.3-py3-none-any.whl
- Upload date:
- Size: 317.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 87190fec5281fb43165e987a19a1c8cd2a51cfc83b4d1469f4dcdda5b7500099 |
|
MD5 | 8c450ee728a4ed755f05ba55654c1aa9 |
|
BLAKE2b-256 | f6fc95de7ac65d9ccd9c4bfb30608ea3278c2ceae78d9ce4830f1170838dd1e0 |