{"record":{"id":"2620f9505210ce80","repo":"invoke-ai/InvokeAI","slug":"checkpoint-contains-len-load-result-unexpected-ke","errorCode":null,"errorMessage":"Checkpoint contains {len(load_result.unexpected_keys)} unexpected keys. This may indicate a corrupted or incompatible checkpoint. First 5 unexpected keys: {load_result.unexpected_keys[:5]}","messagePattern":"Checkpoint contains (.+?) unexpected keys\\. This may indicate a corrupted or incompatible checkpoint\\. First 5 unexpected keys: (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"invokeai/backend/model_manager/load/model_loaders/anima.py","lineNumber":179,"sourceCode":"        with accelerate.init_empty_weights():\n            model = AnimaTransformer(**ANIMA_TRANSFORMER_CONFIG)\n\n        # Determine safe dtype\n        target_device = TorchDevice.choose_torch_device()\n        model_dtype = TorchDevice.choose_anima_inference_dtype(target_device)\n\n        # Handle memory management\n        new_sd_size = sum(ten.nelement() * model_dtype.itemsize for ten in sd.values())\n        self._ram_cache.make_room(new_sd_size)\n\n        # Convert to target dtype (skip non-float tensors like embedding indices)\n        for k in sd.keys():\n            if sd[k].is_floating_point():\n                sd[k] = sd[k].to(model_dtype)\n\n        load_result = model.load_state_dict(sd, assign=True, strict=False)\n        if load_result.unexpected_keys:\n            raise RuntimeError(\n                f\"Checkpoint contains {len(load_result.unexpected_keys)} unexpected keys. \"\n                f\"This may indicate a corrupted or incompatible checkpoint. \"\n                f\"First 5 unexpected keys: {load_result.unexpected_keys[:5]}\"\n            )\n        if load_result.missing_keys:\n            logger.warning(\n                f\"Checkpoint is missing {len(load_result.missing_keys)} keys \"\n                f\"(expected for inv_freq buffers). First 5: {load_result.missing_keys[:5]}\"\n            )\n\n        # Without this the `fp8_storage` toggle is shown for Anima models but does nothing. The\n        # state dict was cast to a single `model_dtype` above, so the layerwise cast has one\n        # unambiguous compute dtype to restore to. AnimaTransformer is a plain nn.Module, so this\n        # takes the hook-based path in `_apply_fp8_to_nn_module`.\n        model = self._apply_fp8_layerwise_casting(model, config, SubModelType.Transformer)\n        return model\n\n","sourceCodeStart":161,"sourceCodeEnd":197,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/backend/model_manager/load/model_loaders/anima.py#L161-L197","documentation":"When loading an Anima model from a single-file checkpoint, the loader calls model.load_state_dict(sd, assign=True, strict=False) and inspects load_result. Any unexpected keys — weights in the file that do not correspond to any parameter in the constructed model — indicate the checkpoint does not match the expected Anima architecture, so the loader raises RuntimeError instead of silently dropping weights.","triggerScenarios":"_load_from_singlefile is given a single-file checkpoint whose key names/prefixes differ from the instantiated Anima model (wrong variant, renamed layers, or an entirely different architecture saved in a compatible-looking file).","commonSituations":"Downloading a renamed or community-modified Anima checkpoint; using a checkpoint from a different model family saved as single-file; a corrupted or partially updated checkpoint.","solutions":["Re-download the official Anima single-file checkpoint from the original source.","Confirm the checkpoint is actually Anima and the right revision; if it is another family, import it under the correct model type.","Inspect the printed unexpected keys and strip/rename them if the checkpoint is a known-compatible variant."],"exampleFix":"// before: trusting any .safetensors as Anima\nmodel = loader._load_from_singlefile(path, dtype)\n// after: sanity-check keys against expected names first\nfrom safetensors import safe_open\nwith safe_open(path, framework=\"pt\") as f:\n    keys = list(f.keys())\nif not any(k.startswith(\"expected_prefix\") for k in keys):\n    raise RuntimeError(\"Checkpoint does not look like an Anima single-file model\")\nmodel = loader._load_from_singlefile(path, dtype)","handlingStrategy":"validation","validationCode":"from safetensors import safe_open\nwith safe_open(checkpoint_path, framework=\"pt\") as f:\n    keys = list(f.keys())\nprint(\"first keys:\", keys[:5])  # confirm prefixes match the Anima architecture before loading","typeGuard":"def looks_like_anima_checkpoint(keys: list[str]) -> bool:\n    return any(k.startswith(\"transformer\") or k.startswith(\"model\") for k in keys)","tryCatchPattern":"try:\n    model = loader._load_from_singlefile(path, dtype)\nexcept RuntimeError as e:\n    if \"unexpected keys\" in str(e):\n        raise RuntimeError(f\"Checkpoint {path} is not a compatible Anima file; re-download it\") from e\n    raise","preventionTips":["Download single-file checkpoints only from trusted official sources","Verify file hashes/checksums after download","Do not rename keys or hand-modify checkpoints"],"tags":["checkpoint","state-dict","corrupt-checkpoint"],"backgroundTag":"checkpoint-key-mismatch","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}