Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Hugging Face 14 min read How-to hardMaster

StackLLaMA: A hands-on guide to train LLaMA with RLHF

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. Memory is kept manageable by loading the model in 8-bit and training only LoRA adapters via peft, which needs roughly 1.2-1.4GB per billion parameters. The same frozen base serves as both policy and reference model, and data parallelism through accelerate or torchrun speeds up training. The reward model reaches 67% accuracy, and the RL run takes about 20 hours on 24 A100s. The post closes with practical notes on reward hacking and training instabilities.

Assumes: transformer language modelsPyTorch training loopsreinforcement learning basicsHugging Face Transformers

Related posts

  • Fine-tune Llama 2 with DPO

    Hugging Face hardMaster

    Direct Preference Optimization (DPO) replaces the reward-model and RL steps of RLHF with a simple binary cross-entropy loss over preference pairs, using a frozen reference model to provide an implicit reward. With TRL's DPOTrainer, data is formatted as prompt/chosen/rejected triples and training is controlled by a beta temperature, typically 0.1 to 0.5. The walkthrough fine-tunes Llama 2 7B on the stack-exchange preference dataset: first SFT with QLoRA (4-bit NF4 plus LoRA adapters), then DPO using the SFT model as both policy and reference. Progress is monitored with logged reward metrics (chosen, rejected, accuracies, margins), where margins should grow and accuracies should approach 1.0.

  • The N Implementation Details of RLHF with PPO

    Hugging Face hardMaster

    A from-scratch PyTorch reproduction of OpenAI's 2019 lm-human-preferences RLHF codebase closely matches the original learning curves on stylistic tasks like sentiment and descriptiveness. Along the way it catalogs the implementation details that matter for faithful replication: reward model and value head scoring the concatenated query and response, a dedicated out-of-vocabulary padding token, position-index adjustment for padding, fixed-length sampling that ignores EOS, learning-rate annealing, per-process seeds, reward normalization, rejection sampling and reward whitening. It also documents a TensorFlow vs PyTorch Adam numerical difference that causes overly aggressive updates, and how different base models behave when reward labels come from gpt2-large. Practical notes show how to replicate each behaviour with Hugging Face transformers, along with the hardware constraints of running the original TF1 code.

  • Putting RL back in RLHF

    Hugging Face hardMaster

    RLOO (REINFORCE Leave-One-Out) is now available as a trainer in Hugging Face TRL. It is an online RLHF algorithm that drops PPO's value model. It treats the whole completion as a single action, uses the REINFORCE loss, and computes each sample's baseline as the mean reward of the other k−1 completions for the same prompt. That cuts vRAM by roughly 50-70% and makes training 2-3x faster than PPO on Pythia 1B and 6.9B, while matching PPO's GPT-4-judged win rate (78.7% for 6.9B on TL;DR) and beating DPO. Code walkthroughs show the advantage computation and that REINFORCE is a special case of the PPO loss. There is a caveat: bf16 numerical mismatches between generation and training logprobs hurt RLOO more than PPO.

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

    Hugging Face hardMaster

    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.

  • TRL v1.0: Post-Training Library Built to Move with the Field

    Hugging Face mediumAdept

    TRL v1.0 turns Hugging Face's post-training codebase, now covering 75+ methods and downloaded 3M times a month, into a library with an explicit stability contract. A semver-governed stable core (SFT, DPO, reward modeling, RLOO, GRPO trainers) sits alongside a fast-moving trl.experimental namespace. Methods are promoted based on the ratio of usage to maintenance cost. Because post-training paradigms keep shifting (PPO to DPO to RLVR/GRPO), the design deliberately avoids shared base classes and generic abstractions and accepts near line-for-line duplication between trainers like RLOO and GRPO, illustrated by the failed Judge abstraction. A comparison against OpenRLHF, veRL, PRIME-RL, Tinker, LLaMA-Factory and others positions TRL as broad, low-infrastructure and stable but lacking native tensor parallelism, with asynchronous GRPO planned next.

The quest for software engineering knowledge.

Look:
Layout: