def transform_op(self, op: Operation):
        """Insert quant-dequant pair for the op."""
        if op.op_type not in self.SUPPORTED_OP_TYPES:
            return

        # Checking op-level config. Skip if we disable compression on certain ops.
        op_config = self.config._get_op_config(op)
        if op_config is None:
            return

        x_is_const = op.inputs["x"].op is not None and op.inputs["x"].op.op_type == "const"
        if x_is_const:
            return  # Input x needs to be non-const.

        new_op_args = dict()
        new_op_args.update(op.inputs.items())
        new_op_args["x"] = self._construct_quant_dequant_op(
            op.inputs["x"], op_config.dtype, op_config.mode
        )
        if op.op_type in self.SUPPORTED_BINARY_OP_TYPES:
            """
            For op with two live inputs (e.g. add):
                Input graph:
                    ... ->|
                          |-> downstream op
                    ... ->|
                Output graph:
                    quantize -> dequantize |
                                           |-> downstream op
                    quantize -> dequantize |
            So we need to quantize y in the same way.
            """
            y_is_const = op.inputs["y"].op is not None and op.inputs["y"].op.op_type == "const"
            if y_is_const:
                return  # Both inputs x and y need to be non-const.
            new_op_args["y"] = self._construct_quant_dequant_op(
                op.inputs["y"], op_config.dtype, op_config.mode
            )

        new_op_args["name"] = op.name
        new_core_op = getattr(mb, op.op_type)(**new_op_args)
        new_core_op.name = op.outputs[0].name

        if new_core_op.op.enclosing_block.try_replace_uses_of_var_after_op(
            old_var=op.outputs[0],
            new_var=new_core_op,
            anchor_op=new_core_op.op,
            end_op=new_core_op,
        ):
            new_core_op.op.enclosing_block.remove_ops([op])
Insert quant-dequant pair for the op.
coremltools/optimize/coreml/_quantization_passes.py