COMMUNITY HUB Independent Local MoE Inference Guides, Hardware Blueprints & Benchmarks for Strata LLM
SL
STRATA LLM
Crash / Stability • Diagnostic Playbook • Updated 2026-11-25

Fixing CUDA Out-of-Memory (OOM) Crashes During Long Context Load

Resolve torch.cuda.OutOfMemoryError and sudden server exits when loading 16K–64K prompts in local MoE and dense LLMs. Covers KV cache quantization, Flash Attention, and micro-batching.

1. Observable Symptoms

  • The inference server starts cleanly and answers short 200-token prompts, but crashes with CUDA Out of Memory as soon as you paste a large file or open a multi-file IDE chat.
  • HTTP clients (Continue.dev, Cursor, Python SDK) receive HTTP 500 or sudden "Connection reset by peer" during prompt prefill.
  • nvidia-smi spikes from 9.5 GB to 11.9+ GB on a 12GB card right before the process terminates.

2. Root Causes

1. Uncompressed FP16 KV Cache Scaling Linearly with Context

At FP16 precision, a 32K context window on a 125B-class model can consume ~7.6 GB of VRAM on its own—leaving almost no room for shared attention weights, CUDA workspace, and hot MoE experts on a 12GB GPU.

2. Large Prompt Prefill Activation Spikes (Batch / uBatch Size)

Processing 8,000 input tokens in a single prefill pass allocates large temporary attention activation matrices. Without Flash Attention or a capped micro-batch size, temporary buffers easily spike 1.5–3.0 GB above steady-state usage.

3. Hidden Desktop VRAM Consumption on Windows 11

Hardware-accelerated Chrome/Edge, Discord, VS Code Electron windows, and NVIDIA ShadowPlay can reserve 1.5–2.5 GB of VRAM before your inference server even starts.

3. Step-by-Step Remediation

Step 1: Audit Existing GPU Processes Before Launch

Check how much VRAM is actually free rather than assuming a 12GB card gives you 12GB of usable space.

nvidia-smi --query-compute-apps=pid,process_name,used_memory --format=csv

Step 2: Enable Q8_0 (or Q4_0) KV Cache Quantization & Cap Context

Switching KV cache from FP16 to Q8_0 immediately cuts KV memory in half with negligible quality loss. Cap --ctx-size to match your card tier (8192 on 12GB, 16384–32768 on 16GB–24GB) and reserve 1.5 GB for OS/CUDA headroom.

strata serve --model qwen3.8-125b-q4 --ctx-size 8192 --kv-cache-type q8_0 --vram-budget 10G --port 8080

Step 3: Lower Prefill Micro-Batch Size If Prefill Still Spikes

If short turns work but pasting a 6,000-token log file still triggers an OOM, lower the prefill micro-batch size (e.g., 512 tokens) so prompt evaluation chunks through VRAM smoothly instead of allocating one massive activation tensor.

4. Verification Command & Expected Output

Send a deterministic 4,000–6,000 token prompt while watching live VRAM allocation via nvidia-smi every second to confirm peak usage stays at least 800 MB below total card VRAM.

nvidia-smi --query-gpu=memory.used,memory.total,utilization.gpu --format=csv -l 1

Expected: memory.used stabilizes around 10,200–10,900 MiB on a 12,288 MiB card throughout both prompt prefill and token decoding with zero allocation errors.

Context Windows & KV Cache Memory Math → MoE VRAM & RAM Calculator → Fixing CUDA OOM Errors Playbook →