Skip to content

Studio: reuse image header dimensions in the dataset labeling listing while the file is unchanged - #16366

Merged
danielhanchen merged 4 commits into
mainfrom
perf/r20-dataset-grid
Nov 1, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
perf/r20-dataset-grid

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 1, 2026 •

Copy link
Copy Markdown
Member

What changes

GET /api/train/diffusion/dataset/{name}/images serves the image dataset showcase on dataset selection, the labeling grid toggle, and every refresh after an upload. On every call it opened each image with Pillow to read the header dimensions. The listing now keeps those dimensions per path in a bounded LRU (8192 entries, like the gallery recipe-chunk cache). The key is the stat the listing already takes for size_bytes: (st_dev, st_ino, st_size, st_mtime_ns, st_ctime_ns). The header is read again as soon as any of these changes: a rewrite moves ctime even when mtime is restored, and a replace moves the inode.

Results (CPU, TestClient end to end, median of 15 requests, 3 runs each, warm page cache)

images in dataset base ms (3 runs) head ms (3 runs) change
20 6.9 / 6.6 / 6.7 5.5 / 6.7 / 5.5 within noise
100 13.7 / 14.3 / 12.8 8.5 / 9.0 / 8.4 -35%
200 21.8 / 21.6 / 21.0 14.1 / 13.7 / 13.9 -35%
1000 82.0 / 87.6 / 77.4 50.1 / 43.3 / 45.8 -44%
3000 229 / 228 / 229 136 / 121 / 137 -42%

The first listing in a process still reads every header and takes as long as on base (20 images: 10-12 ms on both; 200: 19-24 ms on both; 3000: 240-294 ms on both). Fixture: 512x512 PNG and JPEG files, half of them with .txt sidecars, and one metadata.jsonl row per image.

Tests

  • New studio/backend/tests/test_diffusion_dataset_image_size_cache.py:

    • unchanged files are not reopened;
    • a same-size rewrite with the old mtime restored is read again;
    • a file swapped in with os.replace is read again;
    • unreadable files are retried on every listing and never kept;
    • clips never open Pillow;
    • the LRU bound holds;
    • a failed stat reads the header uncached.
  • Base vs head differential over 300 seeded datasets. The fixtures cover:

    • PNG, JPEG, WebP, BMP and GIF, with mixed-case extensions;
    • truncated and garbage images, fake clips;
    • .txt, .caption and .TXT sidecars, including empty, whitespace-only and invalid UTF-8;
    • AppleDouble files and a directory named dir.png;
    • metadata and captions jsonl with alias keys, duplicates, null, non-string and malformed rows.

    Each seed ran 6 mutation steps (rewrite, rewrite with the old mtime, replace with the old mtime, delete, add, caption PUT, metadata rewrite) and compared the images listing and /diffusion/info after every step. All 4978 responses were byte identical, with 16527 cache hits. As a control, a cache keyed on path only, ignoring the stat key, gives 948 differing responses.

  • test_diffusion_dataset_api.py, test_diffusion_dataset_clips.py, test_dataset_upload_limits.py, test_diffusion_train*.py and test_diffusion_training*.py: 369 passed and 4 skipped. The same 3 tests fail on both base and head:

    • test_diffusion_train_perf.py::test_gather_sigmas_matches_search_based_gather
    • test_diffusion_train_speed.py::test_checkpoint_modes_match_uncheckpointed_grads[plain]
    • test_diffusion_train_speed.py::test_checkpoint_modes_match_uncheckpointed_grads[partial]
  • The suites that read routes/training.py as source give the same result on both (725 passed).

  • tests/studio -k "dataset or caption or diffusion_train or labeling": 5 passed, 1 skipped.

Not covered

  • Only measured on Linux ext4. On Windows, st_ctime is the creation time, so a same-size rewrite that also restores the old mtime would not be noticed there. The gallery listing cache makes the same tradeoff. The test for that case is skipped on Windows; a normal in-place save, which moves the mtime, is reread on every OS (test_in_place_rewrite_is_reread).
  • The /diffusion/info dataset summaries and the metadata reread on caption save are unchanged. At realistic dataset sizes both are below the bar.

@danielhanchen

Copy link
Copy Markdown
Member Author

On the Windows xplat staging leg for this PR (danielhanchen#3687), the new test_rewrite_with_the_old_mtime_is_reread fails:

E   AssertionError: assert {'a.png': (8, 6)} == {'a.png': (6, 8)}

This looks like a real gap in the cache key on Windows, not a test problem. The key is (st_dev, st_ino, st_size, st_mtime_ns, st_ctime_ns), and its comment relies on a write moving ctime. On Windows, st_ctime is the file's creation time, so an in-place rewrite that keeps the size and has its mtime restored, which is exactly what the test does, produces an identical key, and the old dimensions are served. On Linux and macOS, ctime is the inode change time, so it passes there.

Two ways to close it: on Windows, fall back to rereading the header whenever the key cannot prove the file is unchanged, or skip the cache entirely on os.name == "nt". Alternatively, add a cheap content signal to the key, such as the first few KB of the file, which contain the PNG/JPEG header the size comes from anyway. Either way, the test is right to expect a reread.

@danielhanchen
danielhanchen marked this pull request as ready for review November 1, 2026 09:15
@danielhanchen
danielhanchen merged commit 6f0e0ae into main Nov 1, 2026
@danielhanchen
danielhanchen deleted the perf/r20-dataset-grid branch November 1, 2026 09:15
@danielhanchen danielhanchen self-assigned this Nov 1, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 1, 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 ⚠️ Failed 2026-11-01T09:16:00.560147Z
We couldn’t complete this review. Comment @codex review to try again.
95e1f96 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: Didn't find any major issues. Bravo.

Reviewed commit: accf639399

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

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.

1 participant