Aluode/PerceptionLabPortable
0
1# coding=utf-82# Copyright 2020 The HuggingFace Inc. team.3# Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.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 at8#9# http://www.apache.org/licenses/LICENSE-2.010#11# Unless required by applicable law or agreed to in writing, software12# 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 and15# limitations under the License.16 17 18from ...configuration_utils import PretrainedConfig19from ...utils import logging20from ..auto import AutoConfig21 22 23logger = logging.get_logger(__name__)24 25 26class EncoderDecoderConfig(PretrainedConfig):27 r"""28 [`EncoderDecoderConfig`] is the configuration class to store the configuration of a [`EncoderDecoderModel`]. It is29 used to instantiate an Encoder Decoder model according to the specified arguments, defining the encoder and decoder30 configs.31 32 Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the33 documentation from [`PretrainedConfig`] for more information.34 35 Args:36 kwargs (*optional*):37 Dictionary of keyword arguments. Notably:38 39 - **encoder** ([`PretrainedConfig`], *optional*) -- An instance of a configuration object that defines40 the encoder config.41 - **decoder** ([`PretrainedConfig`], *optional*) -- An instance of a configuration object that defines42 the decoder config.43 44 Examples:45 46 ```python47 >>> from transformers import BertConfig, EncoderDecoderConfig, EncoderDecoderModel48 49 >>> # Initializing a BERT google-bert/bert-base-uncased style configuration50 >>> config_encoder = BertConfig()51 >>> config_decoder = BertConfig()52 53 >>> config = EncoderDecoderConfig.from_encoder_decoder_configs(config_encoder, config_decoder)54 55 >>> # Initializing a Bert2Bert model (with random weights) from the google-bert/bert-base-uncased style configurations56 >>> model = EncoderDecoderModel(config=config)57 58 >>> # Accessing the model configuration59 >>> config_encoder = model.config.encoder60 >>> config_decoder = model.config.decoder61 >>> # set decoder config to causal lm62 >>> config_decoder.is_decoder = True63 >>> config_decoder.add_cross_attention = True64 65 >>> # Saving the model, including its configuration66 >>> model.save_pretrained("my-model")67 68 >>> # loading model and config from pretrained folder69 >>> encoder_decoder_config = EncoderDecoderConfig.from_pretrained("my-model")70 >>> model = EncoderDecoderModel.from_pretrained("my-model", config=encoder_decoder_config)71 ```"""72 73 model_type = "encoder-decoder"74 sub_configs = {"encoder": AutoConfig, "decoder": AutoConfig}75 has_no_defaults_at_init = True76 77 def __init__(self, **kwargs):78 super().__init__(**kwargs)79 if "encoder" not in kwargs or "decoder" not in kwargs:80 raise ValueError(81 f"A configuration of type {self.model_type} cannot be instantiated because "82 f"both `encoder` and `decoder` sub-configurations were not passed, only {kwargs}"83 )84 encoder_config = kwargs.pop("encoder")85 encoder_model_type = encoder_config.pop("model_type")86 decoder_config = kwargs.pop("decoder")87 decoder_model_type = decoder_config.pop("model_type")88 89 self.encoder = AutoConfig.for_model(encoder_model_type, **encoder_config)90 self.decoder = AutoConfig.for_model(decoder_model_type, **decoder_config)91 self.is_encoder_decoder = True92 93 @classmethod94 def from_encoder_decoder_configs(95 cls, encoder_config: PretrainedConfig, decoder_config: PretrainedConfig, **kwargs96 ) -> PretrainedConfig:97 r"""98 Instantiate a [`EncoderDecoderConfig`] (or a derived class) from a pre-trained encoder model configuration and99 decoder model configuration.100 101 Returns:102 [`EncoderDecoderConfig`]: An instance of a configuration object103 """104 logger.info("Set `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config")105 decoder_config.is_decoder = True106 decoder_config.add_cross_attention = True107 108 return cls(encoder=encoder_config.to_dict(), decoder=decoder_config.to_dict(), **kwargs)109 110 111__all__ = ["EncoderDecoderConfig"]112 