Skip to content

Avoid parameter-sized float64 temporary in ESMDA.assimilate_batch - #386

Closed
larsevj wants to merge 1 commit into
mainfrom
avoid_float64_upcast_in_assimilate
Closed

Avoid parameter-sized float64 temporary in ESMDA.assimilate_batch#386
larsevj wants to merge 1 commit into
mainfrom
avoid_float64_upcast_in_assimilate

Conversation

@larsevj

@larsevj larsevj commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

No description provided.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 float64 intermediate for float32 parameters.
  • Update ESMDA.assimilate_batch to cast inputs to the parameter dtype prior to np.linalg.multi_dot, so the update is produced in X.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 thread tests/test_esmda.py
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]
@larsevj larsevj closed this Aug 4, 2026
@larsevj
larsevj deleted the avoid_float64_upcast_in_assimilate branch August 4, 2026 13:29
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