Skip to content

feat(sn-da-image-caption): parallelize caption_batch via --concurrency - #98

Open
gangbangxie wants to merge 1 commit into
OpenSenseNova:mainfrom
gangbangxie:feat/caption-batch-concurrency
Open

feat(sn-da-image-caption): parallelize caption_batch via --concurrency#98
gangbangxie wants to merge 1 commit into
OpenSenseNova:mainfrom
gangbangxie:feat/caption-batch-concurrency

Conversation

@gangbangxie

Copy link
Copy Markdown

Problem

caption_batch() in skills/sn-da-image-caption/scripts/caption.py loops through a directory one image at a time:

for i, img_path in enumerate(image_files, 1):
    result = caption_image(img_path, ...)

Meanwhile the sister script skills/sn-ppt-entry/scripts/caption_images.py already parallelizes its VLM calls with ThreadPoolExecutor. For a directory of 50 images at ~2-4s each, the standalone path costs 100-200s end-to-end — which is what most people hit when running large batches.

Issue: #92.

Change

  • New --concurrency / -j flag, default 1, clamped to 1-16 (matches the bounds used in caption_images.py).
  • When concurrency > 1, calls go through a ThreadPoolExecutor and results are written back into a pre-sized list by index, so the returned order matches the sorted file order regardless of completion order.
  • When concurrency == 1 (default), behavior is byte-for-byte the same as before: same loop, same caption_image calls. The serial output line still prints index + filename + status.
  • caption_image() is already thread-safe — each invocation builds its own OpenAI client, the cache writes are keyed by <md5>_<prompt_hash>.txt so different threads write to different files, and os.makedirs(..., exist_ok=True) is race-safe.

Diff is +33/-8 in one file.

Testing

Local smoke test against a mocked caption_image that sleeps 50-200ms per call:

  • 8 simulated images, -j 1: ~0.80s, original output style preserved
  • 8 simulated images, -j 4: ~0.33s (≈2.4× speedup)
  • Returned list has identical order in both modes
  • -j 0 and -j 999 clamp safely to 1 and 16
  • Empty directory returns []
  • python3 caption.py --help still parses cleanly

Happy to adjust the default (e.g. raise to 4 to match caption_images.py) if you'd prefer — left at 1 so this is a pure opt-in change.

Closes #92

…oning

caption_batch() currently processes images one at a time, while the
sister caption_images.py in sn-ppt-entry already uses ThreadPoolExecutor.
Add a --concurrency / -j flag (default 1, clamped to 1-16) that runs
caption calls in parallel when set above 1; result order is preserved
regardless of completion order.

Closes OpenSenseNova#92
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.

caption_batch() has no concurrency — caption_images.py solved this, standalone script didn't

1 participant