Repository navigation
perf(vits): cut per-batch preprocessing from seconds to a fraction - #16
Merged
Merged
Conversation
…ime (#15) Clients embedding a dataset through /embed + /ws/predict/job saw bursts of inter-frame timeouts as soon as more than a couple of batches were in flight. Two defects on this side contributed. 1. Blocking Redis calls ran inline on the event loop. Job.exists, Job.fetch and job.return_value() are synchronous redis-py calls, and ws_job_result is an async endpoint sharing one event loop with every other connection this process serves. Under N concurrent job sockets each polling at 1/WS_POLL_INTERVAL, plus concurrent multipart uploads, the loop spent its time inside Redis round-trips and unpickling embedding payloads -- delaying the very heartbeat frames clients use to distinguish a slow job from a dead connection, and so causing the timeouts it was meant to prevent. All three now go through asyncio.to_thread; redis-py clients are thread-safe (they hold a connection pool), as is unpickling a result. 2. WS_MAX_WAIT counted loop iterations, not elapsed time. `elapsed += WS_POLL_INTERVAL` measured only the sleeps and ignored how long each Redis round-trip took, so the effective limit drifted arbitrarily far past WS_MAX_WAIT under load -- exactly when a real bound matters. It is now a time.monotonic() deadline, and the timeout frame reports how long it actually waited. WS_MAX_WAIT and WS_POLL_INTERVAL are now environment-configurable, and the default wait is raised from 300s to 1800s: jobs are processed by a single serial RQ worker per project, so a job submitted while several others are queued ahead of it legitimately waits for all of them, and 300s cut clients off mid-job. A failed job now names itself in the message instead of sending a bare {"status": "failed"}. Note this does not raise throughput -- one worker per project still processes jobs serially, so client-side concurrency only queues them faster. Scaling the rq-worker service is the lever for that. Claude-Session: https://claude.ai/code/session_019s65VkcHwERBZrXXpaivVy Co-authored-by: Claude <noreply@anthropic.com>
"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
Use the MBARI mldevops account (uid 12078, gid 20) instead of a generic appuser, fix ownership of app/venv/cache paths, and expose GIT_VERSION as org.opencontainers.image.version for release tagging. Keeps huggingface_hub==0.36.2 so transformers 4.57.6/DINOv3 loads cleanly. Fixes #12 Co-authored-by: Cursor <cursoragent@cursor.com>
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.
What
Preprocessing 128 imagestook 10–13s per batch, entirely on the CPU with the GPU idle.Three independent causes:
AutoImageProcessor.from_pretrained()withuse_fastunset resolves to the slow processor unless the checkpoint was saved with afast one — in transformers 4.57 only
Qwen2VLImageProcessoris force-upgraded(
FORCE_FAST_IMAGE_PROCESSOR). The slow path resizes, rescales and normalizes one imageat a time in Python/NumPy.
Image.open/load/convertat a time.off disk, despite the bytes already being in memory from the HTTP request.
Changes
visible at startup.
VSS_FAST_PROCESSOR=0restores the old behaviour.VSS_DECODE_WORKERS, defaultmin(8, cpu_count)).PIL releases the GIL inside
load(), so this scales with cores.preprocess_images()now accepts paths or buffers, plus optional labels used forlogging and the failed-image report.
Measured
128 × 1024px crops, 2 cores — decode only, excluding the processor change:
More cores will do better. The
use_fastchange is on top of this and should be the largerwin.