code: def sample_with_optional_upper_bound(
      distribution, sample_shape=(), seed=None
  ):
      """Samples from the given distribution with an optional upper bound."""
      sample = distribution.sample(sample_shape=sample_shape, seed=seed)
      if (
          hasattr(distribution, "upper_bound")
          and distribution.upper_bound is not None
      ):
          sample = tf.minimum(sample, distribution.upper_bound)
  
      return sample
  
imports: import tensorflow.compat.v2
  from tensorflow_probability.python.bijectors import invert
  from tensorflow_probability.python.bijectors import square
  from tensorflow_probability.python.distributions import inverse_gamma
  from tensorflow_probability.python.distributions import transformed_distribution
  from tensorflow_probability.python.internal import prefer_static
label_desc: Use image node_id 2 for calling a global function with example usage: sample_with_optional_upper_bound(distribution, sample_shape, seed) and returns: sample