File size: 5,595 Bytes
3ccaf5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env bash
###############################################################################
# StepProbe — Quick Sanity Test (no GPU needed for most tests)
#
# Validates that all modules work correctly before running the full pipeline.
#
# Usage:
#   bash scripts/test_sanity.sh
###############################################################################
set -euo pipefail

PROJECT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
cd "$PROJECT_DIR"

PASS=0
FAIL=0

check() {
    local name=$1; shift
    echo -n "  Testing: $name ... "
    if eval "$@" &>/dev/null; then
        echo "✅"
        ((PASS++))
    else
        echo "❌"
        ((FAIL++))
    fi
}

echo "=================================================================="
echo "  StepProbe — Sanity Tests"
echo "=================================================================="
echo ""

# 1. Python imports
echo "📦 Module Imports:"
check "stepprobe.segment"  "python -c 'from stepprobe.segment import segment_cot'"
check "stepprobe.align"    "python -c 'from stepprobe.align import align_steps'"
check "stepprobe.diagnose" "python -c 'from stepprobe.diagnose import diagnose_single_problem, LLMJudge, RuleBasedJudge'"
check "stepprobe.metrics"  "python -c 'from stepprobe.metrics import compute_first_failure_step, compute_error_cascade_rate, compute_step_survival_rate, aggregate_metrics'"
check "stepprobe.restore"  "python -c 'from stepprobe.restore import build_silver_bullet_dataset, format_for_sft, format_for_dpo'"
check "stepprobe.utils"    "python -c 'from stepprobe.utils import check_answer, normalize_answer, extract_number, extract_gsm8k_answer'"
echo ""

# 2. Dependencies
echo "📚 Key Dependencies:"
check "torch"          "python -c 'import torch; print(torch.__version__)'"
check "transformers"   "python -c 'import transformers; print(transformers.__version__)'"
check "peft"           "python -c 'import peft; print(peft.__version__)'"
check "datasets"       "python -c 'import datasets; print(datasets.__version__)'"
check "matplotlib"     "python -c 'import matplotlib; print(matplotlib.__version__)'"
check "numpy"          "python -c 'import numpy; print(numpy.__version__)'"
echo ""

# 3. GPU
echo "🖥  GPU:"
check "CUDA available" "python -c 'import torch; assert torch.cuda.is_available()'"
check "GPU memory"     "python -c 'import torch; m=torch.cuda.get_device_properties(0).total_mem/1e9; print(f\"{m:.0f}GB\"); assert m >= 20'"
echo ""

# 4. Core functionality
echo "🧪 Core Functions:"

check "Segmentation" "python -c \"
from stepprobe.segment import segment_cot
seg = segment_cot('t1', 'Step 1: compute 2+2=4\nStep 2: answer is 4\n\\\\boxed{4}')
assert len(seg.steps) >= 2
assert seg.final_answer == '4'
\""

check "Alignment (DTW)" "python -c \"
from stepprobe.align import align_steps
ref = [{'index': 0, 'text': 'step A'}, {'index': 1, 'text': 'step B'}]
hyp = [{'index': 0, 'text': 'step A'}, {'index': 1, 'text': 'step C'}]
aligns = align_steps(ref, hyp, method='dtw')
assert len(aligns) == 2
\""

check "Answer checking" "python -c \"
from stepprobe.utils import check_answer
assert check_answer('42', '42')
assert check_answer('42.0', '42')
assert not check_answer('43', '42')
assert check_answer('\\\\boxed{7}', '7')
\""

check "FFS computation" "python -c \"
from stepprobe.metrics import compute_first_failure_step
steps = [{'index':0,'is_correct':True},{'index':1,'is_correct':False},{'index':2,'is_correct':False}]
assert compute_first_failure_step(steps) == 1
\""

check "ECR computation" "python -c \"
from stepprobe.metrics import compute_error_cascade_rate
steps = [{'index':0,'is_correct':True},{'index':1,'is_correct':False},{'index':2,'is_correct':False}]
ecr = compute_error_cascade_rate(steps)
assert ecr == 1.0
\""

check "SSR computation" "python -c \"
from stepprobe.metrics import compute_step_survival_rate
all_steps = [[{'index':0,'is_correct':True},{'index':1,'is_correct':False}]] * 10
ssr = compute_step_survival_rate(all_steps, max_depth=3)
assert ssr[0] == 1.0 and ssr[1] == 0.0
\""

check "Rule-based judge" "python -c \"
from stepprobe.diagnose import RuleBasedJudge
j = RuleBasedJudge()
etype = j.classify_error('prob', '5 x 8 = 40', '5 x 8 = 42')
assert etype in ['conceptual','methodological','executional','logical']
\""

check "Silver Bullet build" "python -c \"
from stepprobe.restore import build_silver_bullet_dataset, RestorationSample
diag = [{'problem_id':'p1','is_correct_final':False,'raw_output':'wrong','steps':[{'index':0,'is_correct':False,'error_type':'executional'}]}]
ref = [{'problem_id':'p1','raw_output':'correct','steps':[{'index':0,'text':'right'}]}]
probs = [{'problem_id':'p1','question':'q','answer':'a'}]
samples, stats = build_silver_bullet_dataset(diag, ref, probs, max_samples=10)
assert len(samples) >= 1
\""

check "Figure generation (demo)" "python scripts/make_figures.py --demo --output /tmp/stepprobe_test_figs"
echo ""

# 5. Script entry points
echo "📜 Script Entry Points:"
check "run_inference.py --help" "python scripts/run_inference.py --help"
check "run_eval.py --help"      "python scripts/run_eval.py --help"
check "make_figures.py --help"  "python scripts/make_figures.py --help"
echo ""

# Summary
echo "=================================================================="
echo "  Results: $PASS passed, $FAIL failed"
echo "=================================================================="

if [[ $FAIL -gt 0 ]]; then
    echo "  ⚠️  Some tests failed. Fix issues before running the pipeline."
    exit 1
else
    echo "  ✅ All tests passed! Ready to run: bash run_all.sh --quick"
fi