Fix shared-parameter checkpoints and check shapes on load - #97
Open
cemde wants to merge 1 commit into
Open
Conversation
Duplicate references were written as a literal None, which numpy stores as a dtype=object array that np.load refuses without allow_pickle. A shared parameter is now saved once, under its first path, and the loader skips the paths it has already restored. Loading a value whose shape does not match its target parameter now raises instead of corrupting the model.
cemde
force-pushed
the
fix/73-serialisation
branch
from
August 9, 2026 18:23
26eaf31 to
603cbb5
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
Two defects in
_serialisation.py.save_paramsdeduplicates shared parameters by writing the value under the first path it meets and a literalNoneunder every later one.np.savez_compressedstores thatNoneas a 0-ddtype=objectarray.load_paramsthen callsnp.load, which refuses object arrays unlessallow_pickle=True.Separately,
load_paramsassigned whatever the file held without comparing shapes.Impact
Every checkpoint of a model using
pxnn.sharedis unrecoverable:The loader's
is not Noneguard was dead code, sincenp.loadyields a 0-d object array and neverNone, so even withallow_pickle=Truethe shared parameter would have been assigned garbage.Without a shape check, a
Linear(5, 7)checkpoint loaded into aLinear(2, 3)leaves the weight at(7, 5), surfacing much later as an inscrutable broadcasting error inside a training step.Fix
The sentinel the writer and reader had to agree on turned out to be no key at all.
save_paramsdrops theelsebranch, so duplicates are simply not written;load_paramsmirrors it with the same_cache()idiom, skipping the same paths. No object array is ever created, so nothing needsallow_pickle.Plus a shape comparison raising
ValueErrorbefore assignment.Compatibility
Old checkpoints still load, including shared ones that previously could not.
np.loadon an.npzis lazy and the loader no longer visits the duplicate paths, so the staledtype=objectentries are never read.The shape check runs only when the target parameter already holds a value. A freshly constructed
Vodehashset toNone, andmodel = Model(); load_params(model, ...)is the documented workflow, so an unconditional_param.shaperaisedAttributeErroron the normal path. Found in review; regression test added, since every sibling test seeded its target first and left that path uncovered.Closes #73