@@ -21,58 +21,120 @@ set -e
2121CURR_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd -P) "
2222. ${CURR_DIR} /installer_base.sh
2323
24- # TODO(build): Docs on how to build libtorch on Jetson boards
25- # References:
26- # https://github.com/ApolloAuto/apollo/blob/pre6/docker/build/installers/install_libtorch.sh
27- # https://github.com/dusty-nv/jetson-containers/blob/master/Dockerfile.pytorch
28- # https://forums.developer.nvidia.com/t/pytorch-for-jetson-version-1-6-0-now-available
29- # https://github.com/pytorch/pytorch/blob/master/docker/caffe2/ubuntu-16.04-cpu-all-options/Dockerfile
30- bash ${CURR_DIR} /install_mkl.sh
24+ # --- Unified Version Control ---
25+ # Manage all Pytorch related component versions here
26+ PYTORCH_VERSION=" 2.6.0"
27+ CHECKSUM=" " 6887b5186e466a6d5ca044a51d083bb03c48cb1b4952059b7ca51a5398fbafcc" "
28+ TORCHVISION_VERSION=" 0.18.0" # Note: Version must be compatible with Pytorch
29+ TORCHAUDIO_VERSION=" 2.4.0" # Note: Version must be compatible with Pytorch
3130
31+ # --- Environment Detection ---
3232TARGET_ARCH=" $( uname -m) "
33+ CUDA_SUPPORT=false
34+ CUDA_VERSION_STR=" " # e.g., "11.8"
35+ CUDA_VERSION_TAG=" " # e.g., "cu118"
3336
34- # #============================================================##
35- # libtorch_cpu
36-
37- if [[ " ${TARGET_ARCH} " == " x86_64" ]]; then
38- # https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-1.5.0%2Bcpu.zip
39- VERSION=" 1.7.0-2"
40- CHECKSUM=" 02fd4f30e97ce8911ef933d0516660892392e95e6768b50f591f4727f6224390"
41- elif [[ " ${TARGET_ARCH} " == " aarch64" ]]; then
42- VERSION=" 1.6.0-1"
43- CHECKSUM=" 6d1fba522e746213c209fbf6275fa6bac68e360bcd11cbd4d3bdbddb657bee82"
44- else
45- error " libtorch for ${TARGET_ARCH} not ready. Exiting..."
46- exit 1
37+ if [ " ${TARGET_ARCH} " = " x86_64" ] && command -v nvcc > /dev/null 2>&1 ; then
38+ # Get CUDA major and minor version (e.g., 11.8)
39+ CUDA_VERSION_STR=$( nvcc --version | sed -n ' s/.*release \([0-9]*\.[0-9]*\).*/\1/p' )
40+ if [ -n " ${CUDA_VERSION_STR} " ]; then
41+ # Format version for Pytorch whl URL (e.g., 11.8 -> cu118)
42+ CUDA_VERSION_TAG=" cu$( echo ${CUDA_VERSION_STR} | sed ' s/\.//g' ) "
43+ CUDA_SUPPORT=true
44+ ok " Found CUDA ${CUDA_VERSION_STR} . PyTorch will be installed with GPU support."
45+ else
46+ warn " nvcc found, but could not determine CUDA version. Falling back to CPU."
47+ fi
4748fi
4849
49- PKG_NAME=" libtorch_cpu-${VERSION} -linux-${TARGET_ARCH} .tar.gz"
50- DOWNLOAD_LINK=" https://apollo-system.cdn.bcebos.com/archive/6.0/${PKG_NAME} "
51- download_if_not_cached " ${PKG_NAME} " " ${CHECKSUM} " " ${DOWNLOAD_LINK} "
52-
53- tar xzf " ${PKG_NAME} "
54- mv " ${PKG_NAME% .tar.gz} " /usr/local/libtorch_cpu
55- rm -f " ${PKG_NAME} "
56- ok " Successfully installed libtorch_cpu ${VERSION} "
57-
58- # #============================================================##
59- # libtorch_gpu
60- if [[ " ${TARGET_ARCH} " == " x86_64" ]]; then
61- VERSION=" 1.7.0-2"
62- CHECKSUM=" b64977ca4a13ab41599bac8a846e8782c67ded8d562fdf437f0e606cd5a3b588"
63- PKG_NAME=" libtorch_gpu-${VERSION} -cu111-linux-x86_64.tar.gz"
64- else # AArch64
65- VERSION=" 1.6.0-1"
66- PKG_NAME=" libtorch_gpu-1.6.0-1-linux-aarch64.tar.gz"
67- CHECKSUM=" eeb5a223d9dbe40fe96f16e6711c49a3777cea2c0a8da2445d63e117fdad0385"
68- fi
50+ # --- Python PyTorch Installation ---
51+ function install_pytorch_python() {
52+ info " Installing Python PyTorch ${PYTORCH_VERSION} ..."
53+
54+ local INDEX_URL_FLAG=" "
55+ if [ " $CUDA_SUPPORT " = true ]; then
56+ INDEX_URL_FLAG=" --index-url https://download.pytorch.org/whl/${CUDA_VERSION_TAG} "
57+ else
58+ INDEX_URL_FLAG=" --index-url https://download.pytorch.org/whl/cpu"
59+ fi
60+
61+ pip3 install \
62+ torch==${PYTORCH_VERSION} \
63+ torchvision==${TORCHVISION_VERSION} \
64+ torchaudio==${TORCHAUDIO_VERSION} \
65+ ${INDEX_URL_FLAG}
66+
67+ # Verify installation
68+ info " Verifying Python PyTorch installation..."
69+ python3 -c "
70+ import torch
71+ print(f'PyTorch Version: {torch.__version__}')
72+ print(f'CUDA Available: {torch.cuda.is_available()}')
73+ if torch.cuda.is_available():
74+ print(f'CUDA Version: {torch.version.cuda}')
75+ print(f'GPU Name: {torch.cuda.get_device_name(0)}')
76+ "
77+ ok " Python PyTorch installation successful."
78+ }
79+
80+ # --- C++ LibTorch Installation ---
81+ function install_libtorch_cpp() {
82+ info " Installing LibTorch C++ ${PYTORCH_VERSION} ..."
83+ local BASE_URL=" https://download.pytorch.org/libtorch/cpu"
84+ local ARCHIVE=" "
85+ local URL=" "
86+
87+ if [ " ${TARGET_ARCH} " = " x86_64" ]; then
88+ if [ " $CUDA_SUPPORT " = true ]; then
89+ ARCHIVE=" libtorch-cxx11-abi-shared-with-deps-${PYTORCH_VERSION} +${CUDA_VERSION_TAG} .zip"
90+ URL=" ${BASE_URL} /${CUDA_VERSION_TAG} /${ARCHIVE} "
91+ else
92+ ARCHIVE=" libtorch-cxx11-abi-shared-with-deps-${PYTORCH_VERSION} %2Bcpu.zip"
93+ URL=" ${BASE_URL} /cpu/${ARCHIVE} "
94+ fi
95+ elif [ " ${TARGET_ARCH} " = " aarch64" ]; then
96+ # WARNING: Official pre-compiled LibTorch C++ package is not available for aarch64.
97+ # If needed, you must build from source. Skipping installation here.
98+ warn " Official pre-compiled LibTorch C++ is not available for aarch64."
99+ warn " Skipping LibTorch C++ installation. If required, you must build from source."
100+ return 0 # Exit normally
101+ else
102+ error " Unsupported architecture: ${TARGET_ARCH} "
103+ return 1
104+ fi
105+
106+ local DOWNLOAD_DIR=" /tmp/libtorch_download"
107+ mkdir -p " ${DOWNLOAD_DIR} "
108+ pushd " ${DOWNLOAD_DIR} " > /dev/null
109+
110+ info " Downloading from ${URL} "
111+ download_if_not_cached " ${ARCHIVE} " " ${CHECKSUM} " " ${URL} "
112+ unzip -q " ${ARCHIVE} "
113+
114+ # Install to system path
115+ INSTALL_DIR=" /usr/local/libtorch"
116+ info " Installing LibTorch to ${INSTALL_DIR} ..."
117+ rm -rf " ${INSTALL_DIR} " # Remove old version
118+ mkdir -p " ${INSTALL_DIR} "
119+ # Use mv instead of cp -r for better efficiency
120+ mv libtorch/* " ${INSTALL_DIR} /"
121+
122+ # Clean up downloaded files
123+ popd > /dev/null
124+ rm -rf " ${DOWNLOAD_DIR} "
125+
126+ # Optionally: update dynamic linker cache
127+ ldconfig
69128
70- DOWNLOAD_LINK= " https://apollo-system.cdn.bcebos.com/archive/6.0/ ${PKG_NAME} "
71- download_if_not_cached " ${PKG_NAME} " " ${CHECKSUM} " " ${DOWNLOAD_LINK} "
129+ ok " LibTorch C++ ${PYTORCH_VERSION} installed successfully. "
130+ }
72131
73- tar xzf " ${PKG_NAME} "
74- mv " ${PKG_NAME% .tar.gz} " /usr/local/libtorch_gpu
132+ # --- Main Execution Flow ---
133+ main () {
134+ # TODO(daohu527): For inference, no python version is required
135+ # install_pytorch_python
136+ install_libtorch_cpp
137+ info " ✅ All PyTorch components have been installed."
138+ }
75139
76- # Cleanup
77- rm -f " ${PKG_NAME} "
78- ok " Successfully installed libtorch_gpu ${VERSION} "
140+ main " $@ "
0 commit comments