Skip to content

feat[next]: NeighborConnectivity[Domain, Codomain] declarations, NeighborTableType, and shared local dimensions on every backend - #2907

Draft
egparedes wants to merge 24 commits into
connectivities-as-types-2-dimension-classesfrom
connectivities-as-types-3-neighbor-connectivity
Draft

egparedes wants to merge 24 commits into
connectivities-as-types-2-dimension-classesfrom
connectivities-as-types-3-neighbor-connectivity

Conversation

@egparedes

@egparedes egparedes commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #2899 (native stack #3089). Design: ADR 0031 in this PR.

This is a new implementation of the PR on top of the current #2899, where dimension classes compare, hash and pickle by their DimensionRef. The merge commit takes #2899's tree as is, and the commits after it port this PR's features again. The earlier implementation is still in the history before that merge.

What

A neighbor connectivity can be declared as a class, with its local dimension nested in it:

class V2E(gtx.NeighborConnectivity[Vertex, Edge], max_neighbors=6, min_neighbors=5):
    class Local(gtx.LocalDimensionIndex): ...


@gtx.field_operator
def f(a: Field[Dims[Edge], float], s: Field[Dims[Vertex, V2E.Local], float]) -> Field[Dims[Vertex], float]:
    return neighbor_sum(s * a(V2E), axis=V2E.Local) + a(V2E[0])

Additive for user code: FieldOffset and string-keyed offset providers keep working; #2910 removes them. The following public names are renamed without aliases: NeighborConnectivityType → NeighborTableType, OffsetProviderType → TableTypes, is_offset_provider_type → is_table_types, ts.OffsetType(source, target) → ts.ShiftType(codomain, domain, tag).

  • The data is on the connectivity. NeighborConnectivity (metaclass ConnectivityMeta) carries the domain, codomain, local dimension and the optional max_neighbors / min_neighbors. Below the frontend these travel as an immutable ConnectivityRef. Declarations compare, hash and are fingerprinted by that ref, like dimensions (ADR 0030). A local dimension stays a plain dimension class: a class rebuilt from its ref would lose anything stored on it.
  • LocalDimensionIndex is a dimension of kind LOCAL. DimensionKind.LOCAL stays, and a dimension is local if its kind is LOCAL, also for refs and synthesized classes.
  • Owner and sharers. A declaration owns a Local nested in its own body. It is named in the IR by the local dimension's tag (offset_tag). A declaration that shares another one's local dimension (Local: typing.TypeAlias = C2E.Local, as ICON4Py's C2CE needs) is named by its own tag.
  • Checks. check_neighbor_table checks a table against a declaration (domain, codomain, integral dtype, counts, skip values). A program checks its tables against the declarations it references, also through a module (mesh.V2E). Tables over one local dimension must have the same domain, count and skip-value flag, on every backend, embedded included.
  • NeighborTableType holds the ConnectivityRef of the declaration a table is bound to, or the table's structural ConnectivityType when it has none (hand-written IR, FieldOffset).
  • Backends. connectivity_key_over(provider, local_dim) finds a table over a local dimension (the owner's, else a sharer's), so shared local dimensions work on embedded, roundtrip, gtfn, iterator embedded and DaCe. DaCe sizes a connectivity array from its own table (local_dimension_size). The uses_offset_tag_differing_from_local_dim* markers are removed.
  • Typing. Local is annotated nowhere, so Field[Dims[V, V2E.Local], float] type-checks under both mypy and pyright. pyright now runs in the typing session over typing_tests/pyright_probes.py.

Not in this PR

Verification

Local, CPU, Python 3.12. Before the merge of #2899's docs-only 0daf07ea2:

  • unit_tests: 3136 passed. Feature, iterator and regression tests plus the toy-connectivity tests: 3471 passed, 0 failed.
  • Integration and regression tests on the final head: 3886 passed, 0 failed (integration_tests and regression_tests, not requires_gpu, 4 workers); common doctests 14 passed.
  • The new shared-local-dimension feature tests fail without the backend changes (16 failures) and pass with them.
  • pyright probes: 0 errors. mypy typing cases: 21 passed. tach check and pre-commit on the changed files: clean.
  • Not run locally: jax, torch and GPU backends.

🤖 Written by an agent on my behalf.

@egparedes
egparedes added this pull request to stack #2900 September 22, 2026 06:59
@egparedes
egparedes force-pushed the connectivities-as-types-3-neighbor-connectivity branch from d1e1aeb to 8c55110 Compare September 23, 2026 16:09
@egparedes
egparedes force-pushed the connectivities-as-types-3-neighbor-connectivity branch 2 times, most recently from bf5c950 to 90252b3 Compare September 24, 2026 10:12
@egparedes
egparedes removed this pull request from stack #2900 September 24, 2026 10:14
@egparedes
egparedes added this pull request to stack #2917 September 24, 2026 10:14
@egparedes egparedes changed the title feat[next]: NeighborConnectivity declarations and local dimensions that know their owner feat[next]: NeighborConnectivity[Domain, Codomain] declarations, NeighborTableType, and shared local dimensions on every backend Sep 24, 2026
Comment thread src/gt4py/next/common.py Outdated
/,
*,
max_neighbors: Optional[int] = None,
min_neighbors: Optional[int] = None,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why not has_skip_values. Feels like the better concept, because there might not be a natural minimum (e.g. if you use it to express missing values close to the boundary).

- check_neighbor_table: min_neighbors exceeding the table, bool tables
- descriptive errors for non-integer indices, the undeclared base, DSL attributes
- FieldOffset.Local, so the spelling works for legacy offsets in embedded too
- fingerprint a declaration by its dimensions and counts, not only its name
- negative counts are a ValueError
…mension

Flattened sparse patterns (ICON4Py's C2CE, E2ECV, ...) index the same neighbor
axis as another connectivity. A declaration can now adopt an owned local
dimension; it is then named in the IR by its own tag (offset_tag), since the
local dimension's tag already names the owner's table.
An annotated 'Local' -- on 'NeighborConnectivity' or on its metaclass -- makes
every declaration's local dimension a *variable*: pyright then rejects
'Field[Dims[V, V2E.Local], float]', and mypy rejects an adopted or shared one.
Annotate it nowhere; library code reads it through 'common.local_dimension_of',
and adoption and sharing are written 'Local: TypeAlias = ...'.

Also from the review: a redefined declaration re-owns an adopted local dimension
instead of becoming a sharer, and its counts are checked against the local
dimension's own 'size='. The missing-'Local' error names both spellings.

Adds pyright over 'typing_tests/pyright_probes.py' to the typing session, which
is what catches a regression here: the mypy cases cannot.
Now that local dimensions can state their size, the local dimension of
'make_const_list' results is one; a declaration cannot adopt it, since it
belongs to no connectivity.
Reductions, sparse arguments and list materialization used to find a
connectivity by the local dimension's tag, which only names the owner's table.
They now look up a table over the local dimension (common.connectivity_key_over),
so a connectivity sharing another one's local dimension works on every backend,
bound on its own or together with its owner. DaCe sizes a connectivity array's
local dimension from its own table. Lifts the PR 1 xfail markers.
- DaCe if/scan/concat_where/const-list sites find the table over the local dimension
- connectivity_key_over takes a tag, avoids resolve, and picks deterministically
- embedded map_list compares lists by local dimension, not by offset
- a sharer must have its owner's origin; counts are compared one by one
- ADR 0029: sharers must have the owner's neighbor structure
…Type and ts.ShiftType

- 'NeighborConnectivity[Origin, Codomain]' becomes '[Domain, Codomain]', and
  the class attribute 'origin' becomes 'domain': a bound table is a field over
  (Domain, Local) with values in Codomain. 'origin' already names a buffer
  origin ('__gt_origin__').
- 'NeighborConnectivityType' becomes 'NeighborTableType': the declaration the
  table is bound to, dtype, skip value and max_neighbors, with domain and
  codomain derived from the declaration. It is built from the offset-provider
  key ('offset_provider_to_type', 'check_neighbor_table', which now returns
  it), since a sharer's table looks like its owner's; local dimensions record
  their sharers for that. A table under a key no declaration answers to keeps
  its structural 'ConnectivityType', which is also what
  'NeighborTable.__gt_type__()' returns now. 'OffsetProviderType' becomes
  'TableTypes'.
- 'ts.OffsetType(source, target)' becomes 'ts.ShiftType(codomain, domain)',
  printed 'Shift[<tag>: <codomain> -> <domain>]'.
- ADR 0029 describes the result; ADR 0019 names the new record.
ADR 0028 on main is now 'Plain Builders Instead of factory-boy Factories' (#2808),
so the two ADRs of this stack move up by one.
A dimension is local if and only if it subclasses `LocalDimensionIndex`
(`common.is_local_dimension`), so `DimensionKind` loses its `LOCAL` member and
is `HORIZONTAL | VERTICAL`. A local dimension's `kind` is `None`, and declaring
one with `kind=` is a `TypeError`; `None` rather than `HORIZONTAL` keeps every
backend comparison against `HORIZONTAL` / `VERTICAL` meaning what it meant.
`order_dimensions` sorts by an explicit rank (horizontal, local, vertical), so
sparse-field layouts do not move. Displays derive the label from the class:
`Local[local]`, the `ₗ` IR suffix and DaCe's `_gtx_localdim` map variables are
unchanged.

The tree's `DimensionIndex(kind=LOCAL)` declarations become owner-less
`LocalDimensionIndex` subclasses. pyright probes pin the Cartesian axis levels
of ADR 0029, including staggering and shifting a local dimension, with
`reportUnnecessaryTypeIgnoreComment` so a rejection that stops firing fails
the session. ADR 0030 records the change.
…ctivities-as-types-3-neighbor-connectivity

Takes #2899's tree as is: #2899 now compares, hashes and pickles dimension
classes by their ref and keeps DimensionKind.LOCAL, which the old
implementation of this branch contradicts. Its features are ported again on
top of this merge, in the following commits.
NeighborConnectivityType becomes NeighborTableType, OffsetProviderType becomes
TableTypes (is_offset_provider_type becomes is_table_types, OffsetProviderTypeElem
is dropped), and ts.OffsetType(source, target) becomes ts.ShiftType(codomain,
domain), printed 'Shift[<tag>: <codomain> -> <domain>]'. The source_dim and
neighbor_dim properties give way to domain[0] and domain[1].
…lasses

Add `NeighborConnectivity[Domain, Codomain]` declarations with a nested
`Local` dimension, `LocalDimensionIndex`, the immutable `ConnectivityRef`
that stands for a declaration below the frontend, and
`check_neighbor_table` to bind a table to its declaration.

A declaration owns its local dimension when `Local` is nested in it and is
then named in the IR by the local dimension's tag; one that shares or adopts
a local dimension is named by its own tag. `offset_provider_to_type` binds
tables to the declarations the program references; elsewhere a table keeps
its structural type. Declarations compare, hash, pickle and fingerprint by
their ref.
Reductions, sparse arguments and list materialization used to find a table
by the local dimension's tag, which only names the table of the connectivity
declaring it. They now take a table over the local dimension
(`common.connectivity_key_over`), so a connectivity sharing another one's
local dimension works on every backend, bound on its own or together with
the other one. DaCe sizes a field's local dimension from such a table.

Lifts the `uses_offset_tag_differing_from_local_dim*` markers, which every
backend now passes.
The two checkers disagree about what counts as a type: an annotated `Local`
would make a declaration's local dimension a variable for pyright. Probes in
`typing_tests/pyright_probes.py` run under pyright in `test_typing_exports`,
and the mypy cases in `test_next.yaml` cover declarations too.
Records the `NeighborConnectivity` declarations: data on the connectivity and
its ref, owner and sharers by nesting, tables bound to the declarations a
program references, and structural table types below the frontend.
…or structure

Reductions and sparse fields take the neighbor count and skip values from
any table over their local dimension, so a sharer's table with another
count or skip flag than its owner's silently gave wrong results.
`check_shared_local_dimensions` rejects that structurally, without
declarations: `offset_provider_to_type` checks every local dimension, and
`connectivity_key_over` the one it looks up, which is how embedded
execution notices. UnrollReduce takes the table of a `neighbors` argument
from its own offset.
`a(mesh.V2E)` names a declaration that is not a closure variable, so its
table was never checked. The declarations a program references now also
include those its field operators reach through an attribute of a module
or another namespace.
ADR 0031 said that a declaration pickles by its ref, but it has no reducer:
it is pickled by reference, like any class, so a declaration that has since
been redefined cannot be pickled. The toolchain carries refs, which pickle
by value. Say so, and test it.
- A `LocalDimensionIndex` subclass cannot set another kind in its body.
- `V2E[i]` outside embedded execution raises a clear error instead of an
  assertion, as does `FieldOffset[i]`.
- A structural `NeighborTableType` must have a two-dimensional domain, and
  its dtype and skip value must agree with its structure.
- `test_local_dimension_size` uses different counts, so that it tells its
  branches apart.

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.

2 participants