Datasets:

ArXiv:
File size: 3,011 Bytes
9f3bc09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/bin/bash

# Script to upload checkpoints to HuggingFace with customizable repository name
# Usage: ./upload_checkpoints.sh <parent_folder_path> <hf_repo> [<subfolder>]

set -e

if [ $# -lt 2 ] || [ $# -gt 3 ]; then
    echo "Usage: $0 <parent_folder_path> <hf_repo> [<subfolder>]"
    echo "  <parent_folder_path>: Local directory containing checkpoint folders"
    echo "  <hf_repo>: HuggingFace repository (format: username/repo-name)"
    echo "  <subfolder>: Optional subfolder path in the repository"
    echo ""
    echo "Examples:"
    echo "  Upload to root: $0 ./checkpoints myusername/my-model"
    echo "  Upload to subfolder: $0 ./checkpoints myusername/my-model checkpoints/v1"
    exit 1
fi

PARENT_FOLDER="$1"
HF_REPO="$2"
SUBFOLDER="$3" # optional subfolder path in the repository

if [ ! -d "$PARENT_FOLDER" ]; then
    echo "Error: Directory $PARENT_FOLDER does not exist"
    exit 1
fi

# Check if huggingface-cli is available
if ! command -v huggingface-cli &> /dev/null; then
    echo "Error: huggingface-cli not found. Please install it with: pip install huggingface_hub[cli]"
    exit 1
fi

# Check if logged in
if ! huggingface-cli whoami &> /dev/null; then
    echo "Error: Not logged in to HuggingFace. Run: huggingface-cli login"
    exit 1
fi

echo "Searching for checkpoint directories in $PARENT_FOLDER..."

# Find all checkpoint directories
while IFS= read -r -d '' checkpoint_dir; do
    checkpoint_name=$(basename "$checkpoint_dir")
    
    # Extract checkpoint number (e.g., checkpoint-100 -> 100)
    if [[ $checkpoint_name =~ checkpoint-([0-9]+) ]]; then
        ckpt_num="${BASH_REMATCH[1]}"
        
        # Determine the target path in the repository
        if [ -n "$SUBFOLDER" ]; then
            # Remove trailing slash if present
            SUBFOLDER="${SUBFOLDER%/}"
            target_path="$SUBFOLDER/$checkpoint_name"
        else
            target_path="$checkpoint_name"
        fi
        
        echo "=================================================="
        echo "Uploading $checkpoint_name to $HF_REPO/$target_path"
        echo "=================================================="
        
        # Create repository if it doesn't exist
        huggingface-cli repo create "$HF_REPO" --repo-type model --exist-ok
        
        # Upload checkpoint to specific path (excluding optimizer states for smaller size)
        huggingface-cli upload \
            "$HF_REPO" \
            "$checkpoint_dir" \
            "$target_path" \
            --repo-type model \
            --exclude="global_step*" \
            --exclude="*.pt*" \
            --commit-message "Upload $checkpoint_name to $target_path"
        
        echo "✅ Successfully uploaded to: https://huggingface.co/$HF_REPO/tree/main/$target_path"
        echo ""
    else
        echo "Warning: Skipping $checkpoint_name (doesn't match checkpoint-* pattern)"
    fi
done < <(find "$PARENT_FOLDER" -type d -name "checkpoint-*" -print0 | sort -z)

echo "All uploads completed!"