Avoid parameter-sized float64 temporary in ESMDA.assimilate_batch - #386
Closed
larsevj wants to merge 1 commit into
Closed
Avoid parameter-sized float64 temporary in ESMDA.assimilate_batch#386larsevj wants to merge 1 commit into
larsevj wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR aims to reduce peak memory usage in ESMDA.assimilate_batch by preventing creation of a parameter-sized float64 temporary when assimilating float32 parameter ensembles.
Changes:
- Add a regression test intended to ensure the assimilation update does not materialize as a parameter-sized
float64intermediate forfloat32parameters. - Update
ESMDA.assimilate_batchto cast inputs to the parameter dtype prior tonp.linalg.multi_dot, so the update is produced inX.dtype.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| tests/test_esmda.py | Adds a test that inspects the dtype of the parameter-sized update produced during assimilation. |
| src/iterative_ensemble_smoother/esmda.py | Changes the update computation to cast factors to X.dtype before multiplying to avoid a parameter-sized float64 temporary. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+426
to
438
| # Cast before multiplication to avoid a parameter-sized temporary | ||
| # in the observation dtype. | ||
| delta_M = self._compute_delta_M(X=X, missing=missing) | ||
| ensemble_update = np.linalg.multi_dot( | ||
| [delta_M, self.delta_DT, self.term_diag, self.termT, self.D_obs_minus_D] | ||
| factors = ( | ||
| delta_M, | ||
| self.delta_DT, | ||
| self.term_diag, | ||
| self.termT, | ||
| self.D_obs_minus_D, | ||
| ) | ||
| X += np.linalg.multi_dot( | ||
| [factor.astype(X.dtype, copy=False) for factor in factors] | ||
| ) |
Comment on lines
+656
to
+668
| real_multi_dot = np.linalg.multi_dot | ||
| update_dtypes = [] | ||
|
|
||
| def spy(arrays, **kwargs): | ||
| result = real_multi_dot(arrays, **kwargs) | ||
| if result.shape == X_prior.shape: | ||
| update_dtypes.append(result.dtype) | ||
| return result | ||
|
|
||
| monkeypatch.setattr(np.linalg, "multi_dot", spy) | ||
| X_posterior = esmda.assimilate_batch(X=X_prior) | ||
|
|
||
| assert update_dtypes == [X_prior.dtype] |
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.
No description provided.