Skip to content

perf(vits): cut per-batch preprocessing from seconds to a fraction - #17

Merged
danellecline merged 1 commit into
mainfrom
perf/batch-preprocessing
Aug 25, 2026
Merged

danellecline merged 1 commit into
mainfrom
perf/batch-preprocessing

Conversation

@danellecline

@danellecline danellecline commented Aug 25, 2026 •

Copy link
Copy Markdown
Member

"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.

"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
@danellecline
danellecline merged commit 130e80c into main Aug 25, 2026
2 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