UUtah CS 6340 NLP | Fall 2026 | LLM efficiency: QLoRA, Caching, Speculative Decoding
Overview
The lecture explains how QLoRA makes fine-tuning large language models more accessible by combining low-rank adapters with 4-bit quantization, then covers inference techniques that reduce recomputation or latency. It details NF4, double quantization, paged optimizers, KV caching and its memory costs, prompt caching, and speculative decoding, including a reported 65-billion-parameter QLoRA run on a 48 GB GPU and speculative-decoding speedups of up to 3× for summarization and 5× for coding tasks.
Key takeaways
- QLoRA combines 4-bit NF4 storage for frozen base weights with trainable LoRA adapters, and a cited 2023 result fine-tuned a 65B model on a single 48 GB GPU.
- For QLoRA, NF4, double quantization, and paged optimizers address distinct memory costs: weight precision, quantization-scale overhead, and optimizer-state spikes.
- KV caching is an exact optimization for causal attention: cache earlier keys and values, append the current token's vectors, and avoid recomputing prior positions.
- KV caches can become a major memory bottleneck because storage grows with context length and batch size; quantizing the cache to int8 or int4 saves memory but introduces approximation error.
- Prompt caching can reduce repeated prompt processing and API charges when stable instructions and examples form an exact reusable prefix; place changing user data later in the prompt.
- Speculative decoding preserves target-model sampling while accelerating generation by verifying multiple small-model draft tokens in one target-model pass; the lecture cites up to 3× speedup for summarization and 5× for coding-oriented suffix decoding.
Chapters
- LoRA represents a weight update as a low-rank product of adapters A and B, training those matrices instead of the full weight matrix.
- The original LoRA work recommended applying adapters to attention projections, while QLoRA recommends targeting all linear transformations, especially for larger models.
- Typical ranks range from 8 or 16 for fast fine-tuning to a maximum around 128; alpha is commonly set to the rank or twice the rank.
- Training fewer parameters reduces the gradient and optimizer-state memory needed for backpropagation, helping models fit on available GPUs.
- After training, A and B can be merged into the base weights, so LoRA does not require extra adapter computation at inference.
- Rank selection remains a dataset-dependent hyperparameter, with ongoing work investigating how to predict a suitable LoRA rank.
- QLoRA combines LoRA fine-tuning with 4-bit storage of the frozen base model, reducing the memory required for its weights.
- Block quantization limits the effect of an outlier to a small group, such as a window of 128 weights, instead of distorting the scale for an entire matrix.
- The method aims to make larger open-weight models fine-tunable on hardware that cannot support full-precision training.
- NF4 uses the approximately normal distribution of model weights to allocate quantization levels more effectively around zero.
- Double quantization compresses the scale factors used for block quantization, reducing the overhead of storing many block-level scales.
- Paged optimizers can move Adam optimizer state between GPU and CPU during memory spikes, trading transfer time for lower GPU-memory use on long sequences.
- The setup stores frozen base weights in 4-bit NF4, quantizes scale factors to 8 bits, and trains LoRA matrices in 16-bit precision.
- For each layer, weights are dequantized as needed for computation, combined with the adapter update, and then discarded to avoid retaining full-precision copies.
- The 2023 QLoRA result fine-tuned a 65-billion-parameter model on one 48 GB GPU with quality reported as comparable to full 16-bit fine-tuning.
- LoRA and QLoRA describe how model weights are represented and updated, rather than prescribing a particular training loss.
- The same adapter approach can be used with supervised fine-tuning using negative log-likelihood or post-training methods such as GRPO.
- Dequantization adds computation, but fitting the model on a GPU can be preferable to relying on much slower CPU-based training.
- Hugging Face Transformers provides BitsAndBytesConfig for 4-bit loading, NF4, compute precision, and optional double quantization.
- PEFT's LoRA configuration specifies rank, alpha, target modules, dropout, and the task type; target modules determine which linear layers receive adapters.
- Gradient checkpointing saves GPU memory by discarding forward-pass activations and recomputing them during backpropagation, at the cost of extra computation.
- Start with prompting and development-set evaluation; fine-tune only when prompting does not achieve the task's desired performance.
- Use full fine-tuning when the model and sequence lengths fit in GPU memory; try LoRA or QLoRA when memory is the limiting factor.
- Compare model sizes as well as methods: if a 7B model performs as well as a 70B model on the task, the smaller model can reduce inference cost and latency.
- Inference generates tokens sequentially, unlike teacher-forced training, so naively recomputing attention over the entire growing sequence wastes work.
- At each attention layer, the current token uses queries, keys, and values to attend to earlier tokens under causal attention.
- Long contexts make repeated attention computation increasingly expensive, motivating reuse of previously computed keys and values.
- KV caching stores each token's key and value vectors and appends the newly generated token's vectors at every decoding step.
- The current query attends to the cached keys and values, avoiding recomputation for earlier positions; prior queries need not be cached.
- The method is exact for causal attention because earlier token representations do not change when later tokens are generated.
- Cache size grows with sequence length and batch size; the lecture estimates about 2 MB per token for a 70B model using full multi-head attention at 16-bit precision.
- A 2,000-token sequence can therefore require several gigabytes of cache, while long contexts and multi-user batches can push memory requirements much higher.
- Quantized KV caching stores new keys and values in int8 or int4, then dequantizes them for attention, reducing memory but introducing approximation error and extra computation.
- Time to first token mainly reflects processing the supplied prompt, which can be handled in a forward pass; subsequent output tokens are generated sequentially.
- Prompt caching reuses computation for an exact repeated prefix, avoiding another pass over that cached portion and potentially reducing API costs.
- Provider behavior differs: some APIs may cache automatically, while others require explicit configuration, so usage and billing dashboards are worth checking.
- Put stable instructions and examples at the beginning of prompts, followed by user-specific or otherwise changing context.
- Prompt caching generally relies on exact prefix matches; changing an early detail, such as a user ID, can prevent reuse of the rest of the prefix.
- Large repeated system prompts, including detailed LLM-judge rubrics and examples, are especially good candidates for caching.
- A small draft model proposes several tokens, such as five, and the larger target model verifies them together in one forward pass.
- The target can process the known draft sequence in parallel because causal attention and teacher-forcing-style inputs provide the token context.
- Verification proceeds left to right, accepting draft tokens probabilistically and stopping at the first rejection.
- A draft token is accepted with probability min(1, target probability divided by draft probability); at rejection, the next token is sampled from the normalized positive part of the target-minus-draft distribution.
- Tokens drafted after the first rejection are discarded because they were conditioned on an input token that is no longer present.
- The lecture cites roughly 3× faster CNN/Daily Mail summarization and describes newer approaches such as Medusa, Eagle, and suffix decoding, with suffix decoding useful for repetitive coding outputs.
- QLoRA reduces fine-tuning memory through 4-bit base weights and trainable low-rank adapters; KV caching avoids repeated attention work but increases memory use.
- Prompt caching reuses exact repeated prefixes, while speculative decoding uses a draft-and-verify loop to accelerate generation without changing the target model's output distribution.
- The next lecture topic is architectural efficiency, including methods that address KV-cache size and attention computation.
Transcript excerpt
0:00 So, we started to talk about parameter efficient fine tuning and then I introduced one approach to do so called LoRA, which if you remember is has those two adapters we called A and B which decompose each uh weight matrix uh into a product of those two and we assume they are low rank, so they together have less parameters than the full matrix uh has. And we change their weights rather than the uh you know the the full uh matrices weights. Uh so we introduced that but uh there are certain hyper parameters involved in uh LoRA. Uh so the first question is do we have uh adapters A and B for every single weight uh in our transformer architecture and we have many of such
0:48 weight matrices. In the original LoRA paper they have actually um recommended um to apply adapters only to attention heads. Uh they tried to add more that didn't really uh improve performance, so by having less adapters you have less parameters to train and everything is uh faster, right? Because you have uh less memory uh uh that that you you have less less uh consumption uh of the GPU memory. Um however for Q LoRA which is an advancement atop of LoRA that will I will introduce um today. Um Q LoRA authors have recommended to actually uh change the weights of all linear transformations, so all weight
1:39 matrices that are involved in a linear mapping of token representations. So their recommendation is a is a is a uh bit different. So you're not just changing attention heads which themselves have query, key and value linear transformations but all linear transformation in the um in the transformer and they found that is especially important for larger models. So, I would say this is yet
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.