invoke-ai/InvokeAI · error · NotAMatchError

unable to determine cross attention dimension: {e}

Error message

unable to determine cross attention dimension: {e}

What it means

_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.

Source

Thrown at invokeai/backend/model_manager/configs/ip_adapter.py:81

    def _validate_has_weights_file(cls, mod: ModelOnDisk) -> None:
        weights_file = mod.path / "ip_adapter.bin"
        if not weights_file.exists():
            raise NotAMatchError("missing ip_adapter.bin weights file")

    @classmethod
    def _validate_has_image_encoder_metadata_file(cls, mod: ModelOnDisk) -> None:
        image_encoder_metadata_file = mod.path / "image_encoder.txt"
        if not image_encoder_metadata_file.exists():
            raise NotAMatchError("missing image_encoder.txt metadata file")

    @classmethod
    def _get_base_or_raise(cls, mod: ModelOnDisk) -> BaseModelType:
        state_dict = mod.load_state_dict()

        try:
            cross_attention_dim = state_dict["ip_adapter"]["1.to_k_ip.weight"].shape[-1]
        except Exception as e:
            raise NotAMatchError(f"unable to determine cross attention dimension: {e}") from e

        match cross_attention_dim:
            case 768:
                return BaseModelType.StableDiffusion1
            case 1024:
                return BaseModelType.StableDiffusion2
            case 2048:
                return BaseModelType.StableDiffusionXL
            case _:
                raise NotAMatchError(f"unrecognized cross attention dimension {cross_attention_dim}")


class IPAdapter_InvokeAI_SD1_Config(IPAdapter_InvokeAI_Config_Base, Config_Base):
    base: Literal[BaseModelType.StableDiffusion1] = Field(default=BaseModelType.StableDiffusion1)


class IPAdapter_InvokeAI_SD2_Config(IPAdapter_InvokeAI_Config_Base, Config_Base):
    base: Literal[BaseModelType.StableDiffusion2] = Field(default=BaseModelType.StableDiffusion2)

View on GitHub (pinned to 0b6a024f2f)

Solutions

  1. Print the state dict keys and confirm `ip_adapter` and `1.to_k_ip.weight` exist with the expected structure.
  2. Re-download the IP-Adapter file if the state dict is truncated/corrupt.
  3. Use an InvokeAI version that supports your adapter variant (SDXL/Plus/etc. have different key layouts and dedicated config classes).
  4. If keys were renamed by a converter, remap them to the diffusers IP-Adapter naming before registration.

Example fix

// before: probing an SDXL adapter with the SD1/SD2 key layout assumption
base = cls._get_base_or_raise(mod)  # KeyError on '1.to_k_ip.weight'
// 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
Defensive patterns

Strategy: try-catch

Validate before calling

sd = mod.load_state_dict()
assert "ip_adapter" in sd and "1.to_k_ip.weight" in sd["ip_adapter"], "state dict lacks expected IP-Adapter key layout"
dim = sd["ip_adapter"]["1.to_k_ip.weight"].shape[-1]
assert dim in (768, 1024), f"unsupported cross_attention_dim: {dim}"

Type guard

def is_sd15_or_sd2_ip_adapter(sd: dict) -> bool:
    try:
        dim = sd["ip_adapter"]["1.to_k_ip.weight"].shape[-1]
        return dim in (768, 1024)
    except (KeyError, TypeError, AttributeError, IndexError):
        return False

Try / catch

try:
    record = from_model_on_disk(mod)
except NotAMatchError as e:
    if "unable to determine cross attention dimension" in str(e):
        logger.warning("state dict layout not recognized for IP-Adapter probing: %s", e)

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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