Repository navigation
fix(common): a temperature too wide for a double falls back, as the TS port already does - #1411
aashish254 wants to merge 1 commit into
Conversation
…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.
|
Closing this myself: the change is wrong for this repo, and the .NET lane proved it.
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. |
What
clamp_temperatureandtemperature_rejectedcatchOverflowErroralongsideTypeErrorandValueError. 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:tconfined 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 throughclamp_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, andfloat()of that raisesOverflowError, which neitherexceptcaught -- soAgent.__init__(laya/agent.py:757) died duringload()instead of clamping and warning. Measured, on a pristinefe9a6c35tree: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)"printsInfinity-- andclampTemperaturealready hasif (!Number.isFinite(f)) return 1.0;atlaya-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:80andcalibrate.py:780each nameOverflowErrorfor the same reason.How it was verified
tests/test_temperature_loading.pygainstest_overflowing_integer_entries_use_neutral_fallback_and_warn, which loads a real tiny checkpoint whosetemperature[0]andchoice:2are 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 existingtest_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
excepttuples reverted to two types with the tests kept:Nothing moves in the numbers:
tests/test_calibrate.pyprints 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 --checkreports the same two inherited stales as a base tree (lang_detect.json,python_json.json-- the Linux/macOS regeneration noted atgen_fixtures.py:1922), and neither family reads a temperature.Deconfliction:
laya/common.pyis in two open PRs (#921, #687), whose last hunks in that file are at-707,13and-199,8-- both before line 860 -- andtests/test_temperature_loading.pyis in none.git merge-tree --write-treeof this commit onto each of their heads reportsAuto-merging laya/common.pywith no conflict (the only conflicts are in files neither of us shares with the other, from their own drift againstmain).Checklist
main(fe9a6c35, current HEAD)clamp_temperature's andtemperature_rejected's existing prose, and the load promise atcalibrate.py:744, trueFixes #