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]: Introduce FunctionField #1328

Open
wants to merge 67 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 57 commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
3e05028
Introduce ConstantField
samkellerhals Aug 23, 2023
1c35fea
Allow non constant field operand
samkellerhals Aug 23, 2023
5d742d7
Enable slicing with domain and sequence of NamedRange
samkellerhals Aug 24, 2023
4d6f924
Make ndarray from domain
samkellerhals Aug 24, 2023
2505919
Refactor constant field
samkellerhals Aug 25, 2023
5134af2
BinaryOp ConstantField (no domain) with Field
samkellerhals Aug 25, 2023
255c5d5
Add binop on constant field with domain intersection
samkellerhals Aug 25, 2023
8f741dc
refactor[next] Prepare new Field for embedded
havogt Aug 28, 2023
dea2f26
Add indexing to constant field
samkellerhals Aug 28, 2023
4176889
Run qa
samkellerhals Aug 28, 2023
3d9ae6c
remove changes belonging to itir.embedded pr
havogt Aug 28, 2023
6f72417
cleanup setitem
havogt Aug 28, 2023
907d9bc
extract domain slicing
havogt Aug 28, 2023
dd8e0a6
add tests
havogt Aug 28, 2023
29ee689
domain_like and domain construction
havogt Aug 28, 2023
d951322
Fix conflicts
samkellerhals Aug 29, 2023
ae37b54
Update imports
samkellerhals Aug 29, 2023
f80349a
Cleanup
samkellerhals Aug 29, 2023
0c858f3
add out-of-bounds check
havogt Aug 29, 2023
9851de2
fix tests
havogt Aug 29, 2023
03880a0
test alike constructors
havogt Aug 29, 2023
02ca534
cleanup
havogt Aug 29, 2023
9df8329
remove duplicated lines
havogt Aug 30, 2023
1d53155
more compact str representation of Domain
havogt Aug 30, 2023
e85abe4
fix error
havogt Aug 30, 2023
901ac15
remove reps
havogt Aug 30, 2023
643ffaa
fix test
havogt Aug 30, 2023
496475f
Cleanup constant field
samkellerhals Aug 30, 2023
8dfa7b6
Add FunctionField prototype
samkellerhals Aug 30, 2023
d0f5717
Use np.fromfunction
samkellerhals Aug 31, 2023
2c933f9
Refactor FunctionField
samkellerhals Aug 31, 2023
e16c4ae
refactor typealiases
havogt Aug 31, 2023
fc7ae08
Cleanup FunctionField
samkellerhals Aug 31, 2023
176a278
Fix typing
samkellerhals Aug 31, 2023
0089737
remaining review comments
havogt Aug 31, 2023
9ca0b53
Merge remote-tracking branch 'upstream/main' into field_refactoring_p…
havogt Aug 31, 2023
4085970
Update from main
samkellerhals Sep 4, 2023
08b2ef5
Update from main
samkellerhals Sep 4, 2023
d266694
Address review comment
samkellerhals Sep 4, 2023
adfdeef
small cleanup
samkellerhals Sep 5, 2023
b8553a4
Update from main
samkellerhals Sep 5, 2023
df3df37
fix typing
samkellerhals Sep 5, 2023
ca898ac
Fix broadcasting
samkellerhals Sep 5, 2023
9564e29
Cleanup
samkellerhals Sep 5, 2023
3c42dd5
add smore subomain checks
havogt Sep 5, 2023
b29c355
Add scalar op to FunctionField
samkellerhals Sep 6, 2023
a6c4e14
Add invariant
samkellerhals Sep 6, 2023
7cefa7e
test is_finite
samkellerhals Sep 6, 2023
c5a026d
Use custom exceptions
samkellerhals Sep 6, 2023
37abfeb
Add docstring
samkellerhals Sep 6, 2023
08b40d0
Implement remaining methods
samkellerhals Sep 6, 2023
fc8ec3b
test ellipsis function field getitem
samkellerhals Sep 6, 2023
1619203
Fix flake8 and some typing
samkellerhals Sep 6, 2023
99ef611
Add unary math builtins
samkellerhals Sep 8, 2023
5216f40
Add full fbuiltin support
samkellerhals Sep 8, 2023
92da0be
Fix doubly defined methods
samkellerhals Sep 8, 2023
4ab5ab5
Test invariant with np.fromfunction
samkellerhals Sep 12, 2023
1ca17f4
Customisable ndarray
samkellerhals Sep 12, 2023
f243469
Add improvements
samkellerhals Sep 13, 2023
f248c69
Implement invert correctly
samkellerhals Sep 13, 2023
2a18ccc
Introduce builtin test for all fields
samkellerhals Sep 13, 2023
d7d841e
Fix broadcast
samkellerhals Sep 13, 2023
9512cc7
fix formatting
havogt Nov 2, 2023
7e58ee1
some typing fixes
havogt Nov 2, 2023
8280d1e
fix test import
havogt Nov 2, 2023
23fc901
Merge remote-tracking branch 'upstream/main' into add-constant-field
havogt Nov 2, 2023
21a0e26
Merge remote-tracking branch 'upstream/main' into add-constant-field
havogt Dec 16, 2023
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
6 changes: 6 additions & 0 deletions src/gt4py/next/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,12 @@ def __and__(self, other: Domain) -> Domain:
def __str__(self) -> str:
return f"Domain({', '.join(f'{e[0]}={e[1]}' for e in self)})"

def is_finite(self) -> bool:
samkellerhals marked this conversation as resolved.
Show resolved Hide resolved
for _, rng in self:
if Infinity.positive() in (abs(rng.start), abs(rng.stop)):
return False
return True


DomainLike: TypeAlias = (
Sequence[tuple[Dimension, RangeLike]] | Mapping[Dimension, RangeLike]
Expand Down
21 changes: 20 additions & 1 deletion src/gt4py/next/embedded/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# distribution for a copy of the license or check <https://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: GPL-3.0-or-later
from __future__ import annotations

from typing import Any, Optional, Sequence, cast

Expand All @@ -37,7 +38,7 @@ def _relative_sub_domain(

expanded = _expand_ellipsis(index, len(domain))
if len(domain) < len(expanded):
raise IndexError(f"Trying to index a `Field` with {len(domain)} dimensions with {index}.")
raise embedded_exceptions.IndexOutOfBounds(domain=domain, indices=index)
expanded += (slice(None),) * (len(domain) - len(expanded))
for (dim, rng), idx in zip(domain, expanded, strict=True):
if isinstance(idx, slice):
Expand All @@ -64,6 +65,10 @@ def _absolute_sub_domain(
domain: common.Domain, index: common.AbsoluteIndexSequence
) -> common.Domain:
named_ranges: list[common.NamedRange] = []

if len(domain) < len(index):
raise embedded_exceptions.IndexOutOfBounds(domain=domain, indices=index)

for i, (dim, rng) in enumerate(domain):
if (pos := _find_index_of_dim(dim, index)) is not None:
named_idx = index[pos]
Expand Down Expand Up @@ -125,3 +130,17 @@ def _find_index_of_dim(
if dim == d:
return i
return None


def _broadcast_domain(
samkellerhals marked this conversation as resolved.
Show resolved Hide resolved
field: common.Field, new_dimensions: tuple[common.Dimension, ...]
) -> Sequence[common.NamedRange]:
named_ranges = []
for dim in new_dimensions:
if (pos := _find_index_of_dim(dim, field.domain)) is not None:
named_ranges.append((dim, field.domain[pos][1]))
else:
named_ranges.append(
(dim, common.UnitRange(common.Infinity.negative(), common.Infinity.positive()))
)
return named_ranges
42 changes: 35 additions & 7 deletions src/gt4py/next/embedded/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,55 @@
#
# SPDX-License-Identifier: GPL-3.0-or-later

from typing import Optional

from gt4py.next import common
from gt4py.next.errors import exceptions as gt4py_exceptions


class IndexOutOfBounds(gt4py_exceptions.GT4PyError):
domain: common.Domain
indices: common.AnyIndexSpec
index: common.AnyIndexElement
dim: common.Dimension
index: Optional[common.AnyIndexElement]
dim: Optional[common.Dimension]

def __init__(
self,
domain: common.Domain,
indices: common.AnyIndexSpec,
index: common.AnyIndexElement,
dim: common.Dimension,
index: Optional[common.AnyIndexElement] = None,
dim: Optional[common.Dimension] = None,
):
super().__init__(
f"Out of bounds: slicing {domain} with index `{indices}`, `{index}` is out of bounds in dimension `{dim}`."
)
msg = f"Out of bounds: slicing {domain} with index `{indices}`."
if index is not None and dim is not None:
msg += f" `{index}` is out of bounds in dimension `{dim}`."

super().__init__(msg)
self.domain = domain
self.indices = indices
self.index = index
self.dim = dim


class EmptyDomainIndexError(gt4py_exceptions.GT4PyError):
index: common.AnyIndexSpec

def __init__(self, cls_name: str):
super().__init__(f"Error in `{cls_name}`: Cannot index `{cls_name}` with an empty domain.")
self.cls_name = cls_name


class FunctionFieldError(gt4py_exceptions.GT4PyError):
def __init__(self, cls_name: str, msg: str):
super().__init__(f"Error in `{cls_name}`: {msg}.")
self.cls_name = cls_name
self.msg = msg


class InfiniteRangeNdarrayError(gt4py_exceptions.GT4PyError):
def __init__(self, cls_name: str, domain: common.Domain):
samkellerhals marked this conversation as resolved.
Show resolved Hide resolved
super().__init__(
f"Error in `{cls_name}`: Cannot construct an ndarray with an infinite range in domain: `{domain}`."
)
self.cls_name = cls_name
self.domain = domain
Loading