Skip to content

deepseek-ocr SAM ggml_conv_2d with the im2col kept in F32 - #26727

Merged
ngxson merged 2 commits into
ggml-org:masterfrom
sfallah:sf/dsocr-conv-f32
Aug 18, 2026
Merged

deepseek-ocr SAM ggml_conv_2d with the im2col kept in F32#26727
ngxson merged 2 commits into
ggml-org:masterfrom
sfallah:sf/dsocr-conv-f32

Conversation

@sfallah

@sfallah sfallah commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Description

Since #23660, ggml_conv_2d emits an F16 im2col for all non-F16 conv kernels. The SAM convs in DeepSeek-OCR run F32 weights, and the F16 im2col measurably degrades their OCR quality on CUDA. This PR keeps the im2col of these convs in F32, in the model code. On Metal the output is unchanged.

DeepSeek-OCR eval on CUDA (DGX Spark), single-view scan, this branch vs master:

branch model CER chrF
this PR v1 0.2955 66.72
this PR v2 0.7475 25.09
master v1 0.3249 63.14
master v2 0.8737 8.64

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES - I used AI assistance for code review, debugging, implementation checks, and testing. I have reviewed the submitted changes and take responsibility for the full contents of this PR.

@sfallah
sfallah requested a review from a team as a code owner August 7, 2026 14:41
@github-actions github-actions Bot added the mtmd Related to multimodal functionality (video/image/audio) label Aug 7, 2026
}

// ggml_conv_2d with the im2col kept in F32: the F16 im2col it emits since #23660 degrades OCR
static ggml_tensor * conv_2d_f32(ggml_context * ctx0, ggml_tensor * a, ggml_tensor * b,

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.

if we simply cast the kernel to F32 and run ggml_conv_2d, does it work?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No — the kernels are already F32, and ggml_conv_2d applies an F16 im2col on F32 kernels:

a->type == GGML_TYPE_BF16 ? GGML_TYPE_F32 : GGML_TYPE_F16

That F32-kernel + F16-im2col combination is exactly the regression this PR fixes.

Casting the kernels to BF16 would work; BF16 results in an F32 im2col, and for these weights the cast is lossless (HF ships them BF16; the converter upcasts to F32).
But it is a hack that would rely on the side-effect of a->type == GGML_TYPE_BF16 ? GGML_TYPE_F32 : GGML_TYPE_F16.
It will also cost either a cast node in the graph or a converter change.
But a converter change only helps newly converted GGUF files; every already-published mmproj keeps F32 kernels, so the model code has to handle them either way.

@ngxson
ngxson merged commit 9731ad3 into ggml-org:master Aug 18, 2026
23 of 26 checks passed
@sfallah
sfallah deleted the sf/dsocr-conv-f32 branch August 19, 2026 05:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mtmd Related to multimodal functionality (video/image/audio)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants