33from collections import defaultdict
44from copy import deepcopy
55from functools import cache
6- from typing import Annotated , Any , ClassVar , get_type_hints
6+ from typing import Annotated , Any , ClassVar , Self , get_type_hints
77
88from docling .datamodel .backend_options import BackendOptions , BaseBackendOptions
99from docling .datamodel .base_models import (
@@ -159,7 +159,6 @@ def _table_structure_opts_type_adapter() -> TypeAdapter:
159159def _resolve_pipeline_options (
160160 pipeline_options : dict [str , Any ] | None ,
161161 pipeline_cls : type ,
162- device : Device ,
163162) -> PipelineOptions :
164163 option_cls = _find_init_arg_type (pipeline_cls , "pipeline_options" )
165164 if pipeline_options is None :
@@ -198,10 +197,6 @@ def _resolve_pipeline_options(
198197 )
199198 pipeline_options ["table_structure_options" ] = table_structure_opts
200199 pipeline_options = option_cls .model_validate (pipeline_options )
201- accelerator_opts = getattr (pipeline_options , "accelerator_options" , None )
202- if accelerator_opts is not None :
203- accelerator_opts .device = device .to_docling ()
204- pipeline_options .accelerator_options = accelerator_opts
205200 return pipeline_options
206201
207202
@@ -218,13 +213,11 @@ class DoclingFormatOption(BaseFormatOption):
218213 pipeline_cls : str
219214 pipeline_options : dict [str , Any ] | None = None
220215
221- def to_docling (self , device : Device ) -> BaseFormatOption : # noqa: ANN201
216+ def to_docling (self ) -> BaseFormatOption : # noqa: ANN201
222217 from docling .document_converter import FormatOption # noqa: PLC0415
223218
224219 pipeline_cls = _resolve_pipeline_cls (self .pipeline_cls )
225- pipeline_opts = _resolve_pipeline_options (
226- self .pipeline_options , pipeline_cls , device
227- )
220+ pipeline_opts = _resolve_pipeline_options (self .pipeline_options , pipeline_cls )
228221 pipeline_opts = _validate_pipeline_opts (pipeline_opts )
229222 return FormatOption (
230223 pipeline_cls = pipeline_cls ,
@@ -448,3 +441,23 @@ def supported_exts(cls) -> set[SupportedExt]:
448441 def with_setting (self , settings : DoclingSettings ) -> "DoclingPipelineConfig" :
449442 update = {"settings" : settings }
450443 return safe_copy (self , update = update )
444+
445+ def to_device (self ) -> Self :
446+ device = self .device .to_docling ()
447+ new_format_opts = dict ()
448+ for fmt , fmt_opts in self .format_options .items ():
449+ pipeline_opts = fmt_opts .pipeline_options
450+ if pipeline_opts is None :
451+ pipeline_opts = {"generate_picture_images" : True }
452+ accelerator_opts = pipeline_opts .get ("accelerator_options" , dict ())
453+ accelerator_opts ["device" ] = device
454+ pipeline_opts ["accelerator_options" ] = accelerator_opts
455+ update = {"pipeline_options" : pipeline_opts }
456+ new_format_opts [fmt ] = safe_copy (fmt_opts , update = update )
457+
458+ return DoclingPipelineConfig (
459+ device = self .device ,
460+ format_options = new_format_opts ,
461+ settings = self .settings ,
462+ result_buffer = self .result_buffer ,
463+ )
0 commit comments