Comfy-Org/ComfyUI · critical · RuntimeError

Not enough memory, use lower resolution (max approx. {max_re

Error message

Not enough memory, use lower resolution (max approx. {max_res}x{max_res}). Need: {mem_required/64/gb:0.1f}GB free, Have:{mem_free_total/gb:0.1f}GB free

What it means

Raised inside the optimized attention path when the intermediate attention matrix would not fit in (free CUDA + free torch) memory even after splitting into the maximum of 64 steps. The code computes a per-step memory need (tensor_size * 3) versus available memory; when steps > 64 it derives the maximum roughly-square resolution the current free memory supports and raises this RuntimeError with the required vs available GB figures.

Source

Thrown at comfy/ldm/modules/attention.py:397

    else:
        element_size = q.element_size()
        upcast = False

    gb = 1024 ** 3
    tensor_size = q.shape[0] * q.shape[1] * k.shape[1] * element_size
    modifier = 3
    mem_required = tensor_size * modifier
    steps = 1


    if mem_required > mem_free_total:
        steps = 2**(math.ceil(math.log(mem_required / mem_free_total, 2)))
        # print(f"Expected tensor size:{tensor_size/gb:0.1f}GB, cuda free:{mem_free_cuda/gb:0.1f}GB "
        #      f"torch free:{mem_free_torch/gb:0.1f} total:{mem_free_total/gb:0.1f} steps:{steps}")

    if steps > 64:
        max_res = math.floor(math.sqrt(math.sqrt(mem_free_total / 2.5)) / 8) * 64
        raise RuntimeError(f'Not enough memory, use lower resolution (max approx. {max_res}x{max_res}). '
                            f'Need: {mem_required/64/gb:0.1f}GB free, Have:{mem_free_total/gb:0.1f}GB free')

    if mask is not None:
        if len(mask.shape) == 2:
            bs = 1
        else:
            bs = mask.shape[0]
        mask = mask.reshape(bs, -1, mask.shape[-2], mask.shape[-1]).expand(b, heads, -1, -1).reshape(-1, mask.shape[-2], mask.shape[-1])

    # print("steps", steps, mem_required, mem_free_total, modifier, q.element_size(), tensor_size)
    first_op_done = False
    cleared_cache = False
    while True:
        try:
            slice_size = q.shape[1] // steps if (q.shape[1] % steps) == 0 else q.shape[1]
            for i in range(0, q.shape[1], slice_size):
                end = i + slice_size
                if upcast:

View on GitHub (pinned to 1c6d8d45b3)

Solutions

  1. Lower the output resolution/duration so token counts shrink (the error's max_res gives the approximate ceiling)
  2. Free VRAM: close other GPU processes, restart the session to clear fragmentation
  3. Enable/verify model offloading so the model weights are not resident during attention
  4. Use tiled/multi-stage workflows (e.g. upscale passes) instead of one huge attention pass
Defensive patterns

Strategy: fallback

Validate before calling

import torch
free, _ = torch.cuda.mem_get_info()
# rough attention-matrix estimate: q_len * k_len * heads * 4 bytes * 3 (modifier)
est = q_len * k_len * heads * 4 * 3
if est > free * 0.9:
    raise RuntimeError(f'target resolution needs ~{est/2**30:.1f}GB attention memory, only {free/2**30:.1f}GB free')

Try / catch

try:
    out = attention(q, k, v)
except RuntimeError as e:
    if 'Not enough memory, use lower resolution' in str(e):
        # reduce resolution / token count and retry, or free memory first
        out = attention_at_lower_resolution()
    else:
        raise

Prevention

When it happens

Trigger: Running attention at very large spatial resolution (huge q/k token counts, e.g. big image latents or long video) on a GPU whose free VRAM is far below what the attention matrix needs even split 64 ways. Fragmented memory after long sessions can also lower mem_free_total.

Common situations: Generating high-resolution or long-duration content on low-VRAM GPUs; switching to a model with much larger token counts (e.g. video DiT); memory fragmentation from prior runs; other processes occupying the GPU.

Related errors


AI-assisted analysis of Comfy-Org/ComfyUI@1c6d8d45b3 (2026-08-14). Data as JSON: /api/errors/cff64625b5665cc9. Report an issue: GitHub.