-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·146 lines (125 loc) · 4.43 KB
/
setup.sh
File metadata and controls
executable file
·146 lines (125 loc) · 4.43 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/bin/bash
################################################################################
# Script: setup.sh
# Description: Automates the setup of a virtual environment and installs project
# requirements including CTransformers and GGUF weights.
################################################################################
set -euo pipefail
# Define constants and paths
CURRENT_DIR="$(pwd)"
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
VENV_DIR="$SCRIPT_DIR/venv"
MODELS_DIR="$CURRENT_DIR/models"
LLAMA2_GGUF_WEIGHTS_DIR="$MODELS_DIR/llama-2-7b-chat-gguf"
MISTRAL_GGUF_WEIGHTS_DIR="$MODELS_DIR/mistral-7b-v0.1-instruct-gguf"
# Check if Python is installed
check_python() {
if command -v python &> /dev/null; then
PYTHON_CMD="python"
elif command -v python3 &> /dev/null; then
PYTHON_CMD="python3"
else
echo "Python is not installed."
exit 1
fi
}
install_ctransformers_cuda() {
CUDA_VERSION=$(nvcc --version | grep "release" | sed -n 's/.*release \(.*\),.*/\1/p')
if [ -z "$CUDA_VERSION" ]; then
echo "CUDA is not installed or not found."
exit 1
fi
CUDA_MAJOR=$(echo "$CUDA_VERSION" | cut -d. -f1)
CUDA_MINOR=$(echo "$CUDA_VERSION" | cut -d. -f2)
if [ "$CUDA_MAJOR" -gt 12 ] || { [ "$CUDA_MAJOR" -eq 12 ] && [ "$CUDA_MINOR" -ge 2 ]; }; then
echo "Detected CUDA version >= 12.2"
pip install ctransformers[cuda] > /dev/null
else
echo "Detected CUDA version < 12.2"
CMAKE_ARGS="-DCMAKE_CUDA_COMPILER=$(which nvcc)" CT_CUBLAS=1 pip install ctransformers --no-binary ctransformers > /dev/null
fi
}
# Install CTransformers based on the specified device
install_ctransformers() {
local DEVICE="$1"
case "$DEVICE" in
cuda)
echo "Installing CTransformers for CUDA."
install_ctransformers_cuda
;;
metal)
echo "Installing CTransformers for Metal."
pip uninstall ctransformers --yes
CT_METAL=1 pip install ctransformers --no-binary ctransformers
;;
cpu)
echo "Installing CTransformers for CPU."
pip install ctransformers > /dev/null
;;
*)
echo "Unsupported DEVICE: $DEVICE"
exit 1
;;
esac
}
# Download GGUF weights for the specified model
download_gguf_weights() {
local MODEL_NAME="$1"
local DOWNLOAD_DIR
case "$MODEL_NAME" in
llama)
DOWNLOAD_DIR="$LLAMA2_GGUF_WEIGHTS_DIR"
MODEL_IDENTIFIER="TheBloke/Llama-2-7B-Chat-GGUF"
MODEL_FILE_4BIT="llama-2-7b-chat.Q4_K_M.gguf"
MODEL_FILE_8BIT="llama-2-7b-chat.Q8_0.gguf"
;;
mistral)
DOWNLOAD_DIR="$MISTRAL_GGUF_WEIGHTS_DIR"
MODEL_IDENTIFIER="TheBloke/Mistral-7B-Instruct-v0.1-GGUF"
MODEL_FILE_4BIT="mistral-7b-instruct-v0.1.Q4_K_M.gguf"
MODEL_FILE_8BIT="mistral-7b-instruct-v0.1.Q8_0.gguf"
;;
*)
echo "Invalid MODEL_NAME. Supported values: 'llama', 'mistral'"
exit 1
;;
esac
if [ ! -d "$DOWNLOAD_DIR" ]; then
huggingface-cli download "$MODEL_IDENTIFIER" "$MODEL_FILE_4BIT" --local-dir "$DOWNLOAD_DIR" --local-dir-use-symlinks False
huggingface-cli download "$MODEL_IDENTIFIER" "$MODEL_FILE_8BIT" --local-dir "$DOWNLOAD_DIR" --local-dir-use-symlinks False
else
echo "Weights for $MODEL_NAME already downloaded."
fi
}
# Main script starts here
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <DEVICE> <MODEL_NAME>"
exit 1
fi
check_python
# Define command line arguments
DEVICE="$1"
MODEL_NAME="$2"
if [ ! -d "$VENV_DIR" ]; then
"$PYTHON_CMD" -m venv "$VENV_DIR"
echo "Virtual environment '$VENV_DIR' created."
if [ -f "$VENV_DIR/bin/activate" ]; then
# shellcheck disable=SC1091
source "$VENV_DIR/bin/activate"
else
echo "Error: Unable to find virtual environment activation script."
exit 1
fi
"$PYTHON_CMD" -m pip install --upgrade pip > /dev/null
"$PYTHON_CMD" -m pip install -r "$SCRIPT_DIR/requirements.txt" --no-cache-dir > /dev/null
install_ctransformers "$DEVICE"
else
if [ -f "$VENV_DIR/bin/activate" ]; then
# shellcheck disable=SC1091
source "$VENV_DIR/bin/activate"
else
echo "Error: Unable to find virtual environment activation script."
exit 1
fi
fi
download_gguf_weights "$MODEL_NAME"