Skip to main content

Chapter 2: Optimization for Inference

Maximizing Inference Performance​

Techniques for fast, efficient inference in production.


Inference Servers​

ServerStrengthUse Case
vLLMHigh throughputProduction APIs
TGIHF integrationSimple deployment
llama.cppCPU efficiencyEdge/local
OllamaEase of useLocal development

vLLM Deployment​

from vllm import LLM, SamplingParams

llm = LLM(model="./merged_model")
sampling_params = SamplingParams(temperature=0.7, max_tokens=256)

outputs = llm.generate(["Hello, how are you?"], sampling_params)
print(outputs[0].outputs[0].text)

Quantization for Inference​

LevelMemorySpeedQuality
FP16100%1x100%
INT850%1.5x99%
INT425%2x95-98%

Optimization Checklist​

□ Merge LoRA adapter
□ Apply inference quantization (INT8/INT4)
□ Enable KV cache
□ Use appropriate batch size
□ Enable Flash Attention
□ Consider speculative decoding
□ Set up proper monitoring

Continue to Chapter 3 →