def setup_jiant_model( hf_pretrained_model_name_or_path: str, model_config_path: str, task_dict: Dict[str, Task], taskmodels_config: container_setup.TaskmodelsConfig, ): """Sets up tokenizer, encoder, and task models, and instantiates and returns a JiantModel. Args: hf_pretrained_model_name_or_path (:obj:`str` or :obj:`os.PathLike`): Can be either: - A string, the `model id` of a predefined tokenizer hosted inside a model repo on huggingface.co. Valid model ids can be located at the root-level, like ``bert-base-uncased``, or namespaced under a user or organization name, like ``dbmdz/bert-base-german-cased``. - A path to a `directory` containing vocabulary files required by the tokenizer, for instance saved using the :func:`~transformers.PreTrainedTokenizer.save_pretrained` method, e.g., ``./my_model_directory/``. - A path or url to a single saved vocabulary file if and only if the tokenizer only requires a single vocabulary file (like Bert or XLNet), e.g.: ``./my_model_directory/vocab.txt``. (Not applicable to all derived classes) model_config_path (str): Path to the JSON file containing the configuration parameters. task_dict (Dict[str, tasks.Task]): map from task name to task instance. taskmodels_config: maps mapping from tasks to models, and specifying task-model configs. Returns: JiantModel nn.Module. """ if hf_pretrained_model_name_or_path == "roberta-base": encoder = mixer_l16_224() #mixer_s16_224() # hf_model = transformers.AutoModel.from_pretrained(hf_pretrained_model_name_or_path) # print("hf_model: ", hf_model) # exit(0) tokenizer = transformers.AutoTokenizer.from_pretrained( hf_pretrained_model_name_or_path, use_fast=False ) print("tokenizer", tokenizer) # exit(0) # encoder = primary.JiantTransformersModelFactory()(hf_model) taskmodels_dict = { taskmodel_name: create_taskmodel( task=task_dict[task_name_list[0]], # Take the first task encoder=encoder, taskmodel_kwargs=taskmodels_config.get_taskmodel_kwargs(taskmodel_name), ) for taskmodel_name, task_name_list in get_taskmodel_and_task_names( taskmodels_config.task_to_taskmodel_map ).items() } return primary.JiantModel( task_dict=task_dict, encoder=encoder, taskmodels_dict=taskmodels_dict, task_to_taskmodel_map=taskmodels_config.task_to_taskmodel_map, tokenizer=tokenizer, )
def setup_jiant_model( model_type: str, model_config_path: str, tokenizer_path: str, task_dict: Dict[str, Task], taskmodels_config: container_setup.TaskmodelsConfig, ): """Sets up tokenizer, encoder, and task models, and instantiates and returns a JiantModel. Args: model_type (str): model shortcut name. model_config_path (str): Path to the JSON file containing the configuration parameters. tokenizer_path (str): path to tokenizer directory. task_dict (Dict[str, tasks.Task]): map from task name to task instance. taskmodels_config: maps mapping from tasks to models, and specifying task-model configs. Returns: JiantModel nn.Module. """ model_arch = ModelArchitectures.from_model_type(model_type) transformers_class_spec = TRANSFORMERS_CLASS_SPEC_DICT[model_arch] tokenizer = model_setup.get_tokenizer(model_type=model_type, tokenizer_path=tokenizer_path) ancestor_model = get_ancestor_model( transformers_class_spec=transformers_class_spec, model_config_path=model_config_path, ) encoder = get_encoder(model_arch=model_arch, ancestor_model=ancestor_model) taskmodels_dict = { taskmodel_name: create_taskmodel( task=task_dict[task_name_list[0]], # Take the first task model_arch=model_arch, encoder=encoder, taskmodel_kwargs=taskmodels_config.get_taskmodel_kwargs( taskmodel_name), ) for taskmodel_name, task_name_list in get_taskmodel_and_task_names( taskmodels_config.task_to_taskmodel_map).items() } return primary.JiantModel( task_dict=task_dict, encoder=encoder, taskmodels_dict=taskmodels_dict, task_to_taskmodel_map=taskmodels_config.task_to_taskmodel_map, tokenizer=tokenizer, )
def setup_jiant_model( hf_pretrained_model_name_or_path: str, model_config_path: str, task_dict: Dict[str, Task], taskmodels_config: container_setup.TaskmodelsConfig, ): """Sets up tokenizer, encoder, and task models, and instantiates and returns a JiantModel. Args: hf_pretrained_model_name_or_path (:obj:`str` or :obj:`os.PathLike`): Can be either: - A string, the `model id` of a predefined tokenizer hosted inside a model repo on huggingface.co. Valid model ids can be located at the root-level, like ``bert-base-uncased``, or namespaced under a user or organization name, like ``dbmdz/bert-base-german-cased``. - A path to a `directory` containing vocabulary files required by the tokenizer, for instance saved using the :func:`~transformers.PreTrainedTokenizer.save_pretrained` method, e.g., ``./my_model_directory/``. - A path or url to a single saved vocabulary file if and only if the tokenizer only requires a single vocabulary file (like Bert or XLNet), e.g.: ``./my_model_directory/vocab.txt``. (Not applicable to all derived classes) model_config_path (str): Path to the JSON file containing the configuration parameters. task_dict (Dict[str, tasks.Task]): map from task name to task instance. taskmodels_config: maps mapping from tasks to models, and specifying task-model configs. Returns: JiantModel nn.Module. """ model = transformers.AutoModel.from_pretrained( hf_pretrained_model_name_or_path) model_arch = ModelArchitectures.from_model_type(model.base_model_prefix) transformers_class_spec = TRANSFORMERS_CLASS_SPEC_DICT[model_arch] tokenizer = transformers.AutoTokenizer.from_pretrained( hf_pretrained_model_name_or_path) if (os.path.isdir(hf_pretrained_model_name_or_path)): model_config_path = hf_pretrained_model_name_or_path + "/config.json" ancestor_model = get_ancestor_model( transformers_class_spec=transformers_class_spec, model_config_path=model_config_path, ) encoder = get_encoder(model_arch=model_arch, ancestor_model=ancestor_model) taskmodels_dict = { taskmodel_name: create_taskmodel( task=task_dict[task_name_list[0]], # Take the first task model_arch=model_arch, encoder=encoder, taskmodel_kwargs=taskmodels_config.get_taskmodel_kwargs( taskmodel_name), ) for taskmodel_name, task_name_list in get_taskmodel_and_task_names( taskmodels_config.task_to_taskmodel_map).items() } return primary.JiantModel( task_dict=task_dict, encoder=encoder, taskmodels_dict=taskmodels_dict, task_to_taskmodel_map=taskmodels_config.task_to_taskmodel_map, tokenizer=tokenizer, )