|
| 1 | +# Copyright 2025 Sony Semiconductor Israel, Inc. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================== |
| 15 | +import pytest |
| 16 | +import numpy as np |
| 17 | + |
| 18 | +from unittest.mock import Mock |
| 19 | + |
| 20 | +from model_compression_toolkit.core import MixedPrecisionQuantizationConfig |
| 21 | +from model_compression_toolkit.core.common.mixed_precision.sensitivity_evaluation import SensitivityEvaluation |
| 22 | +from model_compression_toolkit.core.common.model_builder_mode import ModelBuilderMode |
| 23 | + |
| 24 | + |
| 25 | +def custom_float_metric(model_mp) -> float: |
| 26 | + return 100.0 |
| 27 | + |
| 28 | + |
| 29 | +def custom_np_float_metric(model_mp) -> np.floating: |
| 30 | + return np.float64(100.0) |
| 31 | + |
| 32 | + |
| 33 | +def custom_str_metric(model_mp) -> str: |
| 34 | + return 'test' |
| 35 | + |
| 36 | + |
| 37 | +def custom_none_metric(model_mp): |
| 38 | + return None |
| 39 | + |
| 40 | + |
| 41 | +def get_sensitivity_evaluator(custom_metric_fn): |
| 42 | + mock_graph = Mock() |
| 43 | + mock_graph.get_topo_sorted_nodes.return_value = ['test', 'this', 'is', 'reset'] |
| 44 | + mock_graph.get_outputs.return_value = [] |
| 45 | + |
| 46 | + def representative_data_gen() -> list: |
| 47 | + for _ in range(5): |
| 48 | + yield np.random.randn(2, 3, 248, 248) |
| 49 | + |
| 50 | + mock_fw_info = Mock() |
| 51 | + |
| 52 | + def custom_model_builder_return_value(*args, **kwargs): |
| 53 | + mode = kwargs.get('mode') |
| 54 | + if mode == ModelBuilderMode.FLOAT: |
| 55 | + return (None, None) |
| 56 | + else: |
| 57 | + return (None, None, None) |
| 58 | + |
| 59 | + def custom_to_tensor(img): |
| 60 | + return img |
| 61 | + |
| 62 | + mock_fw_impl = Mock() |
| 63 | + mock_fw_impl.model_builder.side_effect = custom_model_builder_return_value |
| 64 | + mock_fw_impl.to_tensor.side_effect = custom_to_tensor |
| 65 | + |
| 66 | + mock_set_layer_to_bitwidth = Mock() |
| 67 | + |
| 68 | + mp_cfg = MixedPrecisionQuantizationConfig(custom_metric_fn=custom_metric_fn) |
| 69 | + |
| 70 | + sensitivity_eval = SensitivityEvaluation(graph=mock_graph, |
| 71 | + quant_config=mp_cfg, |
| 72 | + representative_data_gen=representative_data_gen, |
| 73 | + fw_info=mock_fw_info, |
| 74 | + fw_impl=mock_fw_impl, |
| 75 | + set_layer_to_bitwidth=mock_set_layer_to_bitwidth |
| 76 | + ) |
| 77 | + sensitivity_eval._configure_bitwidths_model = lambda *args, **kwargs: None # Method does nothing |
| 78 | + sensitivity_eval.model_mp = Mock() |
| 79 | + return sensitivity_eval |
| 80 | + |
| 81 | + |
| 82 | +class TestMPCustomMetricFunction: |
| 83 | + |
| 84 | + @pytest.mark.parametrize("metric_fn, expected", [ |
| 85 | + (custom_float_metric, 100.0), |
| 86 | + (custom_np_float_metric, np.float64(100.0)), |
| 87 | + ]) |
| 88 | + def test_valid_metric_function(self, metric_fn, expected): |
| 89 | + sensitivity_eval = get_sensitivity_evaluator(metric_fn) |
| 90 | + assert len(sensitivity_eval.interest_points) == 0 |
| 91 | + assert sensitivity_eval.compute_metric(Mock()) == expected |
| 92 | + |
| 93 | + @pytest.mark.parametrize("metric_fn, expected", [ |
| 94 | + (custom_str_metric, str.__name__), |
| 95 | + (custom_none_metric, type(None).__name__), |
| 96 | + ]) |
| 97 | + def test_type_invalid_metric_function(self, metric_fn, expected): |
| 98 | + sensitivity_eval = get_sensitivity_evaluator(metric_fn) |
| 99 | + assert len(sensitivity_eval.interest_points) == 0 |
| 100 | + with pytest.raises(TypeError, match=f'The custom_metric_fn is expected to return float or numpy float, got {expected}'): |
| 101 | + sensitivity_metric = sensitivity_eval.compute_metric(Mock()) |
0 commit comments