{"record":{"id":"a13e6b7eaf3d0323","repo":"invoke-ai/InvokeAI","slug":"unrecognized-token-vector-length-token-vector-len","errorCode":null,"errorMessage":"unrecognized token vector length {token_vector_length}","messagePattern":"unrecognized token vector length (.+?)","errorType":"exception","errorClass":"NotAMatchError","httpStatus":null,"severity":"error","filePath":"invokeai/backend/model_manager/configs/lora.py","lineNumber":669,"sourceCode":"            raise NotAMatchError(\"model looks like an Anima LoRA, not a Stable Diffusion LoRA\")\n\n        # If we've gotten here, we assume that the model is a Stable Diffusion model\n        token_vector_length = lora_token_vector_length(state_dict)\n        if token_vector_length == 768:\n            return BaseModelType.StableDiffusion1\n        elif token_vector_length == 1024:\n            return BaseModelType.StableDiffusion2\n        elif token_vector_length == 1280:\n            return BaseModelType.StableDiffusionXL  # recognizes format at https://civitai.com/models/224641\n        elif token_vector_length == 2048:\n            return BaseModelType.StableDiffusionXL\n        # Some SDXL LoRAs (e.g. self-attention-only \"slider\" LoRAs) target only the UNet\n        # and lack the cross-attention / text-encoder keys that lora_token_vector_length()\n        # needs. Fall back to detecting SDXL from the UNet's deep transformer-block structure.\n        elif _state_dict_looks_like_sdxl_unet_lora(state_dict):\n            return BaseModelType.StableDiffusionXL\n        else:\n            raise NotAMatchError(f\"unrecognized token vector length {token_vector_length}\")\n\n\nclass LoRA_LyCORIS_SD1_Config(LoRA_LyCORIS_Config_Base, Config_Base):\n    base: Literal[BaseModelType.StableDiffusion1] = Field(default=BaseModelType.StableDiffusion1)\n\n\nclass LoRA_LyCORIS_SD2_Config(LoRA_LyCORIS_Config_Base, Config_Base):\n    base: Literal[BaseModelType.StableDiffusion2] = Field(default=BaseModelType.StableDiffusion2)\n\n\nclass LoRA_LyCORIS_SDXL_Config(LoRA_LyCORIS_Config_Base, Config_Base):\n    base: Literal[BaseModelType.StableDiffusionXL] = Field(default=BaseModelType.StableDiffusionXL)\n\n\nclass LoRA_LyCORIS_FLUX_Config(LoRA_LyCORIS_Config_Base, Config_Base):\n    base: Literal[BaseModelType.Flux] = Field(default=BaseModelType.Flux)\n\n","sourceCodeStart":651,"sourceCodeEnd":687,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/backend/model_manager/configs/lora.py#L651-L687","documentation":"NotAMatchError raised by LoRA_LyCORIS_Config_Base._get_base_or_raise when a state dict that passed the generic LyCORIS LoRA key heuristics yields a token vector length that is not 768, 1024, 1280, or 2048, and whose UNet structure does not look like an SDXL UNet LoRA. InvokeAI uses lora_token_vector_length(state_dict) to infer the base model (SD1/SD2/SDXL) from text-encoder cross-attention shapes; a length outside the known set means the library cannot determine which base-model config this LoRA belongs to, so it refuses to match.","triggerScenarios":"Scanning/importing a LoRA file where lora_token_vector_length() computes an unusual embedding size (typically None or an unexpected number) because the file lacks text-encoder/cross-attention keys (e.g. self-attention-only 'slider' LoRAs) or uses a novel architecture, and _state_dict_looks_like_sdxl_unet_lora() also fails to match.","commonSituations":"Installing a text-encoder-less SDXL slider LoRA from Civitai, a LoRA trained for an unsupported/newer base model (e.g. SD3.5, Pony variants with odd text encoders, Flux LoRAs missing the recognized Kohya/diffusers format markers), a corrupted or truncated safetensors file, or a merged checkpoint that still carries LoRA keys.","solutions":["Check the actual token vector length printed in the message: if it is a recognizable value for a different architecture (e.g. 4096 for Flux), the LoRA is for an unsupported base model and cannot be imported as an SD LoRA.","Update InvokeAI to the latest version — new base-model detectors (Flux, Z-Image, Krea-2, etc.) are added over time and may now classify this file.","If the model is a known SDXL slider/text-encoder-less LoRA, verify _state_dict_looks_like_sdxl_unet_lora should match: the state dict must contain SDXL UNet transformer-block keys; re-export/retrain with standard key naming if it does not.","Pass an explicit base/override in the model config (or use 'base_model' override fields when installing) to skip base inference.","Validate the file integrity (re-download the safetensors/checkpoint) in case truncation produced malformed tensors."],"exampleFix":"// before: installing via scan with no hints\nModelOnDisk(path) -> LoRA_LyCORIS_Config_Base.from_model_on_disk(mod, {})\n// after: provide explicit base override so _get_base_or_raise is skipped\nfrom_model_on_disk(mod, {\"base\": BaseModelType.StableDiffusionXL, \"variant\": ModelVariantType.Normal})","handlingStrategy":"validation","validationCode":"from invokeai.backend.model_manager.load.model_util import lora_token_vector_length\nfrom invokeai.backend.model_manager.configs.lora import _state_dict_looks_like_sdxl_unet_lora\n\nsd = load_file(\"model.safetensors\")\ntvl = lora_token_vector_length(sd)\nif tvl not in (768, 1024, 1280, 2048) and not _state_dict_looks_like_sdxl_unet_lora(sd):\n    print(f\"Unsupported LoRA: token vector length {tvl}, not an SDXL UNet LoRA\")","typeGuard":"from typing import Any\n\ndef is_supported_sd_lora(state_dict: dict[str, Any]) -> bool:\n    tvl = lora_token_vector_length(state_dict)\n    return tvl in (768, 1024, 1280, 2048) or _state_dict_looks_like_sdxl_unet_lora(state_dict)","tryCatchPattern":"from invokeai.backend.model_manager.configs.base import NotAMatchError\n\ntry:\n    config = LoRA_LyCORIS_Config_Base.from_model_on_disk(mod, {})\nexcept NotAMatchError as e:\n    logger.warning(\"LoRA import skipped: %s\", e)\n    config = None","preventionTips":["Prefer LoRAs that include text-encoder/cross-attention keys; self-attention-only slider LoRAs need current InvokeAI for the SDXL UNet fallback.","Keep InvokeAI updated so new base-model detectors are available at scan time.","Inspect state-dict keys with safetensors' safe_open before importing unknown files.","Use explicit base-model override fields for LoRAs that auto-detection repeatedly fails on."],"tags":["lora","model-import","base-model-detection","invokeai"],"backgroundTag":"unrecognized-model-format","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}