VISUAL LESSON · 6 PICTURES

Parameter-Efficient Tuning: LoRA, QLoRA & Mobile KV Cache

How do low-rank matrix decomposition (LoRA) and 4-bit NormalFloat quantization (QLoRA) enable fine-tuning on consumer hardware, and how is the KV cache optimized on mobile devices?

Progress stays on this device · no login
BEFORE THE FIRST PICTURE

How this lesson unfolds.

Begin with How does low-rank matrix decomposition W = W_0 + B*A enable specialized fine-tuning while updating less than 1% of parameters?. Follow the visual diagrams to build an intuitive, mathematically grounded mental model of lora & qlora: low-rank adapter fine-tuning on edge hardware.

Picture 1 of 6 · tap to inspect in full resolution
01

How does lora low rank adaptation foundations operate mathematically within the overall LoRA & QLoRA: Low-Rank Adapter Fine-Tuning on Edge Hardware?

INTUITION

Instead of repainting an entire 7-billion-pixel mural, you clip a tiny piece of clear plastic over the canvas and sketch only the small changes. The original painting (frozen base weights W₀) remains completely untouched, while your lightweight plastic overlay (B · A) adds the custom fine-tuned behavior.

MATHEMATICAL STATEMENT
  1. $$\mathbf{y} = f(\mathbf{X} \mathbf{W} + \mathbf{b})$$
✦ More context & mechanics
Conceptual Mechanics & Logic

LoRA freezes the pretrained weight matrix W₀ ∈ R^(d × k) and parameterizes the update as the low-rank product of two compact matrices:

W = W₀ + Δ W = W₀ + (α) / (r) B A

where A ∈ R^(r × k) is initialized with Gaussian noise N(0, σ²), B ∈ R^(d × r) is initialized to zero, and rank r ≪ min(d, k) (typically r ∈ [8, 64]). The scalar α scales the adapter learning rate.

Real-World Usage

The universal industry standard for fine-tuning LLMs and diffusion models across consumer GPUs (RTX 4090, Apple Silicon).

History & Origin Story

Introduced by Edward Hu et al. (Microsoft, 2021) in 'LoRA: Low-Rank Adaptation of Large Language Models', reducing trainable parameters by 10,000×.

How to Memorize & Golden Words

Freeze the giant base matrix; train the low-rank sandwich B · A: zero inference latency when weights are merged.

Picture 2 of 6 · tap to inspect in full resolution
02

How does qlora 4bit frozen base adapter training operate mathematically within the overall LoRA & QLoRA: Low-Rank Adapter Fine-Tuning on Edge Hardware?

INTUITION

Freezing a 14 GB model into a tiny 3.5 GB block of ice using 4-bit NormalFloat (NF4), and then grafting tiny 16-bit LoRA adapter branches onto the frozen trunk. You fine-tune a world-class model on a single 150 graphics card!

MATHEMATICAL STATEMENT
  1. $$\mathbf{y} = f(\mathbf{X} \mathbf{W} + \mathbf{b})$$
✦ More context & mechanics
Conceptual Mechanics & Logic

QLoRA introduces three breakthrough innovations:

  1. 4-bit NormalFloat (NF4): An information-theoretically optimal quantile quantization for normally distributed weights.
  2. Double Quantization (DQ): Quantizes the quantization constants c₁ themselves to 8-bit, saving 0.37 bits per parameter (3 GB on a 65B model).
  3. Paged Optimizers: Uses CUDA unified memory to page memory automatically between GPU VRAM and CPU RAM during activation spikes.
Real-World Usage

The standard fine-tuning engine powering open-source tools like Unsloth, Axolotl, and Hugging Face TRL.

History & Origin Story

Invented by Tim Dettmers et al. (University of Washington, 2023) in 'QLoRA: Efficient Finetuning of Quantized LLMs'.

How to Memorize & Golden Words

NF4 stores the frozen base; double quantization saves the scales; 16-bit LoRA learns the task.

Picture 3 of 6 · tap to inspect in full resolution
03

How does domain adaptation specialized slm roles operate mathematically within the overall LoRA & QLoRA: Low-Rank Adapter Fine-Tuning on Edge Hardware?

INTUITION

Instead of carrying 10 heavy encyclopedias for medicine, law, code, and finance, you carry 1 small pocket dictionary and 10 lightweight 5-page cheat sheets (10 MB LoRA adapters). You swap the cheat sheet in seconds to turn the pocket dictionary into a specialized doctor, lawyer, or programmer.

MATHEMATICAL STATEMENT
  1. $$\mathbf{y} = f(\mathbf{X} \mathbf{W} + \mathbf{b})$$
✦ More context & mechanics
Conceptual Mechanics & Logic

Multi-adapter serving shares a single frozen base model in VRAM and dynamically applies task-specific low-rank offsets per request:

yᵢ = Xᵢ W₀ + (αₖ) / (rₖ) Xᵢ Bₖ Aₖ

where adapter index k matches the user request task ID (e.g. medical QA vs python code completion).

Real-World Usage

Deployed in enterprise multi-tenant serving (P-LoRA, S-LoRA) to serve thousands of personalized fine-tuned models on a single GPU.

History & Origin Story

Developed by Sheng et al. (2023) in 'S-LoRA: Serving Thousands of Concurrent LoRA Adapters', demonstrating that adapter switching costs under 1 millisecond.

How to Memorize & Golden Words

One base model, thousands of personas: adapter swapping turns general models into specialized domain experts.

Picture 4 of 6 · tap to inspect in full resolution
04

How does kv cache memory optimization mobile operate mathematically within the overall LoRA & QLoRA: Low-Rank Adapter Fine-Tuning on Edge Hardware?

INTUITION

When chatting with a bot on your phone, every previous word in the conversation stores a memory fingerprint (Key and Value tensors). After 2,000 words, that memory takes up more RAM than the entire model itself! Optimizing the KV cache keeps your phone from crashing with 'Out of Memory'.

MATHEMATICAL STATEMENT
  1. $$\mathbf{y} = f(\mathbf{X} \mathbf{W} + \mathbf{b})$$
✦ More context & mechanics
Conceptual Mechanics & Logic

The memory consumption of standard FP16 KV cache scales linearly with sequence length T and batch size B:

Memory_(KV) = 2 × 2 bytes × L × H_(KV) × dₖ × T × B

For a 32-layer model with H_(KV) = 4 (GQA) and dₖ = 128, context of T = 4096 requires 2 × 2 × 32 × 4 × 128 × 4096 ≈ 268 MB per batch. Quantizing the KV cache to INT8 or INT4 cuts this footprint to 134 MB or 67 MB.

Real-World Usage

Essential in on-device AI runtimes (Apple Intelligence on iPhone 15 Pro/16, Google Nano on Pixel 8/9, llama.cpp on Android).

History & Origin Story

KV caching originated in early seq2seq transformers; mobile quantization of the cache was standardized by Hooper et al. (2024) in KV-Quant.

How to Memorize & Golden Words

GQA reduces head count; INT8 quantization reduces bit width: shrink the KV cache to keep mobile context alive.

Picture 5 of 6 · tap to inspect in full resolution
05

How does speculative decoding slm draft acceleration operate mathematically within the overall LoRA & QLoRA: Low-Rank Adapter Fine-Tuning on Edge Hardware?

INTUITION

A brilliant but slow professor pairing up with an eager, lightning-fast student. The student quickly drafts 4 candidate words in 10 milliseconds. The professor glances at all 4 words at once in a single parallel check: 'Word 1 is good, Word 2 is good, Word 3 is wrong.' The system accepts the first 2 words in 1 step, doubling generation speed!

MATHEMATICAL STATEMENT
  1. $$\mathbf{y} = f(\mathbf{X} \mathbf{W} + \mathbf{b})$$
✦ More context & mechanics
Conceptual Mechanics & Logic

Speculative decoding uses a small draft model M_(draft) (e.g. 1B) to generate γ draft tokens sequentially. The large target model M_(target) (e.g. 70B) evaluates all γ+1 positions simultaneously in a single parallel forward pass. Tokens are accepted using speculative rejection sampling:

P(accept x_(t+1)) = min(1, (P_(target)(x_(t+1) | x_(<t+1))) / (P_(draft)(x_(t+1) | x_(<t+1))))

If accepted, the system generates up to γ+1 tokens in the time of a single target model forward step.

Real-World Usage

Standard acceleration technique in production LLM inference (vLLM, TensorRT-LLM, Apple Intelligence on Mac/iPhone).

History & Origin Story

Introduced simultaneously by Leviathan, Kalman, and Matias (Google, 2023) and Chen et al. (DeepMind, 2023) in 'Fast Inference from Transformers via Speculative Decoding'.

How to Memorize & Golden Words

Draft fast with the small model; verify in parallel with the large model: mathematical speedup with zero quality loss.

Picture 6 of 6 · tap to inspect in full resolution
06

How does edge deployment npu benchmarking efficiency operate mathematically within the overall LoRA & QLoRA: Low-Rank Adapter Fine-Tuning on Edge Hardware?

INTUITION

Taking the sports car to the dyno test track: you measure not just top speed (tokens per second), but also gas mileage (watts of battery power consumed) and engine temperature (thermal degrees Celsius). A model that runs at 50 tokens/sec but sets the phone on fire in 30 seconds is a failure.

MATHEMATICAL STATEMENT
  1. $$\mathbf{y} = f(\mathbf{X} \mathbf{W} + \mathbf{b})$$
✦ More context & mechanics
Conceptual Mechanics & Logic

Comprehensive edge benchmarking evaluates four primary hardware metrics:

  1. Time-to-First-Token (TTFT): Prompt processing latency (dominated by compute FLOPs).
  2. Inter-Token-Latency (ITL): Generation speed (dominated by memory bandwidth GB/s).
  3. Power Draw (Watts): Energy per token = (Power (W)) / (Tokens per second).
  4. Thermal Dissipation: Degrees Celsius over continuous 5-minute sustained generation.
Real-World Usage

Reported in mobile chipset specifications (Apple M4 Neural Engine, Qualcomm Snapdragon X Elite NPU, MediaTek Dimensity 9300).

History & Origin Story

Standardized by MLPerf Mobile (MLCommons) to provide transparent, reproducible benchmarking across heterogeneous mobile SoCs.

How to Memorize & Golden Words

Tokens per second is speed; tokens per joule is efficiency: mobile AI requires both.