Skip to content

fix(common): a temperature too wide for a double falls back, as the TS port already does - #1411

Closed
aashish254 wants to merge 1 commit into
NandhaKishorM:mainfrom
aashish254:temperature-overflow-double
Closed

aashish254 wants to merge 1 commit into
NandhaKishorM:mainfrom
aashish254:temperature-overflow-double

Conversation

@aashish254

Copy link
Copy Markdown
Contributor

What

clamp_temperature and temperature_rejected catch OverflowError alongside TypeError and ValueError. Six changed characters, one new test method, two new table rows. No input that produces a number today changes answer.

Why

Both functions state a promise the code does not keep for one value:

  • laya/common.py:861 -- "A usable temperature: t confined to [lo, hi], falling back to 1.0 if it is not a number."
  • laya/common.py:887 -- "A value that will not convert is rejected the same way: it has a neutral fallback to report, and the diagnostic must never raise for it."
  • laya/calibrate.py:744 -- "Each temperature is passed through clamp_temperature, so a non-numeric or out-of-range entry cannot crash a later forward the way an unclamped zero used to."

JSON has no double limit. A checkpoint config or calibration file with a temperature wider than a double parses to an exact Python int, and float() of that raises OverflowError, which neither except caught -- so Agent.__init__ (laya/agent.py:757) died during load() instead of clamping and warning. Measured, on a pristine fe9a6c35 tree:

clamp_temperature(10**400)      -> OverflowError: int too large to convert to float
temperature_rejected(10**400)   -> OverflowError: int too large to convert to float
clamp_temperature(-10**400)     -> OverflowError: int too large to convert to float
temperature_rejected(-10**400)  -> OverflowError: int too large to convert to float
clamp_temperature(float('inf')) -> 1.0      (already refused)

The same file loaded by the TypeScript port answers 1.0, because a 400-digit JSON literal parses to the nearest double -- node -e "console.log(JSON.parse('{\"t\": ' + '9'.repeat(400) + '}').t)" prints Infinity -- and clampTemperature already has if (!Number.isFinite(f)) return 1.0; at laya-ts/src/common.ts:453. So one checkpoint field gave a Python user a crash and a JS user a working neutral fallback: the ports disagree, and Python is the half that broke its own docstring.

The module family already treats this exception as "does not read as a number" -- confidence.py:67 (merged yesterday as #1405), calibrate.py:80 and calibrate.py:780 each name OverflowError for the same reason.

How it was verified

python -B tests/test_temperature_loading.py   # 11 tests, OK          (same file at base: Ran 10, OK)
python -B tests/test_calibrate.py             # 375 passed, 0 failed  (base: 375 passed, 0 failed)
ruff check laya/ --select=E9,F63,F7,F82,F401,F811 --line-length=120
python -m compileall -q laya/ tests/

tests/test_temperature_loading.py gains test_overflowing_integer_entries_use_neutral_fallback_and_warn, which loads a real tiny checkpoint whose temperature[0] and choice:2 are the 400-digit value and asserts the neutral 1.0, the single warning naming both entries, and the resulting predictions -- the same shape as the existing test_nonfinite_values_keep_neutral_fallback_and_warn. Two rows go into the shared predicate table ((10 ** 400, 1.0, True), (-(10 ** 400), 1.0, True)), which the file's own docstring says is also the ONNX site's coverage.

Mutation arm, both except tuples reverted to two types with the tests kept:

Ran 11 tests in 0.187s
FAILED (errors=4)
  ... line 892, in temperature_rejected
    value = float(raw)
OverflowError: int too large to convert to float

Nothing moves in the numbers: tests/test_calibrate.py prints the identical line in both trees -- synthetic ECE before=0.3967 after=0.2674 choice:2 T=5.000 noul:2 T=5.000 choice:11+ T=5.000 -- so no fitted temperature or confidence changes; only a crash becomes the fallback the prose promises. laya-java/scripts/gen_fixtures.py --check reports the same two inherited stales as a base tree (lang_detect.json, python_json.json -- the Linux/macOS regeneration noted at gen_fixtures.py:1922), and neither family reads a temperature.

Deconfliction: laya/common.py is in two open PRs (#921, #687), whose last hunks in that file are at -707,13 and -199,8 -- both before line 860 -- and tests/test_temperature_loading.py is in none. git merge-tree --write-tree of this commit onto each of their heads reports Auto-merging laya/common.py with no conflict (the only conflicts are in files neither of us shares with the other, from their own drift against main).

Checklist

  • Focused on one change (split unrelated work into another PR)
  • Rebased on the latest main (fe9a6c35, current HEAD)
  • Tests pass locally (11 OK; 10 at base; mutation arm fails with 4 errors)
  • Docs or examples updated when the public API changed -- no signature or public surface changed; this makes clamp_temperature's and temperature_rejected's existing prose, and the load promise at calibrate.py:744, true

Fixes #

…port already does

clamp_temperature promises the neutral 1.0 for a value that is not a number and
temperature_rejected promises the diagnostic never raises, but float() of a JSON
integer literal wider than a double raises OverflowError, which neither caught.
Loading such a checkpoint failed in Agent.__init__ while laya-ts returned 1.0 for
the same file, because JS parses it to Infinity and its clamp already tests for a
non-finite number.
@aashish254

Copy link
Copy Markdown
Contributor Author

Closing this myself: the change is wrong for this repo, and the .NET lane proved it.

laya-dotnet/tools/calibration_cases.py:236-238 records three cases specifically under the heading "an integer too large for a float", and the .NET job regenerates the goldens from Python (dotnet.yml "Regenerate the routing goldens from Python") and replays them through CalibrationFile.Apply. The committed probe pins Python's answer for those inputs as a raise, so the OverflowError I removed is the cross-port oracle, not a leak:

base:      a temperature that is a huge integer  -> error: OverflowError int too large to convert to float
this branch:                                   -> error: None, warning "calibration temperatures are invalid or outside [0.5, 5]", temperature [1.0, 1.0, 1.0]

dump_routing_golden.py:370 even names it: "ValueError by contract; an OverflowError is a finding, so everything is recorded." AppliesAPayloadLikePython(index: 149/150/151) failed for exactly that reason. Moving that oracle would mean regenerating the golden and changing every port that replays it, which is not what this PR was for.

The laya-ts divergence I described in the body is real, but the repo has chosen Python's raise as the reference behaviour here, so the right fix is the TS side, not this one. Sorry for the noise.

@aashish254 aashish254 closed this Oct 28, 2026
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.

1 participant