| Did you know ... | Search Documentation: |
| Pack array -- archive/kernel_ir.md |
Status: draft.
+=.
Equality saturation needs terms whose meaning doesn't depend on an order of
side effects. Mutation only appears when a dialect lowers the IR.
The one order a kernel may state is an iterate: its iterations run in
order, and one may read back, through after/2, what the previous ones
stored.push (build the graph) → graph_schedule → fuse → choose_formats → allocation → lower each kernel to the IR (index_expr is only called here) → optimize the IR: saturate the enabled rule sets, extract → lower the IR to the dialect, print, compile
The lowering is ir_lower.pl, and each dialect has a printer: ir_prolog.pl
for the Prolog device, ir_c.pl for C and OpenCL C. Between them,
ir_optimize.pl optimizes the clause with the rules each device declares
(see Optimizing).
A kernel is a clause:
kernel(Out-Format0, In1-Format1, ..., InN-FormatN) :- Body.
In1-Format1.forall over a between per
dimension, and stores every element. A kernel computing an element from
others, as a factorization, ranges over the independent part of the domain,
say a batch, and stores the rest inside it, in iterates and foralls. The domain is part of the kernel:
calling the clause computes the whole output, the output indices get
their bounds from their binders like any index variable, and schedule
decisions such as parallel/1 apply to it. Calling a function per element,
as C does, is how a dialect prints the domain, not part of the IR.kernel: a device registers the printed kernel under its
hash.| goal | meaning |
|---|---|
forall(Domain, Goal) | runs Goal for each solution of Domain |
between(0, Max, I) | binds the index variable I to `0..Max` |
load(Buffer-Format, Index, Value) | Value is the element Index of Buffer |
store(Buffer-Format, Index, Value) | writes Value as the element Index of Buffer |
Value is Expr | arithmetic |
cast(Format, In, Out) | Out is In in Format |
( Cond -> Then ; Else ) | Cond is a conjunction of index or value comparisons |
iterate(between(0, Max, K), Goal) | runs Goal for K from 0 to Max, in order: each iteration sees the stores of the previous ones |
load(after(Out-Format, K), Index, Value) | Value is the element Index of the output Out, as the iterations of K before the current one stored it |
aggregate_all(sum(X), Goal, Sum) | sum of X over the solutions of Goal |
aggregate_all(max(X), Goal, Max) | maximum of X over the solutions of Goal |
| annotations | see Annotations |
Index expressions, the arguments Index and the operands of index
comparisons, use +, *, //, mod, integer constants and index variables.
Every index value is non-negative, so // and mod agree with C's / and
`%`.
Reductions are aggregate_all/3 with its Prolog semantics, which is the reference:
max does: dialects must lower it to
agree, not to C's `if (acc < x) acc = x`, which ignores a NaN unless it
comes first;aggregate_all(max(...))
failing on an empty domain is then never observed.
Value expressions use +, -, *, / and ** (of floats only: C
truncates a division of integers, and a power of integers may be a rational
in Prolog), sin, log, sqrt, constants, float,
integer and random_float. They appear in is, and in every other value
position: a cast's input, a store's value and an aggregate's template, as
in cast(float32, VA*VB, P) or aggregate_all(sum(VA*VB), …). A variable
only names a value a goal produces: a load, a cast, a comparison's result, a
selection's result or a reduction; a sub-expression stays inside its
expression. A comparison
producing a value is a conditional: ( A < B -> V = 1 ; V = 0 ).
A selection is a conditional on a value whose branches both assign its
result, each computing only the operand it selects:
( VC =\= 0 -> …, V is VA ; …, V is VB ).
The helper predicates, for buffers that are Prolog data(...) terms:
load(Buffer-_Format, Index, Value) :- I is Index + 1, arg(I, Buffer, Value). store(Buffer-_Format, Index, Value) :- I is Index + 1, nb_setarg(I, Buffer, Value). cast(Format, In, Out), float_format(Format) => round_to_format(Format, In, Out). cast(_, In, Out) => Out is integer(In). % A value rounds to a float format as C converts it, to the nearest with % ties to even: through a one-element C buffer of buffer.pl. round_to_format(Format, In, Out) :- buffer_from_data(Buffer, Format, 1, data(In)), buffer_to_data(Buffer, Format, 1, data(Out)). float_format(float16). float_format(float32). float_format(float64).
A kernel runs by asserting its clause and calling it on data, paired with formats or with free variables:
?- assertz((kernel(O-uint8, B-uint8) :- …)), O = data(_, _, _, _), kernel(O-uint8, data(2, 3)-_).
Casts:
16777217 → 16777216.0 in float32), overflow (65520.0 → inf in
float16) and subnormals (6.0e-8 → 5.960464477539063e-08 in
float16).round(). numpy's
astype truncates instead; the array library rounds on purpose.after(Out, K),
inside an iterate over K, at an element an earlier iteration stored.
The verifier checks that K is an enclosing iterate's and that Out is
stored; that the element is stored earlier is up to the kernel. The
reference catches it: an output starts unbound, so reading an element no
iteration stored is an instantiation error. The buffer pool guarantees it, since an input keeps its memory
until its reader has run, and it is also what makes C's restrict
correct.iterate, where iterations may store the same element in turn: an
after/2 load reads the last stored before its iteration. A fold keeps
its states so, in two slots.
Together, these make the body independent of the order of its stores and
loads, which equality saturation needs. A kernel may have several outputs:
several stores in one forall when they share a domain, or a conjunction of
foralls otherwise.
between), a load, an
is, a cast, a conditional or an aggregate_all.( Valid -> V is …, store(…) ; true ).between(0, Max, I), which also gives its
bounds: `0 =< I =< Max`. An iterate binds one, as its domain. Optimizations read bounds from the binders. After
optimization, a dimension of size 1 may be between(0, 0, 0).Every value has a format. Printers need it to declare variables, and the precision model needs it to round.
Buffer-Format, in the head and in each
of its accesses: an access is self-contained for a rule, and the head is
the signature printers need. The e-graph shares the repeated pair as one
class. A verifier checks that a buffer variable has a single format.load gives the format of the buffer.cast(Format, In, Out) gives Format. In may be a number: a constant
in a format.is gives the format of its operands. The lowering inserts casts so that
the operands of an operation share a format: the load formats of
load_formats/6.bool.integer, boolean and float64: exact
integers, and Prolog's floats.Annotations carry schedule decisions: how a dialect should run a part of a kernel, not what it computes. An annotation is a goal that wraps another one, and runs it unchanged:
simd(Goal) :- call(Goal). parallel(Goal) :- call(Goal).
So an annotated kernel still executes, and gives the same values. Printers
read annotations: simd(aggregate_all(sum(X), G, S)) prints as a C loop with
`#pragma omp simd reduction(+:S)`, and parallel(forall(D, G)) maps the
domain D to OpenCL work items.
As the lowering produces it, with the indices composed from the views:
kernel(O-float64, A-float64, B-float64, X-float64) :-
forall(( between(0, 1, I1), between(0, 1, I2) ),
( aggregate_all(sum(P),
( between(0, 1, T),
load(A-float64,
((I1*2 + I2)//2*4 + (I1*2 + I2) mod 2*2 + T)//4*2
+ ((I1*2 + I2)//2*4 + (I1*2 + I2) mod 2*2 + T) mod 4//2*0
+ ((I1*2 + I2)//2*4 + (I1*2 + I2) mod 2*2 + T) mod 4 mod 2*1,
VA),
load(B-float64,
((I1*2 + I2)//2*4 + (I1*2 + I2) mod 2*2 + T)//4*0
+ ((I1*2 + I2)//2*4 + (I1*2 + I2) mod 2*2 + T) mod 4//2*1
+ ((I1*2 + I2)//2*4 + (I1*2 + I2) mod 2*2 + T) mod 4 mod 2*2,
VB),
P is VA*VB ),
S),
load(X-float64, I1*2 + I2, VX),
V is S + VX,
store(O-float64, I1*2 + I2, V) )).
After index simplification:
kernel(O-float64, A-float64, B-float64, X-float64) :-
forall(( between(0, 1, I1), between(0, 1, I2) ),
( aggregate_all(sum(P),
( between(0, 1, T),
load(A-float64, I1*2 + T, VA),
load(B-float64, T*2 + I2, VB),
P is VA*VB ),
S),
load(X-float64, I1*2 + I2, VX),
V is S + VX,
store(O-float64, I1*2 + I2, V) )).
B.pad([1-1]) + 1
kernel(O-uint8, B-uint8) :-
forall(between(0, 3, I),
( ( 1 =< I, I < 3 -> load(B-uint8, I - 1, VB) ; VB = 0 ),
V is VB + 1,
store(O-uint8, I, V) )).
(A * A) < 1000000000, with an inlined uint32 product
kernel(O-bool, A-uint32) :-
forall(between(0, 1, I),
( load(A-uint32, I, VA0),
cast(uint64, VA0, VA),
P0 is VA*VA,
cast(uint64, P0, P),
( P < 1000000000 -> V = 1 ; V = 0 ),
store(O-bool, I, V) )).
The product is inlined: cast(uint64, P0, P) is its rounding to its own
format, the rounding of its fused/2. The load's cast to uint64 is the
compute format.
(A * 2.0).as(integer).max([1])
kernel(O-int8, A-float64) :-
forall(between(0, 1, I),
( aggregate_all(max(X),
( between(0, 1, J),
load(A-float64, I*2 + J, VA),
P is VA*2.0,
cast(int8, P, X) ),
M),
store(O-int8, I, M) )).
(A.sum([2]) * 2).sum([1])
kernel(O-uint8, A-uint8) :-
forall(between(0, 1, I),
( aggregate_all(sum(Y),
( between(0, 1, J),
aggregate_all(sum(X),
( between(0, 1, K),
load(A-uint8, I*4 + J*2 + K, X) ),
S0),
Y is S0*2 ),
S),
store(O-uint8, I, S) )).
rand
kernel(O-float64) :-
forall(between(0, 3, I),
( V is random_float,
store(O-float64, I, V) )).
random_float isn't pure: rules must never duplicate or remove it.
Nor may a rule move a goal across the iterations of an iterate: an
after/2 load depends on their order. The index rules only rewrite
arithmetic, which keeps it.
lower_kernel/5 lowers a fused schedule node:
forall over between(0, Dim-1, I) for each
dimension of the output;load, at the index index_expr composes from
its views, guarded by its validity condition when the views mask it;fused(Node, Expr) is Expr's goals, then a cast to Node's format;reindex(Via, Expr) computes Expr with the index of the node Via
reads;aggregate_all over between for each reduced axis;float and integer ops are casts;index_expr and node_index are called here only, and their results are
the raw index terms the optimizations simplify.
ir_optimize.pl adds the kernel clause to an e-graph of library(egraph) as
it is (head, goals, expressions and variables), saturates it, and extracts
the cheapest clause. Each device declares its rules with the multifile
ir_optimize:layers(Device, Layers): a layer is a list of module-qualified
rules saturated together, and layers run in order. A device without layers
isn't optimized. A kernel is optimized once: ir_optimize/3 is tabled by
variant, since the same kernel is lowered again by every realize computing
it.
library(egraph) doesn't count a property set on a class as a change, so a
saturation could stop before a rule reading that property fires. A layer is
therefore run one pass at a time until neither the nodes nor the properties
change, at most 32 passes.
Conventions for rules:
==, fd_inf/2...), without unifying it with a
value.fd property of
its operands, which only integers and index variables have. Values, whose
arithmetic may be inexact, are never touched.X = 0+X, and
associativity builds the classes 0+0, (0+0)+0... which nothing merges.'$NODE'(_): a bare
variable pattern matches none.random_float) or a
store.
ir_index.pl removes a // or a mod that the bounds of the index
variables make exact, which a C compiler can't do: the output indices are
parameters of the kernel function, of unknown range. Identities, constants
and strength reduction are left to the compiler. All devices use it, as a
single layer.
fd(V): V is a clpfd variable equal to the value of the class. A
binder between(L, H, I) gives the class of I the domain `L..H`, and
+, *, // and mod propagate it. Merging two classes unifies their
variables, which intersects their domains.divisor(S): S divides every value of the class.// and mod agree with C's /
and `%`):
add_assoc, add_comm: to find the multiple of K in a sum;div_split: (A+B)//K = A//K + B//K when K divides A;mod_split: (A+B) mod K = B mod K when K divides A;mod_small: A mod K = A when A < K.materialize: a class whose fd is a number holds that number, as a node
of cost 0.
A dimension of size 1 has an index known to be 0, so its binder is extracted
as between(0, 0, 0): the printers keep it as a dimension, with an unused
index parameter in C, and print no loop for it in a reduction.
What is left is linear arithmetic and divisions by multiples
((I*256)//256) that the compiler folds. Divisions that aren't affine stay,
for instance (F*2 + D)//8 with F in `0..7 and D in 0..1`.
Future rule sets:
( Cond -> Then ; Else ) into
Then;cast to the format a load already has is removed;forall in parallel/1.| IR | C | OpenCL | Prolog |
|---|---|---|---|
| head | kernel function and runtime predicate, typed from the pairs | __kernel function, typed from the pairs | the clause itself |
output forall | runtime loops calling the kernel function | get_global_id: a work item per index of its domain | forall |
between in a reduction | for loop | for loop | between |
load, store | `a1[Index]`, `a0[Index] = V` | same | arg/3, nb_setarg/3 |
iterate, a forall inside the domain | for loop, in order | for loop, in order in its work item | forall |
load(after(Out, K), …) | `a0[Index]` | same | arg/3 |
X is E | `T X = E; with T` the format of X | same | X is E |
cast(F, I, O) | `T O = (T)I;` (round from a float to an integer) | same | round_to_format or integer |
| conditional | if/else, or ?: for values | same | -> |
aggregate_all(sum(X), G, S) | `S = 0; for (…) S += X;` | same | aggregate_all |
simd(G) | `#pragma omp simd reduction(…)` | — | call |
parallel(G) | — | work items | call |
The examples above run as written, once asserted. With B = data(2, 3), the
padded read stores data(1, 3, 4, 1). With A = data(1, 2, 3, 4),
B = data(5, 6, 7, 8) and X = data(1, 1, 1, 1), both versions of the
matmul store data(20, 23, 44, 51).
ir_index.pl checks 50 random view
graphs).