user commited on
Commit
ae504d9
·
1 Parent(s): 629637c

weight compression script

Browse files
Files changed (1) hide show
  1. compress_weights.bash +38 -0
compress_weights.bash ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+
3
+ # this script generates compressed blobs for a model into huggingface's cache, to be transferred elsewhere and decompressed
4
+ # the size is reduced by about 22%
5
+
6
+ LEVEL=-4
7
+ #LEVEL=-9
8
+ #LEVEL=--adapt=min=1,max=22
9
+
10
+ model_id="$1"
11
+ revision="$2"
12
+ repo_url=https://huggingface.co/"$model_id"
13
+ clonepath="$(basename "$repo_url")"
14
+ cachepath="${HOME}/.cache/huggingface/hub/models--${model_id//\//--}/blobs"
15
+ HF_TOKEN="$(<${HOME}/.cache/huggingface/token)"
16
+ GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/"$1" "$clonepath"
17
+ mkdir -p "$cachepath"
18
+ cd "$clonepath"
19
+ git checkout "$revision" || exit -1
20
+ filecount=$(ls *.safetensors | wc -l)
21
+ for file in *.safetensors
22
+ do
23
+ file_url="$repo_url"/resolve/"$revision"/"$file"
24
+ export $(cat "$file" | tr \ =) # sets oid and size
25
+ blobname=${oid##*:}
26
+ outfile="$cachepath"/"$blobname".zst
27
+ if [ -e "$outfile" ]; then
28
+ zstd -l "$outfile" && { echo "$file" 1>&3; continue; }
29
+ echo
30
+ echo "# Recompressing $file #"
31
+ echo
32
+ fi
33
+ curl -s -L -H "Authorization: Bearer $HF_TOKEN" "$file_url" | pv -cpteabm $((60*60*24)) -s "$size" -N "$file" | zstd -q --no-progress --force --size-hint="$size" --stream-size="$size" --ultra --long=31 --memory=2048MB --threads=1024 "$LEVEL" | pv -cpteabm $((60*60*24)) -s "$((size * 100/129))" -N "$blobname".zst > "$outfile"
34
+ zstd -l "$outfile"
35
+ echo
36
+ echo
37
+ echo "$file" 1>&3
38
+ done 3>&1 1>&2 | pv -lcpteabm $((60*60*24)) -s "$filecount" -N "$clonepath"