Skip to content

Commit

Permalink
Cap max values for choice parameters (#1686)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #1686

See title

Reviewed By: esantorella

Differential Revision: D47006938

fbshipit-source-id: 8e291bf743ed22a6985ecef6b0da53200baf842c
  • Loading branch information
David Eriksson authored and facebook-github-bot committed Jun 26, 2023
1 parent e4aa110 commit aa57b50
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
8 changes: 7 additions & 1 deletion ax/core/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down Expand Up @@ -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.
Expand Down
17 changes: 17 additions & 0 deletions ax/core/tests/test_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit aa57b50

Please sign in to comment.