Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

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

Daniel Hanf6f13c006f98
diff --git a/studio/backend/tests/test_sharegpt_image_parallel_fetch.py b/studio/backend/tests/test_sharegpt_image_parallel_fetch.py
new file mode 100644
index 00000000000..acfef41451d
--- /dev/null
+++ b/studio/backend/tests/test_sharegpt_image_parallel_fetch.py
@@ -0,0 +1,221 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""ShareGPT+image conversion fetches row images concurrently but keeps row order, failures and caller context."""
+
+import contextvars
+import logging
+import sys
+import threading
+import time
+from pathlib import Path
+
+import pytest
+from datasets import Dataset
+from PIL import Image
+
+_BACKEND_ROOT = Path(__file__).resolve().parents[1]
+if str(_BACKEND_ROOT) not in sys.path:
+ sys.path.insert(0, str(_BACKEND_ROOT))
+
+import utils.hardware # noqa: E402
+from utils.datasets import format_conversion # noqa: E402
+from utils.datasets.format_conversion import convert_sharegpt_with_images_to_vlm_format # noqa: E402
+
+_CALLER = contextvars.ContextVar("caller", default = None)
+
+
+class _SlowFetch:
+ def __init__(
+ self,
+ fail_rows = (),
+ delay = 0.02,
+ ):
+ self.fail_rows = set(fail_rows)
+ self.delay = delay
+ self.lock = threading.Lock()
+ self.active = 0
+ self.peak = 0
+ self.seen_context = []
+
+ def __call__(self, url):
+ row = int(url.rsplit("/", 1)[1].split(".")[0])
+ with self.lock:
+ self.active += 1
+ self.peak = max(self.peak, self.active)
+ self.seen_context.append(_CALLER.get())
+ try:
+ time.sleep(self.delay)
+ if row in self.fail_rows:
+ raise OSError(f"cannot fetch row {row}")
+ return Image.new("RGB", (row + 1, 1))
+ finally:
+ with self.lock:
+ self.active -= 1
+
+
+def _dataset(
+ n,
+ local_rows = (),
+ tmp_path = None,
+):
+ rows = []
+ for i in range(n):
+ image = f"https://example.com/{i}.png"
+ if i in local_rows:
+ path = tmp_path / f"{i}.png"
+ Image.new("RGB", (i + 1, 1)).save(path)
+ image = str(path)
+ rows.append(
+ {
+ "image": image,
+ "conversations": [
+ {"from": "human", "value": f"<image> row {i}"},
+ {"from": "gpt", "value": f"answer {i}"},
+ ],
+ }
+ )
+ return Dataset.from_list(rows)
+
+
+def workers(monkeypatch):
+ monkeypatch.setattr(utils.hardware, "safe_thread_num_proc", lambda desired = None: 4)
+
+
+def _widths(converted):
+ return [m["messages"][0]["content"][0]["image"].size[0] - 1 for m in converted]
+
+
+def test_url_rows_are_fetched_concurrently_in_row_order(monkeypatch, workers):
+ fetch = _SlowFetch()
+ monkeypatch.setattr(format_conversion, "_fetch_row_image", fetch)
+
+ converted = convert_sharegpt_with_images_to_vlm_format(_dataset(24))
+
+ assert _widths(converted) == list(range(24))
+ assert [m["messages"][0]["content"][1]["text"] for m in converted] == [
+ f"row {i}" for i in range(24)
+ ]
+ assert 1 < fetch.peak <= 4
+
+
+def test_failures_skip_rows_and_log_the_earliest_failing_row(monkeypatch, workers, caplog):
+ # Row 9 fails fast while row 3 is still sleeping; the log must still name row 3, as a serial pass would.
+ class _Fetch(_SlowFetch):
+ def __call__(self, url):
+ row = int(url.rsplit("/", 1)[1].split(".")[0])
+ self.delay = 0.2 if row == 3 else 0.0
+ return super().__call__(url)
+
+ fetch = _Fetch(fail_rows = (3, 9))
+ monkeypatch.setattr(format_conversion, "_fetch_row_image", fetch)
+ logs = []
+ monkeypatch.setattr(
+ format_conversion.logger, "info", lambda msg, *a, **k: logs.append(str(msg))
+ )
+
+ converted = convert_sharegpt_with_images_to_vlm_format(_dataset(16))
+
+ assert _widths(converted) == [i for i in range(16) if i not in (3, 9)]
+ first = [m for m in logs if "First conversion failure" in m]
+ assert first == ["⚠️ First conversion failure: OSError: cannot fetch row 3"]
+ assert any("Skipped 2/16" in m for m in logs)
+
+
+def test_every_row_failing_raises_the_same_error(monkeypatch, workers):
+ monkeypatch.setattr(format_conversion, "_fetch_row_image", _SlowFetch(fail_rows = range(8)))
+
+ with pytest.raises(ValueError, match = "All 8 samples failed during ShareGPT\\+image conversion"):
+ convert_sharegpt_with_images_to_vlm_format(_dataset(8))
+
+
+def test_fetches_run_under_the_callers_context(monkeypatch, workers):
+ fetch = _SlowFetch()
+ monkeypatch.setattr(format_conversion, "_fetch_row_image", fetch)
+ token = _CALLER.set("account-a")
+ try:
+ convert_sharegpt_with_images_to_vlm_format(_dataset(12))
+ finally:
+ _CALLER.reset(token)
+
+ assert fetch.seen_context == ["account-a"] * 12
+
+
+def test_mixed_local_and_url_rows_keep_row_order(monkeypatch, workers, tmp_path):
+ fetch = _SlowFetch()
+ monkeypatch.setattr(format_conversion, "_fetch_row_image", fetch)
+ monkeypatch.setattr(format_conversion, "_own_image_path", lambda path, account: path)
+ local = {1, 2, 5, 11}
+
+ converted = convert_sharegpt_with_images_to_vlm_format(_dataset(12, local, tmp_path))
+
+ assert _widths(converted) == list(range(12))
+ assert len(fetch.seen_context) == 12 - len(local)
+
+
+def test_local_only_dataset_starts_no_threads(monkeypatch, workers, tmp_path):
+ monkeypatch.setattr(format_conversion, "_own_image_path", lambda path, account: path)
+ before = threading.active_count()
+ created = []
+ real_init = threading.Thread.__init__
+
+ def counting_init(self, *a, **k):
+ created.append(1)
+ real_init(self, *a, **k)
+
+ monkeypatch.setattr(threading.Thread, "__init__", counting_init)
+ ds = _dataset(6, set(range(6)), tmp_path)
+
+ converted = convert_sharegpt_with_images_to_vlm_format(ds)
+
+ assert _widths(converted) == list(range(6))
+ assert created == []
+ assert threading.active_count() == before
+
+
+def test_concurrent_fetches_are_capped_on_many_core_hosts(monkeypatch):
+ monkeypatch.setattr(utils.hardware, "safe_thread_num_proc", lambda desired = None: 64)
+ fetch = _SlowFetch(delay = 0.05)
+ monkeypatch.setattr(format_conversion, "_fetch_row_image", fetch)
+
+ converted = convert_sharegpt_with_images_to_vlm_format(_dataset(48))
+
+ assert _widths(converted) == list(range(48))
+ assert 1 < fetch.peak <= format_conversion._MAX_ROW_FETCH_WORKERS == 8
+
+
+def test_a_row_without_the_image_key_is_skipped_as_before(monkeypatch, workers):
+ monkeypatch.setattr(format_conversion, "_fetch_row_image", _SlowFetch())
+ rows = list(_dataset(6))
+ del rows[2]["image"]
+
+ converted = convert_sharegpt_with_images_to_vlm_format(rows)
+
+ assert _widths(converted) == [0, 1, 3, 4, 5]
+
+
+def test_repo_filename_rows_download_concurrently(monkeypatch, workers, tmp_path):
+ import huggingface_hub
+
+ files = [f"images/{i}.png" for i in range(12)]
+ for i in range(12):
+ Image.new("RGB", (i + 1, 1)).save(tmp_path / f"{i}.png")
+ monkeypatch.setattr(huggingface_hub.HfApi, "list_repo_files", lambda self, *a, **k: files)
+ fetch = _SlowFetch()
+
+ def fake_download(repo, filename, **kwargs):
+ name = filename.rsplit("/", 1)[1]
+ fetch(f"https://hub/{name}")
+ return str(tmp_path / name)
+
+ monkeypatch.setattr(huggingface_hub, "hf_hub_download", fake_download)
+ rows = [
+ {"image": f"{i}.png", "conversations": [{"from": "human", "value": f"<image> row {i}"}]}
+ for i in range(12)
+ ]
+
+ converted = convert_sharegpt_with_images_to_vlm_format(rows, dataset_name = "org/ds")
+
+ assert _widths(converted) == list(range(12))
+ assert 1 < fetch.peak <= 4
diff --git a/studio/backend/utils/datasets/format_conversion.py b/studio/backend/utils/datasets/format_conversion.py
index 74e6b68b858..350cb0d8e8b 100644
--- a/studio/backend/utils/datasets/format_conversion.py
+++ b/studio/backend/utils/datasets/format_conversion.py
@@ -3,6 +3,7 @@
"""Dataset format conversion between Alpaca, ShareGPT, and ChatML."""
+import contextvars
import os
from .cells import cell_text
@@ -446,6 +447,10 @@ def _format_eta(seconds):
return f"{h}h {m}m"
+# huggingface_hub snapshot_download's default; caps parallel requests to one image host or the Hub.
+_MAX_ROW_FETCH_WORKERS = 8
+
+
def _fetch_row_image(url):
from io import BytesIO
@@ -946,6 +951,28 @@ def convert_sharegpt_with_images_to_vlm_format(
converted_list = []
failed_count = 0
+ def _record(future_or_sample):
+ nonlocal failed_count
+ try:
+ if isinstance(future_or_sample, Future):
+ converted_list.append(future_or_sample.result())
+ else:
+ converted_list.append(_convert_single_sample(future_or_sample))
+ except Exception as e:
+ failed_count += 1
+ if failed_count == 1:
+ logger.info(f"⚠️ First conversion failure: {type(e).__name__}: {e}")
+ pbar.set_postfix(ok = len(converted_list), failed = failed_count, refresh = False)
+
+ from concurrent.futures import Future
+
+ num_workers = 1
+ if _image_lookup is not None or (
+ isinstance(first_image, str) and first_image.startswith(("http://", "https://"))
+ ):
+ from utils.hardware import safe_thread_num_proc
+ num_workers = min(safe_thread_num_proc(), _MAX_ROW_FETCH_WORKERS)
+
pbar = tqdm(
dataset,
total = total,
@@ -953,15 +980,34 @@ def convert_sharegpt_with_images_to_vlm_format(
unit = "sample",
**_quiet_bar_kwargs(),
)
- for sample in pbar:
- try:
- converted_list.append(_convert_single_sample(sample))
- except Exception as e:
- failed_count += 1
- if failed_count == 1:
- logger.info(f"⚠️ First conversion failure: {type(e).__name__}: {e}")
- pbar.set_postfix(ok = len(converted_list), failed = failed_count, refresh = False)
- pbar.close()
+ try:
+ if num_workers <= 1:
+ for sample in pbar:
+ _record(sample)
+ else:
+ from collections import deque
+ from concurrent.futures import ThreadPoolExecutor
+
+ # Settled in row order (same output and first-failure log as serial); 2x workers bounds images held in memory.
+ pending = deque()
+ with ThreadPoolExecutor(max_workers = num_workers) as executor:
+ try:
+ for sample in pbar:
+ # A fresh context copy per task: the fetch sees the caller's account / HF cache settings.
+ pending.append(
+ executor.submit(
+ contextvars.copy_context().run, _convert_single_sample, sample
+ )
+ )
+ if len(pending) > 2 * num_workers:
+ _record(pending.popleft())
+ while pending:
+ _record(pending.popleft())
+ except BaseException:
+ executor.shutdown(wait = False, cancel_futures = True)
+ raise
+ finally:
+ pbar.close()
if failed_count > 0:
logger.info(f"⚠️ Skipped {failed_count}/{total} ({failed_count*100//total}%) samples")