Pattern operations
rearrange, reduce, repeat, einsum and split take a pattern inputs -> output. Each input side describes one operand: it must have as many entries as the operand has axes (E0403), and its entries bind by position:
- A name already in scope (a dim, a parameter’s axis, an earlier pattern’s name) must have the axis’s size (E0401).
- A new name is introduced with the axis’s size, and stays in scope for the rest of the function once the operation checks.
- A number must equal the axis’s size (E0401).
- A group
(gh p1), or a grouped size16gh, which is(gh 16), binds one axis whose size is the product of the parts. At most one part may be unknown, and it is solved for (E0402, E0409); a group that can’t fit is E0401. Groups can’t be nested, and a name can appear only once per side (E0407). - A name that is the name of a different axis of the operand, as
Dinrearrange(x, b D n -> ...)on a[b n D], is E0408: patterns name axes by position, and this is probably a transposition.
Keyword arguments give sizes to new names: rearrange(x, b n (h d) -> b h n d, h=12). The value is a positive integer, or an integer value known at run time, which gives an unknown size. Naming a size that’s already in scope, or a name that isn’t in the pattern, is E0406.
The output side’s names must be names from the inputs (E0405), except in repeat. A number on the output other than 1 must match an equal number on the input, except in repeat (E0407). The result’s axes are named after the output’s names; groups and numbers give unnamed axes.
| Operation | Form | Rule |
|---|---|---|
rearrange | rearrange(x, b n D -> b D n) | Every input name is in the output (E0407), and the number of elements is kept (E0401). |
reduce | reduce(x, b n D -> b D, mean) | Names left out are reduced with sum, mean, max, min or prod (a name or a string; E0406). |
repeat | repeat(x, b D -> b r D, r=4) | New names and numbers add axes; a new name needs a size (E0409). |
einsum | einsum(q, k: b H n Dh, b H m Dh -> b H n m) | One input side per operand, only names (E0407). A name shared by several operands must have one size; names not in the output are summed over. Operands must share a dtype (E0502) and not be masks. The result is Real. |
split | split(x, b n (3 H Dh) -> b H n Dh) | The one number on the left that isn’t on the right (other than 1) is the number of results, at most 256 (E0407). They must add up to the input (E0401). Bind the results to that many names (E0411). |
A pattern that renames the axis a Logit, Prob or LogProb domain is over carries the domain to the new name; grouping that axis leaves just its range. reduce gives the element type its reduction would (Built-in operations).