120 lines
4.8 KiB
Python
120 lines
4.8 KiB
Python
# coding=utf-8
|
|
# Copyright 2025 Google Inc. HuggingFace Inc. team. All rights reserved.
|
|
#
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
from ...configuration_utils import PretrainedConfig
|
|
from ...utils import logging
|
|
from ..auto import CONFIG_MAPPING, AutoConfig
|
|
|
|
|
|
logger = logging.get_logger(__name__)
|
|
|
|
|
|
class ShieldGemma2Config(PretrainedConfig):
|
|
r"""
|
|
This is the configuration class to store the configuration of a [`ShieldGemma2ForImageClassification`]. It is used to instantiate an
|
|
ShieldGemma2ForImageClassification according to the specified arguments, defining the model architecture. Instantiating a configuration
|
|
with the defaults will yield a similar configuration to that of the shieldgemma-2-4b-it.
|
|
|
|
e.g. [google/gemma-3-4b](https://huggingface.co/google/gemma-3-4b)
|
|
|
|
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
|
|
documentation from [`PretrainedConfig`] for more information.
|
|
|
|
Args:
|
|
text_config (`Union[ShieldGemma2TextConfig, dict]`, *optional*):
|
|
The config object of the text backbone.
|
|
vision_config (`Union[AutoConfig, dict]`, *optional*):
|
|
Custom vision config or dict.
|
|
mm_tokens_per_image (`int`, *optional*, defaults to 256):
|
|
The number of tokens per image embedding.
|
|
boi_token_index (`int`, *optional*, defaults to 255999):
|
|
The begin-of-image token index to wrap the image prompt.
|
|
eoi_token_index (`int`, *optional*, defaults to 256000):
|
|
The end-of-image token index to wrap the image prompt.
|
|
image_token_index (`int`, *optional*, defaults to 262144):
|
|
The image token index to encode the image prompt.
|
|
initializer_range (`float`, *optional*, defaults to 0.02):
|
|
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
|
|
|
|
|
|
Example:
|
|
|
|
```python
|
|
>>> from transformers import ShieldGemma2ForConditionalGeneration, ShieldGemma2Config, SiglipVisionConfig, ShieldGemma2TextConfig
|
|
|
|
>>> # Initializing a Siglip-like vision config
|
|
>>> vision_config = SiglipVisionConfig()
|
|
|
|
>>> # Initializing a ShieldGemma2 Text config
|
|
>>> text_config = ShieldGemma2TextConfig()
|
|
|
|
>>> # Initializing a ShieldGemma2 gemma-3-4b style configuration
|
|
>>> configuration = ShieldGemma2Config(vision_config, text_config)
|
|
|
|
>>> # Initializing a model from the gemma-3-4b style configuration
|
|
>>> model = ShieldGemma2TextConfig(configuration)
|
|
|
|
>>> # Accessing the model configuration
|
|
>>> configuration = model.config
|
|
```"""
|
|
|
|
model_type = "shieldgemma2"
|
|
attribute_map = {
|
|
"image_token_id": "image_token_index",
|
|
"boi_token_id": "boi_token_index",
|
|
"eoi_token_id": "eoi_token_index",
|
|
}
|
|
sub_configs = {"text_config": AutoConfig, "vision_config": AutoConfig}
|
|
|
|
def __init__(
|
|
self,
|
|
text_config=None,
|
|
vision_config=None,
|
|
mm_tokens_per_image: int = 256,
|
|
boi_token_index: int = 255_999,
|
|
eoi_token_index: int = 256_000,
|
|
image_token_index: int = 262_144,
|
|
initializer_range: float = 0.02,
|
|
**kwargs,
|
|
):
|
|
if isinstance(vision_config, dict):
|
|
vision_config["model_type"] = (
|
|
vision_config["model_type"] if "model_type" in vision_config else "siglip_vision_model"
|
|
)
|
|
vision_config = CONFIG_MAPPING[vision_config["model_type"]](**vision_config)
|
|
elif vision_config is None:
|
|
vision_config = CONFIG_MAPPING["siglip_vision_model"]()
|
|
|
|
self.vision_config = vision_config
|
|
|
|
if isinstance(text_config, dict):
|
|
text_config["model_type"] = text_config["model_type"] if "model_type" in text_config else "gemma3_text"
|
|
text_config = CONFIG_MAPPING[text_config["model_type"]](**text_config)
|
|
elif text_config is None:
|
|
text_config = CONFIG_MAPPING["gemma3_text"]()
|
|
|
|
self.text_config = text_config
|
|
self.vision_config = vision_config
|
|
self.mm_tokens_per_image = mm_tokens_per_image
|
|
self.boi_token_index = boi_token_index
|
|
self.eoi_token_index = eoi_token_index
|
|
self.image_token_index = image_token_index
|
|
self.initializer_range = initializer_range
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
|
__all__ = ["ShieldGemma2Config"]
|