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

Nan check #1619

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
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
19 changes: 19 additions & 0 deletions src/gt4py/next/ffront/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from gt4py.next import (
allocators as next_allocators,
backend as next_backend,
common,
embedded as next_embedded,
errors,
)
Expand Down Expand Up @@ -53,6 +54,22 @@

DEFAULT_BACKEND: Callable = None

import numpy as np

def check_nan(*args, id: str, **kwargs):
for i, arg in enumerate(args):
if isinstance(arg, common.Field):
np_arg = arg.asnumpy()
is_finite, arg = np.isfinite(np_arg).any(), i
if not is_finite:
raise Exception(f"{id} contains NaN values, in field {i} with values {np_arg}.")
for arg_name, arg in kwargs.items():
if isinstance(arg, common.Field):
np_arg = arg.asnumpy()
is_finite, arg = np.isfinite(np_arg).any(), arg
if not is_finite:
raise Exception(f"{id} contains NaN values, in field {arg_name} with values {np_arg}")


# TODO(tehrengruber): Decide if and how programs can call other programs. As a
# result Program could become a GTCallable.
Expand Down Expand Up @@ -186,6 +203,7 @@ def itir(self) -> itir.FencilDefinition:
return past_to_itir.PastToItirFactory()(no_args_past).program

def __call__(self, *args, offset_provider: dict[str, Dimension], **kwargs: Any) -> None:
check_nan(*args, **kwargs, id=self.itir.id)
if self.backend is None:
warnings.warn(
UserWarning(
Expand All @@ -206,6 +224,7 @@ def __call__(self, *args, offset_provider: dict[str, Dimension], **kwargs: Any)
self.backend(
self.definition_stage, *args, **(kwargs | {"offset_provider": offset_provider})
)
check_nan(*args, **kwargs, id=self.itir.id)


try:
Expand Down
Loading