Skip to content

Add a Lightning DDP inference script and SLURM batch scripts - #28

Open
rushin682 wants to merge 5 commits into
mainfrom
worktree-lightning-sharded-inference
Open

rushin682 wants to merge 5 commits into
mainfrom
worktree-lightning-sharded-inference

Conversation

@rushin682

@rushin682 rushin682 commented Oct 10, 2026 •

Copy link
Copy Markdown
Collaborator

What

scripts/run_inference_flash_ddp.py splits a single store across several GPUs with Lightning DDP. run_inference.py and run_inference_flash.py stay the simple one-store-per-process scripts from #24 (only a docstring changes), so the multi-GPU path is opt-in and the single-GPU path has no Lightning dependency.

  • --devices N / --num-nodes M, launched with one SLURM task per GPU (srun --ntasks-per-node=N).
  • Each rank predicts every N-th cell. Rank 0 gathers the rows, restores table order and writes the pred_table in the same format as run_inference_flash.py.
  • Each rank seeds with seed + rank, so ranks do not share noise.
  • Progress is a tqdm bar (Lightning's default Rich bar prints nothing to a log file until the store is done).
  • FlowPipeline.__call__ is split into sample_batch and denormalize so the Lightning path reuses the sampling code instead of copying it. __call__ is unchanged; a new test pins it to those two.

The DDP script is a copy of run_inference_flash.py with the Lightning parts added, rather than importing from it, so each script reads on its own.

SpatialDataset.native_mpp now reads source_he_mpp for stores whose spatialdata_io_reader is visium, as for he (both are on the H&E pixel grid with identity transforms). Before, a visium store fell through to the shape-scale fallback and got the wrong pixel size; a visium store without source_he_mpp now raises like the other readers.

slurm_scripts/ adds three self-contained sbatch scripts for 4-GPU dc-hwai nodes. Each writes one log per store, lists failed stores in failed.txt and exits non-zero if any failed.

Script Runs Stores
run_inference.sbatch run_inference.py one per GPU, 4 at a time; a finished store's GPU takes the next one (largest first)
run_inference_flash.sbatch run_inference_flash.py same
run_inference_flash_ddp.sbatch run_inference_flash_ddp.py --devices 4 SLURM array (default --array=0-3%4); stores sorted by size and dealt round-robin to the tasks, each task runs its stores one after another, each split over all 4 GPUs

The per-GPU scripts launch one srun step per store (1 GPU, 16 cores, 120G), so no bash helper is needed. The DDP script needs --gpu-bind=none so each task sees all four GPUs, and accepts a glob for SDATA_DIR. .gitignore now ignores only the local slurm_scripts/*.sh helpers.

Why

A store with ~800k cells takes more than 24 h on one GPU, past the QoS wall clock, and the per-store queue cannot help with a single slide.

Heads-up

  • With N > 1 the values are not bitwise equal to a 1-GPU run: each cell gets different noise and batches hold different cells. The difference is the size of a seed change (numbers below). With --devices 1 the output is bitwise identical to run_inference_flash.py.
  • The visium native_mpp fix changes predictions for visium stores (their patches are now cut at the correct pixel size). Outputs for xenium and he stores are unchanged.
  • In the DDP script a failed store aborts the process; the sbatch runs each store as its own srun step, so the store is logged in failed.txt and the job moves on. Stores that already have a pred_table are skipped, so a rerun resumes.
  • The sbatch scripts hardcode account=hai_1240, partition=dc-hwai and default REPO/VENV paths; override REPO/VENV with --export.

Tested

  • pytest tests (before the split): 47 passed, 2 failed (test_models.py: llama3_forward, mixer_autoencoder_roundtrip; CUDA errors on the GPU-less login node, not re-run on a GPU).

  • hatch test tests/test_inference.py tests/test_datasets.py after the split, CPU: 31 passed, including the visium cases.

  • prek hooks on the changed files: all pass.

  • H100, 243 cells, batch size 16, mean |Δ| (measured with the DDP code before it moved to its own file; the code is unchanged):

    Comparison mean |Δ|
    --devices 1 vs run_inference_flash.py 0 (bitwise, default and --fast)
    4 GPUs vs 1 GPU, seed 0 / seed 1 0.183 / 0.182
    seed 1 vs seed 0, 1 GPU 0.188

    Table format (dtype, shape, obs, var, obsm["spatial"], uns["spatialdata_attrs"]) is identical.

  • 56,510 cells, 4 × H100: 1838 s vs 7191 s on 1 GPU (3.9×). Format identical, mean |Δ| 0.153.

  • sbatch scripts, 4-8 small stores (210-243 cells) on one H100 node: with 8 stores both per-GPU scripts run 4 at a time and refill a GPU as each step ends; the DDP script runs its stores one after another on 4 GPUs. All jobs exit 0 and every store gets a pred_table.

  • Not yet run: the sbatch scripts since the split into run_inference_flash_ddp.py, the DDP sbatch as an array, and the failure path (failed.txt, non-zero exit).

🤖 Generated with Claude Code

## What
`run_inference_flash.py` can now shard each store over several GPUs with
Lightning DDP (`--devices N`, `--num-nodes M`; one SLURM task per GPU).
Each rank predicts every N-th cell; rank 0 gathers the rows, restores
table order and writes the `pred_table` with the existing code, so the
table format is the same for 1 and N GPUs. Each rank seeds with
`seed + rank`. The Rich progress bar is replaced by tqdm, which writes
lines to a log file.

`FlowPipeline.__call__` is split into `sample_batch` and `denormalize`
so the Lightning path reuses the sampling code. `__call__` itself is
unchanged. A new test pins `__call__` to those two.

## Why
A store with ~800k cells takes more than 24 h on one GPU, past the QoS
wall clock. The per-store queue cannot help with a single slide.

**With N > 1 values are not bitwise equal to 1 GPU**: each cell gets
different noise and batches contain different cells. The difference is
the size of a seed change (see below). With one device the output is
bitwise identical to the previous script. With several devices a failed
store aborts the job instead of being skipped.

## Tested
- `pytest tests`: 47 passed, 2 failed (`test_models.py`
  `llama3_forward`, `mixer_autoencoder_roundtrip`, CUDA errors on the
  GPU-less login node; not re-run on a GPU).
- `prek` hooks on the changed files: all pass.
- H100 node, 243-cell store, batch size 16: `--devices 1` is bitwise
  equal to the old script (default and `--fast`); 4 GPUs vs 1 GPU gives
  mean |d| 0.183 / 0.182 (seed 0 / 1), against 0.188 for seed 1 vs 0 on
  1 GPU; table format identical.
- 56,510-cell store, 4 x H100: 1838 s vs 7191 s on 1 GPU (3.9x), format
  identical, mean |d| 0.153.
@codecov-commenter

codecov-commenter commented Oct 10, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 48.25%. Comparing base (e96e260) to head (1a22cdc).

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #28      +/-   ##
==========================================
+ Coverage   48.20%   48.25%   +0.05%     
==========================================
  Files          10       10              
  Lines        1029     1030       +1     
==========================================
+ Hits          496      497       +1     
  Misses        533      533              
Files with missing lines Coverage Δ
src/phoenix/datasets/zarr_dataset.py 96.38% <100.00%> (ø)
src/phoenix/helpers/inference.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Three self-contained sbatch scripts in slurm_scripts/, each one job on a
4-GPU dc-hwai node, replacing the per-node bash helpers:

- run_inference.sbatch / run_inference_flash.sbatch: one store per GPU,
  4 at a time, one `srun` step per store (1 GPU, 16 cores, 120G). When a
  step ends, the next store (largest first) takes its GPU.
- run_inference_flash_ddp.sbatch: stores run one after another, each
  split over all 4 GPUs with `--devices 4`.

All three write one log per store, list failed stores in failed.txt and
exit non-zero if any failed. A failed DDP store no longer aborts the job.
`.gitignore` now only ignores the local bash helpers; run_inference.py
docstrings point at the new location.

Tested on H100 with 4-8 small stores: 8 stores refill 4 GPUs in both
per-GPU scripts, DDP steps run sequentially on 4 GPUs, all exit 0. The
failure path (failed.txt, non-zero exit) was not exercised.
@rushin682 rushin682 changed the title Split one SpatialData store across GPUs in run_inference_flash.py Split one SpatialData store across GPUs in run_inference_flash.py, add SLURM scripts Oct 17, 2026
The progress-bar comment said the tqdm bar appends a line per update.
Under `srun` that is only true with `srun -u`: tqdm redraws one line with
'\r' and ends it when the bar closes, and srun holds output back until a
newline. Document that and how to read such a log.

Comment only; no code change.
…t the DDP sbatch

`SpatialDataset.native_mpp` now reads `source_he_mpp` for stores whose
`spatialdata_io_reader` is `visium`, as for `he`: both sit on the H&E
pixel grid with identity transforms. Previously a visium store fell
through to the shape-scale fallback and got the wrong pixel size. A
visium store without `source_he_mpp` raises, like the other readers.

`run_inference_flash_ddp.sbatch` is now a SLURM array (default
`--array=0-3%4`). Stores are sorted by size and dealt round-robin to the
tasks, and SDATA_DIR may be a glob.

Tests: added a visium case to test_datasets.py (pixel size and the
missing-attribute error). Not run before committing.
…erence_flash.py

The Lightning DDP version of run_inference_flash.py now lives in
scripts/run_inference_flash_ddp.py, and run_inference_flash.py is back
to the main version (#24) apart from a docstring fix. The one-store-
per-GPU path stays a plain script without Lightning, which is easier to
read, and DDP is opt-in for slides too large for one GPU. The code is
copied, not shared; only the docstring, logger name and CLI description
of the DDP script changed. run_inference_flash_ddp.sbatch calls the new
script.

The docstring of run_inference_flash.py pointed at the removed
inference_node.sh; it now points at slurm_scripts/run_inference_flash.sbatch
and the DDP script.

Tested: prek hooks pass; `hatch test tests/test_inference.py
tests/test_datasets.py` 31 passed on CPU (login node), including the
visium cases from the previous commit. Neither sbatch has been rerun on
a GPU node since the split; run_inference_flash.sbatch now drives the
Rich progress bar of the main script, whose output under `srun -u` into
a log file has not been checked.
@rushin682 rushin682 changed the title Split one SpatialData store across GPUs in run_inference_flash.py, add SLURM scripts Add a Lightning DDP inference script and SLURM batch scripts Oct 30, 2026
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.

2 participants