OCR 修复:无 cuDNN 用 CPU、禁用 ir_optim 避免 SIGILL。
This commit is contained in:
+30
-4
@@ -7,6 +7,28 @@ ocr_worker_dir() {
|
||||
echo "${INSTALL_DIR}/deploy/ocr-worker"
|
||||
}
|
||||
|
||||
detect_ocr_use_gpu() {
|
||||
if [[ "${OCR_USE_GPU:-auto}" == "false" ]]; then
|
||||
echo "false"
|
||||
return
|
||||
fi
|
||||
if [[ "${OCR_USE_GPU:-auto}" == "true" ]]; then
|
||||
if ldconfig -p 2>/dev/null | grep -q libcudnn; then
|
||||
echo "true"
|
||||
else
|
||||
log_warn "未检测到 cuDNN 库,GPU OCR 不可用,改用 CPU"
|
||||
echo "false"
|
||||
fi
|
||||
return
|
||||
fi
|
||||
# auto
|
||||
if command -v nvidia-smi >/dev/null && ldconfig -p 2>/dev/null | grep -q libcudnn; then
|
||||
echo "true"
|
||||
else
|
||||
echo "false"
|
||||
fi
|
||||
}
|
||||
|
||||
install_ocr_worker() {
|
||||
local worker_dir
|
||||
worker_dir="$(ocr_worker_dir)"
|
||||
@@ -14,9 +36,11 @@ install_ocr_worker() {
|
||||
log_error "未找到 ${worker_dir}"
|
||||
return 1
|
||||
fi
|
||||
log_info "安装/更新 OCR Worker (PaddleOCR GPU)…"
|
||||
log_info "安装/更新 OCR Worker (PaddleOCR)…"
|
||||
chmod +x "${worker_dir}"/*.sh 2>/dev/null || true
|
||||
OCR_PORT="${OCR_PORT}" bash "${worker_dir}/install.sh"
|
||||
local use_gpu
|
||||
use_gpu="$(detect_ocr_use_gpu)"
|
||||
OCR_PORT="${OCR_PORT}" OCR_USE_GPU="${use_gpu}" bash "${worker_dir}/install.sh"
|
||||
}
|
||||
|
||||
ocr_screen_running() {
|
||||
@@ -35,7 +59,9 @@ start_ocr_screen() {
|
||||
return 1
|
||||
fi
|
||||
|
||||
log_info "启动 OCR Worker → screen 会话「${OCR_SCREEN_NAME}」(GPU 常驻, 端口 ${OCR_PORT})"
|
||||
local use_gpu log_file
|
||||
use_gpu="$(detect_ocr_use_gpu)"
|
||||
log_info "启动 OCR Worker → screen「${OCR_SCREEN_NAME}」(GPU=${use_gpu}, 端口 ${OCR_PORT})"
|
||||
if ocr_screen_running; then
|
||||
screen -S "${OCR_SCREEN_NAME}" -X quit 2>/dev/null || true
|
||||
sleep 1
|
||||
@@ -46,7 +72,7 @@ start_ocr_screen() {
|
||||
|
||||
screen -dmS "${OCR_SCREEN_NAME}" bash -c "
|
||||
cd '${worker_dir}' &&
|
||||
export OCR_USE_GPU=true OCR_PORT='${OCR_PORT}' OCR_HOST=0.0.0.0 &&
|
||||
export OCR_USE_GPU='${use_gpu}' OCR_PORT='${OCR_PORT}' OCR_HOST=0.0.0.0 &&
|
||||
exec bash run.sh >> '${log_file}' 2>&1
|
||||
"
|
||||
sleep 2
|
||||
|
||||
@@ -10,6 +10,7 @@ from fastapi import FastAPI, File, Header, HTTPException, UploadFile
|
||||
from PIL import Image
|
||||
|
||||
os.environ.setdefault("OPENCV_IO_ENABLE_OPENEXR", "0")
|
||||
os.environ.setdefault("FLAGS_use_mkldnn", "0")
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
|
||||
logger = logging.getLogger("ocr-worker")
|
||||
@@ -36,7 +37,8 @@ def _create_engine(use_gpu: bool):
|
||||
lang="ch",
|
||||
show_log=False,
|
||||
use_gpu=use_gpu,
|
||||
enable_mkldnn=not use_gpu,
|
||||
enable_mkldnn=False,
|
||||
ir_optim=False,
|
||||
det_limit_side_len=min(OCR_MAX_SIDE, 1280),
|
||||
rec_batch_num=8,
|
||||
)
|
||||
@@ -150,19 +152,7 @@ def _run_ocr_impl(content: bytes) -> dict:
|
||||
|
||||
|
||||
def run_ocr_on_bytes(content: bytes) -> dict:
|
||||
try:
|
||||
return _run_ocr_impl(content)
|
||||
except Exception as exc:
|
||||
err = str(exc).lower()
|
||||
gpu_fail = _engine_mode == "gpu" and any(
|
||||
x in err for x in ("cuda", "cudnn", "gpu", "out of memory", "resource exhausted", "precondition")
|
||||
)
|
||||
if gpu_fail and OCR_USE_GPU:
|
||||
logger.warning("GPU OCR runtime failed, retry CPU: %s", exc)
|
||||
_reset_engine()
|
||||
get_engine(force_cpu=True)
|
||||
return _run_ocr_impl(content)
|
||||
raise
|
||||
return _run_ocr_impl(content)
|
||||
|
||||
|
||||
@app.get("/health")
|
||||
|
||||
@@ -25,23 +25,23 @@ source "${VENV}/bin/activate"
|
||||
pip install -U pip wheel -i "${PIP_MIRROR}"
|
||||
|
||||
install_paddle() {
|
||||
if command -v nvidia-smi >/dev/null 2>&1; then
|
||||
local use_gpu="${OCR_USE_GPU:-false}"
|
||||
if [[ "${use_gpu}" == "true" ]] && command -v nvidia-smi >/dev/null 2>&1 && ldconfig -p 2>/dev/null | grep -q libcudnn; then
|
||||
local cuda_major
|
||||
cuda_major="$(nvidia-smi 2>/dev/null | sed -n 's/.*CUDA Version: \([0-9]*\)\.[0-9]*/\1/p' | head -1)"
|
||||
cuda_major="${cuda_major:-11}"
|
||||
echo "==> 检测到 NVIDIA GPU,CUDA 主版本: ${cuda_major}"
|
||||
echo "==> 安装 paddlepaddle-gpu (CUDA ${cuda_major}.x)…"
|
||||
if [[ "${cuda_major}" -ge 12 ]]; then
|
||||
echo "==> 安装 paddlepaddle-gpu (CUDA 12.x)…"
|
||||
pip install paddlepaddle-gpu==2.6.2 -i https://www.paddlepaddle.org.cn/packages/stable/cu123/ \
|
||||
|| pip install paddlepaddle-gpu==2.6.2 -i https://www.paddlepaddle.org.cn/packages/stable/cu118/
|
||||
else
|
||||
echo "==> 安装 paddlepaddle-gpu (CUDA 11.x)…"
|
||||
pip install paddlepaddle-gpu==2.6.2 -i https://www.paddlepaddle.org.cn/packages/stable/cu118/
|
||||
fi
|
||||
else
|
||||
echo "==> 未检测到 GPU,安装 CPU 版 paddlepaddle…"
|
||||
echo "==> 安装 paddlepaddle CPU(无 cuDNN 或 OCR_USE_GPU=false)…"
|
||||
pip install paddlepaddle==2.6.2 -i "${PIP_MIRROR}"
|
||||
fi
|
||||
pip install 'protobuf>=3.20,<4' -q
|
||||
}
|
||||
|
||||
install_paddle
|
||||
|
||||
Reference in New Issue
Block a user