docling-project / docling-project/docling
\\n on GPU. \n on CPU
- Dominant language
- Python
- Stars
- 66.4k
- Forks
- 4.8k
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 84
Description
### Bug
When you export a document as markdown, there is a different behavior depending on whether you use a GPU or CPU.
...
### Steps to reproduce
1. Use the pdf file provided or use your own. [lorem.pdf](https://github.com/user-attachments/files/24048973/lorem.pdf)
2. Change the path to the file in the code. Currently says `data/input/lorem.pdf`
Run this code on CPU or GPU:
```
from pathlib import Path
import torch
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
from docling_core.types.doc.base import ImageRefMode
def main() -> None:
# 1. Setup Accelerator Options
try:
if torch.cuda.is_available():
print("CUDA is available - using GPU acceleration")
accelerator_options = AcceleratorOptions(device="cuda")
else:
print("PyTorch CUDA not available - falling back to CPU")
accelerator_options = AcceleratorOptions(device="cpu")
except ImportError:
print("PyTorch not available - falling back to CPU")
accelerator_options = AcceleratorOptions(device="cpu")
# 2. Setup Pipeline Options
pipeline_options = PdfPipelineOptions()
pipeline_options.accelerator_options = accelerator_options
pipeline_options.do_ocr = True
# 3. Create Converter
converter = DocumentConverter(
format_options={
InputFormat.PDF: PdfFormatOption(
pipeline_options=pipeline_options,
)
}
)
# 4. Convert Document
file_path = Path("data/input/lorem.pdf")
if not file_path.exists():
print(f"File not found: {file_path}")
return
print(f"Converting {file_path}...")
result = converter.convert(file_path)
document = result.document
# 5. Export to Markdown
print("Exporting to Markdown...")
markdown_content = document.export_to_markdown(
image_mode=ImageRefMode.REFERENCED,
page_break_placeholder="<-- Page Break -->",
)
print(repr(markdown_content))
# Check for the specific issue mentioned in SectionChunker
if "\\n" in markdown_content:
print("Detected double backslash newlines (\\\\n) in content.")
else:
print("No double backslash newlines detected.")
if __name__ == "__main__":
main()
```
On CPU:
`## The standard Lorem Ipsum passage, used since the 1500s\n\n"Lorem ipsum dolor sit amet, consectetur adipiscing elit, [...]
`
On GPU:
`## The standard Lorem Ipsum passage, used since the 1500s\\n\\n\"Lorem ipsum dolor sit amet, consectetur adipiscing elit, [...]
`
Notice the` \\n` on the GPU.
...
### Docling version
2.64.0
...
### Python version
Python 3.12.10
...
Contributor guide
Assessment
This issue has not been assessed yet.