#!/bin/zsh # Speculative decoding on Apple Silicon: Qwen3.5-9B draft -> Qwen 27B target, plus the 35B-A3B MoE. # Runs end to end offline in <= 40 minutes and writes results.json. # ./run.sh full run (needs the GPU) # ./run.sh --smoke CPU-only smoke test of the pipeline with a synthetic backend (no GPU, fast) set -u cd "${0:A:h}" PY=/opt/homebrew/Caskroom/miniconda/base/bin/python DRAFT=mlx-community/Qwen3.5-9B-MLX-4bit MOE=mlx-community/Qwen3.6-35B-A3B-4bit TOTAL_S=$(( 38 * 60 )) # hard cap for the benchmark part, leaves ~2 min headroom MOE_RESERVE_S=$(( 5 * 60 )) # time kept for the MoE reference run ANALYSIS_RESERVE_S=60 export HF_HUB_OFFLINE=1 TRANSFORMERS_OFFLINE=1 HF_DATASETS_OFFLINE=1 TOKENIZERS_PARALLELISM=false export MPLCONFIGDIR="$PWD/.mplconfig" PIP_NO_CACHE_DIR=1 PYTHONUNBUFFERED=1 SMOKE=""; OUTDIR=out; RESULTS=results.json if [[ "${1:-}" == "--smoke" ]]; then SMOKE="--fake"; OUTDIR=out-smoke; RESULTS=results-smoke.json fi START=$(date +%s) DEADLINE=$(( START + TOTAL_S )) export SPEC_OUT="$PWD/$OUTDIR" rm -rf "$OUTDIR"; mkdir -p "$OUTDIR" "$MPLCONFIGDIR" LOG="$OUTDIR/run.log" exec > >(tee -a "$LOG") 2>&1 echo "== start $(date) (deadline in $(( TOTAL_S / 60 )) min)" # analysis env: pinned pandas/matplotlib from ./wheels, installed offline if [[ ! -x .venv-analysis/bin/python ]]; then $PY -m venv .venv-analysis && .venv-analysis/bin/pip install -q --no-index --find-links wheels \ pandas==3.0.6 matplotlib==3.11.2 numpy==2.5.4 || { echo "analysis venv failed"; exit 1; } fi echo "== environment + thermal state (before)" $PY envinfo.py before echo "== rollback-logic unit test (numpy stand-in for MLX)" $PY test_specdec.py | tail -1 || { echo "specdec test failed"; exit 1; } echo "== tokenizer compatibility check" $PY compat.py | grep -v "^Warning" || { echo "compat check failed"; exit 1; } PRIMARY=$($PY -c "import json; print(json.load(open('$OUTDIR/compat.json'))['primary_target'] or '')") SECONDARY=$($PY -c "import json; print(json.load(open('$OUTDIR/compat.json'))['secondary_target'] or '')") if [[ -z "$PRIMARY" ]]; then echo "no valid target/draft pair, stopping"; exit 1; fi echo "primary target: $PRIMARY secondary: ${SECONDARY:-none}" NOW=$(date +%s) T_DEADLINE=$(( DEADLINE - MOE_RESERVE_S - ANALYSIS_RESERVE_S )) if [[ -n "$SECONDARY" ]]; then # both targets valid: ~60% of the dense time to the primary, the rest to the secondary (base vs k4) T1_DEADLINE=$(( NOW + (T_DEADLINE - NOW) * 6 / 10 )) else T1_DEADLINE=$T_DEADLINE fi echo "== dense target + draft (one process, both models resident)" $PY bench.py --model "$PRIMARY" --draft "$DRAFT" --configs base,k2,k4,k6 --reps 3 \ --temp-pass --draft-alone --microbench --stock-check --reserve-s 45 \ --budget-s $(( T1_DEADLINE - $(date +%s) )) --deadline $T1_DEADLINE --tag target --out "$OUTDIR" $SMOKE \ || echo "!! target bench exited with an error" if [[ -n "$SECONDARY" ]]; then echo "== secondary dense target" $PY bench.py --model "$SECONDARY" --draft "$DRAFT" --configs base,k4 --reps 3 --reserve-s 0 \ --budget-s $(( T_DEADLINE - $(date +%s) )) --deadline $T_DEADLINE --tag target2 --out "$OUTDIR" $SMOKE \ || echo "!! secondary bench exited with an error" fi echo "== MoE reference (no draft)" M_DEADLINE=$(( DEADLINE - ANALYSIS_RESERVE_S )) $PY bench.py --model "$MOE" --configs base --reps 3 --reserve-s 0 \ --budget-s $(( M_DEADLINE - $(date +%s) )) --deadline $M_DEADLINE --tag moe --out "$OUTDIR" $SMOKE \ || echo "!! moe bench exited with an error" echo "== thermal state (after)" $PY envinfo.py after echo "== analysis" .venv-analysis/bin/python analyze.py --out "$OUTDIR" --results "$RESULTS" echo "== done in $(( ($(date +%s) - START) / 60 )) min $(( ($(date +%s) - START) % 60 )) s -> $RESULTS"