Supported Primitives Guide#
This document contains details of supported primitives in Composable Kernel (CK). In contrast to the API Reference Guide, the Supported Primitives Guide is an introduction to the math which underpins the algorithms implemented in CK.
Softmax#
For vectors \(x^{(1)}, x^{(2)}, \ldots, x^{(T)}\) of size \(B\) you can decompose the softmax of concatenated \(x = [ x^{(1)}\ | \ \ldots \ | \ x^{(T)} ]\) as,
where \(f(x^{(j)}) = \exp( x^{(j)} - m(x^{(j)}) )\) is of size \(B\) and \(z(x^{(j)}) = f(x_1^{(j)})+ \ldots+ f(x_B^{(j)})\) is a scalar.
For a matrix \(X\) composed of \(T_r \times T_c\) tiles, \(X_{ij}\), of size \(B_r \times B_c\) you can compute the row-wise softmax as follows.
For \(j\) from \(1\) to \(T_c\), and \(i\) from \(1\) to \(T_r\) calculate,
If \(j=1\), initialize running max, running sum, and the first column block of the output,
Else if \(j>1\),
Update running max, running sum and column blocks \(k=1\) to \(k=j-1\)
Initialize column block \(j\) of output and reset running max and running sum variables: