trl icon indicating copy to clipboard operation
trl copied to clipboard

Simple Quick Start examples are great – but on what hardware? A note in the doc would be lovely.

Open jchwenger opened this issue 9 months ago • 5 comments

Feature request

Hi there,

This is a detail, really, but I notice that your simple quickstart:

from trl import SFTTrainer
from datasets import load_dataset

trainer = SFTTrainer(
    model="Qwen/Qwen3-0.6B",
    train_dataset=load_dataset("trl-lib/Capybara", split="train"),
)
trainer.train()

This uses around 25GB of RAM on an A100 in Colab. Unfortunately, that still requires Colab Pro, which is not ideal for students. To my surprise, this is also not impossibly far from the memory of a T4 (16GB, but really 14-15GB).

I'm going to look into the quickest way to reduce the memory footprint whilst keeping this example minimal (probablymax_length?), but since the free hardware available to students worldwide remains the T4 on Colab, usually, would you consider keeping that in mind for those introductory examples? That would hugely help preparing educational material, and would make the library more quickly/widely adopted as well!

What do you think? It might be as easy as adding one note in the docs saying "to run on a T4, add this option"... Thanks in advance for reading!

Motivation

Make the quickstart examples more accessible (especially for students).

Your contribution

  1. Pointing out the memory issue in Colab (free version).

  2. Some small tests: A. 11GB VRAM

    trainer = SFTTrainer(
        model="Qwen/Qwen2.5-0.5B",
        args = SFTConfig(per_device_train_batch_size=1), # instead of the default of 8
        train_dataset=dataset,
    )
    trainer.train()
    

    B. 13.6GB VRAM

    trainer = SFTTrainer(
        model="Qwen/Qwen2.5-0.5B",
        args = SFTConfig(max_length=256), # instead of the default of 1024
        train_dataset=dataset,
    )
    trainer.train()
    

    C. 12.4GB VRAM

     # reduced from https://ai.google.dev/gemma/docs/core/huggingface_text_finetune_qlora
     # Check if GPU benefits from bfloat16
     if torch.cuda.get_device_capability()[0] >= 8:
         torch_dtype = torch.bfloat16
     else:
         torch_dtype = torch.float16
     trainer = SFTTrainer(
         model="Qwen/Qwen2.5-0.5B",
         args = SFTConfig(
             # default max_len of 1024
             per_device_train_batch_size=1,
             gradient_accumulation_steps=4,
             gradient_checkpointing=True,
             fp16=True if torch_dtype == torch.float16 else False,
             bf16=True if torch_dtype == torch.bfloat16 else False,
         )
         train_dataset=dataset,
     )
     trainer.train()
    

jchwenger avatar Feb 04 '26 17:02 jchwenger

@jchwenger Great benchmarks , this is genuinely useful for anyone trying to run TRL examples on free Colab hardware.

Based on your tests, the most practical T4-friendly quickstart would combine max_length reduction with gradient_checkpointing, since that keeps the example minimal while staying safely under 15GB:

trainer = SFTTrainer(
    model="Qwen/Qwen2.5-0.5B",
    args=SFTConfig(
        max_length=256,
        per_device_train_batch_size=1,
        gradient_checkpointing=True,
        bf16=True,
    ),
    train_dataset=load_dataset("trl-lib/Capybara", split="train"),
)

A small hardware note in the quickstart docs , something like "this example requires ~25GB VRAM; for free Colab (T4), see low-memory config" , would go a long way without cluttering the minimal example itself.

Saad-Mallebhari avatar Feb 28 '26 09:02 Saad-Mallebhari

Thanks @Saad-Mallebhari!

jchwenger avatar Feb 28 '26 13:02 jchwenger

Hi, I ran some benchmarks on this and opened a PR at #5472 that adds a hardware requirements note to the quickstart and improves the "Out of Memory?" section with memory optimization tips

Tested the default quickstart (Qwen2.5-0.5B + Capybara) on my RTX 2070 (8 GB) locally and on T4, L4, A10G, and A100 using Modal

With default settings it only runs on the A100 (peak of 24.2 GB via nvidia-smi)

Also found that L4/A10G (24 GB) OOM with defaults, even though the actual peak usage is ~21 GB. Setting PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True before training fixes it (peak of 21.3 GB via nvidia-smi)

Finally I managed to run the example on my 2070 at 5.3 GB with following config per_device_train_batch_size=1, max_length=256, fp16=True, optim="adamw_8bit"

To reproduce the benchmarks, you can use the attached script: benchmark_quickstart.py

You will need a HF_TOKEN and Modal installed (pip install modal). Then run:

modal secret create huggingface HF_TOKEN=<your_token>
modal run benchmark_quickstart.py

I used following versions:

torch: 2.10.0+cu128
transformers: 4.57.6
datasets: 4.8.4
trl: 1.1.0.dev0
modal: 1.4.1
python: 3.13.0 

pqbas avatar Apr 07 '26 21:04 pqbas

Hey @jchwenger, sorry for the slow reply, we're catching up on the backlog. Since v1.7.0, SFT defaults to a chunked cross-entropy loss, so the full vocab x sequence logits tensor is no longer materialized:

https://github.com/huggingface/trl/blob/0312c254c8a65b2a1f69253aaecdc7c4b4032baf/trl/trainer/sft_config.py#L333-L334

I re-ran the quickstart exactly as written on a T4 (15 GB) with current main. It trains without OOM, peaking around 10 GB with Qwen2.5-0.5B and 13 GB with Qwen3-0.6B, while the old loss (loss_type="nll") still OOMs on the first step. So free Colab should work out of the box now. Closing, but happy to re-open if you still hit OOM on a recent version (please include your trl env output). Thanks @pqbas for the earlier benchmarks too!

albertvillanova avatar Sep 25 '26 14:09 albertvillanova

Thanks for this @albertvillanova, for sure I'll let you know if I encounter that issue again!

jchwenger avatar Sep 28 '26 08:09 jchwenger