mirror of
https://github.com/photoprism/photoprism.git
synced 2026-07-17 16:49:04 +00:00
AI: Add GPU/CUDA option to ONNX Runtime install script #5703
This commit is contained in:
parent
021bdeb428
commit
75e08934b8
1 changed files with 88 additions and 17 deletions
105
scripts/dist/install-onnx.sh
vendored
105
scripts/dist/install-onnx.sh
vendored
|
|
@ -7,7 +7,37 @@ TODAY=$(date -u +%Y%m%d)
|
|||
TMPDIR=${TMPDIR:-/tmp}
|
||||
SYSTEM=$(uname -s)
|
||||
ARCH=${PHOTOPRISM_ARCH:-$(uname -m)}
|
||||
DESTDIR_ARG="${1:-/usr}"
|
||||
|
||||
# ONNX_GPU selects a CUDA (GPU) build instead of the default CPU build. It can be
|
||||
# set via the ONNX_GPU environment variable or a --gpu[=cuda12|cuda13] argument:
|
||||
# unset / 0 / cpu -> CPU build (default)
|
||||
# 1 / gpu / cuda13 -> CUDA 13 build (recommended; CUDA 12 is deprecated upstream)
|
||||
# cuda12 -> CUDA 12 build (legacy)
|
||||
# GPU builds are only published for Linux x64 and additionally require the NVIDIA
|
||||
# driver plus a matching CUDA runtime and cuDNN to be present at runtime.
|
||||
ONNX_GPU=${ONNX_GPU:-}
|
||||
|
||||
# Parse optional flags; the first non-flag argument is the install prefix.
|
||||
DESTDIR_ARG="/usr"
|
||||
destdir_set=0
|
||||
for arg in "$@"; do
|
||||
case "${arg}" in
|
||||
--gpu) ONNX_GPU="cuda13" ;;
|
||||
--gpu=*) ONNX_GPU="${arg#--gpu=}" ;;
|
||||
--cpu) ONNX_GPU="" ;;
|
||||
-*) echo "Error: unknown option '${arg}'." >&2; exit 2 ;;
|
||||
*) if [[ "${destdir_set}" == 0 ]]; then DESTDIR_ARG="${arg}"; destdir_set=1; fi ;;
|
||||
esac
|
||||
done
|
||||
|
||||
# Normalize the GPU selection to an empty (CPU) or "cudaNN" variant.
|
||||
gpu_variant=""
|
||||
case "${ONNX_GPU,,}" in
|
||||
""|0|cpu|false|no) gpu_variant="" ;;
|
||||
1|gpu|cuda13|cuda-13) gpu_variant="cuda13" ;;
|
||||
cuda12|cuda-12) gpu_variant="cuda12" ;;
|
||||
*) echo "Error: unsupported ONNX_GPU value '${ONNX_GPU}' (use cuda12 or cuda13)." >&2; exit 1 ;;
|
||||
esac
|
||||
|
||||
if [[ ! -d "${DESTDIR_ARG}" ]]; then
|
||||
mkdir -p "${DESTDIR_ARG}"
|
||||
|
|
@ -22,6 +52,11 @@ fi
|
|||
|
||||
mkdir -p "${DESTDIR}" "${TMPDIR}"
|
||||
|
||||
# version_lt returns success if $1 is a strictly lower semantic version than $2.
|
||||
version_lt() {
|
||||
[[ "$1" != "$2" ]] && [[ "$(printf '%s\n%s\n' "$1" "$2" | sort -V | head -n1)" == "$1" ]]
|
||||
}
|
||||
|
||||
archive=""
|
||||
sha=""
|
||||
|
||||
|
|
@ -29,10 +64,27 @@ case "${SYSTEM}" in
|
|||
Linux)
|
||||
case "${ARCH}" in
|
||||
amd64|AMD64|x86_64|x86-64)
|
||||
archive="onnxruntime-linux-x64-${ONNX_VERSION}.tgz"
|
||||
sha="1254da24fb389cf39dc0ff3451ab48301740ffbfcbaf646849df92f80ee92c57"
|
||||
if [[ -n "${gpu_variant}" ]]; then
|
||||
# Upstream renamed the CUDA-12 archive from "-gpu-" to "-gpu_cuda12-" in v1.27.0.
|
||||
if [[ "${gpu_variant}" == "cuda12" ]] && version_lt "${ONNX_VERSION}" "1.27.0"; then
|
||||
archive="onnxruntime-linux-x64-gpu-${ONNX_VERSION}.tgz"
|
||||
sha="cb7df7ee2ca0f962c7ce7c839aeae36223d146a91fb4646d62fb0046f297479f"
|
||||
else
|
||||
archive="onnxruntime-linux-x64-gpu_${gpu_variant}-${ONNX_VERSION}.tgz"
|
||||
if [[ "${gpu_variant}" == "cuda13" ]]; then
|
||||
sha="aa619d5701bbe58046cc998b21e692d5b2aefac1479f375c4b988526cb80befa"
|
||||
fi
|
||||
fi
|
||||
else
|
||||
archive="onnxruntime-linux-x64-${ONNX_VERSION}.tgz"
|
||||
sha="1254da24fb389cf39dc0ff3451ab48301740ffbfcbaf646849df92f80ee92c57"
|
||||
fi
|
||||
;;
|
||||
arm64|ARM64|aarch64)
|
||||
if [[ -n "${gpu_variant}" ]]; then
|
||||
echo "Error: ONNX Runtime GPU/CUDA builds are only available for Linux x64." >&2
|
||||
exit 1
|
||||
fi
|
||||
archive="onnxruntime-linux-aarch64-${ONNX_VERSION}.tgz"
|
||||
sha="34ff1c2d0f12e2cf3d33a0c5f82e39792e1d581fbd6968fd7c30d173654be01a"
|
||||
;;
|
||||
|
|
@ -43,6 +95,10 @@ case "${SYSTEM}" in
|
|||
esac
|
||||
;;
|
||||
Darwin)
|
||||
if [[ -n "${gpu_variant}" ]]; then
|
||||
echo "Error: ONNX Runtime GPU/CUDA builds are only available for Linux x64." >&2
|
||||
exit 1
|
||||
fi
|
||||
case "${ARCH}" in
|
||||
arm64|ARM64|aarch64)
|
||||
archive="onnxruntime-osx-arm64-${ONNX_VERSION}.tgz"
|
||||
|
|
@ -64,6 +120,10 @@ case "${SYSTEM}" in
|
|||
;;
|
||||
esac
|
||||
|
||||
# Allow an explicit checksum override (e.g. when installing a non-default version
|
||||
# or a GPU variant whose checksum is not pinned in this script).
|
||||
sha="${ONNX_SHA256:-${sha}}"
|
||||
|
||||
verify_sha() {
|
||||
local expected="$1"
|
||||
local file="$2"
|
||||
|
|
@ -79,6 +139,11 @@ if [[ -z "${archive}" ]]; then
|
|||
exit 1
|
||||
fi
|
||||
|
||||
if [[ -z "${sha}" ]]; then
|
||||
echo "Error: no checksum pinned for '${archive}'. Set ONNX_SHA256 to install it." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
primary_url="https://dl.photoprism.app/onnx/runtime/v${ONNX_VERSION}/${archive}?${TODAY}"
|
||||
fallback_url="https://github.com/microsoft/onnxruntime/releases/download/v${ONNX_VERSION}/${archive}"
|
||||
package_path="${TMPDIR}/${archive}"
|
||||
|
|
@ -110,22 +175,28 @@ echo "Extracting to ${DESTDIR}..."
|
|||
tar --overwrite --mode=755 -C "${DESTDIR}" -xzf "${package_path}"
|
||||
|
||||
# Normalize layout: copy libraries into ${DESTDIR}/lib and remove extracted tree.
|
||||
# The archive extracts to a top directory named after itself (minus ".tgz"),
|
||||
# which also covers the GPU builds and their extra provider libraries.
|
||||
output_lib_dir="${DESTDIR}/lib"
|
||||
mkdir -p "${output_lib_dir}"
|
||||
|
||||
for extracted in "${DESTDIR}/onnxruntime-linux-x64-${ONNX_VERSION}" "${DESTDIR}/onnxruntime-linux-aarch64-${ONNX_VERSION}" "${DESTDIR}/onnxruntime-osx-arm64-${ONNX_VERSION}" "${DESTDIR}/onnxruntime-osx-universal2-${ONNX_VERSION}"; do
|
||||
if [[ -d "${extracted}/lib" ]]; then
|
||||
find "${extracted}/lib" -maxdepth 1 -type f -name "libonnxruntime*.so*" -print0 | while IFS= read -r -d '' file; do
|
||||
cp -af "${file}" "${output_lib_dir}/"
|
||||
done
|
||||
# copy any symlinks as well to preserve SONAME links
|
||||
find "${extracted}/lib" -maxdepth 1 -type l -name "libonnxruntime*.so*" -print0 | while IFS= read -r -d '' link; do
|
||||
target=$(readlink "${link}")
|
||||
ln -sf "${target}" "${output_lib_dir}/$(basename "${link}")"
|
||||
done
|
||||
rm -rf "${extracted}"
|
||||
fi
|
||||
done
|
||||
# Determine the extracted top-level directory from the archive itself: upstream's
|
||||
# GPU archive filenames (e.g. "-gpu_cuda13-") do not match their internal
|
||||
# directory name (e.g. "-gpu-"), so it cannot be derived from the file name.
|
||||
# (|| true: head closes the pipe early, which SIGPIPEs tar under pipefail.)
|
||||
extracted_name=$(tar tzf "${package_path}" 2>/dev/null | head -1 | cut -d/ -f1 || true)
|
||||
extracted="${DESTDIR}/${extracted_name}"
|
||||
if [[ -n "${extracted_name}" && -d "${extracted}/lib" ]]; then
|
||||
find "${extracted}/lib" -maxdepth 1 -type f -name "libonnxruntime*.so*" -print0 | while IFS= read -r -d '' file; do
|
||||
cp -af "${file}" "${output_lib_dir}/"
|
||||
done
|
||||
# copy any symlinks as well to preserve SONAME links
|
||||
find "${extracted}/lib" -maxdepth 1 -type l -name "libonnxruntime*.so*" -print0 | while IFS= read -r -d '' link; do
|
||||
target=$(readlink "${link}")
|
||||
ln -sf "${target}" "${output_lib_dir}/$(basename "${link}")"
|
||||
done
|
||||
rm -rf "${extracted}"
|
||||
fi
|
||||
|
||||
if [[ "${SYSTEM}" == "Linux" ]]; then
|
||||
if [[ "${DESTDIR}" == "/usr" || "${DESTDIR}" == "/usr/local" ]]; then
|
||||
|
|
@ -135,4 +206,4 @@ if [[ "${SYSTEM}" == "Linux" ]]; then
|
|||
fi
|
||||
fi
|
||||
|
||||
echo "ONNX Runtime ${ONNX_VERSION} installed in '${DESTDIR}'."
|
||||
echo "ONNX Runtime ${ONNX_VERSION}${gpu_variant:+ (GPU/${gpu_variant})} installed in '${DESTDIR}'."
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue