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

feat[next]: Index builtin #1699

Merged
merged 12 commits into from
Nov 4, 2024
Merged

feat[next]: Index builtin #1699

merged 12 commits into from
Nov 4, 2024

Conversation

SF-N
Copy link
Contributor

@SF-N SF-N commented Oct 22, 2024

Adds index builtin for embedded and gtfn backends.

@@ -31,9 +31,9 @@
INT_LITERAL: SIGNED_INT
FLOAT_LITERAL: SIGNED_FLOAT
OFFSET_LITERAL: ( INT_LITERAL | CNAME ) "ₒ"
_literal: INT_LITERAL | FLOAT_LITERAL | OFFSET_LITERAL
AXIS_LITERAL: CNAME ("ᵥ" | "ₕ")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed for consistency.

@@ -31,9 +31,9 @@
INT_LITERAL: SIGNED_INT
FLOAT_LITERAL: SIGNED_FLOAT
OFFSET_LITERAL: ( INT_LITERAL | CNAME ) "ₒ"
_literal: INT_LITERAL | FLOAT_LITERAL | OFFSET_LITERAL
AXIS_LITERAL: CNAME ("ᵥ" | "ₕ")
_literal: INT_LITERAL | FLOAT_LITERAL | OFFSET_LITERAL | AXIS_LITERAL
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Otherwise index(OFFSET_LITERAL) isn't parsed properly.

Copy link
Contributor

Choose a reason for hiding this comment

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

Do you mean index(AXIS_LITERAL)?

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

@@ -611,7 +611,6 @@ def convert_el_to_sid(el_expr: Expr, el_type: ts.ScalarType | ts.FieldType) -> E
tuple_constructor=lambda *elements: SidComposite(values=list(elements)),
)

assert isinstance(lowered_input_as_sid, (SidComposite, SidFromScalar, SymRef))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This check was superfluous from the beginning since the format is checked anyway when the SetAt is constructed. We just removed it instead of allowing an index FunCall.

@SF-N SF-N changed the title Index builtin feat[next] Index builtin Oct 22, 2024
@SF-N SF-N changed the title feat[next] Index builtin feat[next]: Index builtin Oct 22, 2024
@SF-N SF-N requested a review from havogt October 22, 2024 14:58
Copy link
Contributor

@havogt havogt left a comment

Choose a reason for hiding this comment

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

a few comments

@@ -1156,6 +1156,7 @@ def ndarray(self) -> core_defs.NDArrayObject:
def asnumpy(self) -> np.ndarray:
raise NotImplementedError()

# TODO(tehrengruber): Use a regular zero dimensional field instead.
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand the comment.

Copy link
Contributor

Choose a reason for hiding this comment

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

We moved the comment to a better place. With respect to the comment itself: The IndexField class is rather strange. It has two modes of operation: Either it is a field with (conceptually) field(domain) == domain or it is a zero-dimensional field. Both modes don't share any implementation similarities, but are mushed into the same class. The way the class behaves is then controlled using _cur_index. It would be much simpler to just make field[index] return a zero-dimensional field which is exactly what we want instead of re-implementing it here.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand what you are saying...

@@ -31,9 +31,9 @@
INT_LITERAL: SIGNED_INT
FLOAT_LITERAL: SIGNED_FLOAT
OFFSET_LITERAL: ( INT_LITERAL | CNAME ) "ₒ"
_literal: INT_LITERAL | FLOAT_LITERAL | OFFSET_LITERAL
AXIS_LITERAL: CNAME ("ᵥ" | "ₕ")
_literal: INT_LITERAL | FLOAT_LITERAL | OFFSET_LITERAL | AXIS_LITERAL
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you mean index(AXIS_LITERAL)?

assert all(
isinstance(inp, SymRef) for inp in node.inputs
) # backend only supports SymRef inputs, not `index` calls
input_names = [str(inp.id) for inp in node.inputs] # type: ignore[union-attr] # backend only supports SymRef inputs, not `index` calls
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
input_names = [str(inp.id) for inp in node.inputs] # type: ignore[union-attr] # backend only supports SymRef inputs, not `index` calls
input_names = [str(inp.id) for inp in node.inputs] # type: ignore[union-attr] # `inp`s are only `SymRef`s

or similar

@SF-N SF-N requested a review from havogt October 23, 2024 15:51
@@ -260,6 +260,17 @@ def visit_Program(self, node: gtfn_ir.Program, **kwargs: Any) -> Union[str, Coll
#include <gridtools/fn/${grid_type_str}.hpp>
#include <gridtools/fn/sid_neighbor_table.hpp>
#include <gridtools/stencil/global_parameter.hpp>
#include <gridtools/stencil/positional.hpp>

// TODO(havogt): move to gtfn?
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd propose to wait until GridTools/gridtools#1806 is merged next week.

Copy link
Contributor

Choose a reason for hiding this comment

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

Once #1720 is in, we can remove this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I removed that code.

havogt added a commit that referenced this pull request Nov 4, 2024
updates minimum gridtools_cpp version for #1699
Comment on lines 263 to 273
#include <gridtools/stencil/positional.hpp>

// TODO(havogt): move to gtfn?
namespace gridtools{
namespace fn{
template <class T>
auto index(T){
return stencil::positional<std::decay_t<T>>();}
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
#include <gridtools/stencil/positional.hpp>
// TODO(havogt): move to gtfn?
namespace gridtools{
namespace fn{
template <class T>
auto index(T){
return stencil::positional<std::decay_t<T>>();}
}
}

@SF-N SF-N requested a review from havogt November 4, 2024 10:51
Copy link
Contributor

@havogt havogt left a comment

Choose a reason for hiding this comment

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

lgtm

@SF-N SF-N merged commit 9f3b0a7 into GridTools:main Nov 4, 2024
31 checks passed
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.

3 participants