code: def _masked_observe(
      name: str,
      fn: TorchDistributionMixin,
      obs: Optional[torch.Tensor],
      obs_mask: torch.Tensor,
      *args,
      **kwargs,
  ) -> torch.Tensor:
      # Split into two auxiliary sample sites.
      with poutine.mask(mask=obs_mask):
          observed = sample(
              f"{name}_observed", fn, *args, **kwargs, obs=obs
          )
      with poutine.mask(mask=~obs_mask):
          unobserved = sample(f"{name}_unobserved", fn, *args, **kwargs)
  
      # Interleave observed and unobserved events.
      shape = obs_mask.shape + (1,) * fn.event_dim
      batch_mask = obs_mask.reshape(shape)
      try:
          value = torch.where(batch_mask, observed, unobserved)
      except RuntimeError as e:
          if "must match the size of tensor" in str(e):
              shape = torch.broadcast_shapes(
                  observed.shape, unobserved.shape
              )
              batch_shape = shape[: len(shape) - fn.event_dim]
              raise ValueError(
                  f"Invalid obs_mask shape {tuple(obs_mask.shape)}; should be "
                  f"broadcastable to batch_shape = {tuple(batch_shape)}"
              ) from e
          raise
      return deterministic(name, value)
  
imports: import copy
  import warnings
  from collections import OrderedDict
  from contextlib import ExitStack, contextmanager
  from inspect import isclass
  from typing import Callable, Iterator, Optional, Sequence, Union
  import torch
  from torch.distributions import constraints
  import pyro.distributions
  import pyro.infer
  import pyro.poutine
  from pyro.distributions.torch_distribution import TorchDistributionMixin
  from pyro.params import param_with_module_name
  from pyro.params.param_store import ParamStoreDict
  from pyro.poutine.plate_messenger import PlateMessenger
  from pyro.poutine.runtime import _MODULE_NAMESPACE_DIVIDER, _PYRO_PARAM_STORE, InferDict, Message, am_i_wrapped, apply_stack, effectful
  from pyro.poutine.subsample_messenger import SubsampleMessenger
  from pyro.util import deep_getattr, set_rng_seed
label_desc: Use image node_id 4 for calling a global function with example usage: _masked_observe(name, fn, obs, obs_mask) and returns: deterministic