Skip to content

perf(laplace): reduce matrix memory -- 62.4% less peak memory - #1300

Merged
lawrennd merged 1 commit into
SheffieldML:mainfrom
dexhunter:perf/laplace-matrix-memory
Nov 16, 2026
Merged

lawrennd merged 1 commit into
SheffieldML:mainfrom
dexhunter:perf/laplace-matrix-memory

Conversation

@dexhunter

Copy link
Copy Markdown
Contributor

TL;DR

  • Reduce peak memory in Laplace._compute_B_statistics for dense Gaussian process inference.
  • Release finished matrices and reuse product buffers for subtraction.
  • Peak memory: 64,009,592 → 24,074,184 bytes, 62.4% less; saves 39,935,408 bytes for 1,000 observations.
  • Observed runtime: 172.96 ms → 166.88 ms; memory reduction is the goal.
  • The three required likelihood inference and prediction tests pass with python3 -m pytest (full command below).

What & Why

The function kept temporary matrices alive after their last use and allocated new arrays for subtraction.
It now releases those matrices sooner and writes subtraction results into owned product arrays.

The same matrix products and factorization compute all four return values.
The input arrays remain unchanged.

Validation

Peak allocated bytes for individual calls, measured after creating the inputs:

Situation Before After Change
1,000 observations, positive curvature 64,010,400 bytes 24,074,832 bytes 39,935,568 bytes less
17 observations, negative curvature clipped 24,672 bytes 15,718 bytes 8,954 bytes less
9 observations, zero curvature and Fortran layout 12,112 bytes 11,304 bytes 808 bytes less
2 observations, NaN rejection 2,009 bytes 2,009 bytes Unchanged

The combined workload produced these statistics across five fixed inputs and three runs:

Statistic Value
Old -> new (mean) 61.0 MiB -> 23.0 MiB (62.4% less peak memory)
95% CI of the new/old ratio (bootstrap, 10,000 resamples) 0.376–0.376 (62.4–62.4% less peak memory)
Mean ± SD of the change per input -38.1 MiB ± 0 bytes
95% CI of the mean change (t-interval) -38.1 MiB to -38.1 MiB
Number of fixed inputs (n) 5 (3 runs)

The memory measurements use tracemalloc, NumPy, SciPy, Python 3.12, and one BLAS thread.
Numerical comparisons check every return value and unchanged inputs with tolerances of 1e-9.
The individual-call table uses the same cases and memory helper; its allocation boundary differs slightly from the combined workload.

Native tests: 3 passed in 0.89s, run from the repository root:

python3 -m pytest GPy/testing/test_likelihood.py::TestGammaLikelihood::test_laplace_inference GPy/testing/test_likelihood.py::TestStudentTPredictive::test_laplace_predict GPy/testing/test_likelihood.py::TestPredictiveQuadrature::test_poisson_laplace_predict -q --tb=line

Both ruff check GPy and ruff format --check GPy pass.

The reference benchmark measures one dense 1,000-observation call:

OPENBLAS_NUM_THREADS=1 OMP_NUM_THREADS=1 MKL_NUM_THREADS=1 python3 osspr-intake/harness/laplace_b_peak.py

Why this is safe and easy to maintain

The change adds seven net lines and no branches.
The calculations stay readable, and outputs are unchanged.
Gamma, Student-T, and Poisson tests cover inference and prediction.

AI assistance

This PR was prepared with AI assistance from Aiden. Candidates were tracked with Weco Observe.

Weco share link: https://dashboard.weco.ai/share/KM2wDuSc3Y5aO2RvMU1tl3Mvj6lBFlWO

Release temporary matrices after their last use and reuse owned product
buffers for subtraction and diagonal updates. Keep the same matrix products
and return values while reducing peak allocated memory by 62.4%.

Co-Authored-By: Aiden <[email protected]>
@codecov

codecov Bot commented Nov 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 62.17%. Comparing base (b616c84) to head (585e06f).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1300      +/-   ##
==========================================
+ Coverage   62.15%   62.17%   +0.01%     
==========================================
  Files         206      206              
  Lines       21955    21962       +7     
  Branches     2564     2564              
==========================================
+ Hits        13646    13654       +8     
  Misses       7682     7682              
+ Partials      627      626       -1     
Flag Coverage Δ
unittests 62.17% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lawrennd lawrennd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@lawrennd

Copy link
Copy Markdown
Member

Thanks Dex

@lawrennd
lawrennd merged commit f46452a into SheffieldML:main Nov 16, 2026
19 checks passed
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