From 08ded5885554e04d3231347e7bdbc4e42e5aa685 Mon Sep 17 00:00:00 2001 From: Sait Cakmak Date: Wed, 30 Oct 2024 11:40:20 -0700 Subject: [PATCH 1/2] Remove CapParameter transform (#2993) Summary: This was only used in legacy param sweep. Cleaning up some of the unused transforms will help improve maintainability of the stack. Reviewed By: dme65, Balandat Differential Revision: D65216724 --- ax/modelbridge/transforms/cap_parameter.py | 55 ---------------- .../tests/test_cap_parameter_transform.py | 66 ------------------- ax/storage/transform_registry.py | 3 +- sphinx/source/modelbridge.rst | 8 --- 4 files changed, 1 insertion(+), 131 deletions(-) delete mode 100644 ax/modelbridge/transforms/cap_parameter.py delete mode 100644 ax/modelbridge/transforms/tests/test_cap_parameter_transform.py diff --git a/ax/modelbridge/transforms/cap_parameter.py b/ax/modelbridge/transforms/cap_parameter.py deleted file mode 100644 index bbfb885e946..00000000000 --- a/ax/modelbridge/transforms/cap_parameter.py +++ /dev/null @@ -1,55 +0,0 @@ -#!/usr/bin/env python3 -# Copyright (c) Meta Platforms, Inc. and affiliates. -# -# This source code is licensed under the MIT license found in the -# LICENSE file in the root directory of this source tree. - -# pyre-strict - -from typing import Optional, TYPE_CHECKING - -from ax.core.observation import Observation -from ax.core.parameter import RangeParameter -from ax.core.search_space import SearchSpace -from ax.modelbridge.transforms.base import Transform -from ax.models.types import TConfig -from ax.utils.common.typeutils import checked_cast - -if TYPE_CHECKING: - # import as module to make sphinx-autodoc-typehints happy - from ax import modelbridge as modelbridge_module # noqa F401 - - -class CapParameter(Transform): - """Cap parameter range(s) to given values. Expects a configuration of form - { parameter_name -> new_upper_range_value }. - - This transform only transforms the search space. - """ - - def __init__( - self, - search_space: SearchSpace | None = None, - observations: list[Observation] | None = None, - modelbridge: Optional["modelbridge_module.base.ModelBridge"] = None, - config: TConfig | None = None, - ) -> None: - # pyre-fixme[4]: Attribute must be annotated. - self.config = config or {} - assert search_space is not None, "CapParameter requires search space" - # pyre-fixme[4]: Attribute must be annotated. - self.transform_parameters = { # Only transform parameters in config. - p_name for p_name in search_space.parameters if p_name in self.config - } - - def _transform_search_space(self, search_space: SearchSpace) -> SearchSpace: - for p_name, p in search_space.parameters.items(): - if p_name in self.transform_parameters: - if not isinstance(p, RangeParameter): - raise NotImplementedError( - "Can only cap range parameters currently." - ) - checked_cast(RangeParameter, p).update_range( - upper=self.config.get(p_name) - ) - return search_space diff --git a/ax/modelbridge/transforms/tests/test_cap_parameter_transform.py b/ax/modelbridge/transforms/tests/test_cap_parameter_transform.py deleted file mode 100644 index 91289b50fd0..00000000000 --- a/ax/modelbridge/transforms/tests/test_cap_parameter_transform.py +++ /dev/null @@ -1,66 +0,0 @@ -#!/usr/bin/env python3 -# Copyright (c) Meta Platforms, Inc. and affiliates. -# -# This source code is licensed under the MIT license found in the -# LICENSE file in the root directory of this source tree. - -# pyre-strict - -from ax.core.parameter import ChoiceParameter, ParameterType, RangeParameter -from ax.core.search_space import SearchSpace -from ax.exceptions.core import UnsupportedError -from ax.modelbridge.transforms.cap_parameter import CapParameter -from ax.utils.common.testutils import TestCase -from ax.utils.testing.core_stubs import get_robust_search_space - - -class CapParameterTest(TestCase): - def setUp(self) -> None: - super().setUp() - self.search_space = SearchSpace( - parameters=[ - RangeParameter( - "a", lower=1, upper=3, parameter_type=ParameterType.FLOAT - ), - ChoiceParameter( - "b", parameter_type=ParameterType.STRING, values=["a", "b", "c"] - ), - ] - ) - - def test_transform_search_space(self) -> None: - t = CapParameter( - search_space=self.search_space, - observations=[], - config={"a": "2"}, - ) - t.transform_search_space(self.search_space) - # pyre-fixme[16]: Optional type has no attribute `upper`. - self.assertEqual(self.search_space.parameters.get("a").upper, 2) - t2 = CapParameter( - search_space=self.search_space, - observations=[], - config={"b": "2"}, - ) - with self.assertRaises(NotImplementedError): - t2.transform_search_space(self.search_space) - - def test_w_parameter_distributions(self) -> None: - rss = get_robust_search_space() - # Transform a non-distributional parameter. - t = CapParameter( - search_space=rss, - observations=[], - config={"z": "2"}, - ) - t.transform_search_space(rss) - # pyre-fixme[16]: Optional type has no attribute `upper`. - self.assertEqual(rss.parameters.get("z").upper, 2) - # Error with distributional parameter. - t = CapParameter( - search_space=rss, - observations=[], - config={"x": "2"}, - ) - with self.assertRaisesRegex(UnsupportedError, "transform is not supported"): - t.transform_search_space(rss) diff --git a/ax/storage/transform_registry.py b/ax/storage/transform_registry.py index 6a8474fbb70..684580cb12a 100644 --- a/ax/storage/transform_registry.py +++ b/ax/storage/transform_registry.py @@ -8,7 +8,6 @@ from ax.modelbridge.transforms.base import Transform -from ax.modelbridge.transforms.cap_parameter import CapParameter from ax.modelbridge.transforms.choice_encode import ( ChoiceEncode, ChoiceToNumericChoice, @@ -81,7 +80,7 @@ TrialAsTask: 14, UnitX: 15, Winsorize: 16, - CapParameter: 17, + # CapParameter: 17, DEPRECATED PowerTransformY: 18, ChoiceEncode: 19, # TO BE DEPRECATED ChoiceToNumericChoice: 19, diff --git a/sphinx/source/modelbridge.rst b/sphinx/source/modelbridge.rst index 15c69e70ce7..5cb3ce8d0b0 100644 --- a/sphinx/source/modelbridge.rst +++ b/sphinx/source/modelbridge.rst @@ -182,14 +182,6 @@ Transforms :undoc-members: :show-inheritance: -`ax.modelbridge.transforms.cap_parameter` -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -.. automodule:: ax.modelbridge.transforms.cap_parameter - :members: - :undoc-members: - :show-inheritance: - `ax.modelbridge.transforms.centered_unit_x` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ From 250b819c332503d115bc35a2292ffed9a1f13bbd Mon Sep 17 00:00:00 2001 From: Sait Cakmak Date: Wed, 30 Oct 2024 11:40:20 -0700 Subject: [PATCH 2/2] Remove CenteredUnitX transform Summary: Cleaning up some of the unused transforms will help improve maintainability of the stack. Differential Revision: D65216803 --- ax/modelbridge/transforms/centered_unit_x.py | 20 ------------------- .../tests/test_centered_unit_x_transform.py | 17 ---------------- sphinx/source/modelbridge.rst | 8 -------- 3 files changed, 45 deletions(-) delete mode 100644 ax/modelbridge/transforms/centered_unit_x.py delete mode 100644 ax/modelbridge/transforms/tests/test_centered_unit_x_transform.py diff --git a/ax/modelbridge/transforms/centered_unit_x.py b/ax/modelbridge/transforms/centered_unit_x.py deleted file mode 100644 index d4fbbe6afd7..00000000000 --- a/ax/modelbridge/transforms/centered_unit_x.py +++ /dev/null @@ -1,20 +0,0 @@ -#!/usr/bin/env python3 -# Copyright (c) Meta Platforms, Inc. and affiliates. -# -# This source code is licensed under the MIT license found in the -# LICENSE file in the root directory of this source tree. - -# pyre-strict - - -from ax.modelbridge.transforms.unit_x import UnitX - - -class CenteredUnitX(UnitX): - """Map X to [-1, 1]^d for RangeParameter of type float and not log scale. - - Transform is done in-place. - """ - - target_lb: float = -1.0 - target_range: float = 2.0 diff --git a/ax/modelbridge/transforms/tests/test_centered_unit_x_transform.py b/ax/modelbridge/transforms/tests/test_centered_unit_x_transform.py deleted file mode 100644 index 681188459d2..00000000000 --- a/ax/modelbridge/transforms/tests/test_centered_unit_x_transform.py +++ /dev/null @@ -1,17 +0,0 @@ -#!/usr/bin/env python3 -# Copyright (c) Meta Platforms, Inc. and affiliates. -# -# This source code is licensed under the MIT license found in the -# LICENSE file in the root directory of this source tree. - -# pyre-strict - -from ax.modelbridge.transforms.centered_unit_x import CenteredUnitX -from ax.modelbridge.transforms.tests.test_unit_x_transform import UnitXTransformTest - - -class CenteredUnitXTransformTest(UnitXTransformTest): - transform_class = CenteredUnitX - # pyre-fixme[4]: Attribute must be annotated. - expected_c_dicts = [{"x": -0.5, "y": 0.5}, {"x": -0.5, "a": 1.0}] - expected_c_bounds = [0.0, 1.5] diff --git a/sphinx/source/modelbridge.rst b/sphinx/source/modelbridge.rst index 5cb3ce8d0b0..65bd024aeb0 100644 --- a/sphinx/source/modelbridge.rst +++ b/sphinx/source/modelbridge.rst @@ -182,14 +182,6 @@ Transforms :undoc-members: :show-inheritance: -`ax.modelbridge.transforms.centered_unit_x` -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -.. automodule:: ax.modelbridge.transforms.centered_unit_x - :members: - :undoc-members: - :show-inheritance: - `ax.modelbridge.transforms.choice\_encode` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~