Skip to content

fix: loosen percentage tolerance for negative values in lexico_best - #1677

Open
Anish Mehta (anishmehta24) wants to merge 7 commits into
microsoft:mainfrom
anishmehta24:fix/lexico-best-percentage-max
Open

Anish Mehta (anishmehta24) wants to merge 7 commits into
microsoft:mainfrom
anishmehta24:fix/lexico-best-percentage-max

Conversation

@anishmehta24

@anishmehta24 Anish Mehta (anishmehta24) commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Why are these changes needed?

ExperimentAnalysis.lexico_best applies a percentage tolerance as f_best * (1 + p). The values there are minimized, so a "max" objective is negated, and for a negative best value this puts the bound below the best value instead of above it. No result is feasible and best_result raises:

analysis = tune.run(
    lambda c: {"acc": 10 + c["x"], "time": 5 - c["x"]},
    config={"x": tune.uniform(0, 1)},
    lexico_objectives={"metrics": ["acc", "time"], "modes": ["max", "min"],
                       "tolerances": {"acc": "10%", "time": 0}, "targets": {}},
    num_samples=10,
)
analysis.best_result
# ValueError: zero-size array to reduction operation minimum which has no identity

The same happens for a "min" objective with negative values, and it is an IndexError when that objective is the last one. The percentage is now taken of the absolute best value and added to it (f_best + |f_best| * p / 100), so it loosens the bound for negative values too. The bound is computed exactly with fractions.Fraction, so large values and float16 values do not overflow, and NumPy integers are promoted before abs(). The ratio is built from the percentage text, so digits a float cannot hold are kept. For integer, fraction and decimal best values the exact bound is used for the exact values, since they need not be integers. A float is kept if it is within the exact bound either as stored or as printed, so "45%" of 100 keeps 55, "75%" of 0.1 keeps 0.025, "1%" of 0.051 keeps 0.05151 and "10%" of Fraction(1) keeps 1.1. Decimal values of a "max" objective are negated with copy_negate(), so no digit is rounded away, and a Decimal best gets the exact bound for an absolute tolerance too, since adding to it rounds to the context precision and could put the bound below the best value. Absolute tolerances on other values, 0% and infinite best values keep the same bound, and a malformed percentage still raises ValueError.

Related issue number

None found. This is the best_result error left for a separate PR in #1669.

Checks

In test/tune/test_lexico_best.py, the three negative-value cases, the 45%, 75% and large-value max cases, the int8 case, the decimal best, fraction best with float values, decimal "max" beyond the context precision and long percentage cases, the negative decimal "min" value beyond the context precision with a zero tolerance, the decimal "max" value with a 1E-40 tolerance and the tune.run case fail on main and pass with the change; the positive-value, 0%, 1% decimal, large min, float16, 2**70 integer, integer best with fraction or decimal values, fraction best, infinite-value, malformed-percentage and decimal "max" value with a zero tolerance cases pass on both. The rest of pytest test/tune passes, apart from test_no_optuna, which needs pip in the venv.

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 reported for the percentage-tolerance handling change in ExperimentAnalysis.lexico_best.

No open issues 🎉

What changed in this PR

Fixes percentage-tolerance handling in ExperimentAnalysis.lexico_best so negative minimized values remain feasible, including negated maximization objectives. This addresses the result-selection error left separate from #1669’s FLOW2 fixes.

Changes:

  • Loosens percentage bounds for negative best values while preserving positive-value, absolute-tolerance, zero-percent, and infinite-value behavior.
  • Adds focused regression tests and a tune.run integration case for maximization with percentage tolerances.
File Description
test/​tune/​test_lexico_best.py Adds percentage-tolerance regression tests and end-to-end result-selection coverage.
flaml/​tune/​tune.py Makes percentage bounds sign-aware and preserves infinite best values.

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 six submitted cases pass on both native stacks, but the new percentage formula still excludes an ordinary exact-boundary candidate.

P2: "45%" rejects score 55 when the maximized best score is 100. Using public SimpleTrial objects and ExperimentAnalysis.best_result, results (score, time) = (100, 5), (55, 1), (54, 0) select (100, 5) on both stacks. The equivalent absolute tolerance of 45 selects (55, 1) and excludes 54. -100 * (1 - 0.45) rounds to -55.00000000000001, defeating the inclusive comparison. Please compare deterioration with allowed slack without introducing that extra multiplier rounding and add the 55/54 boundary regression. Exact main's percentage path raises instead, so I am not claiming a healthy old boundary implementation.

Low-priority validation follow-up: malformed "ten%" is newly accepted when the best score is infinite because parsing is bypassed; main raises ValueError. Parse the percentage before the infinity arithmetic shortcut.

The separate negative-loss native Tune failure reproduces on exact main as well as this head. It is retained as an inherited search-side integration limitation, not a newly introduced defect in this analysis correction.

Posted by thinkall-agent-auto-reviewer

Use best + |best| * p / 100 instead of multiplying the best value, so a 45% tolerance on 100 keeps 55. Parse the percentage before the infinite-value shortcut.
@anishmehta24

Copy link
Copy Markdown
Contributor Author

Changed in e9d5842. The percentage is now a slack of |best| * p / 100 added to the best value, like an absolute tolerance, so "45%" on 100 keeps 55 and drops 54. I kept it as value <= best + slack rather than value - best <= slack because the subtraction rounds 0.55 - 0.5 above 0.05 and would drop a 10% case that main keeps. The percentage is also parsed before the infinity shortcut, so "ten%" raises ValueError again; both cases have tests.

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

The percentage-tolerance calculation can overflow to infinity for large finite losses, admitting candidates outside the intended bound and allowing them to win on the next objective.

1 open issue

Review effort: Balanced

Comment thread flaml/tune/tune.py Resolved

@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 previous two findings are fixed at e9d5842a8be7ebaa9103a1995ac410342c2c12c3: the ordinary 55/100 boundary is included for "45%", and malformed "ten%" raises ValueError even when the best metric is infinite. The nine current submitted tests pass on both native stacks (18 passes).

The full current review found additional regressions, all independently reproduced through public SimpleTrial / ExperimentAnalysis calls against current and preceding heads.

[P2] Another exact percentage boundary is rejected. With maximized (score, time) = (0.1, 5), (0.025, 1), (0.024, 0) and "75%", current selects (0.1, 5) on both stacks; preceding selects (0.025, 1). 0.025 == 0.1 / 4 is exact in binary floating point, but the new rounded allowance makes the cutoff -0.025000000000000008. Preserve this inclusive boundary as well as the now-fixed 100/55 case.

[P2] Multiplication overflows before division restores the intended scale. (1e307, 5), (6e306, 1), (1e306, 0) with max score and "45%" now selects (1e306, 0), outside the tolerance, on both stacks. Preceding and absolute 4.5e306 controls select (6e306, 1). Positive-minimum "20%" similarly admits 2e307 against best 1e307. NumPy2 float16 shows this at ordinary magnitudes: best 10000 with "10%" admits 20000. Avoid the overflowing intermediate without restoring the old rounded boundary.

[P2] Promote signed integers before taking their magnitude. Minimized np.int8(-128) with "10%" now removes every feasible trial and raises ValueError; preceding selects the in-tolerance -120 on both stacks. abs preserves the int8 overflow instead of producing a positive allowance.

[P3] The infinity guard rejects finite Python integers. Metrics around 2**70 now raise TypeError at np.isfinite; preceding handles the same values and selects the in-tolerance trial on both stacks. Classify integral scalars without an unsupported NumPy ufunc conversion.

Please add focused regressions for these inputs. These are new regressions from the latest arithmetic correction, not repeats of the two fixed findings. Approval remains withheld; no prior review was dismissed or foreign thread resolved.

Posted by thinkall-agent-auto-reviewer

Use fractions so large values do not overflow, promote integers before abs(), and keep exact boundaries of both the stored float and its printed decimal.
@anishmehta24

Copy link
Copy Markdown
Contributor Author

Changed in 2708317. The percentage bound is now computed exactly with fractions.Fraction, so it no longer overflows (the 1e307 and float16 cases), and integer bests are converted to Python ints before abs() and compared against an exact integer bound, which fixes int8 -128 and the 2**70 values. A float is kept if it is within the exact bound either as stored or as printed, so 0.025 for "75%" of 0.1 is kept, and so is 0.05151 for "1%" of 0.051, which main keeps and the previous head dropped. All of these inputs have tests.

The ubuntu 3.12 failure on e9d5842 started after the Spark tests failed in that job; test/tune/test_constraints.py failed there as well before the job hit the time limit.

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

Float conversion and integer flooring of tolerance bounds can incorrectly exclude eligible fractional-valued trials, potentially selecting the wrong trial on the next objective.

2 open issues
1 resolved since last review

Review effort: Balanced

Comment thread flaml/tune/tune.py Resolved
Comment thread flaml/tune/tune.py Resolved

@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 previous boundary, finite-overflow, int8-magnitude and large-Python-integer regressions are fixed at 2708317f8f3803b99ace26772dd6098b8b53ce46. All 16 submitted cases pass on both native stacks (32 passes), and the independent controls reproduce the old failures only on the preceding head.

Two current issues are independently corroborated in the existing Copilot discussions; no duplicate inline threads are added.

[P2] Integer best does not imply an integer-only candidate set. In discussion 4718857233, flooring the bound loses valid fractional candidates. Actual public SimpleTrial / ExperimentAnalysis results with losses 1, Fraction(21, 20) and Fraction(6, 5), times 5, 1 and 0, and "10%" tolerance select loss 1 on current source. Both the preceding head and exact main select the eligible 21/20 trial. The same error occurs with Decimal("1.05"). This reproduces on Python 3.10/NumPy 1.26 and Python 3.13/NumPy 2.3. Preserve the rational cutoff rather than flooring solely because the best value is integral.

[P2] Preserve rational best values before computing the cutoff. In discussion 4718857189, losses Fraction(1, 3), Fraction(11, 30) and Fraction(2, 5) with the same times and "10%" select 1/3 on current source, while exact main selects the valid boundary 11/30. Both stacks reproduce it. The preceding head instead raises its known np.isfinite TypeError for this rational best, so I am not claiming a healthy preceding-head control. The new float conversion still discards precision before the exact calculation.

The narrow-float16 fractional comparison limitation also reproduces on exact main and the preceding head; it is retained as inherited, not newly attributed to this correction. Approval remains withheld. No foreign thread was resolved and no prior review was dismissed.

The required independent full-PR review is also blocked at 2/6 valid lenses: four slots produced empty responses, and their one allowed same-model recovery failed again. Those diagnostics are retained, not counted as clean reviews; no additional retry or provider substitution was performed.

Posted by thinkall-agent-auto-reviewer

… lexico_best

An integer best no longer floors the bound, as other values can be fractions or decimals, and fraction and decimal bests are no longer converted to float before the bound is computed.
@anishmehta24

Copy link
Copy Markdown
Contributor Author

Changed in 4ce9870. Integer, fraction and decimal bests now get the exact bound as a Fraction instead of a floored int or a float, so 21/20 and Decimal("1.05") are kept against 1, and 11/30 against 1/3. These cases have tests, plus a decimal best that a float cannot hold exactly.

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.

🔵 Needs a closer look

Human assessment is needed because mixed-numeric and extended-precision edge cases can exclude valid trials, including the best trial, and cause best_result to raise.

No open issues 🎉

2 resolved since last review

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 integer-best fractional/Decimal eligibility and rational 1/3-to-11/30 boundary fixes work at 4ce9870abacff6b00ce71d0b4028b003f79a8f15. All 20 submitted cases pass on each native stack (40 passes). The new correction-only review nevertheless finds three independently reproduced boundary regressions.

[P2] Exact best rejects an eligible float on its printed boundary. Losses Fraction(1) and float 1.1, "10%" tolerance, and times 5/1 now select the exact best instead of the boundary float. Both preceding and exact-main controls select 1.1 on both stacks. Compare heterogeneous candidates according to the promised stored-or-printed float rule; globally widening the exact cutoff would also admit exact values just outside it.

[P2] Decimal maximization rounds the candidate before the exact comparison. At the default precision 28, maximum 5.000000000000000000000000006 and its exact 10% boundary 4.5000000000000000000000000054, with times 5/1, now select the slower maximum on both stacks. Preceding source selects the boundary. Unary negation loses the boundary's final digit before the new exact cutoff is applied; preserve Decimal values during sign normalization. Exact main raises its Decimal/float TypeError, not a healthy control.

[P2] Preserve the original percentage digits for exact metrics. Decimal best 1, tolerance "10.0000000000000001%", and exact boundary 1.100000000000000001, times 5/1, now select the slower best on both stacks; preceding selects the boundary. Parsing through float turns the supplied percentage into 10.0 before the exact ratio is constructed. Exact main again raises its Decimal/float TypeError.

This is a review of the new correction, not a replacement for the earlier blocked full Medium review. Its 2/6 coverage and exhausted recoveries remain retained; no failed result is counted as signoff. No review files were added or pushed to the PR branch, no foreign threads were resolved, and no prior review was dismissed. Approval is withheld.

Posted by thinkall-agent-auto-reviewer

…xico_best

Compare float values with an exact best by the stored-or-printed rule, negate decimals with copy_negate() so no digit is rounded away, and build the ratio from the percentage text instead of its float.
@anishmehta24

Copy link
Copy Markdown
Contributor Author

Changed in dace90e. With an exact best, float values now use the stored-or-printed rule and exact values the exact bound, so 1.1 is kept against Fraction(1) while a fraction or float just above it is not. Decimals are negated with copy_negate() and the ratio is built from the percentage text, so the 28-digit maximum and the "10.0000000000000001%" boundary are kept. All three cases have tests.

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

Zero-tolerance arithmetic can round the bound for a maximized Decimal objective, excluding every feasible trial and causing best_trial to raise.

1 open issue

Review effort: Balanced

Comment thread flaml/tune/tune.py Resolved

@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 three previously reported correction regressions are fixed at dace90e969ef367635fed2f35a8f1b57b8866886: heterogeneous exact/printed-float boundary selection, Decimal maximization beyond the context precision, and preservation of percentage-string digits. The 23 submitted tests pass on both native stacks (46 passes), and the focused public SimpleTrial/ExperimentAnalysis controls now select the expected boundary trial where the preceding head selected the slower best.

This is a native fix-validation update, not independent full-PR approval. The expanded multi-helper, candidate-aware logic requires Medium review. The earlier full review remains at 2/6 valid lenses after exhausted same-model recoveries; that gap is not cleared by new commits or native test results. No blocked slots were restarted, no lower-tier review substituted, and no provider allowance reset.

Approval remains withheld. No review-output files were committed or pushed to the PR branch, no foreign thread was resolved, and no prior review was dismissed.

Posted by thinkall-agent-auto-reviewer

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.

🔵 Needs a closer look

Human assessment is needed because numeric precision and comparison edge cases can admit ineligible trials, exclude the best trial, or raise exceptions.

No open issues 🎉

1 resolved since last review

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 new Decimal absolute-tolerance correction works at 82b70343380b3823d1e3fa9240587347aeee2a61: current public analysis keeps the zero-tolerance maximum, includes the 1E-40 boundary while excluding the next candidate, and retains the negative Decimal minimum on the final objective. The preceding head raises reduction/index errors in these focused cases on both stacks. All 26 current submitted tests pass on each stack (52 passes).

One test-quality follow-up remains: run the regression assertions inside localcontext() with precision28 and explicit ROUND_HALF_EVEN. At precision50, the preceding source passes all three fixture cases without this fix on both stacks, so a caller or earlier test that changes context could mask a regression.

The integral-best plus Decimal-tolerance limitation (10**28 plus tolerance1 excluding Decimal(10**28+1)) reproduces on current AND preceding source at precision28. It is retained as inherited, not newly attributed to this correction. The compact-Decimal/Fraction expansion concern remains an unbenchmarked scalability follow-up.

This is a new-correction review, not a replacement for the earlier blocked full Medium scope. Its 2/6 coverage and exhausted recoveries remain unchanged; no whole-current-head approval is claimed. No review files were added or pushed to the PR branch, no foreign threads were resolved, and no prior review was dismissed.

Posted by thinkall-agent-auto-reviewer

@anishmehta24

Copy link
Copy Markdown
Contributor Author

Changed in f240da6. Both parametrized tests now run inside localcontext(Context(prec=28, rounding=ROUND_HALF_EVEN)), so with the caller at precision 50 the three Decimal cases fail again on dace90e.

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.

🔵 Needs a closer look

Human assessment is needed because critical precision-loss and percentage-overflow edge cases can eliminate all feasible trials or select an incorrect winner.

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.

Reviewed the test-only delta from 82b7034 to f240da6; production code is unchanged. The explicit precision-28, ROUND_HALF_EVEN local Decimal context addresses the preceding context-dependent regression-test gap and restores the caller's context.

The focused test\tune\test_lexico_best.py suite passed 26 tests on Python 3.10/NumPy 1.26 and 26 on Python 3.13/NumPy 2.3, with the same counts on the preceding revision. With caller precision 50 and ROUND_UP, a separately injected native-addition regression now fails all three updated absolute-tolerance tests on both stacks; the preceding tests incorrectly passed. These are expected regression-detection failures, not production failures.

The independent Low test-delta review was clean. This is not whole-PR approval: the required broader review remains incomplete at 2/6 valid assignments, and unchanged production code does not resolve retained full-scope concerns. Approval remains withheld.

Posted by thinkall-agent-auto-reviewer

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