def _try_match_and_transform_pattern(
        self,
        dequantize_op: Operation,
        block: Block,
        config: OpLinearQuantizerConfig,
        visited_ops: set,
    ) -> bool:
        """
        This function performs the pattern match for all target patterns.
        It priorizes longer patterns to shorter ones for more fusions on hardware.
        Reject if the trailing `quantize` and `dequantize` pair already existed.

        A list of valid patterns.
        - conv
        - conv, activation
        - add
        - add, activation
        - pool (max_pool, avg_pool)

        E.g. Identify valid patterns:
        - (`quantize` ->) dequantize` -> `conv`
        - (`quantize` ->) dequantize` -> `conv` -> `relu`
        - (`quantize` ->) dequantize` -> `avg_pool`
        - (`quantize` ->) dequantize` -> `max_pool`
        E.g. Reject if trailing `quantize` -> `dequantize` exist:
        - (`quantize` ->) dequantize` -> `conv` -> `quantize` -> `dequantize`
        - (`quantize` ->) dequantize` -> `conv` -> `relu` -> `quantize` -> `dequantize`
        """

        # Reject if 1st operation is not `conv`/`add`/`pool`.
        SUPPORTED_OP_TYPES = ["conv", "add", "avg_pool", "max_pool"]
        if any(_check_child_op_type(dequantize_op, val) for val in SUPPORTED_OP_TYPES):
            pass
        else:
            return False

        core_op = dequantize_op.outputs[0].child_ops[0]
        last_op = core_op

        # For operations with two inputs, both need to be `dequantize`.
        if core_op.op_type == "add":
            # Check both inputs
            in_var_x = core_op.inputs["x"]
            in_var_y = core_op.inputs["y"]
            in_x_prev_op = in_var_x.op
            in_y_prev_op = in_var_y.op
            if not (in_x_prev_op.op_type == "dequantize" and in_y_prev_op.op_type == "dequantize"):
                return False

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

        # Reject if trailing `quantize` -> `dequantize` pair exist.
        if _check_child_op_type(core_op, "quantize"):
            return False

        _child_op = None
        if len(core_op.outputs[0].child_ops) > 0:
            _child_op = core_op.outputs[0].child_ops[0]

        # Check if 2nd operation is part of a valid pattern.
        # E.g. `dequantize` -> `conv` -> activation -> `quantize`.
        if _child_op is not None:
            if _child_op.op_type in self._allowed_activations:
                if len(_child_op.outputs[0].child_ops) > 0:
                    if _check_child_op_type(_child_op, "quantize"):
                        return False

                    _child_child_op = _child_op.outputs[0].child_ops[0]
                    last_op = _child_op
                    _child_op = _child_child_op

        return self._try_apply_transform(last_op, _child_op, block, visited_ops, op_config)

        This function performs the pattern match for all target patterns.
        It priorizes longer patterns to shorter ones for more fusions on hardware.
        Reject if the trailing `quantize` and `dequantize` pair already existed.

        A list of valid patterns.
        - conv
        - conv, activation
        - add
        - add, activation
        - pool (max_pool, avg_pool)

        E.g. Identify valid patterns:
        - (`quantize` ->) dequantize` -> `conv`
        - (`quantize` ->) dequantize` -> `conv` -> `relu`
        - (`quantize` ->) dequantize` -> `avg_pool`
        - (`quantize` ->) dequantize` -> `max_pool`
        E.g. Reject if trailing `quantize` -> `dequantize` exist:
        - (`quantize` ->) dequantize` -> `conv` -> `quantize` -> `dequantize`
        - (`quantize` ->) dequantize` -> `conv` -> `relu` -> `quantize` -> `dequantize`
        
coremltools/converters/mil/mil/passes/defs/optimize_activation_quantization.py