{"record":{"id":"30c80b730c11882b","repo":"invoke-ai/InvokeAI","slug":"unable-to-determine-cross-attention-dimension-e","errorCode":null,"errorMessage":"unable to determine cross attention dimension: {e}","messagePattern":"unable to determine cross attention dimension: (.+?)","errorType":"validation","errorClass":"NotAMatchError","httpStatus":null,"severity":"error","filePath":"invokeai/backend/model_manager/configs/ip_adapter.py","lineNumber":81,"sourceCode":"    def _validate_has_weights_file(cls, mod: ModelOnDisk) -> None:\n        weights_file = mod.path / \"ip_adapter.bin\"\n        if not weights_file.exists():\n            raise NotAMatchError(\"missing ip_adapter.bin weights file\")\n\n    @classmethod\n    def _validate_has_image_encoder_metadata_file(cls, mod: ModelOnDisk) -> None:\n        image_encoder_metadata_file = mod.path / \"image_encoder.txt\"\n        if not image_encoder_metadata_file.exists():\n            raise NotAMatchError(\"missing image_encoder.txt metadata file\")\n\n    @classmethod\n    def _get_base_or_raise(cls, mod: ModelOnDisk) -> BaseModelType:\n        state_dict = mod.load_state_dict()\n\n        try:\n            cross_attention_dim = state_dict[\"ip_adapter\"][\"1.to_k_ip.weight\"].shape[-1]\n        except Exception as e:\n            raise NotAMatchError(f\"unable to determine cross attention dimension: {e}\") from e\n\n        match cross_attention_dim:\n            case 768:\n                return BaseModelType.StableDiffusion1\n            case 1024:\n                return BaseModelType.StableDiffusion2\n            case 2048:\n                return BaseModelType.StableDiffusionXL\n            case _:\n                raise NotAMatchError(f\"unrecognized cross attention dimension {cross_attention_dim}\")\n\n\nclass IPAdapter_InvokeAI_SD1_Config(IPAdapter_InvokeAI_Config_Base, Config_Base):\n    base: Literal[BaseModelType.StableDiffusion1] = Field(default=BaseModelType.StableDiffusion1)\n\n\nclass IPAdapter_InvokeAI_SD2_Config(IPAdapter_InvokeAI_Config_Base, Config_Base):\n    base: Literal[BaseModelType.StableDiffusion2] = Field(default=BaseModelType.StableDiffusion2)","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/backend/model_manager/configs/ip_adapter.py#L63-L99","documentation":"_get_base_or_raise determines the IP-Adapter's base model from the shape of state_dict[\"ip_adapter\"][\"1.to_k_ip.weight\"][-1] (the cross-attention dimension). Any exception reading that tensor (missing key, corrupted/unexpected state dict structure, non-tensor value) is wrapped as NotAMatchError. A successful read then maps 768->SD1 and 1024->SD2.","triggerScenarios":"Loading an IP-Adapter state dict that lacks the \"ip_adapter\" sub-dict or the \"1.to_k_ip.weight\" key; state dict saved with different key layout (e.g. key renaming in newer diffs or sdxl adapters with different structure); corrupted weight files that fail tensor shape access.","commonSituations":"Probing SDXL IP-Adapters or other variants whose keys don't match the SD1/SD2 layout; truncated downloads failing during load_state_dict; adapters exported with custom key names by third-party tools.","solutions":["Print the state dict keys and confirm `ip_adapter` and `1.to_k_ip.weight` exist with the expected structure.","Re-download the IP-Adapter file if the state dict is truncated/corrupt.","Use an InvokeAI version that supports your adapter variant (SDXL/Plus/etc. have different key layouts and dedicated config classes).","If keys were renamed by a converter, remap them to the diffusers IP-Adapter naming before registration."],"exampleFix":"// before: probing an SDXL adapter with the SD1/SD2 key layout assumption\nbase = cls._get_base_or_raise(mod)  # KeyError on '1.to_k_ip.weight'\n// after: use the SDXL IP-Adapter config class which reads '1.to_k_ip.weight' under the sdxl-specific state dict layout, or remap keys first","handlingStrategy":"try-catch","validationCode":"sd = mod.load_state_dict()\nassert \"ip_adapter\" in sd and \"1.to_k_ip.weight\" in sd[\"ip_adapter\"], \"state dict lacks expected IP-Adapter key layout\"\ndim = sd[\"ip_adapter\"][\"1.to_k_ip.weight\"].shape[-1]\nassert dim in (768, 1024), f\"unsupported cross_attention_dim: {dim}\"","typeGuard":"def is_sd15_or_sd2_ip_adapter(sd: dict) -> bool:\n    try:\n        dim = sd[\"ip_adapter\"][\"1.to_k_ip.weight\"].shape[-1]\n        return dim in (768, 1024)\n    except (KeyError, TypeError, AttributeError, IndexError):\n        return False","tryCatchPattern":"try:\n    record = from_model_on_disk(mod)\nexcept NotAMatchError as e:\n    if \"unable to determine cross attention dimension\" in str(e):\n        logger.warning(\"state dict layout not recognized for IP-Adapter probing: %s\", e)","preventionTips":["Confirm the adapter variant (SD1/SD2 vs SDXL/Plus) before probing","Verify download integrity; truncated files break tensor access","Keep InvokeAI updated for support of newer IP-Adapter key layouts"],"tags":["ip-adapter","state-dict","invokeai"],"backgroundTag":"state-dict-key-missing","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}