Finite axes¶
A tensor indexed by finite types is a function from those types, laid out as an array (Paszke et al., 2021). This page states which types are finite, how their elements are ordered, the typing and elaboration of tabulation, selection, and reduction, and what data binding guarantees. The surface is in Tensors indexed by finite types.
1. Finite types and their order¶
A type \(T\) is finite when one of the following holds, and then \(|T|\) and the enumeration \(e_T : \{0, \dots, |T| - 1\} \to T\) are:
| \(T\) | \(\lvert T \rvert\) | \(e_T\) |
|---|---|---|
Bool |
\(2\) | false, true |
Unit |
\(1\) | unit |
| an index sort of nullary constructors \(c_1 \mid \dots \mid c_k\) | \(k\) | \(c_1, \dots, c_k\) |
| \(A_1 \times \dots \times A_m\) | \(\prod_j \lvert A_j \rvert\) | lexicographic, \(A_1\) slowest |
| a closed record over labels \(l_1 < \dots < l_m\) | \(\prod_j \lvert A_{l_j} \rvert\) | lexicographic in label order |
| a closed variant over labels \(l_1 < \dots < l_m\) | \(\sum_j \lvert A_{l_j} \rvert\) | \(l_1\)'s elements, then \(l_2\)'s, ... |
| a closed, unindexed family with constructors \(C_1, \dots, C_k\) | \(\sum_i \prod_j \lvert F_{ij} \rvert\) | \(C_1\)'s elements, then \(C_2\)'s, ...; within \(C_i\), lexicographic in its fields \(F_{i1}, F_{i2}, \dots\) |
A family is finite only when no constructor binds statics of its own and the enumeration of its
fields does not reach the family again; an index sort with a constructor that takes arguments is
recursive. Every other type (Int, Real, String, functions, tensors, open rows, abstract
type variables) is refused as an axis. Since the enumeration is fixed by the type, a position on an
axis and an element of its type determine each other: \(e_T\) is a bijection, and its inverse is the
mixed-radix rank element_ordinal computes (quivers.qiec.axes).
2. Tensor types¶
A tensor type \(E[T_1, \dots, T_k]\) has one dimension per axis. In the core, the dimension of an axis
\(T\) is the extent \(|T|\) carried together with \(T\) (an
AxisExtent), and the dimension of Fin n is the extent \(n\)
alone. Two tensor types are equal when their element types and dimensions are, so
\(E[T] \neq E[U]\) for \(T \neq U\) and \(E[T] \neq E[\mathrm{Fin}\ |T|]\). The kernel checks that the
extent of a named dimension is the cardinality of its type (qiec-axis).
The elaborator puts the dimensions of every written tensor type in canonical order: named axes first, stably sorted by the spelling of their types, then positional axes in written order. A permutation of the axes of a written type therefore elaborates to the same type, and a tensor type has one layout: the entry at elements \((t_1, \dots, t_k)\) of the axes in canonical order is at positions \((e_{T_1}^{-1}(t_1), \dots, e_{T_k}^{-1}(t_k))\).
Because a dimension carries its axis, every operation on shapes keeps axis names: slicing off a leading axis, the extent of a comprehension, the broadcast shape of a primitive applied to equally shaped tensors, and the size of a plate. A static extent argument is an extent, not an axis, so a computation over \(E[\mathrm{Fin}\ n]\) instantiated at \(n = |T|\) takes a positional tensor and refuses one over \(T\).
3. Tabulation, selection, and reduction¶
where \(B[T]\) adds \(T\) to the axes of a tensor-typed \(B\) and the result is in canonical order, and
the axis \(T_j\) selected by \(a\) is the first unselected axis whose type is \(a\)'s (\(\mathrm{Fin}\ n\)
for an Int). Elaboration produces the kernel's existing terms:
for w : T. eis aComprehensionover the extent of \(T\) whose index \(i\) is \(w\)'s position; \(w\) is bound to \(e_T(i)\) (Boolean comparisons, mixed-radix digits, and a conditional over constructor or label ranges) only when the body reads it as a value. A body that is a tensor whose axes would follow \(T\) out of canonical order is tabulated first and then permuted by a comprehension over the canonical axes.t[a]is a chain ofGatherterms in canonical order, under a comprehension over the unselected axes. The position of \(a\) is the binder's index when \(a\) is a tabulation or reduction binder over \(T_j\), a literal when \(a\) is closed, and otherwise the mixed-radix rank of its parts, which a pure term computes only when \(T_j\) is built fromBool,Unit, products, and records. A family or variant value is read only by a case analysis, so selecting by a run-time one is refused.op over w : T. etabulates \(e\) over \(w\) and applies the kernelReductionopto each slice along \(w\)'s axis. Several binders reduce one at a time,op over v, w. easop over v. op over w. e, which every reduction admits since each is associative and commutative and its slices have one size.w == afor a binder \(w\) over \(T\) compares positions: \(e_T\) is injective, so equal positions are equal elements.
Each form denotes the function it builds: the entry of for w : T. e at position \(i\) is \(e\) with
\(w := e_T(i)\), and (for w : T. e)[a] is \(e\) with \(w := a\).
4. The first-order boundary¶
The reference machine runs the checked module, whose types carry axis names. The compiled runtime
and every transpile target read the first-order module, in which
erase_axes has replaced every named extent by its plain extent
before elimination. Erasure changes no term: a named and a positional dimension of one extent have
one layout, so every run agrees with the reference machine on the same positional arrays. A tensor
type with positional axes only is the same core type, byte for byte, whichever spelling wrote it.
5. Data binding¶
Labeled data for a tensor type is aligned to the canonical layout by
align_value: the data's dimension names must be the type's
axis names, and on each axis the stated coordinates must be exactly the labels of \(e_T\), each
stated once. A missing, an unknown, or a repeated coordinate, or a missing or repeated cell of a
table, is refused with the coordinate named, and nothing is reordered except by labels.
The aligned value is a function of the stated cells alone. Two tables that state the same cells
in different row orders, or an array and a table that state the same cells, align to equal values,
so a run and the digest of its data (FitCache) do not
depend on the order the data was written in.
Cited sources¶
Adam Paszke, Daniel D. Johnson, David Duvenaud, Dimitrios Vytiniotis, Alexey Radul, Matthew J. Johnson, Jonathan Ragan-Kelley, and Dougal Maclaurin. Getting to the Point: Index Sets and Parallelism-Preserving Autodiff for Pointful Array Programming. Proceedings of the ACM on Programming Languages, 5(ICFP):88:1–88:29, 2021. doi:10.1145/3473593.