okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221packaging: bundle libatomic.so.1 in the Linux AMD packages; check imports against the base system1a5c6dd19

main
9.4 KiB123 linesraw
1#!/bin/bash
2# Linux AMD (HIP) engine package: strata + the CPU picture reader (strata-vision) + the GPU picture reader (strata-vision-hip),
3# the ROCm libraries both load, and the rocBLAS / hipBLASLt kernel data for the listed archs, packaged as an engine component.
4# Run on a Linux box that has ROCm (TheRock Python wheels are enough); the tester packages were built in an Ubuntu 22.04 chroot
5# (glibc 2.35) so they run on every distribution since.  All settings are environment variables:
6#
7#   VARIANT=rx9070     rx9070 (gfx1200 gfx1201) | rx7900 (gfx1100 gfx1101 gfx1102) | rx6000 (gfx1030 gfx1031) | halo (gfx1151) | igpu (gfx1103 gfx1150) | a name + ARCHS=
8#   ARCHS="gfx1200;gfx1201"          the code objects (default: the variant's)
9#   SRC=<this checkout>              the Strata source (default: the checkout this script is in)
10#   LLAMA=<llama.cpp checkout>       optional override (kept for two releases): ggml (and strata-vision) come from that checkout instead of the vendored third_party/
11#   HP=<ROCm devel root>  CORE=<ROCm core root>  LIBW=<ROCm libraries root>   (TheRock: _rocm_sdk_devel, _rocm_sdk_core, _rocm_sdk_libraries_*)
12#   OUT=<work folder>  PKGVER=0.1.60  COMMIT=<source commit for the build id>  JOBS=16
13#   VISION_ONLY=1                    build only the two readers (no engine, no package)
14#   SPLIT_DIR=<folder>               ship the strata-split-N-amd binaries of that folder (made by packaging/build_split_hip.sh) beside strata
15#   NO_PACKAGE=1                     stop after the stage folder
16#   SHAPE=q4_lite                    the engine of a model shape, its own component (packaging/shape_build.sh)
17#   VISION_HIP_DEFS="-DGGML_CUDA_FA=OFF"   extra cmake definitions for the GPU reader only
18#
19# Both readers get RUNPATH $ORIGIN (tools/vision/CMakeLists.txt), and every bundled library is patched to $ORIGIN, so the
20# component folder runs from anywhere with nothing from the environment (packaging/binary_deps.py checks it).
21set -u
22V=${VARIANT:?VARIANT=rx9070|rx7900|rx6000|halo|igpu|<name> with ARCHS=}
23SRC=${SRC:-$(cd "$(dirname "$0")/.." && pwd)}
24LLAMA=${LLAMA:-}      # an override: a llama.cpp checkout to take ggml from (kept for two releases); empty: the vendored third_party/strata-ggml
25GGML=(); [ -z "$LLAMA" ] || GGML=("-DSTRATA_GGML_DIR=$LLAMA")
26HP=${HP:?HP=<ROCm devel root>}
27CORE=${CORE:-$HP}
28LIBW=${LIBW:-$HP}
29OUT=${OUT:-$PWD/work-$V}
30PKGVER=${PKGVER:-0.1.60}
31COMMIT=${COMMIT:-unknown}
32JOBS=${JOBS:-16}
33case "$V" in
34  rx9070) ARCHS=${ARCHS:-gfx1200;gfx1201} ;;
35  rx7900) ARCHS=${ARCHS:-gfx1100;gfx1101;gfx1102} ;;
36  rx6000) ARCHS=${ARCHS:-gfx1030;gfx1031} ;;   # RDNA2: no WMMA (plain kernels), no hipBLASLt kernels; built, not tested on a real RX 6000 card
37  halo)   ARCHS=${ARCHS:-gfx1151} ;;
38  igpu)   ARCHS=${ARCHS:-gfx1103;gfx1150} ;;
39  *)      ARCHS=${ARCHS:?ARCHS=gfx...} ;;
40esac
41NOTE_EXTRA=""; [ "$V" = rx6000 ] && NOTE_EXTRA=" (built, not tested on a real RX 6000 card)"
42ARCHLIST=${ARCHS//;/ }
43DATAARCH=${ARCHS//;/,}
44mkdir -p "$OUT"; S=$OUT/build.status
45st() { echo "$* $(date +%T)" >> "$S"; }
46# a step that failed stops the build with its non-zero exit (a lost exit code let stale binaries through before): chk $? "what"
47chk() { local rc=$1; shift; st "$* rc=$rc"; [ "$rc" = 0 ] || { st "ABORT $* failed (rc=$rc)"; exit 1; }; }
48# a tree without .git (a tarball) has no commit for the build id: the packer writes it to BUILD_ID (CMakeLists.txt reads it), first 9 characters
49ID9=${COMMIT:0:9}
50if [ "$COMMIT" != unknown ] && [ ! -e "$SRC/.git" ] && [ ! -f "$SRC/BUILD_ID" ]; then printf '%s\n' "$ID9" > "$SRC/BUILD_ID"; fi
51: > "$S"
52export HIP_PLATFORM=amd HIP_COMPILER=clang HIP_RUNTIME=rocclr ROCM_PATH=$HP HIP_PATH=$HP
53# TheRock 7.14.x dists (the Strix Halo / gfx1151 package) keep libnuma, libz, libdrm ... in lib/rocm_sysdeps; older wheels have no such folder
54SYSDEPS=""; [ -d "$HP/lib/rocm_sysdeps" ] && SYSDEPS=";$HP/lib/rocm_sysdeps"
55HIPDEFS=(-DCMAKE_HIP_COMPILER=$HP/llvm/bin/clang++ -DCMAKE_HIP_COMPILER_ROCM_ROOT=$HP "-DCMAKE_PREFIX_PATH=$HP;$LIBW$SYSDEPS"
56         "-DCMAKE_HIP_FLAGS=--rocm-path=$HP --rocm-device-lib-path=$HP/lib/llvm/amdgcn/bitcode")
57st "start $V ($ARCHS) commit $COMMIT"
58cd "$SRC" || exit 1
59. "$SRC/packaging/shape_build.sh"; shape_setup "$SRC"
60if [ -z "${VISION_ONLY:-}" ]; then
61  # STRATA_CMAKE_EXTRA: more configure arguments for the engine build, e.g. -DSTRATA_GEOMETRY_FILE=/abs/path/geometry.hpp (a new model shape)
62  # shellcheck disable=SC2086
63  cmake -G Ninja -S . -B "$OUT/b" -DCMAKE_BUILD_TYPE=Release -DSTRATA_ENABLE_HIP=ON -DSTRATA_ENABLE_CUDA=OFF -DSTRATA_BUILD_TESTS=OFF \
64    -DSTRATA_PREFILL_MMQ=ON -DSTRATA_MMQ=OFF "-DCMAKE_HIP_ARCHITECTURES=$ARCHS" "${HIPDEFS[@]}" ${GGML[@]+"${GGML[@]}"} -DSTRATA_PORTABLE=ON \
65    "-DCMAKE_EXE_LINKER_FLAGS=-Wl,-rpath,'\$ORIGIN' -Wl,--enable-new-dtags" ${STRATA_CMAKE_EXTRA:-} $shape_cmake > "$OUT/cfg.log" 2>&1
66  chk $? "engine configured"
67  nice -n 10 cmake --build "$OUT/b" --target strata -j "$JOBS" > "$OUT/build.log" 2>&1
68  chk $? "engine built"
69  [ -x "$OUT/b/strata" ] || { st "ABORT no strata"; exit 1; }
70fi
71# strata-vision builds from the vendored third_party/strata-llama; LLAMA=<llama.cpp checkout> overrides it
72VLLAMA=(); [ -z "$LLAMA" ] || VLLAMA=("-DLLAMA_DIR=$LLAMA")
73# the CPU reader: portable (AVX2), the fallback that runs on any PC
74cmake -G Ninja -S tools/vision -B "$OUT/bv" -DCMAKE_BUILD_TYPE=Release ${VLLAMA[@]+"${VLLAMA[@]}"} -DSTRATA_VISION_CUDA=OFF \
75  -DSTRATA_PORTABLE=ON > "$OUT/cfg-vision.log" 2>&1
76nice -n 10 cmake --build "$OUT/bv" --target strata-vision -j "$JOBS" > "$OUT/build-vision.log" 2>&1
77chk $? "cpu reader built"
78[ -x "$OUT/bv/bin/strata-vision" ] || { st "ABORT no strata-vision"; exit 1; }
79# the GPU reader: ggml-hip for the same archs; the CPU kernels in it are portable too
80cmake -G Ninja -S tools/vision -B "$OUT/bh" -DCMAKE_BUILD_TYPE=Release ${VLLAMA[@]+"${VLLAMA[@]}"} -DSTRATA_VISION_HIP=ON \
81  "-DSTRATA_VISION_HIP_ARCHS=$ARCHS" -DSTRATA_PORTABLE=ON "${HIPDEFS[@]}" ${VISION_HIP_DEFS:-} > "$OUT/cfg-vision-hip.log" 2>&1
82chk $? "gpu reader configured"
83nice -n 10 cmake --build "$OUT/bh" --target strata-vision -j "$JOBS" > "$OUT/build-vision-hip.log" 2>&1
84chk $? "gpu reader built"
85[ -x "$OUT/bh/bin/strata-vision-hip" ] || { st "ABORT no strata-vision-hip"; tail -20 "$OUT/build-vision-hip.log" >> "$S"; exit 1; }
86[ -n "${VISION_ONLY:-}" ] && { st "VISION-DONE"; exit 0; }
87STAGE=$OUT/stage; rm -rf "$STAGE"; mkdir -p "$STAGE"
88ARCHJSON=$(for a in $ARCHLIST; do printf '"%s", ' "$a"; done | sed 's/, $//')
89cat > "$STAGE/BUILD.json" <<EOJ
90{"version": "$PKGVER", "source": "f-amd-vision $COMMIT", "variant": "$V", "archs": [$ARCHJSON], "portable": true, "vision": "gpu", "vision_cpu": "strata-vision", "vision_gpu": "strata-vision-hip", "vision_archs": [$ARCHJSON]}
91EOJ
92# the libraries the engine and the GPU reader load (the CPU reader needs only the system's), collected from the build outputs
93DATA=(--data "$LIBW/lib/rocblas/library=rocblas/library" --data "$LIBW/lib/hipblaslt/library=hipblaslt/library" --data-arch "$DATAARCH")
94BINS=("$OUT/b/strata" "$OUT/bh/bin/strata-vision-hip")
95for b in "${BINS[@]}"; do
96  python3 packaging/collect_libs.py --binary "$b" --search "$LIBW/lib" --search "$HP/lib" --search "$CORE/lib" --search "$CORE/lib/llvm/lib" ${SYSDEPS:+--search "$HP/lib/rocm_sysdeps/lib"} \
97    --also 'libamd_comgr.so*' --also 'libhiprtc*.so*' --out "$STAGE" \
98    "${DATA[@]}" >> "$OUT/collect.log" 2>&1
99  chk $? "collected for $(basename "$b")"
100  DATA=()                                              # the kernel data once
101done
102cp "$OUT/b/strata" "$OUT/bv/bin/strata-vision" "$OUT/bh/bin/strata-vision-hip" "$STAGE/"
103# the all-cards split engines (packaging/build_split_hip.sh: strata-split-2/3/4-amd, RUNPATH $ORIGIN) ride in the package when SPLIT_DIR names the folder holding them (--tree takes them)
104[ -n "${SPLIT_DIR:-}" ] && cp "$SPLIT_DIR"/strata-split-*-amd "$STAGE/"
105for f in "$STAGE"/*.so "$STAGE"/*.so.*; do [ -f "$f" ] && [ ! -L "$f" ] && patchelf --set-rpath '$ORIGIN' "$f"; done
106for f in "$STAGE/strata" "$STAGE/strata-vision" "$STAGE/strata-vision-hip"; do
107  st "RUNPATH $(basename "$f"): $(readelf -d "$f" | grep -E 'RPATH|RUNPATH' | cut -d'[' -f2 | tr -d ']' | paste -sd' ')"
108done
109if [ -n "${SHAPE:-}" ]; then shape_check "$STAGE/strata" >> "$S" 2>&1; chk $? "shape $SHAPE"; fi
110# the hipBLASLt version of this ROCm into BUILD.json, and the tuning tables of tools/hip that match it (architecture and version) into tuning/
111python3 packaging/stage_tuning.py --stage "$STAGE" --archs "$ARCHLIST" --rocm "$HP" >> "$OUT/stage_tuning.log" 2>&1
112trc=$?; st "tuning tables rc=$trc: $(tail -n 1 "$OUT/stage_tuning.log")"; [ "$trc" = 0 ] || { st "ABORT tuning tables"; exit 1; }
113[ -n "${NO_PACKAGE:-}" ] && { st "STAGE-DONE"; exit 0; }
114rm -rf "$OUT/components"; mkdir -p "$OUT/components/engine"
115ARCHARGS=""; for a in $ARCHLIST; do ARCHARGS="$ARCHARGS --arch $a"; done
116python3 packaging/package_engine.py --src "$STAGE" --backend hip --version "$PKGVER" --out "$OUT/components/engine" $ARCHARGS \
117  --variant "$V" --build-id "$COMMIT+rocm" --tree $shape_pkg --notes "AMD $V build: engine, CPU picture reader, GPU picture reader${NOTE_EXTRA:-}" > "$OUT/package.log" 2>&1
118chk $? "packaged"
119cat "$OUT/package.log" >> "$S"
120# every library a bundled file needs is in the package or on binary_deps.py's short base-system list (glibc, libgcc_s, libstdc++);
121# collect_libs.py bundles libatomic.so.1 (librocm_sysdeps_numa.so.1 needs it) with its notice in licenses/
122for d in "$OUT/components/engine"/*/; do python3 packaging/binary_deps.py "$d" >> "$S" 2>&1; chk $? "imports covered in $(basename "$d")"; done
123st "BUILD-DONE"