softmax.c file
Functions
-
static void softmax_row(vm_
float_ t* dst, const vm_ float_ t* src, const int cols, const int used, const vm_ float_ t temperature) - One row.
-
auto vm_softmax(vm_
float_ t* out, const vm_ float_ t* x, int rows, int cols, vm_ float_ t temperature) -> bool - Softmax of each row.
-
auto vm_softmax_causal(vm_
float_ t* out, const vm_ float_ t* x, int rows, int cols, vm_ float_ t temperature) -> bool - Causal softmax of a square row-major matrix.
Function documentation
static void softmax_row(vm_ float_ t* dst,
const vm_ float_ t* src,
const int cols,
const int used,
const vm_ float_ t temperature)
One row.
used is the unmasked prefix; the tail is written as +0.
dst may exactly alias src. The max is read before any store.
bool vm_softmax(vm_ float_ t* out,
const vm_ float_ t* x,
int rows,
int cols,
vm_ float_ t temperature)
Softmax of each row.
| Parameters | |
|---|---|
| out | Output; length rows * cols. May exactly alias x. Unchanged on failure. |
| x | |
| rows | Row count. Must be positive. |
| cols | Softmax length. Must be positive. |
| temperature | Denominator of the logits. Must be greater than 0. |
| Returns | false when a pointer, a size, or the temperature is invalid. |
x and out are row-major, rows by cols, and cols is contiguous. Each row subtracts its max, divides by temperature, exponentiates with VECMAT_, and divides by the sum of those exponentials. Pass temperature 1 to leave the logits unscaled.
A null pointer, rows <= 0, cols <= 0, or a temperature that is not greater than 0 (zero, negative, or NaN) returns false and does not write out. out may exactly alias x. A partial overlap is caller error. No allocation.
On finite inputs whose shifted exponents and row sum stay finite, each output differs from the same steps in IEEE-754 double by less than 32 * cols * VECMAT_ absolute. The row sums to 1 within 16 * cols * VECMAT_ absolute.
bool vm_softmax_causal(vm_ float_ t* out,
const vm_ float_ t* x,
int rows,
int cols,
vm_ float_ t temperature)
Causal softmax of a square row-major matrix.
| Parameters | |
|---|---|
| out | Output; length rows * cols. May exactly alias x. Unchanged on failure. |
| x | |
| rows | Row count. Must equal cols and be positive. |
| cols | Column count. Must equal rows and be positive. |
| temperature | Denominator of the logits. Must be greater than 0. |
| Returns | false when a pointer, a size, the shape, or the temperature is invalid. |
rows must equal cols. On row i, column j > i is −∞ before the softmax: it does not enter the max, and it is written as +0. The unmasked prefix uses the same max, temperature scale, exp, and normalize as vm_.
A null pointer, a non-positive size, rows != cols, or a temperature that is not greater than 0 returns false and does not write out. out may exactly alias x. No allocation. The finite-input tolerance matches vm_.