Skip to content

Studio: fetch ShareGPT+image row images concurrently, in row order - #15786

Merged
danielhanchen merged 4 commits into
mainfrom
perf/sharegpt-image-parallel
Oct 29, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
perf/sharegpt-image-parallel

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

What changes

convert_sharegpt_with_images_to_vlm_format resolved every row image one at a time, so a ShareGPT+image dataset whose image column holds URLs or bare repo filenames (one hf_hub_download per row) waited on the network once per row.

When the first row's image needs the network (an http(s):// URL, or a bare filename found in the repo lookup), rows are now converted on a thread pool of min(safe_thread_num_proc(), 8) workers. 8 is the default of huggingface_hub's own snapshot_download, and lower than the uncapped safe_thread_num_proc() count convert_to_vlm_format already uses for URL rows, so a many core host does not open dozens of connections to one host.

  • Results are settled strictly in row order, so the converted list, the skipped count and the "First conversion failure" log line (the earliest failing row, same text) match a serial pass.
  • At most 2 * workers rows are in flight or waiting their turn, which bounds the decoded images held in memory.
  • Each task runs under a fresh copy of the caller's context (contextvars): the account context var is not inherited by new threads, so this keeps the fetch acting for the caller's account and HF cache settings.
  • Every other dataset (local paths, embedded or decoded images, or a host where the worker count is 1) takes the same serial loop as before and starts no threads.
  • An error that is not an Exception still propagates; pending fetches are cancelled on the way out.
  • The fetch helpers, their retries and SSRF guard are unchanged.

Results

Fake fetch with 50 ms latency, 200 URL rows, 6 failing, 8 workers, fresh process per run, arms interleaved, 6 runs each:

Arm Median Range Peak concurrent fetches Rows Output digest
main 10.136 s 10.126 to 10.393 1 194 9acb9ce6f67c512c
this PR 1.313 s 1.297 to 1.418 8 194 9acb9ce6f67c512c

Real network, 48 image URLs from a public Hugging Face dataset through the real guarded fetcher (base, head, head, base): 17.8 s / 2.4 s / 3.1 s / 16.6 s, identical digest. The digest covers every converted row's text and image pixels.

Tests

studio/backend/tests/test_sharegpt_image_parallel_fetch.py:

  • URL rows are fetched concurrently and come back in row order. Fails on main (peak 1), passes here.
  • Bare repo filename rows download concurrently through hf_hub_download. Fails on main, passes here.
  • With 64 cores worth of workers available, at most 8 fetches run at once. Fails on main, passes here.
  • Failures skip their rows and the log names the earliest failing row even when a later row fails first; every row failing raises the same ValueError; fetches see the caller's context variables; mixed local and URL rows keep row order; a local only dataset creates no threads; a row missing the image key is skipped, not raised. Pass on both.

tests/test_vlm_conversion_row_urls.py passes on both arms. Staging CI on ubuntu-latest, macos-15 and windows-latest is green.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review 🔄 Running since 2026-10-29T10:08:58.232791Z 6675508 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Something went wrong. Try again later by commenting “@codex review”.

Provided git ref 7f00c785fcdd19854ebd2087a371cbb530738a94 does not exist
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 7f00c785fc

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

danielhanchen added a commit to Etherll/unsloth that referenced this pull request Oct 29, 2026
…hai#15870)

## What changes

For a vision dataset whose image column holds URLs and has more than 200
rows, `convert_to_vlm_format` first probes the first 200 URLs to
estimate throughput and the broken-link rate, discards the results, and
then the conversion loop downloads and decodes those same 200 images
again.

The probe now keeps each image it downloaded successfully, keyed by row,
and the first conversion batch uses it instead of fetching the URL
again. Everything else stays as it was:

- A URL whose probe download failed is not cached. The conversion
retries it exactly as before, so a transient failure in the probe can
still succeed.
- The rest of each row's conversion (instruction, caption,
`random.choice` for list captions) still runs in both passes, so the
number of draws from the global RNG is the same as before.
- Converted rows, their order, `failed_count`, the fail-rate gate, the
ETA text and the progress messages are all unchanged.
- Each cached image is popped when the first batch uses it. Main already
kept the 200 probe results (images included) alive through its `futures`
dict until the first batch started, and the finished list holds every
converted image anyway, so peak memory does not grow.
- One behaviour does differ: a URL whose content changes in the seconds
between the probe and the first batch is now converted from the probe's
download, where main used its second download. Either copy is a snapshot
of the same row taken seconds apart, and the row is still downloaded
through the same fetch path with the same checks.

## Results

I ran a local HTTP server that serves 64x64 JPEGs, returns 404 for every
11th row, and counts GET requests. Each arm ran 3 times.

| rows | GETs before | GETs after | converted | output sha256 (texts +
pixel bytes) | progress messages |
|---|---|---|---|---|---|
| 450 | 650 | 469 | 409 / 409 | identical | identical |
| 1200 | 1400 | 1219 | 1090 / 1090 | identical | identical |

After the change, the GET count is the row count plus the probe rows
that failed and get retried. Before the change it was the row count plus
200. Both counts were identical across all repeats. On loopback the wall
time is inside run-to-run noise. The saving shows up on real remote
URLs: about 200 downloads per dataset, or roughly 200 / (probe
throughput) seconds.

## Tests

- New `studio/backend/tests/test_vlm_probe_reuses_downloads.py` with 450
and 1200 rows, permanent and transient probe failures. It checks per-URL
fetch counts, that transiently failed probe rows still convert, and the
captions and pixels of every kept row. It fails before this change
(probe rows are fetched twice) and passes after it.
- The neighbouring VLM tests (`test_vlm_conversion_row_urls`,
`test_vlm_null_caption_rows`, `test_vlm_probe_untrusted_rows`,
`test_vlm_question_column`, `test_vlm_repo_relative_image_paths`,
`test_vlm_text_only_rows`) give 72 passed both before and after.
- `py_compile` passes on Python 3.9 and 3.10.

## Not covered

- I did not time it against real remote image hosts. The evidence is the
request count plus output parity.
- Datasets with 200 rows or fewer skip the probe and are unaffected.
- `convert_sharegpt_with_images_to_vlm_format` and
`convert_llava_to_vlm_format` have no URL probe, so there was nothing to
change there.
- Merges cleanly with the open unslothai#15786 (ShareGPT image rows fetched
concurrently) and unslothai#15787 (null image or caption rows found from Arrow
validity); with all three applied, their tests and this one pass
together (26 passed).
@danielhanchen
danielhanchen marked this pull request as ready for review October 29, 2026 10:08
@danielhanchen
danielhanchen merged commit f6f13c0 into main Oct 29, 2026
9 of 37 checks passed
@danielhanchen
danielhanchen deleted the perf/sharegpt-image-parallel branch October 29, 2026 10:08
danielhanchen added a commit that referenced this pull request Oct 29, 2026
#15992)

## Summary


`studio/backend/tests/test_sharegpt_image_parallel_fetch.py::test_local_only_dataset_starts_no_threads`
(added in #15786) fails whenever it is the first test in its process to
create a `tqdm` bar, and passes otherwise. Seen on the `(Python 3.13,
rest)` backend shard for #15934, which does not touch this code:

```
tests/test_sharegpt_image_parallel_fetch.py:173: in test_local_only_dataset_starts_no_threads
E   assert [1] == []
```

Run on its own it fails every time on main (`pytest
tests/test_sharegpt_image_parallel_fetch.py::test_local_only_dataset_starts_no_threads`).
The one thread is tqdm's `TMonitor`: `tqdm.__new__` starts it for the
first bar in a process (unless `monitor_interval` is 0), and the
conversion's progress bar is that first bar when nothing earlier in the
xdist worker made one. The product does what the test means to check (a
local-only dataset takes the serial path and starts no fetch workers);
the test counted a process-global helper as a worker.

## What changed

The test sets `tqdm.std.tqdm.monitor_interval = 0` through `monkeypatch`
before counting, so the process-wide `Thread.__init__` count and the
`active_count()` check stay as strict as before and only the monitor is
out of the picture.

## Verification

- Base (main): the test alone fails with `assert [1] == []`.
- Head: alone 1 passed; whole file 9 passed.
- With the serial branch in `convert_sharegpt_with_images_to_vlm_format`
forced onto the thread pool (`if num_workers <= 1:` changed to `if
False:`), the test fails again with `assert [1] == []`, so it still
catches a local-only dataset starting fetch workers.
danielhanchen added a commit to Imagineer99/unsloth that referenced this pull request Nov 8, 2026
…in order (unslothai#17029)

In image + text datasets, the image column sometimes holds bare file
names (`0001.jpg`, `images/0001.jpg`) that point at files in the dataset
repo. `convert_to_vlm_format` resolved those with one `hf_hub_download`
per row, one row at a time, before training could start. Each call is at
least one Hub request (a HEAD even when the file is cached, since no
revision is pinned), so 10k rows meant 10k sequential round trips.
unslothai#15786 already moved the ShareGPT + image converter onto an in-order
pool for the same case. This converter's image + text path was still
serial.

## Change

For rows whose image is a repo file name, the download and decode run on
a thread pool: `min(safe_thread_num_proc(), 8)` workers, the same cap as
the ShareGPT converter. Rows are still settled in row order on the
calling thread, so the converted list, the `random.choice` pick for list
captions, the failure count and the first-failure log line are the same
as before. At most 2x workers images are held while waiting. Each task
runs in a copy of the caller's context, so the fetch sees the same
account and HF cache settings. Rows with local paths, URLs or embedded
images are unchanged, and URL datasets keep their own probe-and-batch
path.

Both converters now share one `_settle_in_order` helper (bounded
in-order pool, context copy per task, queued tasks cancelled on
interrupt), replacing the ShareGPT converter's inline copy. Net change
in `format_conversion.py`: about 40 lines added, 60 removed.

## Results (200 rows, fake Hub with 100 ms per download, 3 alternating
runs per side)

| | main | this branch |
| --- | --- | --- |
| `convert_to_vlm_format` | 20.31 to 20.48 s | 2.58 to 2.67 s |
| converted rows / output digest | 199 / same | 199 / same |
| fetches issued before a Ctrl-C on row 20 stops it | 21 | 31 (queued
ones cancelled, no leftover threads) |

The gain scales with the per-request latency and the worker count (8
here).

## Tests

- New `tests/test_vlm_repo_image_fetch_pool.py`. 48 rows that mix
repo-relative and bare names, list captions picked with a fixed seed,
and one download that fails: the pooled output equals the serial output,
in the same order with the same captions. The same files are fetched,
and more than one fetch runs at a time. It fails on main, because
fetches run one at a time there.
- `tests/test_vlm_*.py` + `tests/test_sharegpt_*.py`: 119 passed
(ShareGPT pool tests unchanged and green on the shared helper).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant