code: def test_clipped_gradients_on_different_losses(
      self, batch_size, l2_norm_clip, is_eager, reduction
  ):
      loss_fn = tf.keras.losses.MeanSquaredError(reduction=reduction)
      self._model.compile(loss=loss_fn, run_eagerly=is_eager)
      x_batch = tf.reshape(
          tf.range(batch_size * self._input_dim, dtype=tf.float32),
          [batch_size, -1],
      )
      y_batch = tf.reshape(
          1.0 + tf.range(batch_size, dtype=tf.float32), [batch_size, -1]
      )
      # Stop early for efficiency.
      if reduction == "none":
          with self.assertRaises(NotImplementedError):
              clip_grads.compute_clipped_gradients_and_outputs(
                  self._model,
                  l2_norm_clip,
                  layer_registry.make_default_layer_registry(),
                  x_batch,
                  y_batch,
              )
          return
      # NOTE: losses from this point are scalar losses.
      with tf.GradientTape() as tape:
          y_pred = self._model(x_batch)
          loss_value = loss_fn(y_pred, y_batch)
      true_grads = tape.gradient(
          loss_value, self._model.trainable_variables
      )
      (
          clipped_grads,
          _,
          _,
      ) = clip_grads.compute_clipped_gradients_and_outputs(
          self._model,
          l2_norm_clip,
          layer_registry.make_default_layer_registry(),
          x_batch,
          y_batch,
      )
  
      # Computes the L2 norm manually.
      def compute_l2_norm(t):
          sqr_sum_fn = lambda x: tf.reduce_sum(tf.square(x))
          return tf.sqrt(tf.add_n(tf.nest.map_structure(sqr_sum_fn, t)))
  
      true_norm = compute_l2_norm(true_grads)
      computed_norm = compute_l2_norm(clipped_grads)
      norm_bound = (
          l2_norm_clip * batch_size
          if reduction == "sum"
          else l2_norm_clip
      )
      if true_norm >= norm_bound:
          # All of the per-example gradient norms should be less than the L2 norm
          # clip value. Hence, by the triangle inequality, the gradient norm of the
          # summed loss (averaged loss) should be less than the clip value times
          # the batch size (just the clip value).
          self.assertLessEqual(computed_norm, norm_bound)
      else:
          self.assertAlmostEqual(computed_norm, true_norm)
  
imports: from collections.abc import Mapping, Sequence
  from typing import Any, Optional
  from absl.testing import parameterized
  import tensorflow
  from tensorflow_privacy.privacy.fast_gradient_clipping import clip_grads
  from tensorflow_privacy.privacy.fast_gradient_clipping import common_test_utils
  from tensorflow_privacy.privacy.fast_gradient_clipping import layer_registry
  from tensorflow_privacy.privacy.fast_gradient_clipping import type_aliases
label_desc: Use image node_id 2 for calling the ComputeClippedGradsAndOutputsTest obj's underlying member method code with example usage: obj.test_clipped_gradients_on_different_losses(batch_size, l2_norm_clip, is_eager, reduction) and returns: tf