paddle_vllm_daemon.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. #!/bin/bash
  2. # filepath: /home/ubuntu/zhch/PaddleX/zhch/paddle_vllm_daemon.sh
  3. # PaddleOCR-VL vLLM 服务守护进程脚本
  4. LOGDIR="/home/ubuntu/zhch/logs"
  5. mkdir -p $LOGDIR
  6. PIDFILE="$LOGDIR/paddleocr_vl_vllm.pid"
  7. LOGFILE="$LOGDIR/paddleocr_vl_vllm.log"
  8. # 配置参数
  9. CONDA_ENV="paddle" # 根据你的环境调整
  10. PORT="8110"
  11. HOST="0.0.0.0"
  12. MODEL_NAME="PaddleOCR-VL-0.9B"
  13. BACKEND="vllm"
  14. # GPU 配置
  15. GPU_MEMORY_UTILIZATION="0.3"
  16. CUDA_VISIBLE_DEVICES="3" # 使用3号显卡
  17. MAX_MODEL_LEN="16384"
  18. MAX_NUM_BATCHED_TOKENS="8192"
  19. MAX_NUM_SEQS="8"
  20. # PaddleX 环境变量
  21. export PADDLE_PDX_MODEL_SOURCE="bos"
  22. export PYTHONWARNINGS="ignore::UserWarning"
  23. # 正确初始化和激活conda环境
  24. if [ -f "/home/ubuntu/anaconda3/etc/profile.d/conda.sh" ]; then
  25. source /home/ubuntu/anaconda3/etc/profile.d/conda.sh
  26. conda activate $CONDA_ENV
  27. elif [ -f "/opt/conda/etc/profile.d/conda.sh" ]; then
  28. source /opt/conda/etc/profile.d/conda.sh
  29. conda activate $CONDA_ENV
  30. else
  31. echo "Warning: Using direct conda path activation"
  32. export PATH="/home/ubuntu/anaconda3/envs/$CONDA_ENV/bin:$PATH"
  33. fi
  34. start() {
  35. if [ -f $PIDFILE ] && kill -0 $(cat $PIDFILE) 2>/dev/null; then
  36. echo "PaddleOCR-VL vLLM is already running"
  37. return 1
  38. fi
  39. echo "Starting PaddleOCR-VL vLLM daemon..."
  40. echo "Host: $HOST, Port: $PORT"
  41. echo "Model: $MODEL_NAME, Backend: $BACKEND"
  42. echo "GPU memory utilization: $GPU_MEMORY_UTILIZATION"
  43. echo "CUDA devices: $CUDA_VISIBLE_DEVICES"
  44. # 检查conda环境
  45. if ! command -v python >/dev/null 2>&1; then
  46. echo "❌ Python not found. Check conda environment activation."
  47. return 1
  48. fi
  49. # 检查paddlex_genai_server命令
  50. if ! command -v paddlex_genai_server >/dev/null 2>&1; then
  51. echo "❌ paddlex_genai_server not found. Please install vllm-server plugin:"
  52. echo " paddlex --install genai-vllm-server"
  53. return 1
  54. fi
  55. echo "🔧 Using Python: $(which python)"
  56. echo "🔧 Using paddlex_genai_server: $(which paddlex_genai_server)"
  57. # 显示GPU状态
  58. echo "📊 GPU 状态检查:"
  59. if command -v nvidia-smi >/dev/null 2>&1; then
  60. nvidia-smi --query-gpu=index,name,memory.used,memory.total --format=csv,noheader,nounits | \
  61. grep "^$CUDA_VISIBLE_DEVICES," | \
  62. awk -F',' '{printf " GPU %s: %s - 内存: %sMB/%sMB\n", $1, $2, $3, $4}'
  63. else
  64. echo "⚠️ nvidia-smi not available"
  65. fi
  66. # 启动PaddleOCR-VL vLLM服务
  67. CUDA_VISIBLE_DEVICES=$CUDA_VISIBLE_DEVICES nohup paddlex_genai_server \
  68. --model_name $MODEL_NAME \
  69. --backend $BACKEND \
  70. --host $HOST \
  71. --port $PORT \
  72. --backend_config <(cat <<EOF
  73. gpu-memory-utilization: $GPU_MEMORY_UTILIZATION
  74. EOF
  75. ) > $LOGFILE 2>&1 &
  76. echo $! > $PIDFILE
  77. echo "✅ PaddleOCR-VL vLLM started with PID: $(cat $PIDFILE)"
  78. echo "📋 Log file: $LOGFILE"
  79. echo "🌐 Service URL: http://$HOST:$PORT"
  80. echo "📖 API Documentation: http://localhost:$PORT/docs"
  81. echo ""
  82. echo "Waiting for service to start..."
  83. sleep 5
  84. status
  85. }
  86. stop() {
  87. if [ ! -f $PIDFILE ]; then
  88. echo "PaddleOCR-VL vLLM is not running"
  89. return 1
  90. fi
  91. PID=$(cat $PIDFILE)
  92. echo "Stopping PaddleOCR-VL vLLM (PID: $PID)..."
  93. # 优雅停止
  94. kill $PID
  95. # 等待进程结束
  96. for i in {1..10}; do
  97. if ! kill -0 $PID 2>/dev/null; then
  98. break
  99. fi
  100. echo "Waiting for process to stop... ($i/10)"
  101. sleep 1
  102. done
  103. # 如果进程仍在运行,强制结束
  104. if kill -0 $PID 2>/dev/null; then
  105. echo "Force killing process..."
  106. kill -9 $PID
  107. fi
  108. rm -f $PIDFILE
  109. echo "✅ PaddleOCR-VL vLLM stopped"
  110. }
  111. status() {
  112. if [ -f $PIDFILE ] && kill -0 $(cat $PIDFILE) 2>/dev/null; then
  113. PID=$(cat $PIDFILE)
  114. echo "✅ PaddleOCR-VL vLLM is running (PID: $PID)"
  115. echo "🌐 Service URL: http://$HOST:$PORT"
  116. echo "📋 Log file: $LOGFILE"
  117. # 检查端口是否被监听
  118. if command -v ss >/dev/null 2>&1; then
  119. if ss -tuln | grep -q ":$PORT "; then
  120. echo "🔗 Port $PORT is being listened"
  121. else
  122. echo "⚠️ Port $PORT is not being listened (service may be starting up)"
  123. fi
  124. elif command -v netstat >/dev/null 2>&1; then
  125. if netstat -tuln | grep -q ":$PORT "; then
  126. echo "🔗 Port $PORT is being listened"
  127. else
  128. echo "⚠️ Port $PORT is not being listened (service may be starting up)"
  129. fi
  130. fi
  131. # 检查API响应
  132. if command -v curl >/dev/null 2>&1; then
  133. if curl -s --connect-timeout 2 http://127.0.0.1:$PORT/v1/models > /dev/null 2>&1; then
  134. echo "🎯 API 响应正常"
  135. else
  136. echo "⚠️ API 无响应 (service may be starting up)"
  137. fi
  138. fi
  139. # 显示GPU使用情况
  140. if command -v nvidia-smi >/dev/null 2>&1; then
  141. echo "📊 GPU 使用情况:"
  142. nvidia-smi --query-gpu=index,utilization.gpu,utilization.memory,memory.used,memory.total --format=csv,noheader,nounits | \
  143. grep "^$CUDA_VISIBLE_DEVICES," | \
  144. awk -F',' '{printf " GPU %s: GPU利用率 %s%%, 内存利用率 %s%%, 显存 %sMB/%sMB\n", $1, $2, $3, $4, $5}'
  145. fi
  146. # 显示最新日志
  147. if [ -f $LOGFILE ]; then
  148. echo "📄 Latest logs (last 3 lines):"
  149. tail -3 $LOGFILE | sed 's/^/ /'
  150. fi
  151. else
  152. echo "❌ PaddleOCR-VL vLLM is not running"
  153. if [ -f $PIDFILE ]; then
  154. echo "Removing stale PID file..."
  155. rm -f $PIDFILE
  156. fi
  157. fi
  158. }
  159. logs() {
  160. if [ -f $LOGFILE ]; then
  161. echo "📄 PaddleOCR-VL vLLM logs:"
  162. echo "=================="
  163. tail -f $LOGFILE
  164. else
  165. echo "❌ Log file not found: $LOGFILE"
  166. fi
  167. }
  168. config() {
  169. echo "📋 Current configuration:"
  170. echo " Conda Environment: $CONDA_ENV"
  171. echo " Host: $HOST"
  172. echo " Port: $PORT"
  173. echo " Model Name: $MODEL_NAME"
  174. echo " Backend: $BACKEND"
  175. echo " GPU Memory Utilization: $GPU_MEMORY_UTILIZATION"
  176. echo " CUDA Visible Devices: $CUDA_VISIBLE_DEVICES"
  177. echo " Max Model Length: $MAX_MODEL_LEN"
  178. echo " Max Num Seqs: $MAX_NUM_SEQS"
  179. echo " PID File: $PIDFILE"
  180. echo " Log File: $LOGFILE"
  181. echo ""
  182. echo " Model Source: ${PADDLE_PDX_MODEL_SOURCE:-default}"
  183. # 显示环境信息
  184. echo ""
  185. echo "🔧 Environment:"
  186. echo " Python: $(which python 2>/dev/null || echo 'Not found')"
  187. echo " paddlex_genai_server: $(which paddlex_genai_server 2>/dev/null || echo 'Not found')"
  188. echo " Conda: $(which conda 2>/dev/null || echo 'Not found')"
  189. echo " CUDA: $(which nvcc 2>/dev/null || echo 'Not found')"
  190. # 显示GPU信息
  191. if command -v nvidia-smi >/dev/null 2>&1; then
  192. echo ""
  193. echo "🔥 GPU Information:"
  194. nvidia-smi --query-gpu=index,name,driver_version,memory.total --format=csv,noheader,nounits | \
  195. grep "^$CUDA_VISIBLE_DEVICES," | \
  196. awk -F',' '{printf " GPU %s: %s (Driver: %s, Memory: %sMB)\n", $1, $2, $3, $4}'
  197. fi
  198. }
  199. test_api() {
  200. echo "🧪 Testing PaddleOCR-VL vLLM API..."
  201. if [ ! -f $PIDFILE ] || ! kill -0 $(cat $PIDFILE) 2>/dev/null; then
  202. echo "❌ PaddleOCR-VL vLLM service is not running"
  203. return 1
  204. fi
  205. if ! command -v curl >/dev/null 2>&1; then
  206. echo "❌ curl command not found"
  207. return 1
  208. fi
  209. echo "📡 Testing /v1/models endpoint..."
  210. response=$(curl -s --connect-timeout 5 http://127.0.0.1:$PORT/v1/models)
  211. if [ $? -eq 0 ]; then
  212. echo "✅ Models endpoint accessible"
  213. echo "$response" | python -m json.tool 2>/dev/null || echo "$response"
  214. else
  215. echo "❌ Models endpoint not accessible"
  216. fi
  217. echo ""
  218. echo "📡 Testing health endpoint..."
  219. health_response=$(curl -s --connect-timeout 5 http://127.0.0.1:$PORT/health)
  220. if [ $? -eq 0 ]; then
  221. echo "✅ Health endpoint accessible"
  222. echo "$health_response"
  223. else
  224. echo "❌ Health endpoint not accessible"
  225. fi
  226. }
  227. test_client() {
  228. echo "🧪 Testing PaddleOCR-VL client with vLLM server..."
  229. if [ ! -f $PIDFILE ] || ! kill -0 $(cat $PIDFILE) 2>/dev/null; then
  230. echo "❌ PaddleOCR-VL vLLM service is not running. Start it first with: $0 start"
  231. return 1
  232. fi
  233. # 测试用例文件路径
  234. TEST_IMAGE="/home/ubuntu/zhch/data/至远彩色印刷工业有限公司/2023年度报告母公司.img/2023年度报告母公司_page_006.png"
  235. TEST_OUTPUT="/tmp/paddleocr_vl_vllm_test_output"
  236. PIPELINE_CONFIG="/home/ubuntu/zhch/PaddleX/zhch/my_config/PaddleOCR-VL-Client.yaml"
  237. if [ ! -f "$TEST_IMAGE" ]; then
  238. echo "⚠️ Test image not found: $TEST_IMAGE"
  239. echo "Please provide a test image or update the TEST_IMAGE path in the script"
  240. return 1
  241. fi
  242. if [ ! -f "$PIPELINE_CONFIG" ]; then
  243. echo "⚠️ Pipeline config not found: $PIPELINE_CONFIG"
  244. echo "Please update the PIPELINE_CONFIG path in the script"
  245. return 1
  246. fi
  247. echo "📄 Testing with image: $TEST_IMAGE"
  248. echo "⚙️ Using pipeline config: $PIPELINE_CONFIG"
  249. echo "📁 Output directory: $TEST_OUTPUT"
  250. echo ""
  251. # 方法1: 使用 paddlex 命令行 (推荐)
  252. echo "🔧 Using paddlex CLI..."
  253. mkdir -p "$TEST_OUTPUT"
  254. paddlex --pipeline "$PIPELINE_CONFIG" \
  255. --input "$TEST_IMAGE" \
  256. --save_path "$TEST_OUTPUT" \
  257. --use_doc_orientation_classify False \
  258. --use_doc_unwarping False
  259. if [ $? -eq 0 ]; then
  260. echo "✅ CLI test completed successfully"
  261. echo "📁 Results saved to: $TEST_OUTPUT"
  262. # 显示生成的文件
  263. if [ -d "$TEST_OUTPUT" ]; then
  264. echo ""
  265. echo "📂 Generated files:"
  266. ls -lh "$TEST_OUTPUT" | tail -n +2 | awk '{print " " $9 " (" $5 ")"}'
  267. fi
  268. else
  269. echo "❌ CLI test failed"
  270. return 1
  271. fi
  272. }
  273. # 显示使用帮助
  274. usage() {
  275. echo "PaddleOCR-VL vLLM Service Daemon"
  276. echo "================================="
  277. echo "Usage: $0 {start|stop|restart|status|logs|config|test|test-client}"
  278. echo ""
  279. echo "Commands:"
  280. echo " start - Start the PaddleOCR-VL vLLM service"
  281. echo " stop - Stop the PaddleOCR-VL vLLM service"
  282. echo " restart - Restart the PaddleOCR-VL vLLM service"
  283. echo " status - Show service status and resource usage"
  284. echo " logs - Show service logs (follow mode)"
  285. echo " config - Show current configuration"
  286. echo " test - Test API endpoints"
  287. echo " test-client - Test PaddleX client with vLLM server"
  288. echo ""
  289. echo "Configuration (edit script to modify):"
  290. echo " Host: $HOST"
  291. echo " Port: $PORT"
  292. echo " Model: $MODEL_NAME"
  293. echo " Backend: $BACKEND"
  294. echo " GPU Memory: $GPU_MEMORY_UTILIZATION"
  295. echo " CUDA Devices: $CUDA_VISIBLE_DEVICES"
  296. echo ""
  297. echo "Examples:"
  298. echo " ./paddle_vllm_daemon.sh start"
  299. echo " ./paddle_vllm_daemon.sh status"
  300. echo " ./paddle_vllm_daemon.sh logs"
  301. echo " ./paddle_vllm_daemon.sh test"
  302. echo " ./paddle_vllm_daemon.sh test-client"
  303. }
  304. case "$1" in
  305. start)
  306. start
  307. ;;
  308. stop)
  309. stop
  310. ;;
  311. restart)
  312. stop
  313. sleep 3
  314. start
  315. ;;
  316. status)
  317. status
  318. ;;
  319. logs)
  320. logs
  321. ;;
  322. config)
  323. config
  324. ;;
  325. test)
  326. test_api
  327. ;;
  328. test-client)
  329. test_client
  330. ;;
  331. *)
  332. usage
  333. exit 1
  334. ;;
  335. esac