HF Transfer (hf-transfer)
hf-transfer is a utility library designed to significantly speed up file transfers to and from the Hugging Face Hub. It works by optimizing the underlying transfer mechanisms used by the `huggingface_hub` library. The current version is 0.1.9, and it generally follows the release cadence of the broader Hugging Face ecosystem, with updates often tied to improvements in hub interaction.
Warnings
- breaking The environment variable for enabling hf-transfer changed from `HF_HUB_ENABLE_HF_TRANSFER` to `HF_TRANSFER_ENABLED`.
- gotcha hf-transfer must be enabled *before* any `huggingface_hub` operations are called for acceleration to take effect.
- gotcha hf-transfer does not provide standalone download/upload functionality; it acts as an accelerator for the `huggingface_hub` library. Therefore, `huggingface_hub` is a required dependency.
- gotcha Performance benefits of hf-transfer are most noticeable for large files or many small files, and can be influenced by network conditions and local storage speed. It may not provide significant speedups for very small transfers.
Install
-
pip install hf-transfer
Imports
- enable_transfer
import hf_transfer hf_transfer.enable_transfer()
Quickstart
import hf_transfer
from huggingface_hub import snapshot_download
import os
# Enable hf_transfer to accelerate subsequent Hugging Face Hub operations
hf_transfer.enable_transfer()
# Example: Download a small model from the Hugging Face Hub
# This operation will now be accelerated by hf_transfer
repo_id = "HuggingFaceH4/no_deid_test_data"
local_dir = "./no_deid_test_data_hf_transfer"
print(f"Downloading {repo_id} to {local_dir} with hf_transfer acceleration...")
try:
snapshot_download(repo_id=repo_id, local_dir=local_dir)
print(f"Successfully downloaded {repo_id} to {local_dir}")
except Exception as e:
print(f"An error occurred during download: {e}")
finally:
# Clean up downloaded files for subsequent runs if desired
# import shutil
# if os.path.exists(local_dir):
# shutil.rmtree(local_dir)
pass