class_docstr: Tests for ops in horovod.tensorflow.
class_docstr_tok: ["Tests","for","ops","in","horovod.tensorflow","."]
docstr: Test on XLA/GPU that the allreduce correctly sums 1D, 2D, 3D tensors
  with postscaling
docstr_tok: ["Test","on","XLA\/GPU","that","the","allreduce","correctly","sums","1D",",","2D",",","3D","tensors","with","postscaling"]
code: def test_horovod_allreduce_gpu_postscale(self):
      """Test on XLA/GPU that the allreduce correctly sums 1D, 2D, 3D tensors
      with postscaling"""
  
      # Only do this test if there are GPUs available.
      if not tf.test.is_gpu_available(cuda_only=True):
          return
  
      if int(os.environ.get("HOROVOD_MIXED_INSTALL", 0)):
          # Skip if compiled with CUDA but without HOROVOD_GPU_ALLREDUCE.
          return
  
      hvd.init()
      size = hvd.size()
  
      def hvd_allreduce_test(self, dtype, dim):
          np.random.seed(1234)
          factor = np.random.uniform()
          tensor = self.random_uniform([17] * dim, -100, 100)
          tensor = tf.cast(tensor, dtype=dtype)
          summed = hvd.allreduce(
              tensor, average=False, postscale_factor=factor
          )
  
          multiplied = tensor * size
          # Scaling done in FP64 math for integer types.
          multiplied = tf.cast(
              multiplied, tf.float64 if dtype in int_types else dtype
          )
          factor = tf.convert_to_tensor(
              factor, tf.float64 if dtype in int_types else dtype
          )
          multiplied = tf.cast(factor * multiplied, dtype)
          difference = summed - multiplied
          difference = (
              tf.cast(difference, tf.int32)
              if dtype == tf.uint8
              else difference
          )
          max_difference = tf.reduce_max(tf.abs(difference))
          return max_difference
  
      local_rank = hvd.local_rank()
      dtypes = self.filter_supported_types(
          [
              tf.uint8,
              tf.int8,
              tf.int32,
              tf.int64,
              tf.float16,
              tf.float32,
          ]
      )
      int_types = [tf.uint8, tf.int8, tf.int32, tf.int64]
      dims = [1, 2, 3]
      for dtype, dim in itertools.product(dtypes, dims):
          with tf.device("/gpu:%s" % local_rank):
              max_difference = tf.function(
                  hvd_allreduce_test, jit_compile=True
              )(self, dtype, dim)
  
          # Threshold for floating point equality depends on number of
          # ranks, since we're comparing against precise multiplication.
          if size <= 3 or dtype in int_types:
              threshold = 0
          elif size < 10:
              threshold = 1e-4
          elif size < 15:
              threshold = 5e-4
          else:
              break
  
          diff = self.evaluate(max_difference)
          self.assertTrue(
              diff <= threshold,
              "hvd.allreduce produces incorrect results",
          )
  
imports: import os
  import sys
  import pytest
  import math
  import numpy
  import itertools
  from packaging import version
  import warnings
  import tensorflow
  from horovod.tensorflow.util import _executing_eagerly
  from tensorflow.python.framework import ops
  import horovod.tensorflow
  from tensorflow.python.framework.test_util import run_all_in_graph_and_eager_modes
label_desc: Use image node_id 7 for calling the XLATests obj's underlying member method code with example usage: obj.test_horovod_allreduce_gpu_postscale() and returns: max_difference