code: def upper_tail(distribution, tail_mass):
      """Approximates upper tail quantile for range coding.
  
      For range coding of random variables, the distribution tails need special
      handling, because range coding can only handle alphabets with a finite
      number of symbols. This method returns a cut-off location for the upper
      tail, such that approximately `tail_mass` probability mass is contained in
      the tails (together). The tails are then handled by using the 'overflow'
      functionality of the range coder implementation (using an Elias gamma code).
  
      Args:
        distribution: A `tfp.distributions.Distribution` object.
        tail_mass: Float between 0 and 1. Desired probability mass for the tails.
  
      Returns:
        A `tf.Tensor` broadcastable to shape `self.batch_shape` containing the
        approximate upper tail quantiles for each scalar distribution.
      """
      try:
          tail = distribution._upper_tail(
              tail_mass
          )  # pylint:disable=protected-access
      except (AttributeError, NotImplementedError):
          try:
              tail = distribution.quantile(1 - tail_mass / 2)
          except NotImplementedError:
              try:
                  target = tf.math.log(
                      tf.cast(tail_mass / 2, distribution.dtype)
                  )
                  tail = estimate_tails(
                      distribution.log_survival_function,
                      target,
                      distribution.batch_shape_tensor(),
                      distribution.dtype,
                  )
              except NotImplementedError:
                  raise NotImplementedError(
                      "`distribution` must implement `_upper_tail()`, `quantile()`, or "
                      "`log_survival_function()` so that upper tail can be located."
                  )
      return tf.stop_gradient(tail)
  
imports: import tensorflow
label_desc: Use image node_id 4 for calling a global function with example usage: upper_tail(distribution, tail_mass) and returns: tf