class PretrainedModel(name_scope=None, dtype='float32')[source]

The base class for all pretrained models. It mainly provides common methods for loading (construction and loading) and saving pretrained models. Loading and saving also rely on the following class attributes which should be overridden by derived classes accordingly:

  • model_config_file (str): Represents the file name of model configuration for configuration saving and loading in local file system. The value is model_config.json.

  • resource_files_names (dict): Name of local file where the model configuration can be saved and loaded locally. Currently, resources only include the model state, thus the dict only includes 'model_state' as key with corresponding value 'model_state.pdparams' for model weights saving and loading.

  • pretrained_init_configuration (dict): Provides the model configurations of built-in pretrained models (contrasts to models in local file system). It has pretrained model names as keys (such as bert-base-uncased), and the values are dict preserving corresponding configuration for model initialization.

  • pretrained_resource_files_map (dict): Provides resource URLs of built-in pretrained models (contrasts to models in local file system). It has the same key as resource_files_names (that is “model_state”), and the corresponding value is a dict with specific model name to model weights URL mapping (such as “bert-base-uncased” -> “”).

  • base_model_prefix (str): Represents the attribute associated to the base model in derived classes of the same architecture adding layers on top of the base model. Note: A base model class is pretrained model class decorated by register_base_model, such as BertModel; A derived model class is a pretrained model class adding layers on top of the base model, and it has a base model as attribute, such as BertForSequenceClassification.

Methods common to models for text generation are defined in GenerationMixin and also inherited here.

Besides, metaclass InitTrackerMeta is used to create PretrainedModel, by which subclasses can track arguments for initialization automatically.

property base_model

The body of the same model architecture. It is the base model itself for base model or the base model attribute for derived model.



property model_name_list

Contains all supported built-in pretrained model names of the current PretrainedModel class.



classmethod from_pretrained(pretrained_model_name_or_path, *args, **kwargs)[source]

Creates an instance of PretrainedModel. Model weights are loaded by specifying name of a built-in pretrained model, or a community contributed model, or a local file directory path.

  • pretrained_model_name_or_path (str) –

    Name of pretrained model or dir path to load from. The string can be:

    • Name of a built-in pretrained model

    • Name of a community-contributed pretrained model.

    • Local directory path which contains model weights file(“model_state.pdparams”) and model config file (“model_config.json”).

  • *args (tuple) – Position arguments for model __init__. If provided, use these as position argument values for model initialization.

  • **kwargs (dict) – Keyword arguments for model __init__. If provided, use these to update pre-defined keyword argument values for model initialization. If the keyword is in __init__ argument names of base model, update argument values of the base model; else update argument values of derived model.

  • load_state_as_np (bool, optional) – The weights read in can be choosed to place on CPU or GPU though the model is on the default device. If True, load the model weights as numpy.ndarray on CPU. Otherwise, weights would be loaded as tensors on the default device. Note that if on GPU, the latter would creates extra temporary tensors in addition to the model weights, which doubles the memory usage . Thus it is suggested to use True for big models on GPU. Default to False.


An instance of PretrainedModel.

Return type



from paddlenlp.transformers import BertForSequenceClassification

# Name of built-in pretrained model
model = BertForSequenceClassification.from_pretrained('bert-base-uncased')

# Name of community-contributed pretrained model
model = BertForSequenceClassification.from_pretrained('yingyibiao/bert-base-uncased-sst-2-finetuned')

# Load from local directory path
model = BertForSequenceClassification.from_pretrained('./my_bert/')

Get model configuration.


The config of the model.

Return type



Saves model configuration to a file named “model_config.json” under save_dir.


save_dir (str) – Directory to save model_config file into.


Saves model configuration and related resources (model state) as files under save_dir. The model configuration would be saved into a file named “model_config.json”, and model state would be saved into a file named “model_state.pdparams”.

The save_dir can be used in from_pretrained as argument value of pretrained_model_name_or_path to re-load the trained model.


save_dir (str) – Directory to save files into.


from paddlenlp.transformers import BertForSequenceClassification

model = BertForSequenceClassification.from_pretrained('bert-base-uncased')
# reload from save_directory
model = BertForSequenceClassification.from_pretrained('./trained_model/')

A decorator for PretrainedModel class. It first retrieves the parent class of the class being decorated, then sets the base_model_class attribute of that parent class to be the class being decorated. In summary, the decorator registers the decorated class as the base model class in all derived classes under the same architecture.


cls (PretrainedModel) – The class (inherited from PretrainedModel) to be decorated .


The input class cls after decorating.

Return type



from paddlenlp.transformers import BertModel, register_base_model

BertModel = register_base_model(BertModel)
assert BertModel.base_model_class == BertModel