interplane.Get Omega
Read in your language

ggml-omega

aien-dev/ggml-omega

Brought by drakestapleton

branch main Branches Tags History

ggml-omega / build.sh

104 lines · 7173 bytes Raw Blame History
1#!/bin/sh
2# Build libggml-omega.so (Linux) or .dylib (macOS): a ggml backend (llama.cpp backend API v3) whose op kernels are seeded-Omega
3# units (kernels/*.om) bound through the omega-bridge native call ABI (src/native.om, docs/NATIVE-ABI.md).
4# OMEGA_GGML_OMC the seeded compiler, sha256 pinned below (omega-mv seed/out/omc at e5b32567)
5# OMEGA_GGML_BIND_OMC pinned baseline compiler for the native binding generator
6# OMEGA_GGML_STAGE all (default), prepare on Linux ARM64, or compile on the target host
7# OMEGA_BRIDGE an aien-dev/omega-bridge checkout; its src/native.om must match the pinned sha256
8# GGML_SRC a llama.cpp checkout at the pinned commit (headers: ggml/include, ggml/src)
9# GGML_LIB the folder holding that checkout's libggml-base.so and libggml-cpu.so
10# OMEGA_GGML_OUT output folder (default build)
11# OMEGA_GGML_MUTANT negative controls: bin-add (the add lane of bin.om computes x - y; test-backend-ops
12# must fail) or math-exp (math.om's e^x series keeps 4 terms instead of 13; tests/kernels.c
13# must fail) or gbmm-k (gbmm.om plans k - 1: the GB10 MUL_MAT drops the last k element;
14# test-backend-ops -b OmegaGB10 must fail) or bfs-lo (bfs.om writes the hi plane into lo:
15# the GB10 GEMV route computes about 2 W x; test-backend-ops -b OmegaGB10 must fail)
16# OMEGA_GGML_GB10 1: also build the GB10 device (ggml-omega-gb10.c, migration-pending host glue) against
17# CUDA++: CUDAPP_SRC (aien-dev/CUDApp src/, needs omega_gpu_gemm_ex.h) and CUDAPP_LIB
18# (its libomega_gpu.a built with -fPIC). Default 0: the host device only.
19set -eu
20PIN=2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f
21LLAMA_PIN=7aca0d2ea8d92b56911233b822db01f9fbb1ae50
22ROOT=$(cd "$(dirname "$0")" && pwd)
23K=$ROOT/kernels
24NATIVE_PIN=e2077722aaeffb1121b70160e22357ea47f369e0039ab1ea06dc700f6f4e45d7
25BIND_PIN=734f0a39319fbfa930bcc62cb30dbf06d47d78e126372b74f5baf478e6126b61
26STAGE=${OMEGA_GGML_STAGE:-all}
27case "$STAGE" in all|prepare|compile) ;; *) echo "invalid OMEGA_GGML_STAGE" >&2; exit 2;; esac
28O=${OMEGA_GGML_OUT:-$ROOT/build}
29GB10=${OMEGA_GGML_GB10:-0}
30if [ "$STAGE" != compile ]; then
31BR=${OMEGA_BRIDGE:?set OMEGA_BRIDGE to an omega-bridge checkout (src/native.om sha256 $NATIVE_PIN)}
32OMC=${OMEGA_GGML_OMC:?set OMEGA_GGML_OMC to the seeded omc (sha256 $PIN)}
33BIND_OMC=${OMEGA_GGML_BIND_OMC:?set OMEGA_GGML_BIND_OMC to the baseline generator compiler}
34got=$(sha256sum < "$BIND_OMC" | cut -d' ' -f1)
35[ "$got" = "$BIND_PIN" ] || { echo "FAIL binding compiler pin" >&2; exit 1; }
36got=$(sha256sum < "$OMC" | cut -d' ' -f1)
37[ "$got" = "$PIN" ] || { echo "FAIL omc sha256 $got, pinned $PIN" >&2; exit 1; }
38got=$(sha256sum < "$BR/src/native.om" | cut -d' ' -f1)
39[ "$got" = "$NATIVE_PIN" ] || { echo "FAIL omega-bridge src/native.om sha256 $got, pinned $NATIVE_PIN" >&2; exit 1; }
40rm -rf "$O"; mkdir -p "$O/units"
41"$BIND_OMC" < "$BR/src/native.om" > "$O/native"; chmod +x "$O/native"
42# file:tile instances. T is the f32/f16 lane count; dq8 takes blocks (B), bytes (W) and floats (X).
43INST="bin:32 bin:1 sq:32 sq:1 rmsc:0 scl:32 scl:1 smw:32 smw:1 smwh:32 smwh:1 vmax:32 vmax:1
44sexp:32 sexp:1 rcp:0 rtsc:0 rope:0 df32:256 df32:32 df32:1 dh16:256 dh16:32 dh16:1 dq8:8 dq8:1
45dq8r:0 h2fv:32 h2fv:1 f2hv:32 f2hv:1 cpy:32 cpy:1 cpyh:32 cpyh:1 swi:32 swi:1 silu:32 silu:1 bfs:256 bfs:1"
46GB10=${OMEGA_GGML_GB10:-0}
47[ "$GB10" = 1 ] && INST="$INST gbmm:0"
48: > "$O/units.in"
49for it in $INST; do
50 f=${it%%:*}; t=${it#*:}; u=$f; [ "$t" = 0 ] || u=${f}_$t
51 { cat "$K/math.om"
52 sed -e "s/@T@/$t/g; s/@B@/$t/g; s/@W@/$((34 * t))/g; s/@X@/$((32 * t))/g" "$K/$f.om"; } > "$O/units/$u.om"
53 if [ "${OMEGA_GGML_MUTANT:-}" = bin-add ] && [ "$f" = bin ]; then
54 sed -i 's/^put d i add x y$/put d i sub x y/' "$O/units/$u.om"
55 grep -q '^put d i sub x y$' "$O/units/$u.om" || { echo "FAIL mutant not applied to $u" >&2; exit 1; }
56 fi
57 if [ "${OMEGA_GGML_MUTANT:-}" = math-exp ]; then
58 sed -i 's/^for i ulong 13$/for i ulong 4/; s/^n = f64 sub 13 i$/n = f64 sub 4 i/' "$O/units/$u.om"
59 grep -q '^for i ulong 4$' "$O/units/$u.om" || { echo "FAIL mutant not applied to $u" >&2; exit 1; }
60 fi
61 if [ "${OMEGA_GGML_MUTANT:-}" = gbmm-k ] && [ "$f" = gbmm ]; then
62 sed -i 's/^put p 2 get d 0$/put p 2 sub get d 0 1/' "$O/units/$u.om"
63 grep -q '^put p 2 sub get d 0 1$' "$O/units/$u.om" || { echo "FAIL mutant not applied to $u" >&2; exit 1; }
64 fi
65 if [ "${OMEGA_GGML_MUTANT:-}" = bfs-lo ] && [ "$f" = bfs ]; then
66 sed -i 's/^put l i ushort shr bitcast uint r 16$/put l i ushort shr u 16/' "$O/units/$u.om"
67 grep -q '^put l i ushort shr u 16$' "$O/units/$u.om" || { echo "FAIL mutant not applied to $u" >&2; exit 1; }
68 fi
69 "$OMC" < "$O/units/$u.om" > "$O/units/$u.bin" || { echo "FAIL omc $u" >&2; exit 1; }
70 { cat "$O/units/$u.om"; printf '\0'; printf '%08d' "$(wc -c < "$O/units/$u.bin")"; cat "$O/units/$u.bin"; } >> "$O/units.in"
71done
72"$O/native" c float < "$O/units.in" > "$O/omega_ggml_kernels.c"
73cp "$ROOT/ggml-omega.c" "$O/backend-source.c"
74(cd "$O" && sha256sum omega_ggml_kernels.c units.in backend-source.c > prepared.sha256)
75printf '%s\n' "$PIN" "$BIND_PIN" "$NATIVE_PIN" "$LLAMA_PIN" > "$O/pins.txt"
76[ "$STAGE" != prepare ] || { echo "PREPARED $O"; exit 0; }
77fi
78SRC=${GGML_SRC:?set GGML_SRC to the pinned llama.cpp checkout}
79LIB=${GGML_LIB:?set GGML_LIB to its libraries}
80head=$(git -C "$SRC" rev-parse HEAD)
81[ "$head" = "$LLAMA_PIN" ] || { echo "FAIL llama.cpp pin" >&2; exit 1; }
82(cd "$O" && shasum -a 256 -c prepared.sha256)
83cmp "$ROOT/ggml-omega.c" "$O/backend-source.c"
84expected=$(printf '%s\n' "$PIN" "$BIND_PIN" "$NATIVE_PIN" "$LLAMA_PIN")
85[ "$(cat "$O/pins.txt")" = "$expected" ] || { echo "FAIL prepared pins" >&2; exit 1; }
86cc -std=gnu11 -O2 -Wall -Wextra -Werror -fPIC -c -o "$O/omega_ggml_kernels.o" "$O/omega_ggml_kernels.c"
87GBC= GBO= GBL=
88if [ "$GB10" = 1 ]; then
89 CS=${CUDAPP_SRC:?set CUDAPP_SRC to aien-dev/CUDApp src/ (with omega_gpu_gemm_ex.h)}
90 CL=${CUDAPP_LIB:?set CUDAPP_LIB to libomega_gpu.a of that checkout, built with -fPIC}
91 [ -f "$CS/omega_gpu_gemm_ex.h" ] || { echo "FAIL $CS has no omega_gpu_gemm_ex.h" >&2; exit 1; }
92 cc -std=gnu11 -O2 -Wall -Wextra -Werror -fPIC -pthread -I"$SRC/ggml/include" -I"$SRC/ggml/src" -I"$CS" -I"$ROOT" \
93 -c -o "$O/ggml-omega-gb10.o" "$ROOT/ggml-omega-gb10.c"
94 GBC=-DOMEGA_GGML_GB10; GBO=$O/ggml-omega-gb10.o; GBL="$CL -lm"
95fi
96cc -std=gnu11 -O2 -Wall -Wextra -Werror -fPIC -pthread $GBC -I"$SRC/ggml/include" -I"$SRC/ggml/src" -I"$O" -I"$ROOT" \
97 -c -o "$O/ggml-omega.o" "$ROOT/ggml-omega.c"
98case "$(uname -s)" in
99 Darwin) LINK_FLAGS="-dynamiclib -Wl,-exported_symbol,_ggml_backend_init"; EXT=dylib ;;
100 *) LINK_FLAGS="-shared -Wl,--version-script=$ROOT/exports.map"; EXT=so ;;
101esac
102cc $LINK_FLAGS -pthread -o "$O/libggml-omega.$EXT" "$O/ggml-omega.o" $GBO "$O/omega_ggml_kernels.o" $GBL \
103 -L"$LIB" -lggml-base -lggml-cpu -Wl,-rpath,"$LIB"
104echo "BUILT $O/libggml-omega.$EXT ($(find "$O/units" -name "*.bin" | wc -l) Omega units, GB10 device $GB10, omc $(echo $PIN | cut -c1-12), llama.cpp $(echo $LLAMA_PIN | cut -c1-12))"