|
| 1 | +#!/usr/bin/env bash |
| 2 | +# check_sync.sh — verify critical code sections are in sync between |
| 3 | +# quant.h (single header) and src/ (split sources). |
| 4 | +# |
| 5 | +# This catches the #67-class bug: a feature implemented in quant.h |
| 6 | +# but not ported to the split sources (or vice versa). |
| 7 | +# |
| 8 | +# Usage: bash scripts/check_sync.sh |
| 9 | +# Returns 0 if all checks pass, 1 if any drift is detected. |
| 10 | + |
| 11 | +set -euo pipefail |
| 12 | + |
| 13 | +HEADER="quant.h" |
| 14 | +RED='\033[0;31m' |
| 15 | +GREEN='\033[0;32m' |
| 16 | +YELLOW='\033[1;33m' |
| 17 | +NC='\033[0m' |
| 18 | + |
| 19 | +ERRORS=0 |
| 20 | + |
| 21 | +check_marker_list() { |
| 22 | + local label="$1" |
| 23 | + local file1="$2" |
| 24 | + local file2="$3" |
| 25 | + local pattern="$4" |
| 26 | + |
| 27 | + local list1 list2 |
| 28 | + list1=$(grep -o "$pattern" "$file1" 2>/dev/null | sort -u) |
| 29 | + list2=$(grep -o "$pattern" "$file2" 2>/dev/null | sort -u) |
| 30 | + |
| 31 | + if [ "$list1" = "$list2" ]; then |
| 32 | + echo -e " ${GREEN}✓${NC} $label" |
| 33 | + else |
| 34 | + echo -e " ${RED}✗${NC} $label — MISMATCH" |
| 35 | + diff <(echo "$list1") <(echo "$list2") || true |
| 36 | + ERRORS=$((ERRORS + 1)) |
| 37 | + fi |
| 38 | +} |
| 39 | + |
| 40 | +check_field_exists() { |
| 41 | + local label="$1" |
| 42 | + local field="$2" |
| 43 | + local file="$3" |
| 44 | + |
| 45 | + if grep -q "$field" "$file" 2>/dev/null; then |
| 46 | + echo -e " ${GREEN}✓${NC} $label: '$field' found in $(basename $file)" |
| 47 | + else |
| 48 | + echo -e " ${RED}✗${NC} $label: '$field' MISSING in $(basename $file)" |
| 49 | + ERRORS=$((ERRORS + 1)) |
| 50 | + fi |
| 51 | +} |
| 52 | + |
| 53 | +check_both_have() { |
| 54 | + local label="$1" |
| 55 | + local pattern="$2" |
| 56 | + local file1="$3" |
| 57 | + local file2="$4" |
| 58 | + |
| 59 | + local has1 has2 |
| 60 | + has1=$(grep -c "$pattern" "$file1" 2>/dev/null || echo 0) |
| 61 | + has2=$(grep -c "$pattern" "$file2" 2>/dev/null || echo 0) |
| 62 | + |
| 63 | + if [ "$has1" -gt 0 ] && [ "$has2" -gt 0 ]; then |
| 64 | + echo -e " ${GREEN}✓${NC} $label: present in both files" |
| 65 | + elif [ "$has1" -eq 0 ] && [ "$has2" -eq 0 ]; then |
| 66 | + echo -e " ${YELLOW}—${NC} $label: absent in both (OK if not yet needed)" |
| 67 | + else |
| 68 | + local missing |
| 69 | + [ "$has1" -eq 0 ] && missing="$(basename $file1)" || missing="$(basename $file2)" |
| 70 | + echo -e " ${RED}✗${NC} $label: MISSING in $missing" |
| 71 | + ERRORS=$((ERRORS + 1)) |
| 72 | + fi |
| 73 | +} |
| 74 | + |
| 75 | +echo "=== quant.h ↔ split-source sync check ===" |
| 76 | +echo "" |
| 77 | + |
| 78 | +# --- 1. CHAT_END_MARKERS list --- |
| 79 | +echo "[1] CHAT_END_MARKERS (template token filter)" |
| 80 | +# Extract only the markers from the CHAT_END_MARKERS array definition |
| 81 | +extract_markers() { |
| 82 | + sed -n '/CHAT_END_MARKERS\[\]/,/NULL/p' "$1" | grep -o '"[^"]*"' | sort -u |
| 83 | +} |
| 84 | +local_m1=$(extract_markers "$HEADER") |
| 85 | +local_m2=$(extract_markers "src/engine/tq_generate.c") |
| 86 | +if [ "$local_m1" = "$local_m2" ]; then |
| 87 | + echo -e " ${GREEN}✓${NC} End markers" |
| 88 | +else |
| 89 | + echo -e " ${RED}✗${NC} End markers — MISMATCH" |
| 90 | + diff <(echo "$local_m1") <(echo "$local_m2") || true |
| 91 | + ERRORS=$((ERRORS + 1)) |
| 92 | +fi |
| 93 | + |
| 94 | +# --- 2. Phi-3 fused tensor support --- |
| 95 | +echo "" |
| 96 | +echo "[2] Phi-3 fused tensor fields" |
| 97 | +check_field_exists "Config: has_fused_qkv" "has_fused_qkv" "include/turboquant/tq_engine.h" |
| 98 | +check_field_exists "Config: has_fused_up_gate" "has_fused_up_gate" "include/turboquant/tq_engine.h" |
| 99 | +check_field_exists "Layer: gguf_w_qkv" "gguf_w_qkv" "include/turboquant/tq_engine.h" |
| 100 | +check_field_exists "Layer: gguf_w_up_gate" "gguf_w_up_gate" "include/turboquant/tq_engine.h" |
| 101 | +check_field_exists "Config: rope_factors_short" "rope_factors_short" "include/turboquant/tq_engine.h" |
| 102 | + |
| 103 | +# --- 3. Fused QKV forward path --- |
| 104 | +echo "" |
| 105 | +echo "[3] Fused QKV forward path" |
| 106 | +check_both_have "Fused QKV matmul" "gguf_w_qkv" \ |
| 107 | + "$HEADER" "src/engine/tq_transformer.c" |
| 108 | +check_both_have "Fused FFN gate||up" "gguf_w_up_gate" \ |
| 109 | + "$HEADER" "src/engine/tq_transformer.c" |
| 110 | + |
| 111 | +# --- 4. LongRoPE --- |
| 112 | +echo "" |
| 113 | +echo "[4] LongRoPE rotation" |
| 114 | +check_both_have "rope_factors_short" "rope_factors_short" \ |
| 115 | + "$HEADER" "src/engine/tq_transformer.c" |
| 116 | +check_both_have "rope_factors_long" "rope_factors_long" \ |
| 117 | + "$HEADER" "src/engine/tq_transformer.c" |
| 118 | + |
| 119 | +# --- 5. BOS token handling --- |
| 120 | +echo "" |
| 121 | +echo "[5] BOS token handling" |
| 122 | +check_both_have "BOS <s> lookup in tokenizer" '"<s>"' \ |
| 123 | + "$HEADER" "src/engine/tq_tokenizer.c" |
| 124 | +check_both_have "BOS <s> auto-detect in generate" '"<s>"' \ |
| 125 | + "$HEADER" "src/engine/tq_generate.c" |
| 126 | +check_both_have "BOS <|begin_of_text|> lookup" '"<|begin_of_text|>"' \ |
| 127 | + "$HEADER" "src/engine/tq_tokenizer.c" |
| 128 | + |
| 129 | +# --- 6. Hybrid attention stride (GQA fix) --- |
| 130 | +echo "" |
| 131 | +echo "[6] Hybrid attention cache stride" |
| 132 | +check_both_have "max_head_dim in quant cache" "max_head_dim" \ |
| 133 | + "$HEADER" "src/engine/tq_transformer.c" |
| 134 | +check_both_have "max_kv_heads in quant cache" "max_kv_heads" \ |
| 135 | + "$HEADER" "src/engine/tq_transformer.c" |
| 136 | + |
| 137 | +# --- 7. Memory free completeness --- |
| 138 | +echo "" |
| 139 | +echo "[7] GGUF dequant memory free" |
| 140 | +check_both_have "free(layer->attn_norm)" "free(layer->attn_norm)" \ |
| 141 | + "$HEADER" "src/engine/tq_model.c" |
| 142 | + |
| 143 | +# --- Summary --- |
| 144 | +echo "" |
| 145 | +echo "=========================================" |
| 146 | +if [ "$ERRORS" -eq 0 ]; then |
| 147 | + echo -e " ${GREEN}ALL CHECKS PASSED${NC}" |
| 148 | +else |
| 149 | + echo -e " ${RED}$ERRORS SYNC ISSUES DETECTED${NC}" |
| 150 | +fi |
| 151 | +echo "=========================================" |
| 152 | +exit "$ERRORS" |
0 commit comments