Comfy AIMDO
Comfy AIMDO (AI Model Dynamic Offloader) is a core utility for ComfyUI, acting as a PyTorch VRAM allocator. It implements on-demand offloading of model weights when the primary PyTorch VRAM allocator comes under pressure, aiming to prevent out-of-memory errors and optimize GPU memory usage for AI models. It is currently at version 0.2.12 and is actively maintained as an integral part of the ComfyUI ecosystem.
Warnings
- gotcha After updating ComfyUI, users frequently encounter `ModuleNotFoundError: No module named 'comfy_aimdo'`. This often happens because ComfyUI's update mechanism might not automatically update or install all its core dependencies in the virtual environment.
- breaking Dynamic VRAM support via Comfy AIMDO requires PyTorch version 2.8 or later. Using `--enable-dynamic-vram` with older PyTorch versions (e.g., < 2.8) will result in a fallback to legacy `ModelPatcher`, leading to potentially unreliable VRAM estimates and reduced performance benefits, especially on Windows.
- gotcha Some users have reported 'Torch Dynamo recompilation issues' or warnings from PyTorch when `comfy-aimdo` is active, leading to temporary disabling of AIMDO for troubleshooting. These issues can be subtle and might relate to specific hardware or PyTorch configurations.
Install
-
pip install comfy-aimdo -
cd ComfyUI_DIRECTORY virtual_env_path\python.exe -m pip install comfy-aimdo
Imports
- control
import comfy_aimdo.control
Quickstart
# 1. Ensure Comfy AIMDO is installed (it's often a dependency of ComfyUI).
# pip install comfy-aimdo
# 2. Launch ComfyUI with dynamic VRAM management enabled.
# Comfy AIMDO operates as a backend, and its functionality is typically activated
# by launching ComfyUI with specific flags, rather than direct Python API calls.
# This example assumes you have ComfyUI installed and its 'main.py' is runnable.
import subprocess
import os
# Path to your ComfyUI directory
comfyui_path = os.environ.get('COMFYUI_PATH', './ComfyUI')
# Command to launch ComfyUI with dynamic VRAM (AIMDO) enabled
# The --enable-dynamic-vram flag tells ComfyUI to utilize comfy-aimdo if detected
command = [
os.path.join(comfyui_path, 'python_env/python.exe') if os.name == 'nt' else os.path.join(comfyui_path, 'venv/bin/python'),
os.path.join(comfyui_path, 'main.py'),
'--enable-dynamic-vram',
'--listen' # Optional: listen on all interfaces
]
print(f"Launching ComfyUI with AIMDO: {' '.join(command)}")
try:
# This will block until ComfyUI exits. For non-blocking, use Popen without .wait()
process = subprocess.run(command, check=True, text=True, capture_output=True)
print("ComfyUI output:")
print(process.stdout)
if process.stderr:
print("ComfyUI errors (if any):")
print(process.stderr)
except FileNotFoundError:
print(f"Error: ComfyUI or its Python environment not found at {comfyui_path}.")
print("Please ensure COMFYUI_PATH environment variable is set correctly or adjust the 'comfyui_path' variable.")
except subprocess.CalledProcessError as e:
print(f"Error launching ComfyUI: {e}")
print("Stderr:")
print(e.stderr)
print("Stdout:")
print(e.stdout)
except Exception as e:
print(f"An unexpected error occurred: {e}")
print("ComfyUI launched (or attempted to launch). Check your browser at http://127.0.0.1:8188 (or specified --listen address).")