Fix Kubernetes GPU OOM Errors in LLM Workloads
In this article
GPU out-of-memory (OOM) errors in Kubernetes LLM workloads usually mean your model's memory footprint exceeds the GPU's available VRAM. This guide walks you through diagnosing and fixing these errors, from checking pod status to adjusting model parallelism and Kubernetes resource limits.
What You'll Need
- kubectl access to your cluster (with permissions to view pods and describe nodes)
- Access to your LLM deployment manifests (YAML files)
- Basic understanding of model sizes and GPU memory (e.g., a 7B parameter model in FP16 needs ~14GB VRAM)
Step-by-Step
- Confirm the OOM error. Run
kubectl get podsto find your pod. Thenkubectl describe pod. Look for events like "OOMKilled" or "Failed to allocate memory". Also checkkubectl logsfor CUDA errors like "CUDA error: out of memory". - Check GPU memory usage. Use
kubectl execinto the pod and runnvidia-smito see how much VRAM is used. If you can't exec, check node metrics withkubectl top node(if metrics-server is installed) or use a monitoring tool like Prometheus. - Verify your resource requests and limits. In your deployment YAML, check the
resources.limitsfornvidia.com/gpu. If you set a limit of 1 GPU, the scheduler will assign one GPU, but your model might need more memory than that GPU has. If you set a limit of 0, the pod might not get a GPU at all. - Reduce the model's memory footprint. The most common fix is to use a smaller model or a quantized version. For example, switch from FP16 to INT8 quantization (e.g., using bitsandbytes or GPTQ). This can reduce memory usage by 2-4x. Alternatively, enable memory-efficient attention (e.g., FlashAttention) if your framework supports it.
- Enable model parallelism. If you must use a large model, split it across multiple GPUs. Use libraries like Hugging Face Accelerate, DeepSpeed, or vLLM's tensor parallelism. For example, with vLLM, set
--tensor-parallel-size 2to use 2 GPUs. Update your Kubernetes resources to request 2 GPUs. - Adjust batch size and sequence length. In your inference server configuration, reduce the batch size and max sequence length. For example, in vLLM, set
--max-num-seqsand--max-model-len. In a custom script, reduce the batch size in your data loader. - Use a memory-optimized inference server. vLLM, TensorRT-LLM, and TGI (Text Generation Inference) are designed to manage GPU memory efficiently. If you're using a naïve PyTorch script, switch to one of these.
- Check for memory fragmentation. If the error occurs after a few requests, it might be fragmentation. Restart the pod and use a server that performs memory defragmentation (e.g., vLLM does this automatically). Also, set
PYTORCH_CUDA_ALLOC_CONF=expandable_segments:Truein your container's environment to reduce fragmentation. - Update Kubernetes limits. If you've increased parallelism, update
resources.limits.nvidia.com/gputo the number of GPUs you need. Also ensure your node has that many GPUs. If you're using a GPU operator, check that the device plugin is running. - Monitor and iterate. After making changes, redeploy and check logs again. Use
kubectl rollout statusand thenkubectl logsto confirm the error is gone.
Common Mistakes
- Setting GPU limits but not requests: Kubernetes only schedules based on requests. If requests are 0, the pod may be scheduled on a node with no GPUs, leading to a different error.
- Assuming the model fits in VRAM based on parameter count alone: You must account for activations, gradients (if training), and framework overhead. A 13B model in FP16 needs ~26GB for weights, but with a 2048-token sequence, you might need 30GB+.
- Using a single GPU for a model that requires more VRAM than available, instead of using model parallelism.
- Ignoring CPU memory: Sometimes the OOM is on CPU (system RAM) when loading the model. Check
kubectl describe podfor that.
Tips
- Use
nvidia-smiinside the pod to see real-time VRAM usage. You can also usewatch -n 1 nvidia-smito monitor over time. - For vLLM, set
--gpu-memory-utilization 0.9to use 90% of VRAM, leaving headroom for fragmentation. - If you're using PyTorch, call
torch.cuda.empty_cache()after each batch to free unused memory, but note this can slow down performance. - Consider using a GPU with more VRAM (e.g., A100 80GB vs 40GB) if your workload consistently OOMs.
- For training, use gradient accumulation to reduce batch size without reducing effective batch size.
Troubleshooting
If the error persists after following the steps:
- Check if the pod is actually using the GPU. Run
kubectl exec. If it shows no processes, your code might not be using CUDA. Ensure your container has the CUDA runtime and your code calls-- nvidia-smi .cuda()or uses a library that does. - Look at the full error message. Sometimes it's not a GPU OOM but a CPU OOM during model loading. Check the logs for "RuntimeError: CUDA out of memory" vs "Killed" (which indicates CPU OOM).
- Test outside Kubernetes. Run the same workload in a standalone Docker container with the same GPU to see if the error is specific to Kubernetes (e.g., due to resource limits or device plugin issues).
- Check the GPU operator. If you're using the NVIDIA GPU Operator, ensure the driver and device plugin are healthy. Run
kubectl get pods -n gpu-operator. - Try a different node. If one node has a faulty GPU, you might get OOM errors. Use
kubectl cordonandkubectl drainto move the pod to another node.
Frequently Asked Questions
Why does my 7B model OOM on a 16GB GPU?
FP16 weights take ~14GB, but you also need memory for activations, KV cache, and framework overhead. With a long sequence or large batch, you can easily exceed 16GB. Use quantization or reduce batch size.
Can I set a memory limit for the GPU in Kubernetes?
No, Kubernetes only supports requesting GPUs as a resource (e.g., nvidia.com/gpu: 1). You cannot specify VRAM amount. You control memory usage via your application settings.
What is the difference between OOMKilled and CUDA out of memory?
OOMKilled is a Kubernetes event that occurs when the container exceeds its memory limit (CPU RAM). CUDA out of memory is a runtime error from the GPU driver. Both can happen in LLM workloads, but they require different fixes.
Does vLLM handle OOM better than plain PyTorch?
Yes, vLLM uses PagedAttention to manage KV cache memory efficiently and can handle memory fragmentation better. It also has a built-in scheduler that prevents OOM by rejecting requests when memory is full.
How do I know if I need model parallelism?
If your model's weights alone exceed the GPU's VRAM, you need parallelism. For example, a 13B model in FP16 needs ~26GB, so you need at least 2x16GB GPUs with tensor parallelism.
Can I increase GPU memory by setting an environment variable?
No, VRAM is physical. However, setting PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True can reduce fragmentation and allow you to use more of the available VRAM.