class_docstr: Test for cases in which enable_check_numerics() catches infs or nans.
class_docstr_tok: ["Test","for","cases","in","which","enable_check_numerics","(",")","catches","infs","or","nans","."]
docstr: Test catching Infinity caused by overflow in a tf.function with while.
docstr_tok: ["Test","catching","Infinity","caused","by","overflow","in","a","tf.function","with","while","."]
code: def testOverflowInTfFunction(self):
      """Test catching Infinity caused by overflow in a tf.function with while."""
      check_numerics_callback.enable_check_numerics()
  
      @def_function.function
      def accumulation_function(counter, lim, accum):
          while math_ops.less(counter, lim):
              accum.assign(accum * 2.0)
              counter.assign_add(1)
          return 1
  
      counter = variables.Variable(0, dtype=dtypes.int32)
      # Repeated `* 2.0` overflows a float32 tensor in 128 steps. So the
      # 1000-step limit is sufficient.
      lim = constant_op.constant(1000, dtype=dtypes.int32)
      accum = variables.Variable(1.0)
  
      if not context.executing_eagerly():
          self.evaluate([counter.initializer, accum.initializer])
  
      message = self._assertRaisesInvalidArgumentErrorAndGetMessage(
          lambda: self.evaluate(
              accumulation_function(counter, lim, accum)
          )
      )
  
      self.assertAllClose(self.evaluate(counter), 128)
      # Check the content of the error message.
      # The overflow to +Infinity happens during the `* 2.0` operation.
      self.assertTrue(re.search(r"graph op.*\"Mul\"", message))
      self.assertTrue(re.search(r"dtype.*float32", message))
      self.assertIn("shape: ()\n", message)
      # Check that the correct input op is printed.
      self.assertIn("Input tensors (2):", message)
      # Check that the correct input ops are printed.
      self.assertTrue(
          re.search(r"0:.*Tensor.*ReadVariableOp:0", message)
      )
      self.assertTrue(re.search(r"1:.*Tensor.*mul/y:0", message))
      # Check that the correct line for op creation is printed.
      self.assertTrue(
          re.search(r"Stack trace of op's creation", message)
      )
      self.assertIn("accumulation_function", message)
  
imports: import re
  import numpy
  from tensorflow.python.data.ops import dataset_ops
  from tensorflow.python.debug.lib import check_numerics_callback
  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
  from tensorflow.python.framework import ops
  from tensorflow.python.framework import test_util
  from tensorflow.python.ops import array_grad
  from tensorflow.python.ops import custom_gradient
  from tensorflow.python.ops import gen_nn_ops
  from tensorflow.python.ops import gradient_checker_v2
  from tensorflow.python.ops import math_grad
  from tensorflow.python.ops import math_ops
  from tensorflow.python.ops import variables
  from tensorflow.python.platform import googletest
  from tensorflow.python.platform import test
label_desc: Use image node_id 9 for calling the CheckNumericsCallbackUnhealthyTest obj's underlying member method code with example usage: obj.testOverflowInTfFunction() and returns: int