Sub topic 14
Serving large language models and GPUs
This chapter covers what changes when the model behind an InferenceService is a large language model (LLM) that needs GPUs. It assumes you can already deploy an InferenceService, that you know how a serving runtime is chosen from modelFormat, and that you understand storageUri and concurrency-based autoscaling from earlier chapters. You also need basic Kubernetes scheduling knowledge: resource limits, node selectors, and taints.
Why LLMs change the picture
The classic scikit-learn example from earlier chapters has weights of a few kilobytes. It starts in a second, runs on a CPU, and answers each request in milliseconds. An LLM breaks all three assumptions.
- Size. An 8-billion-parameter model in 16-bit precision has about 16 GB of weights. A 70-billion-parameter model needs about 140 GB.
- Hardware. Token generation is dominated by memory bandwidth and matrix math. In practice that means GPUs.
- Request shape. One request produces many tokens, one after another. A request can take seconds or minutes, and clients often want the tokens streamed back as they are produced.
KServe handles these cases with the same InferenceService resource. The differences are in the runtime, the resource requests, the storage path, and the autoscaling settings.
The Hugging Face runtime and vLLM
KServe ships a Hugging Face serving runtime. You select it with modelFormat.name: huggingface. For supported text-generation models, it uses vLLM as the inference engine when possible. vLLM batches concurrent requests continuously and manages the attention key-value (KV) cache efficiently. The runtime exposes an OpenAI-compatible HTTP API, so existing OpenAI client libraries can talk to it by changing the base URL.
Two details matter in practice:
- The runtime image and its flags change between KServe releases. Check the documentation for your installed version before copying flags.
- Arguments the runtime does not consume itself are passed to the underlying engine. This is how you set limits such as maximum context length.
A GPU-backed InferenceService
This manifest serves an 8B instruction-tuned model from the Hugging Face Hub. It assumes a Secret named hf-secret with a key token, and a cluster with the NVIDIA device plugin installed.
apiVersion: serving.kserve.io/v1beta1
kind: InferenceService
metadata:
name: llm-demo
spec:
predictor:
minReplicas: 1
maxReplicas: 2
nodeSelector:
nvidia.com/gpu.present: "true"
tolerations:
- key: nvidia.com/gpu
operator: Exists
effect: NoSchedule
model:
modelFormat:
name: huggingface
args:
- --model_name=llama3
- --model_id=meta-llama/Meta-Llama-3-8B-Instruct
- --max_model_len=4096
env:
- name: HF_TOKEN
valueFrom:
secretKeyRef:
name: hf-secret
key: token
resources:
requests:
cpu: "4"
memory: 24Gi
nvidia.com/gpu: "1"
limits:
cpu: "4"
memory: 24Gi
nvidia.com/gpu: "1"The parts that differ from earlier examples:
nvidia.com/gpuappears underlimits. Kubernetes treats GPUs as whole, non-overcommittable extended resources. You set the limit, and the request is set to match it. Fractions are not allowed.nodeSelectorandtolerationsplace the pod on GPU nodes. Many clusters taint GPU nodes so that ordinary workloads stay off them. The label shown is set by NVIDIA GPU feature discovery. Your cluster may use a different label.HF_TOKENcomes from a Secret. Gated models such as Llama require an authenticated download. Never put the token in the manifest.--max_model_lencaps the context length. A smaller cap shrinks the KV cache the engine reserves, which leaves more GPU memory for concurrent requests.
Calling the endpoint
The OpenAI-compatible routes live under /openai/v1/. The model name in the body must match --model_name.
curl -H "Host: ${SERVICE_HOSTNAME}" -H "Content-Type: application/json" \
http://${INGRESS_HOST}:${INGRESS_PORT}/openai/v1/chat/completions \
-d '{
"model": "llama3",
"messages": [{"role": "user", "content": "Explain KServe in one sentence."}],
"max_tokens": 64,
"stream": false
}'Set "stream": true to receive server-sent events as tokens are generated. Streaming keeps the connection open for the full generation time. Check that your ingress, gateway, and any proxy idle timeouts are longer than your longest expected response, or long generations will be cut off.
Sizing GPU memory
Before choosing a GPU, estimate memory. Weights take roughly parameters multiplied by bytes per parameter. The KV cache and runtime overhead come on top of that, and the KV cache grows with context length and the number of concurrent sequences.
| Model size | 16-bit weights | 8-bit weights | 4-bit weights |
|---|---|---|---|
| 8B | about 16 GB | about 8 GB | about 4 GB |
| 70B | about 140 GB | about 70 GB | about 35 GB |
An 8B model in 16-bit precision on a 24 GB GPU leaves only a few gigabytes for the KV cache. That works for low concurrency and short contexts. A 70B model does not fit on any single common GPU in 16-bit precision. You either quantize it or split it across GPUs.
Splitting is called tensor parallelism. In vLLM you pass a flag such as --tensor_parallel_size=4 and request nvidia.com/gpu: "4". All four GPUs must be on the same node, and fast interconnect such as NVLink helps a lot. Tensor-parallel workers also communicate through shared memory. If the pod fails with shared-memory errors, mount a memory-backed emptyDir at /dev/shm and set a generous size limit.
Model loading and storage
Weights this large change how you think about the storage step from the earlier chapter.
- Download time. Pulling 16 GB from the Hub on every pod start can take minutes. The pod cannot become ready until loading finishes.
- Rate limits and availability. Many replicas starting at once all hit the Hub. An outage or throttle then blocks scale-out.
- Better pattern. Copy the weights once to object storage or a PersistentVolumeClaim, then reference it with
storageUri(for examples3://...orpvc://...). Recent KServe versions also accepthf://URIs. A pre-populated PVC mounted read-only avoids repeated downloads entirely.
Also size the node's ephemeral storage and the container's memory. Loading a model can briefly use host memory comparable to the weight size.
Autoscaling and cold starts
The concurrency-based scaling from the autoscaling chapter still applies, but the trade-offs shift.
- Scale to zero is risky. A cold start includes scheduling onto a GPU node (possibly triggering a node autoscaler to add one), pulling a large container image, loading weights, and initializing the engine. This can take many minutes. For interactive use, set
minReplicas: 1. Use scale to zero only for batch or occasional workloads that tolerate the delay. - Concurrency targets differ. One vLLM replica handles many requests at once through batching, so the
scaleTargetthat suits a small model is far too low here. Measure the concurrency at which latency starts to degrade on your GPU, and set the target somewhat below it. - Request count is a weak signal. Two requests with 50 and 4,000 output tokens load the GPU very differently. If concurrency tracks load poorly, consider scaling on engine metrics such as queue depth or KV cache usage. This needs a metrics-based autoscaler such as KEDA with RawDeployment mode. Check which scalers your KServe version supports.
- GPUs are scarce.
maxReplicasis only a ceiling. If no GPU is free, new pods stay Pending, so the effective limit is your GPU quota.
Troubleshooting
| Symptom | Likely cause | What to check |
|---|---|---|
| Pod stays Pending | No node has a free GPU, or a taint is not tolerated | `kubectl describe pod`, look at scheduling events |
| Pod restarts with CUDA out-of-memory | Weights plus KV cache exceed GPU memory | Lower `--max_model_len`, quantize, or use a larger GPU |
| 401 or 403 during startup | Missing or invalid `HF_TOKEN`, or license not accepted | Secret contents, and model access on the Hub |
| Never becomes ready | Download or load is slow | Container logs, and the readiness probe timing |
| Requests cut off mid-answer | Proxy or ingress timeout | Gateway timeout settings |
Start with kubectl logs on the kserve-container. Engine errors about memory and unsupported architectures show up there.
Key points
- LLM serving uses the same InferenceService resource, with the
huggingfacemodel format and a GPU resource limit. - The runtime exposes OpenAI-compatible routes under
/openai/v1/. - Plan GPU memory from parameter count and precision, and leave room for the KV cache.
- Pre-stage weights on a PVC or in object storage, and keep at least one replica warm for interactive traffic.
Exercise
A 70B-parameter model is stored in 16-bit precision. A node has four 80 GB GPUs. Roughly how much memory do the weights need, and what settings would you add to the InferenceService to serve it?
Show answer
The weights need about 140 GB (70 billion parameters times 2 bytes). That does not fit on one 80 GB GPU, but it fits across four with room for the KV cache (320 GB total). Request nvidia.com/gpu: "4" in requests and limits, pass a tensor-parallel flag such as --tensor_parallel_size=4 to the engine, and make sure the pod lands on a single node with four GPUs. Mount a memory-backed emptyDir at /dev/shm if shared-memory errors appear.
Exercise
Your LLM service uses minReplicas: 0. Users complain that the first request after a quiet period times out. Explain why, and give two changes that reduce the problem.
Show answer
With scale to zero, the first request must wait for a new pod to be scheduled on a GPU node, pull the image, load the weights, and start the engine. That can take minutes, which exceeds client timeouts. Set minReplicas: 1 to keep a warm replica. Alternatively, store the weights on a pre-populated PVC and use a cached image so startup is faster.