code: def _maybe_build_joint_distribution(structure_of_distributions):
      """Turns a (potentially nested) structure of dists into a single dist."""
      # Base case: if we already have a Distribution, return it.
      if dist_util.is_distribution_instance(structure_of_distributions):
          return structure_of_distributions
  
      # Otherwise, recursively convert all interior nested structures into JDs.
      outer_structure = tf.nest.map_structure(
          _maybe_build_joint_distribution, structure_of_distributions
      )
      if hasattr(outer_structure, "_asdict") or isinstance(
          outer_structure, collections.abc.Mapping
      ):
          return joint_distribution_named.JointDistributionNamed(
              outer_structure
          )
      else:
          return (
              joint_distribution_sequential.JointDistributionSequential(
                  outer_structure
              )
          )
  
imports: import collections
  import contextlib
  import functools
  import tensorflow.compat.v2
  from tensorflow_probability.python.bijectors import invert
  from tensorflow_probability.python.distributions import distribution
  from tensorflow_probability.python.distributions import independent
  from tensorflow_probability.python.distributions import joint_distribution_named
  from tensorflow_probability.python.distributions import joint_distribution_sequential
  from tensorflow_probability.python.distributions import normal
  from tensorflow_probability.python.distributions import transformed_distribution
  from tensorflow_probability.python.experimental.mcmc.particle_filter import infer_trajectories
  from tensorflow_probability.python.internal import distribution_util
  from tensorflow_probability.python.internal import prefer_static
  from tensorflow_probability.python.internal import reparameterization
  from tensorflow_probability.python.internal import samplers
  from tensorflow_probability.python.internal import tensorshape_util
label_desc: Use image node_id 2 for calling a global function with example usage: _maybe_build_joint_distribution(structure_of_distributions) and returns: structure_of_distributions, joint_distribution_named, joint_distribution_sequential