Skip to content

fix: make CPU predictions thread-count invariant and floor nonnegative linear fallback - #139

Merged
RobbinBouwmeester merged 2 commits into
CompOmics:mainfrom
rpinho:fix-136-thread-invariance
Nov 29, 2026
Merged

RobbinBouwmeester merged 2 commits into
CompOmics:mainfrom
rpinho:fix-136-thread-invariance

Conversation

@rpinho

@rpinho rpinho commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Summary

After PR #138 prevented small-reference spline/ridge blow-ups, two issues from #136 remain on main (35025c3c):

  1. Thread-count-dependent CPU drift: deeplc._model_ops.predict() calls torch.set_num_threads(num_threads), changing CPU Conv1D/Linear floating-point reduction order (~3e-8 to 1.2e-6 in shared projections). The float32 per-head spline + ridge calibration amplifies that rounding perturbation across thread counts (up to 5.27e-4 min on the 1,536-row bundled subset and 0.01225 min on the full 5,430-row bundled example between 1 and 3 threads).
  2. Negative straight-line fallback extrapolation: On F006544_PSMList.tsv (1,125 target PSMs, 15 distinct peptidoforms, head 6310), MultiHeadRidgeCalibration's < 200-peptidoform straight-line fallback extrapolates below zero (-3,098.15 s) even when every observed reference RT is nonnegative.

Who hits this: proteomics users running DeepLC (directly or via MS²Rescore's DeepLCFeatureGenerator) across machines with different CPU core/thread counts, or calibrating on small reference sets where the linear fallback extrapolates below 0 (#136).

Changes

  • deeplc/_model_ops.py:
    • On CPU inference for built-in models (DeepLCModel, FlexCNNMultitaskModel) with the standard DeepLCDataset and num_workers == 0, compute length buckets first (preserving original padding windows), subdivide into fixed batches of at most 64 peptides, run each batch with torch.set_num_threads(1), and schedule independent batches across a bounded ThreadPoolExecutor(max_workers=num_threads) (_ordered_cpu_batches).
    • Guard the shared PyTorch thread setting with _CPU_PREDICTION_LOCK, propagate caller CPU autocast settings to worker threads, and always restore prior_threads in a finally block (including on exceptions).
    • Keep GPU, custom model/dataset subclasses, and explicit num_workers > 0 calls on the existing path.
  • deeplc/calibration/multihead.py:
    • In MultiHeadRidgeCalibration._fit_linear(), record _linear_floor = 0.0 when all observed reference retention times are nonnegative (and -np.inf otherwise), and apply np.maximum(..., self._linear_floor) in transform() for the straight-line fallback. Signed reference scales retain signed outputs.
  • tests/test_thread_invariance.py & CHANGELOG.md:
    • Add 5 regression tests (all failing on main, passing with this patch) covering thread invariance across num_threads, thread-setting restoration, concurrent calls, and nonnegative vs signed linear-fallback bounds.

Verification

  • Across independent subprocesses for num_threads in {1, 2, 3, 4, 8}, calibrated RT predictions have 0.0 max absolute difference on both F006544_PSMList.tsv and the bundled example datasets (1,536-row subset and full 5,430-row file), match unpatched num_threads=1 (0.0 diff on the 5,430-row file), and retain ~3.1x–3.4x multi-thread speedup at 4 threads vs 1 (note: ~10% slower than unpatched at 4 threads in local macOS arm64 benchmarks, ~7% faster at 1 thread).
  • On F006544_PSMList.tsv, the residual -3,098.15 s decoy extrapolation is floored to 0.0 s (0 negatives), while signed reference scales, refits, pickling, and NaN propagation are preserved.
  • Full test suite passes (260 passed vs 255 on main), along with ruff check deeplc, ruff format --check deeplc, and mypy deeplc (0 new diagnostics).
  • Found and written with AI agents; a separate cross-family AI referee independently reproduced DeepLC 4.5 produces different results based on the number of threads #136 and verified the patch, thread invariance, and edge cases before this PR.

Fixes #136

🤖 Generated with Claude Code

https://claude.ai/code/session_01LKxc9GKLwnyeV2jaZBxH9N

rpinho and others added 2 commits November 26, 2026 07:36
…e linear fallback

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LKxc9GKLwnyeV2jaZBxH9N
- Wrap both the parallel CPU path and the default path in the same
  save/restore block, so predict never leaves the caller's Torch thread
  setting changed.
- Raise ValueError for a negative num_threads.
- Document the CPU batch_size cap of 64 in predict and core.predict.
- Changelog: note that CPU predictions differ slightly from 4.5.0 and
  that a larger batch_size is lowered on the parallel path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@RobbinBouwmeester
RobbinBouwmeester merged commit 4349062 into CompOmics:main Nov 29, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

DeepLC 4.5 produces different results based on the number of threads

2 participants