-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_routing_system_gpu3.sh
More file actions
executable file
·451 lines (369 loc) · 14.2 KB
/
Copy pathrun_routing_system_gpu3.sh
File metadata and controls
executable file
·451 lines (369 loc) · 14.2 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#!/bin/bash
# ====================================================================
# 智能数学问题路由系统 - 完整运行流程脚本
# 指定在GPU3上运行,使用Llama3.1-8B模型
# ====================================================================
set -e # 遇到错误立即退出
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# 配置参数
MODEL_NAME="llama3.1-8b"
SAMPLES=200
EVAL_SAMPLES=50
GPU_ID=4
PROJECT_DIR="/home/czy/CISC_PRO"
# 日志文件
LOG_DIR="${PROJECT_DIR}/logs"
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
MAIN_LOG="${LOG_DIR}/routing_system_${TIMESTAMP}.log"
# 创建日志目录
mkdir -p $LOG_DIR
# 函数定义
print_header() {
echo -e "\n${CYAN}================================================================${NC}"
echo -e "${CYAN}$1${NC}"
echo -e "${CYAN}================================================================${NC}\n"
}
print_step() {
echo -e "\n${GREEN}🚀 步骤 $1: $2${NC}"
echo "$(date '+%Y-%m-%d %H:%M:%S') - 步骤 $1: $2" >> $MAIN_LOG
}
print_success() {
echo -e "${GREEN}✅ $1${NC}"
echo "$(date '+%Y-%m-%d %H:%M:%S') - 成功: $1" >> $MAIN_LOG
}
print_error() {
echo -e "${RED}❌ $1${NC}"
echo "$(date '+%Y-%m-%d %H:%M:%S') - 错误: $1" >> $MAIN_LOG
}
print_warning() {
echo -e "${YELLOW}⚠️ $1${NC}"
echo "$(date '+%Y-%m-%d %H:%M:%S') - 警告: $1" >> $MAIN_LOG
}
print_info() {
echo -e "${BLUE}ℹ️ $1${NC}"
echo "$(date '+%Y-%m-%d %H:%M:%S') - 信息: $1" >> $MAIN_LOG
}
# 检查函数
check_prerequisites() {
print_step "0" "检查系统环境和依赖"
# 检查目录
if [ ! -d "$PROJECT_DIR" ]; then
print_error "项目目录不存在: $PROJECT_DIR"
exit 1
fi
cd $PROJECT_DIR
print_info "工作目录: $(pwd)"
# 检查GPU
if command -v nvidia-smi &> /dev/null; then
print_info "检查GPU状态..."
nvidia-smi --query-gpu=index,name,memory.used,memory.total --format=csv,noheader,nounits | while IFS=, read gpu_id name mem_used mem_total; do
if [ "$gpu_id" == "$GPU_ID" ]; then
print_info "目标GPU $GPU_ID: $name (内存: ${mem_used}MB/${mem_total}MB)"
fi
done
# 设置GPU环境变量
export CUDA_VISIBLE_DEVICES=$GPU_ID
print_info "设置使用GPU: $GPU_ID"
else
print_warning "nvidia-smi未找到,无法检查GPU状态"
fi
# 检查Python环境
if command -v python &> /dev/null; then
python_version=$(python --version 2>&1)
print_info "Python版本: $python_version"
print_info "Python路径: $(which python)"
else
print_error "Python未找到"
exit 1
fi
# 检查必要的Python包
print_info "检查Python依赖包..."
python -c "
import sys
try:
import torch
import transformers
import datasets
import pandas
import numpy
print('✅ 所有依赖包已安装')
print(f'PyTorch版本: {torch.__version__}')
print(f'Transformers版本: {transformers.__version__}')
print(f'CUDA可用: {torch.cuda.is_available()}')
if torch.cuda.is_available():
print(f'可用GPU数量: {torch.cuda.device_count()}')
except ImportError as e:
print(f'❌ 缺少依赖包: {e}')
sys.exit(1)
" || exit 1
# 检查模型文件
MODEL_PATH="/data/Meta-Llama-3.1-8B-Instruct"
if [ -d "$MODEL_PATH" ]; then
print_info "模型路径验证通过: $MODEL_PATH"
else
print_error "模型路径不存在: $MODEL_PATH"
exit 1
fi
# 检查脚本文件
for script in role_profiles.py question_classifier.py performance_analyzer.py intelligent_router.py; do
if [ -f "$script" ]; then
print_info "脚本文件检查通过: $script"
else
print_error "脚本文件不存在: $script"
exit 1
fi
done
print_success "系统环境检查完成"
}
# 运行角色性能画像
run_role_profiling() {
print_step "1" "执行角色性能画像 (样本数: $SAMPLES)"
local log_file="${LOG_DIR}/role_profiling_${TIMESTAMP}.log"
print_info "开始角色性能画像..."
print_info "这可能需要较长时间,请耐心等待..."
if CUDA_VISIBLE_DEVICES=$GPU_ID python role_profiles.py \
--model $MODEL_NAME \
--samples $SAMPLES \
2>&1 | tee $log_file; then
# 查找生成的文件
profile_file=$(find role_profiling_results -name "role_profile_detailed_*.csv" -type f -printf '%T@ %p\n' | sort -n | tail -1 | cut -d' ' -f2-)
if [ -n "$profile_file" ] && [ -f "$profile_file" ]; then
print_success "角色画像完成,文件: $profile_file"
echo "PROFILE_FILE=$profile_file" >> "${PROJECT_DIR}/pipeline_vars.env"
else
print_error "角色画像文件未找到"
return 1
fi
else
print_error "角色性能画像执行失败"
return 1
fi
}
# 运行问题分类
run_question_classification() {
print_step "2" "执行问题分类 (样本数: $SAMPLES)"
local log_file="${LOG_DIR}/question_classification_${TIMESTAMP}.log"
print_info "开始问题分类..."
if CUDA_VISIBLE_DEVICES=$GPU_ID python question_classifier.py \
--model $MODEL_NAME \
--samples $SAMPLES \
2>&1 | tee $log_file; then
# 查找生成的文件
classification_file=$(find question_classification_results -name "question_categories_*.csv" -type f -printf '%T@ %p\n' | sort -n | tail -1 | cut -d' ' -f2-)
if [ -n "$classification_file" ] && [ -f "$classification_file" ]; then
print_success "问题分类完成,文件: $classification_file"
echo "CLASSIFICATION_FILE=$classification_file" >> "${PROJECT_DIR}/pipeline_vars.env"
else
print_error "问题分类文件未找到"
return 1
fi
else
print_error "问题分类执行失败"
return 1
fi
}
# 运行性能分析
run_performance_analysis() {
print_step "3" "执行性能分析与路由指南生成"
# 加载变量
source "${PROJECT_DIR}/pipeline_vars.env"
local log_file="${LOG_DIR}/performance_analysis_${TIMESTAMP}.log"
print_info "开始性能分析..."
print_info "使用文件:"
print_info " - 角色画像: $PROFILE_FILE"
print_info " - 问题分类: $CLASSIFICATION_FILE"
if python performance_analyzer.py \
--profile-file "$PROFILE_FILE" \
--classification-file "$CLASSIFICATION_FILE" \
2>&1 | tee $log_file; then
# 查找生成的文件
routing_guide_file=$(find analysis_results -name "routing_guide_*.json" -type f -printf '%T@ %p\n' | sort -n | tail -1 | cut -d' ' -f2-)
if [ -n "$routing_guide_file" ] && [ -f "$routing_guide_file" ]; then
print_success "性能分析完成,文件: $routing_guide_file"
echo "ROUTING_GUIDE_FILE=$routing_guide_file" >> "${PROJECT_DIR}/pipeline_vars.env"
else
print_error "路由指南文件未找到"
return 1
fi
else
print_error "性能分析执行失败"
return 1
fi
}
# 运行智能路由器评估
run_intelligent_routing() {
print_step "4" "执行智能路由器评估 (样本数: $EVAL_SAMPLES)"
# 加载变量
source "${PROJECT_DIR}/pipeline_vars.env"
local log_file="${LOG_DIR}/intelligent_routing_${TIMESTAMP}.log"
print_info "开始智能路由器评估..."
print_info "使用路由指南: $ROUTING_GUIDE_FILE"
if CUDA_VISIBLE_DEVICES=$GPU_ID python intelligent_router.py \
--model $MODEL_NAME \
--routing-guide "$ROUTING_GUIDE_FILE" \
--samples $EVAL_SAMPLES \
2>&1 | tee $log_file; then
# 查找生成的文件
eval_result_file=$(find intelligent_routing_results -name "routing_evaluation_*.json" -type f -printf '%T@ %p\n' | sort -n | tail -1 | cut -d' ' -f2-)
eval_report_file=$(find intelligent_routing_results -name "routing_evaluation_report_*.md" -type f -printf '%T@ %p\n' | sort -n | tail -1 | cut -d' ' -f2-)
if [ -n "$eval_result_file" ] && [ -f "$eval_result_file" ]; then
print_success "智能路由评估完成"
print_info "评估结果: $eval_result_file"
if [ -n "$eval_report_file" ] && [ -f "$eval_report_file" ]; then
print_info "评估报告: $eval_report_file"
fi
else
print_error "路由评估结果文件未找到"
return 1
fi
else
print_error "智能路由器评估执行失败"
return 1
fi
}
# 生成最终报告
generate_final_report() {
print_step "5" "生成最终报告"
# 加载变量
source "${PROJECT_DIR}/pipeline_vars.env"
local final_report="${PROJECT_DIR}/FINAL_REPORT_${TIMESTAMP}.md"
cat > $final_report << EOF
# 智能数学问题路由系统 - 执行报告
**执行时间**: $(date '+%Y年%m月%d日 %H:%M:%S')
**模型**: $MODEL_NAME
**GPU**: $GPU_ID
**画像样本数**: $SAMPLES
**评估样本数**: $EVAL_SAMPLES
## 📁 生成的文件
### 1. 角色性能画像
- **详细记录**: \`$PROFILE_FILE\`
- **描述**: 包含8个专家角色在$SAMPLES个GSM8K样本上的详细表现记录
### 2. 问题分类结果
- **分类结果**: \`$CLASSIFICATION_FILE\`
- **描述**: $SAMPLES个问题的智能分类结果,分为12个数学问题类别
### 3. 性能分析与路由指南
- **路由指南**: \`$ROUTING_GUIDE_FILE\`
- **描述**: 数据驱动的专家推荐指南,包含每个类别的最佳专家角色
### 4. 智能路由评估
- **评估结果**: \`$(find intelligent_routing_results -name "routing_evaluation_*.json" -type f -printf '%T@ %p\n' | sort -n | tail -1 | cut -d' ' -f2-')\`
- **评估报告**: \`$(find intelligent_routing_results -name "routing_evaluation_report_*.md" -type f -printf '%T@ %p\n' | sort -n | tail -1 | cut -d' ' -f2-')\`
- **描述**: 在$EVAL_SAMPLES个新问题上的智能路由系统性能评估
## 📊 使用方式
### 查看评估报告
\`\`\`bash
cat "$(find intelligent_routing_results -name "routing_evaluation_report_*.md" -type f -printf '%T@ %p\n' | sort -n | tail -1 | cut -d' ' -f2-')"
\`\`\`
### 使用智能路由器解决新问题
\`\`\`python
from intelligent_router import IntelligentRouter
router = IntelligentRouter(
model_name="$MODEL_NAME",
routing_guide_file="$ROUTING_GUIDE_FILE"
)
# 解决数学问题
question = "你的数学问题..."
selected_role, category, confidence, reasoning = router.route_question(question)
final_answer, extracted_answer = router.solve_with_role(question, selected_role)
print(f"选择的专家: {selected_role}")
print(f"最终答案: {extracted_answer}")
\`\`\`
## 📝 日志文件
- **主日志**: \`$MAIN_LOG\`
- **详细日志**: \`$LOG_DIR/\`目录下的各步骤日志文件
---
*报告生成时间: $(date)*
EOF
print_success "最终报告已生成: $final_report"
# 显示关键统计信息
if [ -f "$final_report" ]; then
print_info "🎉 系统构建完成! 查看完整报告:"
echo -e "${PURPLE}cat '$final_report'${NC}"
fi
}
# 清理函数
cleanup() {
print_info "清理临时文件..."
rm -f "${PROJECT_DIR}/pipeline_vars.env"
}
# 错误处理
error_handler() {
local exit_code=$?
print_error "脚本执行失败 (退出码: $exit_code)"
print_info "检查日志文件获取详细错误信息: $MAIN_LOG"
cleanup
exit $exit_code
}
# 主函数
main() {
# 设置错误处理
trap error_handler ERR
trap cleanup EXIT
# 开始执行
print_header "🎯 智能数学问题路由系统 - 完整构建流程"
echo -e "${BLUE}配置信息:${NC}"
echo -e " 🤖 模型: ${YELLOW}$MODEL_NAME${NC}"
echo -e " 🎯 GPU: ${YELLOW}$GPU_ID${NC}"
echo -e " 📊 画像样本: ${YELLOW}$SAMPLES${NC}"
echo -e " 🧪 评估样本: ${YELLOW}$EVAL_SAMPLES${NC}"
echo -e " 📁 项目目录: ${YELLOW}$PROJECT_DIR${NC}"
echo -e " 📋 主日志: ${YELLOW}$MAIN_LOG${NC}\n"
# 初始化环境变量文件
echo "# 流程变量文件 - $(date)" > "${PROJECT_DIR}/pipeline_vars.env"
# 执行各个步骤
check_prerequisites
run_role_profiling
run_question_classification
run_performance_analysis
run_intelligent_routing
generate_final_report
# 成功完成
print_header "🎉 智能路由系统构建成功完成!"
echo -e "\n${GREEN}🏆 恭喜! 您的智能数学问题路由系统已成功构建!${NC}\n"
echo -e "${CYAN}📋 下一步操作:${NC}"
echo -e " 1. 查看最终报告: ${YELLOW}cat FINAL_REPORT_${TIMESTAMP}.md${NC}"
echo -e " 2. 查看评估结果: ${YELLOW}cat intelligent_routing_results/routing_evaluation_report_*.md${NC}"
echo -e " 3. 使用路由器解决新问题: ${YELLOW}python intelligent_router.py --help${NC}"
print_success "脚本执行完成 - 总用时: $SECONDS 秒"
}
# 检查命令行参数
if [ "$1" == "--help" ] || [ "$1" == "-h" ]; then
echo "智能数学问题路由系统 - 完整运行脚本"
echo ""
echo "用法: $0 [选项]"
echo ""
echo "选项:"
echo " --help, -h 显示此帮助信息"
echo " --quick 快速测试模式 (50个样本)"
echo " --full 完整模式 (500个样本)"
echo ""
echo "默认配置:"
echo " 模型: $MODEL_NAME"
echo " GPU: $GPU_ID"
echo " 画像样本: $SAMPLES"
echo " 评估样本: $EVAL_SAMPLES"
echo ""
echo "示例:"
echo " $0 # 使用默认配置"
echo " $0 --quick # 快速测试"
echo " $0 --full # 完整评估"
exit 0
fi
# 处理特殊模式
if [ "$1" == "--quick" ]; then
SAMPLES=50
EVAL_SAMPLES=20
print_info "🚀 快速测试模式: 画像样本=$SAMPLES, 评估样本=$EVAL_SAMPLES"
elif [ "$1" == "--full" ]; then
SAMPLES=500
EVAL_SAMPLES=100
print_info "🔥 完整评估模式: 画像样本=$SAMPLES, 评估样本=$EVAL_SAMPLES"
fi
# 执行主函数
main