Skip to content

Speed up GP gradient and initial hyperparameter estimation - #96

Open
k-yoshimi wants to merge 2 commits into
developfrom
perf/gp-grad-and-init-hyperparam
Open

Speed up GP gradient and initial hyperparameter estimation#96
k-yoshimi wants to merge 2 commits into
developfrom
perf/gp-grad-and-init-hyperparam

Conversation

@k-yoshimi

Copy link
Copy Markdown
Contributor

Summary

Two numerically-equivalent GP speedups:

  • exact.get_grad_marlik: reuse the existing Cholesky factor via scipy.linalg.cho_solve instead of recomputing a full LU-based inverse with scipy.linalg.inv. Numerically identical (max abs diff ~1e-18), ~14× faster for the inverse step. This is on the hyperparameter-optimization hot path.
  • cov._gauss.get_cand_params: vectorize the random pairwise-distance sampling loop into a single np.random.randint((M, 2)) draw. Produces the identical random sequence as the previous per-iteration loop, so per-seed results are unchanged.

Tests

  • test_get_grad_marlik_matches_finite_difference: checks the cho_solve-based gradient against a central finite-difference of eval_marlik (rtol=1e-6, atol=1e-7; observed error ~1e-9 across seeds).
  • test_get_cand_params_matches_old_loop_implementation: runs the real cov.get_cand_params and asserts it equals an in-test reimplementation of the old loop under the same seed.
  • test_get_cand_params_is_reproducible_for_fixed_seed.

Verified by mutation testing (wrong Cholesky lower flag and non-equivalent pair sampling each fail the corresponding test). Reviewed with Codex until convergence.

🤖 Generated with Claude Code

Kazuyoshi Yoshimi and others added 2 commits June 27, 2026 08:37
- exact.get_grad_marlik: reuse the existing Cholesky factor via
  scipy.linalg.cho_solve instead of recomputing a full LU-based inverse
  with scipy.linalg.inv. Numerically identical (max abs diff ~1e-18),
  ~14x faster for the inverse step.
- cov._gauss.get_params_bound: vectorize the random pairwise-distance
  sampling loop with a single np.random.randint((M, 2)) draw. Produces
  the identical random sequence as the previous per-iteration loop, so
  results are unchanged for a given seed.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Addresses AI review feedback (rounds 1-2): lock down the numerical/RNG
equivalence claims behind the perf changes, exercising the production code.

- test_get_grad_marlik_matches_finite_difference: checks the cho_solve-based
  gradient against a central finite-difference of eval_marlik (rtol=1e-6,
  atol=1e-7; observed error ~1e-9 across seeds).
- test_get_cand_params_matches_old_loop_implementation: runs the real
  cov.get_cand_params and asserts it equals an in-test reimplementation of the
  old per-iteration loop under the same seed (covers the full path including the
  post-sort randint draw).
- test_get_cand_params_is_reproducible_for_fixed_seed: guards seed stability.
  RNG global state is saved/restored around the seeded tests.

Verified by mutation testing: a wrong inverse and a non-equivalent pair-sampling
change each make the corresponding test fail.
@k-yoshimi
k-yoshimi requested review from rtmr and yomichi June 27, 2026 01:01
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.

1 participant