Unify the gp2Scale covariance computation behind one primitive - #35
Merged
Conversation
The prior covariance, the blocks added by an append, and the posterior's cross-covariance were three different code paths at three levels of support: two near-duplicate distributed functions in GPprior, and nothing at all for the posterior, which called the kernel directly and so built a dense (N x n_pred) array on the client. They are all the same operation over a block grid, differing only in whether the two point sets are the same. distributed_covariance in the new gp2Scale_covariance module is that operation; GPprior._gp2Scale_covariance is its only caller and owns scatter lifetime and nothing else. Falling out of the unification: * A four-argument, args-taking kernel now works under gp2Scale. The old workers called kernel(x1, x2, hps) unconditionally, so a signature supported everywhere else in fvGP raised TypeError on the worker. * The posterior cross-covariance is distributed and stays sparse, so posterior_mean never materializes (N x n_pred). posterior_covariance cannot avoid a dense solve, KV^-1 being dense whatever KV is, so it chunks over prediction points to cap the intermediate at (N x chunk). * The joint-covariance methods (joint_gp_prior, gp_mutual_information, gp_total_correlation) are dense in N by construction; they now densify K explicitly and warn, rather than failing somewhere inside numpy. Add a row-wise distribution alongside the existing block-wise one, selected with gp2Scale_distribution. Row-wise has each worker return a finished CSR row strip, so the COO-to-CSR sort happens in parallel and host assembly is a concatenation with no global COO and no mirroring; it cannot exploit symmetry and so doubles the kernel evaluations. Block-wise remains the default. Assembly speedups: the per-block diagonal mask was a Python list comprehension over every nonzero, now vectorized (97.6 ms -> 0.34 ms on 1.1M nonzeros, and it ran once per diagonal block per likelihood evaluation); empty blocks are skipped rather than shipped; indices are int32 where the matrix allows; the mirrored triplets are written into a single preallocation instead of two np.hstack passes, freeing each gathered block as it is copied; and the augmented matrix is assembled from all-CSR blocks so scipy takes its fast path instead of converting the lot to COO. Fix the scatter race documented as unavoidable. Dask keys scattered data by a hash of its content, so a second GP on the same data, or a second prediction at the same points, lands on one key and the first copy's release races the second scatter inside the scheduler; the tasks then return CancelledError or KeyError. Every scatter now uses hash=False and so gets its own key. _harvest also raises on an exception result rather than letting it reach the assembly and fail there as an unrelated type error. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #35 +/- ##
==========================================
+ Coverage 83.14% 83.31% +0.17%
==========================================
Files 19 20 +1
Lines 6827 7017 +190
==========================================
+ Hits 5676 5846 +170
- Misses 1151 1171 +20 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The prior covariance, the blocks added by an append, and the posterior's cross-covariance were three different code paths at three levels of support: two near-duplicate distributed functions in GPprior, and nothing at all for the posterior, which called the kernel directly and so built a dense (N x n_pred) array on the client.
They are all the same operation over a block grid, differing only in whether the two point sets are the same. distributed_covariance in the new gp2Scale_covariance module is that operation; GPprior._gp2Scale_covariance is its only caller and owns scatter lifetime and nothing else.
Falling out of the unification:
Add a row-wise distribution alongside the existing block-wise one, selected with gp2Scale_distribution. Row-wise has each worker return a finished CSR row strip, so the COO-to-CSR sort happens in parallel and host assembly is a concatenation with no global COO and no mirroring; it cannot exploit symmetry and so doubles the kernel evaluations. Block-wise remains the default.
Assembly speedups: the per-block diagonal mask was a Python list comprehension over every nonzero, now vectorized (97.6 ms -> 0.34 ms on 1.1M nonzeros, and it ran once per diagonal block per likelihood evaluation); empty blocks are skipped rather than shipped; indices are int32 where the matrix allows; the mirrored triplets are written into a single preallocation instead of two np.hstack passes, freeing each gathered block as it is copied; and the augmented matrix is assembled from all-CSR blocks so scipy takes its fast path instead of converting the lot to COO.
Fix the scatter race documented as unavoidable. Dask keys scattered data by a hash of its content, so a second GP on the same data, or a second prediction at the same points, lands on one key and the first copy's release races the second scatter inside the scheduler; the tasks then return CancelledError or KeyError. Every scatter now uses hash=False and so gets its own key. _harvest also raises on an exception result rather than letting it reach the assembly and fail there as an unrelated type error.