{"record":{"id":"bb1150a4600bfd2a","repo":"invoke-ai/InvokeAI","slug":"you-have-provided-len-slice-size-but-self-con","errorCode":null,"errorMessage":"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)}.","messagePattern":"You have provided (.+?), but (.+?) has (.+?) different attention layers\\. Make sure to match `len\\(slice_size\\)` to be (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"invokeai/backend/util/hotfixes.py","lineNumber":541,"sourceCode":"\n        # retrieve number of attention layers\n        for module in self.children():\n            fn_recursive_retrieve_sliceable_dims(module)\n\n        num_sliceable_layers = len(sliceable_head_dims)\n\n        if slice_size == \"auto\":\n            # half the attention head size is usually a good trade-off between\n            # speed and memory\n            slice_size = [dim // 2 for dim in sliceable_head_dims]\n        elif slice_size == \"max\":\n            # make smallest slice possible\n            slice_size = num_sliceable_layers * [1]\n\n        slice_size = num_sliceable_layers * [slice_size] if not isinstance(slice_size, list) else slice_size\n\n        if len(slice_size) != len(sliceable_head_dims):\n            raise ValueError(\n                f\"You have provided {len(slice_size)}, but {self.config} has {len(sliceable_head_dims)} different\"\n                f\" attention layers. Make sure to match `len(slice_size)` to be {len(sliceable_head_dims)}.\"\n            )\n\n        for i in range(len(slice_size)):\n            size = slice_size[i]\n            dim = sliceable_head_dims[i]\n            if size is not None and size > dim:\n                raise ValueError(f\"size {size} has to be smaller or equal to {dim}.\")\n\n        # Recursively walk through all the children.\n        # Any children which exposes the set_attention_slice method\n        # gets the message\n        def fn_recursive_set_attention_slice(module: torch.nn.Module, slice_size: List[int]):\n            if hasattr(module, \"set_attention_slice\"):\n                module.set_attention_slice(slice_size.pop())\n\n            for child in module.children():","sourceCodeStart":523,"sourceCodeEnd":559,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/backend/util/hotfixes.py#L523-L559","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["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","Count the attention layers (len(sliceable_head_dims) from the model config) and supply a list of exactly that length","Disable attention slicing entirely if you don't need the VRAM savings"],"exampleFix":"// before\nmodel.set_attention_slice([64])\n// after\nmodel.set_attention_slice(\"auto\")  # or an int: model.set_attention_slice(64)","handlingStrategy":"validation","validationCode":"n_layers = len(model.config.attention_head_dim) if hasattr(model.config, 'attention_head_dim') else None\nif isinstance(slice_size, list) and n_layers is not None and len(slice_size) != n_layers:\n    slice_size = 'auto'  # or fix the list length before calling","typeGuard":"def is_valid_slice_list(slice_size, n_layers):\n    return not isinstance(slice_size, list) or len(slice_size) == n_layers","tryCatchPattern":"try:\n    model.set_attention_slice(slice_size)\nexcept ValueError as e:\n    logger.warning(\"bad slice_size, falling back to auto: %s\", e)\n    model.set_attention_slice(\"auto\")","preventionTips":["Prefer 'auto' or a single int over hand-built lists","Never reuse slice lists across different model architectures","Re-derive slice lists after model/library upgrades"],"tags":["valueerror","attention-slicing","config-mismatch"],"backgroundTag":"attention-slice-size-mismatch","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}