feat: Add batched tensor expansion utilities for O(S) ensemble unroll… - #489
feat: Add batched tensor expansion utilities for O(S) ensemble unroll…#489Debadri-das wants to merge 4 commits into
Conversation
Sir-Sloth-The-Lazy
left a comment
There was a problem hiding this comment.
Just a heads up you missed the case when S == T . Love the idea !
Sir-Sloth-The-Lazy
left a comment
There was a problem hiding this comment.
is this intentional ?
|
linking #226 and #332 here, to remember the potential connection. this is one for @joeloskarsson to review. |
|
|
@Debadri-das would you be working on this further ? It would be really great if you do or I can take over if you have other work going on. I have this as a dependency for my work :) Hope to hear from you soon ! |
joeloskarsson
left a comment
There was a problem hiding this comment.
Thanks, added a couple thoughts. Do also check out @Sir-Sloth-The-Lazy's comment, as he is eager to make use of these methods.
| # e.g., [b1, b2] -> [b1, b1, b2, b2] | ||
| return tensor.repeat_interleave(n_members, dim=0) | ||
|
|
||
| def fold_ensemble_batch(tensor: torch.Tensor, n_members: int) -> torch.Tensor: |
There was a problem hiding this comment.
It seems like it would be useful for this to be able to take either the number of members or the batch size (and if both are given, check that their product is size of first dim).
| The input expands (B,...) -> (B * n_members,...) if it lacks an ensemble dimension | ||
| (such as beginning states or circumstances shared by all members). | ||
| It flattens the input to (B * S,...) if it already has an ensemble | ||
| dimension (for example, perturbed LBCs with shape (B, S,...)). | ||
|
|
There was a problem hiding this comment.
I think it would be better to have two separate methods for this, as these are two very different things (repeating the same state vs just a reshape). If there is a lot of repeated code add a supporting helper function to avoid this.
There was a problem hiding this comment.
Happy to discuss if you see a good argument for this being one function!
There was a problem hiding this comment.
The added methods here do something very close to expand_to_batch
neural-lam/neural_lam/models/step_predictors/base.py
Lines 86 to 103 in a208ada
and it seems suboptimal that these methods are in different places in the codebase. Actually,
expand_to_batch seems like it could just as well sit in utils.py, and there is no reason why it has to be a class method. I think it would make sense to in this PR also move that to utils, to have all batch-dim reordering/reshaping functions in the same place.
|
Closing this in favour of #649, which consolidates the probabilistic metrics + ensemble plumbing track for v0.8.0. The |
Describe your changes
This PR introduces a standalone batch-expanding PyTorch utility (
expand_ensemble_batchandfold_ensemble_batch) to optimize tensor manipulation for Ensemble Lateral Boundary Conditions (LBCs) and probabilistic forecasting variables.Motivation and Context: Currently, probabilistic processing of$S$ ensemble members often relies on sequential $O(S)$ sequential bottlenecks, these functions leverage
forloops. To support the upcoming model refactoring and probabilistic capabilities without introducingtorch.repeat_interleave()and.view()to dynamically flatten state tensors(B, ...) -> (B * S, ...). This enables optimized parallelized operations over the GPU without permanently mutating standard AR model dimensions.Dependencies: None. Built strictly with standard PyTorch tensor primitives.
Issue Link
#49 and #62
Type of change
Checklist before requesting a review
Checklist for reviewers
Each PR comes with its own improvements and flaws. The reviewer should check the following:
Author checklist after completed review
Checklist for assignee