Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

2.0 KB
Raw Blame
from pathlib import Path
from typing import Optional

from transformers import CLIPVisionModelWithProjection

from invokeai.backend.model_manager.configs.base import Diffusers_Config_Base
from invokeai.backend.model_manager.configs.factory import AnyModelConfig
from invokeai.backend.model_manager.load.load_default import ModelLoader
from invokeai.backend.model_manager.load.model_loader_registry import ModelLoaderRegistry
from invokeai.backend.model_manager.taxonomy import AnyModel, BaseModelType, ModelFormat, ModelType, SubModelType
from invokeai.backend.model_manager.util.clip_tower_config import clip_tower_config_override
from invokeai.backend.util.load_report import suppress_load_report


@ModelLoaderRegistry.register(base=BaseModelType.Any, type=ModelType.CLIPVision, format=ModelFormat.Diffusers)
class ClipVisionLoader(ModelLoader):
    """Class to load CLIPVision models."""

    def _load_model(
        self,
        config: AnyModelConfig,
        submodel_type: Optional[SubModelType] = None,
    ) -> AnyModel:
        if not isinstance(config, Diffusers_Config_Base):
            raise ValueError("Only DiffusersConfigBase models are currently supported here.")

        if submodel_type is not None:
            raise Exception("There are no submodels in CLIP Vision models.")

        model_path = Path(config.path)

        # Full-CLIP checkpoints may carry a nested projection_dim that
        # disagrees with the weights; see clip_tower_config_override.
        tower_config = clip_tower_config_override(model_path, "vision")
        extra_kwargs = {} if tower_config is None else {"config": tower_config}
        # A full-CLIP checkpoint carries the text tower this class does not build; see
        # suppress_load_report.
        with suppress_load_report():
            model = CLIPVisionModelWithProjection.from_pretrained(
                model_path, torch_dtype=self._torch_dtype, local_files_only=True, **extra_kwargs
            )
        assert isinstance(model, CLIPVisionModelWithProjection)

        return model