|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2025 Google Inc. HuggingFace Inc. team. All rights reserved. |
| 3 | +# |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +from ...configuration_utils import PretrainedConfig |
| 18 | +from ...utils import logging |
| 19 | +from ..auto import CONFIG_MAPPING, AutoConfig |
| 20 | + |
| 21 | + |
| 22 | +logger = logging.get_logger(__name__) |
| 23 | + |
| 24 | + |
| 25 | +class ShieldGemma2Config(PretrainedConfig): |
| 26 | + r""" |
| 27 | + This is the configuration class to store the configuration of a [`ShieldGemma2ForImageClassification`]. It is used to instantiate an |
| 28 | + ShieldGemma2ForImageClassification according to the specified arguments, defining the model architecture. Instantiating a configuration |
| 29 | + with the defaults will yield a similar configuration to that of the shieldgemma-2-4b-it. |
| 30 | +
|
| 31 | + e.g. [google/gemma-3-4b](https://huggingface.co/google/gemma-3-4b) |
| 32 | +
|
| 33 | + Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the |
| 34 | + documentation from [`PretrainedConfig`] for more information. |
| 35 | +
|
| 36 | + Args: |
| 37 | + text_config (`Union[ShieldGemma2TextConfig, dict]`, *optional*): |
| 38 | + The config object of the text backbone. |
| 39 | + vision_config (`Union[AutoConfig, dict]`, *optional*): |
| 40 | + Custom vision config or dict. |
| 41 | + mm_tokens_per_image (`int`, *optional*, defaults to 256): |
| 42 | + The number of tokens per image embedding. |
| 43 | + boi_token_index (`int`, *optional*, defaults to 255999): |
| 44 | + The begin-of-image token index to wrap the image prompt. |
| 45 | + eoi_token_index (`int`, *optional*, defaults to 256000): |
| 46 | + The end-of-image token index to wrap the image prompt. |
| 47 | + image_token_index (`int`, *optional*, defaults to 262144): |
| 48 | + The image token index to encode the image prompt. |
| 49 | + initializer_range (`float`, *optional*, defaults to 0.02): |
| 50 | + The standard deviation of the truncated_normal_initializer for initializing all weight matrices. |
| 51 | +
|
| 52 | +
|
| 53 | + Example: |
| 54 | +
|
| 55 | + ```python |
| 56 | + >>> from transformers import ShieldGemma2ForConditionalGeneration, ShieldGemma2Config, SiglipVisionConfig, ShieldGemma2TextConfig |
| 57 | +
|
| 58 | + >>> # Initializing a Siglip-like vision config |
| 59 | + >>> vision_config = SiglipVisionConfig() |
| 60 | +
|
| 61 | + >>> # Initializing a ShieldGemma2 Text config |
| 62 | + >>> text_config = ShieldGemma2TextConfig() |
| 63 | +
|
| 64 | + >>> # Initializing a ShieldGemma2 gemma-3-4b style configuration |
| 65 | + >>> configuration = ShieldGemma2Config(vision_config, text_config) |
| 66 | +
|
| 67 | + >>> # Initializing a model from the gemma-3-4b style configuration |
| 68 | + >>> model = ShieldGemma2TextConfig(configuration) |
| 69 | +
|
| 70 | + >>> # Accessing the model configuration |
| 71 | + >>> configuration = model.config |
| 72 | + ```""" |
| 73 | + |
| 74 | + model_type = "shieldgemma2" |
| 75 | + sub_configs = {"text_config": AutoConfig, "vision_config": AutoConfig} |
| 76 | + |
| 77 | + def __init__( |
| 78 | + self, |
| 79 | + text_config=None, |
| 80 | + vision_config=None, |
| 81 | + mm_tokens_per_image: int = 256, |
| 82 | + boi_token_index: int = 255_999, |
| 83 | + eoi_token_index: int = 256_000, |
| 84 | + image_token_index: int = 262_144, |
| 85 | + initializer_range: float = 0.02, |
| 86 | + **kwargs, |
| 87 | + ): |
| 88 | + if isinstance(vision_config, dict): |
| 89 | + vision_config["model_type"] = ( |
| 90 | + vision_config["model_type"] if "model_type" in vision_config else "siglip_vision_model" |
| 91 | + ) |
| 92 | + vision_config = CONFIG_MAPPING[vision_config["model_type"]](**vision_config) |
| 93 | + elif vision_config is None: |
| 94 | + vision_config = CONFIG_MAPPING["siglip_vision_model"]() |
| 95 | + |
| 96 | + self.vision_config = vision_config |
| 97 | + |
| 98 | + if isinstance(text_config, dict): |
| 99 | + text_config["model_type"] = text_config["model_type"] if "model_type" in text_config else "gemma3_text" |
| 100 | + text_config = CONFIG_MAPPING[text_config["model_type"]](**text_config) |
| 101 | + elif text_config is None: |
| 102 | + text_config = CONFIG_MAPPING["gemma3_text"]() |
| 103 | + |
| 104 | + self.text_config = text_config |
| 105 | + self.vision_config = vision_config |
| 106 | + self.mm_tokens_per_image = mm_tokens_per_image |
| 107 | + self.boi_token_index = boi_token_index |
| 108 | + self.eoi_token_index = eoi_token_index |
| 109 | + self.image_token_index = image_token_index |
| 110 | + self.initializer_range = initializer_range |
| 111 | + |
| 112 | + super().__init__(**kwargs) |
| 113 | + |
| 114 | + |
| 115 | +__all__ = ["ShieldGemma2Config"] |
0 commit comments