Repository navigation
Description
z_score_theta="transform_to_unconstrained" (and z_score_x=...) only works on CPU. Moving an estimator that uses it to cuda/mps raises a device-mismatch error, and the transform is not part of state_dict(). This affects both the existing Zuko path and the new MDN support from #1888. It's a pre-existing limitation of the feature, surfaced while reviewing #1888.
Reproduction (MPS, but CUDA likely behaves the same)
import torch
from sbi.utils import BoxUniform
from sbi.neural_nets.factory import posterior_nn
prior = BoxUniform(-2 * torch.ones(2), 2 * torch.ones(2))
theta, x = prior.sample((300,)), torch.randn(300, 2)
est = posterior_nn(model="zuko_nsf", z_score_theta="transform_to_unconstrained", x_dist=prior)(theta, x)
est.to("mps").log_prob(theta[:4].to("mps"), x[:4].to("mps"))
# RuntimeError: Expected all tensors to be on the same device, but found at least two devices, mps:0 and cpu!New MDN path (#1888) behaves identically. And the transform is missing from state_dict:
from sbi.neural_nets.net_builders.mdn import build_mdn
est = build_mdn(theta, x, z_score_x="transform_to_unconstrained", x_dist=prior)
any("prior_transform" in k for k in est.state_dict()) # -> FalseThe transform itself ignores the requested device:
from sbi.utils.sbiutils import mcmc_transform
T = mcmc_transform(BoxUniform(-2 * torch.ones(2), 2 * torch.ones(2)), device="mps")
T(torch.zeros(1, 2, device="mps")) # RuntimeError: ... mps:0 and cpuPotential reason
Two issues compound:
-
mcmc_transformignoresdevicefor bounded priors.** Insbi/utils/sbiutils.py, only the unbounded mean/std branch (prior_mean_std_transform) appliesdevice; the bounded branch istransform = biject_to(prior.support), which builds its tensors on the prior's device (CPU) regardless of thedeviceargument. -
The transform is held as a plain attribute, not an
nn.Modulemember. Estimators keep it as e.g.self._prior_transform, which is just atorch.distributions.Transform.nn.Module._apply(which backs.to()/.cuda()) only moves registered parameters, buffers, and submodules, so the transform's internal tensors stay on the old device (in contrast the affine z-score path, which usesregister_bufferand therefore moves and serializes correctly.)
The issue was pre-existing for zuko's transform_to_unconstrained; #1888 inherits the same behavior for MDN.
Suggested fix
- Make
mcmc_transformtake ondevicefor the bounded branch too (build/move thebiject_totransform's tensors ontodevice). - Make estimators follow device moves, e.g. rebuild the transform from a stored prior in an
_apply/tooverride, or register its leaf tensors as buffers, and decide whetherstate_dictshould carry it (vs. relying on reconstruction-then-load_state_dict).
Activity
janfb commentedon Jun 26, 2026
@BHARATH0153 would you be up for taking on this one as well?
BHARATH0153 commentedon Jun 26, 2026
@janfb thanks! assign please working on it
I will make pr by 10pm
fix: transform_to_unconstrained device and state_dict (sbi-dev#1893)
fix: transform_to_unconstrained device mdn device handling (#1893) (#…
fix: zuko transform_to_unconstrained follows device moves (#1893)
simplify: collapse transform walkers to a single _apply_to_transform (#…
fix: make MDN transform_to_unconstrained picklable (#1893)
fix: make Zuko transform_to_unconstrained picklable (#1893)
harden+docs: guard zuko pickle re-link; document transform persistence (
BHARATH0153 commentedon Jul 13, 2026
@janfb Thanks for the quick follow-up and the cleanup on top of #1896 and merging those tree-walkers into a single _apply_to_transform definitely makes the codebase a lot cleaner.The remaining pickling issues too to completely resolve #1893
docs: trim verbose transform docstrings (#1893 review)
fix: make MDN transform_to_unconstrained picklable (#1893)
fix: make Zuko transform_to_unconstrained picklable (#1893)
harden+docs: guard zuko pickle re-link; document transform persistence (
simplify: mirror MDN pickle pattern for Zuko, drop had_prior_transfor…
fix: only invert inverse prior transforms in MDN pickle (#1893 review)