fix(prediction): align fantasy mean_cache key with parent for cache hits - #2759
Closed
umi008 wants to merge 3 commits into
Closed
fix(prediction): align fantasy mean_cache key with parent for cache hits#2759umi008 wants to merge 3 commits into
umi008 wants to merge 3 commits into
Conversation
…uity)
Regression tests verify that fantasy model mean_cache cache key includes observation_nan_policy arg, matching the parent model's key format. Both tests fail on current code — cache key has empty args instead of ('ignore',).
Pass observation_nan_policy.value() to add_to_cache in get_fantasy_strategy so the stored mean_cache key matches the key constructed by DefaultPredictionStrategy._mean_cache(@cached). This eliminates cache miss on every fantasy model call, turning O(n) recomputation into O(1) cache lookup. Also adds regression test verifying cache hit on fantasy model call.
…trategy InterpolatedPredictionStrategy uses different cache keys (interp_inner_prod, interp_response_cache) instead of mean_cache. The new regression tests assumed DefaultPredictionStrategy semantics and failed on TestWiskiExactGP and TestInterpolatedExactGP because: 1. WISKI models use fantasy_mean_cache, not mean_cache 2. GridInterpolationKernel deepcopy fails without torch.no_grad() 3. InterpolatedPredictionStrategy.get_fantasy_strategy doesn't populate mean_cache Skip both tests in TestInterpolatedExactGP (inherited by TestWiskiExactGP).
umi008
force-pushed
the
fix-2669-fantasy-mean-cache-key
branch
from
July 10, 2026 21:02
525f745 to
de76584
Compare
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.
Closes #2669
Root cause
In
DefaultPredictionStrategy.get_fantasy_strategy, the pre-computed fantasy mean cache is stored viaadd_to_cache(fant_strat, "mean_cache", fant_mean_cache)with no positional args. ButDefaultPredictionStrategy.mean_cacheis a@propertythat delegates to_mean_cache(settings.observation_nan_policy.value())which is decorated with@cached(name="mean_cache")and therefore reads via cache key("mean_cache", ("ignore",), ...).The keys do not match, so fantasy models never hit the cache and
mean_cacheis recomputed on every call. This is a pure performance bug — values are correct, but the fantasy update optimization is silently defeated (a problem in particular when many fantasy models are created in a loop).Fix
One line in
gpytorch/models/exact_prediction_strategies.py:263:The fantasy model inherits the parent's context, so the
observation_nan_policyat fantasy-creation time is the same as at call time. If the user changes the policy between fantasy creation and call, the cache naturally recomputes (same behavior as the parent model already has).Changes
gpytorch/models/exact_prediction_strategies.pysettings.observation_nan_policy.value()toadd_to_cacheso the fantasy mean_cache key matches the parenttest/models/test_exact_gp.pyTestFantasyMeanCacheKeyclass with 2 tests: cache-key continuity and cache-hit verificationTest plan
test/models/test_exact_gp.pysuite passes (82 tests; 1 pre-existing unrelated import error)pre-commit run --files gpytorch/models/exact_prediction_strategies.py test/models/test_exact_gp.pypasses