Reinforcement Learning and Language Model Alignment (VTR UGE 21), day 4 of 5

Reinforcement Learning for Language Models

Prof. Dr. Utku Kose, Süleyman Demirel University

RLHF lab

Part A names the stages of reinforcement learning from human feedback (RLHF). Parts B and D repeat two calculations of the lecture step by step: the penalised rewards and advantages of one batch, and the optimum of the penalised objective for three outputs. Part C computes a Bradley-Terry preference and a penalised reward by hand. Part E turns the weight of the Kullback-Leibler (KL) penalty on real outputs of the notebook and shows the optimum of the penalised objective. Part F weighs helpfulness against harmlessness on six requests. Part G lets you label ten pairs yourself and trains a reward model on your labels.

Part A: Which stage of the pipeline?

Name the stage of RLHF that each description refers to.

Back to the lecture: RLHF with a KL penalty, where the text continues after the box of this part.

Part B: Penalised rewards and advantages of one batch

The worked example of the lecture: four sentences of one batch of RLHF, each with its score from the reward model and its log-ratio against the fine-tuned model. The sliders set the KL weight \(\beta\), the four scores and the four log-ratios. Values are rounded to three decimals.

Back to the lecture: RLHF with a KL penalty, where the text continues after the box of this part.

Part C: A preference and a penalised reward by hand

The reward model scores answer A with 1.2 and answer B with 0.4. During RLHF, an answer receives the reward 2.0 from the reward model, the log-ratio of its probability under the new and the fine-tuned model is 3.0, and the KL weight is 0.2.

Back to the lecture: RLHF with a KL penalty. The lecture continues there with Section 6 of the Colab notebook: Open it from the next orange box.

Part D: The optimum of the KL-regularised objective for three outputs

The worked example of the lecture: A reference policy writes three outputs, the reward model scores them, and the optimum of RLHF multiplies each reference probability by \(e^{r/\beta}\) and normalises. The sliders set the reference probabilities of the first two outputs, the three scores and \(\beta\). Lower \(\beta\) and watch the rare output with the highest score take over.

Back to the lecture: Without the leash: reward hacking, where the text continues after the box of this part.

Part E: The KL leash on real outputs

The candidates below are real outputs of the notebook: The most likely sentences of the fine-tuned model and every sentence that the reinforcement-learning runs produced outside the grammar. For each one the lab knows its probability under the reference model, its reward-model score and its gold reward. For a KL weight beta, the optimal policy is proportional to the reference probability times exp(reward-model score / beta). Lower beta and watch what it buys.

This is the optimum of the KL-regularised objective from the lecture, computed over the 392 candidates only, with their reference probabilities rescaled to add up to one. The reference model is the fine-tuned model, and beta is the weight of the KL penalty. The horizontal axis shows the logarithm of beta to base 10: -1 stands for 0.1 and 0 for 1. The dashed line, the share of probability on sentences of the grammar, is drawn at four times its value, so the height 4 means 100 percent.

The ten most probable outputs at this beta

ProbabilityOutputReward modelGoldIn the grammar

Back to the lecture: Without the leash: reward hacking. The lecture continues there with Section 7 of the Colab notebook: Open it from the next orange box.

Part F: Helpful and harmless, one weight between them

The illustration of the lecture: six requests to an assistant, three harmless and three harmful, each with three possible answers, to comply, to give a careful partial answer or to refuse. Each answer has a helpfulness score and a harmlessness score between 0 and 1. The scores are invented for the illustration. The reward of an answer is \(r = (1 - w)\, r_{\mathrm{help}} + w\, r_{\mathrm{harm}}\), and the assistant gives the answer with the highest reward. The slider sets the weight \(w\) of harmlessness, and the figure counts the two kinds of failure for every weight.

Back to the lecture: Safe and ethical alignment. The lecture continues there with Section 8 of the Colab notebook: Open it from the next orange box.

Part G: Be the labeller

In this part, you label the pairs, so the reward model learns your taste. Each of the ten pairs below holds two sentences of the grammar of the lecture: Choose the one you prefer.

The lab then trains a small reward model on your labels. It describes a sentence by three numbers: its different calm words, its different loud words and its words beyond the sixth, the features of the gold reward. Its score is a weighted sum of them, \(r(y)\)​\({} = w_{\mathrm{calm}}\, n_{\mathrm{calm}}\)​\({} + w_{\mathrm{loud}}\, n_{\mathrm{loud}}\)​\({} + w_{\mathrm{long}}\, n_{\mathrm{long}}\), and it is trained with the Bradley-Terry loss of the lecture, \(P(A \succ B) = \sigma(r(A) - r(B))\), with a small penalty that keeps the weights small. The lab compares the weights with those of the gold reward, 0.45, −0.45 and −0.10, and computes the correlation of the scores with the gold reward over all 1512 sentences of the grammar.

Label the pairs twice: once so that the reward model agrees with the gold reward, with a correlation of at least 0.9, and once with a lively taste that prefers loud words, so that the correlation is −0.5 or lower. Then answer the questions below by hand.

By hand: A linear reward model has the weights 0.2 for calm words, 0.1 for loud words and 0 for long sentences. In a labelled pair, the winner has the features (2, 0, 1) and the loser (0, 1, 0). A step of gradient descent on the Bradley-Terry loss of this pair, with the step size 0.5, changes every weight by \(0.5\, (1 - \sigma(\Delta))\) times the difference of its feature between winner and loser, where \(\Delta\) is the score of the winner minus the score of the loser.

Back to the lecture: Your field. The lecture continues there with Section 9 of the Colab notebook: Open it from the next orange box.