Skip to content
Merged
Show file tree
Hide file tree
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
14 changes: 12 additions & 2 deletions ndsl/dsl/stencil.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ def __init__(
else:
self._timing_collector = timing_collector

self._arguments_already_checked = False

if externals is None:
externals = {}
self.externals = externals
Expand Down Expand Up @@ -406,7 +408,11 @@ def __call__(self, *args: Any, **kwargs: Any) -> None:
if self.stencil_config.verbose:
ndsl_log.debug(f"Running {self._func_name}")

self._validate_quantity_sizes(*args, **kwargs)
if (
not self._arguments_already_checked
and self.stencil_config.compilation_config.validate_args
):
self._validate_quantity_sizes(*args, **kwargs)

# Marshal arguments
args_list = list(args)
Expand All @@ -430,7 +436,10 @@ def __call__(self, *args: Any, **kwargs: Any) -> None:
ndsl_debugger.track_data(all_args, self._func_qualname, is_in=True)

# Execute stencil
if self.stencil_config.compilation_config.validate_args:
if (
not self._arguments_already_checked
and self.stencil_config.compilation_config.validate_args
):
Comment thread
romanc marked this conversation as resolved.
if __debug__ and "origin" in kwargs:
raise TypeError("origin cannot be passed to FrozenStencil call")
if __debug__ and "domain" in kwargs:
Expand All @@ -443,6 +452,7 @@ def __call__(self, *args: Any, **kwargs: Any) -> None:
validate_args=True,
exec_info=self._timing_collector.exec_info,
)
self._arguments_already_checked = True
else:
self.stencil_object.run(
**args_as_kwargs,
Expand Down
25 changes: 25 additions & 0 deletions tests/dsl/test_stencil.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,28 @@ def test_stencil_2D_temporaries() -> None:
)
stencil(quantity)
assert (quantity.data[1, 1, :] == 21.0).all()


@pytest.mark.parametrize(
"iterations",
[2, 1],
)
def test_validation_call_count(iterations: tuple[int]):
domain = (2, 2, 5)
quantity = Quantity(np.zeros(domain), ["x", "y", "z"], "n/a", extent=domain)
stencil_config = StencilConfig(
compilation_config=CompilationConfig(backend="numpy", rebuild=True)
)
stencil = FrozenStencil(
copy_stencil,
origin=(0, 0, 0),
domain=domain,
stencil_config=stencil_config,
)
# with expectation:
counting_mock = MagicMock()
with patch.object(FrozenStencil, "_validate_quantity_sizes", counting_mock):
for _i in range(iterations):
stencil(quantity, quantity)

assert counting_mock.call_count == 1