Skip to content

Instantly share code, notes, and snippets.

@152334H
Created February 18, 2024 08:27
Show Gist options
  • Save 152334H/fea46c0d9f499d4ef13d40d4ed877695 to your computer and use it in GitHub Desktop.
Save 152334H/fea46c0d9f499d4ef13d40d4ed877695 to your computer and use it in GitHub Desktop.
move out a model file from the hf cache to an external folder
#!/bin/bash
# Determine the HUBDIR based on the environment variables
if [[ -n "$HF_HUB_CACHE" ]]
then HUBDIR="$HF_HUB_CACHE"
elif [[ -n "$HF_HOME" ]]
then HUBDIR="$HF_HOME/hub"
else HUBDIR="$HOME/.cache/huggingface/hub/"
fi
move_from_hf() {
if [ "$#" -lt 2 ] || [ "$#" -gt 3 ]; then
echo "Usage: move_from_hf MODEL_NAME REF [OUTDIR]"
echo "MODEL_NAME: Name of the model directory"
echo "REF: Reference path, e.g., 'refs/pr/2'"
echo "OUTDIR: Optional. Output directory where the snapshot is to be copied. Defaults to 'out'"
return 1 # Return with an error status
fi
local model_name="$1"
local ref="$2"
local outdir="${3:-out}" # Default output directory is 'out'
local base_dir="$HUBDIR/$model_name"
local ref_file="$base_dir/$ref"
if [ ! -f "$ref_file" ]; then
echo "Error: Reference file does not exist."
return 1
fi
local snapshot_hash=$(cat "$ref_file")
local snapshot_dir="$base_dir/snapshots/$snapshot_hash"
if [ ! -d "$snapshot_dir" ]; then
echo "Error: Snapshot directory does not exist."
return 1
fi
mkdir -p "$outdir" && cp -Lr "$snapshot_dir/"* "$outdir"
echo "Snapshot copied to $outdir with symlinks dereferenced."
}
_move_from_hf_autocomplete() {
local cur=${COMP_WORDS[COMP_CWORD]}
local prev=${COMP_WORDS[COMP_CWORD-1]}
local first=${COMP_WORDS[1]}
case "$COMP_CWORD" in
1) # Model name completion
local models=$(ls -d ${HUBDIR}*/ | xargs -n 1 basename)
COMPREPLY=($(compgen -W "${models}" -- "${cur}"))
;;
2) # Refs completion for the chosen model
local refs=$(find ${HUBDIR}/${first}/refs -type f 2>/dev/null | sed -e "s|${HUBDIR}/${first}/||")
COMPREPLY=($(compgen -W "${refs}" -- "${cur}"))
;;
*) ;; # do nothing
esac
}
complete -F _move_from_hf_autocomplete move_from_hf
# If the script is sourced, directly run the move_from_hf function with passed arguments
# This allows the script to be used both as a source for autocompletion and as an executable command
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]
then export -f move_from_hf
else move_from_hf "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment