From 2f182a1ce60e017dfaab0c84ca4f2c68b678854e Mon Sep 17 00:00:00 2001 From: itsmygithubacct Date: Tue, 4 Aug 2026 03:05:21 -0700 Subject: [PATCH] fix: validate CUDA_ARCH before probing the toolchain MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit build_bonsai_q1_gpu.sh probed for nvcc before it validated CUDA_ARCH, so the same bad input diagnosed differently depending on the host. CUDA_ARCH=native exited 2 with "CUDA_ARCH must look like sm_86" on a machine with CUDA installed, and exited 1 with "nvcc not found" on a machine without it. test_gpu_build_rejects_invalid_arch_before_compilation asserts the former, so the suite failed on any runner with no CUDA toolchain — which is every hosted runner. That test has been red on main since 2026-07-22. An architecture the caller supplied is now checked first: before the toolchain is probed and before BONSAI_BIN_DIR is created. A wrong CUDA_ARCH is wrong on every host, so it has to be reported as a wrong architecture everywhere rather than as a missing compiler on the subset of hosts that lack one. Auto-detection is unchanged and still runs where it did, and the detected value is put through the same check, so nvidia-smi cannot yield an architecture that the explicit path would have refused. Nothing is created and no artifact is written for an architecture that was never going to build. --- bonsai/tools/build_bonsai_q1_gpu.sh | 32 ++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/bonsai/tools/build_bonsai_q1_gpu.sh b/bonsai/tools/build_bonsai_q1_gpu.sh index 7bdeb61..e93c456 100755 --- a/bonsai/tools/build_bonsai_q1_gpu.sh +++ b/bonsai/tools/build_bonsai_q1_gpu.sh @@ -10,12 +10,31 @@ for arg in "$@"; do case "$arg" in -h|--help) usage; exit 0;; esac; done ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" src="$ROOT/tools/bonsai_q1_gpu.cu" +nvcc="${NVCC:-nvcc}" +arch="${CUDA_ARCH:-}" + +# An architecture the caller supplied is checked before the toolchain is probed and +# before anything is created. CUDA_ARCH=native is wrong on every host, so it has to be +# reported as a bad architecture everywhere — not as "nvcc not found" on the hosts that +# happen to lack CUDA, which would make the same bad input diagnose differently per host. +require_supported_arch() { + if [[ ! "$1" =~ ^sm_[0-9]{2,3}$ ]]; then + echo "build_bonsai_q1_gpu.sh: CUDA_ARCH must look like sm_86, got: $1" >&2 + exit 2 + fi + if ((10#${1#sm_} < 75)); then + echo "build_bonsai_q1_gpu.sh: $1 is unsupported; exact BMMA kernels require sm_75 or newer" >&2 + exit 2 + fi +} +if [ -n "$arch" ]; then + require_supported_arch "$arch" +fi + # Built kernel goes to $BONSAI_NOTARY_HOME/bin (build artifacts are not source); the loader prefers it and # falls back to /tools for back-compat. Override with $BONSAI_BIN_DIR. BIN_DIR="${BONSAI_BIN_DIR:-${BONSAI_NOTARY_HOME:-$HOME/.local/trinote}/bin}"; mkdir -p "$BIN_DIR" out="$BIN_DIR/libbonsai_q1_gpu.so" -nvcc="${NVCC:-nvcc}" -arch="${CUDA_ARCH:-}" if ! command -v "$nvcc" >/dev/null 2>&1; then echo "build_bonsai_q1_gpu.sh: nvcc not found on PATH (set NVCC=...). GPU build is opt-in; the CPU path stays." >&2 @@ -39,15 +58,8 @@ if [ -z "$arch" ]; then fi arch="sm_$capability" fi -if [[ ! "$arch" =~ ^sm_[0-9]{2,3}$ ]]; then - echo "build_bonsai_q1_gpu.sh: CUDA_ARCH must look like sm_86, got: $arch" >&2 - exit 2 -fi +require_supported_arch "$arch" # covers the auto-detected value; idempotent for an explicit CUDA_ARCH arch_number="${arch#sm_}" -if ((10#$arch_number < 75)); then - echo "build_bonsai_q1_gpu.sh: $arch is unsupported; exact BMMA kernels require sm_75 or newer" >&2 - exit 2 -fi echo "[bonsai-gpu-build] target architecture: $arch${CUDA_ARCH:+ (override)}" # NO --use_fast_math / -ffast-math: would relax/reorder arithmetic. Integer kernel only, but assert it.