New `move_distribution_to_device` will fail in most cases
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.
Kindly assign this issue to me.
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_recursorand helper function to the new location insbi/utils(from vi_utils). - [ ] For the
move_distribution_to_deviceI would keep current fast-paths for common distributions (i.e. special cases). However, in general we should fall back tomove_all_tensors_to_device
@mhusnain-tech do you want to work on this?
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: @.***>
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:
_base_recursoralready exists insbi/samplers/vi/vi_utils.py, I'll move it (along withmove_all_tensor_to_device) tosbi/utils/as suggested.- Then update
move_distribution_to_deviceto keep the existing fast paths for common distributions, but fall back to the recursivemove_all_tensor_to_devicefor anything else (likeIndependent,MixtureSameFamily, custom dists). - Update
vi_utils.pyto 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.
Hey @XBastille ,
yes looks right! As this should be done before the next release, feel free to go ahead :)
Kind regards, Manuel
Hi @manuelgloeckler , I have opened a PR #1785 , let me know if there's any change required there!!