UUtah CS 6340 NLP | Fall 2026 | LLM efficiency: Mixture-of-Experts & Attention variants
Overview
The lecture explains how Mixture-of-Experts (MoE) feed-forward layers and newer attention variants reduce transformer inference compute or memory, especially for long reasoning traces and agent contexts. MoE activates a learned top-k subset of experts per token, while Multi-Query Attention, Grouped-Query Attention, Multi-Head Latent Attention, sliding-window and sparse attention, and linear-attention hybrids target KV-cache growth and sequence-length costs; these techniques trade off training expense, memory, speed, and long-context quality.
Key takeaways
- MoE routes each token to a learned top-k subset of smaller feed-forward experts, reducing inference FLOPs while retaining all expert parameters in the model’s total capacity.
- An MoE model with 7 billion total parameters and 1 billion active parameters can have inference compute closer to a 1-billion dense model, but its pretraining cost is closer to training all 7 billion parameters.
- Routing collapse is a feedback loop in which a few experts receive more tokens, improve faster, and attract still more traffic; auxiliary load-balancing loss or DeepSeek-V3’s expert-bias adjustments can counter it.
- MQA and GQA reduce KV-cache size by sharing key and value heads, while MLA reduces it by storing lower-dimensional key and value representations; the lecture cites a 7-to-25-fold per-token cache reduction for MLA.
- Sliding-window attention bounds context reads but can lose long-range information, whereas DeepSeek sparse attention selects relevant prior tokens before applying full attention to that subset.
- Linear-attention hybrids target long-generation efficiency by using recurrent-style updates between standard-attention layers, making them relevant to 128K-token contexts and extended coding-agent sessions.
Chapters
- The upcoming Wednesday practicum assesses whether students can detect and test bias in an LLM judge using measures such as correlation.
- The lecture’s focus is architectural efficiency: replacing dense feed-forward layers and standard attention with alternatives suited to long outputs.
- The course schedule includes an efficiency quiz, an AI-and-humanity lecture, and a final informal wrap-up.
- Mixture-of-Experts (MoE) dates back at least to 1993 and combines several expert networks with a gating or routing mechanism.
- A transformer MoE layer replaces the usual feed-forward (FFN) layer with smaller expert FFNs, activating only a few for each input.
- Sparse activation lowers inference FLOPs relative to applying all parameters of a similarly large dense model.
- MoE language models appeared in Google’s 2021 GShard and Switch Transformer work, but adoption grew as infrastructure and open-weight training improved.
- A learned linear router scores each token representation against available experts, normalizes scores, and selects the top k.
- The selected experts transform the token independently, and their outputs are combined in a score-weighted sum.
- The example configuration uses 66 experts and selects 8 for each token; expert count and k are hyperparameters.
- Expert roles are not assigned in advance: routing and expert weights learn through next-token prediction, and any interpretation of specialties is post hoc.
- The lecture compares dense and MoE models on next-token training and validation loss, plus the HellaSwag commonsense benchmark.
- MoE distinguishes total parameters, which include all experts, from active parameters, which are used for a given token.
- DeepSeek results show higher downstream performance than dense models at a similar number of active parameters.
- Sparse activation reduces inference computation, but all expert weights may still need to fit in GPU memory; quantization can reduce that memory burden.
- Routing collapse occurs when a few experts receive too many tokens, improve from the extra training, and attract still more traffic.
- An auxiliary load-balancing term combines the fraction of tokens routed to each expert with that expert’s average routing affinity.
- The token fraction measures imbalance, while the affinity term provides a differentiable signal for adjusting the router.
- The lecture reports improved training and validation behavior when the balancing loss prevents traffic from concentrating on a small number of experts.
- DeepSeek-V3 avoids an auxiliary balancing loss and instead adjusts expert-specific router biases to discourage overloaded experts.
- Fine-grained MoE uses many smaller experts and selects a subset, a design reported as increasingly common compared with fewer, broader experts.
- Some architectures add shared experts that every token uses for general capabilities, alongside token-selected specialists.
- Sparse upcycling initializes experts from a trained dense model, but training MoE from scratch is described as more widely used.
- The lecture names DeepSeek, GPT-OSS, Qwen, NVIDIA Nemotron, and GLM among model families using MoE layers.
- For a hypothetical 7-billion-total-parameter MoE with 1 billion active parameters, pretraining cost is closer to a 7-billion dense model than a 1-billion model.
- After training, inference FLOPs are closer to those of a model with the active parameter count, while model quality falls between comparable 1-billion and 7-billion dense models.
- Smaller experts divide the model’s capacity; they do not make the cost of training all expert parameters disappear.
- Standard multi-head attention uses separate query, key, and value projections for each attention head.
- Multi-Query Attention (MQA) keeps queries head-specific but shares one key head and one value head across all query heads.
- Grouped-Query Attention (GQA) shares key and value projections within groups of query heads, between MQA and standard multi-head attention.
- Reducing key and value heads shrinks KV-cache storage; the lecture notes the adoption of GQA in Llama 2 and Llama 3.
- Multi-Head Latent Attention (MLA) stores lower-dimensional representations of keys and values rather than full-size vectors.
- The compressed vectors are expanded when needed for attention computations, trading projection work for lower cache storage.
- The lecture cites DeepSeek-V2 results reporting MLA as competitive with standard multi-head attention and a 7-to-25-fold reduction in KV-cache memory per token.
- A comparison of cache-memory curves shows that aggressively shrinking cached vectors, for example to 64 dimensions, can greatly limit memory growth with context length.
- Sliding-window attention restricts each token to a fixed window of W preceding tokens, reducing cache needs from the full history to a bounded window.
- A fixed window can discard long-range dependencies, such as information about a character introduced near the beginning of a book.
- DeepSeek sparse attention first uses a cheaper selection step to identify relevant prior tokens, then runs full attention over the selected K tokens.
- Both approaches save memory and computation by limiting which past tokens contribute to attention.
- A student asks whether training all experts makes MoE more expensive; the answer distinguishes total parameters from active parameters.
- A 7-billion-total-parameter MoE requires training its full expert capacity, roughly comparable in scale to training a 7-billion dense model.
- At inference, selecting 1 billion active parameters can make per-token computation closer to a 1-billion model, while retaining more total learned capacity.
- This trade-off becomes especially valuable when reasoning models or coding agents generate long sequences and spend substantial time producing tokens.
- Linear attention aims to recover the constant-size recurrent state associated with RNNs while avoiding the information-loss limitations of ordinary recurrent models.
- The explanation starts by treating the query-key interaction as a similarity function, then expresses it using a kernel feature map.
- Factoring the kernelized equation lets the computation be updated recurrently rather than storing and comparing every prior token in the usual way.
- The lecture connects the idea to the 2020 paper “Transformers are RNNs: Fast Autoregressive Transformers with Linear Attention” and notes that the derivation is optional quiz material.
- Open-weight models increasingly combine linear attention with standard attention layers, retaining standard attention periodically.
- The lecture names MiniMax M1, Qwen-Next, and Kimi Linear as examples of models using linear-attention hybrids.
- A Qwen comparison shows that the hybrid configuration gains throughput as generation length grows, unlike the standard-attention-only comparison.
- The lecture cites 128K-token contexts and long-context benchmark quality as relevant to coding agents, where codebases and extended interactions can occupy the prompt.
- Without KV caching, generating a token requires work that grows quadratically with context length, yielding cubic work across an entire generated response.
- KV caching reduces per-token attention work to linear in context length, but the total cache grows with the sequence.
- Sliding-window attention with window W has bounded per-token context work; linear attention aims for constant per-token recurrent-state work.
- The lecture summarizes MoE as using total capacity for quality while sparse active parameters reduce inference computation.
- Inference and training memory can be consumed by weights, gradients, Adam optimizer states, and the KV cache.
- Quantization reduces the bits used for weights or cache values, while LoRA and QLoRA train fewer parameters and reduce associated gradient and optimizer-state memory.
- KV-cache options discussed include retaining fewer tokens, using MQA or GQA, compressing cached vectors with MLA, and applying linear-attention hybrids.
- Reducing per-request memory can increase batch size during both training and inference, improving hardware utilization.
- For a hypothetical 70-billion-parameter model on an 80-GB GPU, 16-bit weights require about 140 GB before accounting for cache.
- Quantizing weights to 4 bits reduces their footprint to about 35 GB, allowing a batch size of 8 in the example.
- Reducing KV heads from 64 to 8 with GQA lowers per-request cache memory to under 1 GB and raises the example batch capacity to 67.
- Further quantizing the KV cache to 8 bits raises the example capacity to 134 simultaneous prompts.
- Prompt prefill processes all prompt tokens in a forward pass, whereas autoregressive decoding generates output tokens one at a time.
- Speculative decoding uses a smaller draft model to propose tokens that a larger model can verify, potentially lowering generation time.
- KV caching and the architectural changes covered in the lecture reduce the cost of processing each decoding step.
- The closing review positions MoE and attention efficiency as active research areas, with the lecture intended as a foundation for further reading.
Transcript excerpt
0:00 Okay. Uh we are ready to start. Um how was your little break? Was it nice? >> Yeah. Good. Um okay. Um today we have another a little bit on steroids lecture with a lot of new technical advancement. You should think of today's lecture as a springboard to learn more about different things. I have very basic knowledge of what I'm going to teach you today. It's very recent. Uh the content of today's lecture. Um so since it's not my area of research, I didn't go too deep uh into it. So I'll give you some basic information and then uh you can kind of deep d have a deep dive in
0:49 whatever uh of the multiple topics we are going to uh cover. So my answers today might be a little like I don't know uh but happy happy to hear out what you uh learned uh after after the lecture. Um as a reminder we have on Wednesday our final practicum. Uh this one will be on the LLM judge as you know from your practice assignment. As I said, um the goal of your preparation until Wednesday should be to really have confidence that you can if someone tells you, hey, uh is this is this judge biased? And you should have ideas of how it can be biased and how you can test for whether there is such a bias, you should, you know, calculate some correlation and make a judgment from there. So this is what I'm going to uh
1:36 test you for and something you'll do many times uh in your life after after this course if you work in NLP. uh and then on Monday we will have a quiz on efficiency part uh and another lecture on impact of AI on humanity and then on
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, UofU Data Science.