configuration_colpali.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright 2024 The HuggingFace Inc. team.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """ColPali model configuration"""
  15. from huggingface_hub.dataclasses import strict
  16. from ...configuration_utils import PreTrainedConfig
  17. from ...utils import auto_docstring, logging
  18. from ..auto import CONFIG_MAPPING, AutoConfig
  19. logger = logging.get_logger(__name__)
  20. @auto_docstring(checkpoint="vidore/colpali-v1.2")
  21. @strict
  22. class ColPaliConfig(PreTrainedConfig):
  23. r"""
  24. Example:
  25. ```python
  26. from transformers.models.colpali import ColPaliConfig, ColPaliForRetrieval
  27. config = ColPaliConfig()
  28. model = ColPaliForRetrieval(config)
  29. ```
  30. """
  31. model_type = "colpali"
  32. sub_configs = {"vlm_config": PreTrainedConfig, "text_config": AutoConfig}
  33. vlm_config: dict | PreTrainedConfig | None = None
  34. text_config: dict | PreTrainedConfig | None = None
  35. embedding_dim: int = 128
  36. def __post_init__(self, **kwargs):
  37. if self.vlm_config is None:
  38. self.vlm_config = CONFIG_MAPPING["paligemma"]()
  39. logger.info(
  40. "`vlm_config` is `None`. Initializing `vlm_config` with the `PaliGemmaConfig` with default values."
  41. )
  42. elif isinstance(self.vlm_config, dict):
  43. self.vlm_config = CONFIG_MAPPING[self.vlm_config["model_type"]](**self.vlm_config)
  44. self.text_config = self.text_config if self.text_config is not None else self.vlm_config.text_config
  45. if isinstance(self.text_config, dict):
  46. self.text_config["model_type"] = self.text_config.get("model_type", "gemma")
  47. self.text_config = CONFIG_MAPPING[self.text_config["model_type"]](**self.text_config)
  48. super().__post_init__(**kwargs)
  49. __all__ = ["ColPaliConfig"]