Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

2.9 KB
Raw Blame
#!/usr/bin/env bash
#
# check-metal-shaders.sh — compile the Mac sidecar's embedded Metal sources
# with the Metal compiler of the macOS this runs on (A40, GH #259, 3.1.2).
#
# The sources are the files cmake writes for GGML_METAL_EMBED_LIBRARY
# (<build>/ggml/src/ggml-metal/autogenerated/ggml-metal-embed-<kind>.metal),
# which are byte for byte what the binary carries and what a user's Mac
# compiles at the first model load. Each one is compiled twice, with and
# without GGML_METAL_HAS_BF16, through scripts/metal-shader-check.m.
#
# Usage:
#   scripts/check-metal-shaders.sh <autogenerated dir>
#   scripts/check-metal-shaders.sh --from-source
#       configures the pinned, patched llama.cpp (scripts/build-llama.sh) and
#       builds only the ggml-metal target, then checks its sources. For a
#       runner that does not build the whole sidecar (metal-shaders.yml).
#
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

die() { printf '[check-metal] ERROR: %s\n' "$*" >&2; exit 1; }

[ "$(uname -s)" = "Darwin" ] || die "this check needs macOS: it asks the system's own Metal compiler"

if [ "${1:-}" = "--from-source" ]; then
  # shellcheck source=build-llama.sh
  source "$SCRIPT_DIR/build-llama.sh"
  require_cmake
  ensure_src
  build_dir="$CACHE_DIR/metal-check-aarch64-apple-darwin"
  flags="$(cmake_flags_for aarch64-apple-darwin)"
  # shellcheck disable=SC2086
  cmake -S "$SRC_DIR" -B "$build_dir" $flags >/dev/null
  cmake --build "$build_dir" --config Release --target ggml-metal -j "${BUILD_JOBS:-3}" >/dev/null
  dir="$build_dir/ggml/src/ggml-metal/autogenerated"
else
  dir="${1:-}"
fi

[ -n "$dir" ] && [ -d "$dir" ] || die "no autogenerated directory given or found: '${dir:-}'"

sources=()
while IFS= read -r f; do sources+=("$f"); done < <(find "$dir" -maxdepth 1 -type f -name 'ggml-metal-embed-*.metal' | LC_ALL=C sort)
# b11960 writes 36. Fewer than a handful means the path is wrong, not that
# there is little to check, and an empty list must never pass.
[ "${#sources[@]}" -ge 10 ] || die "found ${#sources[@]} embedded Metal sources under $dir, expected the full set"

work="$(mktemp -d)"
trap 'rm -rf "$work"' EXIT
xcrun clang -fobjc-arc -framework Foundation -framework Metal \
  -o "$work/metal-shader-check" "$SCRIPT_DIR/metal-shader-check.m"

echo "macOS $(sw_vers -productVersion) ($(sw_vers -buildVersion)), ${#sources[@]} embedded Metal sources"
status=0
for bf16 in 1 0; do
  "$work/metal-shader-check" "$bf16" "${sources[@]}" || status=$?
  # 2 = no Metal device: nothing was checked, stop here and say so.
  [ "$status" -ne 2 ] || die "no Metal device on this machine, the shaders were not checked"
done
if [ "$status" -ne 0 ]; then
  die "the Metal compiler of macOS $(sw_vers -productVersion) refuses the sidecar's shaders. Every Mac on this version would fail to load any model (see GH #259)."
fi
echo "OK: every embedded Metal source compiles on macOS $(sw_vers -productVersion)"