Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.
Python ai4sciencedocument-analysisdocxextract-datalayout-analysisocrparserpdfpdf-converterpdf-extractor-llmpdf-extractor-pretrainpdf-extractor-ragpdf-parserpptxpythonxlsx
fix: simplify VRAM size retrieval and improve error handling in memory management
myhlolibcb30fe79cef
diff --git a/mineru/backend/pipeline/pipeline_analyze.py b/mineru/backend/pipeline/pipeline_analyze.pyindex e57da57b..d56dedab 100644--- a/mineru/backend/pipeline/pipeline_analyze.py+++ b/mineru/backend/pipeline/pipeline_analyze.py@@ -173,25 +173,19 @@ def batch_image_analyze("Please ensure that the torch_npu package is installed correctly.") from e- if str(device).startswith('npu') or str(device).startswith('cuda'):- vram = get_vram(device)- if vram is not None:- gpu_memory = int(os.getenv('MINERU_VIRTUAL_VRAM_SIZE', round(vram)))- if gpu_memory >= 16:- batch_ratio = 16- elif gpu_memory >= 12:- batch_ratio = 8- elif gpu_memory >= 8:- batch_ratio = 4- elif gpu_memory >= 6:- batch_ratio = 2- else:- batch_ratio = 1- logger.info(f'gpu_memory: {gpu_memory} GB, batch_ratio: {batch_ratio}')+ gpu_memory = get_vram(device)+ if gpu_memory is not None:+ if gpu_memory >= 16:+ batch_ratio = 16+ elif gpu_memory >= 12:+ batch_ratio = 8+ elif gpu_memory >= 8:+ batch_ratio = 4+ elif gpu_memory >= 6:+ batch_ratio = 2else:- # Default batch_ratio when VRAM can't be determinedbatch_ratio = 1- logger.info(f'Could not determine GPU memory, using default batch_ratio: {batch_ratio}')+ logger.info(f'gpu_memory: {gpu_memory} GB, batch_ratio: {batch_ratio}')# 检测torch的版本号import torchdiff --git a/mineru/backend/vlm/utils.py b/mineru/backend/vlm/utils.pyindex 4a56eb7f..5defc438 100644--- a/mineru/backend/vlm/utils.py+++ b/mineru/backend/vlm/utils.py@@ -81,20 +81,16 @@ def set_default_gpu_memory_utilization() -> float:def set_default_batch_size() -> int:try:device = get_device()- vram = get_vram(device)- if vram is not None:- gpu_memory = int(os.getenv('MINERU_VIRTUAL_VRAM_SIZE', round(vram)))- if gpu_memory >= 16:- batch_size = 8- elif gpu_memory >= 8:- batch_size = 4- else:- batch_size = 1- logger.info(f'gpu_memory: {gpu_memory} GB, batch_size: {batch_size}')+ gpu_memory = get_vram(device)++ if gpu_memory >= 16:+ batch_size = 8+ elif gpu_memory >= 8:+ batch_size = 4else:- # Default batch_ratio when VRAM can't be determinedbatch_size = 1- logger.info(f'Could not determine GPU memory, using default batch_ratio: {batch_size}')+ logger.info(f'gpu_memory: {gpu_memory} GB, batch_size: {batch_size}')+except Exception as e:logger.warning(f'Error determining VRAM: {e}, using default batch_ratio: 1')batch_size = 1diff --git a/mineru/cli/client.py b/mineru/cli/client.pyindex bc12c771..c1fa3c04 100644--- a/mineru/cli/client.py+++ b/mineru/cli/client.py@@ -172,9 +172,8 @@ def main(def get_virtual_vram_size() -> int:if virtual_vram is not None:return virtual_vram- if get_device_mode().startswith("cuda") or get_device_mode().startswith("npu"):- return round(get_vram(get_device_mode()))- return 1+ else:+ return get_vram(get_device_mode())if os.getenv('MINERU_VIRTUAL_VRAM_SIZE', None) is None:os.environ['MINERU_VIRTUAL_VRAM_SIZE']= str(get_virtual_vram_size())diff --git a/mineru/utils/model_utils.py b/mineru/utils/model_utils.pyindex 7285f3c2..5ce4be12 100644--- a/mineru/utils/model_utils.py+++ b/mineru/utils/model_utils.py@@ -428,8 +428,6 @@ def clean_memory(device='cuda'):def clean_vram(device, vram_threshold=8):total_memory = get_vram(device)- if total_memory is not None:- total_memory = int(os.getenv('MINERU_VIRTUAL_VRAM_SIZE', round(total_memory)))if total_memory and total_memory <= vram_threshold:gc_start = time.time()clean_memory(device)@@ -437,13 +435,28 @@ def clean_vram(device, vram_threshold=8):# logger.info(f"gc time: {gc_time}")-def get_vram(device):+def get_vram(device) -> int:+ env_vram = os.getenv("MINERU_VIRTUAL_VRAM_SIZE")++ # 如果环境变量已配置,尝试解析并返回+ if env_vram is not None:+ try:+ total_memory = int(env_vram)+ if total_memory > 0:+ return total_memory+ else:+ logger.warning(+ f"MINERU_VIRTUAL_VRAM_SIZE value '{env_vram}' is not positive, falling back to auto-detection")+ except ValueError:+ logger.warning(+ f"MINERU_VIRTUAL_VRAM_SIZE value '{env_vram}' is not a valid integer, falling back to auto-detection")++ # 环境变量未配置或配置错误,根据device自动获取+ total_memory = 1if torch.cuda.is_available() and str(device).startswith("cuda"):- total_memory = torch.cuda.get_device_properties(device).total_memory / (1024 ** 3) # 将字节转换为 GB- return total_memory+ total_memory = round(torch.cuda.get_device_properties(device).total_memory / (1024 ** 3)) # 将字节转换为 GBelif str(device).startswith("npu"):if torch_npu.npu.is_available():- total_memory = torch_npu.npu.get_device_properties(device).total_memory / (1024 ** 3) # 转为 GB- return total_memory- else:- return None\ No newline at end of file+ total_memory = round(torch_npu.npu.get_device_properties(device).total_memory / (1024 ** 3)) # 转为 GB++ return total_memorydiff --git a/projects/mineru_tianshu/litserve_worker.py b/projects/mineru_tianshu/litserve_worker.pyindex 6283a208..318a15a9 100644--- a/projects/mineru_tianshu/litserve_worker.py+++ b/projects/mineru_tianshu/litserve_worker.py@@ -101,7 +101,7 @@ class MinerUWorkerAPI(ls.LitAPI):if os.getenv('MINERU_VIRTUAL_VRAM_SIZE', None) is None:if device_mode.startswith("cuda") or device_mode.startswith("npu"):try:- vram = round(get_vram(device_mode))+ vram = get_vram(device_mode)os.environ['MINERU_VIRTUAL_VRAM_SIZE'] = str(vram)except:os.environ['MINERU_VIRTUAL_VRAM_SIZE'] = '8' # 默认值diff --git a/projects/multi_gpu_v2/server.py b/projects/multi_gpu_v2/server.pyindex 97e561d1..4b9d2cf8 100644--- a/projects/multi_gpu_v2/server.py+++ b/projects/multi_gpu_v2/server.py@@ -26,7 +26,7 @@ class MinerUAPI(ls.LitAPI):device_mode = os.environ['MINERU_DEVICE_MODE']if os.getenv('MINERU_VIRTUAL_VRAM_SIZE', None) == None:if device_mode.startswith("cuda") or device_mode.startswith("npu"):- vram = round(get_vram(device_mode))+ vram = get_vram(device_mode)os.environ['MINERU_VIRTUAL_VRAM_SIZE'] = str(vram)else:os.environ['MINERU_VIRTUAL_VRAM_SIZE'] = '1'