Author : Jiacai Liu

Email : [email protected]

url : https://www.notion.so/Brief-Introduction-of-Policy-Gradient-In-LLM-Reasoning-1c04795a3e8b805abbd6ccc9f1a34ac0

Basic Ideas

1. Notation & Objective

Recently, reinforcement learning (RL) methods, especially the policy gradient methods, have achieved great success in enhancing the reasoning abilities of LLM by optimizing its token generation policy. The token generation policy of one LLM can be represented as a policy $\pi$. Given the text prefix $s$, the next token $a$ is sampled from the distribution $\pi(\cdot|s) \in \Delta(\mathcal{V})$. Here we use $\Delta(\mathcal{V})$ as the probability simplex on vocabulary $\mathcal{V}$. With a slightly abuse of this notation, for any sequence of tokens $\hat{y}$ with length $\hat{T}$ and input prefix $x$, we also use $\pi(\hat{y}|\hat{x})$ as the sequence generation policy and

$$ \begin{align}\pi \left( \hat{y}|\hat{x} \right) =\prod_{t=0}^{\hat{T}-1}{\pi \left( \hat{y}t|\left( \hat{x},\hat{y}{<t} \right) \right) .}\end{align} $$

Here $\hat{y}{t}$ is the token in the sequence at index $t$ and $\hat{y}{<t}:=(\hat{y}0,...,\hat{y}{t-1})$. In order to enhance the reasoning abilities of LLM, we often want to find a new policy that maximizes the reward function $\mathcal{r}$ in the trust region of reference policy $\pi_\text{ref}$, i.e.

$$ \begin{align} \underset{\pi}{\max}\,\,\left\{ \mathcal{J} \left( \textcolor{red}{\pi} \right) :=\mathbb{E} _{x\sim \mathcal{D}}\left[ \mathbb{E} _{y\sim \textcolor{red}{\pi} \left( \cdot |x \right)}\left[ r\left( x,y \right) \right] -\beta \,\mathrm{KL}\left( \textcolor{red}{\pi_x},\pi _{x}^{\mathrm{ref}} \right) \right] \right\}, \end{align} $$

where $y=(a_0,...,a_{T-1})$ is the response of prompt $x$, $a_t$ is the token in the response at index $t$, $\mathcal{D}$ is the sampling distribution of $x$, $\beta$ is the coefficient of KL regularization, $\pi_x,\pi^{\text{ref}}x$ is short for $\pi(\cdot|x)$ and $\pi{\text{ref}}(\cdot|x)$ respectively. For parameterized policy $\pi_\theta$, we use $\mathcal{J}(\theta):=\mathcal{J}(\pi_\theta)$ for ease of notation.

2. Policy Gradient

Policy gradient methods solve the optimal policy by gradient ascent directly, i.e.

$$ \theta ^{k+1}=\theta ^k+ \eta_k\nabla _{\theta}\mathcal{J} \left( \theta ^k \right). $$

where $\eta_k$ is the step size. Following theorem gives an expression of $\nabla_\theta\mathcal{J}(\theta)$.

Theorem 1 (Policy Gradient Theorem) For parameterized policy $\pi_\theta$,

$$ \begin{align} \nabla _{\theta}\mathcal{J} \left( \textcolor{red}{\theta} \right) =\mathbb{E} _{x\sim \mathcal{D}}\mathbb{E} _{y\sim \pi _{\textcolor{red}{\theta}}\left( \cdot |x \right)}\left[ \left( r\left( x,y \right) -\beta\log \frac{\pi _{\textcolor{red}{\theta}}\left( y|x \right)}{\pi _{\mathrm{ref}}\left( y|x \right)} \right) \nabla _{\theta}\log \pi _{\textcolor{red}{\theta}}\left( y|x \right) \right] \end{align} $$

Note that the response $y$ is a sequence of tokens, thus by (1), we know that

$$ \begin{align*}\log \pi {\theta}\left( y|x \right) &=\log \prod{t=0}^{T-1}{\pi {\theta}\left( a_t|\left( x,a_0,...,a{t-1} \right) \right) .} \\ \,\, &=\log \prod_{t=0}^{T-1}{\pi {\theta}\left( a_t|s_t \right)} \\ \,\, &=\sum{t=0}^{T-1}{\log \pi _{\theta}\left( a_t|s_t \right)},\end{align*} $$

where $s_t:=\left( x,a_0,...,a_{t-1} \right)$. Plugging it into (3) we obtain following token-level policy gradient theorem directly.