-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_onnx_arm64.sh
More file actions
418 lines (367 loc) · 13.1 KB
/
Copy pathbuild_onnx_arm64.sh
File metadata and controls
418 lines (367 loc) · 13.1 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#!/bin/bash
# ============================================================
# ONNX Runtime GPU Build Script for ARM64
# ============================================================
# This script builds ONNX Runtime from source with CUDA support
# for ARM64 systems (e.g., NVIDIA Jetson, Grace Blackwell).
#
# WARNING: This build process takes 30-60+ minutes and requires
# several GB of disk space.
#
# Prerequisites:
# - NVIDIA GPU with CUDA support
# - CUDA Toolkit installed
# - cuDNN installed
# - CMake 3.26+
# - Python 3.10+
# - Build tools (gcc, g++, make)
# ============================================================
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BUILD_DIR="${SCRIPT_DIR}/onnxruntime_build"
ONNX_VERSION="v1.23.2" # Latest stable release
detect_cuda_architectures() {
local cap_lines=""
local cap=""
local arch=""
local arch_values=()
cap_lines="$(nvidia-smi --query-gpu=compute_cap --format=csv,noheader 2>/dev/null | tr -d '[:space:]' || true)"
if [ -z "$cap_lines" ]; then
cap_lines="$(nvidia-smi -q 2>/dev/null | grep -oE 'CUDA Compute Capability[[:space:]]*:[[:space:]]*[0-9]+\.[0-9]+' | awk -F: '{print $2}' | tr -d '[:space:]' || true)"
fi
if [ -n "$cap_lines" ]; then
while IFS= read -r cap; do
[ -n "$cap" ] || continue
if [[ "$cap" =~ ^([0-9]+)\.([0-9]+)$ ]]; then
arch="${BASH_REMATCH[1]}${BASH_REMATCH[2]}"
if [[ " ${arch_values[*]} " != *" ${arch} "* ]]; then
arch_values+=("$arch")
fi
fi
done <<< "$cap_lines"
fi
if [ ${#arch_values[@]} -eq 0 ]; then
# Safe default set for recent high-end GPUs including Blackwell.
arch_values=("90" "100")
fi
(IFS=';'; echo "${arch_values[*]}")
}
echo ""
echo "============================================================"
echo "ONNX RUNTIME GPU BUILD FOR ARM64"
echo "============================================================"
echo ""
echo -e "${YELLOW}WARNING: This process takes 30-60+ minutes and requires${NC}"
echo -e "${YELLOW}several GB of disk space. Only proceed if you need GPU${NC}"
echo -e "${YELLOW}acceleration for WD Tagger models on ARM64.${NC}"
echo ""
# Check architecture
ARCH=$(uname -m)
if [ "$ARCH" != "aarch64" ] && [ "$ARCH" != "arm64" ]; then
echo -e "${RED}[ERROR] This script is for ARM64 systems only.${NC}"
echo " Detected architecture: $ARCH"
echo " For x86_64, use: pip install onnxruntime-gpu"
exit 1
fi
echo -e "${GREEN}[OK] ARM64 architecture detected${NC}"
# Check for virtual environment
if [ ! -d "${SCRIPT_DIR}/venv" ]; then
echo -e "${RED}[ERROR] Virtual environment not found.${NC}"
echo " Please run ./setup.sh first."
exit 1
fi
echo -e "${GREEN}[OK] Virtual environment found${NC}"
# Activate virtual environment
source "${SCRIPT_DIR}/venv/bin/activate"
# Check Python version
PYTHON_VERSION=$(python3 --version 2>&1 | awk '{print $2}')
PYTHON_MAJOR=$(python3 -c 'import sys; print(sys.version_info.major)')
PYTHON_MINOR=$(python3 -c 'import sys; print(sys.version_info.minor)')
echo -e "${GREEN}[OK] Python ${PYTHON_VERSION}${NC}"
# Check for CUDA
echo ""
echo -e "${BLUE}Checking prerequisites...${NC}"
echo ""
if ! command -v nvcc &> /dev/null; then
echo -e "${RED}[ERROR] CUDA Toolkit not found (nvcc not in PATH).${NC}"
echo ""
echo "Please install CUDA Toolkit:"
echo " Ubuntu/Debian: sudo apt install nvidia-cuda-toolkit"
echo " Or download from: https://developer.nvidia.com/cuda-downloads"
echo ""
echo "After installation, ensure nvcc is in your PATH:"
echo " export PATH=/usr/local/cuda/bin:\$PATH"
exit 1
fi
CUDA_VERSION=$(nvcc --version | grep "release" | awk '{print $5}' | cut -d',' -f1)
echo -e "${GREEN}[OK] CUDA Toolkit ${CUDA_VERSION}${NC}"
# Detect CUDA home
if [ -z "$CUDA_HOME" ]; then
if [ -d "/usr/local/cuda" ]; then
export CUDA_HOME="/usr/local/cuda"
elif [ -d "/opt/cuda" ]; then
export CUDA_HOME="/opt/cuda"
else
CUDA_HOME="$(dirname "$(dirname "$(command -v nvcc)")")"
fi
fi
echo " CUDA_HOME: ${CUDA_HOME}"
CUDA_ARCHITECTURES="$(detect_cuda_architectures)"
echo " CUDA Architectures: ${CUDA_ARCHITECTURES}"
CCCL_INCLUDE=""
for cccl_path in \
"${CUDA_HOME}/targets/sbsa-linux/include/cccl" \
"${CUDA_HOME}/targets/aarch64-linux/include/cccl" \
"${CUDA_HOME}/include/cccl"
do
if [ -d "$cccl_path" ]; then
CCCL_INCLUDE="$cccl_path"
break
fi
done
if [ -n "$CCCL_INCLUDE" ]; then
echo " CCCL Include: ${CCCL_INCLUDE}"
else
echo -e "${YELLOW} [INFO] CCCL include path not found explicitly; using default CUDA include paths${NC}"
fi
# Check for cuDNN
CUDNN_FOUND=false
CUDNN_INCLUDE=""
CUDNN_LIB=""
# Check common cuDNN locations (including ARM64-specific paths)
CUDNN_SEARCH_PATHS=(
"${CUDA_HOME}"
"/usr"
"/usr/local/cudnn"
"/opt/cudnn"
)
CUDNN_INCLUDE_SUBDIRS=(
"include"
"include/aarch64-linux-gnu"
"include/x86_64-linux-gnu"
)
for cudnn_path in "${CUDNN_SEARCH_PATHS[@]}"; do
for include_subdir in "${CUDNN_INCLUDE_SUBDIRS[@]}"; do
if [ -f "${cudnn_path}/${include_subdir}/cudnn.h" ] || [ -f "${cudnn_path}/${include_subdir}/cudnn_version.h" ]; then
CUDNN_INCLUDE="${cudnn_path}/${include_subdir}"
# Find matching library path
if [ -d "${cudnn_path}/lib64" ]; then
CUDNN_LIB="${cudnn_path}/lib64"
elif [ -d "${cudnn_path}/lib/aarch64-linux-gnu" ]; then
CUDNN_LIB="${cudnn_path}/lib/aarch64-linux-gnu"
elif [ -d "${cudnn_path}/lib/x86_64-linux-gnu" ]; then
CUDNN_LIB="${cudnn_path}/lib/x86_64-linux-gnu"
elif [ -d "${cudnn_path}/lib" ]; then
CUDNN_LIB="${cudnn_path}/lib"
fi
if [ -n "$CUDNN_LIB" ]; then
CUDNN_FOUND=true
break 2
fi
fi
done
done
if [ "$CUDNN_FOUND" = false ]; then
echo -e "${RED}[ERROR] cuDNN not found.${NC}"
echo ""
echo "Please install cuDNN:"
echo " Ubuntu/Debian: sudo apt install libcudnn8-dev"
echo " Or download from: https://developer.nvidia.com/cudnn"
exit 1
fi
echo -e "${GREEN}[OK] cuDNN found${NC}"
echo " Include: ${CUDNN_INCLUDE}"
echo " Library: ${CUDNN_LIB}"
# Check for CMake
if ! command -v cmake &> /dev/null; then
echo -e "${RED}[ERROR] CMake not found.${NC}"
echo "Please install CMake 3.26+:"
echo " Ubuntu/Debian: sudo apt install cmake"
echo " Or: pip install cmake"
exit 1
fi
CMAKE_VERSION=$(cmake --version | head -n1 | awk '{print $3}')
echo -e "${GREEN}[OK] CMake ${CMAKE_VERSION}${NC}"
# Check for build tools
if ! command -v gcc &> /dev/null; then
echo -e "${RED}[ERROR] GCC not found.${NC}"
echo "Please install build tools:"
echo " Ubuntu/Debian: sudo apt install build-essential"
exit 1
fi
GCC_VERSION=$(gcc --version | head -n1 | awk '{print $NF}')
echo -e "${GREEN}[OK] GCC ${GCC_VERSION}${NC}"
# Check for git
if ! command -v git &> /dev/null; then
echo -e "${RED}[ERROR] Git not found.${NC}"
echo "Please install git:"
echo " Ubuntu/Debian: sudo apt install git"
exit 1
fi
echo -e "${GREEN}[OK] Git installed${NC}"
# Check for Eigen (required to avoid download issues)
if [ ! -f "/usr/share/eigen3/cmake/Eigen3Config.cmake" ]; then
echo -e "${RED}[ERROR] Eigen3 not found.${NC}"
echo "Please install Eigen3:"
echo " Ubuntu/Debian: sudo apt install libeigen3-dev"
exit 1
fi
echo -e "${GREEN}[OK] Eigen3 installed${NC}"
# Check available disk space (need at least 10GB)
AVAILABLE_SPACE=$(df -BG "${SCRIPT_DIR}" | tail -1 | awk '{print $4}' | sed 's/G//')
if [ "$AVAILABLE_SPACE" -lt 10 ]; then
echo -e "${RED}[ERROR] Insufficient disk space.${NC}"
echo " Available: ${AVAILABLE_SPACE}GB, Required: 10GB+"
exit 1
fi
echo -e "${GREEN}[OK] Disk space: ${AVAILABLE_SPACE}GB available${NC}"
# Check available memory (recommend at least 8GB)
AVAILABLE_MEM=$(free -g | awk '/^Mem:/{print $7}')
if [ "$AVAILABLE_MEM" -lt 4 ]; then
echo -e "${YELLOW}[WARNING] Low available memory: ${AVAILABLE_MEM}GB${NC}"
echo " Build may fail or be very slow. 8GB+ recommended."
fi
echo ""
echo "============================================================"
echo "All prerequisites satisfied. Ready to build."
echo "============================================================"
echo ""
echo "Build configuration:"
echo " - ONNX Runtime version: ${ONNX_VERSION}"
echo " - CUDA version: ${CUDA_VERSION}"
echo " - Python version: ${PYTHON_VERSION}"
echo " - Build directory: ${BUILD_DIR}"
echo ""
read -p "Continue with build? This will take 30-60+ minutes. (y/n): " CONFIRM
if [ "$CONFIRM" != "y" ] && [ "$CONFIRM" != "Y" ]; then
echo "Build cancelled."
exit 0
fi
echo ""
echo -e "${BLUE}[1/5] Preparing build directory...${NC}"
# Clean up any previous build
if [ -d "${BUILD_DIR}" ]; then
echo " Removing previous build directory..."
rm -rf "${BUILD_DIR}"
fi
mkdir -p "${BUILD_DIR}"
cd "${BUILD_DIR}"
echo ""
echo -e "${BLUE}[2/5] Cloning ONNX Runtime repository...${NC}"
echo " This may take a few minutes..."
git clone --recursive --branch ${ONNX_VERSION} --depth 1 https://github.com/microsoft/onnxruntime.git
cd onnxruntime
echo ""
echo -e "${BLUE}[3/5] Installing Python build dependencies...${NC}"
pip install numpy packaging wheel
echo ""
echo -e "${BLUE}[4/5] Building ONNX Runtime with CUDA support...${NC}"
echo ""
echo -e "${YELLOW}This will take 30-60+ minutes. Please be patient.${NC}"
echo "You can monitor CPU/memory usage in another terminal with: htop"
echo ""
# Determine number of parallel jobs (use half of available cores to avoid OOM)
NUM_CORES=$(nproc)
PARALLEL_JOBS=$((NUM_CORES / 2))
if [ "$PARALLEL_JOBS" -lt 1 ]; then
PARALLEL_JOBS=1
fi
echo "Using ${PARALLEL_JOBS} parallel jobs (of ${NUM_CORES} cores)"
echo ""
# Build ONNX Runtime
# Using --config Release for optimized build
# --build_wheel to create pip-installable wheel
# --skip_tests to save time
# --parallel for faster compilation
# Use system Eigen to avoid download issues with hash mismatches
BUILD_CMD=(
./build.sh
--config Release
--build_wheel
--skip_tests
--parallel "${PARALLEL_JOBS}"
--use_cuda
--cuda_home "${CUDA_HOME}"
--cudnn_home "$(dirname "${CUDNN_INCLUDE}")"
--cmake_extra_defines "CMAKE_CUDA_ARCHITECTURES=${CUDA_ARCHITECTURES}"
--cmake_extra_defines "Eigen3_DIR=/usr/share/eigen3/cmake"
--cmake_extra_defines "onnxruntime_USE_PREINSTALLED_EIGEN=ON"
--cmake_extra_defines "onnxruntime_ENABLE_CPUINFO=OFF"
--cmake_extra_defines "onnxruntime_DEV_MODE=OFF"
)
if [ -n "$CCCL_INCLUDE" ]; then
BUILD_CMD+=(
--cmake_extra_defines "CMAKE_CXX_FLAGS=-Wno-error=deprecated-declarations -I${CCCL_INCLUDE}"
--cmake_extra_defines "CMAKE_CUDA_FLAGS=-Wno-deprecated-declarations -I${CCCL_INCLUDE}"
)
else
BUILD_CMD+=(
--cmake_extra_defines "CMAKE_CXX_FLAGS=-Wno-error=deprecated-declarations"
--cmake_extra_defines "CMAKE_CUDA_FLAGS=-Wno-deprecated-declarations"
)
fi
"${BUILD_CMD[@]}"
if [ $? -ne 0 ]; then
echo ""
echo -e "${RED}[ERROR] Build failed!${NC}"
echo ""
echo "Common issues:"
echo " - Out of memory: Try closing other applications or add swap"
echo " - Missing dependencies: Check error messages above"
echo " - CUDA/cuDNN mismatch: Ensure compatible versions"
echo ""
echo "For help, see: https://onnxruntime.ai/docs/build/eps.html#cuda"
exit 1
fi
echo ""
echo -e "${BLUE}[5/5] Installing built wheel...${NC}"
# Find and install the wheel
WHEEL_PATH=$(find "${BUILD_DIR}/onnxruntime/build/Linux/Release/dist" -name "*.whl" | head -1)
if [ -z "$WHEEL_PATH" ]; then
echo -e "${RED}[ERROR] Could not find built wheel.${NC}"
exit 1
fi
echo " Found wheel: $(basename ${WHEEL_PATH})"
# Uninstall existing onnxruntime
pip uninstall -y onnxruntime onnxruntime-gpu 2>/dev/null || true
# Install the new wheel
pip install "${WHEEL_PATH}"
if [ $? -ne 0 ]; then
echo -e "${RED}[ERROR] Failed to install wheel.${NC}"
exit 1
fi
echo ""
echo "============================================================"
echo -e "${GREEN}BUILD COMPLETE!${NC}"
echo "============================================================"
echo ""
# Verify installation
echo "Verifying installation..."
python3 << 'EOF'
import onnxruntime as ort
print(f"ONNX Runtime Version: {ort.__version__}")
providers = ort.get_available_providers()
print(f"Available Providers: {providers}")
if "CUDAExecutionProvider" in providers:
print("\n[SUCCESS] CUDA support is enabled!")
else:
print("\n[WARNING] CUDA provider not found. Build may have issues.")
EOF
echo ""
echo "You can now use WD Tagger with GPU acceleration."
echo ""
# Offer to clean up build directory
echo "The build directory uses several GB of disk space."
read -p "Delete build directory to free space? (y/n): " CLEANUP
if [ "$CLEANUP" = "y" ] || [ "$CLEANUP" = "Y" ]; then
rm -rf "${BUILD_DIR}"
echo "Build directory removed."
fi
echo ""
echo "Done!"