llm_logits / scripts /download_weights.bash
3v324v23's picture
add note to download weights
edc52b4
#!/usr/bin/env bash
# NOTE: `huggingface-cli download` already exists to do this
# fills huggingfaces cache by downloading safetensors files in parallel
# usage: $ bash download_weights.bash SOME/MODEL_ID COMMIT_HASH -j4
#PV="pv -cpteabm $((60*60*24))"
PV="pv -cpteab"
model_id="$1"
revision="$2"
shift 2
repo_url=https://huggingface.co/"$model_id"
clonepath="$(basename "$repo_url")"
cachepath="${HOME}/.cache/huggingface/hub/models--${model_id//\//--}/blobs"
if ! [ -e ${HOME}/.cache/huggingface/token ]
then
huggingface-cli login || exit -1
fi
HF_TOKEN="$(<${HOME}/.cache/huggingface/token)"
GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/"$model_id" "$clonepath"
mkdir -p "$cachepath"
cd "$clonepath"
GIT_LFS_SKIP_SMUDGE=1 git checkout "$revision" || exit -1
filecount=$(ls *.safetensors | wc -l)
export repo_url revision cachepath HF_TOKEN PV
# troubleshot by adding -vx before -c in the bash command
parallel "$@" -i bash -c \
'file_url="$repo_url"/resolve/"$revision"/"{}";\
export $(cat "{}" | tr \ =);\
blobname=${oid##*:};\
outfile="$cachepath"/"$blobname";\
set pipefail;\
if [ "$(($(stat -c %s "$outfile" 2>/dev/null)))" != "$(($size))" ]; then\
curl --silent -L --header "Authorization: Bearer $HF_TOKEN" "$file_url" |\
$PV -s "$size" -N "{}" > "$outfile" 2>/dev/tty || continue;\
fi;\
echo "$blobname" "$outfile"'\
-- *.safetensors |
$PV -l -s "$filecount" -N "$clonepath" 2>/dev/tty |
sha256sum -c && echo SUCCESS || { echo ERROR try again; false; }