Skip to content

Commit 2b83fdd

Browse files
authored
feat: Add XPU device support for Intel GPUs (#2809)
* feat: Add XPU device support * feat: Add XPU as supported device on some models * docs: Add XPU usage to examples * DCO Remediation Commit for jspast <[email protected]> I, jspast <[email protected]>, hereby add my Signed-off-by to this commit: f26e8b8 I, jspast <[email protected]>, hereby add my Signed-off-by to this commit: a4a2bf9 I, jspast <[email protected]>, hereby add my Signed-off-by to this commit: a2d5dac Signed-off-by: jspast <[email protected]> --------- Signed-off-by: jspast <[email protected]>
1 parent 3ef4525 commit 2b83fdd

File tree

11 files changed

+75
-20
lines changed

11 files changed

+75
-20
lines changed

docling/datamodel/accelerator_options.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class AcceleratorDevice(str, Enum):
1717
CPU = "cpu"
1818
CUDA = "cuda"
1919
MPS = "mps"
20+
XPU = "xpu"
2021

2122

2223
class AcceleratorOptions(BaseSettings):
@@ -30,13 +31,13 @@ class AcceleratorOptions(BaseSettings):
3031

3132
@field_validator("device")
3233
def validate_device(cls, value):
33-
# "auto", "cpu", "cuda", "mps", or "cuda:N"
34+
# "auto", "cpu", "cuda", "mps", "xpu", or "cuda:N"
3435
if value in {d.value for d in AcceleratorDevice} or re.match(
3536
r"^cuda(:\d+)?$", value
3637
):
3738
return value
3839
raise ValueError(
39-
"Invalid device option. Use 'auto', 'cpu', 'mps', 'cuda', or 'cuda:N'."
40+
"Invalid device option. Use 'auto', 'cpu', 'mps', 'xpu', 'cuda', or 'cuda:N'."
4041
)
4142

4243
@model_validator(mode="before")

docling/datamodel/layout_model_specs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class LayoutModelConfig(BaseModel):
1919
AcceleratorDevice.CPU,
2020
AcceleratorDevice.CUDA,
2121
AcceleratorDevice.MPS,
22+
AcceleratorDevice.XPU,
2223
]
2324

2425
@property

docling/datamodel/pipeline_options_asr_model.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class InlineAsrOptions(BaseAsrOptions):
3939
AcceleratorDevice.CPU,
4040
AcceleratorDevice.CUDA,
4141
AcceleratorDevice.MPS,
42+
AcceleratorDevice.XPU,
4243
]
4344

4445
@property

docling/datamodel/pipeline_options_vlm_model.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class InlineVlmOptions(BaseVlmOptions):
9393
AcceleratorDevice.CPU,
9494
AcceleratorDevice.CUDA,
9595
AcceleratorDevice.MPS,
96+
AcceleratorDevice.XPU,
9697
]
9798

9899
stop_strings: List[str] = []

docling/datamodel/vlm_model_specs.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
supported_devices=[
2929
AcceleratorDevice.CPU,
3030
AcceleratorDevice.CUDA,
31+
AcceleratorDevice.XPU,
3132
],
3233
extra_generation_config=dict(skip_special_tokens=False),
3334
scale=2.0,
@@ -92,6 +93,7 @@
9293
supported_devices=[
9394
AcceleratorDevice.CPU,
9495
AcceleratorDevice.CUDA,
96+
AcceleratorDevice.XPU,
9597
],
9698
torch_dtype="bfloat16",
9799
scale=2.0,
@@ -106,6 +108,7 @@
106108
inference_framework=InferenceFramework.VLLM,
107109
supported_devices=[
108110
AcceleratorDevice.CUDA,
111+
AcceleratorDevice.XPU,
109112
],
110113
scale=2.0,
111114
temperature=0.0,
@@ -123,6 +126,7 @@
123126
AcceleratorDevice.CPU,
124127
AcceleratorDevice.CUDA,
125128
# AcceleratorDevice.MPS,
129+
AcceleratorDevice.XPU,
126130
],
127131
torch_dtype="bfloat16",
128132
scale=2.0,
@@ -150,6 +154,7 @@
150154
inference_framework=InferenceFramework.VLLM,
151155
supported_devices=[
152156
AcceleratorDevice.CUDA,
157+
AcceleratorDevice.XPU,
153158
],
154159
scale=2.0,
155160
temperature=0.0,
@@ -167,6 +172,7 @@
167172
AcceleratorDevice.CPU,
168173
AcceleratorDevice.CUDA,
169174
AcceleratorDevice.MPS,
175+
AcceleratorDevice.XPU,
170176
],
171177
scale=2.0,
172178
temperature=0.0,
@@ -179,6 +185,7 @@
179185
inference_framework=InferenceFramework.VLLM,
180186
supported_devices=[
181187
AcceleratorDevice.CUDA,
188+
AcceleratorDevice.XPU,
182189
],
183190
scale=2.0,
184191
temperature=0.0,
@@ -201,7 +208,11 @@
201208
response_format=ResponseFormat.MARKDOWN,
202209
inference_framework=InferenceFramework.TRANSFORMERS,
203210
transformers_model_type=TransformersModelType.AUTOMODEL_VISION2SEQ,
204-
supported_devices=[AcceleratorDevice.CPU, AcceleratorDevice.CUDA],
211+
supported_devices=[
212+
AcceleratorDevice.CPU,
213+
AcceleratorDevice.CUDA,
214+
AcceleratorDevice.XPU,
215+
],
205216
scale=2.0,
206217
temperature=0.0,
207218
)
@@ -224,7 +235,11 @@
224235
response_format=ResponseFormat.MARKDOWN,
225236
inference_framework=InferenceFramework.TRANSFORMERS,
226237
transformers_model_type=TransformersModelType.AUTOMODEL_CAUSALLM,
227-
supported_devices=[AcceleratorDevice.CPU, AcceleratorDevice.CUDA],
238+
supported_devices=[
239+
AcceleratorDevice.CPU,
240+
AcceleratorDevice.CUDA,
241+
AcceleratorDevice.XPU,
242+
],
228243
scale=2.0,
229244
temperature=0.0,
230245
extra_generation_config=dict(num_logits_to_keep=0),
@@ -253,6 +268,7 @@
253268
AcceleratorDevice.CPU,
254269
AcceleratorDevice.CUDA,
255270
# AcceleratorDevice.MPS,
271+
AcceleratorDevice.XPU,
256272
],
257273
scale=2.0,
258274
temperature=0.0,
@@ -295,6 +311,7 @@
295311
AcceleratorDevice.CUDA,
296312
AcceleratorDevice.CPU,
297313
AcceleratorDevice.MPS,
314+
AcceleratorDevice.XPU,
298315
],
299316
scale=2.0,
300317
temperature=0.0,
@@ -313,6 +330,7 @@
313330
AcceleratorDevice.CPU,
314331
AcceleratorDevice.CUDA,
315332
AcceleratorDevice.MPS,
333+
AcceleratorDevice.XPU,
316334
],
317335
scale=2.0,
318336
temperature=0.0,

docling/models/code_formula_model.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ def __init__(
9696
if self.enabled:
9797
self.device = decide_device(
9898
accelerator_options.device,
99-
supported_devices=[AcceleratorDevice.CPU, AcceleratorDevice.CUDA],
99+
supported_devices=[
100+
AcceleratorDevice.CPU,
101+
AcceleratorDevice.CUDA,
102+
AcceleratorDevice.XPU,
103+
],
100104
)
101105

102106
if artifacts_path is None:

docling/utils/accelerator_utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def decide_device(
2222

2323
has_cuda = torch.backends.cuda.is_built() and torch.cuda.is_available()
2424
has_mps = torch.backends.mps.is_built() and torch.backends.mps.is_available()
25+
has_xpu = torch.xpu.is_available()
2526

2627
if supported_devices is not None:
2728
if has_cuda and AcceleratorDevice.CUDA not in supported_devices:
@@ -34,12 +35,19 @@ def decide_device(
3435
f"Removing MPS from available devices because it is not in {supported_devices=}"
3536
)
3637
has_mps = False
38+
if has_xpu and AcceleratorDevice.XPU not in supported_devices:
39+
_log.info(
40+
f"Removing XPU from available devices because it is not in {supported_devices=}"
41+
)
42+
has_xpu = False
3743

3844
if accelerator_device == AcceleratorDevice.AUTO.value: # Handle 'auto'
3945
if has_cuda:
4046
device = "cuda:0"
4147
elif has_mps:
4248
device = "mps"
49+
elif has_xpu:
50+
device = "xpu"
4351

4452
elif accelerator_device.startswith("cuda"):
4553
if has_cuda:
@@ -71,6 +79,12 @@ def decide_device(
7179
else:
7280
_log.warning("MPS is not available in the system. Fall back to 'CPU'")
7381

82+
elif accelerator_device == AcceleratorDevice.XPU.value:
83+
if has_xpu:
84+
device = "xpu"
85+
else:
86+
_log.warning("XPU is not available in the system. Fall back to 'CPU'")
87+
7488
elif accelerator_device == AcceleratorDevice.CPU.value:
7589
device = "cpu"
7690

docs/examples/compare_vlm_models.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,11 @@ def convert(sources: list[Path], converter: DocumentConverter):
141141
response_format=ResponseFormat.MARKDOWN,
142142
inference_framework=InferenceFramework.TRANSFORMERS,
143143
transformers_model_type=TransformersModelType.AUTOMODEL_IMAGETEXTTOTEXT,
144-
supported_devices=[AcceleratorDevice.CUDA, AcceleratorDevice.CPU],
144+
supported_devices=[
145+
AcceleratorDevice.CUDA,
146+
AcceleratorDevice.CPU,
147+
AcceleratorDevice.XPU,
148+
],
145149
scale=2.0,
146150
temperature=0.0,
147151
)
@@ -154,7 +158,11 @@ def convert(sources: list[Path], converter: DocumentConverter):
154158
inference_framework=InferenceFramework.TRANSFORMERS,
155159
transformers_model_type=TransformersModelType.AUTOMODEL_IMAGETEXTTOTEXT,
156160
transformers_prompt_style=TransformersPromptStyle.RAW,
157-
supported_devices=[AcceleratorDevice.CUDA, AcceleratorDevice.CPU],
161+
supported_devices=[
162+
AcceleratorDevice.CUDA,
163+
AcceleratorDevice.CPU,
164+
AcceleratorDevice.XPU,
165+
],
158166
scale=2.0,
159167
temperature=0.0,
160168
)

docs/examples/run_with_accelerator.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
# %% [markdown]
2-
# Run conversion with an explicit accelerator configuration (CPU/MPS/CUDA).
2+
# Run conversion with an explicit accelerator configuration (CPU/MPS/CUDA/XPU).
33
#
44
# What this example does
55
# - Shows how to select the accelerator device and thread count.
66
# - Enables OCR and table structure to exercise compute paths, and prints timings.
77
#
88
# How to run
99
# - From the repo root: `python docs/examples/run_with_accelerator.py`.
10-
# - Toggle the commented `AcceleratorOptions` examples to try AUTO/MPS/CUDA.
10+
# - Toggle the commented `AcceleratorOptions` examples to try AUTO/MPS/CUDA/XPU.
1111
#
1212
# Notes
1313
# - EasyOCR does not support `cuda:N` device selection (defaults to `cuda:0`).
1414
# - `settings.debug.profile_pipeline_timings = True` prints profiling details.
15-
# - `AcceleratorDevice.MPS` is macOS-only; `CUDA` requires a compatible GPU and
16-
# CUDA-enabled PyTorch build. CPU mode works everywhere.
15+
# - `AcceleratorDevice.MPS` is macOS-only; `CUDA` and `XPU` require a compatible GPU and
16+
# CUDA/XPU-enabled PyTorch build. CPU mode works everywhere.
1717

1818
# %%
1919

@@ -43,6 +43,9 @@ def main():
4343
# num_threads=8, device=AcceleratorDevice.MPS
4444
# )
4545
# accelerator_options = AcceleratorOptions(
46+
# num_threads=8, device=AcceleratorDevice.XPU
47+
# )
48+
# accelerator_options = AcceleratorOptions(
4649
# num_threads=8, device=AcceleratorDevice.CUDA
4750
# )
4851

docs/usage/vision_models.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ pipeline_options = VlmPipelineOptions(
105105
AcceleratorDevice.CPU,
106106
AcceleratorDevice.CUDA,
107107
AcceleratorDevice.MPS,
108+
AcceleratorDevice.XPU,
108109
],
109110
scale=2.0,
110111
temperature=0.0,

0 commit comments

Comments
 (0)