code: def _tensor_quant(
      inputs, amax, num_bits=8, unsigned=False, narrow_range=True
  ):
      """Shared function body between TensorQuantFunction and FakeTensorQuantFunction"""
      # Fine scale, per channel scale will be handled by broadcasting, which could be tricky. Pop a warning.
      if isinstance(amax, torch.Tensor) and inputs.dim() != amax.dim():
          logger.debug(
              "amax %s has different shape than inputs %s. Make sure broadcast works as"
              " expected!",
              amax.size(),
              inputs.size(),
          )
  
      logger.debug(
          "{} bits quantization on shape {} tensor.".format(
              num_bits, inputs.size()
          )
      )
  
      if unsigned:
          if inputs.min() < 0.0:
              raise TypeError(
                  "Negative values encountered in unsigned quantization."
              )
  
      # Computation must be in FP32 to prevent potential over flow.
      if inputs.dtype == torch.half:
          inputs = inputs.float()
      if amax.dtype == torch.half:
          amax = amax.float()
  
      min_amax = amax.min()
      if min_amax < 0:
          raise ValueError("Negative values in amax")
  
      max_bound = torch.tensor(
          (2.0 ** (num_bits - 1)) - 1.0, device=amax.device
      )
      if narrow_range:
          min_bound = -max_bound
      else:
          min_bound = -max_bound - 1
      # scale = max_bound / amax
      scale = amax / max_bound
      if unsigned:
          scale = amax / (max_bound * 2)
  
      epsilon = 1.0 / (1 << 24)
      if (
          min_amax <= epsilon
      ):  # Treat amax smaller than minimum representable of fp16 0
          zero_amax_mask = amax <= epsilon
          scale[
              zero_amax_mask
          ] = 0  # Value quantized with amax=0 should all be 0
  
      # outputs = torch.clamp((inputs / scale).round_(), min_bound, max_bound)
      # (x + 0.5).floor() match the implementation of tensorflow fake_quant
      outputs = inputs / scale
      if unsigned:
          outputs -= (2.0 ** (num_bits - 1)) - 1.0
      outputs = (outputs + 0.5).floor_()
      outputs = torch.clamp(outputs, min_bound, max_bound)
  
      if min_amax <= epsilon:
          scale[
              zero_amax_mask
          ] = 1.0  # Return 1 makes more sense for values quantized to 0 with amax=0
  
      # if input_dtype == torch.half:
      #     outputs = outputs.half()
  
      return outputs, scale
  
imports: import numpy
  import yaml
  import logging
  import torch
  from torch.autograd import Function
label_desc: Use image node_id 1 for calling a global function with example usage: _tensor_quant(inputs, amax, num_bits, unsigned, narrow_range) and returns: outputs,  scale