Skip to content

fix: read files that contain a single node - #219

Open
AxelNoun wants to merge 2 commits into
neuromorphs:mainfrom
AxelNoun:fix/nir-t02-1-read-single-node
Open

AxelNoun wants to merge 2 commits into
neuromorphs:mainfrom
AxelNoun:fix/nir-t02-1-read-single-node

Conversation

@AxelNoun

Copy link
Copy Markdown
Contributor

nir.write accepts any NIRNode, but a file whose root is a single node cannot be read back: read passes type_check to whatever node it finds, and only NIRGraph takes that argument. On main (3f4b259), with type_check=True or False:

import numpy as np, nir

nir.write("node.nir", nir.Linear(np.ones((3, 2))))
nir.read("node.nir")
# TypeError: Linear.__init__() got an unexpected keyword argument 'type_check'

This worked in v1.0.6 and fails since v1.0.7 (#167).

Fix: only add type_check when the root node is a NIRGraph. Reading graphs is unchanged.

Tests: test_read_single_node writes and reads each of the 18 non-graph node types, with both values of type_check, and compares their fields. All 36 cases fail on main with the error above and pass with this change. Full suite: 115 passed, 5 skipped, plus test_serialize_deserialize_data, which also fails on main on my Windows machine (PermissionError when h5py reopens a NamedTemporaryFile).

I left the return annotation of read (NIRGraph) and its docstring as they are. If files with a single node are not meant to be supported, I can change this to an explicit error in write instead.

@Jegp Jegp left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great find! One comment, otherwise I think we're good to go.

Comment thread tests/test_readwrite.py Outdated
@pytest.mark.parametrize("type_check", [True, False])
@pytest.mark.parametrize(
"node",
[

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to simply find all the possible NIR nodes programmatically, rather than hard code them?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Good point. The node types now come from the registry that nir.read uses (nir.ir.__all_ir, without NIRGraph), so the test covers every type dict2NIRNode can rebuild. The constructor arguments cannot be derived from the class alone (shapes have to be consistent), so there is still one example per type, but a new test fails as soon as a type in the registry has no example. Done in 0738d97.

List the node types of test_read_single_node from the registry used by
dict2NIRNode instead of hard coding them, and fail when a registered type
has no example instance.

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.

2 participants