huggingface
memory_error
ai_generated
true
torch.cuda.OutOfMemoryError: CUDA out of memory during model.generate()
ID: huggingface/generate-oom
80%Fix Rate
88%Confidence
3Evidence
2023-01-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| any | active | — | — | — |
Root Cause
OOM during text generation. KV cache grows linearly with sequence length, consuming all VRAM.
genericWorkarounds
-
90% success Load model in lower precision
model = AutoModelForCausalLM.from_pretrained(name, torch_dtype=torch.float16, device_map='auto')
-
88% success Use quantization (4-bit or 8-bit)
model = AutoModelForCausalLM.from_pretrained(name, load_in_4bit=True, device_map='auto')
-
85% success Set reasonable max_new_tokens and use streaming
model.generate(inputs, max_new_tokens=256, do_sample=True)
Dead Ends
Common approaches that don't work:
-
Set max_new_tokens very high for better generation quality
78% fail
Longer sequences need quadratically more KV cache memory. Set reasonable limits.
-
Call torch.cuda.empty_cache() between generate calls
65% fail
Does not free the KV cache allocated during generate; only helps if there is fragmented cached memory