-
Notifications
You must be signed in to change notification settings - Fork 49
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
feat[next]: Flatten as_fieldop
tuple arguments
#1873
Open
tehrengruber
wants to merge
11
commits into
GridTools:main
Choose a base branch
from
tehrengruber:collapse_tuple_as_fieldop_args
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d16bbd5
ITIR type inference: store param type in Lambda
tehrengruber 813f328
Flatten as_fieldop tuple arguments
tehrengruber 3745461
Add support for scan and nested tuples
tehrengruber fc20d7c
Fix doctest
tehrengruber fa17228
Merge branch 'store_lambda_param_type' into collapse_tuple_as_fieldop…
tehrengruber 335e932
Fix broken scan (e.g. test_tuple_scalar_scan)
tehrengruber ba03c7e
Merge remote-tracking branch 'origin/main' into collapse_tuple_as_fie…
tehrengruber d399c65
Fix format
tehrengruber 5ad7701
Fix failing tests
tehrengruber d3957bd
Fix format
tehrengruber b52a07c
Cleanup
tehrengruber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
from gt4py import eve | ||
from gt4py.eve import utils as eve_utils | ||
from gt4py.next.iterator import ir as itir | ||
from gt4py.next.iterator.ir_utils import ir_makers as im | ||
from gt4py.next.iterator.ir_utils import common_pattern_matcher as cpm, ir_makers as im | ||
|
||
|
||
@dataclasses.dataclass(frozen=True) | ||
|
@@ -71,3 +71,79 @@ def is_equal(a: itir.Expr, b: itir.Expr): | |
return a == b or ( | ||
CannonicalizeBoundSymbolNames.apply(a) == CannonicalizeBoundSymbolNames.apply(b) | ||
) | ||
|
||
|
||
def canonicalize_as_fieldop(expr: itir.FunCall) -> itir.FunCall: | ||
""" | ||
Canonicalize applied `as_fieldop`s. | ||
|
||
In case the stencil argument is a `deref` wrap it into a lambda such that we have a unified | ||
format to work with (e.g. each parameter has a name without the need to special case). | ||
""" | ||
assert cpm.is_applied_as_fieldop(expr) | ||
|
||
stencil = expr.fun.args[0] # type: ignore[attr-defined] | ||
domain = expr.fun.args[1] if len(expr.fun.args) > 1 else None # type: ignore[attr-defined] | ||
if cpm.is_ref_to(stencil, "deref"): | ||
stencil = im.lambda_("arg")(im.deref("arg")) | ||
new_expr = im.as_fieldop(stencil, domain)(*expr.args) | ||
|
||
return new_expr | ||
|
||
return expr | ||
|
||
|
||
def _remove_let_alias(let_expr: itir.FunCall): | ||
assert cpm.is_let(let_expr) | ||
is_aliased_let = True | ||
for param, arg in zip(let_expr.fun.params, let_expr.args, strict=True): # type: ignore[attr-defined] # ensured by cpm.is_let | ||
is_aliased_let &= cpm.is_ref_to(arg, param.id) | ||
if is_aliased_let: | ||
return let_expr.fun.expr # type: ignore[attr-defined] # ensured by cpm.is_let | ||
return let_expr | ||
|
||
|
||
def unwrap_scan(stencil: itir.Lambda | itir.FunCall): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function has been moved, the only addition is the |
||
""" | ||
If given a scan, extract stencil part of its scan pass and a back-transformation into a scan. | ||
|
||
If a regular stencil is given the stencil is left as-is and the back-transformation is the | ||
identity function. This function allows treating a scan stencil like a regular stencil during | ||
a transformation avoiding the complexity introduced by the different IR format. | ||
|
||
>>> scan = im.call("scan")( | ||
... im.lambda_("state", "arg")(im.plus("state", im.deref("arg"))), True, 0.0 | ||
... ) | ||
>>> stencil, back_trafo = unwrap_scan(scan) | ||
>>> str(stencil) | ||
'λ(arg) → state + ·arg' | ||
>>> str(back_trafo(stencil)) | ||
'scan(λ(state, arg) → state + ·arg, True, 0.0)' | ||
|
||
In case a regular stencil is given it is returned as-is: | ||
|
||
>>> deref_stencil = im.lambda_("it")(im.deref("it")) | ||
>>> stencil, back_trafo = unwrap_scan(deref_stencil) | ||
>>> assert stencil == deref_stencil | ||
""" | ||
if cpm.is_call_to(stencil, "scan"): | ||
scan_pass, direction, init = stencil.args | ||
assert isinstance(scan_pass, itir.Lambda) | ||
# remove scan pass state to be used by caller | ||
state_param = scan_pass.params[0] | ||
stencil_like = im.lambda_(*scan_pass.params[1:])(scan_pass.expr) | ||
|
||
def restore_scan(transformed_stencil_like: itir.Lambda): | ||
new_scan_pass = im.lambda_(state_param, *transformed_stencil_like.params)( | ||
_remove_let_alias( | ||
im.call(transformed_stencil_like)( | ||
*(param.id for param in transformed_stencil_like.params) | ||
) | ||
) | ||
) | ||
return im.call("scan")(new_scan_pass, direction, init) | ||
|
||
return stencil_like, restore_scan | ||
|
||
assert isinstance(stencil, itir.Lambda) | ||
return stencil, lambda s: s |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function has only been moved without changes.