AI Hero Club

Train a Language Model from Scratch · Lesson 6

Marked Complete by You0 / 8

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

Lesson Outline 5 subsections
Course Contents 8 lessons
Full course plan.
Train a Language Model from Scratch / Learning Plan ↗

LESSON 06 / 8Suggested study 120 minutes

Training as a Recoverable State Transition

Why can the next update differ even after loading the same weights and seed?

3 Questions for This Lesson
  • Implements a clear gradient, forwards, reverses, cropping and AdamW updates on a line-by-line basis, distinguishing between loss and budget indicators
  • The first step of Adam W is to explain why the restoration of weight alone cannot restore the optimal trajectory.
  • Save and verify models, optimizers, encoding, data, sampling and random state, and recreate the environment next
Course Project ZIP ↓

Task and Prerequisites: Tracking All Eighty Updates

The conduit can now produce the right goal, and the complete decoder can reverse it. This chapter defines training as a state process, and allows interruptions to be resumed as an acceptable act. Readers need to turn to volume parameters, chain rules and the Python dictionaries; the specific agreement by Adam is given in this chapter. Project fixed CPU single-line, PyTorch 2.6. 0, random seed VII, top four windows for batt, learning rate 0 003, weight decay 0 01, global gradient parsing threshold 1 and running 80 updates. No learning rate schedule, mixed accuracy or distributional communications can be added to the receipt.

The order of one update has the mathematical meaning: first model.train; extracting the next batch and spelling it into x, y; optimizer.zero_grad to remove the last-step residue; forward receiving the average value of the active target for the current batch loss;backward searching for gradients along the full calculation chart; recording the pre-cution parameters and weights of the parameters; global tailoring; optimizer.step updating. Leaking gradients will accumulate unsynchronized guides, updating them before using the wrong state in reverse; Sets a pool by verifying the loss in reverse. Logs are stored in float(loss.detach(), do not keep the calculated stubble and do not inadvertently hold the charts.

The Exact Targets of Clipping and AdamW

Connect the gradients of all parameters to vector g, paradigm ∥g∥2\|g\|_2 When the threshold is exceededc times the zoom factor c/s, otherwise it will not change. Cropping the direction but changing the range does not allow the layers to make a separate determination c or claiming that the global norm does not exceed c. To achieve the use of frame functions, a small number is added to avoid dividing them by zero, so the lesson handles the protection by ignoring the value comparison only for non-zero values and far from the threshold. The gradient should not be considered a failure; all fractional parameters are recorded to detect numerical malfunctions, rather than always interpreting the high gradient as better learning.

g~t=gtmin⁡(1,c/∥gt∥2),mt=β1mt−1+(1−β1)g~t,vt=β2vt−1+(1−β2)g~t2.\tilde g_t=g_t\min(1,c/\|g_t\|_2),\quad m_t=\beta_1m_{t-1}+(1-\beta_1)\tilde g_t,\quad v_t=\beta_2v_{t-1}+(1-\beta_2)\tilde g_t^2.
The gradient is trimmed into the AdamRect estimation; the square, divide and the opening are performed by parameter elements.

The project uses the default beta1=0.9, beta2=0.999, epsilon=1e−8 for AdamW. The rule estimates zero opening value and t steps from the beginning. The deviation is modified to m^t=mt/(1−β1t)\hat m_t=m_t/(1-\beta_1^t) and v^t=vt/(1−β2t)\hat v_t=v_t/(1-\beta_2^t). The weight decay is separated from the gradient rule: multiply parameters first 1−ηλ1-\eta\lambda Less ηm^t/(v^t+ϵ)\eta\hat m_t/(\sqrt{\hat v_t}+\epsilon). The project centrally applied the weight loss to all parameters, including normalization and bias; this was a simple experimental agreement that did not claim to be the best grouping of parameters for all large model training.

θt=(1−ηλ)θt−1−ηm^tv^t+ϵ.\theta_t=(1-\eta\lambda)\theta_{t-1}-\eta\frac{\hat m_t}{\sqrt{\hat v_t}+\epsilon}.
AdamW update of the fixed learning rate; this project does not add L2 to the gradients rule, and therefore does not optimize L directly by Adam with a secondary penalty.

Example 1: The First One-Dimensional Step and Moment State

Make the parameter start two, the original gradient. 5, crop threshold one, so the gradient remains unchanged. Use the learning rate. 1 - It's a decomposition. 01 First step. m=0.05 、 v=0.00025 0.5 and 0.5 respectively after the deviation has been corrected 25. When you ignore the tiny psilon, the self-adaptation step is 0. 1; decay first becomes 1. 998, the final parameter is about 1. 898. The first step may also be reduced by about 0.02 if the gradient is estimated by adding the decay item 0.02. 1, but the trajectory is different since the rectangular state is different. A single drop cannot distinguish between the two algorithms.

The second step, even if the gradient is replaced by — 0.25, m contains the last gradient memory and v contains its square memory. Thus, only parameters 1. 898, reset m and v to zero, will continue in different directions or steps from the original status. Independent inspection is a first step with the standard library as a whole; independent inspection two is a step in comparison between three consecutive steps and all-state sequenced after two steps. Only weight files are equal to the same value as status, because the optimist next depends on history.

Example 2: The Same Weights with a Different Next Batch

The sampler has six windows, size four; one wheel is arranged as [3,0,5,1,4,2]. The first one is taken and the next one should be [4,2]. If you restore it with only the same seed, the next one will be reset to zero, and the next one [3,0,5,1] will repeat the data. Even if weights, optimizations and global random seeds are the same, the trajectory will change because the sampler has its own separate generator. It will need to save the order, the cursor, the wheel, the generator internal state and the number of windows, the size of the batats.

Another fault is that the model consumes a global random number: RNG is restored and the random initialization model is exemplified. The construction process has advanced RNG, and then the dropout or sampling will not be consistent. The correct order is to read the state, construct the model and the optimizer, load weight and state, construct and restore the sampler, and finally restore the global torch RNG. This project dropout is zero, generate greeedy, and the current update does not itself consume the full range random number; The test still reads extra random vector comparisons and retains a true state check for the subsequent addition of random mechanisms, without claiming to have tested dropout recovery.

Standalone ProgramIndependent validation of the Standard Bank: The first step, the recovery of integrity and the reversal of only weights.
import json, math

def step(state,g):
    theta,m,v,t=state
    g=g*min(1.,1./abs(g)) if g else g
    t+=1; m=.9*m+.1*g; v=.999*v+.001*g*g
    mh=m/(1-.9**t); vh=v/(1-.999**t)
    theta=theta*(1-.1*.01)-.1*mh/(math.sqrt(vh)+1e-8)
    return [theta,m,v,t]
state=[2.,0.,0.,0]
first=step(state,.5)
assert abs(first[0]-1.898)<1e-7
continuous=state
for g in [.5,-.25,.1]: continuous=step(continuous,g)
prefix=state
for g in [.5,-.25]: prefix=step(prefix,g)
resumed=step(json.loads(json.dumps(prefix)),.1)
assert continuous==resumed
wrong=step([prefix[0],0.,0.,0],.1)
assert abs(wrong[0]-continuous[0])>1e-3
print('first',first,'continuous',continuous[0],'resume',resumed[0],'weights-only',wrong[0])

The Checkpoint Contract and a Real Recovery Test

Project checkpoint save configurations, models_dict, optimizer state_dic, tokenizer rules and summaries, language summaries, sampler status, global RNG, completed steps and towers_seen. Recalculations of the summary in current language when loaded are rejected; code rules are also recalculated. The configuration in the checkpoint is used to reconstruct the same shape model and cannot continue after the word or window has been modified. The agreement addresses the known state status of this experiment, does not guarantee the safety of any document from unknown sources, nor does it refer to the counter-sequential results of different frame versions as reproducing in a location by location.

Real project tests are updated three times to save the state; for the fourth consecutive time, each parameter, Adam status, loss, sample location and subsequent random vectors are recorded; and for the third step, the fourth step is restored and re-executed. The current CPU environment is equal in value by value. The wrong text summary must be tested for abnormal failure. This is better than “no error on the save” because the latter may only prove that the document is readable and no subsequent transfer is tested; and it is better than just Loss, since different weights may give the same amount of bid to a given batch.

Losses and budget for this frozen training

The training receipts distinguish between initial and final training package losses, verifying the loss by inspection point, and the last loss. The current fixed experiment is 80 updates, with a cumulative total of 4585 valid targets; the training pool covers only 690 targets on a single basis, so that 4585 is not unique in terms of size. The initial training of NL is 5.682412, which ends with 1.653204; the validation was reduced from 5.681924 to 3.190052. The last batch of Loss is about 1.509825, which cannot be used as an alternative to the average training package. The curve describes the work done to optimize the statistical preparation of links and synthetic distribution in the environment, and does not demonstrate a generalization of natural languages.

Scope of the current timescale

The wall clock training session is about 0.416 seconds and contains periodic validation but not interpreter start-up, import frame and construction models; its name and time boundary are recorded in the document, and the machine load changes may vary. Short run times are dominated by fixed costs and system vibrations, and cannot be inferred from this number. The test continues to increase the training budget purposelessly, saves only the true results under the given agreement. The next chapter uses the freeze checkpoint for validation and a prefixing test evaluation and records the readable generation separately from the quantitative.

Training status transfer can be written into a defined function: given model parameters, optimist torque and number of steps, data windows, samplers sorting cursors and random to produce a new state and a receipt. The certainty dependent environment and algorithms are not automatically created by setting an integer Seed. This item sets a single thread, manual seeds, and determinative algorithms, and writes the actual torch version into trading.json; if Python random or other library generator is added in the future, it must also be kept separately. The current source does not use those additional random sources and cannot claim to have overlayed any expansion.

Saves after the complete optimizer.step, before the next sample. This boundary corresponds to the number of steps, weights and cursors that have been completed: if written after Backward, there must be an additional step to save the unserviced gradient, otherwise half-steps will be lost. The project does not provide half-step restoration or design services for multiple parallel authors. The document writing breaks may cause damage to files; the current single process small experiment uses the successful checkpoint as a prerequisite for recovery and does not claim to have production-level atomic rotation and tolerance error logs.

Verify with source records when running

Restoring the language summary does not guarantee that all hyperparameters are appropriate. The model configuration is from a file, with actual learning rates and decays in an optimizer state, and the sampler ' s status has windows and battling size; the window is reconstructed by the same tokenizer and Block_size. If only the text is changed but the total window numbers coincide, the language digest will still be rejected; if only the tokenizer number is changed, the rule summary will be rejected. Changes in the source code are made by looking at source_manifest and test receipts, and currently restore does not automatically prove that two different programs have the same semantics. It is important to separate responsibility for hard checking from by-source records when running.

The effective budget can be reconciled by two separate paths: updating the number of returns to this batch, adding each of them to the targets_seen; the sampler's indexing the windows will also map the length of the windows. If Resume restores Sen, the first budget number may still appear to be continuous, but the target has been repeated; therefore, the test is resumed while comparing the next batch of hidden losss, full parameters, optimists and cursors. A single count does not prove that the trajectory is not broken. The final fixed experiment of 4585 can also check the modifierity with each checkpoint 1170, 2328, 3450 and 4585.

• Outreach: move to change learning rates

The learning rate is fixed not without design. It reduces the state of missing Scheduler at recovery, but also limits the efficiency of training. If a warmup cosine decay is introduced in the future, the timeline must first be defined as being driven by an update or a valid target number and save the scheduler ' s current state or full rules that can be recalculated; two different lengths of battling will make the two tracks different. We do not change the learning rate temporarily to make the curve more beautiful, nor refer to this small budget training as the best way to capture the global picture. The reduced loss was a local empirical observation and the non-column network did not receive the best evidence of merit from the experiment.

The number of steps for an optimist is the actual number of updates performed, not the number of forwards. A validation does many forwards but does not advance Adam ' s state; if the test battling is counted as a training step, the deviation fixes or schedulers are wrong. The project only executes Step inside the update and does not modify parameters or optimizers after the evaluate returns. The recovery test also compares the step count in Adam to prevent weights from being equal to weights. The difference between the eightie updates and the number of times a cycle is recorded in order to allow others to recreate the same budget from their orders, rather than simply copy the last line of the log.

Terminal CommandRuns in the downloading of the root directory of the project, installed in a stationary Python environment; this is the project command and not the Python source code.
python run.py train
python -m unittest -v tests.test_pipeline.PipelineTests.test_exact_same_environment_resume

TRY IT YOURSELF

Practice I: global cropping

The gradient of the two parameters is 3 and 4, with a threshold of 1.

Hint 1
The two-coordinate composition is first sought, and then the scale is uniform.
Hint 2
Recalculates the overall parameters separately to check whether global thresholds are actually met, rather than just each coordinate threshold.
Compare with the Solution
The global scale factor is 5, 1/5, with 0.6 and 0. 8 obtained, just as in the case of 1 scenario. If each mark is reduced to an absolute value of 1, 1 and 1, the global scale is root number 2, which is greater than the threshold. The rectangular estimate is that the previous global crop should be accepted. This item records a total parameter before the cutting, and the log value cannot be misjudged as the backcut.

TRY IT YOURSELF

Practice II: Restore missing items

Checkpoint has models, optimizers, global RNG, but there is no sampler cursor. Why can't you claim to be accurate? Write the minimum recoverable failure.

Hint 1
Models are in the same state and do not guarantee that the next batch will be the same.
Hint 2
Write a fixed line and save cursor, listing the next batch and the batch that is recreated from the beginning, and updating it more closely.
Compare with the Solution
Sets [3,0,5,1,4,2] with six windows, but save4 after the first. The next batch is [4,2]. Re-establishing the sampler will repeat the first four windows from scratch, with different input gradients even if all model states are identical. Saves and restores the number of windows, the size of the batch, the sorting, the cursor, the epoch and the independent generator state, and compares the next and the next parameters to overwrite this failure.

TRY IT YOURSELF

Practice III: Define the boundaries of receipts

Can you claim that the GPU and the new version of the framework are in place?

Hint 1
It is first possible to limit which devices, versions, threads and randomly obtained.
Hint 2
Splits “Equivalent to environment by value” and “Cross-equipment tolerance comparison” into two propositions, and the unrun latter cannot be extrapolated from the former.
Compare with the Solution
No. No. The floating-point protocol sequence, kernel and random realization may change. A sufficient conclusion is to record Python/torch versions, CPU linear numbers, certainty settings, source code and data identity, and to state that the next parameters, status, loss and RNG in the environment are equal by value. Trans-equipment should be re-accepted; the current receipt does not cover the range or write down the result of the failure.

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. Why do you have to take each step before you go?

Show ExplanationThe gradient can be accumulated by default, and the project does not use the gradient cumulative agreement.
2. AdamW can't leave out which state?

Show ExplanationThe next steps in the correction of deviations and self-adaptation are dependent on these historical conditions.
3. When should the global RNG be restored?

Show ExplanationConstructes to push RNG; the saved random state should be finally loaded.
What does 4. 4585 towers_seen suggest?

Show ExplanationIt is an updated budget, not a unique amount of data or model capacity.

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