feat: 外露推理调优参数并支持 device: auto
把此前只能改代码的参数摊到 config.yaml,交付后按客户机器调整: use_tensorrt / precision / enable_cinn / mkldnn_cache_capacity text_det_input_shape / text_rec_input_shape device 新增 auto:启动时探测可用的 CUDA 设备,有卡用 GPU、没卡用 CPU, 同一份配置在不同机器之间搬运不用改。探测不到卡是预期路径,报 INFO; 显式写 gpu 却用不了是配置问题,报 WARNING——便于区分这两种情况。 设备名校验加了两层(配置层拦拼写错误 + 运行时最后一道防线)。起因是实测 踩到:冻结包里的代码比 config.yaml 旧时,字符串 auto 被原样传给 PaddleX, 抛出的 AssertionError 完全看不出是配置写错了。 text_recognition_batch_size 默认值 8 -> 32:实测 1.png 同分辨率 23.87s -> 19.03s,框数不变、字符数 2240 -> 2241(差异仅英文分词空格与个别低置信字符)。 顺带把参数装配收敛到 OCRService.from_settings():启动、下载脚本、压测脚本 原本各自手工罗列参数,压测脚本已经漏掉了新配置项。 Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
"""Settings 新参数的校验,以及 GPU 不可用时的兜底行为。"""
|
||||
|
||||
import pytest
|
||||
from pydantic import ValidationError
|
||||
|
||||
from ocr_server.config import Settings
|
||||
from ocr_server.ocr import OCRService
|
||||
|
||||
|
||||
def test_accepts_tuning_overrides() -> None:
|
||||
settings = Settings(
|
||||
use_tensorrt=True,
|
||||
precision="fp16",
|
||||
enable_cinn=False,
|
||||
mkldnn_cache_capacity=32,
|
||||
text_det_input_shape=[3, 960, 960],
|
||||
text_rec_input_shape=[3, 48, 320],
|
||||
)
|
||||
assert settings.use_tensorrt is True
|
||||
assert settings.precision == "fp16"
|
||||
assert settings.mkldnn_cache_capacity == 32
|
||||
assert settings.text_det_input_shape == [3, 960, 960]
|
||||
assert settings.text_rec_input_shape == [3, 48, 320]
|
||||
|
||||
|
||||
def test_optional_shapes_default_to_none() -> None:
|
||||
settings = Settings(text_det_input_shape=None, text_rec_input_shape=None)
|
||||
assert settings.text_det_input_shape is None
|
||||
assert settings.text_rec_input_shape is None
|
||||
|
||||
|
||||
def test_device_is_normalised() -> None:
|
||||
assert Settings(device=" GPU:0 ").device == "gpu:0"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("device", ["", "gpu:", "gpu-0", "0", "cuda device"])
|
||||
def test_rejects_malformed_device(device: str) -> None:
|
||||
with pytest.raises(ValidationError):
|
||||
Settings(device=device)
|
||||
|
||||
|
||||
def test_rejects_unknown_precision() -> None:
|
||||
with pytest.raises(ValidationError):
|
||||
Settings(precision="int8")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("shape", [[3, 48], [3, 48, 320, 1], [3, 0, 320], "48x320"])
|
||||
def test_rejects_invalid_input_shape(shape: object) -> None:
|
||||
with pytest.raises(ValidationError):
|
||||
Settings(text_rec_input_shape=shape)
|
||||
|
||||
|
||||
def test_gpu_falls_back_when_cuda_unavailable() -> None:
|
||||
"""配置了 GPU 但当前 PaddlePaddle 不支持 CUDA 时,应回落 CPU 而不是崩溃。"""
|
||||
paddle = pytest.importorskip("paddle")
|
||||
service = OCRService(device="gpu", use_tensorrt=True)
|
||||
service._resolve_device()
|
||||
if paddle.device.is_compiled_with_cuda():
|
||||
assert service._effective_device == "gpu"
|
||||
assert service._use_tensorrt is True
|
||||
else:
|
||||
assert service._effective_device == "cpu"
|
||||
# TensorRT 只在 GPU 上生效,回落 CPU 后必须被关掉。
|
||||
assert service._use_tensorrt is False
|
||||
|
||||
|
||||
def test_tensorrt_ignored_on_cpu() -> None:
|
||||
service = OCRService(device="cpu", use_tensorrt=True)
|
||||
service._resolve_device()
|
||||
assert service._effective_device == "cpu"
|
||||
assert service._use_tensorrt is False
|
||||
|
||||
|
||||
def test_auto_is_accepted_as_device() -> None:
|
||||
assert Settings(device="auto").device == "auto"
|
||||
|
||||
|
||||
def test_rejects_unrecognised_device_name() -> None:
|
||||
"""格式合法但取值不存在的设备名要在配置层就挡住。
|
||||
|
||||
否则它会一路传到 PaddleX 内部抛 AssertionError,日志里看不出是配置写错了。
|
||||
"""
|
||||
with pytest.raises(ValidationError):
|
||||
Settings(device="autoo")
|
||||
with pytest.raises(ValidationError):
|
||||
Settings(device="cuda")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("device", ["cpu", "gpu", "gpu:1", "npu", "xpu"])
|
||||
def test_accepts_supported_devices(device: str) -> None:
|
||||
assert Settings(device=device).device == device
|
||||
|
||||
|
||||
@pytest.mark.parametrize("device", ["auto", "autoo", "cuda"])
|
||||
def test_finalize_device_never_passes_unknown_value(device: str) -> None:
|
||||
"""最后一道防线:冻结包代码比 config.yaml 旧时(实测踩过)也不能把值透传给 PaddleX。"""
|
||||
resolved = OCRService._finalize_device(device)
|
||||
assert resolved.split(":", 1)[0] in {
|
||||
"cpu",
|
||||
"gpu",
|
||||
"xpu",
|
||||
"npu",
|
||||
"mlu",
|
||||
"gcu",
|
||||
"dcu",
|
||||
"iluvatar_gpu",
|
||||
"metax_gpu",
|
||||
}
|
||||
|
||||
|
||||
def test_finalize_device_keeps_valid_values() -> None:
|
||||
assert OCRService._finalize_device("gpu:1") == "gpu:1"
|
||||
assert OCRService._finalize_device("cpu") == "cpu"
|
||||
|
||||
|
||||
def has_usable_gpu() -> bool:
|
||||
paddle = pytest.importorskip("paddle")
|
||||
if not paddle.device.is_compiled_with_cuda():
|
||||
return False
|
||||
try:
|
||||
return int(paddle.device.cuda.device_count()) > 0
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def test_auto_selects_matching_device() -> None:
|
||||
"""device=auto 探测到卡就用 GPU,探测不到就用 CPU——两种环境都要能起来。"""
|
||||
service = OCRService(device="auto", use_tensorrt=True)
|
||||
service._resolve_device()
|
||||
if has_usable_gpu():
|
||||
assert service._effective_device == "gpu"
|
||||
assert service._use_tensorrt is True
|
||||
else:
|
||||
assert service._effective_device == "cpu"
|
||||
assert service._use_tensorrt is False
|
||||
Reference in New Issue
Block a user