PYTORCH_CUDA_ALLOC_CONF and VRAM Optimization
Fix CUDA OOM with PYTORCH_CUDA_ALLOC_CONF, optimize FLUX training with blocks_to_swap, and run Wan2.1 and HunyuanVideo on 12 GB GPUs.
On this page
TL;DR
- Add
PYTORCH_CUDA_ALLOC_CONF=garbage_collection_threshold:0.9,max_split_size_mb:512towebui-user.batto eliminate most CUDA OOM errors. - Train FLUX LoRAs on 8–12 GB GPUs with
--blocks_to_swap 35 --fp8_baseand T5XXL fp8 checkpoints; expect ~4 GB at 512², ~6 GB at 768², ~7.5 GB at 1024². - Run Wan2.1-T2V-1.3B (8.19 GB) and HunyuanVideo FP8 (13 B params) on 12 GB cards; enable ComfyUI
--enable-dynamic-vramfor automatic offloading.
PYTORCH_CUDA_ALLOC_CONF and VRAM Optimization for FLUX, Wan2.1, and HunyuanVideo
The PYTORCH_CUDA_ALLOC_CONF environment variable controls PyTorch's CUDA memory allocator through garbage_collection_threshold and max_split_size_mb. Community guides for Stable Diffusion WebUI recommend garbage_collection_threshold:0.9,max_split_size_mb:512, but this variable does not appear in the official PyTorch CUDA semantics documentation (source). Treat this as a community-validated workaround, not a supported API.
Modern diffusion and video models expose model-specific levers: block swapping for FLUX, FP8 quantization for Wan2.1 and HunyuanVideo, and dynamic VRAM modes in ComfyUI. This article consolidates verified numbers, flags, and mechanisms so you can match a model to your GPU without trial-and-error OOM crashes.
Frequent Questions
| Question | Quick Answer |
|---|---|
| How much VRAM does FLUX.1 need for training? | ~4 GB at 512², ~6 GB at 768², ~7.5 GB at 1024² (RTX 3060 12 GB measurements, Civitai #9487) |
What does PYTORCH_CUDA_ALLOC_CONF actually do? | Configures PyTorch's CUDA allocator: garbage_collection_threshold triggers pool sweeps, max_split_size_mb caps allocation block size |
| Does ComfyUI support dynamic VRAM? | Yes—--enable-dynamic-vram turns it on, --disable-dynamic-vram turns it off; fallbacks: highvram, gpu_only, novram, cpu (cli_args.py) |
| Can Wan2.1 run on an RTX 3060 12 GB? | Yes—the T2V-1.3B model needs 8.19 GB VRAM per its model card (Wan2.1 model card) |
What does blocks_to_swap do in FLUX training? | Moves transformer blocks between CPU RAM and GPU VRAM; up to 35 blocks swappable, trading VRAM for iteration time (Kohya docs) |
Model-Specific VRAM Requirements
| Model | Task | VRAM Required | Key Optimization | Source |
|---|---|---|---|---|
| FLUX.1-dev | Training 512×512 | ~4 GB | --blocks_to_swap | Kohya sd-scripts |
| FLUX.1-dev | Training 768×768 | ~6 GB | --fp8_base | Kohya sd-scripts |
| FLUX.1-dev | Training 1024×1024 | ~7.5 GB | --fp8_base + T5XXL fp8 | Kohya sd-scripts |
| Wan2.1-T2V-1.3B | Inference | 8.19 GB | Wan-VAE latent compression | Hugging Face |
| HunyuanVideo | Inference (FP8) | 13 B+ params → ~6.5 GB weights | run_sample_video_fp8.sh | Tencent/HunyuanVideo |
Note: The FLUX numbers come from a single community benchmark on an RTX 3060 12 GB (Civitai #9487). They are widely cited but not independently reproduced. Keep ≥0.5 GB headroom above the listed values.
Technical Mechanisms
Why blocks_to_swap Reduces VRAM in FLUX Training
--blocks_to_swap offloads transformer blocks from GPU VRAM to system RAM. Each swapped block saves VRAM proportional to its activation size but adds CPU↔GPU transfer latency. FLUX.1 exposes up to 35 swappable blocks; Kohya docs state: "Higher numbers save more VRAM but reduce training speed" (flux_train_network.md).
Why Wan-VAE Cuts Video VRAM
Wan-VAE uses a 3D causal convolution (CausalConv3D) to compress pixel-space video into a latent grid with ratios 4× (temporal) / 8× (spatial) / 16× (channel). This enables unlimited-length 1080p generation without scaling VRAM linearly with clip duration (Wan2.1 model card).
Why FP8 Halves HunyuanVideo Weight Memory
FP8 (E4M3FN) stores each parameter in 8 bits instead of 16. HunyuanVideo's 13 B+ parameters drop from ~26 GB to ~13 GB weight memory. The repo ships run_sample_video_fp8.sh and fp8_optimization.py for one-command FP8 inference (Tencent/HunyuanVideo).
Why PYTORCH_CUDA_ALLOC_CONF Affects OOM
PyTorch's CUDA allocator holds freed blocks in a cache. garbage_collection_threshold sets the fraction of reserved memory that triggers a cache sweep; max_split_size_mb caps the size of a single allocation before the allocator splits it. The community-prescribed 0.9 / 512 keeps the cache lean and prevents large monolithic allocations that fragment VRAM. Not documented in official PyTorch CUDA notes (source).
Common Errors and Fixes
| Symptom | Cause (verified) | Fix |
|---|---|---|
| CUDA OOM during Stable Diffusion WebUI training | PyTorch CUDA allocator retains freed blocks | Add set PYTORCH_CUDA_ALLOC_CONF=garbage_collection_threshold:0.9,max_split_size_mb:512 to webui-user.bat (community tip, Civitai #194) |
| VRAM not released after WebUI generation | Python GC does not return VRAM to driver immediately | Click Unload SD checkpoint to free VRAM in Settings → Actions (Civitai #194) |
| OOM during FLUX training at higher resolution/batch | Baseline model VRAM + activations exceed GPU capacity | Lower resolution, add --blocks_to_swap N, enable --fp8_base, use T5XXL fp8 checkpoints on <10 GB GPUs (Kohya docs) |
ComfyUI Dynamic VRAM Flags
| Flag | Effect |
|---|---|
--enable-dynamic-vram | Enables automatic model offloading to CPU when VRAM pressure rises |
--disable-dynamic-vram | Disables the above |
--highvram | Keeps everything in VRAM (fastest, highest VRAM) |
--gpu_only | No CPU offload, no model slicing |
--novram | Aggressive offload; minimal VRAM, slowest |
--cpu | Runs entirely on CPU |
Source: comfy/cli_args.py in ComfyUI repo (GitHub).
FLUX Training: Practical Flag Combinations
| GPU VRAM | Resolution | Recommended Flags |
|---|---|---|
| 8 GB | 512² | --blocks_to_swap 35 --fp8_base + T5XXL fp8 |
| 10–12 GB | 768² | --blocks_to_swap 20-25 --fp8_base |
| 12–16 GB | 1024² | --blocks_to_swap 10-15 --fp8_base |
| 24 GB+ | 1024² | --fp8_base (swap optional) |
Download T5XXL fp8: t5xxl_fp8_e4m3fn.safetensors from comfyanonymous/flux_text_encoders (Hugging Face).
Gaps and Unverified Claims
PYTORCH_CUDA_ALLOC_CONFofficial docs — Not found on PyTorch CUDA notes page; may exist only in source or Docker docs.- FLUX 768² / 1024² VRAM — Single-source community benchmark; no independent replication found.
- VRAM Estimator plugin accuracy — Not independently validated.
- HunyuanVideo single-GPU VRAM — README covers multi-GPU and FP8 but not a single-GPU baseline.
- FLUX.1-dev exact file size — Model card confirms gated safetensors; byte count not sourced.
- SageAttention benchmark details — README claims 2–5× speedup with accuracy parity; full tables not read.
FAQ
How much VRAM does FLUX.1-dev need for inference? Inference VRAM is lower than training. Community reports ~10–12 GB for fp16/bf16 at 1024² with ComfyUI dynamic VRAM off; GGUF quantizations from city96/FLUX.1-dev-gguf drop this to 6–8 GB (Hugging Face).
What is PYTORCH_CUDA_ALLOC_CONF and how do I set it? An environment variable read by PyTorch's CUDA allocator at process start. On Windows: set PYTORCH_CUDA_ALLOC_CONF=garbage_collection_threshold:0.9,max_split_size_mb:512 in webui-user.bat. On Linux/macOS: export PYTORCH_CUDA_ALLOC_CONF=garbage_collection_threshold:0.9,max_split_size_mb:512 in your shell rc or launch script.
Does ComfyUI support dynamic VRAM allocation? Yes. Launch with --enable-dynamic-vram to allow automatic model offloading when VRAM is tight. Complementary flags (--highvram, --gpu_only, --novram, --cpu) select static strategies (ComfyUI cli_args.py).
Can Wan2.1 run on an RTX 3060 12 GB? Yes. The T2V-1.3B model requires 8.19 GB VRAM per its model card, leaving headroom on a 12 GB card (Wan2.1 model card).
What does blocks_to_swap do in FLUX LoRA training? It moves up to 35 transformer blocks between CPU RAM and GPU VRAM per iteration. More blocks swapped = lower peak VRAM, longer step time. Start at 35 for 8 GB cards, reduce as VRAM allows (Kohya flux_train_network.md).
Where do I get T5XXL fp8 checkpoints for low-VRAM FLUX training? comfyanonymous/flux_text_encoders hosts t5xxl_fp8_e4m3fn.safetensors. Load via ComfyUI's DualClipLoader node (Hugging Face).
How does SageAttention compare to FlashAttention? SageAttention's quantized attention kernel claims 2–5× speedup over FlashAttention with no end-to-end metric loss across language, image, and video models (SageAttention GitHub). Integration requires a custom node or patched attention backend.
Key Takeaways
- Allocator config works but isn't documented —
PYTORCH_CUDA_ALLOC_CONFis a community-standard fix for OOM with no official PyTorch reference. - FLUX training fits on 8–12 GB — Combine
--blocks_to_swap,--fp8_base, and T5XXL fp8 to hit ~4 GB at 512². - Video models are VRAM-efficient by design — Wan-VAE's 4/8/16 compression and HunyuanVideo's FP8 weights target consumer GPUs.
- ComfyUI dynamic VRAM is a first-class feature — Toggle it via CLI; no code changes needed.
- Community benchmarks fill documentation gaps — Treat single-source numbers as starting points, not guarantees.
Sources
- PyTorch CUDA semantics notes — official CUDA documentation; does not document
PYTORCH_CUDA_ALLOC_CONF - ComfyUI cli_args.py — ComfyUI dynamic VRAM flag definitions
- Kohya sd-scripts flux_train_network.md — FLUX training flags:
--blocks_to_swap,--fp8_base, T5XXL fp8 - Wan2.1-T2V-1.3B model card — 8.19 GB VRAM requirement, Wan-VAE compression ratios
- Tencent/HunyuanVideo — FP8 weights release, 13 B+ parameters, multi-GPU sequence parallel
- SageAttention — 2–5× speedup claim over FlashAttention
- Civitai #194 — PYTORCH_CUDA_ALLOC_CONF config tip, VRAM unload button
- Civitai #9487 — FLUX training VRAM measurements at 512²/768²/1024²
- FLUX.1-dev-gguf — GGUF quantized FLUX inference
- flux_text_encoders — T5XXL fp8 checkpoints