code: def test_quantized_fc():
      def check_quantized_fc(
          data_shape, num_hidden, use_bias, qdtype, flatten=True
      ):
          if is_test_for_native_cpu():
              hasMKL = False
              for key in os.environ.keys():
                  if operator.eq(key, "BUILD_TAG"):
                      if os.environ["BUILD_TAG"].find("MKL") != -1:
                          hasMKL = True
                      break
              if hasMKL == False:
                  print(
                      "skipped testing quantized_fc on cpu since s8u8s32 is only supported by MKL BLAS library"
                  )
                  return
          elif qdtype == "uint8" and is_test_for_gpu():
              print(
                  "skipped testing quantized_fc for gpu uint8 since it is not supported yet"
              )
              return
  
          def maxabs(a, b):
              return mx.np.maximum(mx.np.abs(a), mx.np.abs(b))
  
          int8_range = 127.0
          if qdtype == "uint8":
              data_low = 0.0
              data_high = 63.0
              quantized_range = 255.0
          else:
              data_low = -63.0
              data_high = 63.0
              quantized_range = 127.0
  
          data = (
              mx.np.random.uniform(
                  low=data_low, high=data_high, size=data_shape
              )
              .astype("int32")
              .astype("float32")
          )
          fc_fp32 = mx.gluon.nn.Dense(
              units=num_hidden, use_bias=use_bias, flatten=flatten
          )
          fc_fp32.initialize()
          fc_fp32(data)
          npx.waitall()
          fp32_params = fc_fp32.collect_params()
          weight_shape = fp32_params["weight"].shape
  
          new_args = dict()
          new_args["weight"] = (
              mx.np.random.uniform(
                  low=data_low,
                  high=data_high,
                  size=fp32_params["weight"].shape,
              )
              .astype("int32")
              .astype("float32")
          )
          data_min = mx.np.min(data).astype("float32")
          data_max = mx.np.max(data).astype("float32")
          weight_min = mx.np.min(new_args["weight"]).astype("float32")
          weight_max = mx.np.max(new_args["weight"]).astype("float32")
          data_range = maxabs(data_min, data_max)
          weight_range = maxabs(weight_min, weight_max)
  
          if use_bias:
              bias = (
                  mx.np.random.uniform(
                      low=data_low,
                      high=data_high,
                      size=fp32_params["bias"].shape,
                  )
                  .astype("int32")
                  .astype("float32")
              )
              bias_min = mx.np.min(bias).astype("float32")
              bias_max = mx.np.max(bias).astype("float32")
              bias_range = maxabs(bias_min, bias_max)
  
              bias_scale = int8_range / bias_range
              data_scale = quantized_range / data_range
              weight_scale = int8_range / weight_range
              bias_int32_rescale = (
                  data_scale * weight_scale / bias_scale
              )
              new_bias = bias.astype("float32") * bias_int32_rescale
              new_args["bias"] = new_bias.astype("int32").astype(
                  "float32"
              )
  
          fc_fp32.load_dict(
              new_args, cast_dtype=True, dtype_source="saved"
          )
          output = fc_fp32(data)
  
          class QuantFC(mx.gluon.nn.HybridBlock):
              def __init__(
                  self, num_hidden, use_bias, flatten, **kwargs
              ):
                  super(QuantFC, self).__init__(**kwargs)
                  self.use_bias = use_bias
                  self._kwargs = {
                      "num_hidden": num_hidden,
                      "no_bias": not use_bias,
                      "flatten": flatten,
                  }
  
                  self.min_data = mx.gluon.Parameter(
                      "min_data",
                      dtype="float32",
                      shape=(1),
                      allow_deferred_init=True,
                  )
                  self.max_data = mx.gluon.Parameter(
                      "max_data",
                      dtype="float32",
                      shape=(1),
                      allow_deferred_init=True,
                  )
  
                  self.weight = mx.gluon.Parameter(
                      "weight",
                      dtype="int8",
                      shape=weight_shape,
                      allow_deferred_init=True,
                  )
                  self.min_weight = mx.gluon.Parameter(
                      "min_weight",
                      dtype="float32",
                      shape=(1),
                      allow_deferred_init=True,
                  )
                  self.max_weight = mx.gluon.Parameter(
                      "max_weight",
                      dtype="float32",
                      shape=(1),
                      allow_deferred_init=True,
                  )
  
                  if use_bias:
                      self.bias = mx.gluon.Parameter(
                          "bias",
                          dtype="int8",
                          shape=(num_hidden,),
                          allow_deferred_init=True,
                      )
                      self.min_bias = mx.gluon.Parameter(
                          "min_bias",
                          dtype="float32",
                          shape=(1),
                          allow_deferred_init=True,
                      )
                      self.max_bias = mx.gluon.Parameter(
                          "max_bias",
                          dtype="float32",
                          shape=(1),
                          allow_deferred_init=True,
                      )
  
              def forward(self, x):
                  device = x.device
                  weight = self.weight.data().to_device(device)
                  bias = (
                      self.bias.data().to_device(device)
                      if self.use_bias
                      else None
                  )
                  min_data = self.min_data.data().to_device(device)
                  max_data = self.max_data.data().to_device(device)
                  min_weight = self.min_weight.data().to_device(device)
                  max_weight = self.max_weight.data().to_device(device)
                  min_bias = (
                      self.min_bias.data().to_device(device)
                      if self.use_bias
                      else None
                  )
                  max_bias = (
                      self.max_bias.data().to_device(device)
                      if self.use_bias
                      else None
                  )
                  out = npx.quantized_fully_connected(
                      data=x,
                      weight=weight,
                      bias=bias,
                      min_data=min_data,
                      max_data=max_data,
                      min_weight=min_weight,
                      max_weight=max_weight,
                      min_bias=min_bias,
                      max_bias=max_bias,
                      **self._kwargs,
                  )
                  return out
  
          fc_int8 = QuantFC(
              num_hidden=num_hidden, use_bias=use_bias, flatten=flatten
          )
          qargs = {
              "weight": new_args["weight"].astype("int8"),
              "min_data": mx.np.array([-data_range]),
              "max_data": mx.np.array([data_range]),
              "min_weight": mx.np.array([-weight_range]),
              "max_weight": mx.np.array([weight_range]),
          }
          if use_bias:
              qargs.update(
                  {
                      "bias": bias.astype("int8"),
                      "min_bias": mx.np.array([-bias_range]),
                      "max_bias": mx.np.array([bias_range]),
                  }
              )
  
          fc_int8.load_dict(
              qargs, cast_dtype=True, dtype_source="saved"
          )
  
          qoutput, min_range, max_range = fc_int8(data.astype(qdtype))
  
          if use_bias:
              # with adding bias, accuracy loss should not be greater than one
              diff = mx.np.abs(output - qoutput.astype(output.dtype))
              cond = mx.np.less(2, diff).sum().item()
              assert cond == 0
          else:
              assert_almost_equal(output.asnumpy(), qoutput.asnumpy())
  
      for qdtype in ["int8", "uint8"]:
          if is_test_for_dnnl():
              check_quantized_fc(
                  (32, 512, 2), 100, False, qdtype, flatten=False
              )
              check_quantized_fc(
                  (32, 512, 2), 100, True, qdtype, flatten=False
              )
              check_quantized_fc(
                  (32, 512, 2, 2), 100, False, qdtype, flatten=False
              )
              check_quantized_fc(
                  (32, 512, 2, 2), 100, True, qdtype, flatten=False
              )
          check_quantized_fc((32, 512, 2, 2), 100, False, qdtype)
          check_quantized_fc((32, 111, 2, 2), 100, False, qdtype)
          check_quantized_fc((32, 512, 2, 2), 100, True, qdtype)
          check_quantized_fc((32, 111, 2, 2), 100, True, qdtype)
          check_quantized_fc((256, 2048, 2, 2), 800, True, qdtype)
          check_quantized_fc((256, 111, 2, 2), 800, True, qdtype)
          check_quantized_fc((256, 2048, 2, 2), 800, False, qdtype)
          check_quantized_fc((256, 111, 2, 2), 800, False, qdtype)
  
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 15 for calling a global function with example usage: test_quantized_fc() and returns: mx, out