Cache visual features in the VLM eval decode loop - #166
Open
camilobrownpinilla wants to merge 4 commits into
Open
Cache visual features in the VLM eval decode loop#166camilobrownpinilla wants to merge 4 commits into
camilobrownpinilla wants to merge 4 commits into
Conversation
Encode the clip once per request via model.encode_visual and reuse the projected embeds each step instead of re-running the vision tower.
Pass only the cache to forward (never both), skip the vision tower when max_new_tokens is 0, drop finished rows from the batch, and write tokens into a preallocated buffer. Tests now assert the cache reaches forward and pin the mock to the real forward signature.
camilobrownpinilla
force-pushed
the
feat/image-encode-cache
branch
from
July 23, 2026 22:09
b3bace2 to
16fda85
Compare
camilobrownpinilla
marked this pull request as ready for review
July 23, 2026 22:10
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes the VLM eval adapter’s autoregressive decode loop by encoding visual inputs once per request (via model.encode_visual) and reusing the projected visual embeddings across decode steps, eliminating redundant vision-tower work during generation.
Changes:
- Cache projected visual embeddings once per batch/request and pass them through the decode loop via
visual_embedsinstead of re-encoding per step. - Refactor the decode loop to use a preallocated token buffer and to drop finished rows from subsequent forwards (including compacting
frame_maskandvisual_embeds). - Add focused unit tests to pin: single visual-encode per request, correct forwarding of cached embeds, signature alignment with
VLMWrapper.forward, and correct behavior under batch compaction.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| examples/vlm-evaluation/adapter.py | Encodes visuals once and reuses cached embeddings during decoding; adds token buffer + active-row compaction in the batched decode loop. |
| examples/vlm-evaluation/tests/unit/test_adapter.py | Expands decode-loop tests to assert visual caching behavior, forbid pixel+cache dual passing, pin mock/real forward signature parity, and validate compaction correctness. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Encodes frames once per request via model.encode_visual and reuses the projected embeds each step instead of re-running the vision tower.
Testing
uv run ruff check kempnerforge/ tests/passesuv run ruff format --check kempnerforge/ tests/ scripts/passesuv run pyright kempnerforge/passes (0 errors)uv run pytest tests/unit/ -v --timeout=60passesCloses #126