RLHF 的高效微調

Reinforcement Learning from Human Feedback(RLHF)

Mina Parham

AI Engineer

參數高效微調

  • 微調整個模型

代表神經網路的示意圖。

Reinforcement Learning from Human Feedback(RLHF)

參數高效微調

  • 使用 PEFT 微調

代表大多數參數凍結之神經網路的示意圖。

  • LoRA:只調整少數層
  • 量化:降低資料型別精度
Reinforcement Learning from Human Feedback(RLHF)

步驟 1:以 8-bit 精度載入作用中模型

from peft import prepare_model_for_int8_training

pretrained_model = AutoModelForCausalLM.from_pretrained( model_name, load_in_8bit=True )
pretrained_model_8bit = prepare_model_for_int8_training(pretrained_model)
Reinforcement Learning from Human Feedback(RLHF)

步驟 2:用 peft 加入可訓練的 adapter

from peft import LoraConfig, get_peft_model


config = LoraConfig(
r=32, # Rank of the low-rank matrices
lora_alpha=32, # Scaling factor for the LoRA updates
lora_dropout=0.1, # Dropout rate for LoRA layers
bias="lora_only"# Only update bias terms for LoRA layers, others remain frozen
)
lora_model = get_peft_model(pretrained_model_8bit, config) model = AutoModelForCausalLMWithValueHead.from_pretrained(lora_model)
Reinforcement Learning from Human Feedback(RLHF)

步驟 3:以單一模型同時產生 reference 與 active logits

ppo_trainer = PPOTrainer(
    config, # The config we just defined
    model, # Our PPO model
    ref_model=None, 
    tokenizer=tokenizer, 
    dataset=dataset, 
    data_collator=collator, 
    optimizer=optimizer
)
Reinforcement Learning from Human Feedback(RLHF)

一起來練習吧!

Reinforcement Learning from Human Feedback(RLHF)

Preparing Video For Download...