Skip to content

Machine learning libraries download weights into a cache you never see

Pretrained weights, datasets and compiled kernels land in hidden cache folders. Where each framework puts them and what is safe.

5 min read

The first time a model is loaded, the library downloads its weights and caches them so the second time is instant. That is sensible and it means a machine used for experiments accumulates gigabytes of weights for models used once.

Where each framework caches

du -sh ~/.cache/torch ~/.cache/huggingface ~/.keras \
  ~/tensorflow_datasets ~/.cache/matplotlib 2>/dev/null | sort -h
echo $TORCH_HOME $HF_HOME
FrameworkDefault locationWhat is in it
PyTorch~/.cache/torchPretrained weights, hub checkouts
Hugging Face~/.cache/huggingfaceModels, datasets, tokenizers
Keras~/.kerasWeights and small datasets
TensorFlow Datasets~/tensorflow_datasetsPrepared dataset copies
Matplotlib~/.cache/matplotlibFont cache, small

Prepared datasets are the surprise

A dataset library does not only download data, it prepares it into its own format, so you can end up with the raw download and the prepared copy. For an image dataset that means the same data twice, and prepared copies are regenerated from the raw archive if you keep it.

Moving the cache rather than clearing it

export HF_HOME=/Volumes/External/hf-cache
export TORCH_HOME=/Volumes/External/torch-cache

Every one of these libraries respects an environment variable for its cache location. Pointing them at an external drive is the better answer than repeated clearing if this is regular work, and it is the same approach as moving other developer caches off the internal drive.

Clearing safely

Hugging Face has an interactive tool that shows sizes and last used dates before deleting, which makes it the model to follow. For the others, deleting the cache directory is safe and costs a download, so the only question is your connection.

huggingface-cli scan-cache
huggingface-cli delete-cache

If local models rather than framework caches are the bulk, model files and where each tool keeps them covers the larger case.

Common questions

Where does PyTorch cache pretrained models?

In ~/.cache/torch by default, or wherever TORCH_HOME points. It holds downloaded weights and hub checkouts, all of which are fetched again if removed.

How do I move ML caches to an external drive?

Set the environment variables each library respects, such as HF_HOME and TORCH_HOME, to a path on the external drive. It is a better long term answer than clearing the caches repeatedly.

Is it safe to delete the Hugging Face cache?

Yes, everything in it is downloadable again. Use huggingface-cli scan-cache and delete-cache, which show sizes and last used dates so you can remove selectively.

Why do datasets appear twice on my Mac?

Because dataset libraries keep the raw download and a prepared copy in their own format. The prepared version is regenerated from the raw archive, so keeping one of the two is usually enough.

Read next