CSS-LM
104 строки · 4.3 Кб
1# coding=utf-8
2# 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 at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# 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 and
15# limitations under the License.
16
17import copy
18import logging
19
20from .configuration_utils import PretrainedConfig
21
22
23logger = logging.getLogger(__name__)
24
25
26class EncoderDecoderConfig(PretrainedConfig):
27r"""
28:class:`~transformers.EncoderDecoderConfig` is the configuration class to store the configuration of a `EncoderDecoderModel`.
29
30It is used to instantiate an Encoder Decoder model according to the specified arguments, defining the encoder and decoder configs.
31Configuration objects inherit from :class:`~transformers.PretrainedConfig`
32and can be used to control the model outputs.
33See the documentation for :class:`~transformers.PretrainedConfig` for more information.
34
35Args:
36kwargs (`optional`):
37Remaining dictionary of keyword arguments. Notably:
38encoder (:class:`PretrainedConfig`, optional, defaults to `None`):
39An instance of a configuration object that defines the encoder config.
40decoder (:class:`PretrainedConfig`, optional, defaults to `None`):
41An instance of a configuration object that defines the decoder config.
42
43Example::
44
45>>> from transformers import BertConfig, EncoderDecoderConfig, EncoderDecoderModel
46
47>>> # Initializing a BERT bert-base-uncased style configuration
48>>> config_encoder = BertConfig()
49>>> config_decoder = BertConfig()
50
51>>> config = EncoderDecoderConfig.from_encoder_decoder_configs(config_encoder, config_decoder)
52
53>>> # Initializing a Bert2Bert model from the bert-base-uncased style configurations
54>>> model = EncoderDecoderModel(config=config)
55
56>>> # Accessing the model configuration
57>>> config_encoder = model.config.encoder
58>>> config_decoder = model.config.decoder
59"""
60model_type = "encoder_decoder"
61
62def __init__(self, **kwargs):
63super().__init__(**kwargs)
64assert (
65"encoder" in kwargs and "decoder" in kwargs
66), "Config has to be initialized with encoder and decoder config"
67encoder_config = kwargs.pop("encoder")
68encoder_model_type = encoder_config.pop("model_type")
69decoder_config = kwargs.pop("decoder")
70decoder_model_type = decoder_config.pop("model_type")
71
72from .configuration_auto import AutoConfig
73
74self.encoder = AutoConfig.for_model(encoder_model_type, **encoder_config)
75self.decoder = AutoConfig.for_model(decoder_model_type, **decoder_config)
76self.is_encoder_decoder = True
77
78@classmethod
79def from_encoder_decoder_configs(
80cls, encoder_config: PretrainedConfig, decoder_config: PretrainedConfig
81) -> PretrainedConfig:
82r"""
83Instantiate a :class:`~transformers.EncoderDecoderConfig` (or a derived class) from a pre-trained encoder model configuration and decoder model configuration.
84
85Returns:
86:class:`EncoderDecoderConfig`: An instance of a configuration object
87"""
88logger.info("Set `config.is_decoder=True` for decoder_config")
89decoder_config.is_decoder = True
90
91return cls(encoder=encoder_config.to_dict(), decoder=decoder_config.to_dict())
92
93def to_dict(self):
94"""
95Serializes this instance to a Python dictionary. Override the default `to_dict()` from `PretrainedConfig`.
96
97Returns:
98:obj:`Dict[str, any]`: Dictionary of all the attributes that make up this configuration instance,
99"""
100output = copy.deepcopy(self.__dict__)
101output["encoder"] = self.encoder.to_dict()
102output["decoder"] = self.decoder.to_dict()
103output["model_type"] = self.__class__.model_type
104return output
105