def _construct_quant_dequant_op(self, input_var: Var, dtype: types, mode: str) -> Operation:
        """Construct new quant-dequant pairs based on an input var."""
        input_min_max = optimize_utils.get_min_and_max_values(
            self._activation_stats, var_name=input_var.name
        )
        # Numerically the scale and zero point won't change if the input array only have two elements:
        # the min and max values of the input array. That's the trick to re-use the util function.
        _, scale, zero_point = optimize_utils.quantize_weight_by_dtype(
            input_min_max,
            axes=0,
            dtype=dtype,
            quantization_mode=mode,
        )
        scale_dtype = np.float16 if input_var.dtype == types.fp16 else np.float32
        scale = scale.astype(scale_dtype)
        if zero_point is not None:
            zero_point = zero_point.astype(types.nptype_from_builtin(dtype))

        if np.all(scale == 0) and zero_point is None:
            # If scale is unexpectedly set as 0, change it to 1 for non-effect scale.
            scale = np.ones_like(scale)

        new_quantize_op = mb.quantize(
            input=input_var,
            scale=scale,
            zero_point=zero_point,
            output_dtype=types.builtin_to_string(dtype),
        )
        new_dequantize_op = mb.dequantize(
            input=new_quantize_op,
            scale=scale,
            zero_point=zero_point,
        )
        return new_dequantize_op
Construct new quant-dequant pairs based on an input var.
coremltools/optimize/coreml/_quantization_passes.py