interplane.Get Omega
Read in your language

MojoLlama

Archived Mojo/MAX quantizer and serving research repository (read-only history)

Brought by drakestapleton

branch main Branches Tags History

MojoLlama / src / mojollama / mojo_engine / bench_full.sh

92 lines · 2785 bytes Raw Blame History
1#!/bin/bash
2# Full benchmark: thread × batch sweep + concurrency test
3set -e
4
5SRC="tinyllama_gen_q8.mojo"
6BASE_DIR="$(dirname "$0")"
7cd "$BASE_DIR"
8
9MOJO_LIB="$HOME/.local/share/uv/tools/mojo/lib/python3.11/site-packages/modular/lib"
10LD_LIBRARY_PATH="$MOJO_LIB"
11
12NW_VALUES=(1 2 4 8 16 32)
13B_VALUES=(1 2 4 8)
14RPW_VALUES=(4 8 16)
15
16echo "=== Thread × Batch × UBATCH Sweep ==="
17echo "B,RPW,nw,tok_s"
18printf "%-4s %-5s %-4s %s\n" "B" "RPW" "nw" "tok/s"
19echo "---- ---- ---- ------"
20
21for rpw in "${RPW_VALUES[@]}"; do
22 for b in "${B_VALUES[@]}"; do
23 # Build for this B, RPW
24 cp "$SRC" "${SRC}.bak"
25 sed -i "s/comptime B: Int = [0-9]*/comptime B: Int = $b/" "$SRC"
26 sed -i "s/comptime RPW: Int = [0-9]*/comptime RPW: Int = $rpw/" "$SRC"
27
28 OBJ="${SRC%.mojo}_B${b}_R${rpw}.o"
29 BIN="${SRC%.mojo}_B${b}_R${rpw}"
30
31 mojo build "$SRC" --emit object -o "$OBJ" 2>/dev/null
32 gcc -o "$BIN" "$OBJ" \
33 -L"$MOJO_LIB" \
34 -lKGENCompilerRTShared \
35 -lAsyncRTRuntimeGlobals \
36 -lMSupportGlobals \
37 -lAsyncRTMojoBindings \
38 -lm -lpthread -ldl -lstdc++ 2>/dev/null
39
40 for nw in "${NW_VALUES[@]}"; do
41 # Run 3 times, take median
42 results=()
43 for trial in 1 2 3; do
44 out=$(LD_LIBRARY_PATH="$MOJO_LIB" timeout 90 "./$BIN" "$nw" 2>&1 | grep "tok/s" | tail -1)
45 tok_s=$(echo "$out" | grep -oP '\(\s*[\d.]+' | tail -1 | sed 's/(//')
46 if [ -n "$tok_s" ]; then
47 results+=("$tok_s")
48 fi
49 done
50 if [ ${#results[@]} -ge 1 ]; then
51 IFS=$'\n' sorted=($(sort -n <<< "${results[*]}")); unset IFS
52 mid=$(( ${#sorted[@]} / 2 ))
53 median="${sorted[$mid]}"
54 printf "%-4s %-5s %-4s %s\n" "$b" "$rpw" "$nw" "$median"
55 fi
56 done
57
58 rm -f "$OBJ" "$BIN"
59 mv "${SRC}.bak" "$SRC"
60 done
61done
62
63echo ""
64echo "=== Concurrency Test (B=4, nw=16, 2 instances) ==="
65echo "Building baseline..."
66cp "$SRC" "${SRC}.bak"
67sed -i "s/comptime B: Int = [0-9]*/comptime B: Int = 4/" "$SRC"
68sed -i "s/comptime RPW: Int = [0-9]*/comptime RPW: Int = 8/" "$SRC"
69mojo build "$SRC" --emit object -o tinyllama_q8_conc.o 2>/dev/null
70gcc -o tinyllama_q8_conc tinyllama_q8_conc.o \
71 -L"$MOJO_LIB" \
72 -lKGENCompilerRTShared \
73 -lAsyncRTRuntimeGlobals \
74 -lMSupportGlobals \
75 -lAsyncRTMojoBindings \
76 -lm -lpthread -ldl -lstdc++ 2>/dev/null
77
78echo "Single instance (baseline):"
79LD_LIBRARY_PATH="$MOJO_LIB" timeout 90 ./tinyllama_q8_conc 16 2>&1 | grep "tok/s"
80
81echo "Two concurrent instances:"
82LD_LIBRARY_PATH="$MOJO_LIB" timeout 90 ./tinyllama_q8_conc 8 &
83PID1=$!
84LD_LIBRARY_PATH="$MOJO_LIB" timeout 90 ./tinyllama_q8_conc 8 &
85PID2=$!
86wait $PID1 $PID2
87# Print results
88echo "Concurrent results:"
89
90mv "${SRC}.bak" "$SRC"
91rm -f tinyllama_q8_conc.o tinyllama_q8_conc
92echo "=== Done ==="