sbi icon indicating copy to clipboard operation
sbi copied to clipboard

New `move_distribution_to_device` will fail in most cases

Open manuelgloeckler opened this issue 9 months ago • 7 comments

Description

In #1751 the move_distribution_to_device was introduce as replacement move_all_tensor_to_device, but this function does only cover a small fraction of special cases (i.e. here and will fail on many commonly used distributions:

  • Everything wrapped in torch.distributions.Independent
  • Everything wrapped in MixtureSameFamily
  • ... (likely most custom distributions)

If would recommend the following:

  • reintroduce the move_all_tensors_to_device
  • Add fast paths to certain common special cases. Otherwise fall back to move_all_tensors_to_device

Given that this function is now frequently used in e.g. potential it should cover a broad range of distributions.

manuelgloeckler avatar Feb 16 '26 09:02 manuelgloeckler

Kindly assign this issue to me.

mhusnain-tech avatar Feb 18 '26 17:02 mhusnain-tech

Sure, happy for any PR on this.

I think the following tasks would be required:

  • [ ] reintroduce this method
  • [ ] I think it also would be good to move the base_recursor and helper function to the new location in sbi/utils (from vi_utils).
  • [ ] For the move_distribution_to_device I would keep current fast-paths for common distributions (i.e. special cases). However, in general we should fall back to move_all_tensors_to_device

manuelgloeckler avatar Feb 19 '26 08:02 manuelgloeckler

@mhusnain-tech do you want to work on this?

janfb avatar Feb 20 '26 06:02 janfb

Yes, please!

On Fri, 20 Feb 2026 at 11:30, Jan Teusen (né Boelts) < @.***> wrote:

janfb left a comment (sbi-dev/sbi#1766) https://github.com/sbi-dev/sbi/issues/1766#issuecomment-3931943276

@mhusnain-tech https://github.com/mhusnain-tech do you want to work on this?

— Reply to this email directly, view it on GitHub https://github.com/sbi-dev/sbi/issues/1766#issuecomment-3931943276, or unsubscribe https://github.com/notifications/unsubscribe-auth/B5ML3XNPKZO7URTVJ7TUPET4M2SXVAVCNFSM6AAAAACVIJG5OWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTSMZRHE2DGMRXGY . You are receiving this because you were mentioned.Message ID: @.***>

mhusnain-tech avatar Feb 20 '26 14:02 mhusnain-tech

Hi @manuelgloeckler , I see this is assigned to @mhusnain-tech, does he have a WIP branch/PR already? If not (or if he's busy) I can open a draft PR implementing the approach I have written below:-

So I've looked into the codebase and here's my understanding of the fix:

  1. _base_recursor already exists in sbi/samplers/vi/vi_utils.py, I'll move it (along with move_all_tensor_to_device) to sbi/utils/ as suggested.
  2. Then update move_distribution_to_device to keep the existing fast paths for common distributions, but fall back to the recursive move_all_tensor_to_device for anything else (like Independent, MixtureSameFamily, custom dists).
  3. Update vi_utils.py to import from the new location instead of defining its own copy.

Does this approach look right? Happy to coordinate so we don’t duplicate work.

XBastille avatar Feb 25 '26 21:02 XBastille

Hey @XBastille ,

yes looks right! As this should be done before the next release, feel free to go ahead :)

Kind regards, Manuel

manuelgloeckler avatar Feb 26 '26 09:02 manuelgloeckler

Hi @manuelgloeckler , I have opened a PR #1785 , let me know if there's any change required there!!

XBastille avatar Feb 26 '26 18:02 XBastille