Skip to content

Commit aa57b50

Browse files
David Erikssonfacebook-github-bot
authored andcommitted
Cap max values for choice parameters (#1686)
Summary: Pull Request resolved: #1686 See title Reviewed By: esantorella Differential Revision: D47006938 fbshipit-source-id: 8e291bf743ed22a6985ecef6b0da53200baf842c
1 parent e4aa110 commit aa57b50

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

ax/core/parameter.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
# TODO: Do a more comprehensive audit of how floating point precision issues
2323
# may creep up and implement a more principled fix
2424
EPS = 1.5e-7
25-
25+
MAX_VALUES_CHOICE_PARAM = 1000
2626
FIXED_CHOICE_PARAM_ERROR = (
2727
"ChoiceParameters require multiple feasible values. "
2828
"Please use FixedParameter instead when setting a single possible value."
@@ -474,6 +474,12 @@ def __init__(
474474
# A choice parameter with only one value is a FixedParameter.
475475
if not len(values) > 1:
476476
raise UserInputError(f"{self._name}({values}): {FIXED_CHOICE_PARAM_ERROR}")
477+
# Cap the number of possible values
478+
if len(values) > MAX_VALUES_CHOICE_PARAM:
479+
raise UserInputError(
480+
f"`ChoiceParameter` with more than {MAX_VALUES_CHOICE_PARAM} values "
481+
"is not supported! Use a `RangeParameter` instead."
482+
)
477483
# pyre-fixme[4]: Attribute must be annotated.
478484
self._values = self._cast_values(values)
479485
# pyre-fixme[4]: Attribute must be annotated.

ax/core/tests/test_parameter.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,23 @@ def testHierarchicalValidation(self) -> None:
315315
dependents={"not_a_value": "other_param"},
316316
)
317317

318+
def testMaxValuesValidation(self) -> None:
319+
ChoiceParameter(
320+
name="x",
321+
parameter_type=ParameterType.INT,
322+
values=list(range(999)), # pyre-ignore
323+
)
324+
with self.assertRaisesRegex(
325+
UserInputError,
326+
"`ChoiceParameter` with more than 1000 values is not supported! Use a "
327+
"`RangeParameter` instead.",
328+
):
329+
ChoiceParameter(
330+
name="x",
331+
parameter_type=ParameterType.INT,
332+
values=list(range(1001)), # pyre-ignore
333+
)
334+
318335
def testHierarchical(self) -> None:
319336
# Test case where only some of the values entail dependents.
320337
hierarchical_param = ChoiceParameter(

0 commit comments

Comments
 (0)