Hong Kong Baptist University · FACULTY OF COMPUTER SCIENCE

MATH7005 Chap.10 Efficient Transformers and Distillation

- one subject, every graph, every model, every mark
6 Chapters4-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 10 of 10 · MATH7005

Efficient Transformers and Distillation

Different methods remove different costs

KV caching avoids recomputing history during autoregressive decoding, rotary position methods change query-key geometry, online attention reduces stored intermediate work and distillation transfers behaviour into a smaller model.

The chapter requires every efficiency claim to name the resource saved and the result retained.

What to verify

Benchmark methods under the same workload and hardware assumptions, record cache growth, compare exact attention outputs where appropriate and separate token-level soft targets from sentence-level teacher-generated targets.

Caching exchanges computation for memory

Past keys and values stay fixed during autoregressive generation, so storing them avoids repeated projections.

The cache grows with sequence length, layer count, head count and head width, and both tensor families must be counted. Measure latency over realistic context lengths because the first decoding step cannot reveal the later saving.

Execution changes can preserve the function

Online softmax maintains a running maximum and rescales earlier contributions whenever that maximum changes.

Online attention extends the recurrence to the value accumulator, reducing materialised intermediates while targeting the same result. Compare with a reference output under a stated tolerance before making a speed claim.

Distillation changes the supervision

Token-level training preserves the teacher distribution at each position, including plausible alternatives.

Sentence-level training uses complete teacher-generated sequences as pseudo-targets after an offline decoding pass. Compare model size, latency and task failures as well as an average score, because a smaller student may lose behaviour important to the product user.

In this chapter

What this chapter covers

  • 01

    KV caching

  • 02

    Rotary position

  • 03

    Stable online softmax

  • 04

    Online attention

  • 05

    Token distillation

  • 06

    Sentence distillation

Worked example · free

Count one layer's key-value cache

Q [6 marks]. A layer caches 50 positions, eight heads and head width 32 for both keys and values. Count the stored scalars. AskSia devised this practice case, and the weighting shown is not an official University scheme.
Choose a method only after naming the bottleneck.Choose a method only after naming the bottleneck. Caching targets repeated decoding work, online attention targets memory traffic, and distillation targets the cost of the deployed model.Different methods remove different costsWhich cost dominates?repeat K,V workKV cachematerialise scoresonline attentionmodel capacitydistillationPosition methods such as RoPE solve a representation problem;they do not reduce these costs directly.
Choose a method only after naming the bottleneck. Caching targets repeated decoding work, online attention targets memory traffic, and distillation targets the cost of the deployed model.
  • 2Count the key values.
  • 2Count the value values.
  • 2Combine the two stores.
Keys store 50 times 8 times 32, which is 12,800 scalars. Values store the same amount, so the layer cache contains 25,600 scalars.
Sia tip — Cache size grows with positions, layers, heads and head width, and it stores both K and V.
Glossary

Key terms

KV Cache
Stored past keys and values reused during autoregressive generation.
Rotary Embedding
A position-dependent rotation applied to paired query and key coordinates.
Online Softmax
A stable recurrence maintaining a running maximum and rescaled denominator.
Token Distillation
Student training against the teacher probability distribution at each token position.
Sentence Distillation
Student training on complete sequences generated by the teacher model.
FAQ

Efficient Transformers and Distillation FAQ

What work does a KV cache save?

It prevents the model from recomputing keys and values for earlier tokens at every decoding step. The new query still compares with the stored history, and the cache consumes memory that grows with sequence length.

Does online attention approximate ordinary attention?

The mathematical reorganisation can produce the same attention result while avoiding full score-matrix materialisation, subject to normal floating-point differences. It changes execution and memory traffic rather than the intended function.

How do token and sentence distillation differ?

Token-level training preserves a soft distribution of alternatives at every position. Sentence-level training uses complete teacher-generated outputs as pseudo-targets, simplifying later training but discarding much of the teacher probability distribution.

How should an efficiency benchmark be reported?

Fix model, hardware, batch and sequence settings, include warm-up and repeated measurements, and report latency with peak memory. Name the baseline time and workload so a percentage improvement can be reproduced rather than read in isolation.

What result should online attention preserve?

It reorganises stable normalisation and value accumulation to reduce intermediate storage while targeting ordinary attention output. Compare a small case with a trusted reference under a stated floating-point tolerance before measuring speed.

Why report memory beside latency?

Caching and other execution choices may accelerate a workload while consuming more memory. A deployment claim is incomplete unless the benchmark states both resources at realistic sequence lengths and identifies any training or offline cost moved elsewhere.

Which costs belong in a student-model comparison?

Include teacher generation or logits used during training, student training, deployed latency, peak memory and model size. Separate one-off preparation from repeated inference so the claimed saving can be evaluated at the intended usage scale.

Study strategy

Assessment move

Build a table with one row for caching, rotary position, online attention, token distillation and sentence distillation. Name the problem, mathematical change, saved resource, added cost and retained behaviour for each. Calculate cache scalars for a small model and extend the count across layers. Work one running-maximum softmax update, making the rescaling of earlier terms explicit.

Compare a teacher probability vector with its argmax target and identify the information discarded. For benchmarks, fix hardware, batch, sequence length and model, include warm-up and repeated measurements, and report latency with peak memory. Inspect teacher-student disagreements that affect the intended product action rather than relying only on an overall average.

Working through Efficient Transformers and Distillation in MATH7005? Sia is AskSia’s AI Computer Science tutor — ask any MATH7005 Efficient Transformers and Distillation question and get a clear, step-by-step explanation grounded in how MATH7005 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.

A+Everything unlocked
Unlocks this Bible + all 12 of your Hong Kong Baptist University subjects - and 1,000+ Bibles across every Australian university.
Sia - your MATH7005 tutor, unlimited, worked the way the exam marks it
The full 4-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works