Skip to content

fix: do not mutate the input dictionary in from_dict - #210

Open
AxelNoun wants to merge 1 commit into
neuromorphs:mainfrom
AxelNoun:fix/nir-2-from-dict-copy
Open

AxelNoun wants to merge 1 commit into
neuromorphs:mainfrom
AxelNoun:fix/nir-2-from-dict-copy

Conversation

@AxelNoun

Copy link
Copy Markdown
Contributor

Input, Output, Flatten, IF, LIF and CubaLIF override from_dict and modify the dictionary they receive before calling NIRNode.from_dict (shape is renamed to input_type/output_type, v_reset is added). As a consequence, calling from_dict twice on the same dictionary fails:

import numpy as np, nir

d = nir.NIRGraph.from_list(nir.Linear(np.ones((2, 3)))).to_dict()
nir.NIRGraph.from_dict(d)
nir.NIRGraph.from_dict(d)  # KeyError: 'shape'

#148 added a copy to NIRNode.from_dict, but these six overrides write to the argument before reaching it. This PR takes a shallow copy at the top of each of them. Nothing else changes; numpy arrays are still shared, as before.

Tests: test_from_dict_does_not_mutate_input (parametrized over the six node types, including dictionaries without v_reset as written by older versions) and test_graph_from_dict_does_not_mutate_input check that the dictionary is unchanged and that a second call succeeds. They fail on main and pass with this change. Full suite: 86 passed, 5 skipped, plus test_serialize_deserialize_data, which fails on main as well on my Windows machine (PermissionError when h5py reopens a NamedTemporaryFile).

This contribution was prepared with the help of an AI assistant (Claude); I reviewed and tested it.

Input, Output, Flatten, IF, LIF and CubaLIF rewrote the dictionary passed
to from_dict before calling NIRNode.from_dict, so a second call on the same
dictionary failed with KeyError: 'shape'. Copy it first, as neuromorphs#148 did for
NIRNode.from_dict.

This branch has not been deployed

No deployments
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