Description
Bug description
I am running a large pdf through OnnxTR (~50MB and 150 pages). If I run the whole PDF at once then my RAM usage spikes (and sometimes crashes my server) and then the memory is never freed. This is happening for both onnxtr-gpu and cpu. When I use the gpu there is no VRAM leak, but the RAM usage is very high. When I try to run the file one image at a time instead, I still see a very slight memory leak but the RAM usage isn't as high as before (but it is much slower because my gpu is not utilised effectively if I only send one image at a time). I have seen the previous discussions on DocTR for the memory leak and tried those flags but the memory leak still persists.
Code snippet to reproduce the bug
os.environ["ONNXTR_MULTIPROCESSING_DISABLE"]="TRUE"
os.environ["ONEDNN_PRIMITIVE_CACHE_CAPACITY"]="1"
general_options = SessionOptions() # For configuartion options see: https://onnxruntime.ai/docs/api/python/api_summary.html#sessionoptions
general_options.enable_cpu_mem_arena = False
providers = [("CUDAExecutionProvider", {"device_id": 0, "cudnn_conv_algo_search": "DEFAULT"})] # For available providers see: https://onnxruntime.ai/docs/execution-providers/
engine_config = EngineConfig(
session_options=general_options,
providers=providers
)
model=ocr_predictor(
det_arch="db_resnet50",reco_arch="parseq",
det_bs=2, # detection batch size
reco_bs=512, # recognition batch size
assume_straight_pages=True, # set to False
if the pages are not straight (rotation, perspective, etc.) (default: True)
straighten_pages=True, # set to True
if the pages should be straightened before final processing (default: False)
export_as_straight_boxes=False, # set to True
if the boxes should be exported as if the pages were straight (default: False)
# Preprocessing related parameters
preserve_aspect_ratio=True, # set to False
if the aspect ratio should not be preserved (default: True)
symmetric_pad=True, # set to False
to disable symmetric padding (default: True)
# Additional parameters - meta information
detect_orientation=False, # set to True
if the orientation of the pages should be detected (default: False)
detect_language=False, # set to True
if the language of the pages should be detected (default: False)
# Orientation specific parameters in combination with assume_straight_pages=False
and/or straighten_pages=True
disable_crop_orientation=False, # set to True
if the crop orientation classification should be disabled (default: False)
disable_page_orientation=False, # set to True
if the general page orientation classification should be disabled (default: False)
# DocumentBuilder specific parameters
resolve_lines=True, # whether words should be automatically grouped into lines (default: True)
resolve_blocks=False, # whether lines should be automatically grouped into blocks (default: False)
paragraph_break=0.035, # relative length of the minimum space separating paragraphs (default: 0.035)
# OnnxTR specific parameters
# NOTE: 8-Bit quantized models are not available for FAST detection models and can in general lead to poorer accuracy
load_in_8_bit=False, # set to True
to load 8-bit quantized models instead of the full precision onces (default: False)
det_engine_cfg=engine_config,
reco_engine_cfg=engine_config,
clf_engine_cfg=engine_config
)
I have tried it with both, the default engine_config and this one. I also tried using "crnn_vgg16_bn" for recognition but that doesn't change much (RAM usage is lower but there is still a leak)
Error traceback
RAM usage spikes up and sometimes causes a crash
2025-05-15 14:46:14.303056+05:30 - Total: 33263783936, Available: 28160000, Used: 32885014528, Percentage: 99.9%
Environment
Ubuntu 24.04.2
onnx 1.17.0
onnxruntime-gpu 1.21.1
onnxtr 0.6.3
opencv-python-headless 4.11.0.86
Tested with both python 3.11 and 3.12
Edit: You can use the PDF to reproduce the issue https://media.britannia.co.in/BRITANNIA_Annual_Report_2023_24_18_07_2024_Final_01_High_1_8d1bc38e58.pdf