def test_grouped_channelwise_palettization_better_than_per_tensor(self, compute_unit, backend):
        """The grouped channelwise lut should be better than per-tensor lut."""
        model, inputs, torch_input_values, coreml_input_values = get_test_model_and_data()
        torchmodel = torch.jit.trace(model, torch_input_values)
        mlmodel = ct.convert(
            torchmodel,
            inputs=inputs,
            convert_to="mlprogram",
            minimum_deployment_target=backend.opset_version,
            compute_precision=ct.precision.FLOAT16
            if backend.precision == "fp16"
            else ct.precision.FLOAT32,
            compute_units=compute_unit,
        )

        per_tensor_config = cto.coreml.OptimizationConfig(
            global_config=cto.coreml.OpPalettizerConfig(
                mode="kmeans",
                nbits=4,
                granularity="per_tensor",
            )
        )
        grouped_channelwise_config = cto.coreml.OptimizationConfig(
            global_config=cto.coreml.OpPalettizerConfig(
                mode="kmeans",
                nbits=4,
                granularity="per_grouped_channel",
                group_size=1,
            )
        )

        if _macos_version() < (15, 0):
            pytest.skip("Channelwise palettization prediction only support in iOS18+")

        if backend.opset_version < ct.target.iOS18:
            with pytest.raises(ValueError, match=self._ERR_MSG.format("per_grouped_channel")):
                cto.coreml.palettize_weights(mlmodel, grouped_channelwise_config)
            return

        mlmodel_per_tensor_palettized = cto.coreml.palettize_weights(mlmodel, per_tensor_config)
        mlmodel_grouped_channelwise_palettized = cto.coreml.palettize_weights(
            mlmodel, grouped_channelwise_config
        )
        output_ref = mlmodel.predict(coreml_input_values)
        output_per_tensor = mlmodel_per_tensor_palettized.predict(coreml_input_values)
        output_grouped_channelwise = mlmodel_grouped_channelwise_palettized.predict(
            coreml_input_values
        )
        for k_ref, v_ref in output_ref.items():
            snr_per_tensor = compute_snr_and_psnr(v_ref, output_per_tensor[k_ref])[0]
            snr_grouped_channelwise = compute_snr_and_psnr(
                v_ref, output_grouped_channelwise[k_ref]
            )[0]
            assert snr_grouped_channelwise > snr_per_tensor
The grouped channelwise lut should be better than per-tensor lut.
coremltools/test/optimize/coreml/test_post_training_quantization.py