Repository navigation
fix(calibrate): keep a ragged pool's fit from vanishing into 1.0 - #1158
NandhaKishorM merged 2 commits into
Conversation
A type-level fit pools every option count that type has seen, so the pairs arrive ragged. Squaring them off put -1e4 in the same softmax each row runs through, which stole probability from rows whose own logits sat under it and overflowed to NaN in float32 once the optimiser drove the scale toward zero -- and clamp_temperature reads a NaN back as the neutral 1.0. Bound the log-space search to LOG_T_MARGIN e-folds outside the answer band (no in-band minimum is lost) and group the pairs by option count so no pad column competes.
…verage Repeating one option count's rows has to move the fit toward the scale that width wants on its own. Averaging the per-width averages instead leaves it where it was, so the pooled objective is checked for a move big enough to be the weighting, not for a strict inequality a float difference would satisfy.
|
Merged in 0.4.5. This is the one real behaviour fix in the release, so I measured the thing that matters most about it: whether it moves a fit that was already correct. It does not. Fifty-four single-width pools, every combination of The ragged case is as broken as you describe. The twelve rows whose every logit sits under the old Pooling used to flip the answer from the floor to the ceiling, and through No shipped payload changes: this is the fitting path, not the reading path, so the 94-row real-weights decision diff against 0.4.4 is identical. Thanks for taking the trouble to show that the objective still averages over rows rather than over widths. That was the first thing I went looking for in the grouped version. |
What
laya/calibrate.py::fit_one_temperaturestops losing fits. Two edits, both inside that onefunction's objective:
(
_pairs_by_widthreplaces_pairs_to_tensors);LOG_T_MARGIN(10.0) e-folds outside the[TEMP_MIN, TEMP_MAX]band it has to answer inside.laya/calibrate.py+34/-14,tests/test_calibrate.py+120. No signature, no payload schema,no public name changes:
LOG_T_MARGINis a module constant besideMIN_TYPE_N, and the answeris still whatever
clamp_temperature(fitted, TEMP_MIN, TEMP_MAX)returns.Why
A question type is fit on every record of that type pooled across option-count bands
(
_group_recordsatlaya/calibrate.py:205, consumed by_fit_groupsat:254, which callsfit_one_temperature(pairs, min_n=MIN_TYPE_N)at:260for the type-level scalar and:266for each bucket). Those pools are ragged: a band like
3-5holds three widths on its own, andthe type-level pool spans all of them. So
fit_one_temperaturegets rows of different lengthsas routine input, not as an edge case.
On
682ac671it used to square them off:_pairs_to_tensorsat:88allocatedtorch.full((len(pairs), kmax), -1e4)(:90). Anyfill value is a real column in the single softmax every row then goes through, so it competes
for the probability mass the fit is allocating. Two things follow, and both end in
clamp_temperature(laya/common.py:828-843) reporting the neutral1.0for a fit that ran:TEMP_MINwalks itpast float32's
exp()underflow (~-88), divides the objective by zero, and turns everygradient into NaN.
Reproducible from the public API with no model weights:
What each arm returns, measured on
682ac671and on this branch (one process per tree, eachprinting the
laya/calibrate.pypath it loaded):682ac6711.00000000000.50000000000.5000000000,0.5000000000lowrows alone0.50000000000.5000000000lowrows pooled with 12 width-4 rows5.00000000000.5000000000fit_temperature_mapon 808 records of one type at widths 2 and 5,temperature[1]5.00.5The NaN path, with the intermediate values of the old objective exposed (its
_pairs_to_tensorsand closure copied verbatim, so this is the old code, not a paraphrase):
The optimum is below the floor for all five, so
TEMP_MINis the right answer and they get 1.0.They are not ragged, which is why the search bound is the part that matters: any pool of
low-spread, mostly-right rows can walk there, and
mag/scale0.3-3.0 is ordinary near-tiedoption text, not a constructed worst case.
temp_bucket(laya/common.py:815) is untouched,MIN_BUCKET_N/MIN_TYPE_Nare untouched, andclamp_temperatureis still the last word, so a value that is a non-number for some other reasonstill lands on 1.0 rather than raising.
How it was verified
tests/test_calibrate.pygoes 257 -> 278 passed, 0 failed; the new section is 21 checks infive labelled groups (
runaway/,pad/,public/,band/,weight/). Full gateall 87 suites passed, 499 passed / 1 skipped, rc 0. ruff rc 0, compileall rc 0, docsstrict build
No issues foundrc 0.No doc file changes here: the public API is unchanged, and
docs/reference/helpers.md:116renders this function's docstring through::: laya.calibrate.fit_one_temperature, so the strict build picked up the new wording on itsown. The built reference page, tags stripped, reads verbatim:
That the new checks are the fix's, not decoration: the same file run against
682ac671'slaya/calibrate.py(base module copied over the branch tree,laya.__file__printed to provewhich copy answered) gives 266 passed, 8 failed, and the eight are exactly the behavioural
ones --
(The three checks that name
_pairs_by_width/LOG_T_MARGINare dropped from that run becausethe base has no such symbol; with them left in, the base run cannot get past the import.)
One check does not compare against a hard-coded number:
band/interior fit beats the floorre-evaluates the objective in float64 and requires the returned T to beat
TEMP_MINand bothsides of itself, so it fails if bounding the search ever hides a real minimum.
Nothing that used to be right moves. 356 single-width configs (8 widths x 4 accuracies x 5 logit
magnitudes x 2 sizes, plus 18 random-logit configs at three widths) were fit on both trees and
diffed at full precision: 351 are bit-identical, including the ones that land strictly inside
the band (
4.89012479782,1.03938007355,4.15759325027), and the 5 that move are exactly the5 non-finite fits above. The base returns 1.0 for 36 of those 356 configs; the other 31 return
1.0 on this branch too, with
steps=1 grad_finite=True log_t=0.000000-- they converged wherethey started because their NLL is already ~0, and this change leaves them alone. One config
(
w=20 acc=0.7 mag=1.0 n=400) sits at0.5222262144088745, genuinely inside the band; thefloat64 objective at that scale is
1.4656239107against1.4686198900at the floor, so it is areal minimum and this branch keeps it to the digit. That is why the bound is 10 e-folds outside
the band rather than on its edge: clamping to the band itself turns the objective flat over the
region a near-floor minimum needs, and that config comes back as
0.5.Mutation check -- five faithful edits to
laya/calibrate.py,tests/test_calibrate.pyaftereach, file restored from
97bb3a60between runs:log_t.exp()), grouping keptrunaway/*atT=1.0pad/*andpublic/*atT=5.0LOG_T_MARGIN = 0.0(bound on the band edge)band/interior fit stays interioratT=0.5LOG_T_MARGIN = 90.0(bound past the underflow)band/the bound stops before float32 exp() underflows,lower edge=-90.69314718055995weight/doubling a width pulls the fit to that width(even=2.0012264251708984 heavy=2.0012261867523193)278 passed, 0 failedThe last mutant is why the
weight/checks exist: with per-width means the fit stops caring howmany rows a width has, and duplicating 8 rows leaves the answer unchanged to 2.4e-07.
Deconfliction: 50 open PRs as at 2026-10-12T06:07Z, re-derived per file -- none edits
laya/calibrate.py. Only #731 (open, last touched 2026-10-01) editstests/test_calibrate.py,at
@@ -16,6 +16,7 @@and@@ -362,6 +363,51 @@; this branch's two hunks are@@ -23,12 +23,14 @@(both import additions in one hunk) and
@@ -177,6 +179,124 @@, so no line is shared -- theirhunk ends at old line 21 and this one starts at 23. #1147 rewrites the prose beside this
(
docs/finetune.md:287, which on682ac671still says the clamp is[0.1, 10]) and calls a typethe optimiser "wants to sharpen below the floor ... gets the bound rather than its own number" --
this branch is what makes that sentence true.
Not fixed here, worth knowing
calibration_report(laya/train.py:754) does notice a type left at 1.0, but its message(
:800) says the items "gave it nothing to correct", which is the wrong story for a NaN fit --those rows had a clear answer. Only
laya/train.py:1139calls it, so a CPU-sidefit_temperature_map/Agent.fit_temperaturescaller gets no diagnostic at all.warn=False(laya/agent.py:1993, note atlaya/calibrate.py:608), sotemperature_rawstores the value afterclamp_temperatureturned the NaN into 1.0. Acalibration file written from such a fit cannot be told apart from one that legitimately
converged at 1.0. Fixing the fit removes the cause; the artifact still keeps no trace.
Checklist
main