model_utils

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

Bases: paddle.fluid.dygraph.layers.Layer, paddlenlp.transformers.generation_utils.GenerationMixin

The base class for all pretrained models. It provides some attributes and common methods for all pretrained models, including attributes init_config, config for initialized arguments and methods for saving, loading. It also includes some class attributes (should be set by derived classes): - model_config_file (str): represents the file name for saving and loading

model configuration, it’s value is model_config.json.

  • resource_files_names (dict): use this to map resources to specific file names for saving and loading.

  • pretrained_resource_files_map (dict): The dict has the same keys as resource_files_names, the values are also dict mapping specific pretrained model name to URL linking to pretrained model.

  • pretrained_init_configuration (dict): The dict has pretrained model names as keys, and the values are also dict preserving corresponding configuration for model initialization.

  • base_model_prefix (str): represents the the attribute associated to the base model in derived classes of the same architecture adding layers on top of the base model.

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

Instantiate an instance of PretrainedModel from a predefined model specified by name or path. :param pretrained_model_name_or_path: A name of or a file path to a

pretrained model.

Parameters
  • *args (tuple) – position arguments for __init__. If provide, use this as position argument values for model initialization.

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

Returns

An instance of PretrainedModel.

Return type

PretrainedModel

save_pretrained(save_directory)[source]

Save model configuration and related resources (model state) to files under save_directory. :param save_directory: Directory to save files into. :type save_directory: str

register_base_model(cls)[source]

Add a base_model_class attribute for the base class of decorated class, representing the base model class in derived classes of the same architecture. :param cls: the name of the model :type cls: class