code: def truthy(x):
      """Normalizes Tensor ranks for use in `if` conditions.
  
      This enables dry-runs of programs with control flow.  Usage: Program the
      conditions of `if` statements and `while` loops to have a batch dimension, and
      then wrap them with this function.  Example:
      ```python
      ctx = frontend.Context
      truthy = frontend.truthy
  
      @ctx.batch(type_inference=...)
      def my_abs(x):
        if truthy(x > 0):
          return x
        else:
          return -x
  
      my_abs([-5], dry_run=True)
      # returns [5] in Eager mode
      ```
  
      This is necessary because auto-batched programs still have a leading batch
      dimension (of size 1) even in dry-run mode, and a Tensor of shape [1] is not
      acceptable as the condition to an `if` or `while`.  However, the leading
      dimension is critical during batched execution; so conditions of ifs need to
      have rank 1 if running batched and rank 0 if running unbatched (i.e.,
      dry-run).  The `truthy` function arranges for this be happen (by detecting
      whether it is in dry-run mode or not).
  
      If you missed a spot where you should have used `truthy`, the error message
      will say `Non-scalar tensor <Tensor ...> cannot be converted to boolean.`
  
      Args:
        x: A Tensor.
  
      Returns:
        x: The Tensor `x` if we are in batch mode, or if the shape of `x` is
          anything other than `[1]`.  Otherwise returns the single scalar in `x` as
          a Tensor of scalar shape (which is acceptable in the conditions of `if`
          and `while` statements.
      """
      if (
          ab_type_inference.is_inferring()
          or vm.is_staging()
          or st.is_running()
      ):
          return x
      elif isinstance(x, tf.Tensor):
          if x.shape == [1]:
              return x[0]
          else:
              return x
      else:
          return x
  
imports: import contextlib
  import functools
  import gast
  import six
  import tensorflow.compat.v2
  from tensorflow_probability.python.experimental.auto_batching import allocation_strategy
  from tensorflow_probability.python.experimental.auto_batching import dsl
  from tensorflow_probability.python.experimental.auto_batching import gast_util
  from tensorflow_probability.python.experimental.auto_batching import instructions
  from tensorflow_probability.python.experimental.auto_batching import lowering
  from tensorflow_probability.python.experimental.auto_batching import stack_optimization
  from tensorflow_probability.python.experimental.auto_batching import stackless
  from tensorflow_probability.python.experimental.auto_batching import tf_backend
  from tensorflow_probability.python.experimental.auto_batching import type_inference
  from tensorflow_probability.python.experimental.auto_batching import virtual_machine
  from tensorflow.python.autograph.converters import break_statements
  from tensorflow.python.autograph.converters import continue_statements
  from tensorflow.python.autograph.converters import return_statements
  from tensorflow.python.autograph.core import converter
  from tensorflow.python.autograph.pyct import anno
  from tensorflow.python.autograph.pyct import inspect_utils
  from tensorflow.python.autograph.pyct import parser
  from tensorflow.python.autograph.pyct import qual_names
  from tensorflow.python.autograph.pyct import templates
  from tensorflow.python.autograph.pyct import transformer
  from tensorflow.python.autograph.pyct.common_transformers import anf
  from tensorflow.python.autograph.pyct.static_analysis import activity
label_desc: Use image node_id 4 for calling a global function with example usage: truthy(x) and returns: x, x, x, x