$npx -y skills add AlexAI-MCP/hermes-CCC --skill grpo-rl-trainingExpert guidance for GRPO/RL fine-tuning with TRL for reasoning and task-specific model training.
| 1 | # GRPO/RL Training with TRL |
| 2 | |
| 3 | Expert guidance for Group Relative Policy Optimization (GRPO) using the TRL library. Battle-tested patterns for fine-tuning language models with custom reward functions. |
| 4 | |
| 5 | ## When to Use |
| 6 | |
| 7 | - Teaching a model to follow specific output formats (JSON, structured reasoning) |
| 8 | - Improving accuracy on math, coding, or reasoning tasks |
| 9 | - Custom task-specific behavior without labeled datasets |
| 10 | - Distilling reasoning capabilities from larger models |
| 11 | |
| 12 | --- |
| 13 | |
| 14 | ## Setup |
| 15 | |
| 16 | ```bash |
| 17 | pip install transformers>=4.47.0 trl>=0.14.0 datasets>=3.2.0 peft>=0.14.0 torch accelerate |
| 18 | ``` |
| 19 | |
| 20 | Optional for logging: |
| 21 | ```bash |
| 22 | pip install wandb |
| 23 | wandb login |
| 24 | ``` |
| 25 | |
| 26 | --- |
| 27 | |
| 28 | ## Minimal GRPO Example |
| 29 | |
| 30 | ```python |
| 31 | from trl import GRPOConfig, GRPOTrainer |
| 32 | from transformers import AutoModelForCausalLM, AutoTokenizer |
| 33 | from datasets import load_dataset |
| 34 | |
| 35 | model_name = "Qwen/Qwen2.5-1.5B-Instruct" |
| 36 | model = AutoModelForCausalLM.from_pretrained(model_name) |
| 37 | tokenizer = AutoTokenizer.from_pretrained(model_name) |
| 38 | |
| 39 | dataset = load_dataset("your/dataset") |
| 40 | |
| 41 | # Define reward function |
| 42 | def reward_fn(prompts, completions, **kwargs): |
| 43 | """ |
| 44 | Returns list of floats — one reward per completion. |
| 45 | Higher = better. Typically in range [-1, 1] or [0, 1]. |
| 46 | """ |
| 47 | rewards = [] |
| 48 | for completion in completions: |
| 49 | # Example: reward for correct format |
| 50 | if completion.strip().startswith("<answer>"): |
| 51 | rewards.append(1.0) |
| 52 | else: |
| 53 | rewards.append(-0.5) |
| 54 | return rewards |
| 55 | |
| 56 | config = GRPOConfig( |
| 57 | output_dir="./grpo-output", |
| 58 | learning_rate=5e-6, |
| 59 | num_train_epochs=3, |
| 60 | per_device_train_batch_size=4, |
| 61 | gradient_accumulation_steps=4, |
| 62 | num_generations=8, # completions per prompt (G in GRPO) |
| 63 | max_prompt_length=512, |
| 64 | max_completion_length=256, |
| 65 | kl_coef=0.1, # KL penalty — start low, tune up if reward hacking |
| 66 | logging_steps=10, |
| 67 | save_steps=100, |
| 68 | report_to="wandb", # or "none" |
| 69 | ) |
| 70 | |
| 71 | trainer = GRPOTrainer( |
| 72 | model=model, |
| 73 | args=config, |
| 74 | reward_funcs=reward_fn, |
| 75 | train_dataset=dataset["train"], |
| 76 | tokenizer=tokenizer, |
| 77 | ) |
| 78 | |
| 79 | trainer.train() |
| 80 | trainer.save_model("./grpo-final") |
| 81 | ``` |
| 82 | |
| 83 | --- |
| 84 | |
| 85 | ## Reward Function Patterns |
| 86 | |
| 87 | ### Format Reward |
| 88 | ```python |
| 89 | import re |
| 90 | |
| 91 | def format_reward(prompts, completions, **kwargs): |
| 92 | pattern = r"<think>.*?</think>\s*<answer>.*?</answer>" |
| 93 | return [1.0 if re.fullmatch(pattern, c, re.DOTALL) else -1.0 for c in completions] |
| 94 | ``` |
| 95 | |
| 96 | ### Accuracy Reward |
| 97 | ```python |
| 98 | def accuracy_reward(prompts, completions, ground_truth, **kwargs): |
| 99 | rewards = [] |
| 100 | for completion, gt in zip(completions, ground_truth): |
| 101 | extracted = extract_answer(completion) |
| 102 | rewards.append(1.0 if extracted == gt else 0.0) |
| 103 | return rewards |
| 104 | ``` |
| 105 | |
| 106 | ### Length Penalty |
| 107 | ```python |
| 108 | def length_penalty_reward(prompts, completions, **kwargs): |
| 109 | return [max(0, 1.0 - len(c) / 2000) for c in completions] |
| 110 | ``` |
| 111 | |
| 112 | ### Combined Rewards |
| 113 | ```python |
| 114 | reward_funcs = [format_reward, accuracy_reward] # TRL averages them |
| 115 | ``` |
| 116 | |
| 117 | --- |
| 118 | |
| 119 | ## PEFT/LoRA Integration (Memory Efficient) |
| 120 | |
| 121 | ```python |
| 122 | from peft import LoraConfig, get_peft_model |
| 123 | |
| 124 | lora_config = LoraConfig( |
| 125 | r=16, |
| 126 | lora_alpha=32, |
| 127 | target_modules=["q_proj", "v_proj"], |
| 128 | lora_dropout=0.05, |
| 129 | bias="none", |
| 130 | task_type="CAUSAL_LM", |
| 131 | ) |
| 132 | |
| 133 | model = get_peft_model(model, lora_config) |
| 134 | model.print_trainable_parameters() |
| 135 | ``` |
| 136 | |
| 137 | --- |
| 138 | |
| 139 | ## Key Hyperparameters |
| 140 | |
| 141 | | Param | Default | Effect | |
| 142 | |-------|---------|--------| |
| 143 | | `num_generations` | 8 | More = better gradient estimate, more memory | |
| 144 | | `kl_coef` | 0.1 | Higher = stays closer to reference model | |
| 145 | | `learning_rate` | 5e-6 | Lower than SFT — RL is unstable with high LR | |
| 146 | | `max_completion_length` | 256 | Controls max tokens generated | |
| 147 | |
| 148 | --- |
| 149 | |
| 150 | ## Common Pitfalls |
| 151 | |
| 152 | **Reward Hacking**: Model finds shortcuts to maximize reward without actually improving. |
| 153 | → Fix: Add KL penalty (`kl_coef`), diverse reward signals, human evaluation |
| 154 | |
| 155 | **Mode Collapse**: All completions become similar. |
| 156 | → Fix: Increase `num_generations`, add temperature, check reward diversity |
| 157 | |
| 158 | **OOM**: Large models with many generations blow up memory. |
| 159 | → Fix: Use LoRA, reduce `num_generations`, use `gradient_checkpointing=True` |
| 160 | |
| 161 | **Reward too sparse**: Model rarely gets positive reward → no learning signal. |
| 162 | → Fix: Start with easier examples, use shaped reward (partial credit) |
| 163 | |
| 164 | --- |
| 165 | |
| 166 | ## Monitoring with W&B |
| 167 | |
| 168 | Key metrics to watch: |
| 169 | - `train/reward`: should increase over time |
| 170 | - `train/kl`: should stay bounded (spike = reward hacking) |
| 171 | - `train/policy_loss`: learning signal |
| 172 | - `train/entropy`: diversity of completions (drop = mode collapse) |
| 173 | |
| 174 | --- |
| 175 | |
| 176 | ## Checkpoint and Resume |
| 177 | |
| 178 | ```bash |
| 179 | # Resume f |