def annotate_weighted_mod_pattern(
    model: _torch.fx.GraphModule,
    pattern_gm: _torch.fx.GraphModule,
    quantization_config: _Optional[_AnnotationConfig],
    filter_fn: _Optional[_Callable[[_Node], bool]],
) -> _Optional[_List[_List[_Node]]]:
    """
    Annotates all nodes in ``model``, which match the pattern specified by ``pattern_gm``
    using ``quantization_config``.

    ``pattern_gm`` captures patterns of the following type:

    input -> weighted_mod -> batch_norm -> activation -> output

    batch_norm and activation may or may not be applied in the pattern.

    Only annotates those patterns in which all nodes return True when ``filter_fn`` is applied
    to them.
    """
    model.graph.eliminate_dead_code()
    model.recompile()

    matcher = _SubgraphMatcherWithNameNodeMap(pattern_gm, ignore_literals=True)
    matches = matcher.match(model.graph)

    annotated_partitions = []
    for match in matches:
        name_node_map = match.name_node_map
        input_node = name_node_map["input"]
        mod_node = name_node_map["mod"]
        weight_node = name_node_map["weight"]
        bias_node = name_node_map["bias"]
        if "output" in name_node_map:
            # In this case, an activation is applied to the weighted module output
            output_node = name_node_map["output"]
            # If the output is same as mod_node, it means we have an inplace activation,
            # so we need to correct the mod_node
            if mod_node == output_node:
                mod_node = mod_node.args[0]
        else:
            output_node = None

        # Validate mod args
        if mod_node.args[0] is not input_node:
            raise ValueError(
                f"Weighted module arg did not contain input node {input_node}"
            )
        if mod_node.args[1] is not weight_node:
            raise ValueError(
                f"Weighted module arg did not contain weight node {weight_node}"
            )
        if len(mod_node.args) > 2 and mod_node.args[2] is not bias_node:
            raise ValueError(
                f"Weighted module arg did not contain bias node {bias_node}"
            )

        # Skip if the partition is already annotated or is filtered out by the user
        partition = [mod_node, weight_node]
        if bias_node is not None:
            partition.append(bias_node)
        if _is_annotated(partition):
            continue
        if filter_fn and any(not filter_fn(n) for n in partition):
            continue

        # Annotate conv inputs and pattern output
        input_qspec_map = dict()
        if not _is_annotated([input_node]):
            input_qspec_map[input_node] = (
                quantization_config.input_activation if quantization_config else None
            )
        else:
            input_qspec_map[input_node] = input_node.meta[
                "quantization_annotation"
            ].output_qspec

        input_qspec_map[weight_node] = (
            quantization_config.weight if quantization_config else None
        )
        output_qspec = (
            quantization_config.output_activation if quantization_config else None
        )
        if bias_node is not None:
            input_qspec_map[bias_node] = None

        if output_node is None:
            mod_node.meta["quantization_annotation"] = _QuantizationAnnotation(
                input_qspec_map=input_qspec_map,
                output_qspec=output_qspec,
                _annotated=True,
            )
        else:
            mod_node.meta["quantization_annotation"] = _QuantizationAnnotation(
                input_qspec_map=input_qspec_map,
                _annotated=True,
            )
            if not _is_annotated([output_node]):
                output_qspec = _adjust_activation_qspec(
                    node=output_node, qspec=output_qspec
                )
                output_node.meta["quantization_annotation"] = _QuantizationAnnotation(
                    output_qspec=output_qspec,
                    _annotated=True,
                )

        _mark_nodes_as_annotated(partition)
        annotated_partitions.append(partition)
    return annotated_partitions

    Annotates all nodes in ``model``, which match the pattern specified by ``pattern_gm``
    using ``quantization_config``.

    ``pattern_gm`` captures patterns of the following type:

    input -> weighted_mod -> batch_norm -> activation -> output

    batch_norm and activation may or may not be applied in the pattern.

    Only annotates those patterns in which all nodes return True when ``filter_fn`` is applied
    to them.
    
coremltools/optimize/torch/quantization/_coreml_quantizer_utils.py