def test_linear_quantizer_compression_metadata(algorithm, dtype, scheme, conv_transpose):
    """
    Test that calling finalize on the module leads to compression metadata being added to the model
    """
    model = nn.Sequential(
        OrderedDict(
            [
                (
                    "conv1",
                    (nn.Conv2d(1, 20, 3) if not conv_transpose else nn.ConvTranspose2d(1, 20, 3)),
                ),
                ("relu", nn.ReLU()),
                ("fc1", nn.Linear(20, 100)),
            ]
        )
    )
    config = LinearQuantizerConfig.from_dict(
        {
            "global_config": {"algorithm": algorithm, "quantization_scheme": scheme},
            "module_name_configs": {
                "conv1": {
                    "algorithm": algorithm,
                    "weight_dtype": dtype,
                    "quantization_scheme": scheme,
                },
                "fc1": None,
            },
        }
    )
    quantizer = LinearQuantizer(model, config)
    quantizer.prepare(inplace=True, example_inputs=(torch.randn(1, 1, 28, 28),))
    for _ in range(4):
        quantizer.step()
    model = quantizer.finalize()

    # Verify metadata version is added to model
    assert "_COREML_/metadata_version" in model.state_dict()

    # Verify compression metadata is added for conv1
    metadata_dict = CompressionMetadata.from_state_dict(model.conv1[0].state_dict())
    assert len(metadata_dict) == 1
    assert "weight" in metadata_dict

    metadata = metadata_dict["weight"]
    assert metadata.compression_type == [CompressionType.quantization.value]
    assert metadata.quantization_n_bits == 4 if dtype == "qint4" else 8
    scale_zero_point_shape = (20, 1) if not conv_transpose else (1, 20)
    assert metadata.quantization_scale.shape == scale_zero_point_shape
    assert metadata.zero_point.shape == scale_zero_point_shape
    if scheme == "symmetric":
        assert torch.all(metadata.zero_point == 0)

    # Verify no compression metadata is added for fc1
    metadata_dict = CompressionMetadata.from_state_dict(model.fc1.state_dict())
    assert len(metadata_dict) == 0

    Test that calling finalize on the module leads to compression metadata being added to the model
    
coremltools/test/optimize/torch/quantization/test_quantizer.py