6 min total

LLMs

Efficiently serving LLMs

Efficiently serving LLMs

KV caching

LLMs — Efficiently serving LLMs
LLMs — Efficiently serving LLMs

When generating a token naïve way would be to reprocess all previous tokens to compute new K and V for self-attention

With KV caching, the model stores the computed K and V tensors for previous tokens in GPU memory as a cache, when generating the next token, it only computes Q for new token and reuses all stored K and V values.

Batching

  1. Multiple input sequences come in (e.g., from users).
  2. Each sequence may be at a different stage (some might be longer, others shorter).
  3. You pad them to the same length (or use advanced batching logic).
  4. Run the model on all sequences in parallel.
LLMs — Efficiently serving LLMs
LLMs — Efficiently serving LLMs — notes
LLMs — Efficiently serving LLMs
LLMs — Efficiently serving LLMs

Continuous Batching

Requests come in asynchronously. To batch them:

  • Maintain a request queue.
  • Every few milliseconds (e.g. 10ms), collect as many requests as possible.
  • Combine them into a batch.
  • Run a single forward pass on the batch.

But this flexibility needs smart batching strategies — especially when some sequences finish earlier than others, or when some can't be batched together.

That’s where filtering comes in.

LLMs — Efficiently serving LLMs

Filtering batching refers to:

💡 "Choosing which sequences in the current batch should participate in the next forward pass."

It filters out sequences that:

  • Are already done (e.g., have generated an EOS token)
  • Don’t need more tokens now (e.g., waiting for user input or throttled)
  • Would slow down the batch because of incompatibility (e.g., different decoding strategies like greedy vs sampling)
LLMs — Efficiently serving LLMs

Quantization

LLMs — Efficiently serving LLMs

Low Rank Adaptation

Parameter efficient fine tuning method

LoRA freezes original model weights and learns a tiny low rank update matrices that are injected during training

"why update the entire piano when you just want to retune a few strings?"

LLMs — Efficiently serving LLMs

Multi-LoRA inference

Now imagine you've fine-tuned multiple LoRA adapters for:

  • Sentiment analysis
  • Code generation
  • Legal document understanding
  • Healthcare QA

You don’t want to reload and unload a new model for each task.

Then combine them

A. One-at-a-Time Activation

  • Load all LoRA weights into memory.
  • At runtime, activate only the task-specific adapter.
  • e.g. for request A use LoRA_a, for request B use LoRA_b

✔️ Efficient

✔️ Modular

❌ Not composable

B. Compositional Activation (Merged Inference)

  • Merge multiple LoRA adapters at inference time.
  • Possible with weight merging if they don’t conflict.
  • Trickier if adapters touch overlapping parameters.

LoRAX

LoRAX is a runtime system built to serve many LoRA adapters simultaneously — optimized for high-throughput, low-latency inference.

Imagine:

  • One GPU
  • 500+ LoRA adapters (different tasks/users)
  • Requests come in randomly
  • How to serve all requests without reloading weights every time?

LoRAX virtualizes and sparsifies LoRA adapter memory.

  • PagedAttention to enable batched, dynamic inference
  • Sparse LoRA Routing: Only apply deltas from active adapters
  • Shared Compute Graphs: Reuse computation between adapters if possible
  • Parallel MLP/Attention Fusion

Each LoRA adapter’s deltas (BA) are kept in GPU memory, organized in paged blocks

At each inference step:

  • Check which adapter is needed
  • Route input through base model + adapter deltas
  • Batch together compatible adapter requests

Use CUDA kernels that can handle multiple deltas simultaneously