AcceleratorOptions(num_threads=N) on PdfPipelineOptions never reaches the PDF parser
Bug
docling --num-threads 1 limits the PDF parser to one thread. The Python API equivalent,
AcceleratorOptions(num_threads=1) on PdfPipelineOptions, does not: the parser still starts
4 threads. So capping docling on a small host works from the CLI and silently does not work
from the API.
The model stages all honour the setting. Only the parser misses it.
ThreadedDoclingParseDocumentBackend falls back to AcceleratorOptions().num_threads
(docling/backend/docling_parse_backend.py:266-269) when parser_threads is unset, and that
is a fresh instance, so it cannot see the one on the caller's pipeline options. Nothing carries
it across: PdfFormatOption leaves backend_options at None, while NativePdfFormatOption
has a model_validator that does exactly this plumbing for the native pipeline.
The env var route still works, which is what makes it look inconsistent rather than broken:
DOCLING_NUM_THREADS=1 arrives because the fresh AcceleratorOptions() reads it from the
environment.
Steps to reproduce
import docling.backend.docling_parse_backend as dpb
from docling.datamodel.accelerator_options import AcceleratorOptions
from docling.datamodel.base_models import InputFormat
from docling.datamodel.pipeline_options import PdfPipelineOptions
from docling.document_converter import DocumentConverter, PdfFormatOption
seen = []
def record(**kw): # stop at the parser config, so no model weights are needed
seen.append(kw["threads"])
raise SystemExit(f"asked for 1, parser got {seen}")
dpb.ThreadedPdfParserConfig = record
opts = PdfPipelineOptions(accelerator_options=AcceleratorOptions(num_threads=1))
DocumentConverter(
format_options={InputFormat.PDF: PdfFormatOption(pipeline_options=opts)}
).convert("tests/data/pdf/bookmark_sample.pdf")
Expected asked for 1, parser got [1], prints asked for 1, parser got [4]. Asking for 16
also gives [4].
With DOCLING_NUM_THREADS=1 in the environment instead, the same script prints [1].
Docling version
2.128.0 (main at cea4b450)
Python version
3.12
I have a fix that gives PdfFormatOption the same validator NativePdfFormatOption already
has, and will open it alongside this.
One thing worth a maintainer call: AcceleratorOptions.num_threads is documented as
"Number of CPU threads to use for model inference", so pointing it at the parser is a
judgement. The argument for it is your own CLI, which already feeds one --num-threads into
both the accelerator options and parser_threads.
Source: docling-project/docling