Skip to content

[FIX] PeptDeep rescoring: no ONNX memory pattern; NCE selection predicts each sampled peptide once - #10641

Merged
timosachsenberg merged 2 commits into
developfrom
claude/peptdeep-onnx-memory
Oct 23, 2026
Merged

timosachsenberg merged 2 commits into
developfrom
claude/peptdeep-onnx-memory

Conversation

@timosachsenberg

@timosachsenberg timosachsenberg commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Two small fixes to PeptDeep rescoring. Both apply to code that is already on develop, and both change nothing in the results.

  1. ONNXPredictorBase: ONNX Runtime's memory pattern is switched off (SessionOptions::DisableMemPattern()).
  2. 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 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 erwinia_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 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, 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.mzML is 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 of big733.mzML now 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

  • Native, erwinia_tmt.mzML (2051 PSMs) and big733.mzML (16408 PSMs), PeptDeep with the automatic NCE: the idXML of the develop build, of develop plus 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.
  • Native, erwinia_tmt.mzML at 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).
  • WebAssembly: the features of all 2051 PSMs of erwinia_tmt.mzML (NCE 30 and automatic) and of all 16408 PSMs of big733.mzML are 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.

file build wall CPU (user) max RSS
erwinia_tmt.mzML develop 5.77 s 84 s 337 MiB
+ memory pattern off 5.73 s 84 s 309 MiB
+ NCE dedup (this PR) 5.59 s 82 s 309 MiB
big733.mzML develop 12.60 s 162 s 1056 MiB
+ memory pattern off 12.57 s 161 s 1005 MiB
+ NCE dedup (this PR) 6.04 s 59 s 987 MiB

So: 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 includes PeptDeepInference_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_fails and all other TOPP_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

  • Improvements
    • Automatic NCE calibration reuses predictions for matching peptide-and-charge pairs, reducing repeated calculations.
    • ONNX prediction sessions better accommodate inputs with changing shapes.

timosachsenberg and others added 2 commits October 17, 2026 10:42
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)
timosachsenberg pushed a commit that referenced this pull request Oct 19, 2026
… 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]>
@timosachsenberg
timosachsenberg marked this pull request as ready for review October 19, 2026 13:01
@coderabbitai

coderabbitai Bot commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: bbd4a115-77f7-4bb0-a1e3-e4b0e5f543ce
📥 Commits

Reviewing files that changed from the base of the PR and between 77cccda and c4c014b.

📒 Files selected for processing: 3
  • src/openms/include/OpenMS/ML/ONNX/ONNXPredictorBase.h
  • src/openms/source/ANALYSIS/ID/PeptDeepRescoring.cpp
  • src/openms/source/ML/ONNX/ONNXPredictorBase.cpp

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 2 remain after this review.

📝 Walkthrough

The changes disable ONNX Runtime memory patterns in predictor sessions and deduplicate peptide-and-charge prediction inputs during automatic NCE calibration. Sampled PSMs use an index to access the prediction for their peptide-and-charge pair.

Changes: ONNX Session Configuration

Layer / File(s) Summary
Disable ONNX memory pattern
src/openms/include/OpenMS/ML/ONNX/ONNXPredictorBase.h, src/openms/source/ML/ONNX/ONNXPredictorBase.cpp
The documentation notes that input shapes change between calls. The constructor disables ONNX Runtime’s memory pattern for sessions.

NCE Prediction Reuse

Layer / File(s) Summary
Deduplicate NCE predictions
src/openms/source/ANALYSIS/ID/PeptDeepRescoring.cpp
NCE calibration predicts each distinct peptide-and-charge pair once per candidate energy. Sampled PSMs map to the corresponding prediction for scoring.

Priority: ➖ Normal

Change: Bug fix

Merge Risk: ⚪ Minimal · up to c4c01

The change disables ONNX memory-pattern allocation and reuses predictions for duplicate peptide-and-charge inputs. The investigated mapping preserves the corresponding predictions, and no concrete user-facing regression is established; no identified issue blocks merge.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely identifies both main changes: disabling the ONNX memory pattern and avoiding duplicate predictions during PeptDeep NCE selection.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

A rabbit hops beside the run,
Unique peptides meet the sun.
One prediction serves each pair,
Session patterns fade from there.
I nibble greens and cheer the change!

Comment @coderabbitai help to get the list of available commands.

@singjc singjc 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.

Nice, looks good to me!

@timosachsenberg
timosachsenberg merged commit 8cb13f9 into develop Oct 23, 2026
33 checks passed
@timosachsenberg
timosachsenberg deleted the claude/peptdeep-onnx-memory branch October 23, 2026 14:33
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