Skip to content

Enhanced type inference and checking, called from NIRGraph.__init__ - #153

Merged
fabio-innatera merged 12 commits into
neuromorphs:mainfrom
fabio-innatera:type-inference-and-check
Jul 7, 2025
Merged

fabio-innatera merged 12 commits into
neuromorphs:mainfrom
fabio-innatera:type-inference-and-check

Conversation

@fabio-innatera

@fabio-innatera fabio-innatera commented Jun 19, 2025 •

Copy link
Copy Markdown
Collaborator

This is my first stab at doing type inference, in addition to type checking, on graph construction (NIRGraph.__init__).

I believe this PR will lead to some more ecosystem breakage, but in many cases I think it should improve the situation of introducing the type_check parameter recently, because many graphs should now be valid after running type inference automatically on construction.

Notes:

  • Each commit should have passing tests, so we can pick-and-chose if necessary.
  • This also adds nested graph type checking and inference.
  • I reactivated a bunch of tests that were skipped before, now at 52 working tests with 5 skipped, up from 42 and 12 skipped.

Input and Output node handling

We automatically add Input and Output nodes if they are missing from the NIRGraph. Before, we only did this on NIRGraph.from_list, but we now do this on all graphs. This is a requirement for nested graphs, otherwise we can't validate the outputs from nested graphs against the data going in and out of it from the outer graph.

A major break is the requirement of Input and Output nodes to be present for graphs that have no non-ambiguous inputs or outputs. This is the case for certain recurrent connections, for example:

graph LR
A --> B
B --> A
Loading

For this graph, it's impossible to know which one is the input/source node and which one is the output/sink node, so the type inference will throw an error.
This graph with a recurrent connection is fine:

graph LR
input --> A
A --> B
B --> A
A --> output
Loading

@fabio-innatera
fabio-innatera force-pushed the type-inference-and-check branch from 919772e to fe2868d Compare June 23, 2025 15:02
@fabio-innatera
fabio-innatera force-pushed the type-inference-and-check branch from fe2868d to 8f108df Compare June 23, 2025 17:27
@fabio-innatera
fabio-innatera force-pushed the type-inference-and-check branch from 8f108df to 76f2032 Compare June 23, 2025 18:06
@fabio-innatera
fabio-innatera marked this pull request as ready for review June 24, 2025 10:33

@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.

Excellent work! I really appreciate the thought you put into this and the number of tests you added to cover this.

I have one comment around the naming for _forward_type_inference that I'd like your opinion on. Apart from that, I couldn't find any problems. Nice!

You bring a good point that this may mess up old graphs/weirdly shaped graphs. But you neatly wrapped the inference + type checking in the constructor, so I still think it should be possible to build the graphs and ignore the type checking. I retain that the checking should be default and that any choice to ignore the typing should be a conscious choice. So, I honestly like this model.

Comment thread nir/ir/graph.py Outdated
Comment thread nir/ir/graph.py
@fabio-innatera
fabio-innatera merged commit c782af5 into neuromorphs:main Jul 7, 2025
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