Skip to content

test: stop start-method leak between tests and run the whole suite under xdist - #968

Merged
tchaton merged 4 commits into
Lightning-AI:mainfrom
deependujha:test/faster-ci-612
Nov 17, 2026
Merged

tchaton merged 4 commits into
Lightning-AI:mainfrom
deependujha:test/faster-ci-612

Conversation

@deependujha

@deependujha deependujha commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

What does this PR do?

Fixes #612. Follow-ups are tracked in #969.

Five changes that cut test time:

1. Restore the multiprocessing start method after each test

optimize() / map() call multiprocessing.set_start_method("spawn", force=True), and that sticks for the rest of the process. Once any test in a pytest worker calls optimize(), every later test in that worker spawns its dataloader workers instead of forking them, and each spawned worker re-imports torch and litdata.

  • On Ubuntu (py3.12) CI, tests/streaming/test_parallel.py alone took ~848s of the ~1340s spent in the parallel step, on 20-item datasets.
  • Local repro: test_parallel_dataset_partial_iteration_resume[False-False-inf] takes 0.6s with forked workers, 70s when it runs after a test that calls optimize(), and 0.24s with the new autouse fixture.

This only helps where the platform default is fork (Linux, Python 3.10–3.13).

2. Fix test_optimize_with_queues_as_input

It built a default-context multiprocessing.Queue() and passed it to optimize(), which spawns its workers. It only passed while an earlier test had left the start method on spawn. The queue is now created from a spawn context.

3. Import lightning_sdk lazily

import litdata eagerly imported lightning_sdk just to re-export Machine, and every spawned dataloader / optimize worker paid for it. litdata.Machine is now resolved on first access via a module __getattr__; from litdata import Machine still works and Machine stays in __all__. Locally import litdata goes from ~1.2s to ~0.7s.

4. Cut the dataloader shutdown wait on macOS (tests only)

On macOS a dataloader worker that sends tensors starts torch_shm_manager, which inherits the pipe the parent uses to detect the worker's exit and outlives the worker. DataLoader._shutdown_workers then waits its full 5s join timeout per worker at the end of every epoch, although the worker has already exited. Plain torch.utils.data.DataLoader does the same (spawn, 2 workers, tensor batches: 10.6s per epoch vs 0.8s without tensors).

A macOS-only session fixture lowers torch's MP_STATUS_CHECK_INTERVAL to 0.5s in the test process. test_combined_dataset_dataloader_states_partial_iterations[4-15] goes from 64s to 9.9s. Trade-off: on macOS a worker that needs more than 0.5s to exit is terminated by torch instead of joined.

5. Run the whole suite in one xdist step with -n 4

After #952 the processing and raw tests no longer share cache dirs, so they don't need a separate sequential step.

CI job time, before and after

Job main this PR
Ubuntu 3.10–3.13 16–38 min 5–7.5 min
Ubuntu 3.14 ~21 min 9 min
macOS 20–30 min 8.5–18 min
Windows 6–10 min 5–6 min

Local (macOS, 6 cores, whole suite, -n 4, no coverage): 8:22 before the macOS shutdown fixture, 4:41 with it.

🤖 Generated with Claude Code

…der xdist

- restore the multiprocessing start method after each test: optimize()/map()
  force it to spawn for the rest of the process, so later multi-worker
  dataloader tests re-imported torch and litdata in every worker
- import lightning_sdk lazily so 'import litdata' (paid by every spawned
  worker) no longer loads it
- run tests/processing and tests/raw in the same xdist run, with -n 4

Refs Lightning-AI#612

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@codecov-commenter

codecov-commenter commented Nov 16, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82%. Comparing base (c6f075c) to head (be7cf11).
⚠️ Report is 5 commits behind head on main.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@          Coverage Diff          @@
##            main    #968   +/-   ##
=====================================
  Coverage     82%     82%           
=====================================
  Files         72      72           
  Lines      16445   16451    +6     
=====================================
+ Hits       13505   13513    +8     
+ Misses      2940    2938    -2     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

…ait on macOS

- test_optimize_with_queues_as_input built a default-context queue and passed
  it to optimize(), which spawns its workers; it only passed while an earlier
  test had left the start method on spawn. Create the queue from a spawn context.
- on macOS, torch_shm_manager keeps each dataloader worker's exit pipe open, so
  the parent waits torch's 5s join timeout per worker at shutdown. Lower
  MP_STATUS_CHECK_INTERVAL to 0.5s in the test process on macOS.

Refs Lightning-AI#612

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are recorded for the multiprocessing test-isolation and parallel CI changes.

No open issues 🎉

What changed in this PR

Speeds up testing by isolating multiprocessing state, reducing worker overhead, and running the full suite in parallel. This addresses #612 and builds on the cache isolation introduced in #952.

Changes:

  • Restore the multiprocessing start method after each test and use a spawn-compatible queue and producer.
  • Load litdata.Machine lazily and shorten dataloader shutdown waits on macOS during tests.
  • Consolidate CI into one four-worker xdist run and update testing guidance.
File Description
tests/​test_imports.py Adds regression tests for lazy SDK loading and the Machine export.
tests/​processing/​test_functions.py Creates the queue and producer from a spawn context.
tests/​conftest.py Adds start-method restoration and a macOS shutdown-timeout fixture.
src/​litdata/​__init__.py Resolves and caches Machine on first access.
.github/​workflows/​ci-testing.yml Runs the whole suite with four xdist workers.
.claude/​skills/​litdata/​reference/​testing.md Documents the unified CI command and new fixtures.

Review effort: Balanced

Comment thread tests/conftest.py Resolved
@tchaton
tchaton merged commit 253daea into Lightning-AI:main Nov 17, 2026
47 of 49 checks passed
@deependujha
deependujha deleted the test/faster-ci-612 branch November 17, 2026 14:24
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.

Explore Optimizing and Running Tests in Parallel for Faster CI in litdata

4 participants