Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

feat: integrate clean_vram function to optimize memory management during hybrid analysis

myhlolic65e9c55c1a3
diff --git a/mineru/backend/hybrid/hybrid_analyze.py b/mineru/backend/hybrid/hybrid_analyze.py
index 3d4a96fe..69d5b89c 100644
--- a/mineru/backend/hybrid/hybrid_analyze.py
+++ b/mineru/backend/hybrid/hybrid_analyze.py
@@ -26,7 +26,7 @@ from ...utils.backend_options import (
from ...utils.config_reader import get_device, get_processing_window_size, get_table_enable
from ...utils.enum_class import ImageType
from ...utils.image_payload import ImagePayloadCache
-from ...utils.model_utils import clean_memory, crop_img, get_vram
+from ...utils.model_utils import clean_memory, clean_vram, crop_img, get_vram
from ...utils.ocr_utils import (
OcrConfidence,
get_adjusted_mfdetrec_res,
@@ -890,6 +890,7 @@ def _extract_with_local_layout(
images_pil_list,
batch_size=min(8, batch_ratio * LAYOUT_BASE_BATCH_SIZE),
)
+ clean_vram(hybrid_pipeline_model.device, vram_threshold=8)
mfd_res = _run_medium_formula_recognition(
hybrid_pipeline_model,
images_layout_res,
@@ -897,6 +898,7 @@ def _extract_with_local_layout(
inline_formula_enable,
batch_ratio,
)
+ clean_vram(hybrid_pipeline_model.device, vram_threshold=8)
_apply_medium_table_recognition(
hybrid_pipeline_model,
images_layout_res,
diff --git a/mineru/model/mfr/utils.py b/mineru/model/mfr/utils.py
index a646ef91..67c0a0fa 100644
--- a/mineru/model/mfr/utils.py
+++ b/mineru/model/mfr/utils.py
@@ -362,6 +362,7 @@ def finalize_mfr_batch_groups(
total_count: int,
requested_batch_size: int,
) -> list[list[int]]:
+ """整理 MFR batch 分组,避免尾部大面积公式被重新合并成超大 batch。"""
if not batch_groups:
return []
@@ -380,10 +381,15 @@ def finalize_mfr_batch_groups(
return batch_groups
return [first_group, second_group]
- while (
- len(batch_groups) >= 3
- and len(batch_groups[-1]) < len(batch_groups[-2])
- ):
+ min_dynamic_batch_size = get_mfr_min_dynamic_batch_size(requested_batch_size)
+ while len(batch_groups) >= 3 and len(batch_groups[-1]) < len(batch_groups[-2]):
+ tail_merge_limit = min(
+ requested_batch_size,
+ max(len(batch_groups[-2]), min_dynamic_batch_size),
+ )
+ # 尾部通常聚集最大面积公式,合并后超过安全上限会造成最后 batch 显存峰值过高。
+ if len(batch_groups[-2]) + len(batch_groups[-1]) > tail_merge_limit:
+ break
tail_group = batch_groups.pop()
batch_groups[-1].extend(tail_group)