AI Hero Club

Efficient LLM Inference · 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.
Efficient LLM Inference / Learning Plan ↗

LESSON 02 / 8Suggested study 120 minutes

KV Caching: Verify Reuse, Positions and Windows

After retaining previous keys and values, how do we prove that chunked computation equals a full-prefix forward pass?

3 Questions for This Lesson
  • Use causal dependence to prove the legitimacy of multilayer caches.
  • Enable and verify global causal position of multi-word widgets
  • Use the error prefix to overflow the window to determine the cache boundary
Course Project ZIP ↓

Prerequisites and the Question This Lesson Resolves

Chapter I merely states that the cache reduces duplication of effort and does not prove the same function after reuse. The key and value for each layer are recorded separately, provided that this chapter prefixes fixed, weight fixed, and the reasoning mode shuts off dropout. K(l)K^{(l)} 、 V(l)V^{(l)} Tier number l=1,…,Nl=1,\ldots,N. Keys and values are not original characters, nor are they ultimately hidden, but rather the level of the layer that is entered by the normalized and linear projection. Each layer cannot share a cache, and the pre-modified cache cannot continue to be used after changing weights.

The project replicates the local Transformer field model to the Vendor, and downloads directories can run independently. The model interface is forward(tokens, targets=None, past_key_values=None, use_cache=False), with output dictionaries containing logits, loss, past_key_values. Tokens are shapes. [B,T][B,T] , returns logits as [B,T,V][B,T,V]. Cache is a group of K, Vs per layer with each volume shape [B,h,L,dh][B,h,L,d_h] of which LL for old prefix length, hh For the head count, dh=d/hd_h=d/h. The external caller must maintain the cache prefix identity.

A Layer-by-Layer Proof That Previous Positions Stay Unchanged

Starting with the input layer, the characters of the old position are identical to the absolute location number, so they are embedded. Assuming that the input of a layer is the same as the original prefix calculation for all the old positions, the re-allocation of the position and the project of Q, K, V. ii, cause and effect mask allows only key position j≤ij\leq i, the new additional characters are not in its visible collection. The scores, softmax and weighting results are the same as originally calculated; the feedback and residues by location do not change this conclusion. By layer, all old positions can be reused at K and V on all layers. Proof of dependence on causal visible aggregations does not apply to two-way attention that can see the future.

New queries must still be counted. LL New block long SS, new block local query number is r=0,…,S−1r=0,\ldots,S-1, and its global position is L+rL+r. The key position number after the spell is j=0,…,L+S−1j=0,\ldots,L+S-1, the legal condition is j≤L+rj\leq L+r. So the first line of the new block can see all the old and its own keys, not the key behind the new block. If you create a zero-starter, SS Cuts down the triangle matrix, which is wrongly blocking most of the positions in the old prefix. A word character test often hides such multiple index errors, so the test must contain a new block larger than one.

Arj=exp⁡(qr⊤kj/dh) 1[j≤L+r]∑u=0L+rexp⁡(qr⊤ku/dh)A_{rj}=\frac{\exp(q_r^\top k_j/\sqrt{d_h})\,\mathbf1[j\leq L+r]}{\sum_{u=0}^{L+r}\exp(q_r^\top k_u/\sqrt{d_h})}
Conceptual formula: achievement should first block illegal points and then stabilize softmax; not actually calculate the illegal index.

Worked Example 1: Appending Two Tokens to Two Cached Tokens

Sets an old prefix two, a new block two, and a four-string key. The first row of the new block is global position two, with visible keys zero, one and two; the second is global position three, with four keys available. If all legal scores are zero, the probability for the first line is three-thirds and four-quarters each. The difference between one, two, three, and four is a fraction of the value vector, with two outputs two and two-point five, respectively. The wrong sub-trigger only gives one and one-fifth. This calculation does not rely on random weights, it can position mask directly, and it does not merely observe a small final error.

Standalone ProgramStandard library regular versus malfunction: global mask offset.
import math
old, new = 2, 2
values = [1.,2.,3.,4.]
outputs=[]
for r in range(new):
    legal=[j for j in range(old+new) if j<=old+r]
    outputs.append(sum(values[j] for j in legal)/len(legal))
assert outputs==[2.,2.5]
wrong=[sum(values[:r+1])/(r+1) for r in range(new)]
assert wrong==[1.,1.5] and wrong!=outputs
print(outputs, wrong)

Value equals the value check to produce the slices. When you enter the complete sequence into four, one, seven, and twelve characters per block, the second call returns only the logits of the current word parts; the reference is used to calculate the first five characters. The fourth to fourth location of the reference should be compared instead of the end of the two pieces. The maximum error in the measurements of the fixed double accuracy, batch 2, total 24 is the maximum error. The item chunks uses the cumulative cursor and stop to take the reference slices, to compare all new locations by block and to return the maximum absolute error. 2.22×10−162.22\times10^{-16}, which is a reordered order of the floating point and does not require bit-by-bit equivalence.

Run in the Project DirectoryProject directory runs: Multiple blocks, lots and all new locations are checked simultaneously.
from lab import make_model,tokens,chunks
model=make_model()
error,past=chunks(model,tokens(24,2),[4,1,7,12])
assert error<1e-10
assert len(past)==2 and past[0][0].shape==(2,4,24,8)
print(error)

Cache Identity Goes Beyond Tensor Shape

Structural check can detect discrepancies in head, layer, device or length, but it is not possible to identify errors of the same length. The cache created by the prefixing of another word is received by the current third word, the shape is perfectly legal, but the probability of conditions has changed into another history. Project test_wrong_prefix_negative_control explicitly requires that such substitution result in detectable differences and prevent a “There's no cache.” pseudo-realization of a ZXAXZ is also justified. The actual service should preserve the matching of the requested identity, model version, location policy and prefix content in the schedule status; the cache load does not contain itself a verifiable original text identity.

Cache batches also have a synonym for identity. If the first line of the batch is completed, the second line of the cache moves to the first row, and the new request for entry to the second row requires simultaneous movement of each layer of K, V, processing length and request numbering. Recalculating only tokens will bring the history of others to the current request. The project does not achieve a real multi-request schedule, while chapter IV is used as a discrete simulation, so the emulator is not called a service provider that supports a variable length cache. Clearing the boundaries of capacity is also protection against the validity of the cache: proof covers only prefixing relationships actually maintained by callers.

Worked Example 2: Why a Full Window Cannot Simply Drop Its Leftmost Token

Length of use of this model CC , allow zero position to C−1C-1 Take it. C=4C=4, process a, b, c, d after the cache of four words. The next reference protocol changes the window to b, c, d, e, four to zero, one, two, and three. The old cache b was located in one and its input vector was zero from the position in the new agreement; deeper b, c,d was mixed with a left window. Deleting a K, V has not been re-positioned or eliminated from the historical mix, so usually does not amount to rebuilding the window.

Projects are treated conservatively and clearly: normal increment when windows are not available; each step is taken more recently after they have been completed CC , re-route from zero and create a new cache. At 13 and 8 hours in length, the cache path is reconstructed five times at 9 to 13 steps, with the maximum error per step measured as 1.67×10−161.67\times10^{-16}. This is a well-defined context cut-off function that does not amount to an unlimited context model, nor does it have the cost advantage of a constant order query after window fills. A direct entry model beyond the window will throw out an anomaly and the test stand will not quietly bypass the top of the position.

The second type of independent inspection is a protocol check: apart from positive values, the spill is confirmed to be a true rejection, an error prefix is a failure, and the number of reconstructions is equal to the length. A change in a future word should not affect the previous location output, which is another test of causality; it is more independent than a comparison of only two sets of possible sharing of the same mask errors. Ten tests for this course cover the main cache boundary, but not all batch reloading and equipment. The GPU, variable length real batch processing and RoPE window strategy should be additionally referenced and tested and cannot follow an absolute location attestation conclusion without checking the premise.

Error Thresholds and Storage Implementations Are Part of the Contract

This experiment uses double accuracy and absolute relative tolerance. 10−1010^{-10}, because small models are mainly used to examine structures rather than simulate common semi-precision. Absolute error limits the fractions close to zero, relative error limits at larger scales; relative error limits only when reference closes to zero; and instability when reference is made. If moved to semi-precision, the order of engagement, rounding and kernels may change errors, the reasonable range should be estimated first and the separation of failure-compromise contrasts should be maintained, and the threshold cannot be arbitrarily scaled up to pass after a failure. Cache math equivalence is derived from the real-digit level, and limited accuracy is an additional experimental conclusion.

Another independent boundary is how to increase. The current teaching process is to achieve a longer load by combining the old K, V and the new K, V, which logically avoids the old projection, but may replicate the old cache at every step. One-step replication increases with length, and cumulative reproduction. Pre-allocation of successive buffer zones or page breakouts can change these storage costs, while increasing the certification obligations for capacity, indexing, recovery and batch identity management. This item therefore does not write “Cache exists” as “Optimal cache distributer achieved”, nor does it directly treat the number of logical elements as a process peak memory. Chapter III only aligns the actual holdings with the formula by item.

The condition of the final check output: A fixed follow-up input trajectory allows the cache to always see the same string of characters as the reference, which is suitable for segregating errors. If you sample each, a very small distribution difference may be drawn to different characters, and then the end of the prefix loses a common input on the equal value. The generation experience test can be retained, but the seeds, sampling rules, parallel processing and stopping must be recorded separately; it cannot replace the mandatory prefix test here.

Exercises and Project Contributions

TRY IT YOURSELF

Base: Draw a valid set of keys

Old prefix 3 and new block 2. Lists the visible key numbers in two rows; points to errors in the lower local triangle.

Hint 1
What is the global location for the new block I query when cache prefix P?
Hint 2
Use the j-jing. P+i The grouping is legal, one by one, and then compares it to a triangle under a new block that only numbers zero, checking whether the old history has been leaked.
Compare with the Solution
The two lines are in three, three, three. 4, so the legal collection is { 0, 1, 2, 3 }、{ 0, 1, 2, 3, 4 } The lower triangles from zero will be released only { 0 ♪ And ♪ 0, 1, error blocking history with current location.

TRY IT YOURSELF

Step: Cache can be used across models

The same phrases, width and prefix, model A trained one step to become model B. Why can't old caches be handed over directly to B?

Hint 1
Which weights do the cache values rely on in the old model and which of the earlier hidden state?
Hint 2
Tracks the amount of possible change in an update from the embedded to each layer of K/V; the shape remains unchanged and does not prove that the value is still a new model.
Compare with the Solution
Keys and values are determined by the input and projection parameters of each layer. A parameter update may alter embedded, unicoded, projected and earlier output; it is shaped in the same non-reform format as a new model. The cache should be discarded and recalculated with a B prefix, or it should otherwise be proved that the update does not affect all buffer dependency.

TRY IT YOURSELF

Challenge: window cost and failure report

window 6, fixed input length 10, how many times do you need to be re-established under this chapter agreement? How should options with only a cache left be validated?

Hint 1
How do the references to each new location be renumbered and calculated after the length exceeds the window?
Hint 2
Columns spill up for the first time to each step of a total length of 10, and do not delete only one cache entry compared with the last six words from zero recalculation logits.
Compare with the Solution
Paragraphs 7, 8 、 9 The failure program must be fully matched to logits with a reference of the most recent six-word count from zero, with clear weights and input; if there are differences, no equivalent can be claimed. Even if a random sample happens to vary very little, it does not repair the structural aberrations that mix the location with multiple layers.

This chapter item adds two reference paths and negative contrast tests for chunks and rolling. The subsequent space book uses each layer of cache that is actually returned, without extrapolation of capacity from the model name; the subsequent count is only compared within the same input and window, and first adopts the page error threshold. “Faster.” is then set up on the same function, while the quantitative section will deliberately change the function and use another set of approximate quality standards.

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. The key premise for the reuse of old positions when adding characters?

Show ExplanationReliance on causality, identical location and fixed parameters is summarized on a layer-by-layer basis.
2. Old L, where do you see the new sectionr?

Show ExplanationThe global location of the query is L+r.
3. Same shape caches guarantee prefix identity?

Show ExplanationShape validation does not check which portion of the cache comes from or which version of the model.
4. The strategy after this project window is full?

Show ExplanationThe combination of absolute and historical places for learning requires that the curriculum be re-established in accordance with the reference agreement.

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