Repository navigation
Bound the window-cache build to the memory available — Closes #32 - #36
Open
conradbzura wants to merge 3 commits into
Open
conradbzura wants to merge 3 commits into
conradbzura wants to merge 3 commits into
Conversation
conradbzura
force-pushed
the
32-bounded-cache-build
branch
4 times, most recently
from
December 4, 2026 00:33
f61f6bb to
fbe1b88
Compare
Records the sha256 of all eight array files and of the record of an entry built from a channel of counts, one recording positions twice, one of coverage deltas and a bigWig, as signals and reversed as controls, over overlapping windows of two widths. The digests were taken from the current build, so a rewrite of how the entry is built has to write the same bytes.
The build held every contig's ordered sites until the last contig was grouped and allocated the result beside them, so its peak followed the whole cache: about 2.8 bytes per byte of entry on top of the bigWig read buffers, and every bigWig reader was held for the whole build. Under an 8 GiB cgroup cap the build was killed at 512 sparse channels while draining the finished cache needed under 1.6 GiB. group_into now groups in two passes, fitted to a budget that defaults to half of what quadrat.pileup.plan.memory_budget reads. The first reads the channels and spills runs of ordered sites to a scratch directory beside the result. bigWig channels are taken 32 to a group, each group's readers made for it and let go after, its reads running across every contig as they finish, as many at once as half the budget holds of their read buffers and the cores allow. The second lays out offsets from the sites counted per window and fills pos, val and chan through a mapping whose blocks are allocated first, a block of windows at a time. Runs and blocks are sized at the cost a site was measured to have while it is put in order, and whoever hands sites on lets go of them first. Sites sharing a window, offset and channel are of one run, already ordered by value, and a stable sort keeps them so, so the arrays are byte for byte what one sort over each contig made. group_into reports how it split the work as a GroupingReport. window_cache opens every track before it stages anything, groups straight into the staged entry through the one staging and commit path write_window_cache now shares, and reads the build back in the residency it resolves to, so a memory build holds one copy rather than two. Only a failed write to the entry is retried in a scratch directory; a track that cannot be read raises once. window_cache and WindowSignalCache.build take budget, and window_cache verbose, to print how each grouping was split. window_group_sums reads bigWig channels in the same groups. BIGWIG_CHUNK_BYTES loses its underscore, since the build's sizing now reads it.
build_training_loader passes verbose to window_cache, so a cold build prints the budget each grouping was sized to and how many runs, reader groups and blocks it took, beside the loci it kept.
conradbzura
force-pushed
the
32-bounded-cache-build
branch
from
December 4, 2026 01:03
fbe1b88 to
f91438e
Compare
conradbzura
marked this pull request as ready for review
December 4, 2026 01:18
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.
Summary
Build the window cache within a memory budget, writing it to disk as it is built, so the build no longer holds every contig's sites beside the finished arrays. The grouping runs in two passes: it reads the channels and spills ordered runs of sites to a scratch directory beside the result, then lays out
offsetsfrom the counts and fillspos,valandchanthrough a mapping, a block of windows at a time. bigWig channels are read in groups of 32, each group's readers made for it and let go before the next. The entry's bytes are unchanged: a test pinning the digests recorded frommainpasses at the default budget and at one byte, a Hypothesis test holds the arrays identical to the default's at any budget up to 1 MB, and a 64-channel synthetic entry built in Docker from this head is byte-identical tomain's.Under
--memory=8g --memory-swap=8g --cpus=8, with the mapped residency, 1,024 sparse channels now build and drain. The build's peak anonymous memory climbs with the channels until it reaches the process baseline plus the budget, 2.80 GiB, and holds there from 512 channels to 1,024;mainwas OOM-killed at 512. That is how this reads criterion #1's "flat, within a fixed margin": the peak is bounded by the baseline plus the budget at every channel count, and flat once the grouping fills the budget. It is met for the mapped residency only: the default residency, memory underfork, is killed at 1,024 channels while loading the finished 8.1 GB cache, which choosing the residency from the budget (#35) addresses. The trade is build time, from the spill pass.Closes #32
Proposed changes
Two-pass grouping within a budget (
quadrat.cache)group_track,WindowSignalCache.buildandwindow_cachetake a keyword-onlybudget: a positive integer of bytes, orNonefor half ofquadrat.pileup.plan.memory_budget(), or half of 4 GiB where that reads nothing. Three things are fitted to it (_build_sizes):group_track's docstring: one channel's sites on one contig, read whole, of which as many as channels read at once plus one are held; a channel of coverage deltas' whole contig of delta rows; the first join of channels of counts on a contig, which overlapping windows can make larger than a run, and the join engine's own buffers; one window's sites over every channel, the least a block takes; a group's readers' data indexes; and the spill and result where the scratch directory is a tmpfs._Spill,_spill_tracks,_spill_bigwigs)._site_order, at the widths the result keeps._bigwig_channel_reads); a contig is flushed once its last channel is in._Spill.merge).pos,valandchanare reserved at full length (posix_fallocate, or zeros written from one 1 MiB buffer), so a full disk is anOSErrorrather than aSIGBUS, then mapped._group_into, on tracks opened and verified up front by_open_frames, which holds a bigWig as its path until its group.Callers
group_track(..., *, scratch_dir=None, budget=None)keeps its contract. It groups into a scratch directory, loads the four arrays one at a time, unlinking each, and removes the directory.WindowSignalCache.build(..., *, budget=None)groups a mapped cache straight into its scratch directory, which_owned_scratch_dirmakes (the reap-at-exit logic factored out offrom_arrays), and holds a memory cache asgroup_trackanswers it.window_cache(..., *, verbose=False, budget=None):_commit_entry(whichwrite_window_cacheshares), and reads it back in the resolved residency, so a memory build loads the committed arrays once;verbose, prints a line per grouping: the budget, the bigWig channels read at once and their groups, the runs and the sites a run is flushed at, the MiB spilled, and the blocks and the sites a block is sized to._commit_entrytells a write from a track fault; a build that persists nothing raises what the write raised, aPermissionErroras one.window_group_sumsreads bigWig channels in the same groups, every contig of a group in one stream, and adds a group's totals per window in channel order once the group is in, so each window takes its bigWig channels in channel order and then its other channels, as onmain, and itsfloat64totals do not change.build_training_loaderpassesverbosetowindow_cache.stores.BIGWIG_CHUNK_BYTESloses its underscore, sincequadrat.cachesizes a read from it.Docs
group_track's docstring states what a grouping fits to its budget and what it does not, the two passes, the cost of ordering a site (_ORDERING_BYTES_PER_SITE, 42 bytes) and the 10 bytes a site of scratch disk.window_cachedocuments the fallback's cost, that a keyed build spills inside its staged entry, and thatresidencyandbudgetare checked on every call, a hit included;window_group_sumsstates what its reads hold and the fixed order each window's totals are added in, its bigWig channels in channel order and then its other channels, as onmain;extract_signalandextract_locidocument the scratch directory a grouping needs.Docker acceptance
Synthetic data: 30,000 peaks and 30,000 negatives, sparse bigWig channels plus 4 controls,
--memory=8g --memory-swap=8g --cpus=8, workers 0, one epoch drained. Build peak anonymous memory in GiB; the branch column is this head, andmainis unchanged since it was measured:maindefaultmainmappedmainOOM-killed, both residencies finish; at 16, mapped builds a 14.4 GB cache at 3.43 GiB and default is killed loading it. 32 and 64 were not run, since their entries and spill would fill the Docker VM's disk.main's 11.0–16.4 s, and 256 channels 37.4–41.8 s against 27.7–28.1 s, with mean build cores falling from 3.5 to 2.5. At this head, one run each with the host short of memory: 12.8 s at 64 channels and 53.4 s at 256. The pool stalling while a run flushes is recorded as a follow-up.Test cases
window_cachemain, and nothing else is leftgroup_trackgroup_trackscratch_dircleanupgroup_trackValueErrornaming it, and the directory left emptygroup_trackOSErrorscratch_diris the one usedgroup_trackgroup_trackTypeErrororValueErrorgroup_trackwindow_group_sumswindow_group_sumswindow_group_sumsWindowSignalCache.buildbuildWindowSignalCache.buildWindowSignalCache.buildENOSPC, or withEACCESOSErrorcarryingENOSPCand nothing left;PermissionErroritself in either residency, and nothing leftWindowSignalCache.buildwrite_cache_arrayswrites, witharray_meta's countsWindowSignalCache.buildos.posix_fallocateWindowSignalCache.buildos.posix_fallocate, and a disk refusing the zerosOSErrorcarryingENOSPC, the refusal made, nothing leftposix_fallocateWindowSignalCache.buildoffsetsand emptypos,valandchan, readable mapped or loadedWindowSignalCache.buildValueErrornaming it, and an empty scratch directoryWindowSignalCache.buildWindowSignalCache.buildwindow_cachewindow_cachewindow_cachewindow_cacheverboseverbosewindow_cacheverbosewindow_cacheverbosegroup_trackstates (Hypothesis)window_cacheverbosememory_budget()window_cacheverbosewindow_cacheverbosewindow_cacheverbosewindow_cacheintverbosewindow_cacheTypeErrororValueErrorwindow_cacheValueError, with no entry or staging leftwindow_cacheFileNotFoundError, and no cache directory madewindow_cachebuild_training_loaderverboseand an empty cache directoryverbosebuilders