Skip to content

mtmd : DeepSeek-V4.1-Flash vision (deepseek41v) - #297

Open
danielhanchen wants to merge 10 commits into
dsv41-dsparkfrom
dsv41-vision
Open

danielhanchen wants to merge 10 commits into
dsv41-dsparkfrom
dsv41-vision

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Adds DeepSeek-V4.1-Flash vision: a deepseek41v projector for mtmd, a fix that keeps the aligner's im2col on the GPU, and the vision router bias for image tokens.

Stacked on the DSpark PR (#296, dsv41-dspark). The diff is only this change: 11 files, +259/-10.

What it adds

  • deepseek41v projector (tools/mtmd/):
    • Reuses the DeepSeek-V4-Flash-Vision ViT and 3x3 aligner graph with the V4.1 token block: [START] ([IMAGE]*w [NEWLINE])*h [END] in reading order, with no lead pads, no pad row, no image_pad tensor, and causal decode.
    • Ports the V4.1 resize plan (min_pixels upscale, single solve, 1024-token cap) and PIL ImageOps.pad (round half to even), and rounds the normalized pixels to BF16 as the reference load_image does.
  • Converter: DeepseekV41VisionModel writes the mmproj from the HF-style vision_config.
  • im2col kernel view (tools/mtmd/models/deepseek4v.cpp, shared with V4): im2col only reads the kernel shape. The zero-stride view was non-contiguous, so CUDA rejected the op and the 3x3 unfold fell back to CPU. A contiguous view of the same shape keeps it on the GPU, with byte-identical outputs. test-backend-ops gets the aligner shape.
  • Vision router bias: embedding (image) ubatches use ffn_exp_probs_b_vl as the expert selection bias, as DeepSeek-V4 does for mtmd input. Token ubatches are unchanged.

Verification

All results are local, on B200 (CUDA sm_100a) and x86 CPU, against the official image processor and vision model (unmodified, F32/F64).

  • Preprocessing: patches are byte-identical to the reference on 5 images (and 25/25 resize sizes on an earlier revision).
  • Embeddings, CPU, F32 mmproj: max-abs error 6.6e-7 to 4.1e-5 on 5 images, 0.08x to 0.25x of the reference's own F32-vs-F64 floor: PASS.
  • Embeddings, CUDA, F32 mmproj with F32 cuBLAS compute and TF32 off (2 images): 1.10x to 1.25x of the floor: PASS. With default CUDA settings the embeddings land in the BF16 runtime band.
  • Ops: test-backend-ops -o IM2COL 103/103 on CUDA.
  • DeepSeek-V4 vision: embeddings are byte-identical to the parent on 2 images.
  • Text: logits for text-only input are bit-identical to the parent PR on CPU and CUDA fixtures.
  • Real model (earlier revision): image prompts run on 2 x B200 and the answers match the pre-merge development branch; with the im2col fix, a chart image encodes in 61 ms instead of 174 ms.
  • Revision: the patches are unchanged from the previously gated revision; this revision only rebases onto the parent.

Known gaps

  • Image answers on the real model were smoke-tested, not scored against the official vision stack. One smoke prompt (counting objects) answers 5 where the image has 4 on every revision; it looks like the model's own answer, but that was not checked against the official stack.
  • Multi-image prompts and image caching are not validated.
  • Platforms: validated on CPU and CUDA only. No ROCm, Metal, Vulkan, Windows or macOS validation.

Reuse the DeepSeek-V4-Flash-Vision ViT + 3x3 aligner graph with the V4.1 token
block: [START] ([IMAGE]*w [NEWLINE])*h [END] in reading order, no lead pads,
no pad row, no image_pad tensor, causal decode. Port the V4.1 resize plan
(min_pixels upscale, single solve, 1024-token cap) and PIL ImageOps.pad
(round half to even), and round the normalized pixels to bf16 as load_image
does. Converter: DeepseekV41VisionModel writes the mmproj from the HF-style
vision_config.
im2col only reads the kernel shape. The zero-stride view was non-contiguous,
so CUDA rejected the op and the 3x3 unfold fell back to CPU. A contiguous
view of the same shape keeps it on the GPU; outputs are byte-identical.
Add the aligner shape to test-backend-ops.
…xp_probs_b_vl)

Embedding (image) ubatches use ffn_exp_probs_b_vl as the expert selection
bias, as DeepSeek-V4 does for mtmd input; token ubatches are unchanged.
@danielhanchen
danielhanchen requested a review from CISC as a code owner October 19, 2026 07:50
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-19T08:05:05.398526Z 9168771 PR opened
🔒 Security Review ✅ Completed 2026-10-19T07:55:09.112624Z 9168771 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

Pushed 0aa1608 (merges the #289 and #296 fixes first). The deepseek41v preprocessor now skips the bf16 rounding loop for placeholder images. get_ro_buf() throws on a placeholder, so the server's token-count routes, which tokenize with placeholders, failed for any request with an image.

Regression check over 9 image sizes: placeholder token counts now equal real-image token counts (failures 0/9; before, every placeholder errored with this clip_image_f32 is a placeholder). The existing mtmd tests are unchanged.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant