AI Hero Club

LLM Post-training · Lesson 2

Marked Complete by You0 / 8

Saved in this browser only. Marking a lesson complete does not mean the project has passed verification.

Lesson Outline 7 subsections
Course Contents 8 lessons
Full course plan.
LLM Post-training / Learning Plan ↗

LESSON 02 / 8Suggested study 120 minutes

SFT: From Response Likelihood to Full-parameter Gradients

After joining a prompt and response, how do we score only response tokens and EOS, then backpropagate through every parameter?

3 Questions for This Lesson
  • Gradually decipher the p−y gradient for softmax cross-entropy and update all parameters along a sexual policy
  • Answer to Dotoken with the correct location of the response-only misallocation mask, and keep the first answer to Token and EOS
  • Distinguishing between oversight objectives by answer, by token and by model repeat frequency
Course Project ZIP ↓

Task and Prerequisites

This chapter provides a fine-tuning of monitoring, SFT in English. The input is a reminder and a model answer to y. The task is to increase the probability of a demonstration. Readers need to have access to the four candidate strategies of the previous chapter, as well as to the one-digit index, logarithm and chain-style rules. We first push out the gradients for all eight parameters in a limited number of candidates, and then explain the demands and masks of the language model with many token answers. The chapter does not rename the four classification references as language models; it only tests the same condition in a minimal compute form.

For hint x, actiona logit is za=ba+waxz_a=b_a+w_ax. Define probability pa=exp⁡(za)/∑jexp⁡(zj)p_a=\exp(z_a)/\sum_j\exp(z_j). As long as the logit is limited, all probability is strict and one. Logit itself can be negative, larger than one, not credible; adding the same constant to all categories does not change softmax. This gradient describes parameters that are not the only direction to be determined by observation, but does not affect distribution and target definition. Values are achieved by subtracting the maximum logit, then calculating indexes and avoiding large positive spillovers.

From Likelihood to Cross-Entropy

A model category k appears to be pkp_k, negative log [N] ℓ=−zk+log⁡∑jexp⁡(zj)\ell=-z_k+\log\sum_j\exp(z_j). This is the precise algebraic variable: the softmax molecule denominators are logged, and the logit difference is given to the homogenization item. For jlogit, the guide to the homogenization item is exp⁡(zj)/∑iexp⁡(zi)=pj\exp(z_j)/\sum_i\exp(z_i)=p_j negative. zkz_k The indicator of negative contribution, i.e. j equals k minus one. ∂ℓ/∂zj=pj−1[j=k]\partial\ell/\partial z_j=p_j-\mathbf1[j=k]. The sum of the gradients is zero, which corresponds to the common equalisation does not change the loss.

ℓ=−zk+log⁡∑iezi,∂ℓ∂zj=−1[j=k]+ezj∑iezi=pj−1[j=k].\begin{aligned}\ell&=-z_k+\log\sum_i e^{z_i},\\ \frac{\partial\ell}{\partial z_j}&=-\mathbf1[j=k]+\frac{e^{z_j}}{\sum_i e^{z_i}}=p_j-\mathbf1[j=k].\end{aligned}
In the case of unimodalization, the indicator value is given only by the model category.

Then you can guide the actual parameters. zjz_j Yeah. bjb_j One, yes. wjw_j The function is x, so the gradient is. pjp_j Deflates the target indicator. The gradient is multiplied by x. The parameters of other categories also have gradients, which do not add points to the correct category alone, regardless of the denominator. The N model is averaged and the gradients are added by strip, with the N divided. The project achieves direct accumulation of the eight scales and then the learning rate is subtracted from the parameter; there is no automatic mini-black box or freezing of a layer of parameters.

∂ℓ∂ba=pa−1[a=k],∂ℓ∂wa=x(pa−1[a=k]).\frac{\partial\ell}{\partial b_a}=p_a-\mathbf1[a=k],\qquad \frac{\partial\ell}{\partial w_a}=x(p_a-\mathbf1[a=k]).
A gradient of all parameters for the limited four candidate strategies; sample mean divided by demonstration.

Example 1: One Update for One Prompt

Sets x=2, all four logits are zero, and the true demonstration is No. 1 “Positive,Short”. Each probability is one quarter and loss log4. The gradient of preference is [0.25,−0.75,0.25,0.25]; the gradient is [0.5,−1.5,0.5,0.5]. If the initial all parameters are zero, the learning rate is 0 1, the updated deviation is [−0.025,0.075,−0.025,−0.025], the tilt rate is [−0.05,0.15,−0.05,−0.05]. The new logit under the same x is [−0.125,0.375,−0.125,−0.125].

The correct category is 0. 5 higher than the rest, so the new probability is e0.5/(e0.5+3)e^{0.5}/(e^{0.5}+3) About. 354661, greater than 0. 25; new loss approximately 1. 036592 This post is part of our special coverage Global Voices Online. 386294. The independent check is that four bias gradients are summated to zero each of the four slope gradients; the independent check is that either parameter is given a central limit difference, which is recalculated twice and should be close to the decomposition number. The limited difference is only valid for the point value and is not a substitute for the preceding decomposition.

Language Model Answer Conditions and Masks

If the answer consists of multiple tokens, the reminder is x, and the answer is y1 to yT in order to include the final EOS as the end goal. Conditional probability is multiplied by step by chain method: step read only to totoken; negative log is turned into T summation. This is not a block of four, but a vocabulary for each output location is sent to the same condition sequence.

LSFT=−1N∑i=1N∑t∈responseilog⁡πθ(yi,t∣xi,yi,<t).\mathcal L_{SFT}=-\frac{1}{N}\sum_{i=1}^N\sum_{t\in\mathrm{response}_i}\log\pi_\theta(y_{i,t}\mid x_i,y_{i,<t}).
This is an average of the total NLL answer, not an average of all token; which type of statement should be used.

Response-only refers to a reference to the answer and its EOS rating, which is used for conditionality and not loss. It does not delete the hint from the input, or change the task. Nor does it rate the position of “Enter appears to belong to answer” alone, because the first answer totoken is predicted by the location at the end of the reminder. The correct mask must be determined by the segment of the target, where input target is wronged and non-response target is set as a value. Sequence filling should also be excluded from the Los denominator, but true EOS does not rule out.

Example 2: Locating the First Response Token

The whole toy string is a reminder [10,11], separator [12], and answers [20,21], EOS [2]. Enter [10,11,12,20,21] with the target [11,12,20,21,2]. Response-only should be changed to [−100,−100,20,21,2]; valid mask is [0,0,1,1,1]. The third input position is 12 separator, but it is responsible for predicting the first answer 20, so it must count. If “Input is answer.token” is used to determine mask, 20 will be missed and an additional wrong location may be counted elsewhere.

Sets three true probabilities for responding to the target at 0.5, 0.25, 0.5, with a total NLL of four times log2, three negative logs at log2, two times log2, and four times log2, with an additional three targets, and approximately 0.924196. Directly averaged a five-point series with a lower average of about 0.554518, but simply placed two unrated points in the denominator, which is not a better model. EOS is number three, removing it will change the termination of learning.

The second independent inspection is an illustrative projection of: 12 forecasts 20, 20 projections 21, 21 projections 2, which covers and closes; 10 projections 11, 11 projections 12 with no indication and no rating. The first answer totoken and the final token are in the counter account. If you fill the batch right, the target negative 100 is completely unchecked; the valid count is given by mask or length and cannot be extrapolated by input number. These principles can be directly transferred to the decoder training, but only once per demonstration of the limited four candidate projects has a complete response to decision-making and does not contain a forged token mask interface.

Empirical Distributions, Duplicates, and Weights

The project 's example of the same x is two correct short, one long. If you ignore the sharing of parameters, the free distribution of the x is q and the corresponding experience frequency is 2/3 and 1/3. The average cross-collateral is q. −(2/3)log⁡qbrief−(1/3)log⁡qverbose-(2/3)\log q_{brief}-(1/3)\log q_{verbose}. A short q plus a long q with binding q is an extremely small one: two thirds of the negative q is short, and one third is short q less q, leaving a zero q short 2/3. This is the best that can be built only at the ideal border that excludes the wrong category or allows its probability to be zero, and limited softmax training is only close.

This example explains why the demonstration does not amount to preference. To push the correct shortness to 0.99 would make the correct long demonstration a very negative log, SFT might oppose further simplification; and the preferred data would clearly require that it be shorter than longer. The course would keep the two objectives separately and not press “Post-training effects” into the same Loss. If the true demonstration contains conflicting answers, SFT would combine the experience rather than automatically know which is the truth.

The answer of Dotoken also offers a different weighting option. If the answer length is one and nine, all token total NLLs are divided by the total length, which increases the overall weight of the answer; if the answer is averaged first, then the answer is equal. You can also give a larger typical gradient measure for each reply.

What Gradient Checks Can Establish

The project test adds one hundred thousandth to each of the eight parameters, recalculating the same fixed demonstration pool of losses and verifying the resolution gradient with a central difference, with a maximum error requirement of less than one hundred thousandth. Checks that all eight parameters change from random base to SFT. The first location deduction or realization error, while the second position gradient is calculated as an issue that has not been updated. All changes in the eight parameters do not prove the best for the global picture, nor does they guarantee that any arbitrary data will allow each parameter to be zero per step; certain gradients may be accurately offset when this task is symmetrically identified, and it is necessary to distinguish between reasonable zero values and break chains.

The SFT uses full-blue gradient decline, fixed learning rates of 0 15, 200 updates, no motion and random sampling, and therefore continues to train only on current parameters and fixed data, without the need to restore Adam's recalibration or data cursor. Randomity is only initiated evenly at small scales of the initial eight parameters; saved Seed and the initial parameters can recreate the starting point. This chapter does not assign this light state to the full Checkpoint needs for real large model training, which are also optimizers, schedulers, random sources and distribution status.

Validation of SFTs should see the full probability of leaving the same context, rather than just an argmax. A model may be correct at every time, but gives a lot of probability to answer; the preferred stage is about distribution quality. Conversely, a new context close to zero may not be confident enough because of sharing linear slopes, even if the distance from zero is low.

The delivery of this chapter is the SFT goal, the parsing gradient, the real full parameter update and answer mask rules. The next chapter no longer assumes “Label gives the only demonstration”, but uses winner/loser as a new data object at the end of the same hint. Reference has to be copied and frozen deep at the end of the SFT because it will be the fixed baseline for the latter DPO ratio; if it is updated with the strategy, the copy distribution obtained in this chapter will no longer assume the original reference role.

The mean denominator also determines the gradient scale. The two answers are three and twelve, respectively. If the local gradient of each token is the same, the answer total NLL averages four times the total range; the answer averages the same for each token. If the reply averages 15 tokens, each token, etc., but the total weight is one to four. All three objectives can be clearly implemented and “To avoid the weighting of long samples” cannot be taken as an unwritten default. The series of items selects a combined and even-handed response, allowing the full answer event definition of the DPO to correspond directly to the SFT account.

The positive item of the monitoring gradient for the wrong category on a single sample does not amount to “punishment of all undemonstrated answers forever”. The next demonstration of the same reminder may include another category as the correct entry, with an average experience frequency balance. If all undemonstrated responses are marked as absolute bad, then a stronger comparative monitoring than SFT is introduced. This course solves this difference through three models, and the winner/loser relationship favours data is provided separately in the next chapter.

The sequence SFTs respond to training along the real past, so they can fail on their error prefix at the time of generation. This is not a mask failure: the correct mask is still only used in the past, but the training was used as a demonstration and produced a strategy. Free generation and cessation should be retained as independent checks, and the low level of NTL per target cannot be directly translated into the whole section.

Standalone ProgramStandard library independently validated: the difference between the centre of the eight parameters, the loss drop and the error of answer mask.
import math
x=2.; label=1; lr=.1
W=[[0.,0.] for _ in range(4)]
def loss(w):
    z=[b+s*x for b,s in w];a=max(z)
    return a+math.log(sum(math.exp(v-a) for v in z))-z[label]
grad=[[.25-float(i==label),x*(.25-float(i==label))] for i in range(4)]
errors=[]
for i in range(4):
    for j in range(2):
        a=[r.copy() for r in W];b=[r.copy() for r in W];a[i][j]+=1e-5;b[i][j]-=1e-5
        errors.append(abs((loss(a)-loss(b))/2e-5-grad[i][j]))
assert max(errors)<1e-9
new=[[v-lr*g for v,g in zip(row,gr)] for row,gr in zip(W,grad)]
assert loss(new)<loss(W)
target=[11,12,20,21,2]; response_target=[-100,-100,20,21,2]
assert [v for v in response_target if v!=-100]==[20,21,2]
print('before',loss(W),'after',loss(new),'max gradient error',max(errors),'response targets',response_target)

TRY IT YOURSELF

Practice I: Token Mask

Hint [7], separator [8], answer [9], EOS [2], write input, target and response-nly targets.

Hint 1
Write the complete sequence and then place “The input predicts the next item”.
Hint 2
The first answer is predicted by the separator position; response-only mask does not enter a word as the answer depending on whether the target belongs to / EOS.
Compare with the Solution
Enter [7,8,9], the original target [8,9,2]; response-nly [−100,9,2]. Input 8 is for the first answer to 9 so it is not possible to score only the place where the input belongs to the answer. End 2 is projected by input 9 and must be retained; there are two valid targets.

TRY IT YOURSELF

Practice II: Supervising optimal distribution

Three A and two B examples of the same reminder show what is the optimal distribution of probabilities for the two categories of freedom?

Hint 1
q and 1−q for both probabilities, and model frequency numbers for average weights.
Hint 2
A negative logarithm of weights seems to be guided by examining boundaries and second-order guides; demonstration frequencies should not be considered a preference.
Compare with the Solution
q as a probability, loss as −(3/5)log⁡q−(2/5)log⁡(1−q)-(3/5)\log q-(2/5)\log(1-q). The number of guides is −3/(5q)+2/(5(1−q))-3/(5q)+2/(5(1-q)) 2q=3(1−q), so q=3/5. The second tier is positive, the boundary loss are scattered, so it's very small inside. It's an empirical frequency drawn up, not an estimate of a preferred winning rate.

TRY IT YOURSELF

Practice III: Learn all parameters of acceptance and inspection

The parsing gradient is correct but the parameters remain unchanged, and two possible reasons are listed for checking to distinguish them.

Hint 1
Distinguish the correct gradient, execute the update and give the updated object three steps to the assessment.
Hint 2
Saves the pre- and post-update values before checking for the object identification; consistency in gradients does not preclude an updated copy or a missing call.
Compare with the Solution
Could not have implemented the update, or updated a copy to evaluate the old object. Save the parameter value and check the difference of the object after a update, and then check whether the training returned to the object is an evaluation of the incoming object. The true large model also examines the optimizer parameter group and requires_grad; this project hand-written SGD directly changes the eight parameter nesting list to avoid using the gradient check separately as a training closing certificate.

CHECK YOUR UNDERSTANDING

Check Your Understanding

Answer first, then open the explanation to check your reasoning. These questions help reveal gaps in understanding; independent implementation and transfer require separate evaluation.

1. What's the gradient for cross-entropy against logit?

Show ExplanationNormalize item to p, real logit item less one-hot.
2. Delete hint input?

Show ExplanationMask defines the loss rather than the removal condition.
3. Does the average token loss match the average total NLL?

Show ExplanationThe different objects of reclassification change the weight of training.
How do you get the post 4.

Show ExplanationSharing variable objects allows DPOs to travel with updates.

Manual completion records your progress; it does not certify your project.