code: def _sample_coalescent_times(leaf_times):
      leaf_times = leaf_times.detach()
      proto = leaf_times
      N = leaf_times.size(-1)
      batch_shape = leaf_times.shape[:-1]
  
      # We don't bother to implement a version that vectorizes over batches;
      # instead we simply sequentially sample and stack.
      if batch_shape:
          flat_leaf_times = leaf_times.reshape(-1, N)
          flat_coal_times = torch.stack(
              list(map(_sample_coalescent_times, flat_leaf_times))
          )
          return flat_coal_times.reshape(batch_shape + (N - 1,))
      assert leaf_times.shape == (N,)
  
      # Sequentially sample coalescent events from latest to earliest.
      leaf_times = leaf_times.sort(
          dim=-1, descending=True
      ).values.tolist()
      coal_times = []
      # Start with the minimum of two active leaves.
      leaf = 1
      t = leaf_times[leaf]
      active = 2
      binomial = active * (active - 1) / 2
      for u in proto.new_empty(N - 1).exponential_().tolist():
          while (
              leaf + 1 < N and u > (t - leaf_times[leaf + 1]) * binomial
          ):
              # Move past the next leaf.
              leaf += 1
              u -= (t - leaf_times[leaf]) * binomial
              t = leaf_times[leaf]
              active += 1
              binomial = active * (active - 1) / 2
          # Add a coalescent event.
          t -= u / binomial
          active -= 1
          binomial = active * (active - 1) / 2
          coal_times.append(t)
      coal_times.reverse()
  
      return proto.new_tensor(coal_times)
  
imports: import functools
  import weakref
  from collections import namedtuple
  import torch
  from torch.distributions import constraints
  from pyro.distributions.util import broadcast_shape, is_validation_enabled
  from pyro.ops.special import safe_log
  from .torch_distribution import TorchDistribution
label_desc: Use image node_id 7 for calling a global function with example usage: _sample_coalescent_times(leaf_times) and returns: proto, flat_coal_times