Skip to content

fix(prediction): align fantasy mean_cache key with parent for cache hits - #2759

Closed
umi008 wants to merge 3 commits into
cornellius-gp:mainfrom
umi008:fix-2669-fantasy-mean-cache-key
Closed

fix(prediction): align fantasy mean_cache key with parent for cache hits#2759
umi008 wants to merge 3 commits into
cornellius-gp:mainfrom
umi008:fix-2669-fantasy-mean-cache-key

Conversation

@umi008

@umi008 umi008 commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Closes #2669

Root cause

In DefaultPredictionStrategy.get_fantasy_strategy, the pre-computed fantasy mean cache is stored via add_to_cache(fant_strat, "mean_cache", fant_mean_cache) with no positional args. But DefaultPredictionStrategy.mean_cache is a @property that 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_cache is 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:

add_to_cache(fant_strat, "mean_cache", fant_mean_cache, settings.observation_nan_policy.value())

The fantasy model inherits the parent's context, so the observation_nan_policy at 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

File Change
gpytorch/models/exact_prediction_strategies.py Pass settings.observation_nan_policy.value() to add_to_cache so the fantasy mean_cache key matches the parent
test/models/test_exact_gp.py New TestFantasyMeanCacheKey class with 2 tests: cache-key continuity and cache-hit verification

Test plan

  • 2 new regression tests pass (TDD: cache key mismatched before fix; same key after fix)
  • test/models/test_exact_gp.py suite passes (82 tests; 1 pre-existing unrelated import error)
  • pre-commit run --files gpytorch/models/exact_prediction_strategies.py test/models/test_exact_gp.py passes

umi008 added 3 commits July 10, 2026 15:02
…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
umi008 force-pushed the fix-2669-fantasy-mean-cache-key branch from 525f745 to de76584 Compare July 10, 2026 21:02
@umi008 umi008 closed this by deleting the head repository Jul 25, 2026
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.

[Bug] mean_cache not attached with correct args in get_fantasy_model

1 participant