-
Notifications
You must be signed in to change notification settings - Fork 54
feat[next]: Check inout field #2182
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
Open
SF-N
wants to merge
15
commits into
GridTools:main
Choose a base branch
from
SF-N:check_in_out_field
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 5 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
5e4601c
Add pass which checks if a field that is written to is also read with…
SF-N fedbcca
Account for tuples
SF-N 0a1cb5a
Merge branch 'main' into check_in_out_field
SF-N 55cefb0
Merge branch 'main' into check_in_out_field
SF-N e1dadff
Fix Doctest
SF-N 078f068
Merge branch 'main' into check_in_out_field
SF-N 865a2a0
Refactor tests and extend tuple testcases
SF-N f94bc07
Raise error for as_fielops in as_fielop args, update tests and refact…
SF-N c4460b8
Merge branch 'main' into check_in_out_field
SF-N 2585fce
Minor
SF-N b26f462
Fix some tests
SF-N 6ee3f2d
Fix import
SF-N 92da44d
Fix tests and refactor shift filtering
SF-N 1eed201
Fix filtering
SF-N b8fb723
Merge branch 'main' into check_in_out_field
SF-N 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
106 changes: 106 additions & 0 deletions
106
src/gt4py/next/iterator/transforms/check_inout_field.py
This file contains hidden or 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,106 @@ | ||
| # GT4Py - GridTools Framework | ||
| # | ||
| # Copyright (c) 2014-2024, ETH Zurich | ||
| # All rights reserved. | ||
| # | ||
| # Please, refer to the LICENSE file in the root directory. | ||
| # SPDX-License-Identifier: BSD-3-Clause | ||
|
|
||
| import dataclasses | ||
|
|
||
| from gt4py.eve import NodeTranslator, PreserveLocationVisitor | ||
| from gt4py.next import common | ||
| from gt4py.next.iterator import ir as itir | ||
| from gt4py.next.iterator.ir_utils import common_pattern_matcher as cpm | ||
| from gt4py.next.iterator.transforms import trace_shifts | ||
|
|
||
|
|
||
| @dataclasses.dataclass(frozen=True) | ||
| class CheckInOutField(PreserveLocationVisitor, NodeTranslator): | ||
| """ | ||
| Checks within a SetAt if any fields which are written to are also read with an offset and raises a ValueError in this case. | ||
|
|
||
| Example: | ||
| >>> from gt4py.next.iterator.transforms import infer_domain | ||
| >>> from gt4py.next.type_system import type_specifications as ts | ||
| >>> from gt4py.next.iterator.ir_utils import ir_makers as im | ||
| >>> float_type = ts.ScalarType(kind=ts.ScalarKind.FLOAT64) | ||
| >>> IDim = common.Dimension(value="IDim", kind=common.DimensionKind.HORIZONTAL) | ||
| >>> i_field_type = ts.FieldType(dims=[IDim], dtype=float_type) | ||
| >>> offset_provider = {"IOff": IDim} | ||
| >>> cartesian_domain = im.call("cartesian_domain")( | ||
| ... im.call("named_range")(itir.AxisLiteral(value="IDim"), 0, 5) | ||
| ... ) | ||
| >>> ir = itir.Program( | ||
| ... id="test", | ||
| ... function_definitions=[], | ||
| ... params=[im.sym("inout", i_field_type), im.sym("in", i_field_type)], | ||
| ... declarations=[], | ||
| ... body=[ | ||
| ... itir.SetAt( | ||
| ... expr=im.as_fieldop(im.lambda_("x")(im.deref(im.shift("IOff", 1)("x"))))( | ||
| ... im.ref("inout") | ||
| ... ), | ||
| ... domain=cartesian_domain, | ||
| ... target=im.ref("inout"), | ||
| ... ), | ||
| ... ], | ||
| ... ) | ||
| >>> CheckInOutField.apply(ir, offset_provider=offset_provider) | ||
| Traceback (most recent call last): | ||
| ... | ||
| ValueError: The target inout is also read with an offset. | ||
| """ | ||
|
|
||
| @classmethod | ||
| def apply( | ||
| cls, | ||
| program: itir.Program, | ||
| offset_provider: common.OffsetProvider | common.OffsetProviderType, | ||
| ): | ||
| return cls().visit(program, offset_provider=offset_provider) | ||
|
|
||
| def visit_SetAt(self, node: itir.SetAt, **kwargs) -> itir.SetAt: | ||
| offset_provider = kwargs["offset_provider"] | ||
|
|
||
| def extract_subexprs(expr): | ||
| """Return a list of all subexpressions in expr.args, including expr itself.""" | ||
| subexprs = [expr] | ||
| if hasattr(expr, "args"): | ||
| for arg in expr.args: | ||
| subexprs.extend(extract_subexprs(arg)) | ||
| return subexprs | ||
|
|
||
| def check_expr(fun, args, offset_provider): | ||
| shifts = trace_shifts.trace_stencil(fun, num_args=len(args)) | ||
| for arg, shift in zip(args, shifts): | ||
| arg_subexprs = extract_subexprs(arg) | ||
| target_subexprs = extract_subexprs(node.target) | ||
| for subexpr in arg_subexprs: | ||
| if subexpr in target_subexprs: # Account for im.make_tuple | ||
| if shift not in (set(), {()}): | ||
| # This condition is just to filter out the trivial offsets in the horizontal and vertical. | ||
| if any( | ||
| offset_provider[off.value].kind | ||
| not in { | ||
| common.DimensionKind.HORIZONTAL, | ||
| common.DimensionKind.VERTICAL, | ||
| } | ||
| or val.value != 0 | ||
| for off, val in shift | ||
| ): | ||
| raise ValueError( | ||
| f"The target {node.target} is also read with an offset." | ||
| ) | ||
| if cpm.is_applied_as_fieldop(arg): | ||
| check_expr(arg.fun, arg.args, offset_provider) | ||
|
|
||
| if cpm.is_applied_as_fieldop(node.expr): | ||
| check_expr(node.expr.fun, node.expr.args, offset_provider) | ||
| else: # Account for im.make_tuple | ||
| if hasattr(node.expr, "args"): | ||
SF-N marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for expr in node.expr.args: | ||
| if cpm.is_applied_as_fieldop(expr): | ||
| check_expr(expr.fun, expr.args, offset_provider) | ||
|
|
||
| return node | ||
This file contains hidden or 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.
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 cases are these?
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.
make_tuple,tuple_getand all functions likeplus,maximum, ...I am checking for
FunCallnow.