Repository navigation
[FIX] PeptDeep rescoring: no ONNX memory pattern; NCE selection predicts each sampled peptide once - #10641
Conversation
The inputs of the PeptDeep models change shape from call to call (batch size, peptide length). For each shape, ONNX Runtime's memory pattern plans one block for all intermediate tensors of a Run, and every Run of that shape allocates and frees that block again. For the MS2 model these blocks are 8 to 25 MB: natively, 542 allocations of 4 MiB or more per search of a 75 MB file (erwinia_tmt.mzML). Without the pattern the tensors are allocated one by one, at most 8.6 MB each. Natively, DisableMemPattern changes only the peak memory: - The features of all 2051 PSMs of erwinia_tmt.mzML are bit-identical, at NCE 30 and with the automatic NCE. - Inference is as fast: 5.03 s instead of 5.08 s per search. - The CPU arena of the MS2 model peaks lower: 82 instead of 149 MB allocated, 59 instead of 93 MB in use. - A whole ProSE search with PeptDeep and the automatic NCE (16 threads, median of 5 runs) takes 5.73 s instead of 5.77 s and peaks at 309 instead of 337 MiB resident. Its idXML output is identical. Where ONNX Runtime has no CPU arena (it has none on 32-bit platforms), these blocks come from malloc, and the allocator decides whether they are reused. This was found in a WebAssembly build of PeptDeepRescoring (the stacked branch of #10604, not part of this commit). There, mimalloc 3.5.1 (Emscripten 6.0.12) places an allocation larger than one 4 MiB arena chunk only in chunks that are entirely free. When it finds none, it reserves new arenas and never releases them. On the threaded WebAssembly module, three PeptDeep-rescored searches of erwinia_tmt.mzML took the heap from 351 to 1414, 2345 and 4096 MB; the PeptDeep steps alone grew it by 540, 576 and 1305 MB. Without the pattern, the PeptDeep step of every search after the first takes no new memory. The search around it still grows the heap from search to search, as it does without PeptDeep. The features are bit-identical in WebAssembly as well. Co-Authored-By: Claude Opus 5.5 <[email protected]> (cherry picked from commit 217d1e3)
…ts sample once The automatic NCE selection predicted its sample of up to 2000 confident PSMs PSM by PSM. The predictions for the run, in contrast, were already made once per distinct (sequence, charge). A file that repeats its peptides therefore made repeated predictions, in batches of up to 500 copies of the same peptides. big733.mzML, which holds erwinia's spectra eight times, is such a file. Each distinct peptide of the sample is now predicted once per candidate energy and reused for every sampled PSM that identifies it. Natively, the NCE selection makes fewer predictions on a file that repeats its peptides: as many per candidate energy as the sample has distinct (sequence, charge), instead of one per sampled PSM. A ProSE search with PeptDeep and the automatic NCE (16 threads, median of 5 runs, on top of the memory pattern change) takes: - for big733.mzML 6.0 s instead of 12.6 s, with 59 instead of 161 CPU seconds; - for erwinia_tmt.mzML 5.6 s instead of 5.7 s. The results do not change. The idXML output is identical for both files, i.e. the features of all 2051 PSMs of erwinia_tmt.mzML and all 16408 PSMs of big733.mzML. The selected NCE (39 for both) and the median cosine of big733 (0.631769) are the same. In WebAssembly (the stacked branch of #10604, not part of this commit), the tensors of those batches grew the heap by another 1.6 GB during "Selecting collision energy", even without ONNX Runtime's memory pattern. For big733 the PeptDeep step now takes the heap from 2151 to 2311 MB instead of 3926 MB, in 6 to 8 s instead of 24 s. The features are bit-identical there as well. Co-Authored-By: Claude Opus 5.5 <[email protected]> (cherry picked from commit 7e9a861)
… PeptDeep classes and ProSEAlgorithm The number of peptides per ONNX Runtime run was written out in five places: 500 in the constructors of PeptDeepMS2Inference, PeptDeepRTInference and PeptDeepCCSInference, in PeptDeepRescoring's 'batch_size', and (128 under __EMSCRIPTEN__, since 888678e) in ProSEAlgorithm's 'peptdeep:batch_size'. ML::PEPTDEEP_DEFAULT_BATCH_SIZE (PeptDeepUtils.h) is now the one definition: 500, and 128 in WebAssembly, where ONNX Runtime has no memory arena and the memory of large tensors is not reused. Natively nothing changes. In WebAssembly, PeptDeepRescoring and the inference classes default to 128 too (the bindings' predictMS2() uses the inference class's default; its comment said the batch size was that of the parameter, which was only true natively). The PeptDeepLibraryPredictor config keeps its own 500: it is not part of ProSE's rescoring. Tests: the default of both parameters and the value of the constant. The develop PR #10641 carries two of the PeptDeep memory commits but not 888678e, so the constant lives only in this branch. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Priority: ➖ Normal Change: Bug fix Merge Risk: ⚪ Minimal · up to
|
Summary
Two small fixes to PeptDeep rescoring. Both apply to code that is already on
develop, and both change nothing in the results.ONNXPredictorBase: ONNX Runtime's memory pattern is switched off (SessionOptions::DisableMemPattern()).PeptDeepRescoring: the automatic NCE selection predicts each distinct (sequence, charge) of its sample once per candidate energy, and reuses the prediction for every sampled PSM that identifies it. Before, it predicted PSM by PSM. The predictions for the run itself were already made once per distinct peptide.They were found while making PeptDeep rescoring work in the WebAssembly build of ProSE (#10604). They are not WebAssembly-specific, so they are proposed here as their own PR. The same two commits (
217d1e3b59,7e9a8614a6) are in #10604; they are cherry-picked here with-x.1. Memory pattern off (
ONNXPredictorBase)The inputs of the PeptDeep models change shape from call to call (batch size, peptide length). For each shape, ONNX Runtime's memory pattern plans one block for all intermediate tensors of a
Run, and everyRunof that shape allocates and frees that block again. For the MS2 model these blocks are 8 to 25 MB: natively, 542 allocations of 4 MiB or more per search oferwinia_tmt.mzML(75 MB). Without the pattern the tensors are allocated one by one, at most 8.6 MB each.Natively this changes only the peak memory: the CPU arena of the MS2 model peaks at 82 instead of 149 MB allocated (59 instead of 93 MB in use), the time is neutral (PeptDeep part of a search 5.03 s instead of 5.08 s), the results are bit-identical.
In WebAssembly it is the cause of the memory growth. ONNX Runtime has no CPU arena on 32-bit platforms, so these blocks come from
malloc. mimalloc 3.5.1 (Emscripten 6.0.12) places an allocation larger than one 4 MiB arena chunk only in chunks that are entirely free; when it finds none, it reserves new arenas and never releases them. On the threaded module, three PeptDeep-rescored searches oferwinia_tmt.mzMLtook the heap from 351 to 1414, 2345 and 4096 MB; the PeptDeep steps alone grew it by 540, 576 and 1305 MB. Without the pattern the PeptDeep step of every search after the first takes no new memory, and the same three searches end at 1327 to 1387 MB (three runs).2. NCE selection predicts each sampled peptide once (
PeptDeepRescoring)The automatic NCE selection scores a sample of up to 2000 confident PSMs at every candidate energy of its grid. It predicted the spectra PSM by PSM, so a file that repeats its peptides made repeated predictions, in batches of up to 500 copies of the same peptides.
big733.mzML(erwinia's spectra eight times, 16408 PSMs) is such a file. Now each distinct (sequence, charge) of the sample is predicted once per candidate energy; the number of predictions is the number of distinct peptides of the sample instead of the number of sampled PSMs.Natively the search of
big733.mzMLis twice as fast (see below). In WebAssembly the tensors of those batches grew the heap by another 1.6 GB during "Selecting collision energy", even without the memory pattern: the PeptDeep step ofbig733.mzMLnow takes the heap from 2151 to 2311 MB instead of 3926 MB, in 6 to 8 s instead of 24 s.Results are bit-identical
erwinia_tmt.mzML(2051 PSMs) andbig733.mzML(16408 PSMs), PeptDeep with the automatic NCE: the idXML of thedevelopbuild, ofdevelopplus the first commit and of this branch are identical (apart from the time stamp of the run); the selected NCE (39) and the median cosines (0.620535, 0.631769) are the same.erwinia_tmt.mzMLat NCE 30 and with the automatic NCE: the features of all 2051 PSMs are byte-identical (measured on the native build of the WebAssembly stack with the two library changes as a patch).erwinia_tmt.mzML(NCE 30 and automatic) and of all 16408 PSMs ofbig733.mzMLare bit-identical.Native measurements
ProSE search with PeptDeep and the automatic NCE, 16 threads, native ONNX build (Release, ONNX Runtime 1.23.2), median of 5 runs, the three builds interleaved. The machine was shared with other jobs, so read the times to about 0.2 s. "Max RSS" is the peak resident set of the process.
erwinia_tmt.mzMLdevelopbig733.mzMLdevelopSo: the memory pattern changes the memory and not the time; the dedup changes the time of files that repeat their peptides and not that of
erwinia_tmt.mzML, which has 1977 distinct peptides in its 2051 PSMs.Tests (native,
-DWITH_ONNX=ON -DENABLE_CLASS_TESTING=ON, Release)ctest -R "ProSE|PeptDeep|ONNX": 79 of 79 passed, 0 failed. This includesPeptDeepInference_test,PeptDeepRescoring_test(with the automatic NCE selection),PeptDeepLibraryPredictor_test,ProSEAlgorithm_test,TOPP_ProSE_peptdeep,TOPP_ProSE_peptdeep_no_annotation_fails,TOPP_ProSE_peptdeep_missing_model_failsand all otherTOPP_ProSE_*tests. No test was added: the existing tests cover both code paths, and the effects are in the memory and time of large inputs. The WebAssembly regression test for the heap growth is in #10604.Related
#10604 (WebAssembly, ProSE bindings for the ProSE app) carries the same two commits and motivated them. They drop out of its diff once this PR is merged.
🤖 Generated with Claude Code
Summary by CodeRabbit