-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfigure
More file actions
executable file
·133 lines (108 loc) · 3.62 KB
/
configure
File metadata and controls
executable file
·133 lines (108 loc) · 3.62 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
#!/bin/sh
# tinytorch configure script
# Detects libtorch and generates src/Makevars for real or stub backend.
# ---- Force stub mode (for testing) ----
if [ "$TINYTORCH_STUB" = "1" ]; then
echo "* TINYTORCH_STUB=1: forcing stub backend"
cat > src/Makevars <<EOF
PKG_CPPFLAGS = -DTINYTORCH_HAVE_LIBTORCH=0
OBJECTS = stubs.o
EOF
echo "Generated src/Makevars (stub mode)"
exit 0
fi
# ---- Platform check ----
PLATFORM=$(uname -s)
case "$PLATFORM" in
Linux) ;;
*)
echo "* Non-Linux platform ($PLATFORM): using stub backend"
echo " tinytorch will install but is_available() will return FALSE"
cat > src/Makevars <<EOF
PKG_CPPFLAGS = -DTINYTORCH_HAVE_LIBTORCH=0
OBJECTS = stubs.o
EOF
echo "Generated src/Makevars (stub mode)"
exit 0
;;
esac
# ---- Search for libtorch ----
TORCH_HOME=""
TORCH_SOURCE=""
# 1. LIBTORCH_HOME env var
if [ -n "$LIBTORCH_HOME" ] && [ -d "$LIBTORCH_HOME/include/torch" ]; then
TORCH_HOME="$LIBTORCH_HOME"
TORCH_SOURCE="LIBTORCH_HOME env var"
fi
# 2. ~/.local/lib/libtorch
if [ -z "$TORCH_HOME" ] && [ -d "$HOME/.local/lib/libtorch/include/torch" ]; then
TORCH_HOME="$HOME/.local/lib/libtorch"
TORCH_SOURCE="~/.local/lib/libtorch"
fi
# 3. torch R package
if [ -z "$TORCH_HOME" ]; then
TORCH_PKG=$(Rscript -e "cat(system.file(package='torch'))" 2>/dev/null)
if [ -n "$TORCH_PKG" ] && [ -d "$TORCH_PKG/include/torch" ]; then
TORCH_HOME="$TORCH_PKG"
TORCH_SOURCE="torch R package"
fi
fi
# 4. Stub fallback
if [ -z "$TORCH_HOME" ]; then
echo "* libtorch not found: using stub backend"
echo " Searched: LIBTORCH_HOME, ~/.local/lib/libtorch, torch R package"
echo " Install libtorch with tinytorch::install_libtorch() and reinstall"
cat > src/Makevars <<EOF
PKG_CPPFLAGS = -DTINYTORCH_HAVE_LIBTORCH=0
OBJECTS = stubs.o
EOF
echo "Generated src/Makevars (stub mode)"
exit 0
fi
# ---- Validate libtorch ----
TORCH_INCLUDE="${TORCH_HOME}/include"
TORCH_LIB="${TORCH_HOME}/lib"
if [ ! -f "$TORCH_LIB/libtorch.so" ] && [ ! -f "$TORCH_LIB/libtorch_cpu.so" ]; then
echo "WARNING: libtorch shared library not found in $TORCH_LIB"
echo " Falling back to stub backend"
cat > src/Makevars <<EOF
PKG_CPPFLAGS = -DTINYTORCH_HAVE_LIBTORCH=0
OBJECTS = stubs.o
EOF
echo "Generated src/Makevars (stub mode)"
exit 0
fi
echo "Found libtorch via $TORCH_SOURCE:"
echo " Headers: $TORCH_INCLUDE"
echo " Libraries: $TORCH_LIB"
# ---- Detect CUDA ----
CUDA_LIBS=""
CUDA_CPPFLAGS=""
if [ -f "$TORCH_LIB/libtorch_cuda.so" ]; then
CUDA_LIBS="-Wl,--no-as-needed -ltorch_cuda -lc10_cuda -Wl,--as-needed -lcuda"
for cuda_dir in /usr/local/cuda /usr/local/cuda-12 /usr/local/cuda-12.8; do
if [ -f "$cuda_dir/include/cuda_runtime_api.h" ]; then
CUDA_CPPFLAGS="-I${cuda_dir}/include -DTINYTORCH_CUDA"
echo " CUDA SDK: $cuda_dir/include"
break
fi
done
if [ -z "$CUDA_CPPFLAGS" ]; then
CUDA_CPPFLAGS="-DTINYTORCH_CUDA -DTINYTORCH_CUDA_NO_SDK"
echo " CUDA SDK: not found (using dlsym fallback)"
fi
echo " CUDA: enabled"
else
echo " CUDA: not available (CPU only)"
fi
# ---- Generate Makevars (real backend) ----
REAL_OBJECTS="RcppExports.o init.o gen-ops.o tensor.o ops.o nn_functions.o transformer.o fused_kernels.o gpu_launch.o indexing.o autograd.o optimizer.o convert.o"
cat > src/Makevars <<EOF
CXX_STD = CXX17
PKG_CPPFLAGS = -I${TORCH_INCLUDE} -I${TORCH_INCLUDE}/torch/csrc/api/include ${CUDA_CPPFLAGS} -DTINYTORCH_HAVE_LIBTORCH=1
PKG_CXXFLAGS = \$(CXX17STD) -D_GLIBCXX_USE_CXX11_ABI=1
PKG_LIBS = -L${TORCH_LIB} -ltorch -ltorch_cpu -lc10 ${CUDA_LIBS} \\
-Wl,-rpath,${TORCH_LIB}
OBJECTS = ${REAL_OBJECTS}
EOF
echo "Generated src/Makevars (real backend)"