-
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]: Infer as_fieldop type without domain #1853
Open
SF-N
wants to merge
34
commits into
GridTools:main
Choose a base branch
from
SF-N:infer_as_fieldop_type_without_domain
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 31 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
9b856cd
New global ordering relation of dimensions
SF-N a23c6cb
Minor text edits
SF-N 545f98e
Merge branch 'main' into decouple_inferences
SF-N c851f8f
Use new promote_dims and update tests
SF-N 74e4584
Merge branch 'main' into decouple_inferences
SF-N 31a611d
Minor fix
SF-N 428a979
Remove unnecesary test
SF-N 4c2b63b
Fix Doctests
SF-N 279fbb0
Merge branch 'main' into decouple_inferences
SF-N 2f91dcf
Add functionality to ifer return type of as_fieldop without domain, n…
SF-N fbfce3e
Merge branch 'main' into infer_as_fieldop_type_without_domain
SF-N c0849a4
Merge branch 'main' into decouple_inferences
SF-N 98e424d
Merge branch 'main' into infer_as_fieldop_type_without_domain
SF-N 392153a
clean up type inference of as_fieldop
SF-N 360c10f
Working on tuples
SF-N da549c0
Some more fixes
SF-N 462b672
Run pre-commit
SF-N 9050385
Merge branch 'main' into decouple_inferences
SF-N e0be279
Merge branch 'main' into infer_as_fieldop_type_without_domain
SF-N 5b2da27
Fix some tests
SF-N 2a9a25a
Merge branch 'main' into decouple_inferences
SF-N e1c2bef
Merge branch 'main' into infer_as_fieldop_type_without_domain
SF-N 9e48581
Merge branch 'main' into decouple_inferences
SF-N 8bea1b5
Some fixes and add more tests
SF-N 249ba8b
Merge branch 'main' into infer_as_fieldop_type_without_domain
SF-N 2820315
Merge branch 'main' into decouple_inferences
SF-N c6a841a
Address review comments
SF-N bd66acb
Fix tests
SF-N e5ed262
Fix test
SF-N 6627c8f
Merge decouple_inferences
SF-N 392a91d
Fix tests
SF-N 435df54
Address some review comments
SF-N 657bd9d
Fix for no offset_provider_type
SF-N dd3e6db
Merge branch 'main' into infer_as_fieldop_type_without_domain
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,9 @@ | |
|
||
from gt4py.eve.extended_typing import Callable, Iterable, Optional, Union | ||
from gt4py.next import common | ||
from gt4py.next.iterator import builtins | ||
from gt4py.next.ffront import fbuiltins | ||
from gt4py.next.iterator import builtins, ir as itir | ||
from gt4py.next.iterator.transforms import trace_shifts | ||
from gt4py.next.iterator.type_system import type_specifications as it_ts | ||
from gt4py.next.type_system import type_info, type_specifications as ts | ||
from gt4py.next.utils import tree_map | ||
|
@@ -302,6 +304,7 @@ def as_fieldop( | |
# information on the ordering of dimensions. In this example | ||
# `as_fieldop(it1, it2 -> deref(it1) + deref(it2))(i_field, j_field)` | ||
# it is unclear if the result has dimension I, J or J, I. | ||
# Todo: update comment | ||
if domain is None: | ||
domain = it_ts.DomainType(dims="unknown") | ||
|
||
|
@@ -314,15 +317,93 @@ def applied_as_fieldop(*fields) -> ts.FieldType | ts.DeferredType: | |
): | ||
return ts.DeferredType(constraint=None) | ||
|
||
output_dims = [] | ||
for i, field in enumerate(fields): | ||
input_dims = common.promote_dims( | ||
*[field.dims if isinstance(field, ts.FieldType) else []] | ||
) | ||
seen = set() | ||
if isinstance(stencil.node, itir.Expr): | ||
shifts_results = trace_shifts.trace_stencil( | ||
stencil.node, num_args=len(fields) | ||
) # TODO: access node differently? | ||
|
||
def resolve_shift( | ||
input_dim: common.Dimension, shift_tuple: tuple[itir.OffsetLiteral, ...] | ||
) -> common.Dimension | None: | ||
""" | ||
Resolves the final dimension by applying shifts from the given shift tuple. | ||
|
||
Iterates through the shift tuple, updating `input_dim` based on matching offsets. | ||
|
||
Parameters: | ||
- input_dim (common.Dimension): The initial dimension to resolve. | ||
- shift_tuple (tuple[itir.OffsetLiteral, ...]): A tuple of offset literals defining the shift. | ||
|
||
Returns: | ||
- common.Dimension | None: The resolved dimension or `None` if no shift is applied. | ||
""" | ||
if not shift_tuple or not offset_provider_type: | ||
return None | ||
|
||
final_target: common.Dimension | None = None | ||
|
||
for off_literal in shift_tuple[::2]: | ||
offset_type = offset_provider_type[off_literal.value] # type: ignore [index] # ensured by accessing only every second element | ||
if ( | ||
isinstance(offset_type, common.Dimension) and input_dim == offset_type | ||
): # no shift applied | ||
return offset_type | ||
if isinstance( | ||
offset_type, (fbuiltins.FieldOffset, common.NeighborConnectivityType) | ||
): | ||
off_source = ( | ||
offset_type.source | ||
if isinstance(offset_type, fbuiltins.FieldOffset) | ||
else (offset_type.codomain) | ||
) | ||
off_targets = ( | ||
offset_type.target | ||
if isinstance(offset_type, fbuiltins.FieldOffset) | ||
else (offset_type.domain) | ||
) | ||
|
||
if input_dim == off_source: # Check if input fits to offset | ||
for target in off_targets: | ||
if ( | ||
target.value != off_literal.value | ||
): # Exclude target matching off_literal.value | ||
final_target = target | ||
input_dim = target # Update input_dim for next iteration | ||
return final_target | ||
|
||
if any(shifts_results[i]): | ||
for input_dim in input_dims: | ||
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. Change ordering to:
for readability. |
||
for shift_tuple in shifts_results[ | ||
i | ||
]: # Use shift tuple corresponding to the input field | ||
final_dim = ( | ||
resolve_shift(input_dim, shift_tuple) or input_dim | ||
) # If ther are no shifts, take input_dim | ||
if final_dim not in seen: | ||
seen.add(final_dim) | ||
output_dims.append(final_dim) | ||
else: | ||
output_dims.extend(input_dims) | ||
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.
|
||
else: | ||
output_dims = domain.dims | ||
|
||
stencil_return = stencil( | ||
*(_convert_as_fieldop_input_to_iterator(domain, field) for field in fields), | ||
offset_provider_type=offset_provider_type, | ||
) | ||
|
||
assert isinstance(stencil_return, ts.DataType) | ||
return type_info.apply_to_primitive_constituents( | ||
lambda el_type: ts.FieldType(dims=domain.dims, dtype=el_type) | ||
if domain.dims != "unknown" | ||
else ts.DeferredType(constraint=ts.FieldType), | ||
lambda el_type: ts.FieldType( | ||
dims=sorted({dim for dim in output_dims}, key=lambda dim: (common.dims_kind_order[dim.kind], dim.value)) if output_dims != "unknown" else [], | ||
dtype=el_type, | ||
), | ||
stencil_return, | ||
) | ||
|
||
|
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
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.
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.
I think the subset requirement is not necessary anymore, e.g. in the case
with
im.sym("inp", float_vertex_v2e_k_field)
andim.sym("out", float_vertex_k_field)
the expr_type is
and the target_type is
which is correct in my opinion.
cf.
test_fencil_with_nb_field_input