Finite axes¶
Which types index a tensor axis, the canonical order of their elements and of a tensor's axes, the coordinate labels data and targets read, and the erasure of axis names at the first-order boundary. The rules are in Finite axes.
axes
¶
Finite types as the index sets of tensor axes.
A tensor's dimensions are extents. A dimension that is an
AxisExtent is also named: it ranges over
the elements of a finite type, in the order that type fixes, so a tensor
Real[World] is a function from World to Real laid out as an array
[@paszke2021getting]. A positional dimension is the Fin n case: it
ranges over the positions 0 to n - 1.
This module answers what such an axis is:
- whether a type is finite, and its elements in canonical order
(
axis_elements) with their coordinate labels; - its cardinality (
axis_cardinality) and the extent that names it (axis_extent); - the canonical order of a tensor's axes
(
canonical_dimensions): named axes first, sorted by the axis's rendering, then positional axes in the order written, so a tensor type does not depend on the order its axes are written in; - the axis map of a tensor type, which data binding and transpile targets
read (
tensor_axes); - the erasure of names at the first-order boundary
(
erase_axes), after which every compiled or exported reader sees positional arrays in canonical order.
The canonical order of a finite type's elements:
| Type | Elements, in order |
|---|---|
Bool |
false, true |
Unit |
unit |
| an index sort of nullary constructors | its constructors, in declaration order |
| a closed family | its constructors in declaration order, each with its fields varying lexicographically, the first field slowest |
a product A * B |
lexicographic, A slowest |
a record {a : A, b : B} |
lexicographic over the fields in label order |
a variant <a : A, b : B> |
the labels in order, each with its payload's elements |
Every other type is not finite, and naming an axis by it is refused with the property that fails: an infinite primitive, a recursive family, a family with indices or with a constructor that binds its own statics, a function type, an open row, an abstract type variable.
Declarations
¶
Bases: Protocol
The declarations enumerating a finite type reads.
KernelRegistry and the
elaborator's tables both provide them.
constructors
property
¶
constructors: Mapping[ConstructorId, ConstructorDecl]
The declared constructors, by identity.
FiniteAxisError
¶
Bases: TypeError
A type named as an axis is not finite, or an extent misnames it.
| PARAMETER | DESCRIPTION |
|---|---|
message
|
What was refused and the property that fails.
TYPE:
|
code
|
The diagnostic code,
TYPE:
|
Source code in src/quivers/qiec/axes.py
ElementTree
didactic-model
¶
ElementTree(*, kind: str, head: str = '', labels: tuple[str, ...] = (), parts: tuple[ElementTree, ...] = field(default=(), opaque=True, extras=OPAQUE_BY_VALUE))
Bases: QiecModel
The structure of one element of a finite type.
| PARAMETER | DESCRIPTION |
|---|---|
kind
|
TYPE:
|
head
|
The Boolean's spelling, the constructor's name, or the variant's label; empty for a tuple, a record, or unit.
TYPE:
|
labels
|
The record's field labels in order, aligned with
TYPE:
|
parts
|
The trees of the element's components, in order.
TYPE:
|
AxisElement
didactic-model
¶
AxisElement(*, label: str, tree: ElementTree = field(opaque=True, extras=OPAQUE_BY_VALUE))
Bases: QiecModel
One element of a finite type, as an axis coordinate.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The element's coordinate label:
TYPE:
|
tree
|
The element's structure: a constructor, label, or literal name at the head with the trees of its parts, which the elaborator turns into a value or a static index.
TYPE:
|
TensorAxis
didactic-model
¶
TensorAxis(*, name: str, extent: int | None, coordinates: tuple[str, ...] = (), named: bool = False)
Bases: QiecModel
One axis of a tensor type, as data and targets see it.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The axis's name: the rendering of its finite type, or
TYPE:
|
extent
|
Its extent, when static.
TYPE:
|
coordinates
|
The coordinate labels in canonical order; for a positional axis,
the positions
TYPE:
|
named
|
Whether the axis is named by a finite type.
TYPE:
|
product_components
¶
The components of a builtin product type.
| PARAMETER | DESCRIPTION |
|---|---|
type_
|
A type.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[TypeExpr, ...] or None
|
The component types, or |
Source code in src/quivers/qiec/axes.py
family_of
¶
family_of(type_: TypeApplication, registry: Declarations) -> FamilyDecl | None
The declared family a type application applies, if any.
| PARAMETER | DESCRIPTION |
|---|---|
type_
|
A type application.
TYPE:
|
registry
|
The declarations in scope.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
FamilyDecl or None
|
The family, or |
Source code in src/quivers/qiec/axes.py
constructor_field_types
¶
constructor_field_types(family: FamilyDecl, constructor: ConstructorDecl, type_: TypeApplication) -> tuple[TypeExpr, ...]
A constructor's field types at the family's static arguments.
| PARAMETER | DESCRIPTION |
|---|---|
family
|
The family.
TYPE:
|
constructor
|
One of its constructors.
TYPE:
|
type_
|
The family applied to its parameters.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[TypeExpr, ...]
|
The field types with the parameters substituted. |
Source code in src/quivers/qiec/axes.py
element_label
¶
element_label(tree: ElementTree) -> str
The coordinate label of one element.
| PARAMETER | DESCRIPTION |
|---|---|
tree
|
The element.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Its surface spelling: |
Source code in src/quivers/qiec/axes.py
axis_elements
¶
axis_elements(axis: AxisType, registry: Declarations) -> tuple[AxisElement, ...]
The elements of a finite type, in canonical order.
| PARAMETER | DESCRIPTION |
|---|---|
axis
|
A finite type or index sort.
TYPE:
|
registry
|
The declarations in scope.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[AxisElement, ...]
|
Each element with its coordinate label. |
| RAISES | DESCRIPTION |
|---|---|
FiniteAxisError
|
If the type is not finite, naming the property that fails. |
Source code in src/quivers/qiec/axes.py
axis_cardinality
¶
axis_cardinality(axis: AxisType, registry: Declarations) -> int
The number of elements of a finite type.
| PARAMETER | DESCRIPTION |
|---|---|
axis
|
A finite type or index sort.
TYPE:
|
registry
|
The declarations in scope.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
Its cardinality. |
| RAISES | DESCRIPTION |
|---|---|
FiniteAxisError
|
If the type is not finite. |
Source code in src/quivers/qiec/axes.py
element_ordinal
¶
element_ordinal(axis: AxisType, tree: ElementTree, registry: Declarations) -> int
The position of one element in its type's canonical order.
| PARAMETER | DESCRIPTION |
|---|---|
axis
|
A finite type or index sort.
TYPE:
|
tree
|
An element of it.
TYPE:
|
registry
|
The declarations in scope.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
The element's ordinal, computed by mixed radix without listing the type's elements. |
| RAISES | DESCRIPTION |
|---|---|
FiniteAxisError
|
If the type is not finite or the tree is not one of its elements. |
Source code in src/quivers/qiec/axes.py
axis_extent
¶
axis_extent(axis: AxisType, registry: Declarations) -> AxisExtent
The dimension of an axis named by a finite type.
| PARAMETER | DESCRIPTION |
|---|---|
axis
|
A finite type or index sort.
TYPE:
|
registry
|
The declarations in scope.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
AxisExtent
|
The named extent. |
| RAISES | DESCRIPTION |
|---|---|
FiniteAxisError
|
If the type is not finite. |
Source code in src/quivers/qiec/axes.py
registration_order
¶
registration_order(families: Iterable[FamilyDecl], constructors: Iterable[ConstructorDecl]) -> tuple[ConstructorDecl, ...]
Order constructors so each follows those its fields' axes enumerate.
A constructor field of type Real[T] has an axis whose extent is the
number of values of T, which the constructors of the families T
reaches determine, so checking the field reads them. Families keep
their given order where no axis relates them, and each family's
constructors keep theirs; the order depends only on the declarations,
not on the order a lowering produced them in.
| PARAMETER | DESCRIPTION |
|---|---|
families
|
The families the constructors belong to.
TYPE:
|
constructors
|
The constructors, grouped by family in their declared order.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[ConstructorDecl, ...]
|
The constructors, each family's after those of every family its fields' axes reach. |
| RAISES | DESCRIPTION |
|---|---|
FiniteAxisError
|
If a field of a family's constructors indexes a tensor by a type that reaches the family itself. |
Source code in src/quivers/qiec/axes.py
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 | |
named_extents
¶
named_extents(root: object) -> Iterator[ShapeIndex]
Every shape under a static term or kernel node that names an axis.
| PARAMETER | DESCRIPTION |
|---|---|
root
|
A type, a static term, or any kernel node.
TYPE:
|
| YIELDS | DESCRIPTION |
|---|---|
ShapeIndex
|
Each shape with at least one named dimension, once per occurrence. |
Source code in src/quivers/qiec/axes.py
check_axes
¶
check_axes(root: object, registry: Declarations) -> None
Check every named shape under a term.
A named dimension must be the cardinality of a finite type. Axis order is not checked here: a type written in source is put in canonical order by the elaborator, while terms the elaborator builds may lay an intermediate out in another order before permuting it.
| PARAMETER | DESCRIPTION |
|---|---|
root
|
A type or other static term.
TYPE:
|
registry
|
The declarations in scope.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
FiniteAxisError
|
If an axis is not finite or its extent is not its cardinality. |
Source code in src/quivers/qiec/axes.py
check_axis_extent
¶
check_axis_extent(extent: AxisExtent, registry: Declarations) -> None
Check that a named extent is its axis's cardinality.
| PARAMETER | DESCRIPTION |
|---|---|
extent
|
A named dimension.
TYPE:
|
registry
|
The declarations in scope.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
FiniteAxisError
|
If the axis is not finite, or the extent is not its cardinality. |
Source code in src/quivers/qiec/axes.py
canonical_dimensions
¶
A tensor's dimensions in canonical axis order.
| PARAMETER | DESCRIPTION |
|---|---|
dimensions
|
The dimensions as written.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[IndexTerm, ...]
|
Named axes sorted by the rendering of their types (repeats of one type keep their written order), then positional axes in written order. |
Source code in src/quivers/qiec/axes.py
canonical_axis_order
¶
canonical_axis_order(dimensions: tuple[IndexTerm, ...]) -> tuple[int, ...]
The source position of each axis in canonical order.
| PARAMETER | DESCRIPTION |
|---|---|
dimensions
|
The dimensions as laid out.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[int, ...]
|
For each canonical position, the position of the dimension that goes there; the identity when the layout is canonical. |
Source code in src/quivers/qiec/axes.py
is_canonical
¶
is_canonical(dimensions: tuple[IndexTerm, ...]) -> bool
Whether dimensions are in canonical axis order.
| PARAMETER | DESCRIPTION |
|---|---|
dimensions
|
A tensor's dimensions.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True when sorting them canonically leaves them unchanged. |
Source code in src/quivers/qiec/axes.py
axis_name
¶
axis_name(dimension: IndexTerm) -> str
The name of one dimension as data and targets spell it.
| PARAMETER | DESCRIPTION |
|---|---|
dimension
|
A tensor dimension.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The name a named axis is written with, else the rendering of its
type, or |
Source code in src/quivers/qiec/axes.py
tensor_axes
¶
tensor_axes(dimensions: tuple[IndexTerm, ...], registry: Declarations) -> tuple[TensorAxis, ...]
The axes of a tensor, with names and coordinate labels.
| PARAMETER | DESCRIPTION |
|---|---|
dimensions
|
The tensor's dimensions, in canonical order.
TYPE:
|
registry
|
The declarations in scope.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[TensorAxis, ...]
|
One entry per dimension. A name that repeats within the tensor is
suffixed |
Source code in src/quivers/qiec/axes.py
named_dimensions
¶
named_dimensions(dimensions: tuple[IndexTerm, ...]) -> bool
Whether any dimension is named by a finite type.
| PARAMETER | DESCRIPTION |
|---|---|
dimensions
|
A tensor's dimensions.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True when one is an |
Source code in src/quivers/qiec/axes.py
positional_dimension
¶
One dimension with its name forgotten.
| PARAMETER | DESCRIPTION |
|---|---|
dimension
|
A dimension.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
IndexTerm
|
The plain extent of a named dimension; any other dimension itself. |
Source code in src/quivers/qiec/axes.py
erase_axes
¶
Forget every axis name in a kernel graph.
The compiled runtime and every transpile target read arrays in canonical order by position; this is the boundary where names stop.
| PARAMETER | DESCRIPTION |
|---|---|
root
|
A module, or any kernel node.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
T
|
The graph with every named extent replaced by its plain extent; the root itself when it names no axis. |