What This Lesson Builds and What It Assumes
The first four chapters already give a hyphen, embedding, location, and causally multi-headed focus. This chapter links attention to truly stackable decoder blocks. At the end of the chapter, you should be able to explain the shape, specific figures for uniformization, the gradient circuits for residues, and the number of parameters of the whole set of models, item by item. If average values, squares and arrays are calculated and guided by one function; if the multi-chain approach is described in local variations when needed. This does not prove that any deep network will be trained.
The amount agreed to be , the current sequence length is , hide width as , first number , the width of each head is , front feed hides the width as . These are the positive integer numbers, and Integer block input. It's shape. real; lower case below This indicates the length of one of the locations. vectors. Statistics are calculated along the final feature axis, neither over time nor over batches. All activations are in an empty schematic number.
This item freezes to pre-LN: each sub layer is first recalculated and then returned to the original input. There are two separate LayerNom units for each block, with a non-selective and biased linear Q/K/V/O map for attention. The precise GELU is activated, and the single stabilization constant is fixed. . This chapter schilling dropout is zero, so the function is determined in a forward direction; with dropout on, constant equivalence against values should be fixed randomly or under evaluation mode.
Position-Wise LayerNorm: Mean, Population Variance, and Stability Constant
Definitions For all of that position Arithmetic average of individual characteristics; defining centralised coordinates ; defining population patterns variance To centralize squares and divide them by . This is not a statistical task to estimate an unknown overall variance from the sample, so it is not divided is modified without prejudice. , standardize the value 。
Parameters and It is the training-by-characterization scaling and deflection, with the default starting values of one and zero, respectively; the same set of parameters is shared in all batches and locations, but the statistical values are recalculated at each location. They do not press each position into the same vector, but they adjust the standardized coordinates. , the average of the results of the reclassification is zero because of the centralization coordinates and zero; its population variance is zero Not the strict one.
The final conclusion can be directly inserted into the verification: standardized squares and divides is equal to the original centralised squares and divided by The molecules are right. ♪ When... It's close to one when it's much larger than a stable constant; , which is zero. Here comes the conditional value close, which cannot be reduced to a permanent unit variance. The average value of zero is no longer necessary after the analog parameter is not the default.
The peer check is an independent algebra test: one constant for all features , add the new mean , so the centralised coordinates remain the same, the differences are the same, and the output is the same. The positive scale does not replicate this conclusion: Multiply , the denominator becomes , the fixed stabilization constant has not been scaled down accordingly, so it is usually only approximate. If the entire length is consolidated over time, the future hyphen will enter the statistics, undermining the causal contract of the project.
Building a Block from Two Residual Branches
Remember. It keeps the input for a multi-purpose map of causes and consequences, as defined in the previous chapter. shapes;keeping This is a pre-map for the next definition. The following two indicators indicate that the parameter of independent uniformization is not repeated for the same output. The first step is to generate an increment of attention by using the original input and to get an intermediate state. ; second step based on Pre-generation feed increment, again Added to get block output 。
This order must be implemented on a case-by-case basis. The second one is read in the same way as the one that has been added to the increase of attention. Not at first. ; otherwise you're making another calculation. The two ends must be the same shape and cannot rely on radio to make a mistake. Increment is copied to the full. Attention is responsible for exchanging information between legitimate past positions, while the previous feed deals with the characteristics of each location separately and the weights are shared over time.
For the definition of pre-dips, a mathematical vector agreement is used to: Shape As , Yes. , Yes. , Yes. . Count first. , non-linear by coordinates, then multiplied Back to Width . The actual PyTorch Linear storage matrix is the output width in front of the output, so the call times its conversion; the mathematical matrix cannot be confused with the stored shape.
Here import the error function as a special function provided by the standard library, defined as the equivalent So... It is the probability that the sum will be in its current position, not a random coin toss; there is no sampling of the exact GELU forward. Negative input usually gets a small negative output, rather than a direct zero change like ReLU. If there is no non-linear layer between the two layers, the matrix multiplier can be merged into a layer of analogue map, and the extension repression does not increase the non-linear expression of this function.
Example 1: A Feed-forward Residual with a Nonzero Contribution
Take a position. , Enter , both of which are the default values. In order to separate the pre-dips and project zero attention output, there is zero concentration increase. . Preset as a matrix of units, This is an example of a mechanism, not a recommendation to project zero when training is conducted.
The second set of homogenizations is calculated: the average is two, the centre is negative one and one, and the population is one. Got it. of which . Using standard normal symmetry, ;in values , . Right multiplied by half of the unit matrix, with an increase of approximately ;back input, block output approximately 。
The output does not require an overall average value of zero, because reclassification takes place only at the branch entrance and the main road is kept in its original scale. , so the second coordinates of the increment minus the first coordinates should be the right one. ; the value is about 0:49,99975. This constant equal is a different check path from the direct error function, where the positive value is still at GELU and the negative value is cut to zero; however, if the whole function is replaced by ReLU, it will still be able to do so, and a specific value must be checked.
Example 2: Failure Controls for Variance, Position, and Stability
Still one or three vectors, if you miswrite the dichotomy , with a two-point margin, standardization becomes negative at about 0:071 and positive at 0771. The shape is exactly right, but the values are about 30%; subsequent activations and residues will change. The correct difference should be one from “Square and di divided feature two”, which is not judged successful by the symmetrical appearance of the output. Symmetry can only check part of the error and cannot recognize the error in the denominator.
Retrieval Constant Vector . The average value is two, the variance is zero; both coordinates are standardized when maintaining a stabilization constant are zero and the output equals . If the stable constant is deleted, the denominator is zero, and the formula is not defined; this is not a negligible small rounding error. Any common equalisation of the constant vector should still be given the same output. The code calculates the correct realization, the error denominator and the zero variant boundary, so that one success does not obscure the other two types of malfunctions.
import math
EPS = 1e-5
def layer_norm(x, eps=EPS, sample_variance=False):
mean = sum(x) / len(x)
divisor = len(x)-1 if sample_variance else len(x)
variance = sum((v-mean)**2 for v in x) / divisor
return [(v-mean)/math.sqrt(variance+eps) for v in x]
def gelu(x):
return 0.5*x*(1+math.erf(x/math.sqrt(2)))
z = layer_norm([1.0, 3.0])
y = [x+0.5*gelu(v) for x,v in zip([1.0,3.0],z)]
assert abs((gelu(z[1])-gelu(z[0]))-z[1]) < 1e-12
assert layer_norm([2.0,2.0]) == [0.0,0.0]
shifted = layer_norm([11.0,13.0])
assert max(abs(a-b) for a,b in zip(z,shifted)) < 1e-12
expected_var = 1.0/(1.0+EPS)
assert abs(sum(v*v for v in z)/2-expected_var) < 1e-12
print('preLN residual', y)
print('wrong denominator', layer_norm([1.0,3.0],sample_variance=True))
try:
layer_norm([2.0,2.0],eps=0.0)
except ZeroDivisionError:
print('expected failure: zero variance without eps')Residual Connections Provide a Direct Gradient Path Without Guaranteeing Learning
Equating a position or entire length to a column vector, only for discussion of the guide. Write for any debris sublayer of which is a de minimis function; Number of Item is output Enter . The bias. Yeah. , the guide is the corresponding item of the unit matrix. . This constant item doesn't need to be regularized, focused and activated through the branch.
The first is the gradient that returns directly from the main road of the debris, and the second comes from the branch. They add up instead of opting for a reservation, so they can offset each other. , which is negative, with a constant zero output and a total of zero. So “There's always a way.” is the exact structure and “The gradient never disappears.” is not rolled out by it. Multiple sublayers are stacked with their respective accompaniment matrices, and single layer additions cannot be mistaken as an entire net gradient.
If Post-LN is used, the form becomes , the guide is a unicoded range. This algebraic difference suggests that the two structures are different and do not necessarily constitute a better theorizing pre-LN under an arbitrary task. When all branches of this project are set aside for zero, the pre-LN block should be kept strictly in the input; if the code still exports a standardized vector, it is likely to move the unit back to the addup. The final LayerNom at the end of the model is another layer and does not belong to the constant test.
Shapes and Parameters: Counting Every Learnable Scalar
Another shape exercise: if the batch is two, three, four, two, eight, and the front bow is two times three times four; the search key is two times two times two times three times two; the attention weight is two times two times two times three times three; and then two times three times four is returned to two times three after the project. The first feed is two times three times eight, and the second linear layer is restored two times three times four. Any step of an accidental exchange of lengths and feature axes may be exposed only when they are not equal, so the test is that three and four are deliberately used.
The parameter ledger must also be separated from the active account. When the sequence becomes longer, the current activation and attention matrix grows, but the linear layer matrix does not grow with the unicoded parameters; rather, expanding the available table of positions increases the registered parameters, even if only one word is entered this time. Depending on whether the parameter is eligible for a gradient, then whether the batch gradient is not zero, and finally whether the optimizer actually holds them, there are three different questions. The latter two chapters will be tested separately and cannot skip because the total numbers match.
The four images of attention are all. weights per unit, total ; wrap Q/K/V into one The storage matrix does not change the quantity. , the two-sided is . Two lengths for each unit , two sets of . Add each piece . The header only changes the delineation of these widths, fixed The total number of parameters will not be changed when the structure remains unchanged.
| Object | Mathematical input & output | PyTorch storage/parameters |
|---|---|---|
| Q/K/V/O Projection | [...,d]→[...,d] | [d,d] each, no bias; total 4d2 |
| FF First level | [...,d]→[...,f] | weight [f,d],bias [f] |
| GELU | [...,f]→[...,f] | Zero learning parameters |
| FF 2nd floor | [...,f]→[...,d] | weight [d,f],bias [d] |
| Two, LayerNom. | [...,d]→[...,d] | gamma [d], beta [d]; total 4d |
| Impairment addition | Two [B,T,d] [B,T,d] | Zero learning parameters |
In order to extend the account book to the entire Chapter 7 machine, another word-size is defined. Maximum number of locations Number of blocks . The word embedded and the output of unstrapped weights , learning absolute position embedded in And eventually it's down to the same thing. . The current actual input length Do not change the number of parameters in the registered list, only . The parameters are not shared among each other; if the same object is mistakenly returned, the number of parameters will be small.
Take The four pieces are projected at 16,384, with a pre-granting weight of 32,768 and a pre-release preference of 320, which is two,566 and a total of 49,728 and 56. Two,994,456; the word is embedded in the head with a head of 40,966 and the position is 4,966, the position of the final LN 128, for a total of 10,777,766. This figure is an accurate calculation of the configuration, not a visible byte, nor does it include the state of the optimifier.
def parameter_count(V,C,N,d,f):
per_block = 4*d*d + 2*d*f + (f+d) + 4*d
return 2*V*d + C*d + N*per_block + 2*d
assert parameter_count(32,64,2,64,256) == 107776
assert parameter_count(10,6,2,4,8) == 424
print(parameter_count(32,64,2,64,256))Project Contributions and Later Dependencies
The present chapter reads: hero_transformer/model.py Add two sets of homogenization, one by one. FFN with pre- LN Fragmentation blocks, using the pre-brand causality focus as a certified submodule access. The submission should show that the input output is identical, the two branches are zero-time-long, etc., that changing the future dictional characters does not affect past output, and that the parameter books are consistent with the registered parameters four results. The causality check moves from the attention layer to the block as a whole, to prevent the error of the single axis or a prior feedback action over time from being re-introverted.
from hero_transformer.model import Config, TransformerLM
cfg = Config(vocab_size=32,block_size=64,d_model=64,n_heads=4,
n_layers=2,d_ff=256,dropout=0.0)
model = TransformerLM(cfg)
actual = sum(p.numel() for p in model.parameters())
assert actual == 107776, actual
print('registered parameters', actual)Chapter VI will be pushed back along the same calculation chart in this chapter to the gradients of LayerNom, GELU, linear layers and residues, so that the axes, stabilization constants and activation agreements cannot be replaced temporarily. Chapter VII repeats calls for multiple non-shared blocks, joins finalLN and head, with full logits and losses. The forward and parameter checks do not indicate that each parameter is connected to the gradient; that is the different nature of the latter two chapters that need to continue to be validated.
TRY IT YOURSELF
Practice one basic: full account for a small configuration
Sets V=10, C=6, N=2, d=4, h=2, f=8, other structures. Calculates the number of parameters per block and whole machine; changes h to one, will the quantity change?
Hint 1
Hint 2
Compare with the Solution
TRY IT YOURSELF
Practice two-bit reasoning: A constant check equation for GELU
Without checking the value table, using standard normal density for zero symmetry, rolls g(a) - g(-a) =a. Explain why this check does not detect all GELU errors.
Hint 1
Hint 2
Compare with the Solution
TRY IT YOURSELF
Practice three-poll diagnostics: The constant is not the same as the constant model.
Forces zero output per concentration and FFN output, and the block test is passed. But the complete model output is still different from the input embedded, does there have to be bugs? List two types of malfunction that must be checked separately.
Hint 1
Hint 2
Compare with the Solution
BUILD THE MECHANISM
Connect pre-LN debris
The same Starter.py achieves TransformerBlock.forward by calling the last step of student attention. This chapter only takes normal whole-section input; cache parameters are already in the scaffolds only to retain interfaces, and chapter 10 requires interpretation and use. Downloading this full course, running at the project root directory; editing only learner/starter.py.
Input and Output This chapter past_key_value=None 、 use_cache=False; first LN has a disability, then LN has a disability FFN ♪ The wreckage. ♪
Module configurations, parameters, dropout and meta-team returns are provided; the previous student attachment is restored only without a cache forward.
# TransformerBlock.forward(替换已提供类的此方法)
def forward(self, hidden, past_key_value=None, use_cache=False):
raise NotImplementedError('block')Hint 1
The input of each feeder is named first, distinguishing between the original Hidden and the homogenized value.
Hint 2
The Hidden after the first disability is the input for the second LN.
Hint 3
Zero branch should be constant; non-zero fixed weights count to distinguish pre/post LN 。
Independent inspection .venv/bin/python -m learner.check --stage block --output runs/learner-block.json
Migration test Change the FF width from 7 to 11 and maintain the input output width 4; explain why both residues can be added together.
Reference Implementation and Explanation
# TransformerBlock.forward(替换已提供类的此方法)
def forward(self, hidden, past_key_value=None, use_cache=False):
attended, present = self.attention(
self.ln_attention(hidden), past_key_value, use_cache
)
hidden = hidden + attended
hidden = hidden + self.feedforward(self.ln_feedforward(hidden))
return hidden, present