code: def flatten_rightmost(ndims=3):
      """Flatten rightmost dims."""
  
      def flatten_rightmost_(x):
          """Implementation of `flatten_rightmost`."""
          leftmost_ndims = ps.rank(x) - ndims
          new_shape = ps.pad(
              ps.shape(x)[:leftmost_ndims],
              paddings=[[0, 1]],
              constant_values=-1,
          )
          y = tf.reshape(x, new_shape)
          if x.shape.ndims is not None:
              d = x.shape[leftmost_ndims:]
              d = np.prod(d) if d.is_fully_defined() else None
              y.set_shape(x.shape[:leftmost_ndims].concatenate(d))
          return y
  
      return flatten_rightmost_
  
imports: import collections
  import contextlib
  import functools
  import sys
  import numpy
  import tensorflow.compat.v2
  from tensorflow_probability.python.internal import dtype_util
  from tensorflow_probability.python.internal import prefer_static
  from tensorflow_probability.python.internal import tensorshape_util
label_desc: Use image node_id 7 for calling a global function with example usage: flatten_rightmost(ndims) and returns: flatten_rightmost_, y