Repository navigation
SILo localization: stop copying the movie; budget 6x per trial (stacked on #51) - #54
Merged
Merged
Conversation
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>
Contributor
There was a problem hiding this comment.
🔵 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #51 (base
silo-memory-estimate). Merge #51 first, then retarget this tomain.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:
loadAndProcessTrialAsynckept references to the movie (IM) and its variance factors (vIM) while callinglocalizeSources_vIM. WhenlocalizeSources_vIMmasked them, MATLAB copied both (copy-on-write). That is 2 movies.IMs(the smoothed movie) stayed alive to the end, andstdIMwas a new array next to the baselineIMb. That is 2 more.X(mask) = nanwith 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).
loadAndProcessTrialAsynclocalizeSources_vIMthrough two nested functions,takeMovieandtakeVarFactors. These clear the caller's references, solocalizeSources_vIMmasks and filters the arrays in place instead of copying them.~).localizeSources_vIMsetFrames), so no index array is built.IMsas soon as the baseline is made.vIMand the s.d. once used.vIM), makes the ones single, like the movie, instead of double. They are all ones either way.localizationTrialBytesEvidence
All runs are on 780125: multi-ROI SLAP2, 2 channels, 80 Hz, about 2250 frames per trial. Each trial ran
loadAndProcessTrialAsyncwith #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:
Same outputs
The outputs are
meanIM, the activity image,aData, the peaks, the discarded frames and the half images. Compared withisequaln, 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):
Where the peak falls, from memory probes on E1T1DMD2:
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.nWorkersand the pool size.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
TestLoadAndProcessTrialAsync, with a recording fake oflocalizeSources_vIM, checks three things:TestLocalizationTrialBytesis updated for 6×.TestLoadAndProcessTrialAsync,TestLocalizationTrialBytesandTestSILogive 28 passed; all oftests/unitgives 169 passed, 0 failed (R2024b, Windows).🤖 Generated with Claude Code