RLlib: Industry-grade, scalable reinforcement learning#

../_images/rllib-logo.png

RLlib is an open source library for reinforcement learning (RL). It supports production-grade, scalable, fault-tolerant RL workloads and keeps simple, unified APIs across a wide range of industry applications.

Whether you train policies in a multi-agent setup, from historic offline data, or with externally connected simulators, RLlib covers each of these autonomous decision-making cases, so you can start running experiments quickly.

Industry leaders use RLlib in production in many different verticals, such as gaming, robotics, finance, climate and industrial control, manufacturing and logistics, automobile, and boat design.

RLlib in 60 seconds#

../_images/rllib-index-header.svg

A few steps get your first RLlib workload running on your laptop. Install RLlib and PyTorch:

pip install "ray[rllib]" torch

Note

To run the Atari or MuJoCo examples, install these additional packages:

pip install "gymnasium[atari,accept-rom-license,mujoco]"

That’s all you need to start coding against RLlib. This example runs the PPO algorithm on the Taxi domain. First, create a config for the algorithm. The config defines the RL environment and any other settings the algorithm needs.

from ray.rllib.algorithms.ppo import PPOConfig
from ray.rllib.connectors.env_to_module import FlattenObservations

# Configure the algorithm.
config = (
    PPOConfig()
    .environment("Taxi-v3")
    .env_runners(
        num_env_runners=2,
        # Observations are discrete (ints) -> We need to flatten (one-hot) them.
        env_to_module_connector=lambda env: FlattenObservations(),
    )
    .evaluation(evaluation_num_env_runners=1)
)

Next, build the algorithm and train it for two iterations. One training iteration includes parallel, distributed sample collection by the EnvRunner actors, followed by loss calculation on the collected data, and a model update step.

from pprint import pprint

# Build the algorithm.
algo = config.build_algo()

# Train it for 2 iterations ...
for _ in range(2):
    pprint(algo.train())

At the end of your script, evaluate the trained algorithm and release its resources:

# ... and evaluate it.
pprint(algo.evaluate())

# Release the algo's resources (remote actors, like EnvRunners and Learners).
algo.stop()

You can use any Farama-Foundation Gymnasium registered environment with the env argument.

In config.env_runners(), you can specify the number of parallel EnvRunner actors that collect samples from the environment, among many other settings.

You can also change the neural network architecture with RLlib’s DefaultModelConfig, and set up a separate config for the evaluation EnvRunner actors through the config.evaluation() method.

To learn more about the RLlib training APIs, see the RLlib Python API. For an example of an action inference loop after training, see this example script.

For a quick preview of which algorithms and environments RLlib supports, expand the dropdowns below.

RLlib Algorithms
RLlib Environments

Farama-Foundation Environments

gymnasium single_agent

pip install "gymnasium[atari,accept-rom-license,mujoco]"
config.environment("CartPole-v1")  # Classic Control
config.environment("ale_py:ALE/Pong-v5")  # Atari
config.environment("Hopper-v5")  # MuJoCo

PettingZoo multi_agent

pip install "pettingzoo[all]"
from ray.tune.registry import register_env
from ray.rllib.env.wrappers.pettingzoo_env import PettingZooEnv
from pettingzoo.sisl import waterworld_v4
register_env("env", lambda _: PettingZooEnv(waterworld_v4.env()))
config.environment("env")

RLlib Multi-Agent

RLlib’s MultiAgentEnv API multi_agent

from ray.rllib.examples.envs.classes.multi_agent import MultiAgentCartPole
from ray import tune
tune.register_env("env", lambda cfg: MultiAgentCartPole(cfg))
config.environment("env", env_config={"num_agents": 2})
config.multi_agent(
    policies={"p0", "p1"},
    policy_mapping_fn=lambda aid, *a, **kw: f"p{aid}",
)

Why choose RLlib?#

Scalable and Fault-Tolerant

RLlib workloads scale along two axes:

  • The number of EnvRunner actors. Set this through config.env_runners(num_env_runners=...) to scale the speed of your simulator data collection step. This EnvRunner axis is fully fault tolerant. You can train against custom environments that are unstable or that frequently stall, and even place all your EnvRunner actors on spot machines.

  • The number of Learner actors for multi-GPU training. Set this through config.learners(num_learners=...). Normally you set it to the number of available GPUs, and also set config.learners(num_gpus_per_learner=1). If you don’t have GPUs, use this setting for DDP-style learning on CPUs instead.

Multi-Agent Reinforcement Learning (MARL)

RLlib natively supports multi-agent reinforcement learning (MARL), so you can run any complex configuration.

  • Independent multi-agent learning: every agent collects data to update its own policy network and treats other agents as part of the environment. This is the default.

  • Collaborative training: train a team of agents that share one policy and its parameters, or give some agents their own policy networks. You can share value functions across the whole team or part of it, so you optimize global or local objectives.

  • Adversarial training: have agents compete against each other. Use self-play, or league-based self-play, to train them through stages of increasing difficulty.

  • Any combination of the preceding. You can train teams of any size against other teams, where the agents in each team have individual sub-objectives and neutral agents sit out the competition.

Offline RL and Behavior Cloning

RLlib integrates Ray Data for large-scale data ingestion in offline RL and behavior cloning (BC) workloads.

See a basic tuned behavior cloning example, or an example of pre-training a policy with BC and fine-tuning it with online PPO.

Support for External Env Clients

RLlib supports externally connected RL environments by customizing the EnvRunner logic. Instead of RLlib-owned, internal Gymnasium environments, you can connect external, TCP-connected environments that act independently and can even run their own action inference, for example through ONNX.

For an example, see RLlib acting as a server for external env TCP clients.

Learn more#

RLlib Key Concepts

Learn the core concepts of RLlib, such as algorithms, environments, models, and learners.

RL Environments

Get started with environments RLlib supports, such as the Farama Foundation’s Gymnasium, PettingZoo, and custom formats for vectorized and multi-agent environments.

Models (RLModule)

Learn how to configure RLlib’s default models and implement your own custom models through the RLModule APIs, which support arbitrary architectures with PyTorch, complex multi-model setups, and multi-agent models with components shared between agents.

Algorithms

See the RL algorithms RLlib provides for on-policy and off-policy training, offline and model-based RL, multi-agent RL, and more.

Customize RLlib#

RLlib provides APIs for customizing every part of your experimental and production training workflows. For example, you can code your own environments in Python with the Farama Foundation’s Gymnasium or DeepMind’s OpenSpiel, provide custom PyTorch models, write your own optimizer setups and loss definitions, or define custom exploratory behavior.

../_images/rllib-new-api-stack-simple.svg

RLlib’s API stack: Built on Ray, RLlib provides off-the-shelf, distributed, fault-tolerant algorithms and loss functions, PyTorch default models, multi-GPU training, and multi-agent support. You customize your experiments by subclassing the existing abstractions.#

Cite RLlib#

If RLlib helps with your academic research, the Ray RLlib team encourages you to cite these papers:

@inproceedings{liang2021rllib,
    title={{RLlib} Flow: Distributed Reinforcement Learning is a Dataflow Problem},
    author={
        Wu, Zhanghao and
        Liang, Eric and
        Luo, Michael and
        Mika, Sven and
        Gonzalez, Joseph E. and
        Stoica, Ion
    },
    booktitle={Conference on Neural Information Processing Systems ({NeurIPS})},
    year={2021},
    url={https://proceedings.neurips.cc/paper/2021/file/2bce32ed409f5ebcee2a7b417ad9beed-Paper.pdf}
}

@inproceedings{liang2018rllib,
    title={{RLlib}: Abstractions for Distributed Reinforcement Learning},
    author={
        Eric Liang and
        Richard Liaw and
        Robert Nishihara and
        Philipp Moritz and
        Roy Fox and
        Ken Goldberg and
        Joseph E. Gonzalez and
        Michael I. Jordan and
        Ion Stoica,
    },
    booktitle = {International Conference on Machine Learning ({ICML})},
    year={2018},
    url={https://arxiv.org/pdf/1712.09381}
}