{"record":{"id":"68738b4ec31d83e6","repo":"invoke-ai/InvokeAI","slug":"expected-pretrainedmodel-for-gemma-encoder-got-t-68738b","errorCode":null,"errorMessage":"Expected PreTrainedModel for Gemma encoder, got {type(gemma_encoder).__name__}.","messagePattern":"Expected PreTrainedModel for Gemma encoder, got (.+?)\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"invokeai/app/invocations/sdxl_pid_decode.py","lineNumber":143,"sourceCode":"                    shift_factor = float(getattr(config, \"shift_factor\", None) or 0.0)\n                else:\n                    scaling_factor = float(getattr(vae, \"scale_factor\", scaling_factor))\n                    shift_factor = float(getattr(vae, \"shift_factor\", shift_factor))\n            del vae_info\n            TorchDevice.empty_cache()\n        context.logger.info(\n            f\"SDXL PiD decode: latent shape={tuple(latents.shape)} (expect [B, 4, H/8, W/8]) dtype={latents.dtype} \"\n            f\"using scale={scaling_factor:.5f} shift={shift_factor:.5f}\"\n        )\n\n        # 2) Encode caption with Gemma-2.\n        gemma_text_encoder_info = context.models.load(self.gemma2_encoder.text_encoder)\n        gemma_tokenizer_info = context.models.load(self.gemma2_encoder.tokenizer)\n        with ExitStack() as stack:\n            (_, gemma_encoder) = stack.enter_context(gemma_text_encoder_info.model_on_device())\n            (_, gemma_tokenizer) = stack.enter_context(gemma_tokenizer_info.model_on_device())\n            if not isinstance(gemma_encoder, PreTrainedModel):\n                raise TypeError(f\"Expected PreTrainedModel for Gemma encoder, got {type(gemma_encoder).__name__}.\")\n            if not isinstance(gemma_tokenizer, PreTrainedTokenizerBase):\n                raise TypeError(\n                    f\"Expected PreTrainedTokenizerBase for Gemma tokenizer, got {type(gemma_tokenizer).__name__}.\"\n                )\n\n            # Encode on the encoder's intended compute device. compute_device honours cpu_only and is\n            # stable under partial loading — the first parameter may be offloaded to CPU while later\n            # modules load on CUDA, so inferring the device from the first parameter could place caption\n            # inputs on the wrong device.\n            device = gemma_text_encoder_info.compute_device\n            encode_dtype = TorchDevice.choose_bfloat16_safe_dtype(device)\n            context.util.signal_progress(\"Encoding caption with Gemma-2\")\n            caption_embs, caption_mask = encode_caption_for_pid(\n                [self.prompt],\n                tokenizer=gemma_tokenizer,\n                encoder=gemma_encoder,\n                device=device,\n                dtype=encode_dtype,","sourceCodeStart":125,"sourceCodeEnd":161,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/invocations/sdxl_pid_decode.py#L125-L161","documentation":"This invocation loads the Gemma text encoder onto the device via model_on_device() and asserts the materialized object is a HuggingFace PreTrainedModel before decoding. If the loaded model is not that type, the underlying model record was built/loaded incorrectly and decoding would fail downstream, so a TypeError is raised immediately.","triggerScenarios":"context.models.load(self.gemma2_encoder.text_encoder) resolves to a record whose loaded object is not a PreTrainedModel instance — e.g. the field points at the wrong submodel/model type, a loader returned a raw state dict, or the model class for the record is misconfigured.","commonSituations":"Hand-edited or migrated model-manager records; pointing the Gemma encoder node at a non-encoder checkpoint; a plugin/loader bug returning a wrapper object; loading with an incompatible transformers version.","solutions":["Point the Gemma2 encoder node's text_encoder field at the correct Gemma text-encoder model record and re-run.","Delete and re-download/re-import the Gemma encoder so its record and loader config are rebuilt.","Verify the transformers library version can instantiate the model as PreTrainedModel; upgrade/downgrade as needed.","If you control the loader, assert the loaded class type before returning from model_on_device."],"exampleFix":"// before\n(_, gemma_encoder) = stack.enter_context(gemma_text_encoder_info.model_on_device())\n// after\n(_, gemma_encoder) = stack.enter_context(gemma_text_encoder_info.model_on_device())\nassert isinstance(gemma_encoder, PreTrainedModel), f\"bad encoder: {type(gemma_encoder).__name__}\"","handlingStrategy":"type-guard","validationCode":"# before invoking, confirm the field resolves to a HF encoder record\ninfo = context.models.load(node.gemma2_encoder.text_encoder)\nif info.hash is None:\n    raise LookupError(\"Gemma encoder record invalid\")","typeGuard":"def is_pretrained_model(obj) -> bool:\n    from transformers import PreTrainedModel\n    return isinstance(obj, PreTrainedModel)","tryCatchPattern":"try:\n    result = invoke(context)\nexcept TypeError as e:\n    if \"Gemma encoder\" in str(e):\n        reimport_gemma_encoder()\n        retry(context)\n    else:\n        raise","preventionTips":["Pin the transformers version InvokeAI expects","Re-download Gemma models through the UI, not manual file copies","Verify model records' types after DB migrations"],"tags":["type-check","huggingface","pid-decode"],"backgroundTag":"model-type-mismatch","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}