{"record":{"id":"aaab4371e4bffccc","repo":"invoke-ai/InvokeAI","slug":"expected-pretrainedmodel-for-gemma-encoder-got-t-aaab43","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/z_image_pid_decode.py","lineNumber":140,"sourceCode":"                    # FluxAutoEncoder stores the constants directly on the module.\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\"Z-Image PiD decode: latent shape={tuple(latents.shape)} dtype={latents.dtype} \"\n            f\"stats[min={latents.min().item():.3f} max={latents.max().item():.3f} \"\n            f\"mean={latents.mean().item():.3f}] using scale={scaling_factor:.4f} shift={shift_factor:.4f}\"\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\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,","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/invocations/z_image_pid_decode.py#L122-L158","documentation":"z_image_pid_decode.py loads the Gemma text encoder via context.models and asserts the object returned by model_on_device() is a transformers PreTrainedModel before encoding. If the loader yields any other type (wrapper, stub, wrong module), a TypeError is raised identifying the actual class. This guards against a corrupted or incorrectly-imported Gemma encoder being fed to the PiD decode path.","triggerScenarios":"Calling the PiD decode invocation whose gemma2_encoder submodel loads to an object that fails isinstance(gemma_encoder, PreTrainedModel) — i.e. model_on_device returns a non-transformers model object.","commonSituations":"Corrupted or partially downloaded Gemma encoder files; a Gemma text-encoder model imported with an incompatible format or converted by a tool producing a non-PreTrainedModel wrapper; transformers/InvokeAI version mismatch in model loading classes.","solutions":["Re-download or re-import the Gemma text encoder so it is a standard transformers PreTrainedModel.","Verify the gemma2_encoder submodel points at the correct model config in the model manager.","Update InvokeAI and transformers to compatible versions so loaded models resolve to PreTrainedModel.","Check the loaded model files (config.json/model weights) for corruption and repair with the model installer."],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":"info = context.models.load(gemma_encoder_key)\nwith info.model_on_device() as (_, enc):\n    if not isinstance(enc, PreTrainedModel):\n        fail_fast(enc)","typeGuard":"def is_pretrained_model(obj) -> bool:\n    from transformers import PreTrainedModel\n    return isinstance(obj, PreTrainedModel)","tryCatchPattern":"try:\n    decode(context)\nexcept TypeError as e:\n    if \"Expected PreTrainedModel for Gemma encoder\" in str(e):\n        reinstall_gemma_encoder()\n    else:\n        raise","preventionTips":["Install Gemma encoder only via the model manager so it loads as a standard transformers model.","Verify checksums after downloading to avoid corrupted encoder files.","Keep transformers and InvokeAI versions in sync."],"tags":["type-check","transformers","gemma","z-image"],"backgroundTag":"unexpected-model-type","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}