Skip to main content

A type-safe, algorithm-agnostic Reinforcement Learning framework

Project description

RL Template

A modular reinforcement learning training framework built on PyTorch and Gymnasium, providing abstract base classes and a concrete PPO implementation.

Features

  • Abstract base classesBaseAgent, BaseEnv, BaseTrain with template method pattern for easy extension
  • PPO implementation — Clipped surrogate loss, GAE, linear LR decay, entropy bonus, gradient clipping
  • Pre-allocated buffer — Zero-allocation rollout storage with bounds checking, supports discrete and continuous actions
  • Frozen configs — Immutable PPOConfig and computed TrainConfig dataclasses
  • Auto GPU detection — Automatic CUDA/CPU device selection
  • 107 tests — Full test suite covering all components

Project Structure

rl_template/
  __init__.py
  agent.py               # BaseAgent — abstract policy/value interface
  env.py                 # BaseEnv — Gymnasium v1 API wrapper
  train.py               # BaseTrain — rollout/update training loop
  common.py              # Buffer — pre-allocated numpy rollout buffer
  config.py              # PPOConfig, TrainConfig, WandbConfig
  errors.py              # EmptyBufferError
  algorithms/
    ppo/
      ppo.py             # PPOTrainer — GAE + clipped surrogate loss
tests/
  test_agent.py          # BaseAgent tests (12)
  test_env.py            # BaseEnv tests (11)
  test_train.py          # BaseTrain tests (6)
  test_common.py         # Buffer tests (21)
  test_config.py         # Config tests (13)
  test_errors.py         # Error tests (7)
  test_ppo.py            # PPOTrainer tests (15)
  test_buffer_integration.py  # Buffer integration tests (6)
  test_continuous_actions.py  # Continuous action tests (16)

Installation

Requires Python 3.11+.

# With uv (recommended)
uv sync

# With pip
pip install -r requirements.txt

Usage

import numpy as np
from rl_template.agent import BaseAgent
from rl_template.env import BaseEnv
from rl_template.train import BaseTrain
from rl_template.common import Buffer
from rl_template.config import TrainConfig, PPOConfig
from rl_template.algorithms.ppo.ppo import PPOTrainer


# 1. Implement your agent
class MyAgent(BaseAgent):
    # Implement forward(), get_distribution()
    ...

# 2. Implement your environment
class MyEnv(BaseEnv):
    # Implement reset(), step(), close()
    ...

# 3. Configure training
config = TrainConfig(model_name="my_agent", model_saved_path="./checkpoints")
ppo_config = PPOConfig(lr=3e-4, gamma=0.99, clip_eps=0.2)

# 4. Wire everything together
agent = MyAgent()
env = MyEnv()
buffer = Buffer(step=config.rollout_steps, state_shape=(4,))
ppo_trainer = PPOTrainer(agent, lr=ppo_config.lr, gamma=ppo_config.gamma)

# 5. Create trainer and run
class MyTrain(BaseTrain):
    def rollout_phase(self, state): super().rollout_phase(state)
    def update_weights(self, step): super().update_weights(step)
    def save_model(self): super().save_model()

trainer = MyTrain(agent, env, buffer, config, ppo_trainer)
state, _ = env.reset()

for step in range(config.num_update):
    trainer.rollout_phase(state)
    trainer.update_weights(step)
    trainer.save_model()

Key Classes

Class Description
BaseAgent Abstract agent combining ABC and nn.Module. Subclasses implement forward() and get_distribution(). The get_action() template method handles sampling and log-probability computation.
BaseEnv Abstract Gymnasium v1 environment wrapper. Subclasses implement reset(), step(), and close().
BaseTrain Abstract training loop orchestrating rollout collection, GAE computation, and PPO updates.
Buffer Pre-allocated numpy buffer for rollout data. Supports discrete and continuous actions with automatic dtype handling.
PPOTrainer PPO algorithm implementation with GAE, clipped surrogate loss, value loss, entropy bonus, and linear LR decay.
PPOConfig Frozen (immutable) PPO hyperparameters.
TrainConfig Training configuration with auto-computed model_path and num_update.

PPO Algorithm

The PPO trainer implements:

  • Generalized Advantage Estimation (GAE) — Blended n-step returns for low-variance advantage estimates
  • Clipped surrogate loss — Prevents destructive policy updates
  • Value function loss — MSE loss for the critic network
  • Entropy bonus — Encourages exploration
  • Linear LR decay — Learning rate anneals to zero over training
  • Gradient clipping — Max norm of 1.0 for stability

Testing

python -m pytest tests/ -v

All 107 tests cover unit tests for each component, integration tests for buffer workflows, and continuous action space validation.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rl_template-0.1.0.tar.gz (22.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rl_template-0.1.0-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file rl_template-0.1.0.tar.gz.

File metadata

  • Download URL: rl_template-0.1.0.tar.gz
  • Upload date:
  • Size: 22.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for rl_template-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c61feae4f99f314ad25387768eb41a51216b5c114be883a2afe764d79cdbeb55
MD5 e72e032aab3f1dbab2ad4970688aae42
BLAKE2b-256 f8bfc2814dbec8309724a8c01f4bac4379fd2ba97de68ddeef6113f0a17e5c6f

See more details on using hashes here.

File details

Details for the file rl_template-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: rl_template-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for rl_template-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 90d28127fd241189c35547f75065c32606dba8b94f44adf78bc86f771a448127
MD5 cf87e025bc8856c53d2d9b788cd241cc
BLAKE2b-256 883239686eb895fc59c7bce1809e304cf53f0ea085b7b30aa0e426cdb016ec7a

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