code: def test_requantize_int32_to_int8():
      def quantized_int32_to_float(qdata, min_range, max_range):
          assert qdata.dtype == "int32"
          quantized_range = onp.iinfo("int32").max
          real_range = onp.maximum(
              onp.abs(min_range), onp.abs(max_range)
          )
          scale = float(real_range) / float(quantized_range)
          return qdata.astype("float32") * scale
  
      def float_to_quantized_int8(data, min_range, max_range):
          assert data.dtype == "float32"
          real_range = onp.maximum(
              onp.abs(min_range), onp.abs(max_range)
          )
          quantized_range = onp.iinfo("int8").max
          scale = float(quantized_range) / float(real_range)
          return (
              onp.sign(data)
              * onp.minimum(
                  onp.abs(data) * scale + 0.5, quantized_range
              )
          ).astype("int8")
  
      def requantize(qdata, min_data, max_data, real_range):
          data = quantized_int32_to_float(qdata, min_data, max_data)
          output = float_to_quantized_int8(
              data, -real_range, real_range
          )
          return output, -real_range, real_range
  
      def requantize_baseline(
          qdata,
          min_data,
          max_data,
          min_calib_range=None,
          max_calib_range=None,
      ):
          if (
              min_calib_range is not None
              and max_calib_range is not None
          ):
              real_range = onp.maximum(
                  onp.abs(min_calib_range), onp.abs(max_calib_range)
              )
              return requantize(qdata, min_data, max_data, real_range)
          else:
              min_range = quantized_int32_to_float(
                  onp.min(qdata), min_data, max_data
              )
              max_range = quantized_int32_to_float(
                  onp.max(qdata), min_data, max_data
              )
              return requantize(
                  qdata,
                  min_data,
                  max_data,
                  onp.maximum(onp.abs(min_range), onp.abs(max_range)),
              )
  
      def check_requantize(
          shape, min_calib_range=None, max_calib_range=None
      ):
          qdata = mx.nd.random.uniform(
              low=-1000.0, high=1000.0, shape=shape
          ).astype("int32")
          min_range = mx.nd.array([-1010.0])
          max_range = mx.nd.array([1020.0])
          if min_calib_range is None or max_calib_range is None:
              (
                  qdata_int8,
                  min_output,
                  max_output,
              ) = mx.nd.contrib.requantize(qdata, min_range, max_range)
          else:
              (
                  qdata_int8,
                  min_output,
                  max_output,
              ) = mx.nd.contrib.requantize(
                  qdata,
                  min_range,
                  max_range,
                  min_calib_range=min_calib_range,
                  max_calib_range=max_calib_range,
              )
  
          (
              qdata_int8_np,
              min_output_np,
              max_output_np,
          ) = requantize_baseline(
              qdata.asnumpy(),
              min_range.asscalar(),
              max_range.asscalar(),
              min_calib_range=min_calib_range,
              max_calib_range=max_calib_range,
          )
          assert_almost_equal(
              qdata_int8.asnumpy(), qdata_int8_np, atol=1
          )
          assert_almost_equal(
              min_output.asnumpy(), onp.array([min_output_np])
          )
          assert_almost_equal(
              max_output.asnumpy(), onp.array([max_output_np])
          )
  
      @use_np
      def check_requantize_with_gluon(
          shape, min_calib_range=None, max_calib_range=None
      ):
          qdata = mx.np.random.uniform(
              low=-1000.0, high=1000.0, size=shape
          ).astype("int32")
          min_range = mx.np.array([-1010.0])
          max_range = mx.np.array([1020.0])
  
          class RequantizeBlock(mx.gluon.nn.HybridBlock):
              def __init__(
                  self,
                  min_calib_range=None,
                  max_calib_range=None,
                  **kwargs,
              ):
                  super(RequantizeBlock, self).__init__(**kwargs)
                  self.min_calib_range = min_calib_range
                  self.max_calib_range = max_calib_range
  
              def forward(self, x, min_range, max_range):
                  if (
                      self.min_calib_range is not None
                      and self.max_calib_range is not None
                  ):
                      out = npx.requantize(
                          x,
                          min_range,
                          max_range,
                          min_calib_range=self.min_calib_range,
                          max_calib_range=self.max_calib_range,
                      )
                  else:
                      out = npx.requantize(x, min_range, max_range)
                  return out
  
          requant = RequantizeBlock(
              min_calib_range, max_calib_range
          )  # m*_calib_ranges can be None
          qdata_int8, min_output, max_output = requant(
              qdata, min_range, max_range
          )
  
          (
              qdata_int8_np,
              min_output_np,
              max_output_np,
          ) = requantize_baseline(
              qdata.asnumpy(),
              min_range.item(),
              max_range.item(),
              min_calib_range=min_calib_range,
              max_calib_range=max_calib_range,
          )
          assert_almost_equal(
              qdata_int8.asnumpy(), qdata_int8_np, atol=1
          )
          assert_almost_equal(
              min_output.asnumpy(), onp.array([min_output_np])
          )
          assert_almost_equal(
              max_output.asnumpy(), onp.array([max_output_np])
          )
  
      # test with gluon API.
      check_requantize_with_gluon((3, 4, 10, 10))
      check_requantize_with_gluon((32, 3, 23, 23))
      check_requantize_with_gluon(
          (3, 4, 10, 10),
          min_calib_range=-1050.0,
          max_calib_range=1040.0,
      )
      check_requantize_with_gluon(
          (32, 3, 23, 23),
          min_calib_range=-134.349,
          max_calib_range=523.43,
      )
      # Test with nd array API
      check_requantize((3, 4, 10, 10))
      check_requantize((32, 3, 23, 23))
      check_requantize(
          (3, 4, 10, 10),
          min_calib_range=-1050.0,
          max_calib_range=1040.0,
      )
      check_requantize(
          (32, 3, 23, 23),
          min_calib_range=-134.349,
          max_calib_range=523.43,
      )
  
imports: import os
  import mxnet
  import numpy
  from mxnet import npx
  from mxnet.util import use_np
  from mxnet.gluon.model_zoo import vision
  from mxnet.test_utils import assert_almost_equal, assert_exception, rand_ndarray, rand_shape_nd, same, DummyIter
  from common import xfail_when_nonstandard_decimal_separator
  from mxnet.io import NDArrayIter
  import unittest
  import operator
label_desc: Use image node_id 9 for calling a global function with example usage: test_requantize_int32_to_int8() and returns: unknown, unknown, output,  unknown,  real_range, requantize, requantize, out