code: def testCtcLossFp16(self, sparse_labels):
      batch_size = 8
      num_labels = 6
      max_label_length = 5
      num_frames = 12
  
      labels = np.random.randint(
          1, num_labels, [batch_size, max_label_length]
      )
      labels = ops.convert_to_tensor(labels, dtypes.int64)
      fp16_logits = np.random.uniform(
          size=[num_frames, batch_size, num_labels]
      )
      fp16_logits = ops.convert_to_tensor(fp16_logits, dtypes.float16)
      label_length = np.random.randint(
          2, max_label_length, [batch_size]
      )
      label_length = ops.convert_to_tensor(label_length, dtypes.int64)
  
      label_mask = array_ops.sequence_mask(
          label_length,
          maxlen=max_label_length,
          dtype=label_length.dtype,
      )
      labels *= label_mask
      logit_length = [num_frames] * batch_size
  
      fp16_loss, fp16_grad = _ctc_loss_v3(
          labels,
          fp16_logits,
          label_length,
          logit_length,
          use_gpu=True,
          sparse=sparse_labels,
      )
      fp32_loss, fp32_grad = _ctc_loss_v3(
          labels,
          math_ops.cast(fp16_logits, dtypes.float32),
          label_length,
          logit_length,
          use_gpu=True,
          sparse=sparse_labels,
      )
  
      self.assertEqual(fp16_loss.dtype, dtypes.float16)
      self.assertEqual(fp16_grad.dtype, dtypes.float16)
      self.assertAllClose(
          self.evaluate(fp16_loss),
          self.evaluate(math_ops.cast(fp32_loss, dtypes.float16)),
      )
      self.assertAllClose(
          self.evaluate(fp16_grad),
          self.evaluate(math_ops.cast(fp32_grad, dtypes.float16)),
      )
  
imports: from absl.testing import parameterized
  import numpy
  from tensorflow.python.eager import backprop
  from tensorflow.python.eager import context
  from tensorflow.python.eager import def_function
  from tensorflow.python.framework import constant_op
  from tensorflow.python.framework import dtypes
  from tensorflow.python.framework import errors_impl
  from tensorflow.python.framework import ops
  from tensorflow.python.framework import random_seed
  from tensorflow.python.framework import sparse_tensor
  from tensorflow.python.framework import tensor_spec
  from tensorflow.python.framework import test_util
  from tensorflow.python.ops import array_ops
  from tensorflow.python.ops import ctc_ops
  from tensorflow.python.ops import gradients_impl
  from tensorflow.python.ops import math_ops
  from tensorflow.python.ops import random_ops
  from tensorflow.python.ops import sparse_ops
  from tensorflow.python.platform import test
label_desc: Use image node_id 2 for calling the CTCLossTestV3 obj's underlying member method code with example usage: obj.testCtcLossFp16(sparse_labels) without return types