Skip to content

Swap estimated bounds after decreasing operations so integer casting keeps working - #1731

Open
0z5a wants to merge 1 commit into
facebookresearch:mainfrom
0z5a:fix/bounds-decreasing-operations
Open

0z5a wants to merge 1 commit into
facebookresearch:mainfrom
0z5a:fix/bounds-decreasing-operations

Conversation

@0z5a

@0z5a 0z5a commented Nov 14, 2026

Copy link
Copy Markdown

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Motivation and Context / Related issue

Data.bounds forwards the bounds through the operations that come after the bound layer. It keeps them in the same order even when the operation is decreasing, so -ng.p.Scalar(lower=1, upper=5) reports bounds (-1, -5), and 10 / ng.p.Scalar(lower=1, upper=10) reports (10, 1).

The docstring says this estimate "may be inaccurate", but the Int layer depends on it for correctness: it clamps the rounded value between round(lower + 0.5) and round(upper - 0.5). With the bounds swapped, every value ends up clamped to the same integer:

p = (-ng.p.Scalar(lower=1, upper=5)).set_integer_casting()
p.value = -3
p.value  # -5 on main, -3 with this change

RandomSearch on that parameter returns -5 on every ask. ng.p.Scalar(lower=-5, upper=-1).set_integer_casting() covers the same range and returns all of -5..-1. A half-bounded parameter is also wrong: (-ng.p.Scalar(init=3, lower=1)).set_integer_casting().value is -1 instead of -3.

The fix is in Data.bounds:

  • When both bounds are set, take their elementwise min/max after each forwarded operation. This also handles a multiplication by an array with mixed signs.
  • When only one bound is set, forward the missing side as +/-inf to check whether the operation is decreasing. If it is (for all elements), the bound moves to the other side.

Bounds for increasing operations, including Log with one or two bounds, are unchanged. Two cases are still not handled: a half-bounded array multiplied by factors of mixed signs keeps the current behavior, and non-monotonic operations (e.g. x ** 2 over a range containing 0) are still estimated from the bounds alone.

How Has This Been Tested (if it applies)

  • Added test_bound_estimation_decreasing_operation in nevergrad/parametrization/test_layers.py. It covers negation, * -1, 10 / x, 0.5 ** x, and a half-bounded negation, and checks the bounds and that every integer in range round-trips through integer casting. It fails on main for all 5 cases and passes with the change.
  • pytest nevergrad/parametrization passes (238 tests).
  • black, mypy --implicit-reexport and pylint (with the CI flags) report nothing new on the changed files.

Checklist

  • The documentation is up-to-date with the changes I made.
  • I have read the CONTRIBUTING document and completed the CLA.
  • All tests passed, and additional code has been covered with new tests.

This change was drafted with Claude Code (claude-opus-5-5); the test results above are from the runs described.

Data.bounds forwards the bounds through the operations after the bound
layer but kept their order, so -Scalar(lower=1, upper=5) reported
(-1, -5). The Int layer clamps with these bounds, so integer casting
mapped every value to one integer. Take the elementwise min/max when
both bounds are set, and move a single bound to the other side when the
operation is decreasing.
@meta-cla meta-cla Bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label Nov 14, 2026

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

CLA Signed Do not delete this pull request or issue due to inactivity.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant