Skip to content

SILo localization: stop copying the movie; budget 6x per trial (stacked on #51) - #54

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

KasparP merged 1 commit into
mainfrom
silo-localization-free-memory

Conversation

@KasparP

@KasparP KasparP commented Nov 22, 2026

Copy link
Copy Markdown
Collaborator

Stacked on #51 (base silo-memory-estimate). Merge #51 first, then retarget this to main.

Problem

#51 sizes SILo's localization pool for the largest trial, at 9× one channel of its registered movie plus 1 GB per trial. That matched what localization used: a 780125 trial peaked at 8.3× one channel. Most of that was copies localization doesn't need:

  • loadAndProcessTrialAsync kept references to the movie (IM) and its variance factors (vIM) while calling localizeSources_vIM. When localizeSources_vIM masked them, MATLAB copied both (copy-on-write). That is 2 movies.
  • IMs (the smoothed movie) stayed alive to the end, and stdIM was a new array next to the baseline IMb. That is 2 more.
  • X(mask) = nan with a movie-sized logical mask builds a double index array, 8 bytes per masked element. Multi-ROI movies are mostly NaN between the ROIs (92% on 780125 DMD2), so each such line briefly took about 1.85 movies more. Measured on a 1 GiB single movie with 92% masked: 1.85 GiB extra, against 0.02 GiB when masking a frame at a time.

Change

What is computed doesn't change, and the outputs are identical (see Evidence).

loadAndProcessTrialAsync

  • Hands the movie and variance factors to localizeSources_vIM through two nested functions, takeMovie and takeVarFactors. These clear the caller's references, so localizeSources_vIM masks and filters the arrays in place instead of copying them.
  • If localization fails, it loads the movie again for the existing retry with the image edges excluded. That costs extra reading only when localization fails. Before, the retry used the copy it kept.
  • No longer returns the movie as its first output; SILo discarded it (~).

localizeSources_vIM

  • Applies NaN and zero masks a frame at a time (setFrames), so no index array is built.
  • Computes the variance of the first 500 valid frames before the baseline, and clears IMs as soon as the baseline is made.
  • Computes the s.d. in the baseline's memory with the same operations in the same order, and clears vIM and the s.d. once used.
  • Keeps one frame's local maxima instead of a movie-sized logical array, and clears the filtered movie before the final normalization.
  • For raster imaging (no vIM), makes the ones single, like the movie, instead of double. They are all ones either way.

localizationTrialBytes

  • Budgets 6× one channel of the largest trial plus 1 GB (was 9×). Its help text gives the measured peaks.

Evidence

All runs are on 780125: multi-ROI SLAP2, 2 channels, 80 Hz, about 2250 frames per trial. Each trial ran loadAndProcessTrialAsync with #51 (7ae1ae0) and with this branch, each in a fresh MATLAB process (R2024b, Windows).

Two of the trials are h5 copies of E1T1DMD2, written the way MultiRoiRegistration writes them. They cover the h5 reading the exports use, and a short trial:

  • the whole trial;
  • its first 400 frames.

Same outputs

The outputs are meanIM, the activity image, aData, the peaks, the discarded frames and the half images. Compared with isequaln, they are identical for all 6 trials (41–77 peaks each).

Peak memory per trial

The table gives each process's peak working set, and in brackets the peak above the process's ~0.8 GB, as a multiple of one channel of the movie (GB = 2^30 bytes, as SILo prints):

trial one channel #51 this PR
E1T1DMD2 (TIF, 513×705 px) 3.04 GB 26.0 GB (8.31×) 13.9 GB (4.31×)
E1T10DMD2 (TIF) 3.04 GB 26.0 GB (8.31×) 13.9 GB (4.31×)
E1T1DMD1 (TIF, 320×845 px) 2.27 GB 19.7 GB (8.32×) 10.6 GB (4.33×)
E1T4DMD1 (TIF) 2.27 GB 19.7 GB (8.33×) 10.6 GB (4.32×)
E1T1DMD2 (h5) 3.04 GB 26.0 GB (8.29×) 13.8 GB (4.29×)
E1T1DMD2, first 400 frames (h5) 0.54 GB 5.5 GB (8.70×) 3.9 GB (5.69×)

Where the peak falls, from memory probes on E1T1DMD2:

  • Now: while the movie is smoothed and its baseline made. The worker then holds 4 movie-sized arrays (the movie, the variance factors, the smoothed movie and the baseline) plus the NaN mask.
  • Before: at the s.d., with 8 movie-sized arrays.
  • Loading both channels (13.1 GB, 4.0×) is unchanged.
  • Trials of 500 frames or fewer peak higher: the variance of the first 500 valid frames copies the whole smoothed movie, giving 5.7×.

6× covers that worst case and leaves about 40% margin for full-length trials. Run times didn't change: 189–222 s per TIF trial, with #51's and this branch's runs side by side.

Per-trial estimate and trials that fit

The per-channel sizes are from #51's export logs, for the longest trial. "At once" assumes 224 GB available; the exports logged 214–231 GB. SILo also caps the count at params.nWorkers and the pool size.

session, path one channel #51 this PR
828409_2025-11-11 P1 5.19 GB 47.7 GB, 4 at once 32.1 GB, 6 at once
828409_2025-11-11 P2 2.58 GB 24.2 GB, 9 16.5 GB, 13
828408_2025-11-19 P1 5.62 GB 51.6 GB, 4 34.7 GB, 6
828408_2025-11-19 P2 2.89 GB 27.0 GB, 8 18.3 GB, 12
780125 DMD2 3.04 GB 28.4 GB, 7 19.2 GB, 11

Not run on Linux. For #51's code, the Linux exports peaked at 5.7–6.7× or more (lower bounds from total memory), against 8.3× here, so Linux workers are not expected to need more than measured here.

Tests

  • New TestLoadAndProcessTrialAsync, with a recording fake of localizeSources_vIM, checks three things:
    • the localizer gets the activity channel, with discarded frames set to NaN, and the variance factors;
    • after a failure, the movie is loaded again and localization is retried with the edges excluded;
    • a second failure is raised.
  • TestLocalizationTrialBytes is updated for 6×.
  • Results: TestLoadAndProcessTrialAsync, TestLocalizationTrialBytes and TestSILo give 28 passed; all of tests/unit gives 169 passed, 0 failed (R2024b, Windows).

🤖 Generated with Claude Code

loadAndProcessTrialAsync hands the movie and variance factors to
localizeSources_vIM without keeping references (nested take functions),
so they are masked and filtered in place instead of copied. On a failed
localization the movie is loaded again for the retry with edges excluded.
The unused movie output is dropped.

localizeSources_vIM masks a frame at a time (a movie-sized logical index
builds a double index array, ~1.85 movies when most pixels are NaN), clears
the smoothed movie once the baseline is made, computes the s.d. in the
baseline's memory and clears arrays once used. Outputs are unchanged.

A 780125 trial now peaks at 4.3x one channel (was 8.3x); trials of up to
500 frames at 5.7x. localizationTrialBytes budgets 6x plus 1 GB (was 9x).

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.

🔵 Needs a closer look

The reduced worker memory budget relies on MATLAB behavior measured only on Windows R2024b, leaving Linux peak memory unvalidated and requiring human review or representative profiling before increasing concurrency.

No open issues 🎉

What changed in this PR

Building on #51’s largest-trial sizing, this PR reduces SILo localization’s memory use so more trials can run concurrently.

Changes:

  • Removes retained movie references, masks arrays frame by frame, and releases large intermediates earlier.
  • Reloads the movie for localization retries and removes the unused movie output.
  • Lowers the per-trial budget from 9× to 6× one channel plus 1 GB, with updated sizing tests and new handoff/retry tests.
File Description
tests/​unit/​source_extraction/​fakes/​loadAndProcessTrialAsync.m Updates the SILo fake to the six-output interface.
tests/​unit/​dependencies/​extraction/​TestLocalizationTrialBytes.m Updates expected memory budgets to 6×.
tests/​unit/​dependencies/​extraction/​TestLoadAndProcessTrialAsync.m Tests activity-channel handoff, retries, and error propagation.
tests/​unit/​dependencies/​extraction/​localizationFakes/​localizeSources_vIM.m Adds a recording localizer fake with configurable failures.
source_extraction/​SILo.m Adapts the localization call to the revised outputs.
dependencies/​extraction/​activityImage/​localizeSources_vIM.m Reduces array lifetimes and uses frame-wise masking and maxima.
dependencies/​extraction/​activityImage/​localizationTrialBytes.m Applies the 6× budget and documents measured peaks.
dependencies/​extraction/​activityImage/​loadAndProcessTrialAsync.m Hands off movie ownership and reloads it when retrying.

Review effort: Balanced

@KasparP
KasparP changed the base branch from silo-memory-estimate to main November 22, 2026 14:56
@KasparP
KasparP merged commit e000598 into main Nov 22, 2026
1 check passed
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