refactor: change model cache name

This commit is contained in:
SWHL committed 2026-10-05 22:20:06 +08:00
1 parent 03f3e5dd02
commit cbc8f0d102
5 files changed
+105 -24

No files matched your search

+32 -1
View File
@@ -2,8 +2,8 @@
# @Author: SWHL
# @Contact: liekkaskono@163.com
import pytest
from rapidocr import ModelType, OCRVersion
from rapidocr.inference_engine.base import InferSession
from rapidocr.utils.model_resolver import (
MODEL_ROUTES,
list_supported_langs,
@@ -17,6 +17,37 @@ def test_model_routes_schema_is_valid():
validate_model_routes(MODEL_ROUTES)
def test_every_route_resolves_and_exists_in_a_model_registry():
registries = InferSession.model_info
for version, version_cfg in MODEL_ROUTES.items():
if version == "_defs":
continue
for task, task_cfg in version_cfg.items():
if task == "aliases":
continue
for model_type, routes in task_cfg.items():
if model_type == "aliases":
continue
task_enum = TaskType(task)
version_enum = OCRVersion(version)
model_enum = ModelType(model_type)
for route in routes:
model_key = route["model_key"]
assert any(
model_key in (registry.get(version, {}).get(task, {}) or {})
for engine, registry in registries.items()
if engine != "model_routes"
), model_key
for lang in route["supported_langs"]:
assert (
route_to_model_key(
task_enum, version_enum, lang, model_enum
)
== model_key
)
@pytest.mark.parametrize(
"task_type,expected_key",
[
+21 -4
View File
@@ -166,10 +166,27 @@ def test_tensorrt_default_cache_dir_uses_model_root_dir(monkeypatch, tmp_path):
assert engine_paths == [
cfg.model_root_dir
/ "models"
/ "en_PP-OCRv4_det_mobile_sm87_fp16_tf32unset.engine"
/ "en_PP-OCRv3_det_mobile_sm87_fp16_tf32unset.engine"
]
def test_tensorrt_model_aliases_share_cache_name(monkeypatch, tmp_path):
tensorrt_main = import_tensorrt_main(monkeypatch)
session = object.__new__(tensorrt_main.TRTInferSession)
base_cfg = {
"model_path": None,
"ocr_version": OCRVersion.PPOCRV6,
"task_type": TaskType.DET,
"model_type": ModelType.SMALL,
}
canonical_cfg = AttrDict(base_cfg, lang_type="ch")
alias_cfg = AttrDict(base_cfg, lang_type="zh")
assert session._get_model_name(canonical_cfg) == "multi_PP-OCRv6_det_small"
assert session._get_model_name(alias_cfg) == session._get_model_name(canonical_cfg)
def test_tensorrt_explicit_model_path_skips_download(monkeypatch, tmp_path):
tensorrt_main = import_tensorrt_main(monkeypatch)
@@ -209,7 +226,7 @@ def test_tensorrt_loads_cached_engine_without_rebuild(monkeypatch, tmp_path):
cfg = make_tensorrt_cfg(tmp_path, engine_cfg={"cache_dir": tmp_path / "trt_cache"})
engine_path = (
tmp_path / "trt_cache" / "en_PP-OCRv4_det_mobile_sm87_fp16_tf32unset.engine"
tmp_path / "trt_cache" / "en_PP-OCRv3_det_mobile_sm87_fp16_tf32unset.engine"
)
engine_path.parent.mkdir()
engine_path.write_bytes(b"cached engine")
@@ -239,7 +256,7 @@ def test_tensorrt_force_rebuild_ignores_cached_engine(monkeypatch, tmp_path):
engine_cfg={"cache_dir": tmp_path / "trt_cache", "force_rebuild": True},
)
engine_path = (
tmp_path / "trt_cache" / "en_PP-OCRv4_det_mobile_sm87_fp16_tf32unset.engine"
tmp_path / "trt_cache" / "en_PP-OCRv3_det_mobile_sm87_fp16_tf32unset.engine"
)
engine_path.parent.mkdir()
engine_path.write_bytes(b"cached engine")
@@ -278,7 +295,7 @@ def test_tensorrt_falls_back_to_rebuild_when_cached_engine_load_fails(
cfg = make_tensorrt_cfg(tmp_path, engine_cfg={"cache_dir": tmp_path / "trt_cache"})
engine_path = (
tmp_path / "trt_cache" / "en_PP-OCRv4_det_mobile_sm87_fp16_tf32unset.engine"
tmp_path / "trt_cache" / "en_PP-OCRv3_det_mobile_sm87_fp16_tf32unset.engine"
)
engine_path.parent.mkdir()
engine_path.write_bytes(b"bad cached engine")