Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor[next]: new ITIR type inference #1531

Open
wants to merge 53 commits into
base: main
Choose a base branch
from

Conversation

tehrengruber
Copy link
Contributor

WIP

tehrengruber added a commit that referenced this pull request Apr 18, 2024
…ir.Ref. (#1532)

In the new ITIR type inference #1531 IR nodes store their type in the node itself. While we initially exclude the attribute from equality comparison we should nonetheless avoid comparison of nodes that only differ in type. This PR removes many of this occurrences.
Copy link
Contributor

@egparedes egparedes left a comment

Choose a reason for hiding this comment

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

First round of review. I focused mainly in general python aspects and skipped the tests and an in-depth review of the type inference algorithm itself, which I'll do in a following review.


new_args = [*args]
for i, (param, arg) in enumerate(
zip(function_type.pos_only_args + list(function_type.pos_or_kw_args.values()), args)
zip(list(function_type.pos_only_args) + list(function_type.pos_or_kw_args.values()), args)
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a question: should zip() be called here with strict=True?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. At some point I split the structural checks (e.g. is the number of arguments correct) and the type checks. Now the structural checks are already run before the types are promoted so we can actually use strict=True here.

@@ -32,6 +31,9 @@
class Node(eve.Node):
location: Optional[SourceLocation] = eve.field(default=None, repr=False, compare=False)

# TODO(tehrengruber): include in comparison if value is not None
Copy link
Contributor

Choose a reason for hiding this comment

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

What does the TODO mean?

"""
Convert to Sym if necessary.

Examples
--------
>>> sym("a")
Sym(id=SymbolName('a'), kind=None, dtype=None)
Sym(id=SymbolName('a'))
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add an example using of type_ != None or mention that case in the docstring ?

(By the way, although it's outside the scope of this PR, the current implementation of ensure_type() doesn't make a lot of sense to me)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added an example. type is not part of the __repr__ of itir.Node which makes the example somewhat odd. Adding it means I need to change many (doct)tests and output is often annoyingly verbose. Should I add it? I'm leaning towards a yes by now.

src/gt4py/next/iterator/ir_utils/ir_makers.py Outdated Show resolved Hide resolved
src/gt4py/next/iterator/transforms/symbol_ref_utils.py Outdated Show resolved Hide resolved
declaration. This is useful for testing or inference on partially inferred sub-nodes.
"""
if not allow_undeclared_symbols:
node = RemoveTypes().visit(node)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you elaborate on this?

src/gt4py/next/iterator/type_system/inference.py Outdated Show resolved Hide resolved
src/gt4py/next/iterator/type_system/inference.py Outdated Show resolved Hide resolved
Comment on lines +243 to +246
if "offset_provider" in inspect.signature(self.type_rule).parameters:
return_type = self.type_rule(*args, offset_provider=self.offset_provider)
else:
return_type = self.type_rule(*args)
Copy link
Contributor

@egparedes egparedes May 3, 2024

Choose a reason for hiding this comment

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

This is ugly and probably not super efficient. I think it would be cleaner to check the signature of the type rule only when registering the rule and set a flag in the returned wrapper exposing whether the wrapped type rule requires the offset provider or not.

return_type = self.type_rule(*args)

# return type is a typing rule by itself
if callable(return_type):
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a comment: checking only if it's callable is slightly dangerous because it's a bit too general and returning by mistake things like a type would be interpreted as a type rule because types are callables (the constructor)

assert isinstance(node.fencil.type, it_ts.FencilType)
return node.fencil.type

def visit_Program(self, node: itir.Program, *, ctx):
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing return type annotation.

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.

None yet

2 participants