Repository navigation
feat[next]: a concrete Dimension is a class, identified by its qualified name #2899
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
Changes from all commits
565fa75
823c0dd
7ea399a
183488f
05f14f1
03cb2f2
5e9e542
2c68d48
263af8f
3109d54
ce59507
0e55c87
996a34a
83c362f
3371a94
dc53f1a
c8a69f0
47b276b
ea75ee7
a1cf830
52ca205
a6ac82b
3a2a0ea
26b33dc
72a0d46
b80b629
3f2c073
34776de
a525844
0792135
4d1820b
b7b5e2f
23ba65f
ace9d20
7138853
c640e91
b7f0a88
66c1147
8bc744d
1255a62
be741dc
682836d
5580a66
d7afac6
dc8285b
52f5fc8
2fef0b2
43509bc
fb9dfce
3103b7f
6a99b90
fdce96c
07eff40
2e1fc88
6f418f9
85cbd19
ef11aae
1631fa2
fa8703c
d0195e7
3692e04
c46eef1
ad2b9ac
72eb4b2
3340ada
ebc070c
3cbc08f
138b656
6f559da
894cb02
86628d5
464049d
2fd7dc5
8a7fabb
c428b2d
700c0da
a1c7c63
f62e65b
e7cdbea
992e7c7
d93891d
d1a68b6
67a115b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,207 @@ | ||
| --- | ||
| tags: [] | ||
| --- | ||
|
|
||
| # Dimensions as Nominal Types | ||
|
|
||
| - **Status**: accepted | ||
| - **Authors**: Enrique González Paredes (@egparedes) | ||
| - **Created**: 2026-09-18 | ||
| - **Updated**: 2026-12-03 | ||
|
|
||
| A concrete dimension becomes a **class**, and an index along it an **instance** of | ||
| that class — the shape `enum.Enum` uses, where the class is the collection and | ||
| the instances are its members: | ||
|
|
||
| ```python | ||
| class IDim(gtx.CartesianAxisIndex): ... | ||
|
|
||
|
|
||
| class KDim(gtx.CartesianAxisIndex, kind=gtx.DimensionKind.VERTICAL): ... | ||
|
|
||
|
|
||
| class Cell(gtx.DimensionIndex): ... # a mesh location: not a Cartesian axis | ||
|
|
||
|
|
||
| IDim # the dimension -- annotated `gtx.Dimension` | ||
| IDim(0) # an index into it -- annotated `IDim` | ||
| ``` | ||
|
|
||
| so `gtx.Field[gtx.Dims[IDim], gtx.float64]` is valid for any PEP 484 checker with | ||
| no GT4Py mypy plugin. | ||
|
|
||
| A dimension's **identity is the Python type** in user code, embedded execution and | ||
| program calls, and its `tag` — the string that crosses into the | ||
| IR and the generated code — is its **qualified Python name**, `f"{cls.__module__}:{cls.__qualname__}"` (a staggered dimension embeds its | ||
| base's full tag). Below the frontend a dimension is a `DimensionRef` value naming that | ||
| class (see *Two levels* below). Dimensions on a Cartesian axis, with index arithmetic | ||
| and a staggered partner, subclass `CartesianAxisIndex`; mesh locations subclass | ||
| `DimensionIndex` directly. | ||
|
|
||
| ## Context | ||
|
|
||
| `Dimension` was a frozen dataclass whose instances are values, not types. Two | ||
| consequences drove this change. | ||
|
|
||
| **Dimensions were not usable as types.** `Field[[IDim], float64]` needed a | ||
| dedicated mypy plugin that substituted at most four distinct placeholders per run, | ||
| made `TypeVar`s over dimensions impossible, and served only mypy — no other checker. | ||
| See #2503. | ||
|
|
||
| **Names are load-bearing in too many places.** A neighbor connectivity is | ||
| currently spread over four independently authored strings that must agree by | ||
| string equality and are never checked against each other at declaration time: the | ||
| `FieldOffset` tag, the Python variable it is bound to, the local `Dimension`'s | ||
| name, and the `offset_provider` key. Whichever one reaches | ||
| `common.get_offset` depends on the execution path and the operation. Making a | ||
| dimension's identity its Python type is the prerequisite for collapsing those | ||
| names into one declaration (a follow-up ADR covers the connectivity half). | ||
|
|
||
| ## Decision | ||
|
|
||
| ### Identity is the Python type; the tag is the qualified name | ||
|
|
||
| Two dimension classes are the same dimension if and only if they are the same | ||
| class. The static view (checkers see nominal types) and the runtime view | ||
| (equality is `is`) agree by construction, and the tag, unique among importable | ||
| classes, is also a valid IR spelling. | ||
|
|
||
| The alternative — `(name, kind)` value equality plus an interning registry, so | ||
| that independently declared same-named dimensions stay interchangeable — was | ||
| considered and rejected. It decouples the Python type's identity from the IR's, | ||
| and needs a name-keyed registry. It also cannot give a dimension nested inside another | ||
| declaration a unique name without further convention, which the connectivity | ||
| work requires. | ||
|
|
||
| One concrete example: take two distinct classes, both named `I`. Under `(name, kind)` | ||
| equality they compare equal, so `typing` caches `Field[Dims[I]]` once and returns that | ||
| same object for the other class. A type checker sees two types where the runtime sees | ||
| one. Under type identity they are two types in both views. | ||
|
|
||
| ### Two levels: classes in user code, `DimensionRef` values in the toolchain | ||
|
|
||
| Classes stay the identity in user code, in embedded execution and wherever a call | ||
| enters GT4Py. Type specifications, the IR and the backends instead carry a | ||
| `DimensionRef`: an immutable, interned value with the class's module, qualname, | ||
| `kind`, whether it is a Cartesian axis and, for a staggered dimension, its base. The | ||
| conversion happens once per class, where classes enter the toolchain (type translation | ||
| of annotations and arguments, offset providers, scan axes). | ||
|
|
||
| Below the frontend, two refs are equal if all their fields are: the same qualified | ||
| name, `kind`, Cartesian axis flag and staggered base is the same dimension. The IR is therefore | ||
| self-contained and a compile task ships no user classes, except, for now, the | ||
| connectivities of the offset provider, which still hold dimension classes. | ||
|
|
||
| Because a re-run notebook cell gives an *equal* ref for a *new* class, program calls | ||
| still compare by class, in the same `__debug__`-only argument check as the type | ||
| check: when a program, or a field operator called as one, receives a field, | ||
| named-collection element or connectivity built on a different class of the same | ||
| name as a dimension its annotations, scan axis or `FieldOffset`s declare, the call | ||
| is rejected, embedded or compiled. | ||
|
|
||
| ### Consequences of the tag being a qualified name | ||
|
|
||
| 1. **Nothing is imported from a tag.** The roundtrip backend, which runs the generated | ||
| program embedded, on classes, takes them from the fields and connectivities of each | ||
| call. It synthesizes a class from the ref only for a dimension that none of them | ||
| has: such a dimension exists only inside the program. | ||
|
|
||
| 2. **Dimensions must be importable to be pickled**, i.e. declared at module level. | ||
| `__init_subclass__` rejects a `<locals>` qualname as an early heuristic; it is | ||
| neither necessary nor sufficient (`type("Dyn", ...)` inside a function passes, | ||
| a class deleted after creation passes), so the authoritative check remains | ||
| pickle's own `save_global`. | ||
|
|
||
| A spawn worker cannot re-import an **interactive `__main__`** (a notebook, the | ||
| REPL, `python -c`), so a compile job that holds a class declared there is compiled | ||
| in the calling thread, with a warning. Scripts keep parallel compilation. | ||
|
|
||
| 3. **No registry.** Module-level classes pickle by reference. | ||
|
|
||
| 4. **Cache fingerprints depend on module paths.** A dimension is fingerprinted by | ||
| qualified name, `kind` and Cartesian axis flag (a staggered one through its base), so | ||
| moving a declaration to another module invalidates compiled artifacts. Re-running a | ||
| notebook cell that declares the same dimension reuses them, unless one of these changed. | ||
|
|
||
| 5. **Generated identifiers need injective mangling.** A tag is not a valid C++, | ||
| DaCe or `eve` identifier, so backends escape it, without mapping two tags to | ||
| one identifier. | ||
|
|
||
| 6. **Staggered dimensions become a real parametrized type.** ADR 0026's | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How much of point 6 is implementation detail not needed at ADR level?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cut to the decision in e7cdbea: 🤖 Written by an agent on my behalf. |
||
| `_Staggered` *name prefix* cannot survive type identity: `Dimension(f"_Staggered{name}")` | ||
| names no importable type, and the prefix cannot recover the base dimension's | ||
| module. `Staggered[D]` replaces it and supersedes that part of ADR 0026. | ||
|
|
||
| `Staggered[KDim]` is a **real class**, built and interned on first use (a PEP 695 | ||
| generic would give a `typing` alias, not a class). It is a *different* dimension, so | ||
| it does not subclass `KDim` and only inherits its `kind`. It keeps its base, and its | ||
| tag is `<owner tag>[<base tag>]`, so its ref is rebuilt from the tag without importing | ||
| anything. The intern table is keyed by the base class, like `typing`'s subscription | ||
| cache, not by a user-authored name. | ||
|
|
||
| ### Cartesian axes are a level of the hierarchy | ||
|
|
||
| A **Cartesian axis** is an oriented integer line, i.e. a two-way infinite path graph: a | ||
| 0-cell at each integer and a 1-cell between consecutive ones. Integer shifts (`D + n`) | ||
| preserve these two *cell classes* and half-integer shifts swap them (ADR 0026); a | ||
| `CartesianAxisIndex` names one class and `Staggered[...]` the other. A `Domain` selects a | ||
| range of it; periodic grids are not modelled. Mesh locations have no such translation: | ||
| their neighbours come from connectivity tables. Loosely, a declared dimension is itself | ||
| called "a Cartesian axis". Two levels below the root encode this: | ||
|
|
||
| ``` | ||
| DimensionIndex # the root; every `type[DimensionIndex]` keeps its meaning | ||
| ├── AnyCartesianAxisIndex # either cell class of a Cartesian axis | ||
| │ ├── CartesianAxisIndex # a declared cell class: what users subclass | ||
| │ └── Staggered[D: CartesianAxisIndex] # its derived partner | ||
| └── (direct subclasses) # mesh locations, and index spaces without geometry | ||
| ``` | ||
|
|
||
| Both levels sit *below* `DimensionIndex`, so `Staggered[K]` stays a `DimensionIndex` | ||
| and no annotation or `issubclass` guard in the tree widens. The bound is on the | ||
| *declared* level, and `Staggered[K]` is only an `AnyCartesianAxisIndex`, so: | ||
|
|
||
| | Rejected | Statically (mypy, pyright) | At runtime | | ||
| | ----------------------------------------------- | -------------------------- | --------------------------------------------- | | ||
| | `Staggered[Staggered[K]]` | `[type-var]` | `TypeError` | | ||
| | `Staggered[Cell]`, staggering a local dimension | `[type-var]` | `TypeError` | | ||
| | `Cell + 1`, `Cell - 1` | `[operator]` | `TypeError`; a `DSLError` in a field operator | | ||
|
|
||
| **Open question:** The hierarchy says what a dimension is, not how dimensions relate to | ||
| each other: the Cartesian axes `I` and `J` are | ||
| orthogonal factors of a product grid, each its own horizontal direction, while the | ||
| mesh locations `Cell`, `Edge` and `Vertex` of a typical model are different entity | ||
| sets on *one* shared horizontal space. Expressing that relation belongs with a future | ||
| entity level (e.g. a `LocationIndex`) and is left to a later ADR. | ||
|
|
||
| ### `Dimension` is annotation-only | ||
|
|
||
| `common.Dimension` becomes an alias for `type[DimensionIndex]`. | ||
|
|
||
| ### Naming | ||
|
|
||
| A dimension's name is `.tag` (typed `common.Tag`) and `.value` keeps its meaning as the | ||
| index position, on the *instance*. | ||
|
|
||
| ### Display uses the unqualified name | ||
|
|
||
| The `tag` is qualified, but it is *identity and IR spelling*, not a display name. | ||
| User-facing diagnostics use `cls.__qualname__` by default, so a field reads like | ||
| `Field[[IDim], float64]`. | ||
|
|
||
| ### Equality between dimensions is identity | ||
|
|
||
| `I == J` is `True` only for the same class. With an integer, `I == 5` builds a `Domain` | ||
| instead, for `concat_where`. | ||
|
|
||
| ## Alternatives considered | ||
|
|
||
| - **`(name, kind)` value equality with an interning registry.** See Decision. | ||
|
|
||
| ## References | ||
|
|
||
| - Resolves, without a plugin, the static-typing gap reported in #2503 (closed). | ||
| - Supersedes the `_Staggered` name-prefix mechanism of | ||
| [ADR 0026](0026-Staggered_Dimensions.md); the indexing convention there is | ||
| unchanged. | ||
| - Consequence for the build cache of [ADR 0023](0023-Fingerprinting.md). | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,7 +46,7 @@ import gt4py.next as gtx | |
| `i + 1`: | ||
|
|
||
| ```{code-cell} ipython3 | ||
| I = gtx.Dimension("I") | ||
| class I(gtx.CartesianAxisIndex): ... | ||
| IField = gtx.Field[gtx.Dims[I], gtx.float64] | ||
| a = gtx.as_field({I: 6}, np.array([0.0, 1.0, 4.0, 9.0, 16.0, 25.0])) | ||
|
|
@@ -172,13 +172,13 @@ dimensions, the offsets `E2C` (the cells of every edge) and `C2E` (the edges of | |
| every cell), and the connectivity tables behind them. | ||
|
|
||
| ```{code-cell} ipython3 | ||
| Cell = gtx.Dimension("Cell") | ||
| Edge = gtx.Dimension("Edge") | ||
| E2CDim = gtx.Dimension("E2C", kind=gtx.DimensionKind.LOCAL) | ||
| C2EDim = gtx.Dimension("C2E", kind=gtx.DimensionKind.LOCAL) | ||
| class Cell(gtx.DimensionIndex): ... | ||
| class Edge(gtx.DimensionIndex): ... | ||
| class E2CDim(gtx.DimensionIndex, kind=gtx.DimensionKind.LOCAL): ... | ||
| class C2EDim(gtx.DimensionIndex, kind=gtx.DimensionKind.LOCAL): ... | ||
| E2C = gtx.FieldOffset("E2C", source=Cell, target=(Edge, E2CDim)) | ||
| C2E = gtx.FieldOffset("C2E", source=Edge, target=(Cell, C2EDim)) | ||
| E2C = gtx.FieldOffset(E2CDim.tag, source=Cell, target=(Edge, E2CDim)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cannot we just use the dimension itself here instead of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A follow-up. Here 🤖 Written by an agent on my behalf. |
||
| C2E = gtx.FieldOffset(C2EDim.tag, source=Edge, target=(Cell, C2EDim)) | ||
| edge_to_cell_table = np.array( | ||
| [[0, -1], [2, -1], [2, -1], [3, -1], [4, -1], [5, -1], | ||
|
|
@@ -189,8 +189,8 @@ cell_to_edge_table = np.array( | |
| ) | ||
| offset_provider = { | ||
| "E2C": gtx.as_connectivity([Edge, E2CDim], codomain=Cell, data=edge_to_cell_table), | ||
| "C2E": gtx.as_connectivity([Cell, C2EDim], codomain=Edge, data=cell_to_edge_table), | ||
| E2CDim.tag: gtx.as_connectivity([Edge, E2CDim], codomain=Cell, data=edge_to_cell_table), | ||
| C2EDim.tag: gtx.as_connectivity([Cell, C2EDim], codomain=Edge, data=cell_to_edge_table), | ||
| } | ||
| CellField = gtx.Field[gtx.Dims[Cell], gtx.float64] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the reversible thing still needed? If not, there is no requirement to share this. They might still share, but it can be removed as implementation detail from this adr.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only injectivity is a requirement, and the ADR now says only that (e7cdbea). For the code, see my reply on
from_codegen_nameindimensions.py.🤖 Written by an agent on my behalf.