Logit Gradient Correction: Deriving Score Centering from Logit Gradients

This post derives SCORE CENTERING from removing a bias term in the logit gradient before it is propagated through the logit Jacobian via chain rule, and then derives the corresponding surrogate objective.

Background: REINFORCE and the Logit Gradient

At a fixed prefix 𝒙, a trainer with 𝐷 parameters πœ½βˆˆβ„π· produces logits 𝒛(𝒙,𝜽)βˆˆβ„π‘‰ over a vocabulary of size 𝑉. Its probability vector 𝒑=softmax(𝒛)βˆˆβ„π‘‰ defines the trainer distribution 𝑃, with 𝑃(π‘Ž)=π‘π‘Ž. A sampler at the same prefix generates token π‘ŽβˆΌπ‘„, with probabilities π’’βˆˆβ„π‘‰ and 𝑄(π‘Ž)=π‘žπ‘Ž. During backward, the recorded 𝒒 and reward π‘…π‘Žβˆˆβ„ are held fixed. Scalars use plain letters, vectors bold lowercase, and matrices bold uppercase.

REINFORCE wants to increase the trainer’s expected reward 𝐽𝑃 under the trainer’s own distribution 𝑃:

𝐽𝑃=π”Όπ‘ŽβˆΌπ‘ƒ[π‘…π‘Ž]=βˆ‘π‘Žπ‘π‘Žπ‘…π‘Ž.

Holding the reward function fixed, differentiate 𝐽𝑃 with respect to 𝜽 to obtain βˆ‡πœ½π½π‘ƒβˆˆβ„π·:

βˆ‡πœ½π½π‘ƒ=βˆ‘π‘Žπ‘…π‘Žβˆ‡πœ½π‘π‘Ž=βˆ‘π‘Žπ‘π‘Žπ‘…π‘Žβˆ‡πœ½logπ‘π‘Ž=π”Όπ‘ŽβˆΌπ‘ƒ[π‘…π‘Žβˆ‡πœ½logπ‘π‘Ž].

The term π‘…π‘Žβˆ‡πœ½logπ‘π‘Žβˆˆβ„π· is REINFORCE’s per-sample gradient contribution. With π‘…π‘Ž held fixed, it equals the negative gradient with respect to 𝜽 of the reward-weighted cross-entropy loss βˆ’π‘…π‘Žlogπ‘π‘Ž.

We first differentiate logπ‘π‘Ž with respect to the logits 𝒛. Given that 𝒑=softmax(𝒛), we have

βˆ‡π’›logπ‘π‘Ž=π’†π‘Žβˆ’π’‘βˆˆβ„π‘‰.

Here, π’†π‘Žβˆˆβ„π‘‰ is the sampled token’s one-hot vector. The gradient π’†π‘Žβˆ’π’‘ contains the derivatives of logπ‘π‘Ž with respect to the logits of all vocabulary tokens. The mean logits contribution on the right therefore determines the mean parameter contribution.

Log-Prob Gradients Have Zero/Nonzero Bias when On/off-Policy

We consider a scenario that π‘…π‘Ž=1 for every token, we have:

π”Όπ‘ŽβˆΌπ‘ƒ[π‘…π‘Žβˆ‡π’›logπ‘π‘Ž]=π”Όπ‘ŽβˆΌπ‘ƒ[βˆ‡π’›logπ‘π‘Ž]=π”Όπ‘ŽβˆΌπ‘ƒ[π’†π‘Žβˆ’π’‘]=π”Όπ‘ŽβˆΌπ‘ƒ[π’†π‘Ž]βˆ’π’‘βˆˆβ„π‘‰.

Thus the gradient of 𝐽𝑃 with respect to the model parameters 𝜽 will be

βˆ‡πœ½π½π‘ƒ=π”Όπ‘ŽβˆΌπ‘ƒ[π‘…π‘Žβˆ‡πœ½logπ‘π‘Ž]=(πœ•π’›πœ•πœ½)βŠ€π”Όπ‘ŽβˆΌπ‘ƒ[π‘…π‘Žβˆ‡π’›logπ‘π‘Ž]=(πœ•π’›πœ•πœ½)⊀(π”Όπ‘ŽβˆΌπ‘ƒ[π’†π‘Ž]βˆ’π’‘)βˆˆβ„π·Γ—π‘‰Γ—β„π‘‰=ℝ𝐷.

The matrix πœ•π’›πœ•πœ½βˆˆβ„π‘‰Γ—π· is the trainer’s logit Jacobian: it describes how the logits change with the model parameters. Its transpose maps a 𝑉-dimensional logits gradient to a 𝐷-dimensional parameter gradient, applying the chain rule through backpropagation. At a fixed prefix and parameter value, this matrix is the same for every possible sampled token, so it can be moved outside the expectation. Score Centering supplies a corrected logits gradient to this same backward pass.

If the tokens are generated by the trainer distribution 𝑃, the expectation π”Όπ‘ŽβˆΌπ‘ƒ[π’†π‘Ž] equals 𝒑,

βˆ‡πœ½π½π‘ƒ=(πœ•π’›πœ•πœ½)⊀(π”Όπ‘ŽβˆΌπ‘ƒ[π’†π‘Ž]βˆ’π’‘)=(πœ•π’›πœ•πœ½)⊀(π’‘βˆ’π’‘)=𝟎.

the logits gradient is unbiased and has zero mean when the reward is constant.

Logit Gradient Correction

If tokens instead come from the sampler distribution 𝑄, then π”Όπ‘ŽβˆΌπ‘„[π’†π‘Ž]=𝒒. Still assuming π‘…π‘Ž=1, the mean trainer log-probability gradient becomes:

π”Όπ‘ŽβˆΌπ‘„[βˆ‡πœ½logπ‘π‘Ž]=(πœ•π’›πœ•πœ½)⊀(π”Όπ‘ŽβˆΌπ‘„[π’†π‘Ž]βˆ’π’‘)=(πœ•π’›πœ•πœ½)⊀(π’’βˆ’π’‘)βˆˆβ„π·.

Here 𝑄 specifies where tokens come from; the differentiated log-probability still belongs to 𝑃. This expression is the mean sampled contribution, not the gradient of the sampler’s expected reward.

Since every reward is 1, the trainer objective 𝐽𝑃=1 has zero gradient. We want the corrected contributions to have zero mean even when tokens come from 𝑄. Shift each one-hot contribution π’†π‘Ž by π’‘βˆ’π’’, giving π”Όπ‘ŽβˆΌπ‘„[π’†π‘Žβˆ’π’’+𝒑]=π’’βˆ’π’’+𝒑=𝒑.

Let 𝐽 denote the per-sample surrogate objective that we will construct in the next section. We require its expected parameter gradient to equal the corrected mean contribution. With 𝑄 held fixed during backward, this requirement is:

βˆ‡πœ½π”Όπ‘ŽβˆΌπ‘„[𝐽]=π”Όπ‘ŽβˆΌπ‘„[βˆ‡πœ½π½]=(πœ•π’›πœ•πœ½)⊀(π”Όπ‘ŽβˆΌπ‘„[π’†π‘Žβˆ’π’’+𝒑]βˆ’π’‘)=(πœ•π’›πœ•πœ½)⊀(π”Όπ‘ŽβˆΌπ‘„[π’†π‘Žβˆ’π’’+π’‘βˆ’π’‘])=(πœ•π’›πœ•πœ½)⊀(π”Όπ‘ŽβˆΌπ‘„[π’†π‘Žβˆ’π’’])=(πœ•π’›πœ•πœ½)⊀(π’’βˆ’π’’)=𝟎.

The essential of this correction is to replace each token’s logits gradient contribution π’†π‘Žβˆ’π’‘ with π’†π‘Žβˆ’π’’. Averaging under the sampler distribution 𝑄 then gives βˆ‘π‘Žπ‘žπ‘Ž(π’†π‘Žβˆ’π’’)=𝟎, restoring zero mean for constant rewards.

From Corrected Logit Gradient to a Surrogate Objective

We now construct the per-sample surrogate 𝐽. For constant reward π‘…π‘Ž=1, the required relation is:

βˆ‡πœ½π”Όπ‘ŽβˆΌπ‘„[𝐽]=(πœ•π’›πœ•πœ½)βŠ€π”Όπ‘ŽβˆΌπ‘„[π’†π‘Žβˆ’π’’].

For varying rewards, we weight each corrected contribution by π‘…π‘Ž. The required gradient of the expected surrogate is

βˆ‡πœ½π”Όπ‘ŽβˆΌπ‘„[𝐽]=(πœ•π’›πœ•πœ½)βŠ€π”Όπ‘ŽβˆΌπ‘„[π‘…π‘Ž(π’†π‘Žβˆ’π’’)].

First, we seek a scalar surrogate objective 𝐽 in terms of the logits 𝒛, and whose gradient with respect to the logits 𝒛 is:

βˆ‡π’›π½=π‘…π‘Ž(π’†π‘Žβˆ’π’’)βˆˆβ„π‘‰.

The required gradient π‘…π‘Ž(π’†π‘Žβˆ’π’’) is constant with respect to 𝒛. An intuitive antiderivative is a linear function whose gradient equals its coefficient vector:

𝐽=π‘…π‘Ž(π’†π‘Žβˆ’π’’)βŠ€π’›=π‘…π‘Ž(π’†π‘ŽβŠ€π’›βˆ’βˆ‘π‘£sg[π‘žπ‘£]𝑧𝑣)=π‘…π‘Ž(π‘§π‘Žβˆ’βˆ‘π‘£sg[π‘žπ‘£]𝑧𝑣).

The last line uses π’†π‘ŽβŠ€π’›=π‘§π‘Ž. The sum βˆ‘π‘£sg[π‘žπ‘£]𝑧𝑣 is the sampler-weighted average of the trainer’s logits over the entire vocabulary. The operator sg holds the probabilities fixed during differentiation while preserving gradients through the logits.

Second, we express the surrogate objective 𝐽 in terms of log-probabilities. Softmax gives log𝑝𝑣=π‘§π‘£βˆ’log(βˆ‘π‘—exp(𝑧𝑗)) for every token 𝑣. Using βˆ‘π‘£sg[π‘žπ‘£]=1, we obtain:

π‘§π‘Žβˆ’βˆ‘π‘£sg[π‘žπ‘£]𝑧𝑣=(π‘§π‘Žβˆ’log(βˆ‘π‘—exp(𝑧𝑗)))βˆ’[βˆ‘π‘£sg[π‘žπ‘£]π‘§π‘£βˆ’log(βˆ‘π‘—exp(𝑧𝑗))]=(π‘§π‘Žβˆ’log(βˆ‘π‘—exp(𝑧𝑗)))βˆ’[βˆ‘π‘£sg[π‘žπ‘£]π‘§π‘£βˆ’(βˆ‘π‘£sg[π‘žπ‘£])⏟=1log(βˆ‘π‘—exp(𝑧𝑗))]=(π‘§π‘Žβˆ’log(βˆ‘π‘—exp(𝑧𝑗)))βˆ’βˆ‘π‘£sg[π‘žπ‘£](π‘§π‘£βˆ’log(βˆ‘π‘—exp(𝑧𝑗)))=logπ‘π‘Žβˆ’βˆ‘π‘£sg[π‘žπ‘£]log𝑝𝑣.

We have the surrogate objective in terms of log-probability:

𝐽=π‘…π‘Ž[logπ‘π‘Žβˆ’βˆ‘π‘£sg[π‘žπ‘£]log𝑝𝑣]=π‘…π‘Ž[logπ‘π‘Žβˆ’π”Όπ‘£βˆΌπ‘„[log𝑝𝑣]].

The surrogate loss replaces the per-sample REINFORCE objective π‘…π‘Žlogπ‘π‘Ž with π‘…π‘Ž times the difference between the sampled token’s trainer log-probability and the 𝑄-weighted average of the trainer’s log-probabilities over the vocabulary.

Averaging the per-sample surrogate over tokens drawn from 𝑄 gives:

π”Όπ‘ŽβˆΌπ‘„[𝐽]=π”Όπ‘ŽβˆΌπ‘„[π‘…π‘Ž[logπ‘π‘Žβˆ’π”Όπ‘£βˆΌπ‘„[log𝑝𝑣]]].