|
| 1 | +# ⚡ RTX GPU Acceleration |
| 2 | + |
| 3 | +<div style="text-align: center"> |
| 4 | + <img loading="lazy" alt="Docling on RTX" src="../../assets/nvidia_logo_green.svg" width="200px" /> |
| 5 | +</div> |
| 6 | + |
| 7 | + |
| 8 | +Whether you're an AI enthusiast, researcher, or developer working with document processing, this guide will help you unlock the full potential of your NVIDIA RTX GPU with Docling. |
| 9 | + |
| 10 | +By leveraging GPU acceleration, you can achieve up to **6x speedup** compared to CPU-only processing. This dramatic performance improvement makes GPU acceleration especially valuable for processing large batches of documents, handling high-throughput document conversion workflows, or experimenting with advanced document understanding models. |
| 11 | + |
| 12 | +<!-- TBA. Performance improvement figure. --> |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | + |
| 16 | +Before setting up GPU acceleration, ensure you have: |
| 17 | + |
| 18 | +- An NVIDIA RTX GPU (RTX 40/50 series) |
| 19 | +- Windows 10/11 or Linux operating system |
| 20 | + |
| 21 | +## Installation Steps |
| 22 | + |
| 23 | +### 1. Install NVIDIA GPU Drivers |
| 24 | + |
| 25 | +First, ensure you have the latest NVIDIA GPU drivers installed: |
| 26 | + |
| 27 | +- **Windows**: Download from [NVIDIA Driver Downloads](https://www.nvidia.com/Download/index.aspx) |
| 28 | +- **Linux**: Use your distribution's package manager or download from NVIDIA |
| 29 | + |
| 30 | +Verify the installation: |
| 31 | + |
| 32 | +```bash |
| 33 | +nvidia-smi |
| 34 | +``` |
| 35 | + |
| 36 | +This command should display your GPU information and driver version. |
| 37 | + |
| 38 | +### 2. Install CUDA Toolkit |
| 39 | + |
| 40 | +CUDA is NVIDIA's parallel computing platform required for GPU acceleration. |
| 41 | + |
| 42 | +Follow the official installation guide for your operating system at [NVIDIA CUDA Downloads](https://developer.nvidia.com/cuda-downloads). The installer will guide you through the process and automatically set up the required environment variables. |
| 43 | + |
| 44 | +### 3. Install cuDNN |
| 45 | + |
| 46 | +cuDNN provides optimized implementations for deep learning operations. |
| 47 | + |
| 48 | +Follow the official installation guide at [NVIDIA cuDNN Downloads](https://developer.nvidia.com/cudnn). The guide provides detailed instructions for all supported platforms. |
| 49 | + |
| 50 | +### 4. Install PyTorch with CUDA Support |
| 51 | + |
| 52 | +To use GPU acceleration with Docling, you need to install PyTorch with CUDA support using the special `extra-index-url`: |
| 53 | + |
| 54 | +```bash |
| 55 | +# For CUDA 12.8 (current default for PyTorch) |
| 56 | +pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu128 |
| 57 | + |
| 58 | +# For CUDA 13.0 |
| 59 | +pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu130 |
| 60 | +``` |
| 61 | + |
| 62 | +!!! note |
| 63 | + The `--index-url` parameter is crucial as it ensures you get the CUDA-enabled version of PyTorch instead of the CPU-only version. |
| 64 | + |
| 65 | +For other CUDA versions and installation options, refer to the [PyTorch Installation Matrix](https://pytorch.org/get-started/locally/). |
| 66 | + |
| 67 | +Verify PyTorch CUDA installation: |
| 68 | + |
| 69 | +```python |
| 70 | +import torch |
| 71 | +print(f"PyTorch version: {torch.__version__}") |
| 72 | +print(f"CUDA available: {torch.cuda.is_available()}") |
| 73 | +print(f"CUDA version: {torch.version.cuda}") |
| 74 | +print(f"GPU device: {torch.cuda.get_device_name(0) if torch.cuda.is_available() else 'None'}") |
| 75 | +``` |
| 76 | + |
| 77 | +### 5. Install and Run Docling |
| 78 | + |
| 79 | +Install Docling with all dependencies: |
| 80 | + |
| 81 | +```bash |
| 82 | +pip install docling |
| 83 | +``` |
| 84 | + |
| 85 | +**That's it!** Docling will automatically detect and use your RTX GPU when available. No additional configuration is required for basic usage. |
| 86 | + |
| 87 | +```python |
| 88 | +from docling.document_converter import DocumentConverter |
| 89 | + |
| 90 | +# Docling automatically uses GPU when available |
| 91 | +converter = DocumentConverter() |
| 92 | +result = converter.convert("document.pdf") |
| 93 | +``` |
| 94 | + |
| 95 | +<details> |
| 96 | +<summary><b>Advanced: Tuning GPU Performance</b></summary> |
| 97 | + |
| 98 | +For optimal GPU performance with large document batches, you can adjust batch sizes and explicitly configure the accelerator: |
| 99 | + |
| 100 | +```python |
| 101 | +from docling.document_converter import DocumentConverter |
| 102 | +from docling.datamodel.accelerator_options import AcceleratorDevice, AcceleratorOptions |
| 103 | +from docling.datamodel.pipeline_options import ThreadedPdfPipelineOptions |
| 104 | + |
| 105 | +# Explicitly configure GPU acceleration |
| 106 | +accelerator_options = AcceleratorOptions( |
| 107 | + device=AcceleratorDevice.CUDA, # Use CUDA for NVIDIA GPUs |
| 108 | +) |
| 109 | + |
| 110 | +# Configure pipeline for optimal GPU performance |
| 111 | +pipeline_options = ThreadedPdfPipelineOptions( |
| 112 | + ocr_batch_size=64, # Increase batch size for GPU |
| 113 | + layout_batch_size=64, # Increase batch size for GPU |
| 114 | + table_batch_size=4, |
| 115 | +) |
| 116 | + |
| 117 | +# Create converter with custom settings |
| 118 | +converter = DocumentConverter( |
| 119 | + accelerator_options=accelerator_options, |
| 120 | + pipeline_options=pipeline_options, |
| 121 | +) |
| 122 | + |
| 123 | +# Convert documents |
| 124 | +result = converter.convert("document.pdf") |
| 125 | +``` |
| 126 | + |
| 127 | +Adjust batch sizes based on your GPU memory (see Performance Optimization Tips below). |
| 128 | + |
| 129 | +</details> |
| 130 | + |
| 131 | +## GPU-Accelerated VLM Pipeline |
| 132 | + |
| 133 | +For maximum performance with Vision Language Models (VLM), you can run a local inference server on your RTX GPU. This approach provides significantly better throughput than inline VLM processing. |
| 134 | + |
| 135 | +### Linux: Using vLLM (Recommended) |
| 136 | + |
| 137 | +vLLM provides the best performance for GPU-accelerated VLM inference. Start the vLLM server with optimized parameters: |
| 138 | + |
| 139 | +```bash |
| 140 | +vllm serve ibm-granite/granite-docling-258M \ |
| 141 | + --host 127.0.0.1 --port 8000 \ |
| 142 | + --max-num-seqs 512 \ |
| 143 | + --max-num-batched-tokens 8192 \ |
| 144 | + --enable-chunked-prefill \ |
| 145 | + --gpu-memory-utilization 0.9 |
| 146 | +``` |
| 147 | + |
| 148 | +### Windows: Using llama-server |
| 149 | + |
| 150 | +On Windows, you can use `llama-server` from llama.cpp for GPU-accelerated VLM inference: |
| 151 | + |
| 152 | +#### Installation |
| 153 | + |
| 154 | +1. Download the latest llama.cpp release from the [GitHub releases page](https://github.com/ggml-org/llama.cpp/releases) |
| 155 | +2. Extract the archive and locate `llama-server.exe` |
| 156 | + |
| 157 | +#### Launch Command |
| 158 | + |
| 159 | +```powershell |
| 160 | +llama-server.exe ` |
| 161 | + --hf-repo ibm-granite/granite-docling-258M-GGUF ` |
| 162 | + -cb ` |
| 163 | + -ngl -1 ` |
| 164 | + --port 8000 ` |
| 165 | + --context-shift ` |
| 166 | + -np 16 -c 131072 |
| 167 | +``` |
| 168 | + |
| 169 | +!!! note "Performance Comparison" |
| 170 | + vLLM delivers approximately **4x better performance** compared to llama-server. For Windows users seeking maximum performance, consider running vLLM via WSL2 (Windows Subsystem for Linux). See [vLLM on RTX 5090 via Docker](https://github.com/BoltzmannEntropy/vLLM-5090) for detailed WSL2 setup instructions. |
| 171 | + |
| 172 | +### Configure Docling for VLM Server |
| 173 | + |
| 174 | +Once your inference server is running, configure Docling to use it: |
| 175 | + |
| 176 | +```python |
| 177 | +from docling.datamodel.pipeline_options import VlmPipelineOptions |
| 178 | +from docling.datamodel.settings import settings |
| 179 | + |
| 180 | +BATCH_SIZE = 64 |
| 181 | + |
| 182 | +# Configure VLM options |
| 183 | +vlm_options = vlm_model_specs.GRANITEDOCLING_VLLM_API |
| 184 | +vlm_options.concurrency = BATCH_SIZE |
| 185 | + |
| 186 | +# when running with llama.cpp (llama-server), use the different model name. |
| 187 | +# vlm_options.params["model"] = "ibm-granite_granite-docling-258M-GGUF_granite-docling-258M-BF16.gguf" |
| 188 | + |
| 189 | +# Set page batch size to match or exceed concurrency |
| 190 | +settings.perf.page_batch_size = BATCH_SIZE |
| 191 | + |
| 192 | +# Create converter with VLM pipeline |
| 193 | +converter = DocumentConverter( |
| 194 | + pipeline_options=vlm_options, |
| 195 | +) |
| 196 | +``` |
| 197 | + |
| 198 | +For more details on VLM pipeline configuration, see the [GPU Support Guide](../usage/gpu.md). |
| 199 | + |
| 200 | +## Performance Optimization Tips |
| 201 | + |
| 202 | +### Batch Size Tuning |
| 203 | + |
| 204 | +Adjust batch sizes based on your GPU memory: |
| 205 | + |
| 206 | +- **RTX 5090 (32GB)**: Use batch sizes of 64-128 |
| 207 | +- **RTX 4090 (24GB)**: Use batch sizes of 32-64 |
| 208 | +- **RTX 5070 (12GB)**: Use batch sizes of 16-32 |
| 209 | + |
| 210 | +### Memory Management |
| 211 | + |
| 212 | +Monitor GPU memory usage: |
| 213 | + |
| 214 | +```python |
| 215 | +import torch |
| 216 | + |
| 217 | +# Check GPU memory |
| 218 | +if torch.cuda.is_available(): |
| 219 | + print(f"GPU Memory allocated: {torch.cuda.memory_allocated(0) / 1024**3:.2f} GB") |
| 220 | + print(f"GPU Memory reserved: {torch.cuda.memory_reserved(0) / 1024**3:.2f} GB") |
| 221 | +``` |
| 222 | + |
| 223 | +## Troubleshooting |
| 224 | + |
| 225 | +### CUDA Out of Memory |
| 226 | + |
| 227 | +If you encounter out-of-memory errors: |
| 228 | + |
| 229 | +1. Reduce batch sizes in `pipeline_options` |
| 230 | +2. Process fewer documents concurrently |
| 231 | +3. Clear GPU cache between batches: |
| 232 | + |
| 233 | +```python |
| 234 | +import torch |
| 235 | +torch.cuda.empty_cache() |
| 236 | +``` |
| 237 | + |
| 238 | +### CUDA Not Available |
| 239 | + |
| 240 | +If `torch.cuda.is_available()` returns `False`: |
| 241 | + |
| 242 | +1. Verify NVIDIA drivers are installed: `nvidia-smi` |
| 243 | +2. Check CUDA installation: `nvcc --version` |
| 244 | +3. Reinstall PyTorch with correct CUDA version |
| 245 | +4. Ensure your GPU is CUDA-compatible |
| 246 | + |
| 247 | +### Performance Not Improving |
| 248 | + |
| 249 | +If GPU acceleration doesn't improve performance: |
| 250 | + |
| 251 | +1. Increase batch sizes (if memory allows) |
| 252 | +2. Ensure you're processing enough documents to benefit from GPU parallelization |
| 253 | +3. Check GPU utilization: `nvidia-smi -l 1` |
| 254 | +4. Verify PyTorch is using GPU: `torch.cuda.is_available()` |
| 255 | + |
| 256 | +## Additional Resources |
| 257 | + |
| 258 | +- [NVIDIA CUDA Documentation](https://docs.nvidia.com/cuda/) |
| 259 | +- [PyTorch CUDA Installation Guide](https://pytorch.org/get-started/locally/) |
| 260 | +- [Docling GPU Support Guide](../usage/gpu.md) |
| 261 | +- [GPU Performance Examples](../examples/gpu_standard_pipeline.py) |
0 commit comments