trl icon indicating copy to clipboard operation
trl copied to clipboard

PPOTrainer: Right way to generate text during inference ?

Open Ali1858 opened this issue 2 years ago • 3 comments

Hi,

I fine-tune the LLAMA2-7B model using PPOTrainer, for fine-tuning, I use the following generation kwargs:

generation_kwargs = {
        "pad_to_multiple_of":16,
        "top_k": 0.0,
        "top_p": 1.0,
        "do_sample": True,
        "pad_token_id": tokenizer.pad_token_id,
        "eos_token_id": tokenizer.eos_token_id,#,100_000,
        "max_new_tokens": max_new_tokens,
    }

and got little improvement in the mean reward score Screenshot 2023-12-14 at 02 18 43 Screenshot 2023-12-14 at 02 30 19

During training the difference between the mean reward of the active and base model is 0.7 on the evaluation dataset and I am satisfied with the result. However, during inference, with below generation_kwargs both the RLHF fine-tune model and base SFT model are performing equally well (mean reward of ~2.3) on the alpaca_eval dataset (100 prompts). Meaning, There is no significant advantage of RLHF fine-tuning.

When the training generation_kwargs, i.e. with top_p=1 and temperature=1 then the RLHF model performs better than the SFT model.

generation_kwargs = {
        "top_k": 0,
        "top_p": 0.9,
        "do_sample": True,
        "temperature":0.8,
        "max_new_tokens":512
        }

During the Inference I am RLHF and SFT models with

     rlhf_model = transformers.AutoModelForCausalLM.from_pretrained(
           conf.model_name,
           use_flash_attention_2=True,
           load_in_8bit=True,
           device_map=device_map,
          torch_dtype=dtype,
          cache_dir=CACHE_DIR,
     )
    # # Load the Lora model
    
    rlhf_model = PeftModel.from_pretrained(base_model,
                                            conf.peft_id+'/final_checkpoint',
                                             adapter_name="rlhf",
                                             is_trainable=False,
                                             device_map=device_map
                                             )
   rlhf_model.set_adapter("rlhf")

   sft_model = transformers.AutoModelForCausalLM.from_pretrained(
           conf.model_name,
           use_flash_attention_2=True,
           load_in_8bit=True,
           device_map=device_map,
          torch_dtype=dtype,
          cache_dir=CACHE_DIR,
     )

I am loading the model incorrectly or setting generation kwargs incorrectly during training? Is it possible to have an example for inference as well?

Ali1858 avatar Dec 14 '23 01:12 Ali1858

Interesting, maybe the data you are training with is different than the prompts from alpaca_eval? In general I would try the same generation kwargs you used during training, there the active model should be better, although i wouldn't expect major difference with the slight modifications you made.

Another note: the KL seems quite high towards the end of training (usually ~10 is an ok value).

In summary here's what I would do to debug:

  1. Use the same generation kwargs as during training
  2. Use the same prompts as used during training
  3. Slowly change the 1 + 2 and see when you start to notice the difference

Hope this helps!

lvwerra avatar Dec 21 '23 14:12 lvwerra

Interesting, maybe the data you are training with is different than the prompts from alpaca_eval?

I used the oasst dataset for training, model is behaving similarly even with the evaluation set from the same dataset

The KL seems quite high towards the end of training (usually ~10 is an ok value).

Although KL is higher it does not have any impact on reward and generated responses are relevant and coherent. However, after using the kl-coeff value KL is around 20, which should be better.

In general I would try the same generation kwargs you used during training, there the active model should be better, although i wouldn't expect major difference with the slight modifications you made.

Yes, if I use same generation kwargs used for training then active model is better than sft.

Thanks for your suggestion. I will try them.

Ali1858 avatar Dec 23 '23 13:12 Ali1858

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 Jan 16 '24 15:01 github-actions[bot]