invoke-ai/InvokeAI · error · ValueError

You have provided {len(slice_size)}, but {self.config} has {

Error message

You have provided {len(slice_size)}, but {self.config} has {len(sliceable_head_dims)} different attention layers. Make sure to match `len(slice_size)` to be {len(sliceable_head_dims)}.

What it means

set_attention_slice validates that the provided slice_size list matches the number of sliceable attention layers in the model config. This patched copy (in InvokeAI's hotfixes, mirroring diffusers' UNet2DConditionModel) raises when the list length differs from len(sliceable_head_dims). Attention slicing splits attention computation into chunks to save VRAM, and each sliceable layer needs exactly one slice size.

Source

Thrown at invokeai/backend/util/hotfixes.py:541

        # retrieve number of attention layers
        for module in self.children():
            fn_recursive_retrieve_sliceable_dims(module)

        num_sliceable_layers = len(sliceable_head_dims)

        if slice_size == "auto":
            # half the attention head size is usually a good trade-off between
            # speed and memory
            slice_size = [dim // 2 for dim in sliceable_head_dims]
        elif slice_size == "max":
            # make smallest slice possible
            slice_size = num_sliceable_layers * [1]

        slice_size = num_sliceable_layers * [slice_size] if not isinstance(slice_size, list) else slice_size

        if len(slice_size) != len(sliceable_head_dims):
            raise ValueError(
                f"You have provided {len(slice_size)}, but {self.config} has {len(sliceable_head_dims)} different"
                f" attention layers. Make sure to match `len(slice_size)` to be {len(sliceable_head_dims)}."
            )

        for i in range(len(slice_size)):
            size = slice_size[i]
            dim = sliceable_head_dims[i]
            if size is not None and size > dim:
                raise ValueError(f"size {size} has to be smaller or equal to {dim}.")

        # Recursively walk through all the children.
        # Any children which exposes the set_attention_slice method
        # gets the message
        def fn_recursive_set_attention_slice(module: torch.nn.Module, slice_size: List[int]):
            if hasattr(module, "set_attention_slice"):
                module.set_attention_slice(slice_size.pop())

            for child in module.children():

View on GitHub (pinned to 0b6a024f2f)

Solutions

  1. Call set_attention_slice('auto') (or pass a single int) so the code computes num_sliceable_layers * [slice_size] for you instead of supplying a hand-built list
  2. Count the attention layers (len(sliceable_head_dims) from the model config) and supply a list of exactly that length
  3. Disable attention slicing entirely if you don't need the VRAM savings

Example fix

// before
model.set_attention_slice([64])
// after
model.set_attention_slice("auto")  # or an int: model.set_attention_slice(64)
Defensive patterns

Strategy: validation

Validate before calling

n_layers = len(model.config.attention_head_dim) if hasattr(model.config, 'attention_head_dim') else None
if isinstance(slice_size, list) and n_layers is not None and len(slice_size) != n_layers:
    slice_size = 'auto'  # or fix the list length before calling

Type guard

def is_valid_slice_list(slice_size, n_layers):
    return not isinstance(slice_size, list) or len(slice_size) == n_layers

Try / catch

try:
    model.set_attention_slice(slice_size)
except ValueError as e:
    logger.warning("bad slice_size, falling back to auto: %s", e)
    model.set_attention_slice("auto")

Prevention

When it happens

Trigger: Calling set_attention_slice with a list whose length does not equal the number of attention layers reported by the model config; e.g. passing [2] (a single int wrapped or scalar) when the UNet has 16 attention layers, or passing a stale list from a differently-shaped model.

Common situations: Enabling attention slicing ('--attention_slice_size' style options or enable_attention_slicing) with a hand-specified list on a model whose architecture differs; copying slice_size config between models (SD 1.5 vs SDXL); upgrading diffusers/InvokeAI so layer counts changed.

Related errors


AI-assisted analysis of invoke-ai/InvokeAI@0b6a024f2f (2026-08-29). Data as JSON: /api/errors/bb1150a4600bfd2a. Report an issue: GitHub.