---
title: "Fine-tuning 20B LLMs with RLHF on a 24GB consumer GPU"
description: "Combining trl with peft and bitsandbytes makes RLHF fine-tuning of a 20B-parameter LLM feasible on a single 24GB consumer GPU."
url: "https://swe.quest/articles/fine-tuning-20b-llms-with-rlhf-on-a-24gb-consumer-gpu-7ad6c7bb6c14c3cf"
canonical: "https://huggingface.co/blog/trl-peft"
source: "Hugging Face"
published: "2023-03-09"
difficulty: "hard"
kind: "How-to"
reading_minutes: 11
tags: ["model-training","llms","machine-learning","python"]
---

# Fine-tuning 20B LLMs with RLHF on a 24GB consumer GPU

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2023-03-09, 11 min read, How-to. Original: [https://huggingface.co/blog/trl-peft](https://huggingface.co/blog/trl-peft)

## Summary

Combining trl with peft and bitsandbytes makes RLHF fine-tuning of a 20B-parameter LLM feasible on a single 24GB consumer GPU. The approach loads the active model in 8-bit using LLM.int8(), trains only small LoRA adapters to shrink optimizer state, and obtains reference logits for PPO from the same model by disabling the adapters, so no second model copy is needed. Memory rules of thumb are 4/2/1 GB per billion parameters in fp32/fp16/int8, plus 8 bytes per parameter for AdamW. A gpt-neox-20b model was fine-tuned on IMDB, its adapters were merged, and it was then RL-tuned to generate positive reviews. The main cost is speed, since adapters make forward and backward passes roughly twice as slow.

Written by swe.quest, not by the author.

**Difficulty:** Hard: written for specialists in the field

## Topics

- [Model training](https://swe.quest/tags/model-training.md) (AI & ML)
- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)
- [Machine learning](https://swe.quest/tags/machine-learning.md) (AI & ML)
- [Python](https://swe.quest/tags/python.md) (Languages)

## Prerequisites

- RLHF and PPO basics ([easy posts on Model training](https://swe.quest/tags/model-training.md?difficulty=easy))
- transformer fine-tuning ([easy posts on Model training](https://swe.quest/tags/model-training.md?difficulty=easy))
- GPU memory and dtypes ([easy posts on Hardware](https://swe.quest/tags/hardware.md?difficulty=easy))

## Keywords

TRL, PEFT, LoRA, LLM.int8(), bitsandbytes, PPO, GPT-NeoX-20B, Hugging Face Transformers

## Related posts

- [Parameter-Efficient Fine-Tuning using 🤗 PEFT](https://swe.quest/articles/parameter-efficient-fine-tuning-using-peft-5c28b9ec4e65b0a1.md): Hugging Face · 2023-02-10 · Medium · 5 min read · Model training, LLMs, Python
  Hugging Face's PEFT library brings parameter-efficient fine-tuning methods (LoRA, Prefix Tuning, Prompt Tuning, P-Tuning) to Transformers and Accelerate.
- [Make LLM Fine-tuning 2x faster with Unsloth and 🤗 TRL](https://swe.quest/articles/make-llm-fine-tuning-2x-faster-with-unsloth-and-trl-5db8c0b6a40cf74e.md): Hugging Face · 2024-01-10 · Medium · 5 min read · Model training, LLMs, Performance engineering, Python
  Unsloth speeds up QLoRA fine-tuning of Llama and Mistral-family models by rewriting PyTorch modules as Triton kernels with manually derived backpropagation.
- [StackLLaMA: A hands-on guide to train LLaMA with RLHF](https://swe.quest/articles/stackllama-a-hands-on-guide-to-train-llama-with-rlhf-9d42cc2a0ae4620b.md): Hugging Face · 2023-04-05 · Hard · 14 min read · Model training, LLMs, Machine learning, Python
  A 7B LLaMA model is trained to answer Stack Exchange questions with the full RLHF pipeline: supervised fine-tuning on packed Stack Exchange text, a pairwise reward model trained on upvote-derived preference scores, and PPO with a KL penalty against a frozen reference model.
- [Fine-tuning Llama 2 70B using PyTorch FSDP](https://swe.quest/articles/fine-tuning-llama-2-70b-using-pytorch-fsdp-0e8dde34b4ad3fd0.md): Hugging Face · 2023-09-13 · Hard · 10 min read · Model training, LLMs, Hardware, Python
  Full fine-tuning of Llama 2 70B across two nodes of 8×A100 80GB GPUs with PyTorch FSDP, Hugging Face Accelerate, Transformers and TRL, addressing three obstacles.
- [What is LLM fine-tuning?](https://swe.quest/articles/what-is-llm-fine-tuning-818b56b81ea17fe8.md): Modal · 2024-12-10 · Medium · 13 min read · Model training, LLMs
  Fine-tuning adjusts a pretrained LLM's weights for a specific task or domain, often matching larger models' quality at lower cost and latency.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
