diff --git a/mlos_bench/mlos_bench/optimizers/convert_configspace.py b/mlos_bench/mlos_bench/optimizers/convert_configspace.py index b0e48ee2df..e4c70a2733 100644 --- a/mlos_bench/mlos_bench/optimizers/convert_configspace.py +++ b/mlos_bench/mlos_bench/optimizers/convert_configspace.py @@ -26,7 +26,10 @@ from mlos_bench.tunables.tunable import Tunable, TunableValue from mlos_bench.tunables.tunable_groups import TunableGroups from mlos_bench.util import try_parse_val -from mlos_core.spaces.converters.util import QUANTIZATION_BINS_META_KEY +from mlos_core.spaces.converters.util import ( + QUANTIZATION_BINS_META_KEY, + monkey_patch_hp_quantization, +) _LOG = logging.getLogger(__name__) @@ -77,10 +80,6 @@ def _tunable_to_configspace( meta: Dict[Hashable, TunableValue] = {"cost": cost} if group_name is not None: meta["group"] = group_name - if tunable.is_numerical and tunable.quantization_bins: - # Temporary workaround to dropped quantization support in ConfigSpace 1.0 - # See Also: https://github.com/automl/ConfigSpace/issues/390 - meta[QUANTIZATION_BINS_META_KEY] = tunable.quantization_bins if tunable.type == "categorical": return ConfigurationSpace( @@ -141,6 +140,14 @@ def _tunable_to_configspace( else: raise TypeError(f"Invalid Parameter Type: {tunable.type}") + if tunable.is_numerical and tunable.quantization_bins: + # Temporary workaround to dropped quantization support in ConfigSpace 1.0 + # See Also: https://github.com/automl/ConfigSpace/issues/390 + new_meta = dict(range_hp.meta or {}) + new_meta[QUANTIZATION_BINS_META_KEY] = tunable.quantization_bins + range_hp.meta = new_meta + monkey_patch_hp_quantization(range_hp) + if not tunable.special: return ConfigurationSpace({tunable.name: range_hp}) diff --git a/mlos_bench/mlos_bench/tests/tunables/tunable_to_configspace_test.py b/mlos_bench/mlos_bench/tests/tunables/tunable_to_configspace_test.py index a62375d4a6..3f53756090 100644 --- a/mlos_bench/mlos_bench/tests/tunables/tunable_to_configspace_test.py +++ b/mlos_bench/mlos_bench/tests/tunables/tunable_to_configspace_test.py @@ -13,7 +13,6 @@ UniformFloatHyperparameter, UniformIntegerHyperparameter, ) -from ConfigSpace.hyperparameters import NumericalHyperparameter from mlos_bench.optimizers.convert_configspace import ( TunableValueKind, @@ -128,6 +127,8 @@ def _cmp_tunable_hyperparameter_numerical(tunable: Tunable, space: Configuration if tunable.in_range(tunable.value): assert param.default_value == tunable.value assert (param.meta or {}).get(QUANTIZATION_BINS_META_KEY) == tunable.quantization_bins + if tunable.quantization_bins: + assert param.sample_value() in list(tunable.quantized_values or []) def test_tunable_to_configspace_categorical(tunable_categorical: Tunable) -> None: