{"record":{"id":"cff64625b5665cc9","repo":"Comfy-Org/ComfyUI","slug":"not-enough-memory-use-lower-resolution-max-appro","errorCode":null,"errorMessage":"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","messagePattern":"Not enough memory, use lower resolution \\(max approx\\. (.+?)x(.+?)\\)\\. Need: (.+?)GB free, Have:(.+?)GB free","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"critical","filePath":"comfy/ldm/modules/attention.py","lineNumber":397,"sourceCode":"    else:\n        element_size = q.element_size()\n        upcast = False\n\n    gb = 1024 ** 3\n    tensor_size = q.shape[0] * q.shape[1] * k.shape[1] * element_size\n    modifier = 3\n    mem_required = tensor_size * modifier\n    steps = 1\n\n\n    if mem_required > mem_free_total:\n        steps = 2**(math.ceil(math.log(mem_required / mem_free_total, 2)))\n        # print(f\"Expected tensor size:{tensor_size/gb:0.1f}GB, cuda free:{mem_free_cuda/gb:0.1f}GB \"\n        #      f\"torch free:{mem_free_torch/gb:0.1f} total:{mem_free_total/gb:0.1f} steps:{steps}\")\n\n    if steps > 64:\n        max_res = math.floor(math.sqrt(math.sqrt(mem_free_total / 2.5)) / 8) * 64\n        raise RuntimeError(f'Not enough memory, use lower resolution (max approx. {max_res}x{max_res}). '\n                            f'Need: {mem_required/64/gb:0.1f}GB free, Have:{mem_free_total/gb:0.1f}GB free')\n\n    if mask is not None:\n        if len(mask.shape) == 2:\n            bs = 1\n        else:\n            bs = mask.shape[0]\n        mask = mask.reshape(bs, -1, mask.shape[-2], mask.shape[-1]).expand(b, heads, -1, -1).reshape(-1, mask.shape[-2], mask.shape[-1])\n\n    # print(\"steps\", steps, mem_required, mem_free_total, modifier, q.element_size(), tensor_size)\n    first_op_done = False\n    cleared_cache = False\n    while True:\n        try:\n            slice_size = q.shape[1] // steps if (q.shape[1] % steps) == 0 else q.shape[1]\n            for i in range(0, q.shape[1], slice_size):\n                end = i + slice_size\n                if upcast:","sourceCodeStart":379,"sourceCodeEnd":415,"githubUrl":"https://github.com/Comfy-Org/ComfyUI/blob/1c6d8d45b3693bfbb32385b410d813a7fd6be216/comfy/ldm/modules/attention.py#L379-L415","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Lower the output resolution/duration so token counts shrink (the error's max_res gives the approximate ceiling)","Free VRAM: close other GPU processes, restart the session to clear fragmentation","Enable/verify model offloading so the model weights are not resident during attention","Use tiled/multi-stage workflows (e.g. upscale passes) instead of one huge attention pass"],"exampleFix":null,"handlingStrategy":"fallback","validationCode":"import torch\nfree, _ = torch.cuda.mem_get_info()\n# rough attention-matrix estimate: q_len * k_len * heads * 4 bytes * 3 (modifier)\nest = q_len * k_len * heads * 4 * 3\nif est > free * 0.9:\n    raise RuntimeError(f'target resolution needs ~{est/2**30:.1f}GB attention memory, only {free/2**30:.1f}GB free')","typeGuard":null,"tryCatchPattern":"try:\n    out = attention(q, k, v)\nexcept RuntimeError as e:\n    if 'Not enough memory, use lower resolution' in str(e):\n        # reduce resolution / token count and retry, or free memory first\n        out = attention_at_lower_resolution()\n    else:\n        raise","preventionTips":["Query free VRAM before starting large generations and cap resolution accordingly","Restart sessions before huge jobs to avoid fragmentation","Use tiled/two-pass workflows for very large outputs"],"tags":["attention","memory","vram","resolution","runtime"],"backgroundTag":null,"analyzedSha":"1c6d8d45b3693bfbb32385b410d813a7fd6be216","analyzedAt":"2026-08-14T19:37:18.893Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}