Skip to content

Commit db455a7

Browse files
authored
Merge pull request #5 from wheelos/cpu_docker
fix: fix deps missing in cpu docker image - add install_proj - add in…
2 parents 129c150 + adb8c8f commit db455a7

File tree

12 files changed

+130
-62
lines changed

12 files changed

+130
-62
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ Build the entire Apollo project with:
100100
To build a single module, use:
101101

102102
```bash
103-
./apollo.sh build <module_name>
103+
./apollo.sh build_cpu <module_name>
104104
# example:
105-
./apollo.sh build planning
105+
./apollo.sh build_cpu planning
106106
```
107107

108108
---

docker/build/installers/install_libtorch.sh

Lines changed: 109 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,58 +21,120 @@ set -e
2121
CURR_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 ---
3232
TARGET_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
4748
fi
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 "$@"

docker/build/installers/install_mkl.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
# See the License for the specific language governing permissions and
1616
# limitations under the License.
1717
###############################################################################
18+
19+
# TODO(daohu527): Pytorch depends on mkl, but it has been packaged in torch and can be removed later.
20+
1821
set -e
1922

2023
cd "$(dirname "${BASH_SOURCE[0]}")"

docker/build/installers/install_ordinary_modules.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ declare -a module_dreamview_deps=("install_dreamview_deps.sh")
4949
declare -a module_drivers_deps=("install_drivers_deps.sh")
5050
declare -a module_guardian_deps=()
5151
declare -a module_localization_deps=("install_proj.sh")
52-
declare -a module_map_deps=()
52+
declare -a module_map_deps=("install_proj.sh")
5353
declare -a module_monitor_deps=()
5454
declare -a module_perception_deps=("install_paddle_deps.sh" "install_ffmpeg.sh" "install_opencv.sh")
55-
declare -a module_planning_deps=("install_adolc.sh" "install_ipopt.sh")
55+
declare -a module_planning_deps=("install_adolc.sh" "install_ipopt.sh" "install_libtorch.sh" "install_opencv.sh")
5656
declare -a module_prediction_deps=("install_opencv.sh")
5757
declare -a module_routing_deps=()
5858
declare -a module_storytelling_deps=()

docker/build/installers/install_proj.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ download_if_not_cached "$PKG_NAME" "$CHECKSUM" "$DOWNLOAD_LINK"
4747

4848
tar xzf "${PKG_NAME}"
4949
pushd proj-${VERSION} >/dev/null
50-
mkdir build && cd build
50+
mkdir -p build && cd build
5151
cmake .. \
5252
-DBUILD_SHARED_LIBS=ON \
5353
-DBUILD_TESTING=OFF \

docker/build/installers/installer_base.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function apollo_environ_setup() {
8282
if [ ! -f "${APOLLO_LD_FILE}" ]; then
8383
echo "${SYSROOT_DIR}/lib" | tee -a "${APOLLO_LD_FILE}"
8484
fi
85-
if [ ! -f "${APOLLO_PROFILE}" ]; then
85+
if [ ! -f "${APOLLO_PROFILE}" ] && [ -f "/opt/apollo/rcfiles/apollo.sh.sample" ]; then
8686
cp -f /opt/apollo/rcfiles/apollo.sh.sample "${APOLLO_PROFILE}"
8787
echo "add_to_path ${SYSROOT_DIR}/bin" >> "${APOLLO_PROFILE}"
8888
fi

modules/planning/learning_based/model_inference/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ cc_library(
2727
"//modules/common/util",
2828
"//modules/planning/common/util:math_util_lib",
2929
"//modules/planning/learning_based/img_feature_renderer:birdview_img_feature_renderer",
30-
#"//third_party/libtorch",
3130
"@opencv//:core",
3231
] + if_gpu(
3332
["@libtorch_gpu"],

modules/planning/tools/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ cc_binary(
2222
name = "inference_demo",
2323
srcs = ["inference_demo.cc"],
2424
deps = [
25-
#"//third_party/libtorch",
2625
"@com_github_gflags_gflags//:gflags",
2726
] + if_gpu(
2827
["@libtorch_gpu"],

scripts/ci/apollo_build.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,8 @@ function run_bazel_build() {
284284
jobs_args="--jobs=${CUSTOM_JOBS}"
285285
fi
286286
# default set cpus number according to the total cpu cores
287-
local cpus_args="--local_resources=cpu=$(nproc)"
287+
local cpu_count=$(($(nproc) / 2))
288+
local cpus_args="--local_resources=cpu=${cpu_count}"
288289
if [[ -n "${CUSTOM_CPUS}" ]]; then
289290
cpus_args="--local_resources=cpu=${CUSTOM_CPUS}"
290291
fi

third_party/libtorch/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,4 @@ install_src_files(
7272
src_dir = ["."],
7373
dest = "3rd-libtorch-gpu/src",
7474
filter = "*",
75-
)
75+
)

0 commit comments

Comments
 (0)