def create_quantize_friendly_weight(
    weight: np.ndarray, nbits: int, signed: bool
) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
    """Create quantize friendly weight by first quantize and then de-quantize the weight."""
    axes = tuple(axis for axis in range(len(weight.shape)) if axis != 0)
    quantized_weight, scale, zero_point = optimize_utils.quantize_weight(
        weight,
        axes,
        nbits,
        signed,
        quantization_mode="LINEAR",
        dtype=np.int8 if signed else np.uint8,
    )
    scale_shape = scale.shape + tuple([1] * len(axes))
    scale = scale.reshape(scale_shape)
    zero_point = zero_point.reshape(scale_shape)
    dequantized_weight = scale * (
        quantized_weight.astype(np.float32) - zero_point.astype(np.float32)
    )
    return dequantized_weight, scale, zero_point
Create quantize friendly weight by first quantize and then de-quantize the weight.
coremltools/test/optimize/coreml/test_post_training_quantization.py