code: def _to_pins(dist, *args, **kwargs):
      """Converts *args and **kwargs to a dict of pins.
  
      Args:
        dist: JointDistribution*-like object with _flat_resolve_names(), dtype, and
          _model_flatten(x).
        *args: Either a sequence of pins that aligns with `_model_flatten` and
          `_flat_resolve_names`, or a single item sequence where `args[0]`
          structure is compatible with `dist.dtype`.
        **kwargs: Named pins with keys corresponding to part names resolved by
          `_flat_resolve_names()`.
  
      Returns:
        pins: Python dict mapping resolved names to pinned values.
      """
      dtypes = dist._model_flatten(dist.dtype)
      if not dtypes:
          return {}
      forbid_sequences = isinstance(
          dist.dtype, dict
      ) and not isinstance(dist.dtype, collections.OrderedDict)
      if bool(args) == bool(kwargs):
          raise ValueError(
              "Exactly one of *args or **kwargs should be set."
          )
      struct0 = dtypes[0]
      if len(args) == 1:
          # We can interpret a single-element *args as either a value (matching the
          # structure of dist.dtype [perhaps partially]), or a first-item pin.
          try:
              tf.nest.assert_same_structure(args[0], struct0)
          except (ValueError, TypeError):
              args = args[0]
              if isinstance(args, dict):
                  kwargs = args
              elif hasattr(args, "_asdict"):
                  kwargs = args._asdict()
      if forbid_sequences and not kwargs:
          raise ValueError(
              "Must provide named pins for unordered dict model type."
          )
      names = dist._flat_resolve_names()
      dtypes_by_name = dict(zip(names, dtypes))
  
      def convert_to_tensors(xs, dtype):
          def convert_with_hint(x, dtype):
              return tensor_util.convert_nonref_to_tensor(
                  x, dtype_hint=dtype
              )
  
          return nest.map_structure_up_to(
              dtype,  # shallow_tree
              convert_with_hint,  # func
              xs,  # x
              dtype,
          )  # dtype
  
      if kwargs:
          unexpected = set(kwargs.keys()) - set(names)
          if unexpected:
              raise ValueError(
                  "Unexpected parameters: {} (allowed: {})".format(
                      sorted(unexpected), names
                  )
              )
          return {
              k: convert_to_tensors(v, dtypes_by_name[k])
              for k, v in kwargs.items()
              if v is not None
          }
      else:
          if len(args) > len(names):
              raise ValueError(
                  "Too many pinned values: {} (allowed: {})".format(
                      len(args), len(names)
                  )
              )
          return {
              k: convert_to_tensors(v, dtypes_by_name[k])
              for k, v in zip(names, args)
              if v is not None
          }
  
imports: import collections
  import tensorflow.compat.v2
  from tensorflow_probability.python.distributions import distribution
  from tensorflow_probability.python.distributions import joint_distribution
  from tensorflow_probability.python.internal import docstring_util
  from tensorflow_probability.python.internal import structural_tuple
  from tensorflow_probability.python.internal import tensor_util
  from tensorflow_probability.python.internal import tensorshape_util
  from tensorflow_probability.python.internal import vectorization_util
  from tensorflow.python.util import nest
label_desc: Use image node_id 1 for calling a global function with example usage: _to_pins(dist) and returns: dict, nest, unknown, unknown, tensor_util