code: def _log_cosh_impl(x):
      """Body of numerically stable log_cosh."""
      # log(cosh(x)) = log(e^x + e^-x) - log(2).
      # For x > 0, we can rewrite this as x + log(1 + e^(-2 * x)) - log(2).
      # The second term will be small when x is large, so we don't get any large
      # cancellations.
      # Similarly for x < 0, we can rewrite the expression as -x + log(1 + e^(2 *
      # x)) - log(2)
      # This gives us abs(x) + softplus(-2 * abs(x)) - log(2)
  
      # For x close to zero, we can write the taylor series of softplus(
      # -2 * abs(x)) to see that we get;
      # log(2) - abs(x) + x**2 / 2. - x**4 / 12 + x**6 / 45. + O(x**8)
      # We can cancel out terms to get:
      # x ** 2 / 2.  * (1. - x ** 2 / 6) + x ** 6 / 45. + O(x**8)
      # For x < 45 * sixthroot(smallest normal), all higher level terms
      # disappear and we can use the above expression.
      numpy_dtype = dtype_util.as_numpy_dtype(x.dtype)
      abs_x = tf.math.abs(x)
      logcosh = (
          abs_x
          + tf.math.softplus(-2 * abs_x)
          - np.log(2).astype(numpy_dtype)
      )
      bound = 45.0 * np.power(np.finfo(numpy_dtype).tiny, 1 / 6.0)
      return tf.where(
          abs_x <= bound,
          tf.math.exp(
              tf.math.log(abs_x)
              + tf.math.log1p(-tf.square(abs_x) / 6.0)
          ),
          logcosh,
      )
  
imports: import collections
  import functools
  import numpy
  import tensorflow.compat.v2
  from tensorflow_probability.python.internal import custom_gradient
  from tensorflow_probability.python.internal import distribute_lib
  from tensorflow_probability.python.internal import dtype_util
  from tensorflow_probability.python.internal import prefer_static
  from tensorflow_probability.python.internal import tensorshape_util
  from tensorflow_probability.python.internal import variadic_reduce
  from tensorflow_probability.python.math.scan_associative import scan_associative
  from tensorflow.python.util import deprecation
label_desc: Use image node_id 17 for calling a global function with example usage: _log_cosh_impl(x) and returns: tf