Repository navigation
Add Deepseek-OCR-2 model #45075
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add Deepseek-OCR-2 model #45075
Changes from all commits
Commits
Show all changes
86 commits
Select commit
Hold shift + click to select a range
e6bed84
feat: add DeepSeek-OCR-2 model registration and docs
thisisiron 7a1a400
feat: add DeepseekOcr2ImageProcessor
thisisiron f3ec285
feat: add DeepseekOcr2Processor and refactor image processor tile_size
thisisiron 95b0d57
feat: add script to convert DeepSeek-OCR-2 weights to Hugging Face fo…
thisisiron b81bc0f
feat: enhance DeepSeek-OCR-2 processing and inference test
thisisiron bbf3f94
refactor
thisisiron 2e05944
feat: add fast image processor
thisisiron 8f3270b
refactor
thisisiron 3ee14eb
test: add image processor tests for DeepseekOcr2
thisisiron 58b683e
fix: make background_color configurable
thisisiron 3710499
refactor: migrate image processors to pil/torchvision backend pattern
thisisiron 1bfa054
test: add processor tests for DeepseekOcr2
thisisiron 1878af2
fix: update __init__
thisisiron 46ddaec
chore: clean up unused imports and fix formatting
thisisiron 8754d8d
feat: add configuration, modeling, and modular for DeepseekOcr2
thisisiron fef7b5f
fix: style fixes, update docs, and minor cleanups
thisisiron 5520c31
fix: use @strict
thisisiron fee7de0
fix: register private models
thisisiron 8531218
docs: add usage example and expand DeepSeek-OCR-2 model doc
thisisiron f6fc20b
fix: add checkpoint to auto_docstring
thisisiron b4bfbf5
fix: remove comment
thisisiron e775577
fix: remove unused max_query
thisisiron 44482df
fix: clean up DeepSeek-OCR2 modular
thisisiron 4b1605a
Merge branch 'main' into add-deepseek_ocr2
thisisiron c6f5eaf
docs: update date
thisisiron f05c252
refactor: inherit SamVisionEncoder
thisisiron 74ee9f3
refactor: use create_causal_mask with or_mask_function
thisisiron 25c5454
Merge branch 'main' into add-deepseek_ocr2
thisisiron 33a6159
Merge branch 'main' into add-deepseek_ocr2
thisisiron 2f931aa
docs: update date
thisisiron d194f99
fix: address PR review
thisisiron c08b036
refactor: use modular for image processor
thisisiron 5ce5029
refactor: restructure DeepSeek-OCR-2 config, image processor, and pro…
thisisiron 917d086
Merge branch 'main' into add-deepseek_ocr2
thisisiron eec01bc
fix: sync hidden_size and rms_norm_eps from encoder_config to vision_…
thisisiron 7ef2141
Merge branch 'main' into add-deepseek_ocr2
thisisiron 10639ec
refactor: remove comment
thisisiron 1efd730
fix
thisisiron 9c70392
fix: correct EncoderConfig docstring example
thisisiron b9c75c6
refactor: add PIL image processor to modular
thisisiron 1433b32
refactor: address review comments on config, processor, and model
thisisiron 22392ad
Merge branch 'main' into add-deepseek_ocr2
thisisiron 6b95f09
fix: adjust processing tests for image token expansion
thisisiron 55fc4aa
fix: move view_separator to correct device for model parallelism
thisisiron 68b5d42
Merge branch 'main' into add-deepseek_ocr2
thisisiron f85aabb
fix: add DeepseekOcr2ImageProcessorPil to __all__
thisisiron c96106a
fix: remove SDPA skip
thisisiron 8dbfda5
test: skip offload/export tests
thisisiron 8b391e6
refactor: address review comments
thisisiron 0102cbf
Merge branch 'main' into add-deepseek_ocr2
thisisiron 451fd53
fix: remove in DeepseekOcr2Model
thisisiron 0f73ee0
refactor: enforce explicit tokens in DeepseekOcr2Processor
thisisiron 50b210c
Merge branch 'main' into add-deepseek_ocr2
thisisiron f81b8b9
refactor: inherit DeepseekOcr2ImageProcessorKwargs from GotOcr2ImageP…
thisisiron d84deae
refactor: remove unused image processing
thisisiron 765a156
fix: update docs
thisisiron 45aab0f
Merge remote-tracking branch 'origin/main' into add-deepseek_ocr2
thisisiron cd72881
fix: export sub-configs
thisisiron 54294ac
Update src/transformers/models/deepseek_ocr2/modular_deepseek_ocr2.py
thisisiron 9310aaf
refactor: switch to mlp_layer_types pattern and clean up redundant ov…
thisisiron 78fdc1c
refactor: derive projector dims from sub-configs
thisisiron a73c8a4
refactor: address comments on naming, decorators, and mask location
thisisiron 9389109
refactor: use VLMModelTester, fix processor text handling, and cache …
thisisiron dddd685
Merge branch 'add-deepseek_ocr2' of https://github.com/thisisiron/tra…
thisisiron d9180fd
fix: enable fullgraph compile
thisisiron 916c8b0
refactor: move weight mapping to conversion_mapping.py
thisisiron 9a9b736
fix: add tie_word_embeddings
thisisiron 1d13301
Merge branch 'main' into add-deepseek_ocr2
thisisiron 3d7c884
Merge branch 'main' into add-deepseek_ocr2
thisisiron 829f82b
refactor: address review comments
thisisiron b7ae2eb
Merge branch 'main' into add-deepseek_ocr2
thisisiron 0c18ada
make ci compatible, passed without changes neat
vasqu 2ba5ede
fixup imports
vasqu 865faa9
Merge branch 'main' into add-deepseek_ocr2
thisisiron 2003552
switch vision encoder mask to block_sequence_ids
thisisiron 17ee704
Merge branch 'main' into add-deepseek_ocr2
thisisiron 28fa3a5
update docs
thisisiron 2772070
Merge branch 'main' into add-deepseek_ocr2
thisisiron 077e99e
update docs
thisisiron 3c8fb52
Clean up DeepSeek-OCR-2 converted config
thisisiron 36c883f
update DeepSeek-OCR-2 checkpoint
thisisiron 9482d7e
Merge branch 'main' into add-deepseek_ocr2
thisisiron fd0e526
update docs and sync generated config
thisisiron b406912
fix
vasqu cf87495
Merge branch 'main' into add-deepseek_ocr2
vasqu 5b87e5f
ok
vasqu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| <!--Copyright 2026 The HuggingFace Team. All rights reserved. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| the License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| specific language governing permissions and limitations under the License. | ||
|
|
||
| ⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be | ||
| rendered properly in your Markdown viewer. | ||
|
|
||
| --> | ||
| *This model was published in HF papers on 2026-01-28 and contributed to Hugging Face Transformers on 2026-06-01.* | ||
|
|
||
| # DeepSeek-OCR-2 | ||
|
|
||
|
|
||
| ## Overview | ||
|
|
||
| The DeepSeek-OCR-2 model was proposed in [Visual Causal Flow: A Novel Approach to OCR-Specialized Vision-Language Models](https://huggingface.co/papers/2601.20552) by the DeepSeek team. | ||
|
|
||
| DeepSeek-OCR-2 is an OCR-specialized vision-language model built on a distinctive architecture: a SAM ViT-B vision encoder feeds into a Qwen2 hybrid attention encoder, which is connected through an MLP projector to a DeepSeek-V2 Mixture-of-Experts (MoE) language model. A key feature of the model is its hybrid attention mechanism, which applies bidirectional attention over image tokens and causal attention over query tokens, enabling efficient and accurate document understanding. | ||
|
|
||
| <img src="https://huggingface.co/deepseek-ai/DeepSeek-OCR-2/resolve/main/assets/fig1.png" width="600"> | ||
|
|
||
| <small> DeepSeek-OCR 2: Visual Causal Flow.</small> | ||
|
|
||
| This model was contributed by [thisisiron](https://huggingface.co/thisisiron). | ||
|
|
||
|
|
||
| ## Usage example | ||
|
|
||
| ### Plain OCR | ||
|
|
||
| ```python | ||
| from transformers import AutoProcessor, AutoModelForImageTextToText | ||
|
|
||
| model = AutoModelForImageTextToText.from_pretrained( | ||
| "deepseek-community/DeepSeek-OCR-2", device_map="auto" | ||
| ) | ||
| processor = AutoProcessor.from_pretrained("deepseek-community/DeepSeek-OCR-2") | ||
|
|
||
| image = "https://huggingface.co/datasets/hf-internal-testing/fixtures_got_ocr/resolve/main/image_ocr.jpg" | ||
| inputs = processor(images=image, text="<image>\nFree OCR.", return_tensors="pt").to(model.device) | ||
|
|
||
| generate_ids = model.generate(**inputs, do_sample=False, max_new_tokens=256) | ||
| processor.decode(generate_ids[0, inputs["input_ids"].shape[1] :], skip_special_tokens=True) | ||
| # "R&D QUALITY IMPROVEMENT\nSUGGESTION/SOLUTION FORM\nName/Phone Ext. : (...)" | ||
| ``` | ||
|
|
||
| ### Grounding with markdown conversion | ||
|
|
||
| The `<|grounding|>` token enables coordinate-aware output with `<|ref|>` and `<|det|>` tags. | ||
|
|
||
| ```python | ||
| inputs = processor( | ||
| images=image, | ||
| text="<image>\n<|grounding|>Convert the document to markdown.", | ||
| return_tensors="pt", | ||
| ).to(model.device) | ||
|
|
||
| generate_ids = model.generate(**inputs, do_sample=False, max_new_tokens=256) | ||
| processor.decode(generate_ids[0, inputs["input_ids"].shape[1] :], skip_special_tokens=False) | ||
| # "<|ref|>title<|/ref|><|det|>[[330, 198, 558, 230]]<|/det|>\n# R&D QUALITY (...)" | ||
| ``` | ||
|
|
||
| ## DeepseekOcr2Config | ||
|
|
||
| [[autodoc]] DeepseekOcr2Config | ||
|
|
||
| ## DeepseekOcr2VisionConfig | ||
|
|
||
| [[autodoc]] DeepseekOcr2VisionConfig | ||
|
|
||
| ## DeepseekOcr2SamVisionConfig | ||
|
|
||
| [[autodoc]] DeepseekOcr2SamVisionConfig | ||
|
|
||
| ## DeepseekOcr2VisionEncoderConfig | ||
|
|
||
| [[autodoc]] DeepseekOcr2VisionEncoderConfig | ||
|
|
||
| ## DeepseekOcr2TextConfig | ||
|
|
||
| [[autodoc]] DeepseekOcr2TextConfig | ||
|
|
||
| ## DeepseekOcr2ImageProcessor | ||
|
|
||
| [[autodoc]] DeepseekOcr2ImageProcessor | ||
|
|
||
| ## DeepseekOcr2ImageProcessorPil | ||
|
|
||
| [[autodoc]] DeepseekOcr2ImageProcessorPil | ||
|
|
||
| ## DeepseekOcr2Processor | ||
|
|
||
| [[autodoc]] DeepseekOcr2Processor | ||
|
|
||
| ## DeepseekOcr2TextModel | ||
|
|
||
| [[autodoc]] DeepseekOcr2TextModel | ||
|
|
||
| ## DeepseekOcr2VisionModel | ||
|
|
||
| [[autodoc]] DeepseekOcr2VisionModel | ||
|
|
||
| ## DeepseekOcr2Model | ||
|
|
||
| [[autodoc]] DeepseekOcr2Model | ||
|
|
||
| ## DeepseekOcr2ForConditionalGeneration | ||
|
|
||
| [[autodoc]] DeepseekOcr2ForConditionalGeneration | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # Copyright 2026 The HuggingFace Team. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from ...utils import _LazyModule | ||
| from ...utils.import_utils import define_import_structure | ||
|
|
||
|
|
||
| if TYPE_CHECKING: | ||
| from .configuration_deepseek_ocr2 import * | ||
| from .image_processing_deepseek_ocr2 import * | ||
| from .image_processing_pil_deepseek_ocr2 import * | ||
| from .modeling_deepseek_ocr2 import * | ||
| from .processing_deepseek_ocr2 import * | ||
| else: | ||
| import sys | ||
|
|
||
| _file = globals()["__file__"] | ||
| sys.modules[__name__] = _LazyModule(__name__, _file, define_import_structure(_file), module_spec=__spec__) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.