-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathsetup_dependencies.sh
More file actions
executable file
·652 lines (560 loc) · 22 KB
/
Copy pathsetup_dependencies.sh
File metadata and controls
executable file
·652 lines (560 loc) · 22 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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
#!/bin/bash
# Unified dependency setup script for inference backend dependencies
# This script fetches backend versions from neuriplo repo and sets up dependencies
set -e # Exit on any error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to check if command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to detect OS
detect_os() {
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
echo "linux"
elif [[ "$OSTYPE" == "darwin"* ]]; then
echo "macos"
elif [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "cygwin" ]]; then
echo "windows"
else
echo "unknown"
fi
}
# Function to get dependency root directory
get_dependency_root() {
local os=$(detect_os)
if [[ "$os" == "windows" ]]; then
echo "$USERPROFILE/dependencies"
else
echo "$HOME/dependencies"
fi
}
# Function to create directory if it doesn't exist
ensure_directory() {
if [[ ! -d "$1" ]]; then
print_status "Creating directory: $1"
mkdir -p "$1"
fi
}
# Function to download file with retry
download_with_retry() {
local url="$1"
local output="$2"
local max_retries=3
local retry_count=0
while [[ $retry_count -lt $max_retries ]]; do
# Use curl for better URL handling (especially for URLs with + characters)
if curl -L --fail -o "$output" "$url"; then
return 0
fi
retry_count=$((retry_count + 1))
print_warning "Download failed, retrying... ($retry_count/$max_retries)"
sleep 2
done
print_error "Failed to download $url after $max_retries attempts"
return 1
}
# Function to fetch versions from neuriplo repo
fetch_backend_versions() {
local local_versions_file="versions.neuriplo.env"
# Check if local versions file exists (should be created by update_backend_versions.sh)
if [[ -f "$local_versions_file" ]]; then
print_status "Using local neuriplo versions from $local_versions_file"
source "$local_versions_file"
else
print_error "Local neuriplo versions file not found: $local_versions_file"
print_status "This should have been created by update_backend_versions.sh"
print_status "Please check if the script ran successfully or try manually:"
print_status "./scripts/update_backend_versions.sh"
return 1
fi
print_status "Fetched neuriplo backend versions:"
print_status "ONNX Runtime: $ONNX_RUNTIME_VERSION"
print_status "TensorRT: $TENSORRT_VERSION"
print_status "LibTorch: $PYTORCH_VERSION"
print_status "OpenVINO: $OPENVINO_VERSION"
print_status "TensorFlow: $TENSORFLOW_VERSION"
if [[ -n "${LITERT_VERSION:-}" ]]; then
print_status "LiteRT: $LITERT_VERSION"
fi
print_status "CUDA: $CUDA_VERSION"
}
# Function to setup ONNX Runtime
setup_onnx_runtime() {
local version="$ONNX_RUNTIME_VERSION"
local dependency_root=$(get_dependency_root)
local install_dir="$dependency_root/onnxruntime-linux-x64-gpu-$version"
print_status "Setting up ONNX Runtime $version (for neuriplo library)..."
if [[ -d "$install_dir" ]]; then
print_warning "ONNX Runtime already exists at $install_dir"
return 0
fi
ensure_directory "$dependency_root"
local temp_dir=$(mktemp -d)
cd "$temp_dir"
local filename="onnxruntime-linux-x64-gpu-$version.tgz"
local url="https://github.com/microsoft/onnxruntime/releases/download/v$version/$filename"
print_status "Downloading ONNX Runtime..."
if download_with_retry "$url" "$filename"; then
print_status "Extracting ONNX Runtime..."
tar -xzf "$filename"
mv "onnxruntime-linux-x64-gpu-$version" "$install_dir"
print_success "ONNX Runtime installed to $install_dir"
else
print_error "Failed to setup ONNX Runtime"
return 1
fi
cd - > /dev/null
rm -rf "$temp_dir"
}
# Function to setup TensorRT
setup_tensorrt() {
local version="$TENSORRT_VERSION"
local cuda_version="$CUDA_VERSION"
local dependency_root=$(get_dependency_root)
local install_dir="$dependency_root/TensorRT-$version"
print_status "Setting up TensorRT $version (for neuriplo library)..."
if [[ -d "$install_dir" ]]; then
print_warning "TensorRT already exists at $install_dir"
return 0
fi
ensure_directory "$dependency_root"
local temp_dir=$(mktemp -d)
cd "$temp_dir"
local filename="TensorRT-$version.Linux.x86_64-gnu.cuda-$cuda_version.tar.gz"
local version_short=$(echo "$version" | cut -d. -f1-3)
local url="https://developer.nvidia.com/downloads/compute/machine-learning/tensorrt/${version_short}/tars/$filename"
print_status "Downloading TensorRT..."
if download_with_retry "$url" "$filename"; then
print_status "Extracting TensorRT..."
tar -xzf "$filename"
mv "TensorRT-$version" "$install_dir"
print_success "TensorRT installed to $install_dir"
else
print_error "Failed to setup TensorRT"
return 1
fi
cd - > /dev/null
rm -rf "$temp_dir"
}
# Function to setup LibTorch
setup_libtorch() {
local version="$PYTORCH_VERSION"
local dependency_root=$(get_dependency_root)
local install_dir="$dependency_root/libtorch"
print_status "Setting up LibTorch $version (for neuriplo library)..."
if [[ -d "$install_dir" ]]; then
print_warning "LibTorch already exists at $install_dir"
return 0
fi
ensure_directory "$dependency_root"
local temp_dir=$(mktemp -d)
cd "$temp_dir"
# Determine compute platform based on CUDA version and user preference
local compute_platform=${COMPUTE_PLATFORM:-cpu}
# If user wants GPU, automatically determine CUDA version from versions file
if [[ "$compute_platform" == "gpu" || "$compute_platform" == "cuda" ]]; then
if [[ -n "$CUDA_VERSION" ]]; then
# Map CUDA version to PyTorch compute platform
case "$CUDA_VERSION" in
"12.8")
compute_platform="cu128"
print_status "Using CUDA $CUDA_VERSION -> compute platform: $compute_platform"
;;
"12.6"|"12.7")
compute_platform="cu126"
print_status "Using CUDA $CUDA_VERSION -> compute platform: $compute_platform"
;;
"12.4"|"12.5")
compute_platform="cu118"
print_status "Using CUDA $CUDA_VERSION -> compute platform: $compute_platform"
;;
"12.0"|"12.1"|"12.2"|"12.3")
compute_platform="cu118"
print_status "Using CUDA $CUDA_VERSION -> compute platform: $compute_platform"
;;
"11.8")
compute_platform="cu118"
print_status "Using CUDA $CUDA_VERSION -> compute platform: $compute_platform"
;;
*)
print_warning "Unknown CUDA version $CUDA_VERSION, using cu118 as fallback"
compute_platform="cu118"
;;
esac
else
print_error "CUDA version not found in versions.neuriplo.env"
print_status "Please set CUDA_VERSION in versions.neuriplo.env or use --compute-platform explicitly"
return 1
fi
fi
local filename="libtorch-cxx11-abi-shared-with-deps-$version+$compute_platform.zip"
# URL encode the filename (replace + with %2B)
local encoded_filename=$(echo "$filename" | sed 's/+/%2B/g')
local url="https://download.pytorch.org/libtorch/$compute_platform/$encoded_filename"
print_status "Downloading LibTorch ($compute_platform)..."
if download_with_retry "$url" "$filename"; then
print_status "Extracting LibTorch..."
unzip -q "$filename"
mv "libtorch" "$install_dir"
print_success "LibTorch installed to $install_dir"
else
print_error "Failed to setup LibTorch"
return 1
fi
cd - > /dev/null
rm -rf "$temp_dir"
}
# Function to setup OpenVINO
setup_openvino() {
local version="$OPENVINO_VERSION"
local dependency_root=$(get_dependency_root)
local dir="$dependency_root/openvino_$version"
print_status "Setting up OpenVINO $version (for neuriplo library)..."
if [[ -d "$dir" && "$FORCE" != "true" ]]; then
print_warning "OpenVINO already exists at $dir"
return 0
fi
print_status "Installing OpenVINO $version to $dir..."
mkdir -p "$dependency_root" && cd "$dependency_root"
# Download OpenVINO toolkit
local tarball="openvino_2025.2.0.tgz"
if [[ ! -f "$tarball" ]]; then
print_status "Downloading OpenVINO toolkit..."
curl -L "https://storage.openvinotoolkit.org/repositories/openvino/packages/2025.2/linux/openvino_toolkit_ubuntu24_2025.2.0.19140.c01cd93e24d_x86_64.tgz" --output "$tarball"
fi
# Extract and move to final location
print_status "Extracting OpenVINO..."
tar -xf "$tarball"
if [[ -d "$dir" ]]; then
rm -rf "$dir"
fi
mv openvino_toolkit_ubuntu24_2025.2.0.19140.c01cd93e24d_x86_64 "$dir"
rm -f "$tarball"
# Create a local Python virtual environment for OpenVINO tools
print_status "Setting up OpenVINO Python tools..."
local venv_dir="$dir/python_env"
python3 -m venv "$venv_dir"
source "$venv_dir/bin/activate"
pip install openvino-dev
deactivate
# Create wrapper script for ovc
mkdir -p "$dir/bin"
cat > "$dir/bin/ovc" << 'EOF'
#!/bin/bash
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
VENV_DIR="$SCRIPT_DIR/../python_env"
source "$VENV_DIR/bin/activate"
ovc "$@"
deactivate
EOF
chmod +x "$dir/bin/ovc"
print_success "OpenVINO $version installed successfully to $dir"
}
# Function to setup TensorFlow
setup_tensorflow() {
local version="$TENSORFLOW_VERSION"
local dependency_root=$(get_dependency_root)
local tensorflow_dir="$dependency_root/tensorflow"
local venv_dir="$dependency_root/tensorflow_env"
print_status "Setting up TensorFlow $version (for neuriplo library)..."
# Check Python
if ! command -v python3 &>/dev/null; then
print_error "Python 3 required for TensorFlow installation"
return 1
fi
# Check if already installed
if [[ -d "$tensorflow_dir" ]]; then
print_warning "TensorFlow C++ libraries already installed at $tensorflow_dir"
return 0
fi
print_status "Creating Python virtual environment..."
mkdir -p "$dependency_root"
if [[ ! -d "$venv_dir" ]]; then
python3 -m venv "$venv_dir" || {
print_error "Failed to create Python virtual environment"
return 1
}
fi
print_status "Activating virtual environment and installing TensorFlow..."
source "$venv_dir/bin/activate" || {
print_error "Failed to activate virtual environment"
return 1
}
# Install TensorFlow
pip install --upgrade pip || {
print_error "Failed to upgrade pip"
return 1
}
pip install "tensorflow==$version" || {
print_error "TensorFlow installation failed"
return 1
}
# Verify installation
print_status "Verifying TensorFlow installation..."
python -c "import tensorflow as tf; print(f'TensorFlow version: {tf.__version__}')" || {
print_error "TensorFlow verification failed"
return 1
}
print_status "Setting up TensorFlow C++ libraries..."
# Find TensorFlow site-packages in the virtual environment
local python_version=$(source "$venv_dir/bin/activate" && python -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")')
local tf_site_packages="$venv_dir/lib/python$python_version/site-packages/tensorflow"
if [[ ! -d "$tf_site_packages" ]]; then
print_error "TensorFlow site-packages not found at $tf_site_packages"
print_status "Available directories in site-packages:"
ls -la "$venv_dir/lib/python$python_version/site-packages/" || true
return 1
fi
# Create TensorFlow C++ directory
rm -rf "$tensorflow_dir"
mkdir -p "$tensorflow_dir/lib" "$tensorflow_dir/include"
# Copy libraries (activate venv for each operation)
print_status "Copying TensorFlow libraries..."
if [[ -f "$tf_site_packages/libtensorflow_cc.so.2" ]]; then
cp "$tf_site_packages/libtensorflow_cc.so.2" "$tensorflow_dir/lib/"
ln -sf "libtensorflow_cc.so.2" "$tensorflow_dir/lib/libtensorflow_cc.so"
else
print_warning "libtensorflow_cc.so.2 not found, searching for alternatives..."
find "$tf_site_packages" -name "libtensorflow_cc.so*" -exec cp {} "$tensorflow_dir/lib/" \; || {
print_warning "No libtensorflow_cc.so found in $tf_site_packages"
print_status "Available files in TensorFlow site-packages:"
find "$tf_site_packages" -name "*.so*" | head -10 || true
}
fi
if [[ -f "$tf_site_packages/libtensorflow_framework.so.2" ]]; then
cp "$tf_site_packages/libtensorflow_framework.so.2" "$tensorflow_dir/lib/"
ln -sf "libtensorflow_framework.so.2" "$tensorflow_dir/lib/libtensorflow_framework.so"
else
print_warning "libtensorflow_framework.so.2 not found, searching for alternatives..."
find "$tf_site_packages" -name "libtensorflow_framework.so*" -exec cp {} "$tensorflow_dir/lib/" \; || {
print_warning "No libtensorflow_framework.so found in $tf_site_packages"
}
fi
# Copy headers
if [[ -d "$tf_site_packages/include" ]]; then
cp -r "$tf_site_packages/include/"* "$tensorflow_dir/include/"
fi
# Copy additional headers if available
if [[ -d "$tf_site_packages/core" ]]; then
mkdir -p "$tensorflow_dir/include/tensorflow/"
cp -r "$tf_site_packages/core" "$tensorflow_dir/include/tensorflow/"
fi
if [[ -d "$tf_site_packages/cc" ]]; then
mkdir -p "$tensorflow_dir/include/tensorflow/"
cp -r "$tf_site_packages/cc" "$tensorflow_dir/include/tensorflow/"
fi
# Create pkg-config file
mkdir -p "$tensorflow_dir/lib/pkgconfig"
cat > "$tensorflow_dir/lib/pkgconfig/tensorflow.pc" << EOF
prefix=$tensorflow_dir
libdir=\${prefix}/lib
includedir=\${prefix}/include
Name: TensorFlow
Version: $version
Libs: -L\${libdir} -ltensorflow_cc -ltensorflow_framework
Cflags: -I\${includedir}
EOF
# Setup environment
local env_file="$dependency_root/setup_env.sh"
if [[ -f "$env_file" ]]; then
grep -v "TensorFlow\|TENSORFLOW" "$env_file" > "${env_file}.tmp" 2>/dev/null || true
mv "${env_file}.tmp" "$env_file" 2>/dev/null || true
fi
cat >> "$env_file" << EOF
export TENSORFLOW_DIR="$tensorflow_dir"
export LD_LIBRARY_PATH="\$TENSORFLOW_DIR/lib:\${LD_LIBRARY_PATH:-}"
export PKG_CONFIG_PATH="\$TENSORFLOW_DIR/lib/pkgconfig:\${PKG_CONFIG_PATH:-}"
EOF
print_success "TensorFlow C++ library installed successfully at $tensorflow_dir"
print_status "You can now build the project with:"
print_status "cmake -DDEFAULT_BACKEND=LIBTENSORFLOW .."
print_status "Source $env_file to use TensorFlow environment variables"
}
setup_litert() {
local script_dir_here
script_dir_here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
bash "${script_dir_here}/setup_litert.sh"
}
# Function to check system dependencies
check_system_dependencies() {
print_status "Checking system dependencies..."
local missing_deps=()
# Check for required commands
local required_commands=("cmake" "curl" "tar" "unzip")
for cmd in "${required_commands[@]}"; do
if ! command_exists "$cmd"; then
missing_deps+=("$cmd")
fi
done
# Check for required packages (Linux)
if [[ "$(detect_os)" == "linux" ]]; then
if ! dpkg -l | grep -q "libopencv-dev"; then
missing_deps+=("libopencv-dev")
fi
if ! dpkg -l | grep -q "libgoogle-glog-dev"; then
missing_deps+=("libgoogle-glog-dev")
fi
fi
if [[ ${#missing_deps[@]} -gt 0 ]]; then
print_error "Missing system dependencies: ${missing_deps[*]}"
print_status "Please install them using your package manager:"
if [[ "$(detect_os)" == "linux" ]]; then
print_status "sudo apt update && sudo apt install -y curl ${missing_deps[*]}"
fi
return 1
fi
print_success "All system dependencies are available"
}
# Function to show usage
show_usage() {
echo "Usage: $0 [OPTIONS]"
echo ""
echo "This script sets up inference backend dependencies for the neuriplo library."
echo "It automatically ensures version files exist and installs the selected backend dependencies."
echo ""
echo "Options:"
echo " --backend <backend> Specify the inference backend to setup"
echo " Supported: opencv_dnn, onnx_runtime, tensorrt, libtorch, openvino, tensorflow, litert, executorch, all"
echo " Default: opencv_dnn (no setup required)"
echo " --compute-platform <platform> For LibTorch: cpu, gpu/cuda, cu118, cu121, rocm6.0"
echo " Default: cpu"
echo " Note: 'gpu' and 'cuda' are equivalent and auto-detect CUDA version"
echo " --help Show this help message"
echo ""
echo "Examples:"
echo " $0 # Setup default backend (OPENCV_DNN)"
echo " $0 --backend onnx_runtime # Setup ONNX Runtime"
echo " $0 --backend libtorch --compute-platform gpu # Setup LibTorch with auto CUDA detection"
echo " $0 --backend libtorch --compute-platform cuda # Same as above"
echo " $0 --backend all # Setup all inference backends"
echo ""
echo "Note: This script automatically calls update_backend_versions.sh to ensure version files exist."
echo "Note: These dependencies are used by the neuriplo library, not this project directly."
}
# Main script
main() {
local backend="opencv_dnn" # Default to OPENCV_DNN
local compute_platform="cpu"
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
--backend)
backend="$2"
shift 2
;;
--compute-platform)
compute_platform="$2"
shift 2
;;
--help)
show_usage
exit 0
;;
*)
print_error "Unknown option: $1"
show_usage
exit 1
;;
esac
done
# Set compute platform for LibTorch
export COMPUTE_PLATFORM="$compute_platform"
print_status "Starting inference backend dependency setup..."
print_status "OS detected: $(detect_os)"
print_status "Dependency root: $(get_dependency_root)"
print_status "Selected backend: $backend"
print_status "Note: These dependencies are for the neuriplo library"
# Automatically ensure version files exist
print_status "Ensuring version files are available..."
if [[ -f "scripts/update_backend_versions.sh" ]]; then
if ! ./scripts/update_backend_versions.sh; then
print_warning "Failed to update version files, continuing with available versions..."
fi
else
print_warning "update_backend_versions.sh not found, continuing with available versions..."
fi
# Check system dependencies first
if ! check_system_dependencies; then
exit 1
fi
# Fetch backend versions from neuriplo repo
if ! fetch_backend_versions; then
exit 1
fi
# Setup based on backend
case "$backend" in
opencv_dnn)
print_status "OPENCV_DNN is the default backend - no additional setup required"
print_success "System dependencies (OpenCV) are already validated"
;;
onnx_runtime)
setup_onnx_runtime
;;
tensorrt)
setup_tensorrt
;;
libtorch)
setup_libtorch
;;
openvino)
setup_openvino
;;
tensorflow)
setup_tensorflow
;;
litert)
setup_litert
;;
executorch)
local script_dir_here
script_dir_here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
bash "${script_dir_here}/setup_executorch.sh"
;;
all)
setup_onnx_runtime
setup_tensorrt
setup_libtorch
setup_openvino
setup_tensorflow
setup_litert
local script_dir_here
script_dir_here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
bash "${script_dir_here}/setup_executorch.sh"
;;
*)
print_error "Unknown backend: $backend"
print_status "Supported backends: opencv_dnn, onnx_runtime, tensorrt, libtorch, openvino, tensorflow, litert, executorch, all"
exit 1
;;
esac
print_success "Inference backend dependency setup completed successfully!"
print_status "These dependencies will be used by the neuriplo library."
print_status "You can now build the project with:"
print_status "mkdir build && cd build"
if [[ "$backend" == "opencv_dnn" ]]; then
print_status "cmake .. # Uses OPENCV_DNN by default"
else
print_status "cmake -DDEFAULT_BACKEND=${backend^^} .."
fi
print_status "cmake --build ."
}
# Run main function with all arguments
main "$@"