Skip to content

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

perf(magic_pdf): optimize batch processing for GPU

myhloli55447c8b6444
diff --git a/magic_pdf/model/doc_analyze_by_custom_model.py b/magic_pdf/model/doc_analyze_by_custom_model.py
index c81bcec0..0f9592b9 100644
--- a/magic_pdf/model/doc_analyze_by_custom_model.py
+++ b/magic_pdf/model/doc_analyze_by_custom_model.py
@@ -177,7 +177,18 @@ def doc_analyze(
if torch.cuda.is_available() and device != 'cpu' or npu_support:
gpu_memory = int(os.getenv("VIRTUAL_VRAM_SIZE", round(get_vram(device))))
if gpu_memory is not None and gpu_memory >= 8:
- batch_ratio = int(gpu_memory-6)
+
+ if 8 <= gpu_memory <= 10:
+ batch_ratio = 2
+ elif 10 < gpu_memory <= 12:
+ batch_ratio = 4
+ elif 12 < gpu_memory <= 16:
+ batch_ratio = 8
+ elif 16 < gpu_memory <= 24:
+ batch_ratio = 16
+ else:
+ batch_ratio = 32
+
if batch_ratio >= 1:
logger.info(f'gpu_memory: {gpu_memory} GB, batch_ratio: {batch_ratio}')
batch_model = BatchAnalyze(model=custom_model, batch_ratio=batch_ratio)