class composer.models.HuggingFaceModel(model, tokenizer=None, use_logits=False, metrics=None)[source]#

A wrapper class that converts ๐Ÿค— Transformers models to composer models.

  • model (PreTrainedModel) โ€“ A ๐Ÿค— Transformers model.

  • tokenizer (PreTrainedTokenizer, optional) โ€“

    The tokenizer used to prepare the dataset. Default None.


    If the tokenizer is provided, its config will be saved in the composer checkpoint, and it can be reloaded using HuggingFaceModel.hf_from_composer_checkpoint(). If the tokenizer is not provided here, it will not be saved in the composer checkpoint.

  • use_logits (bool, optional) โ€“ If True, the modelโ€™s output logits will be used to calculate validation metrics. Else, metrics will be inferred from the HuggingFaceModel directly. Default: False

  • metrics (list[Metric], optional) โ€“ list of torchmetrics to apply to the output of validate. Default: None.


This wrapper is designed to work with ๐Ÿค— datasets that define a labels column.


import transformers
from composer.models import HuggingFaceModel

hf_model = transformers.AutoModelForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=2)
hf_tokenizer = transformers.AutoTokenizer.from_pretrained('bert-base-uncased')
model = HuggingFaceModel(hf_model, hf_tokenizer)
static hf_from_composer_checkpoint(checkpoint_path, model_instantiation_class=None, model_config_kwargs=None, local_checkpoint_save_location=None)[source]#

Loads a HuggingFace model (and tokenizer if present) from a composer checkpoint.


This function does not load the weights from the checkpoint. It just loads the correctly configured model and tokenizer classes.


hf_model, hf_tokenizer = HuggingFaceModel.hf_from_composer_checkpoint('')
composer_model = HuggingFaceModel(hf_model, hf_tokenizer)
trainer = Trainer(model=composer_model,
  • checkpoint_path (str) โ€“ Path to the composer checkpoint, can be a local path, http(s):// url, or s3:// uri

  • model_instantiation_class (Union[Type[transformers.PreTrainedModel], Type[transformers.AutoModel], str]), optional) โ€“ Class to use to create the HuggingFace model. Defaults to the model class used in the original checkpoint. If this argument is a HuggingFace auto class (e.g. transformers.AutoModel or transformers.AutoModelForSequenceClassification), the from_config method will be used, while if it is of type transformers.PreTrainedModel, the constructor will be called. This argument can also be a string, which will attempt to be imported as the class to use.

  • model_config_kwargs โ€“ Dict[str, Any]: Extra arguments to pass in for the model config creation (e.g. num_labels for creating a sequence classification model)

  • local_checkpoint_save_location (Optional[Union[Path, str]], optional) โ€“ If specified, where to save the checkpoint file to locally. If the input checkpoint_path is already a local path, this will be a symlink. Defaults to None, which will use a temporary file.


ValueError โ€“ If the model_instantiation_class, or the model class saved in the checkpoint, is not able to be imported


Tuple[transformers.PreTrainedModel, Optional[transformers.PreTrainedTokenizer]] โ€“ The loaded HuggingFace model and (if present) tokenizer