Skip to content

Fix predict() blowing up when called twice with different grids#2

Merged
JonasTurnwald merged 1 commit into
JonasTurnwald:mainfrom
timpara:fix/stale-inference-cache
Apr 14, 2026
Merged

Fix predict() blowing up when called twice with different grids#2
JonasTurnwald merged 1 commit into
JonasTurnwald:mainfrom
timpara:fix/stale-inference-cache

Conversation

@timpara
Copy link
Copy Markdown
Contributor

@timpara timpara commented Apr 13, 2026

What broke

_maybe_prepare_inference cached OpKer on the first predict() call but never checked whether the next call used a different w_pred. So if you did:

model.predict(grid_A)
model.predict(grid_B)  # kaboom or wrong answer

The second call reused OpKer from grid_A. Depending on grid sizes this either crashes with a shape mismatch or silently gives garbage.

What changed

Now we stash w_pred next to OpKer in the cache dict and compare it before reusing. If the grid changed, we recompute. Thats it.

Test

Added test_predict_different_w_pred -- calls predict twice on the same model with two different grids and checks both reconstructions land within the error band. Without the fix this crashes immediately.

_maybe_prepare_inference never checked if w_pred changed between
calls, so calling predict() a second time with a different grid
would reuse the cached OpKer from the first call and blow up or
give wrong results.

Now we store w_pred alongside OpKer and compare before reusing.
Copy link
Copy Markdown
Owner

@JonasTurnwald JonasTurnwald left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another important catch, thanks a lot! Interesting that this hasn’t come up before.

@JonasTurnwald JonasTurnwald merged commit 990aff4 into JonasTurnwald:main Apr 14, 2026
3 checks passed
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.

2 participants