From aa57b50059c770988fd035d08fb69a4e54820f2f Mon Sep 17 00:00:00 2001 From: David Eriksson Date: Sun, 25 Jun 2023 19:19:47 -0700 Subject: [PATCH] Cap max values for choice parameters (#1686) Summary: Pull Request resolved: https://github.com/facebook/Ax/pull/1686 See title Reviewed By: esantorella Differential Revision: D47006938 fbshipit-source-id: 8e291bf743ed22a6985ecef6b0da53200baf842c --- ax/core/parameter.py | 8 +++++++- ax/core/tests/test_parameter.py | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/ax/core/parameter.py b/ax/core/parameter.py index 3d0d37913e9..c9646f10f86 100644 --- a/ax/core/parameter.py +++ b/ax/core/parameter.py @@ -22,7 +22,7 @@ # TODO: Do a more comprehensive audit of how floating point precision issues # may creep up and implement a more principled fix EPS = 1.5e-7 - +MAX_VALUES_CHOICE_PARAM = 1000 FIXED_CHOICE_PARAM_ERROR = ( "ChoiceParameters require multiple feasible values. " "Please use FixedParameter instead when setting a single possible value." @@ -474,6 +474,12 @@ def __init__( # A choice parameter with only one value is a FixedParameter. if not len(values) > 1: raise UserInputError(f"{self._name}({values}): {FIXED_CHOICE_PARAM_ERROR}") + # Cap the number of possible values + if len(values) > MAX_VALUES_CHOICE_PARAM: + raise UserInputError( + f"`ChoiceParameter` with more than {MAX_VALUES_CHOICE_PARAM} values " + "is not supported! Use a `RangeParameter` instead." + ) # pyre-fixme[4]: Attribute must be annotated. self._values = self._cast_values(values) # pyre-fixme[4]: Attribute must be annotated. diff --git a/ax/core/tests/test_parameter.py b/ax/core/tests/test_parameter.py index c8cfdb70361..7f3778fa325 100644 --- a/ax/core/tests/test_parameter.py +++ b/ax/core/tests/test_parameter.py @@ -315,6 +315,23 @@ def testHierarchicalValidation(self) -> None: dependents={"not_a_value": "other_param"}, ) + def testMaxValuesValidation(self) -> None: + ChoiceParameter( + name="x", + parameter_type=ParameterType.INT, + values=list(range(999)), # pyre-ignore + ) + with self.assertRaisesRegex( + UserInputError, + "`ChoiceParameter` with more than 1000 values is not supported! Use a " + "`RangeParameter` instead.", + ): + ChoiceParameter( + name="x", + parameter_type=ParameterType.INT, + values=list(range(1001)), # pyre-ignore + ) + def testHierarchical(self) -> None: # Test case where only some of the values entail dependents. hierarchical_param = ChoiceParameter(