r""" Base class for all models. :class:`~transformers.PreTrainedModel` takes care of storing the configuration of the models and handles methods for loading/downloading/saving models as well as a few methods common to all models to (i) resize the input embeddings and (ii) prune head
| 263 | |
| 264 | |
| 265 | class PreTrainedModel(nn.Module, ModuleUtilsMixin, GenerationMixin): |
| 266 | r""" Base class for all models. |
| 267 | |
| 268 | :class:`~transformers.PreTrainedModel` takes care of storing the configuration of the models and handles methods for loading/downloading/saving models |
| 269 | as well as a few methods common to all models to (i) resize the input embeddings and (ii) prune heads in the self-attention heads. |
| 270 | |
| 271 | Class attributes (overridden by derived classes): |
| 272 | - ``config_class``: a class derived from :class:`~transformers.PretrainedConfig` to use as configuration class for this model architecture. |
| 273 | - ``load_tf_weights``: a python ``method`` for loading a TensorFlow checkpoint in a PyTorch model, taking as arguments: |
| 274 | |
| 275 | - ``model``: an instance of the relevant subclass of :class:`~transformers.PreTrainedModel`, |
| 276 | - ``config``: an instance of the relevant subclass of :class:`~transformers.PretrainedConfig`, |
| 277 | - ``path``: a path (string) to the TensorFlow checkpoint. |
| 278 | |
| 279 | - ``base_model_prefix``: a string indicating the attribute associated to the base model in derived classes of the same architecture adding modules on top of the base model. |
| 280 | """ |
| 281 | config_class = None |
| 282 | base_model_prefix = "" |
| 283 | |
| 284 | @property |
| 285 | def dummy_inputs(self): |
| 286 | """ Dummy inputs to do a forward pass in the network. |
| 287 | |
| 288 | Returns: |
| 289 | torch.Tensor with dummy inputs |
| 290 | """ |
| 291 | return {"input_ids": torch.tensor(DUMMY_INPUTS)} |
| 292 | |
| 293 | def __init__(self, config, *inputs, **kwargs): |
| 294 | super().__init__() |
| 295 | if not isinstance(config, PretrainedConfig): |
| 296 | raise ValueError( |
| 297 | "Parameter config in `{}(config)` should be an instance of class `PretrainedConfig`. " |
| 298 | "To create a model from a pretrained model use " |
| 299 | "`model = {}.from_pretrained(PRETRAINED_MODEL_NAME)`".format( |
| 300 | self.__class__.__name__, self.__class__.__name__ |
| 301 | ) |
| 302 | ) |
| 303 | # Save config in model |
| 304 | self.config = config |
| 305 | |
| 306 | @property |
| 307 | def base_model(self): |
| 308 | return getattr(self, self.base_model_prefix, self) |
| 309 | |
| 310 | def get_input_embeddings(self): |
| 311 | """ |
| 312 | Returns the model's input embeddings. |
| 313 | |
| 314 | Returns: |
| 315 | :obj:`nn.Module`: |
| 316 | A torch module mapping vocabulary to hidden states. |
| 317 | """ |
| 318 | base_model = getattr(self, self.base_model_prefix, self) |
| 319 | if base_model is not self: |
| 320 | return base_model.get_input_embeddings() |
| 321 | else: |
| 322 | raise NotImplementedError |
nothing calls this directly
no outgoing calls
no test coverage detected