Vector exp on SIMD lanes
mtl::simd::exp computes e^x on batch<float> and batch<double> lanes, and over an array. It removes the scalar std::exp per element from kernels such as softmax (issue #654). The first user is branes-ai/synapse: there, scalar std::exp was about a third of the attention kernel’s time.
#include <mtl/simd/math.hpp>
auto y = mtl::simd::exp(mtl::simd::batch<float>::load_unaligned(x)); // one batchmtl::simd::exp(x, out, n); // an array; x == out is allowedauto m = max(a, b); // lane-wise, found by ADLOne algorithm on every backend
Section titled “One algorithm on every backend”exp is written once, on batch operations and a few internal lane operations: round to nearest, 2^k from the exponent bits, and selects on a comparison. On the Highway backend and on the portable scalar backend, each lane therefore runs the same sequence of operations.
The two backends agree bit for bit wherever both fuse fma.
- Checked on an i7-12700K, over 1,074,578 inputs in
floatanddouble, between three builds: the scalar build at baseline x86-64 (softwarefma), the scalar build with-march=native, and the Highway AVX2 build. - Highway’s SSE4 target has no hardware FMA, so its
fmais a multiply and an add, and 491 of those results differed in the last bit. This is the positionfmaitself takes as “the single FMA decision point”.
Each lane is independent of the others. A result does not depend on its lane, on the batch width, or on how an array is split across threads or tiles. The array form sends its last n mod W elements through a batch padded with zeros, so every element takes the same per-lane path.
The algorithm
Section titled “The algorithm”The algorithm is Cephes’ expf and exp (S. Moshier):
- Clamp
xto[lo, hi], the range wheree^xis neither 0 nor infinite. Lanes outside it, and NaN lanes, are replaced at the end. - Reduce the argument.
k = round(x · log₂e), ties to even. Thenr = x − k ln 2, by Cody–Waite withln 2 = c₁ + c₂, wherec₁is short enough thatk c₁is exact. This gives|r| ≤ ln 2 / 2. - Approximate
e^r.- float:
1 + r + r² P(r), withPof degree 5. - double: the rational form
1 + 2 r P(r²) / (Q(r²) − r P(r²)).
- float:
- Scale by
2^kas2^k₁ · 2^k₂, withk₁ = round(k/2).- Both factors are normal numbers over the whole range.
2^128is not a float, and2^-149is not a normal one. - Each factor is built from the exponent bits.
- The product rounds once, in the last multiplication, which also rounds subnormal results correctly.
- Both factors are normal numbers over the whole range.
Accuracy
Section titled “Accuracy”Measured in ULPs of the result, or of the subnormal spacing for a subnormal result, against a reference in a wider type: double for float, long double for double. tests/unit/simd/test_exp.cpp asserts the bounds.
| lanes | bound | largest measured | sample |
|---|---|---|---|
float | < 1.5 ULP | 1.003 ULP | every 61st float bit pattern in range, and 20,000 around each boundary |
double | < 2 ULP | 1.66 ULP | 500,000 random values, and 20,000 around each boundary |
The results are not correctly rounded.
Special values
Section titled “Special values”| x | exp(x) |
|---|---|
+inf | +inf |
-inf | +0 |
| NaN | the input NaN |
x > hi (88.7228317 for float, 709.782712893384 for double) | +inf |
x < lo (−103.972076 for float, −745.133219101941 for double) | +0 |
lo ≤ x with e^x below the normal range | subnormal, not flushed |
hi is the largest x whose exponential rounds below infinity, and lo the smallest whose exponential rounds to a nonzero subnormal. Both were computed with mpmath at 300 bits.
These hold in IEEE arithmetic. Under -ffast-math (-ffinite-math-only) the compiler may assume that no NaN or infinity occurs, and the NaN and overflow selects are then not guaranteed.
Lane-wise min and max
Section titled “Lane-wise min and max”min(a, b) and max(a, b) follow std::min and std::max:
min(a, b)isbwhereb < a, andaotherwise.max(a, b)isbwherea < b, andaotherwise.
So a NaN in b gives a, and a NaN in a gives NaN.
The rule is written as a compare and a select, rather than the ISA’s min and max. On NaN those differ between targets: x86 returns the second operand, and NEON returns NaN. They are hidden friends of batch, so a call is unqualified and found by ADL. Softmax can use max to keep a running maximum per lane, instead of a horizontal reduce for each vector.
Measured on an i7-12700K P-core, with 16,384 floats in [−30, 10]:
| build | simd::exp | std::exp (glibc, scalar) |
|---|---|---|
| Highway AVX2, 8 lanes | 0.50 ns/element | 1.44 ns/element |
scalar fallback, -march=native | 2.8 ns/element | 1.43 ns/element |
The scalar fallback is slower than glibc’s expf. It is there to give the same results, not to be fast. A kernel that needs the speed builds with -DMTL5_WITH_HIGHWAY=ON.
A subnormal result is slow on x86. Producing one takes a microcode assist of about 150 cycles. Over the whole range [−104, 89], where 9% of the results are subnormal, the AVX2 time rises to 2.7 ns/element, against 2.3 for glibc.