Skip to content

[Feat] model: Add MiniCPM-OCR 1.0 support - #45799

Open
tc-mb wants to merge 2 commits into
sgl-project:mainfrom
tc-mb:model/MiniCPM-OCR-1.0
Open

tc-mb wants to merge 2 commits into
sgl-project:mainfrom
tc-mb:model/MiniCPM-OCR-1.0

Conversation

@tc-mb

@tc-mb tc-mb commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

MiniCPM-OCR 1.0 is the MiniCPM-V 4.6 stack with a different model_type, so
this only registers it: MiniCPMOcrForConditionalGeneration subclasses the 4.6
model unchanged, plus its own config and image-geometry alignment.

Modifications

  • configs/minicpm_ocr_1_0.py: MiniCPMOCR1_0Config /
    MiniCPMOCR1_0VisionConfig subclass the 4.6 configs and pin model_type to
    the checkpoint's values. Registered in configs/__init__.py and
    _CONFIG_REGISTRY.
  • models/minicpmv.py: MiniCPMOcrForConditionalGeneration for
    model_type: minicpm_ocr, routed to the 4.6 version.
  • multimodal/processors/minicpmv4_6.py: new align_merge_factor on
    MiniCPMV4_6ImageProcessor (default 4, so 4.6 is unaffected). OCR's image
    processor applies a single 2x2 merge in "4x", so it uses 2.
  • parser/conversation.py: skip the legacy conv template for minicpm_ocr, so
    the checkpoint's own chat_template.jinja is used.
  • model_loader/loader.py: also set shared_experts_config on the inner
    minicpmv module. MiniCPMV forwards attributes via __getattr__ and
    DefaultModelLoader returns model.eval(), which resolves to the inner
    module, so the attribute set on the wrapper is lost. Not OCR-specific — 4.6
    and 4.7 hit the same AttributeError at warmup.

Accuracy Tests

Run with chat_template_kwargs={"enable_thinking": false}, otherwise the
template emits <think>.

English smoke document, matches the reference:

OCR Smoke Test Document
This is a paragraph for layout detection.
MiniCPM-V OCR pipeline should recognize text
Formula: \( E = mc^2 \)
Table-like lines
Name Score
Alice 95
Bob 88

Chinese crop:

上海斯格威铂尔文大酒店
打浦路15号
绿洲仕格维花园公寓
打浦路252935号

Speed Tests and Profiling

Not benchmarked.

Checklist

  • Format your code according to the pre-commit guide.
  • Add unit tests. The model path needs the released checkpoint; happy to add
    if there is a pattern to follow.
  • Update documentation.
  • Provide accuracy results (above).
  • Follow the SGLang code style guidance.

CI States

Latest PR Test (Base): ⊘ Skipped -- add the run-ci label to run CI tests.
Latest PR Test (Extra): ⊘ Skipped -- add the run-ci label to run CI tests.
Latest PR Test (AMD ROCm 10): ➖ No AMD PR run found for this commit.

Tip

Base CI only covers the core cases, and its workload may not exercise your change. Use /rerun-test to run the tests your change touches, or copy this prompt for an agent:

Find the registered tests this PR affects and run them with /rerun-test, following https://github.com/sgl-project/sglang/blob/main/.agents/skills/rerun-test/SKILL.md

@github-actions

This comment has been minimized.

EntryClass = [
MiniCPMV,
MiniCPMV4_6ForConditionalGeneration,
MiniCPMV4_7ForConditionalGeneration,
MiniCPMOcrForConditionalGeneration,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The AWQ checkpoint loads but ignores the image, because its skip list names merger while the 4.6 path quantizes it as resampler. Consider mapping that exclusion onto resampler, or rejecting quantized checkpoints at startup.

Comment on lines +310 to +312
inner = getattr(model, "minicpmv", None)
if inner is not None:
inner.shared_experts_config = shared_experts

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This restores the lost attribute for every MiniCPMV wrapper, OCR included, but probes one model family inside the shared loader. Would it make sense to fix it in the wrapper, as #41391 does?

Comment on lines +346 to +349
if getattr(hf_config, "model_type", None) == "minicpm_ocr":
align_merge_factor = 2 if downsample_mode == "4x" else 4
else:
align_merge_factor = 4

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No test fails if this branch goes, yet without it 13 of 18 sizes we tried diverge from the HF OCR processor's slicing. Could you add a small CPU case checking OCR target sizes?

@@ -1860,8 +1865,17 @@ class MiniCPMV4_7ForConditionalGeneration(MiniCPMV4_6ForConditionalGeneration):
pass


class MiniCPMOCR1_0ForConditionalGeneration(MiniCPMV4_6ForConditionalGeneration):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing uses this intermediate class except the one below, and the OCR config's drop_vision_last_layer assignment repeats the 4.6 default. Consider folding the class into MiniCPMOcrForConditionalGeneration and dropping that line.

@@ -333,6 +342,11 @@ def __init__(self, hf_config, server_args, _processor, *args, **kwargs):
getattr(vision_cfg, "patch_size", 14) if vision_cfg is not None else 14
)
downsample_mode = getattr(hf_config, "downsample_mode", "16x")
# OCR find_best_resize: one 2x2 merge in 4x, two in 16x.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment repeats the one beside the new align_merge_factor parameter. Would it make sense to keep only that one?

… target

A DSpark draft may sample in its own cropped output vocabulary
(draft_vocab_size < vocab_size) and map the sampled ids onto target ids
through the checkpoint's d2t table.

- VanillaMarkov biases only the draft's output vocabulary: markov_w1 still
  embeds previous target tokens, while markov_w2 spans bias_vocab_size.
- A cropped draft owns its ParallelLMHead and a draft_token_to_target
  buffer, and decodes the delta-encoded d2t table into absolute ids.
- Draft steps write in the draft vocabulary; the block scatters the kept
  step logits onto the target vocabulary with -inf elsewhere, which leaves
  the softmax unchanged.
- The folded sampler sizes the draft buffer wherever step_logits was
  allocated and declines the single-vocabulary megakernel sampler.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants