Mixins & serialization methods
Mixins
The huggingface_hub
library offers a range of mixins that can be used as a parent class for your objects, in order to
provide simple uploading and downloading functions. Check out our integration guide to learn
how to integrate any ML framework with the Hub.
Generic
A generic mixin to integrate ANY machine learning framework with the Hub.
To integrate your framework, your model class must inherit from this class. Custom logic for saving/loading models
have to be overwritten in _from_pretrained
and _save_pretrained
. PyTorchModelHubMixin is a good example
of mixin integration with the Hub. Check out our integration guide for more instructions.
Example:
>>> from dataclasses import dataclass
>>> from huggingface_hub import ModelHubMixin
# Define your model configuration (optional)
>>> @dataclass
... class Config:
... foo: int = 512
... bar: str = "cpu"
# Inherit from ModelHubMixin (and optionally from your framework's model class)
>>> class MyCustomModel(ModelHubMixin):
... def __init__(self, config: Config):
... # define how to initialize your model
... super().__init__()
... ...
...
... def _save_pretrained(self, save_directory: Path) -> None:
... # define how to serialize your model
... ...
...
... @classmethod
... def from_pretrained(
... cls: Type[T],
... pretrained_model_name_or_path: Union[str, Path],
... *,
... force_download: bool = False,
... resume_download: bool = False,
... proxies: Optional[Dict] = None,
... token: Optional[Union[str, bool]] = None,
... cache_dir: Optional[Union[str, Path]] = None,
... local_files_only: bool = False,
... revision: Optional[str] = None,
... **model_kwargs,
... ) -> T:
... # define how to deserialize your model
... ...
>>> model = MyCustomModel(config=Config(foo=256, bar="gpu"))
# Save model weights to local directory
>>> model.save_pretrained("my-awesome-model")
# Push model weights to the Hub
>>> model.push_to_hub("my-awesome-model")
# Download and initialize weights from the Hub
>>> reloaded_model = MyCustomModel.from_pretrained("username/my-awesome-model")
>>> reloaded_model.config
Config(foo=256, bar="gpu")
_save_pretrained
< source >( save_directory: Path )
Overwrite this method in subclass to define how to save your model. Check out our integration guide for instructions.
_from_pretrained
< source >( model_id: str revision: Optional cache_dir: Union force_download: bool proxies: Optional resume_download: bool local_files_only: bool token: Union **model_kwargs )
Parameters
- model_id (
str
) — ID of the model to load from the Huggingface Hub (e.g.bigscience/bloom
). - revision (
str
, optional) — Revision of the model on the Hub. Can be a branch name, a git tag or any commit id. Defaults to the latest commit onmain
branch. - force_download (
bool
, optional, defaults toFalse
) — Whether to force (re-)downloading the model weights and configuration files from the Hub, overriding the existing cache. - resume_download (
bool
, optional, defaults toFalse
) — Whether to delete incompletely received files. Will attempt to resume the download if such a file exists. - proxies (
Dict[str, str]
, optional) — A dictionary of proxy servers to use by protocol or endpoint (e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}
). - token (
str
orbool
, optional) — The token to use as HTTP bearer authorization for remote files. By default, it will use the token cached when runninghuggingface-cli login
. - cache_dir (
str
,Path
, optional) — Path to the folder where cached files are stored. - local_files_only (
bool
, optional, defaults toFalse
) — IfTrue
, avoid downloading the file and return the path to the local cached file if it exists. model_kwargs — Additional keyword arguments passed along to the _from_pretrained() method.
Overwrite this method in subclass to define how to load your model from pretrained.
Use hf_hub_download() or snapshot_download() to download files from the Hub before loading them. Most
args taken as input can be directly passed to those 2 methods. If needed, you can add more arguments to this
method using “model_kwargs”. For example PyTorchModelHubMixin._from_pretrained()
takes as input a map_location
parameter to set on which device the model should be loaded.
Check out our integration guide for more instructions.
from_pretrained
< source >( pretrained_model_name_or_path: Union force_download: bool = False resume_download: bool = False proxies: Optional = None token: Union = None cache_dir: Union = None local_files_only: bool = False revision: Optional = None **model_kwargs )
Parameters
- pretrained_model_name_or_path (
str
,Path
) —- Either the
model_id
(string) of a model hosted on the Hub, e.g.bigscience/bloom
. - Or a path to a
directory
containing model weights saved using save_pretrained, e.g.,../path/to/my_model_directory/
.
- Either the
- revision (
str
, optional) — Revision of the model on the Hub. Can be a branch name, a git tag or any commit id. Defaults to the latest commit onmain
branch. - force_download (
bool
, optional, defaults toFalse
) — Whether to force (re-)downloading the model weights and configuration files from the Hub, overriding the existing cache. - resume_download (
bool
, optional, defaults toFalse
) — Whether to delete incompletely received files. Will attempt to resume the download if such a file exists. - proxies (
Dict[str, str]
, optional) — A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}
. The proxies are used on every request. - token (
str
orbool
, optional) — The token to use as HTTP bearer authorization for remote files. By default, it will use the token cached when runninghuggingface-cli login
. - cache_dir (
str
,Path
, optional) — Path to the folder where cached files are stored. - local_files_only (
bool
, optional, defaults toFalse
) — IfTrue
, avoid downloading the file and return the path to the local cached file if it exists. - model_kwargs (
Dict
, optional) — Additional kwargs to pass to the model during initialization.
Download a model from the Huggingface Hub and instantiate it.
push_to_hub
< source >( repo_id: str config: Union = None commit_message: str = 'Push model using huggingface_hub.' private: bool = False token: Optional = None branch: Optional = None create_pr: Optional = None allow_patterns: Union = None ignore_patterns: Union = None delete_patterns: Union = None api_endpoint: Optional = None )
Parameters
- repo_id (
str
) — ID of the repository to push to (example:"username/my-model"
). - config (
dict
orDataclassInstance
, optional) — Model configuration specified as a key/value dictionary or a dataclass instance. - commit_message (
str
, optional) — Message to commit while pushing. - private (
bool
, optional, defaults toFalse
) — Whether the repository created should be private. - api_endpoint (
str
, optional) — The API endpoint to use when pushing the model to the hub. - token (
str
, optional) — The token to use as HTTP bearer authorization for remote files. By default, it will use the token cached when runninghuggingface-cli login
. - branch (
str
, optional) — The git branch on which to push the model. This defaults to"main"
. - create_pr (
boolean
, optional) — Whether or not to create a Pull Request frombranch
with that commit. Defaults toFalse
. - allow_patterns (
List[str]
orstr
, optional) — If provided, only files matching at least one pattern are pushed. - ignore_patterns (
List[str]
orstr
, optional) — If provided, files matching any of the patterns are not pushed. - delete_patterns (
List[str]
orstr
, optional) — If provided, remote files matching any of the patterns will be deleted from the repo.
Upload model checkpoint to the Hub.
Use allow_patterns
and ignore_patterns
to precisely filter which files should be pushed to the hub. Use
delete_patterns
to delete existing remote files in the same commit. See upload_folder() reference for more
details.
save_pretrained
< source >( save_directory: Union config: Union = None repo_id: Optional = None push_to_hub: bool = False **push_to_hub_kwargs )
Parameters
- save_directory (
str
orPath
) — Path to directory in which the model weights and configuration will be saved. - config (
dict
orDataclassInstance
, optional) — Model configuration specified as a key/value dictionary or a dataclass instance. - push_to_hub (
bool
, optional, defaults toFalse
) — Whether or not to push your model to the Huggingface Hub after saving it. - repo_id (
str
, optional) — ID of your repository on the Hub. Used only ifpush_to_hub=True
. Will default to the folder name if not provided. kwargs — Additional key word arguments passed along to the push_to_hub() method.
Save weights in local directory.
PyTorch
Implementation of ModelHubMixin to provide model Hub upload/download capabilities to PyTorch models. The model
is set in evaluation mode by default using model.eval()
(dropout modules are deactivated). To train the model,
you should first set it back in training mode with model.train()
.
Example:
>>> from dataclasses import dataclass
>>> import torch
>>> import torch.nn as nn
>>> from huggingface_hub import PyTorchModelHubMixin
>>> @dataclass
... class Config:
... hidden_size: int = 512
... vocab_size: int = 30000
... output_size: int = 4
>>> class MyModel(nn.Module, PyTorchModelHubMixin):
... def __init__(self, config: Config):
... super().__init__()
... self.param = nn.Parameter(torch.rand(config.hidden_size, config.vocab_size))
... self.linear = nn.Linear(config.output_size, config.vocab_size)
... def forward(self, x):
... return self.linear(x + self.param)
>>> model = MyModel()
# Save model weights to local directory
>>> model.save_pretrained("my-awesome-model")
# Push model weights to the Hub
>>> model.push_to_hub("my-awesome-model")
# Download and initialize weights from the Hub
>>> model = MyModel.from_pretrained("username/my-awesome-model")
Keras
Implementation of ModelHubMixin to provide model Hub upload/download capabilities to Keras models.
>>> import tensorflow as tf
>>> from huggingface_hub import KerasModelHubMixin
>>> class MyModel(tf.keras.Model, KerasModelHubMixin):
... def __init__(self, **kwargs):
... super().__init__()
... self.config = kwargs.pop("config", None)
... self.dummy_inputs = ...
... self.layer = ...
... def call(self, *args):
... return ...
>>> # Initialize and compile the model as you normally would
>>> model = MyModel()
>>> model.compile(...)
>>> # Build the graph by training it or passing dummy inputs
>>> _ = model(model.dummy_inputs)
>>> # Save model weights to local directory
>>> model.save_pretrained("my-awesome-model")
>>> # Push model weights to the Hub
>>> model.push_to_hub("my-awesome-model")
>>> # Download and initialize weights from the Hub
>>> model = MyModel.from_pretrained("username/super-cool-model")
huggingface_hub.from_pretrained_keras
< source >( *args **kwargs )
Parameters
- pretrained_model_name_or_path (
str
oros.PathLike
) — Can be either:- A string, the
model id
of a pretrained model hosted inside a model repo on huggingface.co. Valid model ids can be located at the root-level, likebert-base-uncased
, or namespaced under a user or organization name, likedbmdz/bert-base-german-cased
. - You can add
revision
by appending@
at the end of model_id simply like this:dbmdz/bert-base-german-cased@main
Revision is the specific model version to use. It can be a branch name, a tag name, or a commit id, since we use a git-based system for storing models and other artifacts on huggingface.co, sorevision
can be any identifier allowed by git. - A path to a
directory
containing model weights saved using save_pretrained, e.g.,./my_model_directory/
. None
if you are both providing the configuration and state dictionary (resp. with keyword argumentsconfig
andstate_dict
).
- A string, the
- force_download (
bool
, optional, defaults toFalse
) — Whether to force the (re-)download of the model weights and configuration files, overriding the cached versions if they exist. - resume_download (
bool
, optional, defaults toFalse
) — Whether to delete incompletely received files. Will attempt to resume the download if such a file exists. - proxies (
Dict[str, str]
, optional) — A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}
. The proxies are used on each request. - token (
str
orbool
, optional) — The token to use as HTTP bearer authorization for remote files. IfTrue
, will use the token generated when runningtransformers-cli login
(stored in~/.huggingface
). - cache_dir (
Union[str, os.PathLike]
, optional) — Path to a directory in which a downloaded pretrained model configuration should be cached if the standard cache should not be used. - local_files_only(
bool
, optional, defaults toFalse
) — Whether to only look at local files (i.e., do not try to download the model). - model_kwargs (
Dict
, optional) — model_kwargs will be passed to the model during initialization
Instantiate a pretrained Keras model from a pre-trained model from the Hub.
The model is expected to be in SavedModel
format.
Passing token=True
is required when you want to use a private
model.
huggingface_hub.push_to_hub_keras
< source >( model repo_id: str config: Optional = None commit_message: str = 'Push Keras model using huggingface_hub.' private: bool = False api_endpoint: Optional = None token: Optional = None branch: Optional = None create_pr: Optional = None allow_patterns: Union = None ignore_patterns: Union = None delete_patterns: Union = None log_dir: Optional = None include_optimizer: bool = False tags: Union = None plot_model: bool = True **model_save_kwargs )
Parameters
- model (
Keras.Model
) — The Keras model you’d like to push to the Hub. The model must be compiled and built. - repo_id (
str
) — ID of the repository to push to (example:"username/my-model"
). - commit_message (
str
, optional, defaults to “Add Keras model”) — Message to commit while pushing. - private (
bool
, optional, defaults toFalse
) — Whether the repository created should be private. - api_endpoint (
str
, optional) — The API endpoint to use when pushing the model to the hub. - token (
str
, optional) — The token to use as HTTP bearer authorization for remote files. If not set, will use the token set when logging in withhuggingface-cli login
(stored in~/.huggingface
). - branch (
str
, optional) — The git branch on which to push the model. This defaults to the default branch as specified in your repository, which defaults to"main"
. - create_pr (
boolean
, optional) — Whether or not to create a Pull Request frombranch
with that commit. Defaults toFalse
. - config (
dict
, optional) — Configuration object to be saved alongside the model weights. - allow_patterns (
List[str]
orstr
, optional) — If provided, only files matching at least one pattern are pushed. - ignore_patterns (
List[str]
orstr
, optional) — If provided, files matching any of the patterns are not pushed. - delete_patterns (
List[str]
orstr
, optional) — If provided, remote files matching any of the patterns will be deleted from the repo. - log_dir (
str
, optional) — TensorBoard logging directory to be pushed. The Hub automatically hosts and displays a TensorBoard instance if log files are included in the repository. - include_optimizer (
bool
, optional, defaults toFalse
) — Whether or not to include optimizer during serialization. - tags (Union[
list
,str
], optional) — List of tags that are related to model or string of a single tag. See example tags here. - plot_model (
bool
, optional, defaults toTrue
) — Setting this toTrue
will plot the model and put it in the model card. Requires graphviz and pydot to be installed. - model_save_kwargs(
dict
, optional) — model_save_kwargs will be passed totf.keras.models.save_model()
.
Upload model checkpoint to the Hub.
Use allow_patterns
and ignore_patterns
to precisely filter which files should be pushed to the hub. Use
delete_patterns
to delete existing remote files in the same commit. See upload_folder() reference for more
details.
huggingface_hub.save_pretrained_keras
< source >( model save_directory: Union config: Optional = None include_optimizer: bool = False plot_model: bool = True tags: Union = None **model_save_kwargs )
Parameters
- model (
Keras.Model
) — The Keras model you’d like to save. The model must be compiled and built. - save_directory (
str
orPath
) — Specify directory in which you want to save the Keras model. - config (
dict
, optional) — Configuration object to be saved alongside the model weights. - include_optimizer(
bool
, optional, defaults toFalse
) — Whether or not to include optimizer in serialization. - plot_model (
bool
, optional, defaults toTrue
) — Setting this toTrue
will plot the model and put it in the model card. Requires graphviz and pydot to be installed. - tags (Union[
str
,list
], optional) — List of tags that are related to model or string of a single tag. See example tags here. - model_save_kwargs(
dict
, optional) — model_save_kwargs will be passed totf.keras.models.save_model()
.
Saves a Keras model to save_directory in SavedModel format. Use this if you’re using the Functional or Sequential APIs.
Fastai
huggingface_hub.from_pretrained_fastai
< source >( repo_id: str revision: Optional = None )
Parameters
- repo_id (
str
) — The location where the pickled fastai.Learner is. It can be either of the two:- Hosted on the Hugging Face Hub. E.g.: ‘espejelomar/fatai-pet-breeds-classification’ or ‘distilgpt2’.
You can add a
revision
by appending@
at the end ofrepo_id
. E.g.:dbmdz/bert-base-german-cased@main
. Revision is the specific model version to use. Since we use a git-based system for storing models and other artifacts on the Hugging Face Hub, it can be a branch name, a tag name, or a commit id. - Hosted locally.
repo_id
would be a directory containing the pickle and a pyproject.toml indicating the fastai and fastcore versions used to build thefastai.Learner
. E.g.:./my_model_directory/
.
- Hosted on the Hugging Face Hub. E.g.: ‘espejelomar/fatai-pet-breeds-classification’ or ‘distilgpt2’.
You can add a
- revision (
str
, optional) — Revision at which the repo’s files are downloaded. See documentation ofsnapshot_download
.
Load pretrained fastai model from the Hub or from a local directory.
huggingface_hub.push_to_hub_fastai
< source >( learner repo_id: str commit_message: str = 'Push FastAI model using huggingface_hub.' private: bool = False token: Optional = None config: Optional = None branch: Optional = None create_pr: Optional = None allow_patterns: Union = None ignore_patterns: Union = None delete_patterns: Union = None api_endpoint: Optional = None )
Parameters
- learner (Learner) — The *fastai.Learner’ you’d like to push to the Hub.
- repo_id (str) — The repository id for your model in Hub in the format of “namespace/repo_name”. The namespace can be your individual account or an organization to which you have write access (for example, ‘stanfordnlp/stanza-de’).
- commit_message (str`, optional*) — Message to commit while pushing. Will default to
"add model"
. - private (bool, optional, defaults to False) — Whether or not the repository created should be private.
- token (str, optional) —
The Hugging Face account token to use as HTTP bearer authorization for remote files. If
None
, the token will be asked by a prompt. - config (dict, optional) — Configuration object to be saved alongside the model weights.
- branch (str, optional) — The git branch on which to push the model. This defaults to the default branch as specified in your repository, which defaults to “main”.
- create_pr (boolean, optional) — Whether or not to create a Pull Request from branch with that commit. Defaults to False.
- api_endpoint (str, optional) — The API endpoint to use when pushing the model to the hub.
- allow_patterns (List[str] or str, optional) — If provided, only files matching at least one pattern are pushed.
- ignore_patterns (List[str] or str, optional) — If provided, files matching any of the patterns are not pushed.
- delete_patterns (List[str] or str, optional) — If provided, remote files matching any of the patterns will be deleted from the repo.
Upload learner checkpoint files to the Hub.
Use allow_patterns and ignore_patterns to precisely filter which files should be pushed to the hub. Use delete_patterns to delete existing remote files in the same commit. See [upload_folder] reference for more details.
Raises the following error:
- ValueError if the user is not log on to the Hugging Face Hub.