20 min
Lesson 3 of 6
Inference Acceleration
From the course AI Model Optimization
Optimize inference speed with batching, caching, and specialized runtimes.
KV Cache Optimization
PagedAttention (vLLM)
from vllm import LLM, SamplingParams
llm = LLM(
model="meta-llama/Llama-2-7b-hf",
tensor_parallel_size=2,
gpu_memory_utilization=0.9
)
# Efficient batched inference
sampling_params = SamplingParams(temperature=0.7, max_tokens=256)
outputs = llm.generate(prompts, sampling_params)
Continuous Batching
Traditional: Wait for batch to complete Continuous: Process requests as they finish
Benefits:
- 2-3x throughput improvement
- Lower latency for short requests
- Better GPU utilization
Speculative Decoding
class SpeculativeDecoder:
def __init__(self, target_model, draft_model):
self.target = target_model # Large, accurate
self.draft = draft_model # Small, fast
def generate(self, prompt, k=4):
tokens = []
while not done:
# Draft model generates k tokens quickly
draft_tokens = self.draft.generate(prompt, num_tokens=k)
# Target model verifies in parallel
accepted = self.target.verify(prompt, draft_tokens)
tokens.extend(accepted)
prompt = prompt + accepted
return tokens
Hardware-Specific Optimization
TensorRT (NVIDIA)
import tensorrt as trt
# Convert model to TensorRT
builder = trt.Builder(logger)
network = builder.create_network()
parser = trt.OnnxParser(network, logger)
parser.parse_from_file("model.onnx")
engine = builder.build_cuda_engine(network)
# 2-4x faster inference on NVIDIA GPUs
Key Takeaways
- •PagedAttention in vLLM enables efficient memory management for long sequences
- •Continuous batching improves throughput 2-3x over traditional batching
- •Speculative decoding uses small draft model to accelerate large model inference
- •TensorRT optimization provides 2-4x speedup on NVIDIA hardware
0 of 6 lessons marked complete
Progress is kept in this browser only.