Cache-system reference
Last updated
Last updated
The caching system was updated in v0.8.0 to become the central cache-system shared across libraries that depend on the Hub. Read the for a detailed presentation of caching at HF.
huggingface_hub.try_to_load_from_cache
( repo_id: strfilename: strcache_dir: typing.Union[str, pathlib.Path, NoneType] = Nonerevision: typing.Optional[str] = Nonerepo_type: typing.Optional[str] = None ) → Optional[str]
or _CACHED_NO_EXIST
Parameters
cache_dir (str
or os.PathLike
) — The folder where the cached files lie.
repo_id (str
) — The ID of the repo on huggingface.co.
filename (str
) — The filename to look for inside repo_id
.
revision (str
, optional) — The specific model version to use. Will default to "main"
if it’s not provided and no commit_hash
is provided either.
repo_type (str
, optional) — The type of the repository. Will default to "model"
.
Returns
Optional[str]
or _CACHED_NO_EXIST
Will return None
if the file was not cached. Otherwise:
The exact path to the cached file if it’s found in the cache
A special value _CACHED_NO_EXIST
if the file does not exist at the given commit hash and this fact was cached.
Explores the cache to return the latest cached file for a given revision if found.
This function will not raise any exception if the file in not cached.
Example:
Copied
huggingface_hub.cached_assets_path
( library_name: strnamespace: str = 'default'subfolder: str = 'default'assets_dir: typing.Union[str, pathlib.Path, NoneType] = None )
Parameters
library_name (str
) — Name of the library that will manage the cache folder. Example: "dataset"
.
namespace (str
, optional, defaults to “default”) — Namespace to which the data belongs. Example: "SQuAD"
.
subfolder (str
, optional, defaults to “default”) — Subfolder in which the data will be stored. Example: extracted
.
assets_dir (str
, Path
, optional) — Path to the folder where assets are cached. This must not be the same folder where Hub files are cached. Defaults to HF_HOME / "assets"
if not provided. Can also be set with HUGGINGFACE_ASSETS_CACHE
environment variable.
Return a folder path to cache arbitrary files.
huggingface_hub
provides a canonical folder path to store assets. This is the recommended way to integrate cache in a downstream library as it will benefit from the builtins tools to scan and delete the cache properly.
Once the folder path is generated, it is guaranteed to exist and to be a directory. The path is based on 3 levels of depth: the library name, a namespace and a subfolder. Those 3 levels grants flexibility while allowing huggingface_hub
to expect folders when scanning/deleting parts of the assets cache. Within a library, it is expected that all namespaces share the same subset of subfolder names but this is not a mandatory rule. The downstream library has then full control on which file structure to adopt within its cache. Namespace and subfolder are optional (would default to a "default/"
subfolder) but library name is mandatory as we want every downstream library to manage its own cache.
Expected tree:
Copied
Example:
Copied
huggingface_hub.scan_cache_dir
( cache_dir: typing.Union[str, pathlib.Path, NoneType] = None )
Parameters
cache_dir (str
or Path
, optional
) — Cache directory to cache. Defaults to the default HF cache directory.
Raises
CacheNotFound
or ValueError
CacheNotFound
— If the cache directory does not exist.
Copied
You can also print a detailed report directly from the huggingface-cli
using:
Copied
( size_on_disk: intrepos: typing.FrozenSet[huggingface_hub.utils._cache_manager.CachedRepoInfo]warnings: typing.List[huggingface_hub.utils._cache_manager.CorruptedCacheException] )
Parameters
size_on_disk (int
) — Sum of all valid repo sizes in the cache-system.
Frozen data structure holding information about the entire cache-system.
Here size_on_disk
is equal to the sum of all repo sizes (only blobs). However if some cached repos are corrupted, their sizes are not taken into account.
delete_revisions
( *revisions: str )
Prepare the strategy to delete one or more revisions cached locally.
Input revisions can be any revision hash. If a revision hash is not found in the local cache, a warning is thrown but no error is raised. Revisions can be from different cached repos since hashes are unique across repos,
Examples:
Copied
Copied
( repo_id: strrepo_type: typing.Literal['model', 'dataset', 'space']repo_path: Pathsize_on_disk: intnb_files: intrevisions: typing.FrozenSet[huggingface_hub.utils._cache_manager.CachedRevisionInfo]last_accessed: floatlast_modified: float )
Parameters
repo_id (str
) — Repo id of the repo on the Hub. Example: "google/fleurs"
.
repo_type (Literal["dataset", "model", "space"]
) — Type of the cached repo.
repo_path (Path
) — Local path to the cached repo.
size_on_disk (int
) — Sum of the blob file sizes in the cached repo.
nb_files (int
) — Total number of blob files in the cached repo.
last_accessed (float
) — Timestamp of the last time a blob file of the repo has been accessed.
last_modified (float
) — Timestamp of the last time a blob file of the repo has been modified/created.
Frozen data structure holding information about a cached repository.
size_on_disk
is not necessarily the sum of all revisions sizes because of duplicated files. Besides, only blobs are taken into account, not the (negligible) size of folders and symlinks.
size_on_disk_str
( )
(property) Sum of the blob file sizes as a human-readable string.
Example: “42.2K”.
refs
( )
(property) Mapping between refs
and revision data structures.
( commit_hash: strsnapshot_path: Pathsize_on_disk: intfiles: typing.FrozenSet[huggingface_hub.utils._cache_manager.CachedFileInfo]refs: typing.FrozenSet[str]last_modified: float )
Parameters
commit_hash (str
) — Hash of the revision (unique). Example: "9338f7b671827df886678df2bdd7cc7b4f36dffd"
.
refs (FrozenSet[str]
) — Set of refs
pointing to this revision. If the revision has no refs
, it is considered detached. Example: {"main", "2.4.0"}
or {"refs/pr/1"}
.
size_on_disk (int
) — Sum of the blob file sizes that are symlink-ed by the revision.
last_modified (float
) — Timestamp of the last time the revision has been created/modified.
Frozen data structure holding information about a revision.
A revision correspond to a folder in the snapshots
folder and is populated with the exact tree structure as the repo on the Hub but contains only symlinks. A revision can be either referenced by 1 or more refs
or be “detached” (no refs).
last_accessed
cannot be determined correctly on a single revision as blob files are shared across revisions.
size_on_disk
is not necessarily the sum of all file sizes because of possible duplicated files. Besides, only blobs are taken into account, not the (negligible) size of folders and symlinks.
size_on_disk_str
( )
(property) Sum of the blob file sizes as a human-readable string.
Example: “42.2K”.
nb_files
( )
(property) Total number of files in the revision.
( file_name: strfile_path: Pathblob_path: Pathsize_on_disk: intblob_last_accessed: floatblob_last_modified: float )
Parameters
file_name (str
) — Name of the file. Example: config.json
.
file_path (Path
) — Path of the file in the snapshots
directory. The file path is a symlink referring to a blob in the blobs
folder.
blob_path (Path
) — Path of the blob file. This is equivalent to file_path.resolve()
.
size_on_disk (int
) — Size of the blob file in bytes.
blob_last_accessed (float
) — Timestamp of the last time the blob file has been accessed (from any revision).
blob_last_modified (float
) — Timestamp of the last time the blob file has been modified/created.
Frozen data structure holding information about a single cached file.
size_on_disk_str
( )
(property) Size of the blob file as a human-readable string.
Example: “42.2K”.
( expected_freed_size: intblobs: typing.FrozenSet[pathlib.Path]refs: typing.FrozenSet[pathlib.Path]repos: typing.FrozenSet[pathlib.Path]snapshots: typing.FrozenSet[pathlib.Path] )
Parameters
expected_freed_size (float
) — Expected freed size once strategy is executed.
blobs (FrozenSet[Path]
) — Set of blob file paths to be deleted.
refs (FrozenSet[Path]
) — Set of reference file paths to be deleted.
repos (FrozenSet[Path]
) — Set of entire repo paths to be deleted.
snapshots (FrozenSet[Path]
) — Set of snapshots to be deleted (directory of symlinks).
Frozen data structure holding the strategy to delete cached revisions.
expected_freed_size_str
( )
(property) Expected size that will be freed as a human-readable string.
Example: “42.2K”.
( )
Exception for any unexpected structure in the Huggingface cache-system.
The distinction is made between files cached from the Hub and assets. Files from the Hub are cached in a git-aware manner and entirely managed by huggingface_hub
. See . All other files that a downstream library caches are considered to be “assets” (files downloaded from external sources, extracted from a .tar archive, preprocessed for training,…).
— If the cache directory is a file, instead of a directory.
Scan the entire HF cache-system and return a structure.
Use scan_cache_dir
in order to programmatically scan your cache-system. The cache will be scanned repo by repo. If a repo is corrupted, a will be thrown internally but captured and returned in the structure. Only valid repos get a proper report.
Returns: a object.
All structures are built and returned by and are immutable.
repos (FrozenSet[CachedRepoInfo]
) — Set of describing all valid cached repos found on the cache-system while scanning.
warnings (List[CorruptedCacheException]
) — List of that occurred while scanning the cache. Those exceptions are captured so that the scan can continue. Corrupted repos are skipped from the scan.
This data structure is returned by and is immutable.
delete_revisions
returns a object that needs to be executed. The is not meant to be modified but allows having a dry run before actually executing the deletion.
revisions (FrozenSet[CachedRevisionInfo]
) — Set of describing all revisions cached in the repo.
last_accessed
and last_modified
reliability can depend on the OS you are using. See for more details.
snapshot_path (Path
) — Path to the revision directory in the snapshots
folder. It contains the exact tree structure as the repo on the Hub. files — (FrozenSet[CachedFileInfo]
): Set of describing all files contained in the snapshot.
blob_last_accessed
and blob_last_modified
reliability can depend on the OS you are using. See for more details.
This object is not meant to be instantiated programmatically but to be returned by . See documentation for usage example.