{"record":{"id":"d2d577379d0a9d1b","repo":"invoke-ai/InvokeAI","slug":"size-size-has-to-be-smaller-or-equal-to-dim","errorCode":null,"errorMessage":"size {size} has to be smaller or equal to {dim}.","messagePattern":"size (.+?) has to be smaller or equal to (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"invokeai/backend/util/hotfixes.py","lineNumber":550,"sourceCode":"            # 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():\n                fn_recursive_set_attention_slice(child, slice_size)\n\n        reversed_slice_size = list(reversed(slice_size))\n        for module in self.children():\n            fn_recursive_set_attention_slice(module, reversed_slice_size)\n\n    def _set_gradient_checkpointing(self, module, value=False):\n        if isinstance(module, (CrossAttnDownBlock2D, DownBlock2D)):\n            module.gradient_checkpointing = value","sourceCodeStart":532,"sourceCodeEnd":568,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/backend/util/hotfixes.py#L532-L568","documentation":"Each per-layer attention slice size must not exceed that layer's head dimension (sliceable_head_dims[i]). This guard rejects any slice entry larger than the corresponding layer's dim, since slicing above the dimension size is meaningless and would misconfigure chunked attention.","triggerScenarios":"Calling set_attention_slice with a list containing an entry greater than the matching layer's dim, e.g. set_attention_slice([128]) on a layer with dim 64, or reusing a slice list tuned for a different model.","commonSituations":"Copy-pasting slice sizes between SD 1.x/2.x/SDXL models whose attention head dims differ; hand-tuning VRAM-saving slice values; config drift after model or library upgrades.","solutions":["Lower each slice_size entry so it is <= the corresponding layer's dim","Use 'auto' or a small power-of-two int (e.g. 2, 4, 8) instead of a custom list","Verify sliceable_head_dims in the model config and size the list to it"],"exampleFix":"// before\nmodel.set_attention_slice([128, 128])\n// after\nmodel.set_attention_slice([64, 64])  # each <= matching layer dim, or use \"auto\"","handlingStrategy":"validation","validationCode":"dims = model.config.attention_head_dim  # list of per-layer dims\nif isinstance(slice_size, list):\n    slice_size = [min(s, d) if s is not None else None for s, d in zip(slice_size, dims)]","typeGuard":"def slice_sizes_in_range(slice_size, dims):\n    return all(s is None or s <= d for s, d in zip(slice_size, dims))","tryCatchPattern":"try:\n    model.set_attention_slice(slice_size)\nexcept ValueError:\n    model.set_attention_slice(\"auto\")","preventionTips":["Clamp each slice entry to its layer dim before applying","Use 'auto' slicing unless you have profiled per-layer dims","Test new slice configs on the target checkpoint before production use"],"tags":["valueerror","attention-slicing","out-of-range"],"backgroundTag":"attention-slice-size-mismatch","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}