strata-vision own CPU tower: the SSE4.1 tier takes its exp from the C library (the scalar tier's), so its output equals the scalar tier's byte for byte

The SSE4.1 tier used a Cephes polynomial for exp. On Qwen3.8 that gave code4217 a cosine of 0.998928 against llama.cpp's CPU reader, 0.00007 under the 0.999
bar. The tier now calls the C library's expf per lane (out of line, with the scalar tier's clamp [-87, 88]). No build switch is left behind and the polynomial
is gone from this tier.

Checks (p2006, Zen 4, 8 threads, 18 pictures, 1,024 tokens, forced sse41 tier; the AVX and AVX2 tiers are unchanged):

| | polynomial exp | libm exp |
|---|---|---|
| Qwen3.8 rule 1 minimum (against llama.cpp's portable AVX2 CPU reader) | 0.998928 (code4217, 1 miss) | 0.999029 (code4217, 0 misses) |
| Qwen3.6 rule 1 minimum | 0.999975 | 0.999975 |
| bytes against the scalar tier, 18 pictures, each model | all 18 differ | 18 of 18 identical |
| big1024 time, Qwen3.8 / Qwen3.6, 3 alternating pairs, median | 23,857 / 22,523 ms | 26,935 / 24,398 ms |
| shapes (260 tokens) time, Qwen3.8 / Qwen3.6 | 3,587 / 3,460 ms | 3,653 / 3,548 ms |
| peak resident memory | 1.16-1.18 GB | 1.16-1.18 GB |

Time per pair over libm, big1024: 0.82 to 0.98 (Qwen3.8), 0.90 to 0.96 (Qwen3.6); run-to-run spread on big1024 is about 10 percent.

The same code under qemu-user with the Westmere CPU model (no AVX, FMA or F16C, so the C library takes its non-FMA expf), z440, Qwen3.8, code4217, 12 threads:
the emulated sse41 output is byte-identical to the native sse41 output (and so to the native scalar output), rule 1 is 0.999029 in both.

Against llama.cpp's reader built for the same CPU class (p2006, 8 threads, medians; big1024 / shapes): sse41 against the SSE4.2 build, Qwen3.8 25,219 / 3,541 ms
against 150,800 / 37,149 ms, Qwen3.6 25,304 / 3,531 ms against 123,306 / 29,265 ms.
This commit is contained in:
Niko1221 committed 2026-11-11 21:38:56 +01:00
1 parent 0991006d2c
commit 117473f643
1 file changed
+7 -15
+7 -15
View File
@@ -4,7 +4,7 @@
//
// No FMA on these CPUs: a product term is a multiply then an add (two roundings), in the same k order as every tier, so the products are bit-identical to
// the scalar tier's on x86 (whose struct V also multiplies then adds) and differ from the AVX2 tier's FMA by the second rounding (within rule 1).
// F16 weights are converted without F16C (bit exact, integer code), exp is the Cephes polynomial of the AVX2 tier with separate multiply and add.
// F16 weights are converted without F16C (bit exact, integer code), exp is the C library's expf per lane, as in the scalar tier.
#include "cpu_tier.h" // only this and the intrinsics headers: no standard-library code may be compiled with this file's ISA flags (cpu_tier.h)
#if defined(__x86_64__) || defined(_M_X64)
@@ -68,21 +68,13 @@ inline void micro(const float* A, size_t lda, const float* B, size_t ldb, int K,
for (int c = 0; c < 2; ++c) _mm_storeu_ps(C + (size_t) r * ldc + c * 4, acc[r][c]);
}
// expf (Cephes, the AVX2 tier's constants), multiply and add separately
// expf of the C library per lane (an out-of-line call per element), with the scalar tier's clamp [-87, 88]: the scalar tier's exp, so this tier's output is
// byte-identical to the scalar tier's (a polynomial exp here gave Qwen3.8 code4217 a cosine of 0.998928 against llama.cpp's reader, under the 0.999 bar)
inline VR vr_exp(VR xv) {
__m128 x = _mm_min_ps(_mm_max_ps(xv, _mm_set1_ps(-87.3f)), _mm_set1_ps(88.3f));
const __m128 fx = _mm_round_ps(_mm_mul_ps(x, _mm_set1_ps(1.44269504088896341f)), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
x = _mm_sub_ps(x, _mm_mul_ps(fx, _mm_set1_ps(0.693359375f)));
x = _mm_sub_ps(x, _mm_mul_ps(fx, _mm_set1_ps(-2.12194440e-4f)));
__m128 y = _mm_set1_ps(1.9875691500e-4f);
y = _mm_add_ps(_mm_mul_ps(y, x), _mm_set1_ps(1.3981999507e-3f));
y = _mm_add_ps(_mm_mul_ps(y, x), _mm_set1_ps(8.3334519073e-3f));
y = _mm_add_ps(_mm_mul_ps(y, x), _mm_set1_ps(4.1665795894e-2f));
y = _mm_add_ps(_mm_mul_ps(y, x), _mm_set1_ps(1.6666665459e-1f));
y = _mm_add_ps(_mm_mul_ps(y, x), _mm_set1_ps(5.0000001201e-1f));
y = _mm_add_ps(_mm_mul_ps(y, _mm_mul_ps(x, x)), _mm_add_ps(x, _mm_set1_ps(1.0f)));
const __m128i e = _mm_slli_epi32(_mm_add_epi32(_mm_cvtps_epi32(fx), _mm_set1_epi32(127)), 23);
return _mm_mul_ps(y, _mm_castsi128_ps(e));
alignas(16) float t[4];
_mm_store_ps(t, xv);
for (int i = 0; i < 4; ++i) t[i] = cpu_tier_expf(t[i] < -87.0f ? -87.0f : t[i] > 88.0f ? 88.0f : t[i]);
return _mm_load_ps(t);
}
// 4 floats rounded to the 16-bit type and back, bit for bit as the scalar tier's round_one: BF16 round to nearest even (a NaN stays), F16 round to nearest