code: def test_sync_1bit_compression(threshold, nrepeat):
      def check_compr_pull_before_push():
          for k, s in compr_keys_shapes:
              val = mx.nd.ones(s)
              kv.pull(k, val)
              check_diff(val, 0)
          for k, s in compr_init_keys_shapes:
              # tests that GC is not used for init of a key
              val = mx.nd.zeros(s)
              kv.pull(k, val)
              check_diff(val, 1)
  
      def check_compr_ones():
          for k, s in compr_keys_shapes:
              val = mx.nd.zeros(s)
              kv.pull(k, val)
              curr_val = val[0][0].asnumpy()[0]
              kv.push(k, mx.nd.ones(s))
              out = mx.nd.zeros(s)
              kv.pull(k, out=out)
              newval = curr_val + rate * nworker
              check_diff(out, newval)
  
      def check_compr_neg_ones():
          for k, s in compr_keys_shapes:
              val = mx.nd.zeros(s)
              kv.pull(k, val)
              curr_val = val[0][0].asnumpy()[0]
              kv.push(k, -1 * mx.nd.ones(s))
              out = mx.nd.ones(s)
              kv.pull(k, out=out)
              # current value should be zero after call
              # check_compr_ones and check_compr_neg_ones
              check_diff(out, 0)
  
      def check_compr_residual(threshold):
          curr_residual = 0
          curr_val = (
              rate * nworker
              if 2 + curr_residual > threshold
              else -rate * nworker
          )
          for k, s in compr_keys_shapes:
              kv.push(k, 2 * mx.nd.ones(s))
              out = mx.nd.zeros(s)
              kv.pull(k, out)
              check_diff(out, curr_val)
  
          curr_residual = 1 if 2 > threshold else 3
          curr_val += (
              rate * nworker
              if 0 + curr_residual > threshold
              else -rate * nworker
          )
          for k, s in compr_keys_shapes:
              kv.push(k, mx.nd.zeros(s))
              out = mx.nd.zeros(s)
              kv.pull(k, out)
              check_diff(out, curr_val)
  
          curr_residual += -1 if curr_residual > threshold else +1
          curr_val += (
              rate * nworker
              if -2 + curr_residual > threshold
              else -rate * nworker
          )
          for k, s in compr_keys_shapes:
              kv.push(k, -2 * mx.nd.ones(s))
              out = mx.nd.zeros(s)
              kv.pull(k, out)
              check_diff(out, curr_val)
  
      def check_compr_random(threshold, nrepeat):
          # set a seed so all workers generate same data. knowing this helps
          # calculate expected value after pull
          mx.random.seed(123)
          rnd.seed(123)
  
          # use new keys so residual is 0 for calculation of expected
          for k, s in compr_random_keys_shapes:
              kv.init(k, mx.nd.zeros(s))
          for k, s in compr_random_keys_shapes:
              curr_residual = np.zeros(s)
              for _ in range(nrepeat):
                  orig_val = mx.nd.zeros(s)
                  kv.pull(k, orig_val)
  
                  grad = mx.nd.array(rnd.rand(s[0], s[1]))
                  # creates a copy because push changes grad because of assignment
                  grad_cpy = mx.nd.array(grad)
                  kv.push(k, grad)
                  val = mx.nd.zeros(s)
                  kv.pull(k, val)
  
                  diff = val - orig_val
  
                  # compute expected by using simulation of operator
                  (
                      compr,
                      curr_residual,
                      decompr,
                  ) = compute_expected_quantization(
                      grad_cpy, curr_residual, threshold, compute_1bit
                  )
                  decompr *= nworker * rate
                  assert_almost_equal(diff.asnumpy(), decompr)
  
      print(
          "worker "
          + str(my_rank)
          + " started with 1bit compression tests"
      )
      check_compr_pull_before_push()
      check_compr_ones()
      check_compr_neg_ones()
      check_compr_residual(threshold)
      check_compr_random(threshold, nrepeat)
      print(
          "worker "
          + str(my_rank)
          + " is done with 1bit compression tests"
      )
  
file_path: mxnet/tests/nightly/dist_sync_kvstore.py
imports: import sys
  import argparse
  import mxnet
  import numpy
  import numpy.random
  from mxnet.test_utils import assert_almost_equal, assert_exception
  from test_kvstore import compute_expected_quantization, compute_1bit, compute_2bit
label_desc: Use image node_id 6 for calling a global function with example usage: test_sync_1bit_compression(threshold, nrepeat) without return types