-
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
feature[next]: Inline center deref lift vars #1455
Merged
tehrengruber
merged 19 commits into
GridTools:main
from
tehrengruber:inline_center_deref_lift_vars
Mar 6, 2024
Merged
Changes from 12 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
2924314
Initial commit extracted from icon4py_compilation_time_improvements
tehrengruber f982fdc
Cleanup
tehrengruber d0171c1
Cleanup
tehrengruber 1656ef9
Cleanup
tehrengruber 6f8912d
Cleanup
tehrengruber 8912360
Cleanup
tehrengruber d9e629e
Merge remote-tracking branch 'origin/main' into inline_center_deref_l…
tehrengruber 266bb7c
Cleanup
tehrengruber 40b131c
Cleanup
tehrengruber 9d1558b
Cleanup
tehrengruber a7a264e
Merge remote-tracking branch 'origin/main' into inline_center_deref_l…
tehrengruber 73f1ed4
Fix small typo
tehrengruber 0c73797
Cleanup
tehrengruber f5a6b49
Merge commit 'ea852984dbf22ec0f2bb72ed454d7a1392040478' into inline_c…
tehrengruber e110536
Apply ruff changes
tehrengruber ce0f7d8
Merge commit '4c8f706a9f3cceff946f128022390c406523a7a1' into inline_c…
tehrengruber 9a36233
Merge commit '77a205b6b31d9854e0e15d01d91349047ec0c426' into inline_c…
tehrengruber 2245f93
Merge remote-tracking branch 'origin/main' into inline_center_deref_l…
tehrengruber d5a50c8
Retrigger CI
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
101 changes: 101 additions & 0 deletions
101
src/gt4py/next/iterator/transforms/inline_center_deref_lift_vars.py
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 |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# GT4Py - GridTools Framework | ||
# | ||
# Copyright (c) 2014-2023, ETH Zurich | ||
# All rights reserved. | ||
# | ||
# This file is part of the GT4Py project and the GridTools framework. | ||
# GT4Py is free software: you can redistribute it and/or modify it under | ||
# the terms of the GNU General Public License as published by the | ||
# Free Software Foundation, either version 3 of the License, or any later | ||
# version. See the LICENSE.txt file at the top-level directory of this | ||
# distribution for a copy of the license or check <https://www.gnu.org/licenses/>. | ||
# | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
import dataclasses | ||
from typing import ClassVar, Optional | ||
|
||
import gt4py.next.iterator.ir_utils.common_pattern_matcher as common_pattern_matcher | ||
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.transforms.inline_lambdas import inline_lambda | ||
from gt4py.next.iterator.transforms.inline_lifts import InlineLifts | ||
from gt4py.next.iterator.transforms.trace_shifts import TraceShifts, copy_recorded_shifts | ||
|
||
|
||
def is_center_derefed_only(node: itir.Node) -> bool: | ||
return hasattr(node.annex, "recorded_shifts") and node.annex.recorded_shifts in [set(), {()}] | ||
|
||
|
||
@dataclasses.dataclass | ||
class InlineCenterDerefLiftVars(eve.NodeTranslator): | ||
""" | ||
Inline all variables which are derefed in the center only (i.e. unshifted). | ||
|
||
Consider the following example where `var` is never shifted: | ||
|
||
`let(var, (↑stencil)(it))(·var + ·var)` | ||
|
||
Directly inlining `var` would increase the size of the tree and duplicate the calculation. | ||
Instead, this pass computes the value at the current location once and replaces all previous | ||
references to `var` by an applied lift which captures this value. | ||
|
||
`let(_icdlv_1, stencil(it))(·(↑(λ() → _icdlv_1) + ·(↑(λ() → _icdlv_1))` | ||
|
||
The lift inliner can then later easily transform this into a nice expression: | ||
|
||
`let(_icdlv_1, stencil(it))(_icdlv_1 + _icdlv_1)` | ||
|
||
Note: This pass uses and preserves the `recorded_shifts` annex. | ||
""" | ||
|
||
PRESERVED_ANNEX_ATTRS: ClassVar[tuple[str, ...]] = ("recorded_shifts",) | ||
|
||
uids: eve_utils.UIDGenerator | ||
|
||
@classmethod | ||
def apply(cls, node: itir.FencilDefinition, uids: Optional[eve_utils.UIDGenerator] = None): | ||
if not uids: | ||
uids = eve_utils.UIDGenerator() | ||
return cls(uids=uids).visit(node) | ||
|
||
def visit_StencilClosure(self, node: itir.StencilClosure, **kwargs): | ||
# TODO(tehrengruber): move the analysis out of this pass and just make it a requirement | ||
# such that we don't need to run in multiple times if multiple passes use it. | ||
TraceShifts.apply(node, save_to_annex=True) | ||
return self.generic_visit(node, **kwargs) | ||
|
||
def visit_FunCall(self, node: itir.FunCall, **kwargs): | ||
node = self.generic_visit(node) | ||
if common_pattern_matcher.is_let(node): | ||
assert isinstance(node.fun, itir.Lambda) # to make mypy happy | ||
eligible_params = [False] * len(node.fun.params) | ||
new_args = [] | ||
bound_scalars: dict[str, itir.Expr] = {} | ||
|
||
for i, (param, arg) in enumerate(zip(node.fun.params, node.args)): | ||
if common_pattern_matcher.is_applied_lift(arg) and is_center_derefed_only(param): | ||
eligible_params[i] = True | ||
bound_arg_name = self.uids.sequential_id(prefix="_icdlv") | ||
capture_lift = im.promote_to_const_iterator(bound_arg_name) | ||
copy_recorded_shifts(from_=param, to=capture_lift) | ||
new_args.append(capture_lift) | ||
# since we deref an applied lift here we can (but don't need to) immediately | ||
# inline | ||
bound_scalars[bound_arg_name] = InlineLifts( | ||
flags=InlineLifts.Flag.INLINE_TRIVIAL_DEREF_LIFT | ||
).visit(im.deref(arg), recurse=False) | ||
else: | ||
new_args.append(arg) | ||
|
||
if any(eligible_params): | ||
new_node = inline_lambda( | ||
im.call(node.fun)(*new_args), | ||
eligible_params=eligible_params, | ||
) | ||
# TODO(tehrengruber): propagate let outwards | ||
return im.let(*bound_scalars.items())(new_node) # type: ignore[arg-type] # mypy not smart enough | ||
|
||
return node |
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
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
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
69 changes: 69 additions & 0 deletions
69
...xt_tests/unit_tests/iterator_tests/transforms_tests/test_inline_center_deref_lift_vars.py
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# GT4Py - GridTools Framework | ||
# | ||
# Copyright (c) 2014-2023, ETH Zurich | ||
# All rights reserved. | ||
# | ||
# This file is part of the GT4Py project and the GridTools framework. | ||
# GT4Py is free software: you can redistribute it and/or modify it under | ||
# the terms of the GNU General Public License as published by the | ||
# Free Software Foundation, either version 3 of the License, or any later | ||
# version. See the LICENSE.txt file at the top-level directory of this | ||
# distribution for a copy of the license or check <https://www.gnu.org/licenses/>. | ||
# | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
from gt4py.next.iterator import ir as itir | ||
from gt4py.next.iterator.ir_utils import ir_makers as im | ||
from gt4py.next.iterator.transforms.inline_center_deref_lift_vars import InlineCenterDerefLiftVars | ||
|
||
|
||
def wrap_in_fencil(expr: itir.Expr) -> itir.FencilDefinition: | ||
return itir.FencilDefinition( | ||
id="f", | ||
function_definitions=[], | ||
params=[im.sym("d"), im.sym("inp"), im.sym("out")], | ||
closures=[ | ||
itir.StencilClosure( | ||
domain=im.call("cartesian_domain")(), | ||
stencil=im.lambda_("it")(expr), | ||
output=im.ref("out"), | ||
inputs=[im.ref("inp")], | ||
) | ||
], | ||
) | ||
|
||
|
||
def unwrap_from_fencil(fencil: itir.FencilDefinition) -> itir.Expr: | ||
return fencil.closures[0].stencil.expr | ||
|
||
|
||
def test_simple(): | ||
testee = im.let("var", im.lift("deref")("it"))(im.deref("var")) | ||
expected = "(λ(_icdlv_1) → ·(↑(λ() → _icdlv_1))())(·it)" | ||
|
||
actual = unwrap_from_fencil(InlineCenterDerefLiftVars.apply(wrap_in_fencil(testee))) | ||
assert str(actual) == expected | ||
|
||
|
||
def test_double_deref(): | ||
testee = im.let("var", im.lift("deref")("it"))(im.plus(im.deref("var"), im.deref("var"))) | ||
expected = "(λ(_icdlv_1) → ·(↑(λ() → _icdlv_1))() + ·(↑(λ() → _icdlv_1))())(·it)" | ||
|
||
actual = unwrap_from_fencil(InlineCenterDerefLiftVars.apply(wrap_in_fencil(testee))) | ||
assert str(actual) == expected | ||
|
||
|
||
def test_deref_at_non_center_different_pos(): | ||
testee = im.let("var", im.lift("deref")("it"))(im.deref(im.shift("I", 1)("var"))) | ||
|
||
actual = unwrap_from_fencil(InlineCenterDerefLiftVars.apply(wrap_in_fencil(testee))) | ||
assert testee == actual | ||
|
||
|
||
def test_deref_at_multiple_pos(): | ||
testee = im.let("var", im.lift("deref")("it"))( | ||
im.plus(im.deref("var"), im.deref(im.shift("I", 1)("var"))) | ||
) | ||
|
||
actual = unwrap_from_fencil(InlineCenterDerefLiftVars.apply(wrap_in_fencil(testee))) | ||
assert testee == actual |
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
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.
What's the TODO here?
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.
... in other passes