code: def calibrate_weights(
      model,
      method="percentile",
      perchannel=True,
      percentile=99.99,
      num_bins=2048,
  ):
      """Calibrate weights of all child quantized modules
  
      Ideally, we would split calibration functionality to histogram collector and calibrator which
      takes histogram and compute amax. But since we haven't decoupled collector and calibrator, it
      is easier to create a separate function to calibrate weight.
  
      .. note::
          This function uses `method` specified by the argument to decide which method to use, NOT the one
          specified by the calibrator embedded in weight_quantizer.
          We haven't moved calibration to GPU, so everything is transfered to CPU
  
      Args:
          model: A torch.nn.Module.
          method: A string of calibration method. Supports "mse" and "percentile". Default "percentile"
          perchannel: A bool. Set channel/neuron axis if True. Default True.
          percentile: A float. Default 99.99
          num_bins: A integer. Number of bins of histogram. Default 2048.
  
      """
      for name, module in model.named_modules():
          if hasattr(module, "weight") and hasattr(
              module, "weight_quantizer"
          ):
              logger.info("Calibrate weight of %s", name)
              num_bits = module.weight_quantizer.num_bits
              unsigned = module.weight_quantizer.unsigned
              channel_second_modules = (
                  quant_nn.QuantConvTranspose1d,
                  quant_nn.QuantConvTranspose2d,
                  quant_nn.QuantConvTranspose3d,
              )
              if perchannel:
                  axis = (
                      1
                      if isinstance(module, channel_second_modules)
                      else 0
                  )
              else:
                  axis = None
              axis_size = (
                  module.weight.shape[axis] if axis is not None else 1
              )
  
              # Histogram is always collected even if method is "max". Although "max" is supported here
              # but it is not the primary usage of this function
              if axis is None:
                  calib_hist, calib_bin_edges = np.histogram(
                      module.weight.abs().cpu().detach().numpy(),
                      bins=2048,
                  )
                  calib_hist = [calib_hist]
                  calib_bin_edges = [calib_bin_edges]
              else:
                  calib_hist = []
                  calib_bin_edges = []
                  for i in range(axis_size):
                      hist, bin_edges = np.histogram(
                          module.weight.index_select(
                              axis,
                              torch.tensor(
                                  i, device=module.weight.device
                              ),
                          )
                          .abs()
                          .cpu()
                          .detach()
                          .numpy(),
                          bins=num_bins,
                      )
                      calib_hist.append(hist)
                      calib_bin_edges.append(bin_edges)
  
              calib_amax = []
              if method == "max":
                  reduce_axis = list(range(module.weight.dim()))
                  reduce_axis.remove(axis)
                  calib_amax.append(
                      quant_utils.reduce_amax(
                          module.weight, axis=reduce_axis
                      )
                  )
              elif method == "mse":
                  for i in range(axis_size):
                      calib_amax.append(
                          _compute_amax_mse(
                              calib_hist[i],
                              calib_bin_edges[i],
                              num_bits,
                              unsigned,
                          )
                      )
              elif method == "percentile":
                  for i in range(axis_size):
                      calib_amax.append(
                          _compute_amax_percentile(
                              calib_hist[i],
                              calib_bin_edges[i],
                              percentile,
                          )
                      )
              else:
                  raise TypeError(
                      "Unsupported calibration method {}".format(method)
                  )
  
              if axis is None:
                  calib_amax = calib_amax[0]
              else:
                  calib_amax_shape = [1] * module.weight.dim()
                  calib_amax_shape[axis] = module.weight.shape[axis]
                  calib_amax = torch.stack(calib_amax).reshape(
                      calib_amax_shape
                  )
              module.weight_quantizer.amax = (
                  calib_amax.detach().cpu().numpy()
              )
  
imports: from collections import Counter
  import numpy
  from scipy.stats import entropy
  import logging
  import torch
  from csrc.pytorch.pytorch_quantization.calib.calibrator import _Calibrator
  from csrc.pytorch.pytorch_quantization.tensor_quant import fake_tensor_quant
  from csrc.pytorch.pytorch_quantization import nn
  from csrc.pytorch.pytorch_quantization import utils
label_desc: Use image node_id 4 for calling a global function with example usage: calibrate_weights(model, method, perchannel, percentile, num_bins) without return types