Skip to content

Check metric constraints when the constrained metric is exactly zero - #1671

Open
Sohail Shaikh (sohailshk) wants to merge 7 commits into
microsoft:mainfrom
sohailshk:fix/metric-constraint-zero-value
Open

Sohail Shaikh (sohailshk) wants to merge 7 commits into
microsoft:mainfrom
sohailshk:fix/metric-constraint-zero-value

Conversation

@sohailshk

Copy link
Copy Markdown

Why are these changes needed?

BlendSearch.on_trial_complete() applies the penalty only if value:, so a reported 0 / 0.0 skipped the constraint: with metric_constraints=[("recall", ">=", 0.5)] a trial reporting recall=0.0 was treated as feasible (its lagrange objective equal to the raw objective). Now the constraint is skipped only when the metric is missing (if value is not None).

Test: recall is 0.0 above x=5 with mode="max"; infeasible trials must get a lagrange-modified score below the raw score, feasible ones the raw score. It fails on main (7.71 < 7.71) and passes now.

Related issue number

Same pattern as #1603 (FLOW2 / tune.report); this check was not part of it.

Checks

Checks run: the new test, the tune/automl constraint tests and test_tune/test_lexiflow (9 passed, 1 skipped); black (line length 120) and ruff on the changed files. The pinned pre-commit hook versions were not run. No doc changes are needed.

BlendSearch.on_trial_complete() read the constrained metric with 'if value:',
so a reported value of 0 (for example recall 0.0 against recall >= 0.5) skipped
the constraint and the trial was treated as feasible. Only skip when the metric
is missing.

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.

🟢 Approval recommended

No active review issues are recorded for the change to check zero-valued metrics against tuning constraints.

No open issues 🎉

What changed in this PR

This PR fixes FLAML’s tuning constraint handling so zero-valued metrics are checked rather than skipped, complementing the zero-metric reporting fix in #1603.

Changes:

  • Replace the truthiness check with an explicit None check when applying metric constraints.
  • Add a regression test verifying that zero-recall violations lower maximized scores while feasible trials retain their raw scores.
File Description
test/​tune/​test_metric_constraint_zero.py Tests penalties for zero-valued constraint violations and unchanged scores for feasible trials.
flaml/​tune/​searcher/​blendsearch.py Applies constraint checks to zero-valued metrics.

Review effort: Balanced

@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.

The zero-valued metric presence check was independently reviewed with review-code (Low, actual gpt-6.2-astra at max reasoning). The regression passes on native Python 3.10 and 3.13 and fails on exact main. The retained low-priority finding is test determinism: seed explicit points on both sides of the recall boundary rather than requiring the optimizer's first 20 suggestions to cover both. No production correctness regression was identified in this single-line correction.

No PR approval submitted. Whole required review evidence, resolved threads, explicit current-head Balanced Copilot approval and every applicable check/context and actual workflow must all pass. At this guarded readback, non-successful actual runs: [{"name": "Build", "status": "in_progress", "conclusion": null}]. No foreign thread was resolved and no earlier review was dismissed.

Posted by thinkall-agent-auto-reviewer

@sohailshk

Copy link
Copy Markdown
Author

Thanks for the review, thinkall. I've addressed the low-priority feedback about test determinism and pushed the update in commit 631966ad.

  1. Test determinism
    Updated the test to use fixed evaluation points:
    points_to_evaluate=[{"x": 2.0}, {"x": 8.0}]
    with num_samples=2.
    The test now explicitly verifies that:
  • Exactly two trials are executed.
  • Exactly one trial is penalized.
  • Exactly one trial is unpenalized.

This removes the dependency on search behavior to determine which configurations get evaluated.
2. Regression verification
Verified the test against both implementations:

  • With blendsearch.py reverted to main: the test fails.
  • On the PR head: the test passes consistently across three repeated runs (3/3).

This confirms that the test detects the original issue and behaves deterministically with the fix.
3. Scope of the commit
Commit 631966ad modifies test_metric_constraint_zero.py only. No source code has been changed.
4. CI status
The Build job was still in progress when the review was submitted, which is why no approval was given at that point. CI is still running.
Summary
The test now uses deterministic evaluation points and explicitly checks the expected penalized and unpenalized trial counts. The regression is reproducible against main, and the updated test passes consistently on the PR head.

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.

🟢 Approval recommended

No active review issues are identified for the zero-valued metric constraint fix.

No open issues 🎉

Review effort: Balanced

@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.

The deterministic float-zero test passes, but the full PR still mishandles unsigned zero metrics on NumPy 2.

P2: a feasible np.uint8(0) count can be penalized, and the opposite constraint can stop trial completion. In actual sequential tune.run, metric errors=np.uint8(0) with integer threshold 1 and <= changes score 5 to -2549999999995. With >=, completion raises OverflowError: Python integer -1 out of bounds for uint8. The raw recorded metric remains uint8. Float controls and NumPy 1.26 behave correctly.

The preceding PR head has the same issue; exact main leaves the valid <= score unchanged but incorrectly skips the violating >= check. This is a remaining full-PR regression from exposing zero to fixed-width arithmetic, not a defect introduced by the latest test rewrite. Please normalize NumPy integer operands before subtraction/multiplication and cover both constraint directions with unsigned zero.

Posted by thinkall-agent-auto-reviewer

…arithmetic

A metric stored as an unsigned numpy integer (for example np.uint8(0)) wrapped
around or overflowed in (value - threshold), so a feasible zero count was
penalized and the opposite constraint direction raised OverflowError on NumPy 2.
@sohailshk

Copy link
Copy Markdown
Author

Fixed thinkall's np.uint8(0) case:

  • Metric values are converted using .item() before threshold arithmetic.
  • Thresholds are also converted to avoid unsigned integer overflow. For example, 0 - np.uint8(1) wraps to 255 in NumPy 2, but not in NumPy 1.26.
  • Added 18 test cases covering both <= and >=.

Tests: With NumPy 2.5.5, 27 tests pass, compared with 12 failures using the earlier source.

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.

🟢 Approval recommended

There are no active review issues requiring resolution.

No open issues 🎉

Review effort: Balanced

@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.

Whole-PR review at 7cdc8269a736ef6a900509e3b3d4f9f885d0493b: two dtype combinations still produce incorrect penalty-adjusted scores in actual NumPy 2 Tune runs.

  • errors=np.array(0, dtype=np.uint8), constrained to <= 1, with score=5 receives 2550000000005.0 instead of 5.0. The metric-side conversion misses zero-dimensional arrays. Current and predecessor reproduce it; main does not. This is a remaining whole-PR regression, not newly introduced by this correction.
  • score=np.float16(5) with errors=np.float64(2) constrained to <= 1 now produces infinity instead of 10000000005.0. The current .item() conversion changes promotion during penalty addition; predecessor/main remain finite. This is new in the latest correction.

NumPy 1.26 and Python-float objective controls remain finite. Normalize zero-dimensional metric operands consistently with thresholds, and normalize the local objective before penalty addition without changing the raw reported score. Add these exact regression cases.

The targeted current suites passed 54 tests, but do not cover these combinations. The Low skill verdict remains issues found despite successful workflows and the Balanced approval recommendation. Evidence remains local only.

Posted by thinkall-agent-auto-reviewer

…the constraint penalty

A 0-d unsigned array was not converted like an unsigned scalar and wrapped around in the violation. A float16 objective overflowed to infinity when the penalty was added; the objective is now a Python number for the arithmetic and the reported result is unchanged.
A 1-d array with one element was not converted and wrapped around like an unsigned scalar in the constraint violation.

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.

🟢 Approval recommended

No active review issues block approval of the constraint-handling changes.

No open issues 🎉

Review effort: Balanced

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.

3 participants