Please enable JavaScript to use CodeHS

AI Explained

Building efficient models

Bigger models are smarter, but they are also slower and more expensive. Efficient architectures use sparse experts, shorter numbers, and clever attention tricks to do more with less.

AI ~12 min High school
The tradeoff

Scale and cost

Model performance generally improves with size, data, and compute. This pattern is called a scaling law: if you make the model bigger and train it on more data, it usually gets better at predicting the next word.

But bigger models are expensive. A trillion-parameter model might need thousands of specialized chips and a small power plant. That is why researchers build efficient models: architectures that keep performance high while using fewer active parameters, less memory, or shorter inference time.

Core idea: efficiency is about doing the right work with the right parameters, not just making everything smaller.


Sparse experts

Mixture of experts

A Mixture of Experts (MoE) model has many small “expert” networks inside it, but each token only activates a few. A router looks at the token and decides which experts should handle it.

The model can have many total parameters (for example, eight experts of the same size), but only a fraction run for any single token. This keeps inference fast while increasing total model capacity.

moe.py
scores = router(token)            # one score per expert
weights = softmax(scores)
top_k = pick_top(weights, k=2)   # only 2 experts run

output = sum(weight * expert(token) for weight, expert in top_k)

Interactive

Expert router

Below is a tiny router with four experts: Math, Language, Code, and Emotion. Click a token to see which experts the router activates and how the final output is a weighted mix of their answers.

Mathnumbers
Languagewords
Codesyntax
Emotionfeelings
2
Select a token to see routing.

More tricks

Other efficiency tricks

  • Long-context attention: instead of comparing every token to every other token, methods like sliding windows and sparse attention limit how far each token can look.
  • KV cache: during generation, the model reuses key and value vectors from previous tokens instead of recomputing them. This makes long conversations much faster.
  • Quantization: store weights in smaller number types, like 8-bit or 4-bit integers, instead of 32-bit floats. The model loses a tiny bit of accuracy but uses far less memory.
  • Distillation: train a small “student” model to copy the outputs of a large “teacher” model. The student is much faster but keeps much of the teacher’s ability.
  • Interpretability: understanding which neurons fire for which concepts helps engineers remove unnecessary parts and make models safer.

Why it matters: efficient models let AI run on phones, browsers, and school laptops, not just giant data centers.


Check yourself
?

Why does MoE not slow down by the number of total experts?

If an MoE model has 64 experts but only uses 2 for each token, how many expert computations are needed per token?

Only 2. The router makes the model sparse: most experts are idle for any given token. So the active compute is small even though the total parameter count is large.


Quick quiz

Three questions

1. What is a Mixture of Experts model?

2. What does quantization do?

3. What is distillation?

Keep learning

Efficient models make AI practical, but they still need to be taught what to do. The next tutorial covers reinforcement learning, where an AI learns by trial and reward.

Part of the AI Explained tutorial series.