Skip to content

Support config_constraints in AutoML - #1670

Open
Ankit Dhandharia (ankitdhandharia) wants to merge 3 commits into
microsoft:mainfrom
ankitdhandharia:fix/issue-840
Open

Ankit Dhandharia (ankitdhandharia) wants to merge 3 commits into
microsoft:mainfrom
ankitdhandharia:fix/issue-840

Conversation

@ankitdhandharia

Copy link
Copy Markdown

Why are these changes needed?

flaml.tune has long supported config_constraints, but AutoML ignored them: both search-construction paths in flaml/automl/automl.py built the searcher's config_constraints from a single hard-coded memory-size constraint and offered no way for a user or a custom estimator to constrain which hyperparameter configurations are explored.

This adds the optional, maintainer-proposed hook BaseEstimator.config_constraints() (default []), which an estimator can override to declare constraints in the existing tune config_constraints syntax — a list of (function, operator, threshold) tuples, with operators <=, >=, <, >. AutoML now merges each estimator's constraints with the memory-size constraint:

  • Sequential per-estimator search appends learner_class.config_constraints() directly (the constraint function receives the raw single-estimator config).
  • Joint/parallel search wraps each estimator's constraints so they apply only to configs whose selected learner is that estimator (mirroring the existing size dispatch), leaving other learners unconstrained.

There is no behavior change for estimators that don't override the method (they return []). Configs that violate a constraint are skipped during search by the existing BlendSearch constraint-checking logic (_violate_config_constriants), so they are never trained.

Related issue number

Closes #840

Checks

Testing

Added a regression test test_config_constraints in test/automl/test_constraints.py. It registers a custom estimator subclassing LGBMEstimator whose config_constraints caps n_estimators <= 16, fits AutoML on synthetic make_classification data (n_jobs=1, the sequential search path), and asserts both that best_config["n_estimators"] <= 16 and that every trained/logged config satisfies the constraint (read back from the training log).

  • Before the fix: python -m pytest test/automl/test_constraints.py::test_config_constraints -q → 1 failed (AutoML never consulted the hook, so configs with n_estimators up to 41 were trained; assert 41 <= 16 failed).
  • After the fix: python -m pytest test/automl/test_constraints.py::test_config_constraints -q → 1 passed.
  • Full file: python -m pytest test/automl/test_constraints.py -q → 3 passed (the two existing metric-constraint tests plus the new one).
  • Neighbouring: python -m pytest test/automl/test_classification.py -q -k "multiclass or binary or custom_learner or lrl1" → 3 passed.

@ankitdhandharia

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Estimator constraints are inconsistently enforced across search methods, strict < checks incorrectly allow equality, and infeasible initialization can prevent discovery of valid models.

3 open issues
What changed in this PR

Adds an estimator-level configuration-constraint hook to AutoML, extending Tune’s existing constraint mechanism toward the support requested in #840.

Changes:

  • Adds optional BaseEstimator.config_constraints(), returning no constraints by default.
  • Combines estimator constraints with memory limits in sequential and joint searches, with learner-specific dispatch for joint configurations.
  • Adds a sequential regression test checking that the best and all logged configurations satisfy an estimator-count limit.
File Description
test/​automl/​test_constraints.py Adds a constrained custom-estimator regression test.
flaml/​automl/​model.py Defines and documents the optional estimator constraint hook.
flaml/​automl/​automl.py Registers estimator constraints and adds joint-search dispatch helpers.

Review effort: Balanced

Comment thread flaml/automl/automl.py
max_resource=max_resource,
config_constraints=[(learner_class.size, "<=", self._mem_thres)],
config_constraints=[(learner_class.size, "<=", self._mem_thres)]
+ list(learner_class.config_constraints()),

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 27fa648. If an estimator's default (low-cost) starting config violates its constraints, SearchState now samples the search space. It uses the feasible config closest to the default (in normalized space) as both the initial point and the low-cost partial config. User starting points that violate the constraints are dropped with a warning, the same way out-of-space starting points already are. Because sequential and joint search both take their starting points from SearchState, this covers both. With n_estimators >= 16, the search now starts at about 21 trees instead of never training. There are tests for the sequential path (all hpo_methods) and the joint path (test_config_constraints_joint_search, spark, cfo/bs).

Comment thread flaml/automl/automl.py
Comment on lines +3117 to +3118
config_constraints=[(learner_class.size, "<=", self._mem_thres)]
+ list(learner_class.config_constraints()),

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 27fa648. AutoMLState._compute_with_config_base now checks the estimator's config_constraints() before training. A violating config is reported with val_loss=inf and never fitted, the same way the parallel path already handles the memory constraint. Every hpo_method goes through this check, in both sequential and joint search. RandomSearch also resamples (up to 100 draws) until it gets a config that satisfies the constraints, so it doesn't waste iterations. test_config_constraints_enforced covers cfo/bs/random/optuna with <=, < and >= constraints, and asserts on every config that reaches fit().

Comment thread flaml/automl/model.py
Returns:
A list of ``(function, operator, threshold)`` tuples, where
``function`` maps a hyperparameter-config dict to a float,
``operator`` is one of ``"<="``, ``">="``, ``"<"`` or ``">"``,

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 27fa648. The comparison is now a shared config_constraints_violated helper in blendsearch.py, and "<" rejects value >= threshold. test_config_constraint_boundary checks all four operators at the threshold, and also checks that tune.run skips the boundary point x=16 for x < 16.

@thinkall Li Jiang (thinkall) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed a current-head estimator-constraint bypass with actual public AutoML/Optuna and native LightGBM fits. A LGBMEstimator subclass whose config_constraints() returns [(lambda config: config["n_estimators"], "<=", 4)], a feasible cap present in the search space, and hpo_method="optuna", max_iter=3, n_jobs=1 trained configurations entering fit with tree counts 6 and 13; its best config also had n_estimators=6. The new constraint API therefore is not consistently enforced across the supported search methods. Please enforce the constraints for those methods, or explicitly reject/document unsupported methods rather than silently violating them. This confirms one of Copilot's existing concerns; the other peer concerns and threads remain separate and unresolved.

The proposed single-learner default-search test passes on both native stacks when run serially. Its first two failures were caused by this validation runner writing the same training-log filename concurrently; those original outputs and the serial recovery are retained and are not presented as a contributor-code regression. An earlier cap-16 Optuna probe happened to draw only feasible values and did not discriminate the issue; it is also retained. The cap-4 run used actual model fitting, not a mocked searcher or a full optional/distributed matrix.

The required Medium independent review-code lens batch is still running; no complete six-lens or clean whole-PR signoff is claimed at this point. These driver-confirmed P2 findings justify the changes request independently of pending CI. No approval is submitted and no earlier review was dismissed.

Posted by thinkall-agent-auto-reviewer

- Skip configs that violate an estimator's config_constraints before
  training, so search methods that don't enforce them (optuna, random)
  can't train an infeasible config. Applies to sequential and joint search.
- RandomSearch resamples until a config satisfies the constraints.
- Treat '<' as strict: a value equal to the threshold now violates it.
- If the default (low-cost) starting config violates the constraints,
  start from the nearest feasible sampled config instead of stalling;
  drop user starting points that violate them.
- Add regression tests for all hpo_methods, the strict boundary, starting
  points and joint search with spark.
@ankitdhandharia

Copy link
Copy Markdown
Author

Li Jiang (@thinkall) Thanks for the review. Your Optuna reproduction (n_estimators <= 4, max_iter=3) showed configs with 6 and 13 trees reaching fit. That's fixed in 27fa648, together with the three Copilot threads:

  • Every hpo_method: an estimator's constraints are checked before training. A violating config is skipped with val_loss=inf and never fitted, so Optuna, random and any other search method can't train it. This applies to sequential and joint search.
  • Strict <: a value equal to the threshold now violates the constraint.
  • Infeasible start: a lower-bound constraint such as n_estimators >= 16 used to leave the search with nothing to train. It now starts from the nearest feasible config.

Tests:

  • test_config_constraints_enforced records every config that enters fit(), for cfo/bs/random/optuna with <= 4, < 16 and >= 16.
  • test_config_constraint_boundary covers the strict boundary.
  • test_config_constraints_starting_point covers starting points that violate the constraints.
  • test_config_constraints_joint_search (spark, cfo/bs) covers the joint path.

Nine of the new tests fail on the previous commit, as do both spark joint-search tests. Locally the new tests pass, along with the tune and automl constraint, searcher, warm-start and log tests (66 passed) and the existing spark custom-learner test.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support config_constraints in AutoML similar to Tune

3 participants