Search code examples
huggingface-transformers

How to change huggingface transformers default cache directory


The default cache directory is lack of disk capacity, I need change the configure of the default cache directory.


Solution

  • You can specify the cache directory whenever you load a model with .from_pretrained by setting the parameter cache_dir. You can also set a default location by exporting an environment variable HF_HOME each time before you use the library (i.e. before importing it!).

    Python example:

    import os
    os.environ['HF_HOME'] = '/blabla/cache/'
    

    bash example:

    export HF_HOME=/blabla/cache/
    

    windows example:

    set HF_HOME=E:\huggingface_cache
    

    Google Colab example (export via os works fine but not the bash variant. An alternative are the magic commands):

    %env HF_HOME=/blabla/cache/
    

    transformers <v4.0.0

    Use the variable TRANSFORMERS_CACHE instead of HF_HOME. You can also use it in v4.0.0 <= transformers <= v5.0.0 but starting from v4.36.0 you will see the following warning:

    FutureWarning: Using TRANSFORMERS_CACHE is deprecated and will be removed in v5 of Transformers. Use HF_HOME instead.