code: def _find_scale_by_kl(
      arr, quantized_dtype="int8", num_bins=8001, num_quantized_bins=255
  ):
      """Given a tensor, find the optimal threshold for quantizing it.
      The reference distribution is `q`, and the candidate distribution is `p`.
      `q` is a truncated version of the original distribution.
  
      Ref:
      http://on-demand.gputechconf.com/gtc/2017/presentation/s7310-8-bit-inference-with-tensorrt.pdf
      """
      assert isinstance(arr, np.ndarray)
      min_val = np.min(arr)
      max_val = np.max(arr)
      thres = max(abs(min_val), abs(max_val))
  
      if min_val >= 0 and quantized_dtype in ["uint8"]:
          # We need to move negative bins to positive bins to fit uint8 range.
          num_quantized_bins = num_quantized_bins * 2 + 1
  
      def get_pointer(arr, ctypes_type):
          ptr = arr.ctypes.data_as(ctypes.POINTER(ctypes_type))
          return ctypes.cast(ptr, ctypes.c_void_p)
  
      hist, hist_edges = np.histogram(
          arr, bins=num_bins, range=(-thres, thres)
      )
      hist_ptr = get_pointer(hist.astype(np.int32), ctypes.c_int)
      hist_edges_ptr = get_pointer(hist_edges, ctypes.c_float)
  
      return _quantize.FindScaleByKLMinimization(
          hist_ptr, hist_edges_ptr, num_bins, num_quantized_bins
      )
  
imports: import ctypes
  import numpy
  from .None import _quantize
label_desc: Use image node_id 1 for calling a global function with example usage: _find_scale_by_kl(arr, quantized_dtype, num_bins, num_quantized_bins) and returns: _quantize, ctypes