Skip to content
Draft
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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ statically meaningful.
[[shared/dimensions-as-types|dimensions as types]] and GridTools/gt4py#2844).
Keeps independently declared same-named dimensions interchangeable and
avoids the importability rule. Rejected because it decouples the Python
type's identity from the IR's, needs a registry plus `copyreg` plus a custom
fingerprint deconstructor to paper over that gap, and cannot give a nested
type's identity from the IR's, needs a name-keyed registry and a blanket
`copyreg` hook to paper over that gap, and cannot give a nested
`V2E.Local` a unique name without further convention. See [[personal/egparedes/connectivities-as-types/connectivities-as-types#identity-is-the-qualified-python-name|Identity]].

**Integer type parameters for `max_neighbors`** (`LocalDimensionIndex[F: int, M: int]`).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,57 +8,56 @@ status: draft

> **Appendix** to [[personal/egparedes/connectivities-as-types/connectivities-as-types|Connectivities as types]].
> The mechanics behind rules 1 and 3-6 of *Identity is the qualified Python name*:
> reconstruction by import, the one `copyreg` hook, fingerprinting, the injective
> `DimensionRef`, the one `copyreg` hook, fingerprinting, the injective
> codegen mangling, and the `Staggered[D]` tag grammar.

## Rule 1 — reconstruction from the IR is an import
## Rule 1 — two levels

1. **Reconstruction from the IR is an import.** `resolve(tag)` imports the
longest importable module prefix and walks the rest as attributes (nested
classes like `V2E.Local` resolve naturally), the way `pickle` references a
class. Only where the module path ends is memoized: the attribute walk is
repeated, so a declaration redefined under the same name (a re-run
notebook cell) resolves to the new class. `AxisLiteral` stores only the tag
and derives `kind` and `dim` from it as read-only properties (the `TODO` at
`iterator/ir.py:93`); the derived `kind` is optional, since a local
dimension's is `None`;
printing IR never imports (`resolve_loaded`). `Staggered[D]` tags use the
small grammar `<owner tag>[<base tag>]`, resolved by subscripting the owner.
1. **Nothing is imported from a tag.** `DimensionRef(module, qualname, kind,
is_cartesian_axis, base)` is frozen, interned and pickled by value. Comparing a ref
with a dimension class raises `TypeError`, since one side missed its conversion;
hash-based lookups cannot be checked and are misses. `AxisLiteral` stores the ref.
`DimensionRef.from_tag(tag, kind, is_cartesian_axis=...)` rebuilds one from text:
the pretty printer prints short names plus a header with each dimension's tag, kind
and axis flag, from which the parser rebuilds the refs. The roundtrip backend, which
runs on classes, takes them from the fields and connectivities of each call, and synthesizes a class
from the ref only for a dimension none of them has.
`Staggered[D]` tags use the small grammar `<owner tag>[<base tag>]`.

## Rules 3-6 — registry, fingerprints, mangling, staggering

3. **No registry.** Classes pickle by reference. One narrow `copyreg` hook
exists, for `Staggered[D]`, whose bracketed qualname `save_global` cannot
look up; it reduces to the base dimension and re-interns.
4. **Fingerprints depend on qualified names, plus `kind`.** A dimension is
deconstructed as its by-reference name *and* its declaration-time `kind`, which
decides a field's layout order and the scan axis — so a dimension redefined under
the same name with another kind does *not* reuse artifacts. `Staggered[D]` goes
4. **Fingerprints depend on qualified names, plus the `DimensionRef`.** A dimension is
deconstructed as its by-reference name *and* its ref. The ref has the `kind`, which
decides a field's layout order and the scan axis, and the axis level
(`CartesianAxisIndex` versus `DimensionIndex`), on which index arithmetic depends —
so a dimension redefined under the same name with another of either does *not* reuse
cached stages or artifacts. `Staggered[D]` goes
through its base and inherits this; its base cannot be flipped in place, since
the class is interned by base identity and its tag embeds the base tag. The axis
level (`CartesianAxisIndex` versus `DimensionIndex`) is deliberately *not*
fingerprinted: it changes what is accepted, never what is emitted. A connectivity
the class is interned by base identity and its tag embeds the base tag. A connectivity
declaration is fingerprinted *additionally* by its domain, codomain, `Local` and
counts. Consequently the ADR 0023 build cache invalidates on module renames.
5. **Codegen names need injective mangling.** `codegen_name(tag)` is a prefix
escape — `_`→`_u`, `.`→`_d`, `[`→`_l`, `]`→`_r` — used by gtfn, DaCe, the
escape — `_`→`_u`, `.`→`_d`, `:`→`_c`, `[`→`_l`, `]`→`_r` — used by gtfn, DaCe, the
roundtrip backend and the nanobind bindings; its inverse
`from_codegen_name` is used by DaCe to parse names back. The simpler
"escape `__`, then replace `.`" is **not** injective (`".."` and `"_"`
collide); the prefix escape is tested exhaustively over its alphabet up
to length 4.
6. **Staggering is part of the dimension change.** The `_Staggered` name
prefix needs the name→class lookup that type identity removes, so
`Staggered[D]` is a real class and part of ADR 0029 (PR A), together with
`Staggered[D]` is a real class and part of ADR 0030 (PR A), together with
the [[personal/egparedes/connectivities-as-types/connectivities-as-types#cartesian-axis-dimensions|Cartesian axis dimensions]] its parameter is bounded on; see
[[personal/egparedes/connectivities-as-types/connectivities-as-types#staggeredd|`Staggered[D]`]].

## The `Staggered[D]` tag

- **IR and serialization.** The tag is
`gt4py.next.common.Staggered[<base tag>]` — the one tag that is not a
qualified name. `resolve` parses the `<owner>[<base>]` grammar and
subscripts the owner, the IR pretty-parser accepts it, and `codegen_name`
`gt4py.next.common:Staggered[<base tag>]` — the one tag that is not a
qualified name. `DimensionRef.from_tag` parses the `<owner>[<base>]` grammar
without importing the base, the IR pretty-parser accepts it, and `codegen_name`
escapes the brackets. `pickle` cannot look up a bracketed qualname, so one
`copyreg` hook reduces a staggered class to its base and re-subscripts on
load; fingerprints are likewise taken through the base.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ npx -y @marp-team/marp-cli@4 --no-stdin \

One declaration for offset, local dimension and provider key

ADR 0029 (dimensions) · ADR 0030 (connectivities)
ADR 0030 (dimensions) · ADR 0031 (connectivities; planned number)

> **Appendix** to [Connectivities as types](connectivities-as-types), summarising the note it is published alongside; where the two disagree the note is authoritative. Every slide links the section it compresses, and the deck adds no facts of its own. Rendering instructions are on the slide before this one.

Expand Down Expand Up @@ -81,11 +81,11 @@ The class *contains* its local dimension and *is* its own provider key. Nothing

## Identity is the qualified Python name

`tag = f"{cls.__module__}.{cls.__qualname__}"`
`tag = f"{cls.__module__}:{cls.__qualname__}"`

- the static view (nominal types) and the runtime view (`is`) agree by construction
- reconstruction from the IR is an **import**, the way `pickle` references a class
- so `<locals>` dimensions are rejected, and a redefinition is a *different* connectivity
- below the frontend a dimension is a **`DimensionRef` value**: nothing is imported from a tag
- pickling needs importable classes, so `<locals>` dimensions are rejected, and a redefinition is a *different* connectivity
- the IR names a connectivity by its `offset_tag` = its local dimension's tag, so shifts, reductions and sparse arguments find one table under **one** string

→ [Identity](connectivities-as-types#identity-is-the-qualified-python-name)
Expand Down Expand Up @@ -164,8 +164,8 @@ Of ten cross-object identity constraints: **six dissolve**, three collapse into

| PR | Contents |
| --- | --- |
| A #2899 | dimensions as classes, `Staggered[D]`, ADR 0029 |
| B #2907 | `NeighborConnectivity`, `LocalDimensionIndex`, ADR 0030 |
| A #2899 | dimensions as classes, `DimensionRef`, `Staggered[D]`, ADR 0030 |
| B #2907 | `NeighborConnectivity`, `LocalDimensionIndex`, ADR 0031 (planned) |
| C #2910 | class-keyed providers, `FieldOffset` removed, migration script |
| D #2912 | `MultiDimensionIndex`, typed embedded positions |

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ status: draft

A **Cartesian axis** is an index space with integer index arithmetic and
exactly one staggered partner — the dimensions `CartesianConnectivity` acts on.
Read one axis of a Cartesian grid as a
1-dimensional CW complex — a path when the axis is bounded, a cycle when it is
periodic. Such a complex has exactly two cell classes, of degree 0 and 1, and a
The axis itself is the two-way infinite integer line (ADR 0030); read it as a
1-dimensional CW complex. A `Domain` selects a bounded range of it, which is a
path complex; a periodic grid is that range plus a wrap-around rule owned by the
grid, not a property of the dimension. Such a complex has exactly two cell classes, of degree 0 and 1, and a
declared axis together with its `Staggered[·]` name those two classes.
`Staggered` is the **involution that swaps them**. The types do not say which
class has degree 0, and they need not: the ADR 0026 position convention
Expand Down Expand Up @@ -99,7 +100,7 @@ a reference implementation and on boundary index ranges.
## 3. Extents

**Extents are declared, never derived.** A path complex has `n` 0-cells and
`n - 1` 1-cells; a cycle has `n` and `n`. So the two members of a pair do not in
`n - 1` 1-cells; with a periodic wrap-around the grid may use `n` and `n`. So the two members of a pair do not in
general have the same range, and for a bounded, halo-free complex *which one is
wider is the degree assignment* (periodicity or halo padding destroys that
correspondence — see §7):
Expand All @@ -110,8 +111,9 @@ the interfaces run `[0, n+1)`. Both are expressible without any new type-level
concept because a `Domain` holds absolute `UnitRange(start, stop)`s rather than
sizes — see
[[personal/havogt/field-data-protocol/field-data-protocol|the field data protocol]]
for why absolute addressing is the right domain model — and the periodic case
needs nothing extra, the two ranges simply coincide in size. The invariant a grid
for why absolute addressing is the right domain model — and a periodic grid
needs nothing extra from the dimension: the two ranges coincide in size and the
wrap-around belongs to the grid's boundary or halo handling. The invariant a grid
builder owes is that the two ranges *interleave*; their sizes then differ by at
most one when the complex is closed, and halo extension relaxes even that (two of
SGRID's four bounded cases, `low` and `high`, have equal sizes). An absolute range
Expand Down Expand Up @@ -240,8 +242,8 @@ Numbered as in the main note's *Open questions*.
readable without the declaration, the frontend and embedded shift test matrix
doubles, and the keyword would have to be folded into the dimension fingerprint.
That last cost is now a known quantity rather than an open hole: Identity rule 4
fingerprints a dimension's `kind` alongside its by-reference name, so an
alignment keyword would follow the same pattern. Not in this stack.
fingerprints a dimension's `DimensionRef` alongside its by-reference name, so an
alignment would be one more field of the ref. Not in this stack.

6. **`kind`'s remaining two jobs.** Once `LOCAL` leaves the enum, `kind` is a
layout sort key and a name for the scan axis. Layout is a property of the field,
Expand All @@ -268,10 +270,10 @@ Numbered as in the main note's *Open questions*.
exterior-calculus surface exists (Proposal 1 of
[[personal/egparedes/discretization-independent-fd-syntax|the surface-syntax note]]).
Absolute ranges do **not** determine the
degree in general: a periodic axis gives both classes `[0, n)` under either
degree in general: on a periodic grid both classes can be `[0, n)` under either
assignment, and halo padding makes the relative widths arbitrary. A static degree
needs no periodicity and would not be ill-defined — it is *deriving extents* that
needs the degree together with a periodicity and halo policy. Revisit if that
is a property of the infinite line and would not be ill-defined — it is *deriving
extents* that needs the degree together with the grid's wrap-around and halo policy. Revisit if that
surface lands — but note that a degree is *canonical* for a mesh
location and only *declarational* for a Cartesian axis, so it belongs on the
`LocationIndex` of the main note's item 8 rather than here
Expand Down
2 changes: 1 addition & 1 deletion content/shared/dimensions-as-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ status: superseded

> **Superseded** by
> [[personal/egparedes/connectivities-as-types/connectivities-as-types|Connectivities as types]]
> (gt4py ADR 0028, GridTools/gt4py#2899), which makes a dimension a class with
> (gt4py ADR 0030, GridTools/gt4py#2899), which makes a dimension a class with
> *type* identity (its qualified name) instead of the `(name, kind)` value
> identity and interning registry proposed here. Kept for reference until that
> proposal moves to `shared/`.
Expand Down