codingutf8 2020 the huggingface inc team c 2018 nvidia corporation 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 logger infoset config isdecodertrue and config addcrossattentiontrue for decoderconfig decoderconfig isdecoder true decoderconfig addcrossattention true return clsencoderencoderconfig todict decoderdecoderconfig todict kwargs coding utf 8 2020 the huggingface inc team c 2018 nvidia corporation 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 encoderdecoderconfig is the configuration class to store the configuration of a encoderdecodermodel it is used to instantiate an encoder decoder model according to the specified arguments defining the encoder and decoder configs configuration objects inherit from pretrainedconfig and can be used to control the model outputs read the documentation from pretrainedconfig for more information args kwargs optional dictionary of keyword arguments notably encoder pretrainedconfig optional an instance of a configuration object that defines the encoder config decoder pretrainedconfig optional an instance of a configuration object that defines the decoder config examples python from transformers import bertconfig encoderdecoderconfig encoderdecodermodel initializing a bert bert base uncased style configuration config_encoder bertconfig config_decoder bertconfig config encoderdecoderconfig from_encoder_decoder_configs config_encoder config_decoder initializing a bert2bert model with random weights from the bert base uncased style configurations model encoderdecodermodel config config accessing the model configuration config_encoder model config encoder config_decoder model config decoder set decoder config to causal lm config_decoder is_decoder true config_decoder add_cross_attention true saving the model including its configuration model save_pretrained my model loading model and config from pretrained folder encoder_decoder_config encoderdecoderconfig from_pretrained my model model encoderdecodermodel from_pretrained my model config encoder_decoder_config instantiate a encoderdecoderconfig or a derived class from a pre trained encoder model configuration and decoder model configuration returns encoderdecoderconfig an instance of a configuration object
from ...configuration_utils import PretrainedConfig
from ...utils import logging


logger = logging.get_logger(__name__)


class EncoderDecoderConfig(PretrainedConfig):
    r

    model_type = "encoder-decoder"
    is_composition = True

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        assert (
            "encoder" in kwargs and "decoder" in kwargs
        ), "Config has to be initialized with encoder and decoder config"
        encoder_config = kwargs.pop("encoder")
        encoder_model_type = encoder_config.pop("model_type")
        decoder_config = kwargs.pop("decoder")
        decoder_model_type = decoder_config.pop("model_type")

        from ..auto.configuration_auto import AutoConfig

        self.encoder = AutoConfig.for_model(encoder_model_type, **encoder_config)
        self.decoder = AutoConfig.for_model(decoder_model_type, **decoder_config)
        self.is_encoder_decoder = True

    @classmethod
    def from_encoder_decoder_configs(
        cls, encoder_config: PretrainedConfig, decoder_config: PretrainedConfig, **kwargs
    ) -> PretrainedConfig:
        r
        logger.info("Set `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config")
        decoder_config.is_decoder = True
        decoder_config.add_cross_attention = True

        return cls(encoder=encoder_config.to_dict(), decoder=decoder_config.to_dict(), **kwargs)