Fix mis-broadcast in exact_predictive_mean for batched multi-output GPs#2762
Open
saitcakmak wants to merge 1 commit into
Open
Fix mis-broadcast in exact_predictive_mean for batched multi-output GPs#2762saitcakmak wants to merge 1 commit into
saitcakmak wants to merge 1 commit into
Conversation
DefaultPredictionStrategy.exact_predictive_mean unconditionally squeezed dim 1 of any 4-dimensional mean_cache. For batched multi-output (batch-independent) models with a singleton batch dimension -- e.g. a GP fantasized with num_fantasies over a single candidate (mean_cache shape [num_fantasies, 1, num_outputs, num_train]) -- this drops the singleton batch dim and causes the remaining dims to mis-broadcast against test_train_covar, multiplying the num_fantasies dimension. Guard the squeeze with an isinstance(train_prior_dist, MultitaskMultivariateNormal) check, restoring the type-based gating that predated commit b9dc064. Fixes the crash reported in meta-pytorch/botorch#3336.
1 task
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.
Summary
DefaultPredictionStrategy.exact_predictive_meanunconditionally squeezed dim 1 of any 4-dimensionalmean_cache. This is only correct for multitask models (MultitaskMultivariateNormal), whosemean_cachecarries a spurious singleton at index 1. For batched multi-output (batch-independent) models — a plain, non-multitaskMultivariateNormal— a 4-dimmean_cachewith a singleton batch dimension gets its batch dim dropped, and the remaining dimensions mis-broadcast againsttest_train_covar.Fixes the crash reported in meta-pytorch/botorch#3336 ("Fantasizing on Multioutput Batch Independent GP adds another output dimension").
Root cause
The
len == 4shape heuristic was introduced in commitb9dc064("Passing unit tests now", part of the original qKG work), replacing the earlierisinstance(train_prior_dist, MultitaskMultivariateNormal)gating. The shape-only check over-fires: it also catches plain batched multi-output models, for which the squeeze is wrong.The bug only triggers when dim 1 (the candidate batch
b) has size 1. Forb > 1,squeeze(1)is a no-op, which is why single-output GPs andq-batched cases were unaffected.Shape trace
For a fantasized batched multi-output GP with a single candidate (
b == 1):The squeezed cache mis-broadcasts, multiplying the two
num_fantasies=8dims into[64, 8, 8, 2, 21](numel 172032) instead of aligning into[64, 8, 1, 2, 21](numel 21504).predictive_mean.view(...)inExactGP.__call__then fails:Fix
Guard the squeeze with the distribution-type check the code originally had, restoring the pre-
b9dc064gating. Multitask behavior is byte-for-byte identical.Minimal repro (pure BoTorch)
Testing
Added
test_batched_multioutput_prediction_singleton_batchintest/models/test_exact_gp.py. It reproduces the crash deterministically (no fantasize randomness) using a batched multi-output GP whose training data has a 3-dim aug batch[b0, b1, m] = [4, 1, 2](producing the same 4-dimmean_cache), and checks the multi-output means against independent per-output single-output GPs to< 1e-6. Mutation-checked: the test fails on unpatched code (RuntimeError: shape '[64, 4, 1, 2, 5]' is invalid for input of size 10240) and passes after the fix.Verification:
test_exact_gp,test_simple_gp_regression,test_batch_multitask_gp_regression,test_independent_multitask_gp_regression,test_kronecker_multitask_gp_regression,test_hadamard_multitask_gp_regression,test_derivative_gp_fantasy): 88 passed (85 baseline + 3 inherited runs of the new test). ✅test_gpytorch,test_gp_regression,test_model_list_gp_regression,test_knowledge_gradient): 69 passed. ✅flake8+ufmtclean. ✅