Skip to main content

Inference optimisation

Squeeze latency and memory with attention backends, KV cache strategies, torch.compile, continuous batching and parallelism.

Adapted for this playbook from the 🤗 Transformers documentation by Hugging Face. Official page: https://huggingface.co/docs/transformers/optimization_overview. Images © Hugging Face (documentation-images) unless noted. This is not a substitute for the upstream docs — verify against the current version.

Covers Hugging Face pages: optimization_overview, attention_interface, kv_cache, cache_explanation, continuous_batching, paged_attention, perf_torch_compile, perf_infer_gpu_multi, llm_tutorial_optimization

High-impact levers​

LeverWhat it does
Dtypefloat16 / bfloat16 cut memory and boost throughput
Attention backendsSDPA / FlashAttention for faster attention
KV cacheAvoid recomputing past keys/values; choose cache implementation
torch.compileFuse kernels for steady-state speedups
Continuous batchingKeep GPUs busy across concurrent requests
Paged attentionMemory-efficient KV management for serving
Tensor / expert parallelismShard large models across GPUs

Official entry: Optimisation overview.

KV cache​

# generate uses KV cache by default for decoder-only models
out = model.generate(**inputs, max_new_tokens=128, use_cache=True)

Deep dive: Cache strategies and Caching.

When to leave Transformers eager serving​

If you need OpenAI-compatible HTTP, high concurrency and paged attention at scale, deploy vLLM / SGLang while keeping the same Hub checkpoint.

Discussion

Comments​

Share feedback or questions about this page. No account required.

Loading comments…