Fix Python 3.12 VRAM spikes with CUDNN benchmark

Disables torch.backends.cudnn.benchmark on Python 3.12 to prevent
severe VRAM allocation spikes that occur during model operations.

The CUDNN benchmarking feature, introduced in v0.3.57 (commit e2d1e5da),
tests multiple convolution algorithms and allocates temporary VRAM.
This interacts poorly with Python 3.12's garbage collection behavior,
causing multi-GB VRAM spikes before and after model inference.

Solution:
- Preserves CUDNN benchmarking performance benefit on other Python versions
- Only disables the problematic behavior on Python 3.12
- Maintains full functionality while fixing memory management issues
- No impact on users not using --fast autotune flag

Tested with TTS model wrappers that reproduce the issue consistently
on Python 3.12 with ComfyUI v0.3.57+.

Fixes: VRAM spikes in Python 3.12 environments
Related: ComfyUI v0.3.57 regression affecting model memory management
This commit is contained in:
User 2025-09-26 00:53:47 -03:00
parent ce4cb2389c
commit 1ce987cf52

View File

@ -53,6 +53,9 @@ except (ModuleNotFoundError, TypeError):
cast_to = comfy.model_management.cast_to #TODO: remove once no more references cast_to = comfy.model_management.cast_to #TODO: remove once no more references
if torch.cuda.is_available() and torch.backends.cudnn.is_available() and PerformanceFeature.AutoTune in args.fast: if torch.cuda.is_available() and torch.backends.cudnn.is_available() and PerformanceFeature.AutoTune in args.fast:
import sys
# Skip CUDNN benchmark on Python 3.12 due to VRAM allocation issues with model wrappers
if sys.version_info[:2] != (3, 12):
torch.backends.cudnn.benchmark = True torch.backends.cudnn.benchmark = True
def cast_to_input(weight, input, non_blocking=False, copy=True): def cast_to_input(weight, input, non_blocking=False, copy=True):