trl icon indicating copy to clipboard operation
trl copied to clipboard

Negative loss and KL when using PPO trainer with llama2

Open Loreley99 opened this issue 3 years ago • 9 comments

Thank you so much for such a great library. But I'm having some problems trying to train with the PPO trainer for llama2-7b. I found that my training charts are strange, the loss appears negative and the KL oscillates between positive and negative. image image

I have checked other related issues,I was not able to find a solution. My setup details are as follows: config = PPOConfig( steps=50000, model_name="meta-llama/Llama-2-7b-hf", learning_rate=1.41e-5, log_with='wandb', batch_size=512, mini_batch_size=16, gradient_accumulation_steps=4, optimize_cuda_cache=True, early_stopping=False, target_kl=1, ppo_epochs=4, seed=0, init_kl_coef=0.2, adap_kl_ctrl=True, ) generation_kwargs = { "min_length": -1, "min_new_tokens": 10, "top_k": 0.0, "top_p": 1.0, "do_sample": True, "pad_token_id": tokenizer.eos_token_id, "eos_token_id":tokenizer.eos_token_id, "no_repeat_ngram_size":3, "max_new_tokens":50, } I have limited knowledge in this area and would appreciate your guidance, thanks very much!

Loreley99 avatar Nov 21 '23 00:11 Loreley99

Can you try to remove: "min_new_tokens": 10? That can lead to negative KL, see: https://huggingface.co/docs/trl/how_to_train#how-to-generate-text-for-training

lvwerra avatar Nov 21 '23 08:11 lvwerra

Can you try to remove: "min_new_tokens": 10? That can lead to negative KL, see: https://huggingface.co/docs/trl/how_to_train#how-to-generate-text-for-training

Thanks for your qucik reply, I removed min_new_tokens, but the loss/policy and KL are still negative: image image

Loreley99 avatar Nov 21 '23 17:11 Loreley99

Very weird. Can you try switching to GPT-2 just to narrow down the issue? Have you tried running the example script without any modifications?

lvwerra avatar Nov 24 '23 14:11 lvwerra

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

github-actions[bot] avatar Dec 21 '23 15:12 github-actions[bot]

hi I am also encountering this issue with llama-2-7b right now. the strange thing is I started with the llama base model without SFT and it converges just fine, but when I apply the llama model with SFT for ppo with the same set of hyperparameter this problem starts to appear. just wondering if there are any updates on this matter?

lindseyfeng avatar Jan 13 '24 15:01 lindseyfeng

I also had the same problem. I believe that Lora-Finetuning an already Lora fine-tuned SFT model causes this problem. After using a full-finetuned SFT model, the problem goes away.

Ali1858 avatar Jan 18 '24 19:01 Ali1858

I think this is because the kl_penalty is set to 'kl', in these mode the kl is calculate only on the target tokens, which is the approximate KL divergence. If you set kl_penalty to 'full', the kl will always be positive. https://github.com/huggingface/trl/blob/main/trl/trainer/ppo_trainer.py

underwoodnoble avatar Apr 24 '24 14:04 underwoodnoble

Using LoRA fine-tuning, this issue occurred, but when fine-tuning with all parameters, the problem disappeared. So, could it be due to LoRA dropout?

HaohzW avatar Feb 09 '25 13:02 HaohzW

Hi, I am getting 0 KL for my PPO training for my text generation to have a fixed length. I have been using llma3.2 1B model with learning rate 1e-4. Anyone came across 0 KL issue? Mean reward is in the range 0.2. Have trained for 5 epochs, 5k dataset.

nimithau avatar Apr 17 '25 12:04 nimithau