AUTOMATIC1111/stable-diffusion-webui · error · 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 from the memory-efficient cross-attention implementation in sd_hijack_optimizations.py. It splits the attention einsum into slices when the required tensor (q*k sizes times a 2.5-3x modifier) exceeds free VRAM; if more than 64 slices would still be needed, it computes the maximum feasible resolution from free memory and aborts. It is effectively an out-of-memory pre-check for the attention layer, not a CUDA OOM.

Source

Thrown at modules/sd_hijack_optimizations.py:260

        r1 = torch.zeros(q.shape[0], q.shape[1], v.shape[2], device=q.device, dtype=q.dtype)

        mem_free_total = get_available_vram()

        gb = 1024 ** 3
        tensor_size = q.shape[0] * q.shape[1] * k.shape[1] * q.element_size()
        modifier = 3 if q.element_size() == 2 else 2.5
        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')

        slice_size = q.shape[1] // steps
        for i in range(0, q.shape[1], slice_size):
            end = min(i + slice_size, q.shape[1])
            s1 = einsum('b i d, b j d -> b i j', q[:, i:end], k)

            s2 = s1.softmax(dim=-1, dtype=q.dtype)
            del s1

            r1[:, i:end] = einsum('b i j, b j d -> b i d', s2, v)
            del s2

        del q, k, v

    r1 = r1.to(dtype)

    r2 = rearrange(r1, '(b h) n d -> b n (h d)', h=h)

View on GitHub (pinned to 82a973c043)

Solutions

  1. Lower the generation resolution to the max_res suggested in the message (or below it)
  2. Enable a cheaper attention backend: launch with --xformers or set attention optimization to SDPA in Settings > Optimizations
  3. Free VRAM: restart the WebUI or use --lowvram so model weights are offloaded and more memory stays free for activations
  4. Reduce batch count/batch size so the q/k tensor shrinks
Defensive patterns

Strategy: fallback

Validate before calling

import math, torch
def estimate_max_res(modifier=2.5, elem_bytes=4):
    free = torch.cuda.mem_get_info()[0] if torch.cuda.is_available() else 32 * 1024**3
    # attention tensor ~ (res/8)^2 * (res/8) * batch * elem * 2 (q and k) -> conservative cube-root estimate
    return int(math.floor(math.sqrt(math.sqrt(free / modifier)) / 8) * 64)

Try / catch

try:
    processing.run(p)
except RuntimeError as e:
    if 'Not enough memory' in str(e):
        p.width = p.height = min(p.width, suggested_max_res)
        processing.run(p)
    else:
        raise

Prevention

When it happens

Trigger: Generating at a resolution whose attention tensor needs mem_required such that 2**ceil(log2(mem_required/mem_free_total)) > 64 — i.e. the batch-size-times-resolution product is far larger than current free GPU memory (e.g. 2048x2048 on an 8GB card with other models resident in VRAM).

Common situations: High-res txt2img or img2img on low-VRAM GPUs; VRAM already consumed by the VAE, TAESD, or a previous generation not freed; running with --use-cpu all disabled and no attention optimization (--xformers / sdp) active.

Related errors


AI-assisted analysis of AUTOMATIC1111/stable-diffusion-webui@82a973c043 (2026-08-14). Data as JSON: /api/errors/f7e9ec2db40adb69. Report an issue: GitHub.