Skip to content

SILo: size the localization pool for the largest trial, not the first - #51

Merged
KasparP merged 1 commit into
mainfrom
silo-memory-estimate
Nov 22, 2026
Merged

KasparP merged 1 commit into
mainfrom
silo-memory-estimate

Conversation

@KasparP

@KasparP KasparP commented Nov 22, 2026

Copy link
Copy Markdown
Collaborator

Problem

SILo localizes as many trials at once as fit in memory. It budgets 9× one channel of a trial's registered movie plus 1 GB per trial. It took that movie from the first valid trial of each imaging path, and the first trial of a SLAP2 recording is often partial.

Evidence from 23 multi-ROI SLAP2 sessions exported with GIAnT 9695d27 on a 246 GB machine:

  • 828409_2025-11-20. Trial 1 was only a few frames long. Localization found no valid pixels in it, and registration found 0 online-correction switches.
    • SILo estimated 1.2 GB per trial for Path 2 and 1.3 GB for Path 1.
    • With 6 workers, Path 2 peaked at 121 GB above baseline, so at least 20 GB per trial.
    • On Path 1, memory rose from 23 GB to over 200 GB within 5 minutes, and the run was killed (exit 137).
  • 828409_2025-11-11. Trial 1 had 1064 frames; the other trials had up to 2354.
    • The Path 1 estimate was 22.1 GB per trial; at least 33.7 GB was measured.
    • 6 workers peaked at 225 GB of 246 GB.
  • 796630_2025-09-26 (estimate 6.2 GB per trial) and 801381_2025-09-26 Path 1 (estimate 15.1 GB per trial) were killed during localization with 12 workers. Both passed with 6. Their trial lengths are not known: those runs left no export summary.

What dominates localization memory

I measured one 780125 trial with memory probes in a fresh MATLAB process. The trial is multi-ROI SLAP2, two channels, 80 Hz, 3.04 GiB per channel (E1T1DMD2).

The peak is 26.0 GB: 8.3× one channel plus the 0.75 GB process. It comes at the s.d. line of localizeSources_vIM (stdIM = sqrt(max(0,Vk.*IMb.*vIM)+Vb)). At that line the worker holds eight movie-sized single arrays of the activity channel, plus its NaN mask (¼ of a movie):

arrays what
2 IM and vIM (variance factors), still referenced by loadAndProcessTrialAsync
2 the masked copies localizeSources_vIM makes of both (copy-on-write, because the caller still holds them)
3 IMs (smoothed, never cleared), IMb (baseline), stdIM
1 a temporary of the s.d. expression

Loading both channels peaks at 13.1 GB (4.3×), and the DoG and peak-finding stages at 17.6 GB, both below the localization peak.

Memory scales with rows × cols × frames of the registered movie. For multi-ROI recordings the movie spans the bounding box of all ROIs plus 2×maxshift, and is NaN between ROIs. Memory does not depend on the number of ROIs or sources.

The 9× factor therefore holds: 8.3× on Windows, and the exports' Linux peaks give at least 5.7–6.7× (table below). The bug was which trial it was applied to.

Change

  • New localizationTrialBytes(mocoDr, fns, numChannels) sizes every kept trial's registered movie and returns 9× one channel of the largest, plus 1 GB.
    • h5 movies are sized from their dataspace, not the compressed file; tif movies from the file.
    • It costs one h5info call per trial.
  • SILo uses it for each path. It also reports which trial the pool was sized for, under the unchanged first line:
    Localizing up to <n> trials at once (<a> GB available, <b> GB per trial)
      sized for trial <t>, the largest of <k>: <c> GB per channel (smallest <d> GB)
    

Before / after, per trial

"After" applies the new sizing to the logged first-trial estimate, scaled by frames to the longest trial. With online compensation, every trial of a path has the same raster crop, so frames are the only difference.

"Measured" is the localization stage's total memory peak above the pre-localization baseline, divided by the number of workers. It is a lower bound on one worker's peak.

session, path first / longest trial (frames) before after measured (≥) trials at once, before → after
828409_2025-11-20 P2 partial, no valid pixels / ~2353 1.2 GB not computed (movies not available here) 20.2 GB 6 (user cap) → fewer
828409_2025-11-20 P1 partial / ~2353 1.3 GB not computed killed at >200 GB with 6 6 → fewer
828409_2025-11-11 P2 1064 / 2354 11.5 GB 24.2 GB 16.7 GB 6 → 6
828409_2025-11-11 P1 1064 / 2354 22.1 GB 47.7 GB 33.7 GB 6 → 4
828408_2025-11-19 P2 1923 / 2355 22.2 GB 27.0 GB 17.5 GB 6 → 6
828408_2025-11-19 P1 1923 / 2355 42.3 GB 51.6 GB 38.8 GB 5 → 4
780125 local, DMD2 all trials 3.04 GiB per channel 28.4 GB 28.4 GB 26.0 GB (one trial, Windows) unchanged

Pool check on 780125 (local, R2024b, Windows). Four trials of Path 2 were localized at once by 4 parfor workers:

  • Each worker's own peak was 26.04–26.06 GB, i.e. 8.3× one channel plus the 0.9 GB worker. The estimate is 28.3 GB.
  • The total over all MATLAB processes peaked at 93 GB, below the 104 GB sum of per-worker peaks, because the workers don't peak at the same moment.

So the "measured" totals from the exports understate a worker's peak by about 10%. On this evidence, Linux workers peak at roughly 6.5–7.5×, still under 9×.

Tests

runtests on TestLocalizationTrialBytes and TestSILo: 25 passed, 0 failed (R2024b, Windows).

  • TestLocalizationTrialBytes checks four things:
    • the largest trial is sized, not the first;
    • h5 movies are sized by data, not by the compressed file;
    • tif movies are sized by file;
    • a missing movie raises an error.
  • TestSILo/testSizesLocalizationForLargestTrial runs SILo with 2 workers on a dataset with a partial first trial. It uses the existing no-pool extractionPool double, so the sizing code runs but localization stays in the client. The test checks that SILo reports sizing for trial 2.

Not in this PR

Peak memory could drop from about 8.3× to about 5× one channel:

  • in loadAndProcessTrialAsync, pass IM and vIM to localizeSources_vIM without keeping references, so it can mask them in place;
  • clear IMs after varIM;
  • clear stdIM after use.

That would localize more trials at once, but it changes the localization code rather than the estimate, so it is left for a separate PR.

🤖 Generated with Claude Code

SILo budgeted 9x one channel of the first valid trial's registered movie
(plus 1 GB) per localizing worker. The first trial of a SLAP2 recording is
often partial: in multi-ROI exports it had 1064 of 2354 frames (828409
2025-11-11) or only a few frames (828409 2025-11-20, estimated at 1.2 and
1.3 GB per trial against >=20 GB measured), and sessions were killed for
memory during localization.

localizationTrialBytes sizes every kept trial's movie (h5 by its data, tif
by its file) and budgets for the largest. The 9x factor stays: one 780125
trial (3.04 GiB per channel) peaks at 26.0 GB, 8.3x plus the 0.75 GB
process, at the s.d. in localizeSources_vIM, where the worker holds eight
movie-sized arrays of the activity channel. SILo also prints which trial
the pool was sized for.

Co-Authored-By: Claude Opus 5.5 <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

There are no active review issues for the change to size localization workers using the largest kept trial.

No open issues 🎉

What changed in this PR

SILo now sizes its localization worker pool using the largest kept trial for each imaging path, rather than the first valid trial. This prevents a partial first trial from understating memory needs while preserving the existing 9× per-channel estimate plus 1 GB.

Changes:

  • Adds a helper that sizes all kept movies using HDF5 dataset dimensions or TIFF file sizes.
  • Updates SILo to use the largest estimate and report the trial used for sizing.
  • Adds unit and integration coverage for largest-trial selection, movie sizing, and missing-file errors.
File Description
tests/​unit/​source_extraction/​TestSILo.m Adds a regression test with a partial first trial and verifies the sizing report.
tests/​unit/​dependencies/​extraction/​TestLocalizationTrialBytes.m Tests largest-trial selection, HDF5 and TIFF sizing, and missing-file errors.
source_extraction/​SILo.m Uses the largest kept trial’s memory estimate and logs sizing details.
dependencies/​extraction/​activityImage/​localizationTrialBytes.m Adds the shared helper for estimating localization memory across trials.

Review effort: Balanced

@KasparP
KasparP merged commit ff9b327 into main Nov 22, 2026
2 checks passed
KasparP added a commit that referenced this pull request Nov 22, 2026
…e-memory

SILo localization: stop copying the movie; budget 6x per trial (stacked on #51)
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