{"record":{"id":"4d28172293ad8ad7","repo":"lllyasviel/Fooocus","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":"error","filePath":"ldm_patched/ldm/modules/attention.py","lineNumber":223,"sourceCode":"        element_size = 4\n    else:\n        element_size = q.element_size()\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    # 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 _ATTN_PRECISION ==\"fp32\":\n                    with torch.autocast(enabled=False, device_type = 'cuda'):\n                        s1 = einsum('b i d, b j d -> b i j', q[:, i:end].float(), k.float()) * scale\n                else:\n                    s1 = einsum('b i d, b j d -> b i j', q[:, i:end], k) * scale\n\n                if mask is not None:\n                    if len(mask.shape) == 2:","sourceCodeStart":205,"sourceCodeEnd":241,"githubUrl":"https://github.com/lllyasviel/Fooocus/blob/ae05379cc97bc4361ec8b4ec90193dab21be763f/ldm_patched/ldm/modules/attention.py#L205-L241","documentation":"Raised inside the sliced attention forward (pytorch_attention) when computing attention would need more than 64 slice steps to fit in free VRAM/RAI memory. The code estimates the output tensor size (modifier 3), computes a power-of-two step count against currently free memory (cuda free + torch cached), and if steps > 64 it gives up and reports the largest approx. square resolution the free memory could support. It is a hard out-of-memory guard for cross/self-attention in the diffusion UNet, not a generic torch OOM.","triggerScenarios":"Running SDXL/SD generation or upscaling at a resolution whose attention matrices (q.shape[1] tokens x dim) exceed free GPU memory by more than 64x. Typical: 2048px+ base generation on 6-8GB GPUs, extreme hires/img2img scales, or when another process (or an unclosed previous model) holds most VRAM. Also triggered in lowvram mode where mem_free_total is small after offloading other models.","commonSituations":"Fooocus 'Performance' set to Speed with very high resolution presets; running two UIs/tabs sharing one GPU; VRAM fragmented by earlier long sessions (torch cache not freed); using --always-gpu on cards near the minimum spec; upscaling with a 4x model on top of an already large image.","solutions":["Lower the output resolution to at or below the max_res value printed in the message, then retry","Free VRAM: close other GPU processes/tabs, then restart the app (clears torch cache fragmentation)","Pick a lighter performance mode (Fooocus: Performance -> Llama/Lightning or use 'Low VRAM' style preset; CLI: --lowvram / --novram) so less memory is held by weights","Reduce upscaling multiplier or upscale in two passes instead of one huge pass","If it persists on spec hardware, update GPU drivers / use a smaller checkpoint variant"],"exampleFix":"// before\nshared.job_prepare()  // generating at 3072x3072 on 8GB -> RuntimeError: Not enough memory\n// after\n// lower resolution to the suggested max_res, e.g.:\nshared.results = process(resolution=1536)  // within printed max approx. 1536x1536","handlingStrategy":"validation","validationCode":"import torch, math\n\ndef max_safe_attention_res(q_len_hint=None):\n    gb = 1024 ** 3\n    free_cuda, _ = torch.cuda.mem_get_info()\n    free_torch = torch.cuda.memory_reserved() - torch.cuda.memory_allocated()\n    mem_free_total = free_cuda + free_torch\n    return math.floor(math.sqrt(math.sqrt(mem_free_total / 2.5)) / 8) * 64\n\nres = max_safe_attention_res()\nif my_target_res > res:\n    my_target_res = res  # clamp instead of crashing","typeGuard":null,"tryCatchPattern":"try:\n    out = model(x)\nexcept RuntimeError as e:\n    if 'Not enough memory' in str(e):\n        # message contains the max supported square resolution - parse and retry\n        import re\n        m = re.search(r'max approx\\. (\\d+)x', str(e))\n        if m:\n            target = int(m.group(1))\n            retry_with_resolution(min(target, current_res))\n        else:\n            raise\n    else:\n        raise","preventionTips":["Clamp requested resolution to the max_res the error formula implies before submitting the job","Free VRAM between jobs: torch.cuda.empty_cache() and avoid running two model-heavy tabs on one GPU","Prefer baked-in low-VRAM performance modes (--lowvram) over manual huge resolutions","Chain upscaling passes (1.5x then 1.5x) instead of one extreme pass"],"tags":["cuda","memory","attention","resolution","out-of-memory","diffusion"],"backgroundTag":null,"analyzedSha":"ae05379cc97bc4361ec8b4ec90193dab21be763f","analyzedAt":"2026-08-15T04:23:59.533Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}