Skip to content

transform_to_unconstrained z-scoring is CPU-only (breaks on MPS) and not in state_dict #1893

Closed

Description

@janfb

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())   # -> False

The 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 cpu

Potential reason

Two issues compound:

  1. mcmc_transform ignores device for bounded priors.** In sbi/utils/sbiutils.py, only the unbounded mean/std branch (prior_mean_std_transform) applies device; the bounded branch is transform = biject_to(prior.support), which builds its tensors on the prior's device (CPU) regardless of the device argument.

  2. The transform is held as a plain attribute, not an nn.Module member. Estimators keep it as e.g. self._prior_transform, which is just a torch.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 uses register_buffer and 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_transform take on device for the bounded branch too (build/move the biject_to transform's tensors onto device).
  • Make estimators follow device moves, e.g. rebuild the transform from a stored prior in an _apply/to override, or register its leaf tensors as buffers, and decide whether state_dict should carry it (vs. relying on reconstruction-then-load_state_dict).

Activity

  1. added
    bug
    on Jun 26, 2026
  2. janfb commented on Jun 26, 2026

    @janfb
    ContributorAuthor

    @BHARATH0153 would you be up for taking on this one as well?

  3. BHARATH0153 commented on Jun 26, 2026

    @BHARATH0153
    Contributor

    @janfb thanks! assign please working on it

    I will make pr by 10pm

  4. added theissue type on Jun 26, 2026
  5. BHARATH0153 commented on Jul 13, 2026

    @BHARATH0153
    Contributor

    @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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions