Skip to content

feat[next]: allow runtime shift offsets, and use them for the SWM periodic halo - #2750

Draft
havogt wants to merge 5 commits into
mainfrom
havogt/swm-periodicity
Draft

havogt wants to merge 5 commits into
mainfrom
havogt/swm-periodicity

Conversation

@havogt

@havogt havogt commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

Moves the halo update of the shallow water example (examples/swm/, added in #3038) into the DSL, replacing the halo refresh on the host that #3038 uses to stay free of src/ changes.

The example side

@gtx.field_operator
def make_periodic(f: IJField, m: gtx.int32, n: gtx.int32) -> IJField:
    f = concat_where(I == -1, f(I + m), f)
    f = concat_where(I == m,  f(I - m), f)
    f = concat_where(J == -1, f(J + n), f)
    f = concat_where(J == n,  f(J - n), f)
    return f

timestep now returns the wrapped prognostic fields, and the time loop touches no halo at all. The prognostic outputs are written over the full domain while the filtered levels only need the interior, so a program supplies a per-output domain=.

The rebinding order is load-bearing: each concat_where feeds the next, so the j pass sees the already-i-corrected field and the corners come out right. A simultaneous formulation would leave them wrong.

m and n are static parameters of the program (with_static_params("m", "n")): the jit backends of #3027 trace scalars, so they need the shift distance as a constant, and the compiled backends then see it as a literal. The opt-in performance comparison now times complete steps, halo included, instead of skipping the halo refresh.

Why this needs frontend changes

The shift distance is m, a program argument rather than a literal, and the frontend rejects that at FOAST construction:

DSLError: Cartesian offsets are only supported with a literal right-hand side,
          e.g. 'IDim + 1', but not 'IDim + expr'.

with_static_args cannot substitute — the check fires when the decorator runs at import, long before compile(m=[M]) exists.

Four files, and an ablation confirms none is redundant. Each row was measured (in August, on #2749's branch) by reverting that change alone and running all three backends:

change required for
ffront/foast_passes/type_deduction.py all backends — otherwise DSLError at import
ffront/foast_to_gtir.py gtfn + dace (embedded never lowers to GTIR)
iterator/transforms/normalize_shifts.py — CanonicalizeShiftOffsets gtfn + dace
pass_manager.py, wiring in apply_common_transforms gtfn only
pass_manager.py, wiring in apply_fieldview_transforms dace only

The last two lines are the non-obvious part: the two backends traverse different pass managers, so each wiring serves exactly one. Dropping either gives AttributeError: 'DomainAccessDescriptor' object has no attribute 'grid_type' on the corresponding backend.

CanonicalizeShiftOffsets exists because our own lowering emits a shift whose offset is a runtime expression; once m is inlined as a static argument it becomes an ir.Literal, but domain inference expects an ir.OffsetLiteral and otherwise yields the unknown-access sentinel.

Validation

After merging main (with #3027 and #3038):

  • pytest --nbmake examples/swm/swm.ipynb passes, including the full 4000 steps against swm_reference.npz (NCAR data)
  • 100 steps against swm_numpy, with the halo now filled in the field operator: gtfn_cpu and embedded_numpy are bit-for-bit equal, dace_cpu and jax_jit (CPU) differ by at most 2e-13 in u, v and 8e-11 in p, within the notebook's tolerances
  • tests/next_tests/unit_tests/ffront_tests/test_foast_to_gtir.py passes

Merging main conflicted in foast_to_gtir.py, where main now lowers literal Dim + idx shifts via connectivity_for_cartesian_shift for staggered dimensions. The runtime-offset case is kept after it, unchanged: an integral runtime offset stays within its dimension, as connectivity_for_cartesian_shift does for integral literals.

Unit suite (August, before merging main)

pytest tests/next_tests/unit_tests/ compared against the same suite on #2749's branch, since main already carries pre-existing failures:

failures + errors
baseline (#2749 branch) 19
this PR, before updating the spec test 20
this PR 19

The one difference was test_foast_to_gtir.py::test_premap_cartesian_non_literal_offset, which asserted that inp(TDim + i) raises. That is the behaviour this PR deliberately changes, so the test is updated rather than silenced — it now pins the lowering, plus two new cases: the SUB form (neg applied to the offset) and that a non-literal float offset still errors, since a runtime offset has no fractional part to resolve to a staggered dimension.

Failure sets are otherwise identical (comm on the sorted lists shows no other difference in either direction).

Note

An alternative that needs no src/ changes is to hardcode the wrap distance as a literal. I measured it and rejected it: it silently miscomputes at any --M other than the default, because the domain follows the grid size but the wrap does not.

havogt added 5 commits August 5, 2026 12:52
Port the NCAR/SWM shallow water benchmark to a single gt4py.next notebook,
alongside a NumPy reference and the 16x16 reference state after the full
4000-step run. Periodicity is applied outside GT4Py for now, which keeps the
example free of any src/ changes.
The scheme is verified against Sadourny (1975) Eq. 4 -- cu, cv, h, z are his
U, V, H and eta term for term. The constants are not from the paper, and the
'Swarztrauber 1984' attribution came from a code comment in NCAR/SWM rather
than from any reference, so point at the benchmark repo instead.
Cartesian shift offsets had to be literals, which meant a periodic wrap by a
runtime grid size could not be written in the DSL. Relax the frontend to accept
a non-literal integral offset, lower it, and canonicalize the resulting literal
to an OffsetLiteral so domain inference sees a real offset rather than the
unknown-access sentinel.

Uses it in the shallow water example, whose halo update moves from NumPy into
the field operator. Updates test_premap_cartesian_non_literal_offset, which
pinned the old rejection, and adds the SUB and non-integral cases.
# Conflicts:
#	src/gt4py/next/ffront/foast_to_gtir.py
Port of the periodicity change to the example as merged in #3038: a
make_periodic field operator with runtime shifts f(I + m), a program with
per-output domains, m and n as static parameters so that the jit backends
can shift by them, and no halo refresh on the host. The comparison now
times complete steps. The top-level copies of the example from #2749 are
removed.
@havogt
havogt removed this pull request from stack #2751 November 24, 2026 13:52
@havogt
havogt changed the base branch from havogt/swm-example-notebook to main November 24, 2026 13:52

This branch has not been deployed

No deployments
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.

1 participant