๐Ÿ”ŒUsing SLM Lab In Your Project

circle-info

This is an advanced tutorial for integrating SLM Lab agents into your own applications.

SLM Lab's modular design allows its components to be used outside the standard lab framework. This is useful when:

  • Building an RL-powered application that integrates with existing systems

  • Creating custom training loops with domain-specific logic

  • Using SLM Lab's well-tested algorithms in a different framework

Installation

git clone https://github.com/kengz/SLM-Lab.git
cd SLM-Lab
uv sync

Key Components

Component
Import
Purpose

spec_util

from slm_lab.spec import spec_util

Load specs from files

util

from slm_lab.lib import util

Utilities including read() for saved specs

make_env

from slm_lab.env import make_env

Create environments with SLM Lab wrappers

Agent

from slm_lab.agent import Agent

The RL agent

MetricsTracker

from slm_lab.agent import MetricsTracker

Metrics collection and logging

net_util

from slm_lab.agent.net import net_util

Network utilities including model loading

Example: Custom Training Loop

Here's a complete example of running a training loop outside the lab:

Example: Inference with Trained Agent

To use a trained agent for inference:

Key APIs

Agent

Environment

Spec Utilities

Model Loading

Tips

  1. Thread management: Set OMP_NUM_THREADS=1 to prevent PyTorch thread overuse

  2. Lab mode: Set os.environ['lab_mode'] before creating agents:

    • 'train': Full training with checkpoints

    • 'dev': Training with rendering

    • 'enjoy': Inference only (loads best checkpoint)

  3. GPU usage: The gpu field in net spec controls device:

    • "auto": Use GPU if available

    • true: Force GPU

    • false: CPU only

Limitations

When using SLM Lab as a module, you lose:

  • Ray Tune hyperparameter search

  • Automatic experiment organization

  • TensorBoard integration

For these features, use the full framework via slm-lab run.

Last updated

Was this helpful?