r""" :class:`~transformers.EncoderDecoder` is a generic model class that will be instantiated as a transformer architecture with one of the base model classes of the library as encoder and another one as decoder when created with the `AutoModel.from_pretrained(pretrai
| 27 | |
| 28 | |
| 29 | class EncoderDecoderModel(PreTrainedModel): |
| 30 | r""" |
| 31 | :class:`~transformers.EncoderDecoder` is a generic model class that will be |
| 32 | instantiated as a transformer architecture with one of the base model |
| 33 | classes of the library as encoder and another one as |
| 34 | decoder when created with the `AutoModel.from_pretrained(pretrained_model_name_or_path)` |
| 35 | class method for the encoder and `AutoModelForCausalLM.from_pretrained(pretrained_model_name_or_path)` class method for the decoder. |
| 36 | """ |
| 37 | config_class = EncoderDecoderConfig |
| 38 | base_model_prefix = "encoder_decoder" |
| 39 | |
| 40 | def __init__( |
| 41 | self, |
| 42 | config: Optional[PretrainedConfig] = None, |
| 43 | encoder: Optional[PreTrainedModel] = None, |
| 44 | decoder: Optional[PreTrainedModel] = None, |
| 45 | ): |
| 46 | assert config is not None or ( |
| 47 | encoder is not None and decoder is not None |
| 48 | ), "Either a configuration or an Encoder and a decoder has to be provided" |
| 49 | if config is None: |
| 50 | config = EncoderDecoderConfig.from_encoder_decoder_configs(encoder.config, decoder.config) |
| 51 | else: |
| 52 | assert isinstance(config, self.config_class), "config: {} has to be of type {}".format( |
| 53 | config, self.config_class |
| 54 | ) |
| 55 | # initialize with config |
| 56 | super().__init__(config) |
| 57 | |
| 58 | if encoder is None: |
| 59 | from transformers import AutoModel |
| 60 | |
| 61 | encoder = AutoModel.from_config(config.encoder) |
| 62 | |
| 63 | if decoder is None: |
| 64 | from transformers import AutoModelForCausalLM |
| 65 | |
| 66 | decoder = AutoModelForCausalLM.from_config(config.decoder) |
| 67 | |
| 68 | self.encoder = encoder |
| 69 | self.decoder = decoder |
| 70 | assert ( |
| 71 | self.encoder.get_output_embeddings() is None |
| 72 | ), "The encoder {} should not have a LM Head. Please use a model without LM Head" |
| 73 | |
| 74 | def tie_weights(self): |
| 75 | # for now no weights tying in encoder-decoder |
| 76 | pass |
| 77 | |
| 78 | def get_encoder(self): |
| 79 | return self.encoder |
| 80 | |
| 81 | def get_decoder(self): |
| 82 | return self.decoder |
| 83 | |
| 84 | def get_input_embeddings(self): |
| 85 | return self.encoder.get_input_embeddings() |
| 86 |
no outgoing calls