grammargem/scripts/build-metallib.sh
BigMarc 8a9ffb55af Add real on-device LLM (MLX) for rewrite / tone / Ask
Replace the MLXEngine stub with real Qwen2.5-3B-4bit inference via
mlx-swift-examples (2.25.9): loads the local model snapshot on disk (no
network), chat-templated generation per AIAction, and tone classification.
Wired behind the existing AIEngine seam; AppState resolves the model dir
through ModelManager.

Build notes:
- arm64-only now (MLX is Metal / Apple-Silicon only; matches the spec).
- swift build (CLI) cannot compile mlx-swift's .metal kernels, so the
  metallib is generated once via scripts/build-metallib.sh (xcodebuild) and
  vendored at AppSupport/mlx.metallib; scripts/build.sh ships it as
  Contents/Resources/mlx-swift_Cmlx.bundle/Contents/Resources/default.metallib
  (the path mlx resolves, and it codesigns cleanly).
- A hidden --mlx-selftest entrypoint runs one headless generation to verify
  the runtime end-to-end.

Verified: the signed app's self-test produces a real rewrite; full test
suite green (the MLX integration test is gated behind GRAMMAGEM_MLX_TEST).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-18 07:34:02 +02:00

26 lines
1.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# Regenerate AppSupport/mlx.metallib — the MLX on-device LLM's compiled Metal
# kernels.
#
# `swift build` (SwiftPM CLI) does NOT compile mlx-swift's .metal sources into a
# metallib, but Xcode's build system does. So we build the package once via
# xcodebuild, then vendor the resulting metallib (shipped colocated with the app
# executable; see scripts/build.sh). Run this when bumping the mlx-swift version.
#
# Requires the Metal Toolchain: xcodebuild -downloadComponent MetalToolchain
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT"
DD=".xcode-build"
echo "==> xcodebuild (compiles mlx-swift metal kernels — a few minutes)"
xcodebuild build -scheme GrammaGem -configuration Release \
-destination 'platform=macOS,arch=arm64' \
-derivedDataPath "$DD" -skipMacroValidation >/dev/null
SRC="$DD/Build/Products/Release/mlx-swift_Cmlx.bundle/Contents/Resources/default.metallib"
[ -f "$SRC" ] || { echo "ERROR: metallib not produced at $SRC" >&2; exit 1; }
cp "$SRC" AppSupport/mlx.metallib
echo "==> vendored AppSupport/mlx.metallib ($(du -h AppSupport/mlx.metallib | cut -f1))"