Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a19d90f
feat[next]: call-site scan builtin
havogt Sep 24, 2026
7e73fb9
Merge remote-tracking branch 'upstream/main' into scan-call
havogt Nov 30, 2026
0aa1637
Merge remote-tracking branch 'upstream/main' into scan-call
havogt Dec 1, 2026
c4ea9d7
refactor[next]: share scan argument checks and lowering between scan …
havogt Dec 1, 2026
1063ce3
refactor[next]: register the embedded scan builtin from the embedded …
havogt Dec 1, 2026
15844d6
refactor[next]: require axis for scan, lower it through the builtin d…
havogt Dec 1, 2026
df49ba4
refactor[next]: carry the scan axis on the GTIR `scan` builtin
havogt Dec 1, 2026
a7ddb95
fix[next]: embedded scans take their range per axis from the output d…
havogt Dec 2, 2026
edcf7c3
fix[next]: address review of the scan-axis change
havogt Dec 2, 2026
5c9c549
test[next]: cover a scan combined with a field on another vertical di…
havogt Dec 2, 2026
09046ac
test[next]: remove test_scan_in_stencil and its marker
havogt Dec 2, 2026
b06bece
fix[next]: include the scan axis in the type of an applied scan
havogt Dec 2, 2026
99a5ce1
Merge remote-tracking branch 'upstream/havogt/scan-axis' into scan-call
havogt Dec 2, 2026
98694ee
test[next]: mark the tuple-domain scan test as using scalars in the d…
havogt Dec 2, 2026
7f3c39e
refactor[next]: one embedded context variable for column ranges
havogt Dec 2, 2026
3f277a2
Merge remote-tracking branch 'upstream/havogt/scan-axis' into scan-call
havogt Dec 2, 2026
591b782
refactor[next]: hold the embedded column ranges as a tuple of named r…
havogt Dec 3, 2026
f0dfee8
Merge remote-tracking branch 'upstream/havogt/scan-axis' into scan-call
havogt Dec 3, 2026
c8215e5
ci: 12 xdist workers for the internal cuda13 CSCS jobs
havogt Dec 3, 2026
ddc4d50
Merge remote-tracking branch 'upstream/main' into gtir-scan-axis
havogt Dec 3, 2026
05d485b
Merge remote-tracking branch 'upstream/havogt/scan-axis' into scan-call
havogt Dec 3, 2026
026b737
test[next]: drop column_axis from tests merged in from main
havogt Dec 3, 2026
e38a220
Merge remote-tracking branch 'upstream/havogt/scan-axis' into scan-call
havogt Dec 3, 2026
54b23c5
Merge remote-tracking branch 'upstream/main' into scan-call
havogt Dec 3, 2026
59e1161
refactor[next]: address review on call-site scan
havogt Dec 3, 2026
3f8fa6c
fix[next]: address independent review of call-site scan
havogt Dec 3, 2026
440afca
test[next]: use a bare type constructor in the named-collection scan …
havogt Dec 3, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/gt4py/next/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
neg,
neighbor_sum,
power,
scan,
sin,
sinh,
sqrt,
Expand Down Expand Up @@ -190,6 +191,7 @@
"neg",
"neighbor_sum",
"power",
"scan",
"sin",
"sinh",
"sqrt",
Expand Down
6 changes: 4 additions & 2 deletions src/gt4py/next/embedded/nd_array_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
def _ops_of(*values: Any) -> array_ops.ArrayOps:
# TODO(havogt): the first field's array library decides, so mixing libraries depends on the
# order of the operands; reject mixed libraries or define a priority instead.
for v in values:
for v in utils.flatten_nested_tuple(values):
if isinstance(v, NdArrayField):
return array_ops.of(v._ndarray)
raise AssertionError("No 'NdArrayField' found in the arguments.")
Expand Down Expand Up @@ -804,7 +804,9 @@ def _register_builtin(builtin: fbuiltins.BuiltInFunction, impl: Callable[..., An
"""Register `impl` for `builtin`, unless the `ArrayOps` of the first field argument replaces it."""

def dispatch(*args: Any, **kwargs: Any) -> Any:
return _ops_of(*args).builtin_overrides.get(builtin, impl)(*args, **kwargs)
return _ops_of(*args, *kwargs.values()).builtin_overrides.get(builtin, impl)(
*args, **kwargs
)

dispatch.__name__ = getattr(impl, "__name__", builtin.name)
assert builtin not in _builtin_implementations
Expand Down
24 changes: 23 additions & 1 deletion src/gt4py/next/embedded/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
from gt4py._core import definitions as core_defs
from gt4py.eve import xtyping
from gt4py.next import common, errors, field_utils, named_collections, utils
from gt4py.next.embedded import array_ops, common as embedded_common, context as embedded_context
from gt4py.next.embedded import (
array_ops,
common as embedded_common,
context as embedded_context,
nd_array_field,
)
from gt4py.next.ffront import fbuiltins
from gt4py.next.otf import arguments
from gt4py.next.type_system import type_specifications as ts, type_translation

Expand Down Expand Up @@ -222,3 +228,19 @@ def impl(field: common.Field | array_ops.NDValue) -> array_ops.NDValue:
return res

return impl(field) # type: ignore[return-value]


def _scan(
Comment thread
havogt marked this conversation as resolved.
Resolved
scan_pass: Callable,
axis: common.Dimension,
forward: bool,
init: core_defs.Scalar | tuple,
/,
*args: Any,
**kwargs: Any,
) -> common.Field | tuple:
return ScanOperator(scan_pass, forward, init, axis)(*args, **kwargs)


# TODO(havogt): Registration is here to avoid a circular import. Consider refactoring the structure of embedded builtins registration in nd_array_field.
nd_array_field._register_builtin(fbuiltins.scan, _scan)
60 changes: 59 additions & 1 deletion src/gt4py/next/ffront/fbuiltins.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from numpy import float32, float64, int8, int16, int32, int64, uint8, uint16, uint32, uint64

from gt4py._core import definitions as core_defs
from gt4py.next import common, named_collections
from gt4py.next import common, named_collections, utils
from gt4py.next.common import Dimension, Field # noqa: F401 [unused-import] for TYPE_BUILTINS
from gt4py.next.embedded import array_ops, context as embedded_context
from gt4py.next.iterator import runtime
Expand Down Expand Up @@ -58,6 +58,7 @@
"where",
"broadcast",
"astype",
"scan",
"abs",
"neg",
"sin",
Expand Down Expand Up @@ -290,6 +291,62 @@ def broadcast(
return field # type: ignore[return-value] # see comment above


class ScanBuiltinFunction(BuiltInFunction[_R, _P]):
def __gt_type__(self) -> ts.FunctionType:
# The arguments are checked against the signature of `scan` in type deduction.
return ts.FunctionType(
pos_only_args=[],
pos_or_kw_args={},
kw_only_args={},
returns=ts.DeferredType(constraint=None),
)

def __call__( # type: ignore[override] # the implementation is dispatched on the arguments of the scan operator
self, scan_pass: Callable, /, **scan_kwargs: Any
) -> Callable[..., common.Field | Tuple]:
bound = inspect.signature(self.function).bind(scan_pass, **scan_kwargs)
bound.apply_defaults()
Comment thread
havogt marked this conversation as resolved.
Resolved

def scan_operator(*args: Any, **kwargs: Any) -> common.Field | Tuple:
impl = self.dispatch(*utils.flatten_nested_tuple((*args, *kwargs.values())))
if impl is self.function:
raise NotImplementedError(f"No implementation of 'scan' for arguments {args}.")
return cast(common.Field | Tuple, impl(*bound.arguments.values(), *args, **kwargs))

return scan_operator


@ScanBuiltinFunction
def scan(
scan_pass: Callable,
/,
*,
axis: common.Dimension,
forward: bool = True,
init: core_defs.Scalar | Tuple = 0.0,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@tehrengruber Removing the default is in a next PR

) -> Callable[..., common.Field | Tuple]:
"""
Create a scan operator from a scan pass, to be called inside a field operator.

Args:
scan_pass: Field operator `(carry, *args) -> carry` on scalars (or tuples thereof).
axis: Vertical dimension to scan along.
forward: Scan direction.
init: Initial value of the carry, a compile-time constant.

Examples:
>>> scan(my_pass, axis=KDim, forward=True, init=0.0)(a) # doctest: +SKIP
"""
raise NotImplementedError()


SCAN_DEFAULTS: Final[dict[str, Any]] = {
name: param.default
for name, param in inspect.signature(scan.function).parameters.items()
if param.default is not inspect.Parameter.empty
}


@WhereBuiltinFunction
def where(
mask: common.Field,
Expand Down Expand Up @@ -430,6 +487,7 @@ def impl(
"broadcast",
"where",
"astype",
"scan",
*MATH_BUILTIN_NAMES,
]

Expand Down
Loading