codingutf8 the huggingface inc team 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 orglicenseslicense2 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 visiontextdualencoder model configuration from configurationutils import pretrainedconfig from utils import logging from auto configurationauto import autoconfig from clip configurationclip import clipvisionconfig logger logging getloggername class visiontextdualencoderconfigpretrainedconfig r visiontextdualencoderconfig is the configuration class to store the configuration of a visiontextdualencodermodel it is used to instantiate visiontextdualencodermodel model according to the specified arguments defining the text model and vision model configs configuration objects inherit from pretrainedconfig and can be used to control the model outputs read the documentation from pretrainedconfig for more information args projectiondim int optional defaults to 512 dimentionality of text and vision projection layers logitscaleinitvalue float optional defaults to 2 6592 the inital value of the logitscale paramter default is used as per the original clip implementation kwargs optional dictionary of keyword arguments examples python from transformers import vitconfig bertconfig visiontextdualencoderconfig visiontextdualencodermodel initializing a bert and vit configuration configvision vitconfig configtext bertconfig config visiontextdualencoderconfig fromvisiontextconfigsconfigvision configtext projectiondim512 initializing a bert and vit model with random weights model visiontextdualencodermodelconfigconfig accessing the model configuration configvision model config visionconfig configtext model config textconfig saving the model including its configuration model savepretrainedvitbert loading model and config from pretrained folder visiontextconfig visiontextdualencoderconfig frompretrainedvitbert model visiontextdualencodermodel frompretrainedvitbert configvisiontextconfig modeltype visiontextdualencoder iscomposition true def initself projectiondim512 logitscaleinitvalue2 6592 kwargs super initkwargs if visionconfig not in kwargs raise valueerrorvisionconfig can not be none if textconfig not in kwargs raise valueerrortextconfig can not be none visionconfig kwargs popvisionconfig textconfig kwargs poptextconfig visionmodeltype visionconfig popmodeltype textmodeltype textconfig popmodeltype if visionmodeltype clip self visionconfig autoconfig formodelvisionmodeltype visionconfig visionconfig elif visionmodeltype clipvisionmodel self visionconfig clipvisionconfigvisionconfig else self visionconfig autoconfig formodelvisionmodeltype visionconfig self textconfig autoconfig formodeltextmodeltype textconfig self projectiondim projectiondim self logitscaleinitvalue logitscaleinitvalue classmethod def fromvisiontextconfigscls visionconfig pretrainedconfig textconfig pretrainedconfig kwargs r instantiate a visiontextdualencoderconfig or a derived class from text model configuration and vision model configuration returns visiontextdualencoderconfig an instance of a configuration object coding utf 8 the huggingface inc team 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 visiontextdualencoder model configuration visiontextdualencoderconfig is the configuration class to store the configuration of a visiontextdualencodermodel it is used to instantiate visiontextdualencodermodel model according to the specified arguments defining the text model and vision model configs configuration objects inherit from pretrainedconfig and can be used to control the model outputs read the documentation from pretrainedconfig for more information args projection_dim int optional defaults to 512 dimentionality of text and vision projection layers logit_scale_init_value float optional defaults to 2 6592 the inital value of the logit_scale paramter default is used as per the original clip implementation kwargs optional dictionary of keyword arguments examples python from transformers import vitconfig bertconfig visiontextdualencoderconfig visiontextdualencodermodel initializing a bert and vit configuration config_vision vitconfig config_text bertconfig config visiontextdualencoderconfig from_vision_text_configs config_vision config_text projection_dim 512 initializing a bert and vit model with random weights model visiontextdualencodermodel config config accessing the model configuration config_vision model config vision_config config_text model config text_config saving the model including its configuration model save_pretrained vit bert loading model and config from pretrained folder vision_text_config visiontextdualencoderconfig from_pretrained vit bert model visiontextdualencodermodel from_pretrained vit bert config vision_text_config instantiate a visiontextdualencoderconfig or a derived class from text model configuration and vision model configuration returns visiontextdualencoderconfig an instance of a configuration object
from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto.configuration_auto import AutoConfig
from ..clip.configuration_clip import CLIPVisionConfig


logger = logging.get_logger(__name__)


class VisionTextDualEncoderConfig(PretrainedConfig):
    r

    model_type = "vision-text-dual-encoder"
    is_composition = True

    def __init__(self, projection_dim=512, logit_scale_init_value=2.6592, **kwargs):
        super().__init__(**kwargs)

        if "vision_config" not in kwargs:
            raise ValueError("`vision_config` can not be `None`.")

        if "text_config" not in kwargs:
            raise ValueError("`text_config` can not be `None`.")

        vision_config = kwargs.pop("vision_config")
        text_config = kwargs.pop("text_config")

        vision_model_type = vision_config.pop("model_type")
        text_model_type = text_config.pop("model_type")

        if vision_model_type == "clip":
            self.vision_config = AutoConfig.for_model(vision_model_type, **vision_config).vision_config
        elif vision_model_type == "clip_vision_model":
            self.vision_config = CLIPVisionConfig(**vision_config)
        else:
            self.vision_config = AutoConfig.for_model(vision_model_type, **vision_config)

        self.text_config = AutoConfig.for_model(text_model_type, **text_config)

        self.projection_dim = projection_dim
        self.logit_scale_init_value = logit_scale_init_value

    @classmethod
    def from_vision_text_configs(cls, vision_config: PretrainedConfig, text_config: PretrainedConfig, **kwargs):
        r

        return cls(vision_config=vision_config.to_dict(), text_config=text_config.to_dict(), **kwargs)