In this article I want to look at a question I had about Inkling 2 Mini. The model runs its middle layers three times. Each time through, a layer uses the same weights, but what comes into it can change, so what comes out can change too. When we get to the loss at the end, how does backpropagation work out what each pass contributed to the gradient of a weight? And when does that weight actually get updated?
Let’s start with one weight and a loop small enough to work through on paper. We’ll write down what goes in and what comes out on each pass, then follow the loss backward to see how those three contributions add up to one update. Once we can do that with numbers, we’ll go back to Inkling and follow a message from one pass to the next.
How do you write a loop in an equation?
Start at 1 and double it three times. In code, each assignment replaces the current value. In a recurrence, a subscript keeps track of which value we mean. Read the right-hand side first. Take the value from the previous pass, multiply by the weight, and give the result the index of the current pass.
The subscript 0 marks the initial state. The subscript k − 1 means one pass earlier. The letter w has no subscript because changing it changes every multiply. Set w = 2 and follow the values from left to right:
| View | What I would write | What I can see |
|---|---|---|
| Loop in code | h = 1for k in range(3): h = w * h | One instruction runs three times. The next iteration reads the value just written. |
| Unrolled computation | h₁ = w·h₀h₂ = w·h₁h₃ = w·h₂ | Three distinct multiplies read one weight. I can point to the input of each multiply when I go backward. |
| Repeated function | f(h) = w·hh₃ = f(f(f(h₀))) | The output of the inner call becomes the input of the next. For this particular function the nested calls simplify to w³·h₀. |
When can I write a loop as a sum? Look at the operation inside it. If a pass adds a reading, hₖ = hₖ₋₁ + xₖ, the result is the starting value plus the sum of the readings. If it multiplies by a factor, the factors form a product. If it applies a neural network to the previous state, the nested functions are already a useful description. I can differentiate the individual calls even when I can’t simplify the whole expression.
Here’s a first check on weight sharing. For the multiply-only loop, h₃ = w³. At w = 2 its slope is 3w² = 12. Count it on the unrolled graph. A nudge to the first multiply acts on input 1 and passes through two more factors of 2, contributing 4. The second acts on input 2 and passes through one more factor of 2, also contributing 4. The last acts on input 4 and contributes 4 directly. One nudge to the shared weight does all three things. The output slope is 4 + 4 + 4 = 12. Next we’ll do this with a message between passes and an actual loss at the end.
What repeats in Inkling
For each token, the Prelude runs once. Core 1 and Core 2 run in sequence, and that pair runs three times. The Postlude gets the output of pass 3. Each pass starts from the same saved Prelude output. The connection between successive passes runs through a separate message path. Passes 2 and 3 also read learned messages from the preceding pass, using its L11 and L17 snapshots. The four message branches feed Q, K, V, and the FFN input. Counting layers from zero, L06 uses the same weights on each visit. L07 has its own set.
In this article I use a scalar to stand in for the core and one scaled message to stand in for the four branches. The arithmetic below is a teaching example, not the model's training code or a claim that CKE trains Inkling. The companion Inkling architecture article walks through the CKE inference circuit; the publisher's looping description explains the model design.
1. Add a message to the loop
Let the saved Prelude value be p = 1, the core weight w = 2, and the message scale a = 0.25. Each pass starts from p. From pass 2 on, it adds a quarter of the previous output before multiplying by w. Call that input to the multiply z.
Here k is the pass number, from 1 to 3. The zero in the first equation just means there is no prior message on pass 1. Notice that p is inside the recurrence: it comes back on every pass. The weight w has no pass index because all three passes read the same parameter.
All the arrows in this drawing still point forward in execution order. Unrolling the loop just gives names to the three visits. It also gives us three values to save or recompute during backward: one z for each use of the weight.
2. Start at the loss
A derivative answers a local question: if I nudge this input a little, how much does the output change? The chain rule joins those local answers. First define the gradient arriving at the output of each pass. At the last pass it comes straight from the squared error:
3. Carry that gradient back one pass
To get from the output of pass k to the previous pass, go backward through the multiply by w, then through the message multiply by a. That gives the two factors in the chain rule:
In this example each trip back multiplies by 2 × 0.25 = 0.5. So the gradient at pass 2 is 0.5 and at pass 1 it is 0.25. If a pass had an additional loss or fed another branch, we would add that incoming gradient before going farther back.
Take pass 2 on its own. A small increase of 0.01 in h₂ sends 0.0025 more into z₃. The multiply by 2 raises h₃ by 0.005. At this point the loss has slope 1 with respect to h₃, so the loss rises by about 0.005. That ratio, 0.005 / 0.01 ≈ 0.5, is the arriving gradient at pass 2. The approximation comes from the curvature of the squared loss.
4. Add the contributions to the shared weight
At one multiply, hold its saved input z fixed. The local derivative of its output with respect to the weight is z. Multiply that by the gradient arriving at that pass. Then sum over the three uses of the same weight:
| Pass | Arriving gradient | To shared w | To previous pass | Directly to p |
|---|---|---|---|---|
| 3 | 1 | 1 × 1.75 = 1.75 | 1 × 2 × 0.25 = 0.5 | 2 |
| 2 | 0.5 | 0.5 × 1.5 = 0.75 | 0.5 × 2 × 0.25 = 0.25 | 1 |
| 1 | 0.25 | 0.25 × 1 = 0.25 | No earlier pass | 0.5 |
Look at pass 3. On the way forward it read the same prelude output p as passes 1 and 2, then added its own message to get z₃ = p + a·h₂. On the way back, the gradient at that sum is δ₃w = 2. Increasing either addend by one unit increases the sum by one unit, so each addend receives the full gradient of 2. One arrow carries 2 directly to p. The other goes through a = 0.25 and carries 0.5 back to h₂.
Now follow the upper branches from passes 2 and 1. They send 1 and 0.5 back to the very same p. That gives the prelude an incoming gradient of 2 + 1 + 0.5 = 3.5. If the prelude is trainable, its backward pass takes that gradient and works out how its own parameters contributed. The lower sum, 2.75, goes to the optimizer for the shared core weight w. We can also differentiate the message scale a, which appears in two passes:
How I think about a loop when it’s time to update the weights
When I see a loop, I draw the stack once for every pass. For Inkling I draw three boxes. I can now work through each box like an ordinary layer, as long as I keep the arrows between them.
- Go forward and record what each box saw. Label the three uses of a weight
w₁,w₂, andw₃on paper. Set all three to 2. Each box gets its own row for its input and output. In our example the inputs are 1, 1.5, and 1.75. - Go backward and record what each use contributed. Start at the loss and visit boxes 3, 2, then 1. In each box, multiply the arriving gradient by the saved input and write the contribution beside that use of the weight. Also send the gradient through the message to the preceding box. In backward order the contributions are 1.75, 0.75, and 0.25.
- Bring the three labels back to one weight. In the model,
w₁ = w₂ = w₃ = w. Nudging the real weight nudges all three uses together. Add their signed contributions and hand the total, 2.75, to the optimizer. SGD now updates that one weight, and all three passes will read its new value on the next forward run.
That is one SGD step at a learning rate of 0.1. Updating after each pass would change the weight partway through a backward calculation that was defined at w = 2. Adam likewise keeps one set of optimizer state for this parameter and consumes the accumulated gradient at the step.
As a cross-check, expand the three passes into one expression. Differentiate it and multiply by the final loss gradient, which is 1 in this example:
If you want to check this on a calculator, evaluate the same loss twice, with w = 2.001 and w = 1.999. Keep a, p, and the target fixed. Divide the difference between the losses by 0.002. You should get about 2.7500. Each evaluation must rerun all three passes, because they all read the weight you changed.
Follow one real message branch
Now replace the simple scaled previous output with a learned message. In an Inkling branch, the current input x and the previous-pass message m also feed a gate network. Call its outputs f and g. The products in the mixer are elementwise:
Below, a bar over a variable means the gradient of the loss with respect to that variable. For instance, u is the gradient at the mixer's output.
Those first two lines hold the gates fixed. To get the full gradients to x and m, also backpropagate the last two lines through tanh, the learned projections, and the gate's normalized inputs. Add the results to the direct contributions. The tanh step multiplies by 1 - tanh(s)^2 at its saved preactivation s.
How does the gradient get from the message back to the two snapshots? The message is a weighted sum of two values. The weights come from a softmax over the two scores:
Hold the scores fixed and the value path just scales the incoming gradient by its weight. For the score path, increasing one score shifts the weighted sum toward that value and away from the current mixture. That's where the difference between the value and the mixture comes from:
A snapshot that contributes to a value and to a score receives both gradients. Repeat that accounting for every branch that reads it. The loss reaches earlier passes through those snapshot paths, and the shared Prelude gets its own sum from the places where the passes read it.
Same accounting for a matrix or a residual
For a column vector x and a shared matrix W, let uₖ be the column vector of loss gradients arriving at the output of pass k. A single pass sends a gradient to its input and contributes an outer product to the shared weight gradient:
For a layer with 3 inputs and 2 outputs, W is 2 × 3. Multiplying its 3 × 2 transpose by the two output gradients sends three numbers back to the input. Multiplying the 2 × 1 output gradient by the 1 × 3 input row gives a 2 × 3 contribution, one number for every weight. In each pass x and u can differ, so the outer products can point in different directions. They still add element by element into the same buffer. At a residual addition the same addition rule shows up again:
The first term takes the skip path; the second goes through the branch. The same multiply-then-add rule accounts for feed-forward, attention, and shared weights. A long sequence also creates dependencies between token positions. That is a different axis from the three visits through the same middle layers.
Sharing reduces the number of stored weights and optimizer slots relative to three separate cores. It does not remove the work of running the three passes forward and backward. Their intermediate values can be kept for backward or recomputed with activation checkpointing. Either way, the optimizer sees one gradient for each shared weight.
Try four loops on paper
For each one, write the forward values first. Circle the quantity the question asks about, then trace every path from it to the output or loss. Open a question to compare your working.
Addition. Start with h₀ = 2. Add 3, then −1, then 4. What is h₃, and how much does a small change in the first reading change h₃?
The states are 2 → 5 → 4 → 8. Unroll the additions and you get h₃ = 2 + x₁ + x₂ + x₃. A change of 0.1 in the first reading survives both later additions and raises the output by 0.1. The derivative with respect to that reading is 1.
Shared multiplication. Start with h₀ = 1 and use hₖ = w·hₖ₋₁ twice. Set w = 3 and L = ½(h₂ − 7)². What gradient reaches the shared weight?
The two outputs are 3 and 9. The arriving loss gradient at pass 2 is 9 − 7 = 2. Pass 2 contributes 2 × 3 = 6 to the weight and sends 2 × 3 = 6 back to the first output. Pass 1 then contributes 6 × 1 = 6 to the weight. Add the two contributions to get 12. Check the closed form below.
A closed message gate. Return to the article’s three-pass example. Set a = 0, keep w = 2, p = 1 and y = 2.5. Which pass contributes to the weight gradient? Can the message scale still learn?
All three outputs are 2. At the last pass the loss gradient is 2 − 2.5 = −0.5. A trip back through the message multiplies by w·a = 0, so the earlier passes contribute zero to the weight gradient. The last pass contributes −0.5 × 1 = −0.5.
Now nudge the scale itself. The final message has a source value of 2 waiting behind the gate. At the last pass, ∂h₃/∂a with that input held fixed is w·h₂ = 4. So dL/da = −0.5 × 4 = −2. A zero gate can cut the gradient to its input while still receiving a gradient of its own.
Two losses. Use h₀ = 1, hₖ = w·hₖ₋₁ twice, and w = 2. This time score both passes against target 1. With L = ½(h₁ − 1)² + ½(h₂ − 1)², what arrives at h₁?
The outputs are 2 and 4. The second loss starts with a gradient of 3 and sends 3 × 2 = 6 back to h₁. The first loss contributes 2 − 1 = 1 directly there. Both depend on h₁, so its arriving gradient is 7.
For the shared weight, pass 1 contributes 7 × 1 = 7 and pass 2 contributes 3 × 2 = 6. That makes dL/dw = 13. If you got 12, trace the arrow from the first loss back to the first output.
Comments
Join the conversation. Replies can go three levels deep.