invoke-ai/InvokeAI · error · NotAMatchError

Gemma2 hidden_size {hidden_size} is incompatible with PiD, w

Error message

Gemma2 hidden_size {hidden_size} is incompatible with PiD, which requires {_PID_GEMMA_HIDDEN_SIZE} (Gemma-2-2b); 9B/27B variants are not supported.

What it means

NotAMatchError raised by Gemma2Encoder_Gemma2Encoder_Config.from_model_on_disk when a Gemma2 directory's config.json reports a hidden_size other than 2304. PiD's caption projection is hard-wired to Gemma-2-2b's 2304-dim hidden state, so 9B (3584) and 27B (4608) variants are rejected early instead of failing with a matrix-shape error deep inside PiD inference. During model scanning this exception is normally caught per candidate config class and just means 'not my kind of model'.

Source

Thrown at invokeai/backend/model_manager/configs/gemma2_encoder.py:77

        raise_for_override_fields(cls, override_fields)

        config_path = mod.path / "config.json"
        if not config_path.exists():
            raise NotAMatchError(f"missing config.json at {config_path}")

        # Reject full diffusers pipelines (they have model_index.json at root).
        if (mod.path / "model_index.json").exists():
            raise NotAMatchError("directory looks like a full diffusers pipeline, not a standalone Gemma2 encoder")

        # Architecture marker is the canonical signal.
        raise_for_class_name(config_path, {"Gemma2ForCausalLM"})

        # Only Gemma-2-2b (2304-dim hidden state) is compatible with PiD's fixed caption projection.
        # Reject 9B/27B variants here so they are not offered as compatible encoders and then fail with
        # a matrix-shape error deep inside PiD inference.
        hidden_size = get_config_dict_or_raise(config_path).get("hidden_size")
        if hidden_size != _PID_GEMMA_HIDDEN_SIZE:
            raise NotAMatchError(
                f"Gemma2 hidden_size {hidden_size} is incompatible with PiD, which requires "
                f"{_PID_GEMMA_HIDDEN_SIZE} (Gemma-2-2b); 9B/27B variants are not supported."
            )

        # Sanity check that tokenizer files live alongside the model (PiD calls
        # AutoTokenizer.from_pretrained on the same directory).
        if not any((mod.path / f).exists() for f in ("tokenizer.json", "tokenizer.model")):
            raise NotAMatchError("directory does not contain Gemma2 tokenizer files (tokenizer.json/tokenizer.model)")

        return cls(**override_fields)


def _read_gguf_arch_and_hidden_size(path: Path) -> tuple[str, int | None]:
    """Read (general.architecture, <arch>.embedding_length) from a GGUF file's metadata.

    Raises NotAMatchError if the file is not a readable GGUF or is missing the architecture marker.
    """
    import gguf

View on GitHub (pinned to 0b6a024f2f)

Solutions

  1. Download/point at Gemma-2-2b (hidden_size 2304), e.g. Efficient-Large-Model/gemma-2-2b-it or google/gemma-2-2b-it
  2. Check config.json hidden_size before importing: it must be 2304 for PiD use
  3. If you only need a generic Gemma2 LM (not a PiD encoder), register it under a different model type instead

Example fix

// before (config.json of wrong variant)
{ "architectures": ["Gemma2ForCausalLM"], "hidden_size": 3584 } // gemma-2-9b-it
// after
{ "architectures": ["Gemma2ForCausalLM"], "hidden_size": 2304 } // gemma-2-2b-it
Defensive patterns

Strategy: validation

Validate before calling

import json
from pathlib import Path

def is_pid_compatible_gemma2_dir(model_dir: str | Path) -> bool:
    cfg = Path(model_dir) / "config.json"
    if not cfg.exists():
        return False
    try:
        data = json.loads(cfg.read_text())
    except (json.JSONDecodeError, OSError):
        return False
    return (
        "Gemma2ForCausalLM" in (data.get("architectures") or [])
        and data.get("hidden_size") == 2304
    )

Type guard

def has_valid_gemma2_config(cfg: dict) -> bool:
    return isinstance(cfg.get("hidden_size"), int) and cfg.get("hidden_size") == 2304

Try / catch

from invokeai.backend.model_manager.configs.identification_utils import NotAMatchError

try:
    config = ModelConfigFactory.from_model_on_disk(mod, {})
except NotAMatchError as e:
    print(f"Not a usable Gemma2 encoder for PiD: {e}")  # suggest downloading gemma-2-2b-it

Prevention

When it happens

Trigger: Calling ModelConfigFactory.from_model_on_disk (directly or via model scan/import) on a directory whose config.json has architectures=["Gemma2ForCausalLM"] but hidden_size != 2304, e.g. any Gemma-2-9b-it or Gemma-2-27b-it checkpoint.

Common situations: User downloaded google/gemma-2-9b-it or gemma-2-27b-it instead of the 2b variant (e.g. Efficient-Large-Model/gemma-2-2b-it) and adds it as a PiD text encoder; also happens when a partial download of a sibling model directory is pointed at.

Related errors


AI-assisted analysis of invoke-ai/InvokeAI@0b6a024f2f (2026-08-29). Data as JSON: /api/errors/37c404ed6dd2fc16. Report an issue: GitHub.