okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
aff3a132f
main
1#!/bin/bash 2# Build the Qwen engine package for Apple Silicon (backend metal): `strata` + `strata-vision` + the Metal sources the engine compiles at start-up. 3# This is the repository's version of the release scripts' Mac stage (phaseA-tests/rel213/scripts/mac_rel213.sh, rel212/scripts/mac_rel212.sh), so a release 4# builds the Mac Qwen package from the tag itself. 5# 6# bash packaging/mac/build_engine.sh --version 0.1.59 [--work DIR] [--build-id ID] [--jobs N] [--stages "engine vision pkg"] 7# 8# Run it in the source tree (a git checkout, or a `git archive` with BUILD_ID beside the sources) on the Mac, under the machine's lock. Stages (default all): 9# engine cmake + ninja: the `strata` target of metal/ vision packaging/build_vision_mac.sh: strata-vision (Metal GPU reader, CPU fallback) 10# pkg stage the files, package_engine.py, --verify, binary_deps.py, `strata --version` 11# It does NOT sign: the package made here is unsigned (packaging/mac/sign-and-notarize.sh --engine signs the folder form; see packaging/ENGINE-COMPONENT.md). 12# 13# Sources of ggml and llama.cpp are the vendored third_party/strata-ggml and third_party/strata-llama: nothing is downloaded. STRATA_GGML_DIR=<llama.cpp checkout> 14# builds from that checkout instead (an override, kept for two releases). Needs Xcode Command Line Tools, cmake + ninja (a pip venv is enough) and Apple's 15# metal-cpp (METAL_CPP_DIR, default ~/strata-mac/metal-cpp). 16# 17# Layout of the package (the engine finds these beside its executable, metal/src/resource_path.h): 18# strata, strata-vision, BUILD.json 19# metal/kernels/*.metal metal/engine-msl/*.metal metal/data/{expert-profile.bin,draft_vocab.bin} 20# metal/strata-quant-common.h the quant header's TEXT (the runtime reads it: it cannot follow an #include). It is the real file of 21# third_party/strata-ggml/, NOT third_party/ggml/ggml-common.h, which is only a one-line forwarder since own-stack 1B. 22# metal/ggml-common.h the same text under the old name, kept for two releases (an engine of an earlier release reads this name) 23set -eo pipefail 24ROOT =$( cd " $( dirname " $ 0" ) /../.." && pwd ) 25VERSION ="" 26WORK =" $ ROOT /pkg-mac-work" 27BUILD_ID ="" 28JOBS =2 29STAGES ="engine vision pkg" 30while [$ # -gt 0 ];do 31case " $ 1 " in 32 --version)VERSION =" $ 2 " ;shift 2;; 33 --work)WORK =" $ 2 " ;shift 2;; 34 --build-id)BUILD_ID =" $ 2 " ;shift 2;; 35 --jobs)JOBS =" $ 2 " ;shift 2;; 36 --stages)STAGES =" $ 2 " ;shift 2;; 37 *)echo "usage: $ 0 --version V [--work DIR] [--build-id ID] [--jobs N] [--stages \"engine vision pkg\"]" ;exit 2;; 38esac 39done 40[ -n" $ VERSION " ] || {echo "--version is required (the engine version of the release, e.g. 0.1.59)" ;exit 2; } 41has () {case " $ STAGES " in *" $ 1 " *)return 0;;esac ;return 1; } 42CMAKE =${ CMAKE :-$( command -v cmake || echo " $ HOME /strata-mac/tools/bin/cmake" )} 43NINJA =${ NINJA :-$( command -v ninja || echo " $ HOME /strata-mac/tools/bin/ninja" )} 44METAL_CPP_DIR =${ METAL_CPP_DIR :- $ HOME /strata-mac/metal-cpp} 45if [ -z" $ BUILD_ID " ];then # the commit the binary is built from: the tree's own git, else a BUILD_ID file beside the sources 46BUILD_ID =$( git -C " $ ROOT " rev-parse --short=9 HEAD 2 > /dev/null || head -1 " $ ROOT /BUILD_ID" 2 > /dev/null || echo unknown) 47fi 48[" $ BUILD_ID " != unknown ] || {echo "no build id: build from a git checkout, put the commit in BUILD_ID, or pass --build-id" ;exit 2; } 49GGML ="" ; [ -z" ${ STRATA_GGML_DIR :-} " ] ||GGML ="-DSTRATA_GGML_DIR= $ STRATA_GGML_DIR " # override only; default the vendored third_party/strata-ggml 50VISION_LLAMA ="" ; [ -z" ${ STRATA_GGML_DIR :-} " ] ||VISION_LLAMA ="--llama $ STRATA_GGML_DIR " 51BUILD =" $ WORK /metal-build" 52STAGE =" $ WORK /stage-metal" 53OUT =" $ WORK /pkg-mac" 54PKG =" $ OUT / $ VERSION -macos-arm64-metal" 55mkdir -p " $ WORK " 56cd " $ ROOT " 57export STRATA_COMMIT =${ STRATA_COMMIT :- $ BUILD_ID } 58export CMAKE_BUILD_PARALLEL_LEVEL =$ JOBS 59 60if has engine;then 61echo "== metal engine build (strata)" 62rm -rf " $ BUILD " 63# shellcheck disable=SC2086 64nice -n 19" $ CMAKE " -S metal-B " $ BUILD " -G Ninja-DCMAKE_BUILD_TYPE=Release -DCMAKE_MAKE_PROGRAM= " $ NINJA " -DMETAL_CPP_DIR= " $ METAL_CPP_DIR " $ GGML \ 65-DSTRATA_BUILD_ID= " $ BUILD_ID " > " $ WORK /metal-cfg.log" 2 >&1 || {tail -30 " $ WORK /metal-cfg.log" ;exit 1; } 66nice -n 19" $ NINJA " -j " $ JOBS " -C " $ BUILD " strata> " $ WORK /metal-build.log" 2 >&1 || {tail -30 " $ WORK /metal-build.log" ;exit 1; } 67tail -3 " $ WORK /metal-build.log" 68fi 69 70if has vision;then 71echo "== strata-vision (Metal GPU reader + CPU fallback)" 72rm -rf " $ WORK /vision-out" 73# shellcheck disable=SC2086 74nice -n 19 bash packaging/build_vision_mac.sh$ VISION_LLAMA --out " $ WORK /vision-out" --build " $ WORK /vision-build" --jobs " $ JOBS " 75fi 76 77if has pkg;then 78echo "== metal engine package (strata + strata-vision)" 79QUANT =third_party/strata-ggml/strata-quant-common.h 80 [ -f" $ QUANT " ]&& [" $( head -c 24 " $ QUANT " ) " ="#ifndef GGML_COMMON_DECL" ] || {echo " $ QUANT is missing or is not the header text" ;exit 1; } 81rm -rf " $ STAGE " ;mkdir -p " $ STAGE /metal/kernels" " $ STAGE /metal/engine-msl" " $ STAGE /metal/data" 82cp " $ BUILD /strata" " $ STAGE /" 83cp " $ WORK /vision-out/strata-vision" " $ STAGE /" # BEFORE package_engine.py: it takes strata-vision from --src 84cp metal/kernels/*.metal" $ STAGE /metal/kernels/" 85cp metal/src/engine/msl/*.metal" $ STAGE /metal/engine-msl/" 86cp " $ QUANT " " $ STAGE /metal/strata-quant-common.h" 87cp " $ QUANT " " $ STAGE /metal/ggml-common.h" # the old name, same text, for two releases 88cp data/expert-profile.bin data/draft_vocab.bin" $ STAGE /metal/data/" 89echo '{"version": "'" $ VERSION "'", "source": "packaging/mac/build_engine.sh '" $ BUILD_ID "'", "backend": "metal", "kernels": "compiled at start from metal/ beside the binary", "portable": true, "vision": "metal"}' > " $ STAGE /BUILD.json" 90rm -rf " $ OUT " ;mkdir -p " $ OUT " 91python3 packaging/package_engine.py--src " $ STAGE " --backend metal--version " $ VERSION " --out " $ OUT " --arch apple-gpu--build-id " $ BUILD_ID +metal" --platform macos-arm64--tree 92python3 packaging/package_engine.py--verify " $ PKG " 93python3 packaging/binary_deps.py" $ PKG " 94test -f " $ PKG /strata-vision" || {echo "strata-vision is not in the Mac engine package" ;exit 1; } 95# the quant header must be the text in the package, under both names (a forwarder there would leave the Metal kernels without their block layouts) 96for n in strata-quant-common.h ggml-common.h;do 97cmp " $ PKG /metal/ $ n " " $ QUANT " || {echo "metal/ $ n in the package is not the header text" ;exit 1; } 98done 99" $ PKG /strata" --version 100echo "== done: $ PKG (unsigned)" 101fi