class_docstr: 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.
class_docstr_tok: ["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","."]
docstr: Instantiate an instance of `PretrainedModel` from a predefined
  model specified by name or path.
  Args:
      pretrained_model_name_or_path (str): A name of or a file path to a
          pretrained model.
      *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.
  Returns:
      PretrainedModel: An instance of PretrainedModel.
docstr_tok: ["Instantiate","an","instance","of","`","PretrainedModel","`","from","a","predefined","model","specified","by","name","or","path",".","Args",":","pretrained_model_name_or_path","(","str",")",":","A","name","of","or","a","file","path","to","a","pretrained","model",".","*","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",".","Returns",":","PretrainedModel",":","An","instance","of","PretrainedModel","."]
code: def from_pretrained(
      cls, pretrained_model_name_or_path, *args, **kwargs
  ):
      """
      Instantiate an instance of `PretrainedModel` from a predefined
      model specified by name or path.
      Args:
          pretrained_model_name_or_path (str): A name of or a file path to a
              pretrained model.
          *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.
      Returns:
          PretrainedModel: An instance of PretrainedModel.
      """
      pretrained_models = list(cls.pretrained_init_configuration.keys())
      resource_files = {}
      init_configuration = {}
      if pretrained_model_name_or_path in pretrained_models:
          for (
              file_id,
              map_list,
          ) in cls.pretrained_resource_files_map.items():
              resource_files[file_id] = map_list[
                  pretrained_model_name_or_path
              ]
          init_configuration = copy.deepcopy(
              cls.pretrained_init_configuration[
                  pretrained_model_name_or_path
              ]
          )
      else:
          if os.path.isdir(pretrained_model_name_or_path):
              for (
                  file_id,
                  file_name,
              ) in cls.resource_files_names.items():
                  full_file_name = os.path.join(
                      pretrained_model_name_or_path, file_name
                  )
                  resource_files[file_id] = full_file_name
              resource_files["model_config_file"] = os.path.join(
                  pretrained_model_name_or_path, cls.model_config_file
              )
          else:
              raise ValueError(
                  "Calling {}.from_pretrained() with a model identifier or the "
                  "path to a directory instead. The supported model "
                  "identifiers are as follows: {}".format(
                      cls.__name__,
                      cls.pretrained_init_configuration.keys(),
                  )
              )
      # FIXME(chenzeyu01): We should use another data path for storing model
      default_root = os.path.join(
          DATA_HOME, pretrained_model_name_or_path
      )
      resolved_resource_files = {}
      for file_id, file_path in resource_files.items():
          path = os.path.join(default_root, file_path.split("/")[-1])
          if file_path is None or os.path.isfile(file_path):
              resolved_resource_files[file_id] = file_path
          elif os.path.exists(path):
              logger.info("Already cached %s" % path)
              resolved_resource_files[file_id] = path
          else:
              logger.info(
                  "Downloading %s and saved to %s"
                  % (file_path, default_root)
              )
              resolved_resource_files[file_id] = get_path_from_url(
                  file_path, default_root
              )
  
      # Prepare model initialization kwargs
      # Did we saved some inputs and kwargs to reload ?
      model_config_file = resolved_resource_files.pop(
          "model_config_file", None
      )
      if model_config_file is not None:
          with io.open(model_config_file, encoding="utf-8") as f:
              init_kwargs = json.load(f)
      else:
          init_kwargs = init_configuration
      # position args are stored in kwargs, maybe better not include
      init_args = init_kwargs.pop("init_args", ())
      # class name corresponds to this configuration
      init_class = init_kwargs.pop(
          "init_class", cls.base_model_class.__name__
      )
  
      # Check if the loaded config matches the current model class's __init__
      # arguments. If not match, the loaded config is for the base model class.
      if init_class == cls.base_model_class.__name__:
          base_args = init_args
          base_kwargs = init_kwargs
          derived_args = ()
          derived_kwargs = {}
          base_arg_index = None
      else:  # extract config for base model
          derived_args = list(init_args)
          derived_kwargs = init_kwargs
          for i, arg in enumerate(init_args):
              if isinstance(arg, dict) and "init_class" in arg:
                  assert (
                      arg.pop("init_class")
                      == cls.base_model_class.__name__
                  ), ("pretrained base model should be {}").format(
                      cls.base_model_class.__name__
                  )
                  base_arg_index = i
                  break
          for arg_name, arg in init_kwargs.items():
              if isinstance(arg, dict) and "init_class" in arg:
                  assert (
                      arg.pop("init_class")
                      == cls.base_model_class.__name__
                  ), ("pretrained base model should be {}").format(
                      cls.base_model_class.__name__
                  )
                  base_arg_index = arg_name
                  break
          base_args = arg.pop("init_args", ())
          base_kwargs = arg
      if cls == cls.base_model_class:
          # Update with newly provided args and kwargs for base model
          base_args = base_args if not args else args
          base_kwargs.update(kwargs)
          model = cls(*base_args, **base_kwargs)
      else:
          # Update with newly provided args and kwargs for derived model
          base_model = cls.base_model_class(*base_args, **base_kwargs)
          if base_arg_index is not None:
              derived_args[base_arg_index] = base_model
          else:
              derived_args = (
                  base_model,
              )  # assume at the first position
          derived_args = derived_args if not args else args
          derived_kwargs.update(kwargs)
          model = cls(*derived_args, **derived_kwargs)
  
      # Maybe need more ways to load resources.
      weight_path = list(resolved_resource_files.values())[0]
      assert weight_path.endswith(
          ".pdparams"
      ), "suffix of weight must be .pdparams"
      state_dict = paddle.load(weight_path)
  
      # Make sure we are able to load base models as well as derived models
      # (with heads)
      start_prefix = ""
      model_to_load = model
      state_to_load = state_dict
      unexpected_keys = []
      missing_keys = []
      if not hasattr(model, cls.base_model_prefix) and any(
          s.startswith(cls.base_model_prefix) for s in state_dict.keys()
      ):
          # base model
          state_to_load = {}
          start_prefix = cls.base_model_prefix + "."
          for k, v in state_dict.items():
              if k.startswith(cls.base_model_prefix):
                  state_to_load[k[len(start_prefix) :]] = v
              else:
                  unexpected_keys.append(k)
      if hasattr(model, cls.base_model_prefix) and not any(
          s.startswith(cls.base_model_prefix) for s in state_dict.keys()
      ):
          # derived model (base model with heads)
          model_to_load = getattr(model, cls.base_model_prefix)
          for k in model.state_dict().keys():
              if not k.startswith(cls.base_model_prefix):
                  missing_keys.append(k)
      if len(missing_keys) > 0:
          logger.info(
              "Weights of {} not initialized from pretrained model: {}".format(
                  model.__class__.__name__, missing_keys
              )
          )
      if len(unexpected_keys) > 0:
          logger.info(
              "Weights from pretrained model not used in {}: {}".format(
                  model.__class__.__name__, unexpected_keys
              )
          )
      model_to_load.set_state_dict(state_to_load)
      if paddle.in_dynamic_mode():
          return model
      return model, state_to_load
  
imports: import copy
  import functools
  import inspect
  import io
  import json
  import os
  import six
  from typing import List, Tuple, Union
  import paddle
  import paddle.nn
  from packaging.version import Version
  from paddle.dataset.common import DATA_HOME
  from paddle.utils.download import get_path_from_url
  from paddlehub.module.module import serving, RunModule, runnable
  from paddlehub.utils.log import logger
  from paddlehub.utils.utils import reseg_token_label
  import paddlenlp
  from paddlenlp.embeddings.token_embedding import EMBEDDING_HOME, EMBEDDING_URL_ROOT
  from paddlenlp.data import JiebaTokenizer
  from paddlehub.compat.module.nlp_module import DataFormatError
label_desc: Use image node_id 6 for calling the PretrainedModel obj's underlying member method code with example usage: obj.from_pretrained(cls, pretrained_model_name_or_path) and returns: model,  state_to_load, model