{"record":{"id":"9272f1261b0364aa","repo":"invoke-ai/InvokeAI","slug":"cannot-load-embedding-for-trigger-it-was-traine","errorCode":null,"errorMessage":"Cannot load embedding for {trigger}. It was trained on a model with token dimension {embedding.shape[0]}, but the current model has token dimension {model_embeddings.weight.data[token_id].shape[0]}.","messagePattern":"Cannot load embedding for (.+?)\\. It was trained on a model with token dimension (.+?), but the current model has token dimension (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"invokeai/backend/model_patcher.py","lineNumber":123,"sourceCode":"            with skip_torch_weight_init():\n                text_encoder.resize_token_embeddings(init_tokens_count + new_tokens_added, pad_to_multiple_of)\n            model_embeddings = text_encoder.get_input_embeddings()\n\n            for ti_name, ti in ti_list:\n                assert isinstance(ti, TextualInversionModelRaw)\n                ti_embedding = _get_ti_embedding(text_encoder.get_input_embeddings(), ti)\n\n                ti_tokens = []\n                for i in range(ti_embedding.shape[0]):\n                    embedding = ti_embedding[i]\n                    trigger = _get_trigger(ti_name, i)\n\n                    token_id = ti_tokenizer.convert_tokens_to_ids(trigger)\n                    if token_id == ti_tokenizer.unk_token_id:\n                        raise RuntimeError(f\"Unable to find token id for token '{trigger}'\")\n\n                    if model_embeddings.weight.data[token_id].shape != embedding.shape:\n                        raise ValueError(\n                            f\"Cannot load embedding for {trigger}. It was trained on a model with token dimension\"\n                            f\" {embedding.shape[0]}, but the current model has token dimension\"\n                            f\" {model_embeddings.weight.data[token_id].shape[0]}.\"\n                        )\n\n                    model_embeddings.weight.data[token_id] = embedding.to(\n                        device=TorchDevice.choose_torch_device(), dtype=text_encoder.dtype\n                    )\n                    ti_tokens.append(token_id)\n\n                if len(ti_tokens) > 1:\n                    ti_manager.pad_tokens[ti_tokens[0]] = ti_tokens[1:]\n\n            yield ti_tokenizer, ti_manager\n\n        finally:\n            if init_tokens_count and new_tokens_added:\n                text_encoder.resize_token_embeddings(init_tokens_count, pad_to_multiple_of)","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/backend/model_patcher.py#L105-L141","documentation":"apply_ti() checks that each embedding vector's dimension matches the model's token embedding width before the in-place copy. A mismatch means the textual inversion was trained against a model with a different embedding dimension, and ValueError is raised naming both dimensions.","triggerScenarios":"Applying a TI embedding whose vector length (embedding.shape[0]) differs from the target CLIP token embedding width (model_embeddings.weight.data[token_id].shape[0]) — e.g. a 768-dim SD1.5 embedding applied to an SDXL (2048-dim) text encoder or vice versa.","commonSituations":"Cross-base-model embedding usage (SD1.5 ↔ SD2.x ↔ SDXL); SD2.x's 1024-dim vs SD1.5's 768-dim embeddings; accidentally selecting the wrong embedding for the active pipeline.","solutions":["Use an embedding trained for the same base model architecture as the currently loaded checkpoint.","If only the trigger name collides, rename the embedding's trigger to avoid confusion.","Retrain or convert the embedding for your base model (dimension conversion is not automatic).","Check the embedding's base model metadata in the Model Manager before enabling it."],"exampleFix":"// before: SD1.5 embedding applied to SDXL pipeline\npipeline = InvokePipelinite(model='sdxl-base', loras=[sd15_embedding])\n// after: match embedding to base model\npipeline = InvokePipelinite(model='sdxl-base', loras=[sdxl_compatible_embedding])","handlingStrategy":"validation","validationCode":"ti_vec = ti_embedding[0]\ntoken_width = model_embeddings.weight.data[0].shape[0]\nif ti_vec.shape[0] != token_width:\n    print(f'Embedding dim {ti_vec.shape[0]} != model token dim {token_width}: wrong base model')","typeGuard":"def embedding_compatible(ti_embedding, model_embeddings) -> bool:\n    return ti_embedding.shape[-1] == model_embeddings.weight.data.shape[-1]","tryCatchPattern":"try:\n    patcher.apply_ti(...)\nexcept ValueError as e:\n    if 'Cannot load embedding' in str(e) and 'token dimension' in str(e):\n        print('Use an embedding trained for this base model architecture')\n    else:\n        raise","preventionTips":["Store/label embeddings with their base model (SD1.5=768, SD2.x=1024, SDXL=2048).","Filter enabled embeddings by base-model compatibility in the Model Manager.","Don't reuse trigger names across models trained for different architectures."],"tags":["textual-inversion","dimension-mismatch","embedding"],"backgroundTag":"tensor-dimension-mismatch","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}