Skip to content

Nemotron 3.5 vLLM embed: faster image embedding (flash attention, preprocess once) - #2836

Merged
jioffe502 merged 4 commits into
mainfrom
jioffe/n35-embed-vision-fastpath
Nov 10, 2026
Merged

jioffe502 merged 4 commits into
mainfrom
jioffe/n35-embed-vision-fastpath

Conversation

@jioffe502

@jioffe502 jioffe502 commented Nov 7, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Makes image embedding with Nemotron 3.5 (vLLM) faster. It fixes two things:

  1. Slow attention kernel. We sent several images to the vision model in one call. To keep the images from attending to each other, vLLM builds a mask, and with a mask it can't use flash attention, so it falls back to a slower kernel. This PR sends one image per call, so no mask is needed and flash attention kicks in.
  2. Every image was preprocessed twice. To know how many tokens an image becomes, we ran the full image preprocessor, then vLLM preprocessed the same image again. We now compute the token count from the image's width and height.

Image-heavy ingest (ViDoRe) is ~70% faster; PDF/text is unchanged. The speedup is per image, so it holds at any dataset size. Per-subset results are in the stacked PR #2837.

Dataset main pps PR pps Recall@10
ViDoRe v3 English, 8 GPUs 31.6 54.3 unchanged
ViDoRe v3 English, 1 GPU 5.0 10.9 unchanged
bo10k (PDF/text), 8 GPUs 331.1 333.1 unchanged

Confidence

  • Embeddings match main (cosine ≥ 0.9997).
  • The size-based token count matches the real preprocessor on all 1,006 image sizes tested, and vLLM's actual count is still checked at runtime.
  • Unit tests cover the new paths.
  • The size-based token count also applies to backend="hf" split planning (shared code); the count depends only on image size, so it is backend-independent.

Checklist

  • I am familiar with the Contributing Guidelines.
  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

Link to Devin session: https://nvidia-cloud.devinenterprise.com/sessions/bad125a41197410993e49df9e8ca2f37
Open in Devin Desktop: https://nvidia-cloud.devinenterprise.com/desktop/session/bad125a41197410993e49df9e8ca2f37?variant=devin
Requested by: @jioffe502


🟢 Devin Review found nothing to fix · View findings · Follow Devin's work

Devin Review

…flash attention

The HF Pixtral vision tower concatenates every image in a vLLM multimodal batch into one sequence and runs SDPA with a dense block-diagonal mask. Run the tower once per image and drop the mask for a single image so SDPA can select flash attention. Per-image outputs keep the stock order and shape.

Text+image admission now counts expanded prompt tokens from the image size and the tokenizer instead of running the processor's pixel preprocessing a second time before vLLM. Images smaller than one merged patch still use the processor so its resize and error behavior is unchanged.

Signed-off-by: Jacob Ioffe <jioffe@nvidia.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

devin-ai-integration[bot]

This comment was marked as resolved.

… array

Signed-off-by: Jacob Ioffe <jioffe@nvidia.com>
@devin-ai-integration
devin-ai-integration Bot marked this pull request as ready for review November 9, 2026 22:18
@devin-ai-integration
devin-ai-integration Bot requested review from a team as code owners November 9, 2026 22:18
@devin-ai-integration
devin-ai-integration Bot requested a review from edknv November 9, 2026 22:18
@devin-ai-integration devin-ai-integration Bot changed the title Nemotron 3.5 vLLM embed: per-image vision tower with flash attention Nemotron 3.5 vLLM embed: faster image embedding (flash attention, preprocess once) Nov 9, 2026
@greptile-apps

greptile-apps Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new actionable issues were established.

Summary

This PR speeds up image embedding by counting image tokens from dimensions and running the vision model once per image.

  • Nemotron 3.5 vLLM embeds each image in its own vision call.
  • Image token checks and split planning use image dimensions.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Text and image] --> B[Count expanded tokens from image size]
  B --> C{Within configured limit?}
  C -->|No| D[Reject before inference]
  C -->|Yes| E[vLLM preprocesses image]
  E --> F[Run vision model once per image]
  F --> G{Returned token count matches?}
  G -->|No| H[Raise mismatch error]
  G -->|Yes| I[Return embedding]
Loading

Reviews (3) · Last reviewed commit: "Merge branch 'main' into jioffe/n35-embe..." · Reviewed by Greptile

greptile-apps[bot]

This comment was marked as resolved.

for size in sizes:
offsets.append(offsets[-1] + size)

def item(value: object, index: int) -> object:

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.

[P3] Consider specializing the slicing to the actual Pixtral contract

This is a nonblocking minimality suggestion. The pinned Pixtral processor reports num_image_patches = [1] * len(image_sizes), and this architecture is already restricted to the Mistral3/Ministral3 retrieval profile. The cumulative offsets plus generic shape-based dispatch over every kwarg support a broader patch/field contract than this model currently uses. Consider slicing the known per-image Pixtral fields directly and continuing to delegate inference/output handling to the parent. That would remove the unused flattened-patch machinery and make the input assumptions explicit. The new test currently uses [2, 3] with a 2-D fake pixel tensor; add or use the real [1, 1] case with 4-D pixels and paired image_sizes to verify the production contract.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Leaving as is: the generic slicing is tested and delegates to the parent, and changing it here would cascade into #2837/#2838 for a nonblocking cleanup. Happy to tighten it in a follow-up.

return_tensors="pt",
)
return int(batch["attention_mask"].sum())
return self._image_prompt_lengths([prompt], [image])[0]

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.

[P3] Clarify or limit the HF admission change beyond the stated vLLM scope

The description presents this as a vLLM optimization, but plan_text_image is shared by both backends, so replacing its processor-based counter unconditionally also changes backend="hf" admission. HF inference still uses the full processor while its split planner now predicts expansion from sizes. I did not find a count mismatch for the supported defaults, so this is a scope/validation comment rather than a confirmed bug. For the smallest vLLM-only change, retain the existing counter for HF; if the shared optimization is intentional, call it out in the description and include an HF planning-versus-processor check near the input limit.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Intentional: the count only depends on image size, so it's the same for both backends, and it matched the real processor on all 1,006 sizes tested. Noted the HF effect in the description.

@jioffe502
jioffe502 merged commit e209550 into main Nov 10, 2026
12 checks passed
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.

2 participants