Repository navigation
fix(calibration): validate fitting inputs and save maps atomically - #846
Conversation
Signed-off-by: Antonio <ajgcvm@gmail.com>
Bruce-Yii
left a comment
There was a problem hiding this comment.
The data-loss fix and the validation are both sound. I verified them on d853cf5 against current main (4aa6761), and #832 merges into this cleanly.
With only your new tests on main, a save that dies part-way through serialization leaves the old file as {"temperature": [ — the working map is gone. On your head a failure at serialization, fsync or replace propagates, leaves the previous bytes intact and leaves no temp file behind. On main all sixteen malformed shapes in your new table are silently accepted — unequal vectors truncated, 1.9 coerced to 1, (True, ...) read as qtype 1, NaN and unnormalised targets passing through; on your head each raises. For input that should not change, I fitted from targets that are the model's own softmax at a known temperature: 0.9 → 0.9009, 1.7 → 1.6974, 2.6 → 2.5947, identical to main, with soft targets, np.int64 indices and padded vectors still working.
The blocker is the test rather than the change. The concurrent-writer check aborts tests/test_calibrate.py on Windows — 18 of 20 runs here end in PermissionError: [WinError 5] from os.replace. Two simultaneous os.replace calls against the same target is an NTFS limitation, not something this rewrite introduced: it reproduces from a bare two-thread os.replace with no Laya code involved. ci.yml's tests (windows) job runs test_calibrate, so merging as-is turns that job red.
Asserting the invariant rather than the absence of an error keeps the check meaningful on both platforms: after two barriered writers the destination must parse as one complete payload, and a writer that lost the race is allowed to raise. The atomicity itself holds on Windows either way — I confirmed the destination stays a complete payload even when one replace loses.
Signed-off-by: Antonio <ajgcvm@gmail.com>
|
Merged for 0.3.24. Verified on the integrated branch rather than on the PR alone: the full shared test list plus the pytest lane green, Thank you. |
What
save_calibrationnow writes to a temporary file in the destination directory, flushes and syncs it, then replaces the destination. A failed save keeps the previous file intact. Existing permissions and symlink targets are preserved, and temporary files are cleaned up on failure.The calibration fitter now rejects malformed records before optimization: fractional or boolean
qtype/k, invalid widths, matrices, non-finite values, mismatched vector lengths, and targets that are negative or do not sum to one. Soft probability targets, NumPy integer indices, and explicitly padded vectors remain supported.fit_one_temperatureuses the same checks, including below the sample floor.Why
The previous save opened the destination with
"w"before serialization finished. A failed write could erase a working calibration file, and two concurrent writers could leave invalid JSON. The fitter also silently truncated mismatched vectors and converted fractional indices withint().This completes the validation and persistence follow-up discussed in #19. #764 already handles payload structure on load; its clamp-and-warn behavior is unchanged. This PR does not include the gradient change in #832.
How it was verified
On macOS with Python 3.12:
Failure tests cover partial serialization,
fsync, replacement, and a failed first save. They check the previous bytes and temporary-file cleanup. A barrier coordinates two writers before replacement; the resulting JSON must equal one complete payload. The concurrent-writer test produced invalid JSON on the unpatched code.A comparison with
mainproduced identical fitted maps for valid synthetic records with soft targets. The public signatures, payload format, and inference calculations are unchanged. No checkpoints were downloaded.Checklist
main