Repository navigation
perf(vits): cut per-batch preprocessing from seconds to a fraction - #17
Merged
Merged
Conversation
"Preprocessing 128 images" took 10-13s per batch, all of it on the CPU with the GPU idle. Three causes, all addressed here. 1. The image processor was the slow one. AutoImageProcessor.from_pretrained() with `use_fast` unset resolves to the slow processor unless the checkpoint was saved with a fast one -- in transformers 4.57 only Qwen2VL is force-upgraded (FORCE_FAST_IMAGE_PROCESSOR). The slow path resizes, rescales and normalizes one image at a time in Python/NumPy. Request the torchvision-backed fast processor instead, and log which class was actually loaded so this is visible at startup. Fast output differs very slightly (torchvision resize vs PIL), so VSS_FAST_PROCESSOR=0 restores the old behaviour for a Redis index built with the slow processor. 2. Images were decoded one at a time. PIL releases the GIL inside load(), so the batch now decodes on a thread pool (VSS_DECODE_WORKERS, default min(8, cpu)). Measured 2.8x on a 2-core box for 128 1024px crops; more with more cores. 3. Every uploaded image was written to a temp file so PIL could read it straight back off disk, despite the bytes already being in memory from the HTTP upload. Pass in-memory buffers instead, which also removes the temp-file cleanup path entirely. preprocess_images() now takes paths or in-memory buffers, plus optional labels used for logging and the failed-image report. Passing a list of paths behaves exactly as before, so predict_from_list.py and tests/load.py are unaffected. pool.map preserves order, which matters because embeddings are matched back to their inputs positionally. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019s65VkcHwERBZrXXpaivVy
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.
"Preprocessing 128 images" took 10-13s per batch, all of it on the CPU with the GPU idle. Three causes, all addressed here.
The image processor was the slow one. AutoImageProcessor.from_pretrained() with
use_fastunset resolves to the slow processor unless the checkpoint was saved with a fast one -- in transformers 4.57 only Qwen2VL is force-upgraded (FORCE_FAST_IMAGE_PROCESSOR). The slow path resizes, rescales and normalizes one image at a time in Python/NumPy. Request the torchvision-backed fast processor instead, and log which class was actually loaded so this is visible at startup. Fast output differs very slightly (torchvision resize vs PIL), so VSS_FAST_PROCESSOR=0 restores the old behaviour for a Redis index built with the slow processor.Images were decoded one at a time. PIL releases the GIL inside load(), so the batch now decodes on a thread pool (VSS_DECODE_WORKERS, default min(8, cpu)). Measured 2.8x on a 2-core box for 128 1024px crops; more with more cores.
Every uploaded image was written to a temp file so PIL could read it straight back off disk, despite the bytes already being in memory from the HTTP upload. Pass in-memory buffers instead, which also removes the temp-file cleanup path entirely.
preprocess_images() now takes paths or in-memory buffers, plus optional labels used for logging and the failed-image report. Passing a list of paths behaves exactly as before, so predict_from_list.py and tests/load.py are unaffected. pool.map preserves order, which matters because embeddings are matched back to their inputs positionally.