strata-vision own CPU tower: SSE4.1 and AVX kernel tiers for x86 CPUs without AVX2

Two new tiers on the CpuOps contract (cpu_tier.h: no standard-library code in the tier files), for the CPUs below AVX2 that setup builds for
(STRATA_ISA_FLOOR none / avx): Nehalem / Westmere and the Pentium / Celeron / Atom parts without AVX (sse41), Sandy / Ivy Bridge and AMD's AVX
CPUs before Excavator (avx).

- cpu_kernels_sse41.cpp: 6 x 8 register tile on __m128; cpu_kernels_avx.cpp: 6 x 16 on __m256 (-msse4.1 / -mavx; MSVC /arch:AVX).
- No FMA on these CPUs: every product term is a multiply then an add, in the k order of every tier. The SSE4.1 tier's products are bit-identical
  to the scalar tier's on x86 (unit test).
- F16 weights are converted in integer code, bit exact, without F16C (Sandy Bridge has none); the 16-bit rounding equals the scalar tier's bit
  for bit; exp is the Cephes polynomial of the AVX2 tier with separate multiply and add; the one-vector tail product as in AVX2 / AVX-512.
- Dispatch: Isa ranks scalar < sse41 < avx < avx2 < avx512 (Neon apart); the probe reads SSE4.1 and AVX (with the OS ymm state);
  STRATA_FORCE_ISA takes sse41 and avx.
- own_reader_supported keeps `auto` on mtmd for every x86 tier below AVX2 (the line reads `< Avx2` instead of `== Scalar`): behaviour unchanged
  until the gate.
- strata-vision-own-cpu-test runs every tier the CPU has, capped by STRATA_FORCE_ISA, and adds the rotary / position-add kernels and the SSE4.1 =
  scalar product check.

Checked: cpu-test on z440 (Haswell-EP) with each tier forced (scalar, sse41, avx, avx2): all passed; under qemu-user -cpu Westmere the cpu-test
picks sse41 and passes, and strata-vision --tower own / mtmd / auto and strata-vision-own read Qwen3.8 and Qwen3.6 pictures with no illegal
instruction (z440 job z440-20261111-1105-4195).
This commit is contained in:
Niko1221 committed 2026-11-11 12:29:51 +01:00
1 parent 0c83610e5a
commit 0991006d2c
8 files changed
+442 -13

No files matched your search

+5 -2
View File
@@ -72,7 +72,7 @@ endif()
set(STB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../third_party/stb" CACHE PATH "stb_image.h (third_party/stb)")
add_library(strata_vit_own STATIC gguf_lite.cpp vit_geometry.cpp image_prep.cpp vit_ref.cpp own_reader.cpp
vit_cpu.cpp cpu_ops.cpp cpu_kernels_scalar.cpp cpu_kernels_avx2.cpp cpu_kernels_avx512.cpp cpu_kernels_neon.cpp)
vit_cpu.cpp cpu_ops.cpp cpu_kernels_scalar.cpp cpu_kernels_sse41.cpp cpu_kernels_avx.cpp cpu_kernels_avx2.cpp cpu_kernels_avx512.cpp cpu_kernels_neon.cpp)
find_package(Threads REQUIRED)
target_link_libraries(strata_vit_own PUBLIC Threads::Threads)
if(NOT MSVC)
@@ -102,12 +102,15 @@ if(NOT MSVC)
# the CPU tower: the generic part (vit_cpu.cpp) keeps the baseline, each kernel tier is compiled for its own instruction set and only called when the CPU
# has it (cpu_caps.h); the tiers' files include no standard-library code (cpu_tier.h)
if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64")
set_source_files_properties(cpu_kernels_sse41.cpp PROPERTIES COMPILE_OPTIONS "-O3;-msse4.1")
set_source_files_properties(cpu_kernels_avx.cpp PROPERTIES COMPILE_OPTIONS "-O3;-mavx")
set_source_files_properties(cpu_kernels_avx2.cpp PROPERTIES COMPILE_OPTIONS "-O3;-mavx2;-mfma;-mf16c")
set_source_files_properties(cpu_kernels_avx512.cpp PROPERTIES COMPILE_OPTIONS "-O3;-mavx2;-mfma;-mf16c;-mavx512f;-mavx512bw;-mavx512dq;-mavx512vl")
else()
set_source_files_properties(cpu_kernels_avx2.cpp cpu_kernels_avx512.cpp PROPERTIES COMPILE_OPTIONS "-O3")
set_source_files_properties(cpu_kernels_sse41.cpp cpu_kernels_avx.cpp cpu_kernels_avx2.cpp cpu_kernels_avx512.cpp PROPERTIES COMPILE_OPTIONS "-O3")
endif()
else()
set_source_files_properties(cpu_kernels_avx.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX") # (the SSE4.1 tier needs no flag on x64)
set_source_files_properties(cpu_kernels_avx2.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX2")
set_source_files_properties(cpu_kernels_avx512.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX512")
endif()
+16 -6
View File
@@ -18,9 +18,11 @@
namespace strata_vit {
namespace quant {
enum class Isa { Scalar = 0, Avx2 = 1, Avx512 = 2, Neon = 3 }; // Neon: AArch64 only (baseline Advanced SIMD); the x86 tiers are 0 .. 2
// ranked: an x86 tier includes every lower one (STRATA_FORCE_ISA caps by rank). Neon: AArch64 only (baseline Advanced SIMD), outside the x86 ranking.
enum class Isa { Scalar = 0, Sse41 = 1, Avx = 2, Avx2 = 3, Avx512 = 4, Neon = 5 };
struct Caps {
bool sse41 = false, avx = false; // avx: the CPU has it and the OS saves the ymm state
bool avx2 = false, fma = false, f16c = false, avx512 = false; // avx512: F, BW, DQ, VL (what the tower's kernels use) and the OS saves the state
};
@@ -33,9 +35,11 @@ inline Caps probe() {
const int n = r[0];
if (n < 7) return c;
__cpuid(r, 1);
const bool osxsave = (r[2] >> 27) & 1, fma = (r[2] >> 12) & 1, f16c = (r[2] >> 29) & 1;
const bool osxsave = (r[2] >> 27) & 1, fma = (r[2] >> 12) & 1, f16c = (r[2] >> 29) & 1, avx = (r[2] >> 28) & 1;
c.sse41 = (r[2] >> 19) & 1;
unsigned long long xcr = osxsave ? _xgetbv(0) : 0;
const bool ymm = (xcr & 6) == 6, zmm = (xcr & 0xe6) == 0xe6;
c.avx = ymm && avx;
__cpuidex(r, 7, 0);
c.avx2 = ymm && ((r[1] >> 5) & 1);
c.fma = ymm && fma;
@@ -47,6 +51,8 @@ inline Caps probe() {
inline Caps probe() {
Caps c;
__builtin_cpu_init();
c.sse41 = __builtin_cpu_supports("sse4.1");
c.avx = __builtin_cpu_supports("avx"); // libgcc tests the OS ymm state for it
c.avx2 = __builtin_cpu_supports("avx2");
c.fma = __builtin_cpu_supports("fma");
c.f16c = __builtin_cpu_supports("f16c");
@@ -67,6 +73,8 @@ inline bool isa_ok(Isa i) {
const Caps& c = caps();
if (i == Isa::Avx512) return c.avx512 && c.avx2 && c.fma;
if (i == Isa::Avx2) return c.avx2 && c.fma && c.f16c;
if (i == Isa::Avx) return c.avx && c.sse41;
if (i == Isa::Sse41) return c.sse41;
#if defined(__aarch64__) || defined(_M_ARM64)
if (i == Isa::Neon) return true; // part of every AArch64 CPU
#else
@@ -75,23 +83,25 @@ inline bool isa_ok(Isa i) {
return true;
}
// the best tier this CPU runs, capped by STRATA_FORCE_ISA=scalar|avx2|avx512 (on AArch64: neon, or scalar)
// the best tier this CPU runs, capped by STRATA_FORCE_ISA=scalar|sse41|avx|avx2|avx512 (on AArch64: neon, or scalar)
inline Isa best_isa() {
#if defined(__aarch64__) || defined(_M_ARM64)
if (const char* e = std::getenv("STRATA_FORCE_ISA"))
if (std::string(e) == "scalar") return Isa::Scalar;
return Isa::Neon;
#endif
Isa best = isa_ok(Isa::Avx512) ? Isa::Avx512 : isa_ok(Isa::Avx2) ? Isa::Avx2 : Isa::Scalar;
Isa best = isa_ok(Isa::Avx512) ? Isa::Avx512 : isa_ok(Isa::Avx2) ? Isa::Avx2 : isa_ok(Isa::Avx) ? Isa::Avx : isa_ok(Isa::Sse41) ? Isa::Sse41 : Isa::Scalar;
if (const char* e = std::getenv("STRATA_FORCE_ISA")) {
const std::string s(e);
const Isa cap = s == "scalar" ? Isa::Scalar : s == "avx2" ? Isa::Avx2 : Isa::Avx512;
const Isa cap = s == "scalar" ? Isa::Scalar : s == "sse41" ? Isa::Sse41 : s == "avx" ? Isa::Avx : s == "avx2" ? Isa::Avx2 : Isa::Avx512;
if ((int) cap < (int) best) best = cap;
}
return best;
}
inline const char* isa_name(Isa i) { return i == Isa::Neon ? "neon" : i == Isa::Avx512 ? "avx512" : i == Isa::Avx2 ? "avx2" : "scalar"; }
inline const char* isa_name(Isa i) {
return i == Isa::Neon ? "neon" : i == Isa::Avx512 ? "avx512" : i == Isa::Avx2 ? "avx2" : i == Isa::Avx ? "avx" : i == Isa::Sse41 ? "sse41" : "scalar";
}
// ---- 16-bit floats (bit exact, no hardware needed) ----
inline float bf16_to_f32(uint16_t h) {
+174
View File
@@ -0,0 +1,174 @@
// tools/vision/own/cpu_kernels_avx.cpp - the AVX tier without FMA, F16C or AVX2 (6 x 16 register tile: 12 ymm accumulators), for Sandy Bridge / Ivy
// Bridge and AMD's AVX CPUs before Excavator. Compiled with -mavx (/arch:AVX on MSVC); only called when quant::isa_ok(Avx).
//
// As the SSE4.1 tier: a product term is a multiply then an add in the same k order as every tier (no FMA), F16 weights are converted in integer code
// (bit exact, no F16C: Sandy Bridge has none), exp is the Cephes polynomial with separate multiply and add. AVX has no 256-bit integer operations, so the
// integer steps (exponent bits, conversions, rounding) run on 128-bit halves.
#include "cpu_tier.h" // only this and <immintrin.h>: no standard-library code may be compiled with this file's ISA flags (cpu_tier.h)
#if defined(__x86_64__) || defined(_M_X64)
#include <immintrin.h>
namespace strata_vit {
namespace {
namespace kn {
template <class T> inline T kmin(T a, T b) { return b < a ? b : a; }
template <class T> inline T kmax(T a, T b) { return a < b ? b : a; }
inline void kcopy(float* d, const float* s, int n) { for (int i = 0; i < n; ++i) d[i] = s[i]; }
inline void kzero(float* d, int n) { for (int i = 0; i < n; ++i) d[i] = 0.0f; }
#define STRATA_KN_RAW 1
constexpr int VW = 8;
typedef __m256 VR;
inline VR vr_load(const float* p) { return _mm256_loadu_ps(p); }
inline void vr_store(float* p, VR v) { _mm256_storeu_ps(p, v); }
inline VR vr_set1(float f) { return _mm256_set1_ps(f); }
inline VR vr_zero() { return _mm256_setzero_ps(); }
inline VR vr_add(VR a, VR b) { return _mm256_add_ps(a, b); }
inline VR vr_sub(VR a, VR b) { return _mm256_sub_ps(a, b); }
inline VR vr_mul(VR a, VR b) { return _mm256_mul_ps(a, b); }
inline VR vr_div(VR a, VR b) { return _mm256_div_ps(a, b); }
inline VR vr_fma(VR a, VR b, VR c) { return _mm256_add_ps(_mm256_mul_ps(a, b), c); } // two roundings (no FMA here)
inline VR vr_max(VR a, VR b) { return _mm256_max_ps(a, b); }
inline float vr_sqrt1(float x) { return _mm_cvtss_f32(_mm_sqrt_ss(_mm_set_ss(x))); }
inline float vr_hadd(VR a) { // the AVX2 tier's order
__m128 s = _mm_add_ps(_mm256_castps256_ps128(a), _mm256_extractf128_ps(a, 1));
s = _mm_add_ps(s, _mm_movehl_ps(s, s));
s = _mm_add_ss(s, _mm_shuffle_ps(s, s, 1));
return _mm_cvtss_f32(s);
}
inline float vr_hmax(VR a) {
__m128 s = _mm_max_ps(_mm256_castps256_ps128(a), _mm256_extractf128_ps(a, 1));
s = _mm_max_ps(s, _mm_movehl_ps(s, s));
s = _mm_max_ss(s, _mm_shuffle_ps(s, s, 1));
return _mm_cvtss_f32(s);
}
#define STRATA_KN_HAS_MICRO 1
template <int MR>
inline void micro(const float* A, size_t lda, const float* B, size_t ldb, int K, float* C, size_t ldc, int init, const float* bias) {
__m256 acc[MR][2];
for (int r = 0; r < MR; ++r)
for (int c = 0; c < 2; ++c) {
__m256 v = _mm256_setzero_ps();
if (init != 0) v = _mm256_loadu_ps(C + (size_t) r * ldc + c * 8);
if (init != 1 && bias) v = _mm256_add_ps(v, _mm256_loadu_ps(bias + c * 8));
acc[r][c] = v;
}
for (int k = 0; k < K; ++k) {
const __m256 b0 = _mm256_loadu_ps(B + (size_t) k * ldb), b1 = _mm256_loadu_ps(B + (size_t) k * ldb + 8);
for (int r = 0; r < MR; ++r) {
const __m256 a = _mm256_set1_ps(A[(size_t) r * lda + k]);
acc[r][0] = _mm256_add_ps(_mm256_mul_ps(a, b0), acc[r][0]);
acc[r][1] = _mm256_add_ps(_mm256_mul_ps(a, b1), acc[r][1]);
}
}
for (int r = 0; r < MR; ++r)
for (int c = 0; c < 2; ++c) _mm256_storeu_ps(C + (size_t) r * ldc + c * 8, acc[r][c]);
}
inline __m256 combine(__m128 lo, __m128 hi) { return _mm256_insertf128_ps(_mm256_castps128_ps256(lo), hi, 1); }
// expf (Cephes, the AVX2 tier's constants), multiply and add separately; 2^n built on the two 128-bit halves
inline VR vr_exp(VR xv) {
__m256 x = _mm256_min_ps(_mm256_max_ps(xv, _mm256_set1_ps(-87.3f)), _mm256_set1_ps(88.3f));
const __m256 fx = _mm256_round_ps(_mm256_mul_ps(x, _mm256_set1_ps(1.44269504088896341f)), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
x = _mm256_sub_ps(x, _mm256_mul_ps(fx, _mm256_set1_ps(0.693359375f)));
x = _mm256_sub_ps(x, _mm256_mul_ps(fx, _mm256_set1_ps(-2.12194440e-4f)));
__m256 y = _mm256_set1_ps(1.9875691500e-4f);
y = _mm256_add_ps(_mm256_mul_ps(y, x), _mm256_set1_ps(1.3981999507e-3f));
y = _mm256_add_ps(_mm256_mul_ps(y, x), _mm256_set1_ps(8.3334519073e-3f));
y = _mm256_add_ps(_mm256_mul_ps(y, x), _mm256_set1_ps(4.1665795894e-2f));
y = _mm256_add_ps(_mm256_mul_ps(y, x), _mm256_set1_ps(1.6666665459e-1f));
y = _mm256_add_ps(_mm256_mul_ps(y, x), _mm256_set1_ps(5.0000001201e-1f));
y = _mm256_add_ps(_mm256_mul_ps(y, _mm256_mul_ps(x, x)), _mm256_add_ps(x, _mm256_set1_ps(1.0f)));
const __m256i n = _mm256_cvtps_epi32(fx);
const __m128i b = _mm_set1_epi32(127);
const __m128i elo = _mm_slli_epi32(_mm_add_epi32(_mm256_castsi256_si128(n), b), 23);
const __m128i ehi = _mm_slli_epi32(_mm_add_epi32(_mm256_extractf128_si256(n, 1), b), 23);
return _mm256_mul_ps(y, combine(_mm_castsi128_ps(elo), _mm_castsi128_ps(ehi)));
}
// rounding to 16 bits, bit for bit as the scalar tier (see cpu_kernels_sse41.cpp for the method), on 128-bit halves
inline __m128 round4(__m128 x, int kind) {
const __m128i u = _mm_castps_si128(x);
const __m128i absu = _mm_and_si128(u, _mm_set1_epi32(0x7fffffff));
if (kind == 1) {
const __m128i lsb = _mm_and_si128(_mm_srli_epi32(u, 16), _mm_set1_epi32(1));
const __m128i r = _mm_and_si128(_mm_add_epi32(u, _mm_add_epi32(_mm_set1_epi32(0x7fff), lsb)), _mm_set1_epi32((int) 0xffff0000u));
const __m128i nan = _mm_cmpgt_epi32(absu, _mm_set1_epi32(0x7f800000));
return _mm_castsi128_ps(_mm_blendv_epi8(r, u, nan));
}
const __m128 a = _mm_castsi128_ps(absu);
const __m128i sign = _mm_and_si128(u, _mm_set1_epi32((int) 0x80000000u));
const __m128i lsb = _mm_and_si128(_mm_srli_epi32(absu, 13), _mm_set1_epi32(1));
const __m128i nrm = _mm_and_si128(_mm_add_epi32(absu, _mm_add_epi32(_mm_set1_epi32(0xfff), lsb)), _mm_set1_epi32((int) 0xffffe000u));
const __m128 sub = _mm_sub_ps(_mm_add_ps(a, _mm_set1_ps(0.5f)), _mm_set1_ps(0.5f));
const __m128 is_sub = _mm_cmplt_ps(a, _mm_set1_ps(6.103515625e-05f));
__m128 q = _mm_blendv_ps(_mm_castsi128_ps(nrm), sub, is_sub);
q = _mm_or_ps(q, _mm_castsi128_ps(sign));
const __m128 keep = _mm_cmplt_ps(a, _mm_set1_ps(65520.0f));
return _mm_blendv_ps(x, q, keep);
}
inline void round_buf(float* p, size_t n, int kind) {
size_t i = 0;
for (; i + 4 <= n; i += 4) _mm_storeu_ps(p + i, round4(_mm_loadu_ps(p + i), kind));
if (i < n) {
float t[4] = {0};
for (size_t j = i; j < n; ++j) t[j - i] = p[j];
_mm_storeu_ps(t, round4(_mm_loadu_ps(t), kind));
for (size_t j = i; j < n; ++j) p[j] = t[j - i];
}
}
inline __m128 h2f4(__m128i h) {
const __m128i em = _mm_and_si128(h, _mm_set1_epi32(0x7fff));
const __m128i sign = _mm_slli_epi32(_mm_and_si128(h, _mm_set1_epi32(0x8000)), 16);
const __m128i sh = _mm_slli_epi32(em, 13);
const __m128 fin = _mm_mul_ps(_mm_castsi128_ps(sh), _mm_castsi128_ps(_mm_set1_epi32(0x77800000)));
const __m128i inf = _mm_or_si128(sh, _mm_set1_epi32(0x7f800000));
const __m128i big = _mm_cmpgt_epi32(em, _mm_set1_epi32(0x7bff));
const __m128i r = _mm_blendv_epi8(_mm_castps_si128(fin), inf, big);
return _mm_castsi128_ps(_mm_or_si128(r, sign));
}
inline float f16_one(uint16_t h) { return _mm_cvtss_f32(h2f4(_mm_cvtsi32_si128((int) h))); }
inline float bf16_one(uint16_t h) { return _mm_cvtss_f32(_mm_castsi128_ps(_mm_cvtsi32_si128((int) ((uint32_t) h << 16)))); }
inline void conv16(const void* Wm, int kind, size_t off, int n, float* out) {
const uint16_t* p = (const uint16_t*) Wm + off;
int i = 0;
if (kind == 2) {
for (; i + 8 <= n; i += 8) {
const __m128i w = _mm_loadu_si128((const __m128i*) (p + i));
_mm256_storeu_ps(out + i, combine(h2f4(_mm_cvtepu16_epi32(w)), h2f4(_mm_cvtepu16_epi32(_mm_srli_si128(w, 8)))));
}
for (; i < n; ++i) out[i] = f16_one(p[i]);
} else {
const __m128i z = _mm_setzero_si128();
for (; i + 8 <= n; i += 8) {
const __m128i w = _mm_loadu_si128((const __m128i*) (p + i));
_mm256_storeu_ps(out + i, combine(_mm_castsi128_ps(_mm_unpacklo_epi16(z, w)), _mm_castsi128_ps(_mm_unpackhi_epi16(z, w))));
}
for (; i < n; ++i) out[i] = bf16_one(p[i]);
}
}
} // namespace kn
} // namespace
} // namespace strata_vit
#define STRATA_KN_MR 6
#define KN_MIN(a, b) kmin(a, b)
#define KN_MAX(a, b) kmax(a, b)
#define KN_COPY(d, s, n) kcopy((d), (s), (n))
#define KN_ZERO(d, n) kzero((d), (n))
#define STRATA_KN_NAME "avx"
#define STRATA_KN_GETTER cpu_ops_avx
#include "cpu_kernels_impl.h"
#else
namespace strata_vit {
const CpuOps& cpu_ops_avx() { return cpu_ops_scalar(); }
}
#endif
+176
View File
@@ -0,0 +1,176 @@
// tools/vision/own/cpu_kernels_sse41.cpp - the SSE4.1 tier (6 x 8 register tile: 12 accumulators of the 16 xmm registers), for x86 CPUs without AVX
// (Nehalem / Westmere, the Pentium and Celeron parts without AVX, Silvermont / Goldmont). Compiled with -msse4.1 (MSVC x64: no flag needed); only called
// when quant::isa_ok(Sse41).
//
// 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.
#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)
#include <emmintrin.h>
#include <smmintrin.h>
namespace strata_vit {
namespace {
namespace kn {
template <class T> inline T kmin(T a, T b) { return b < a ? b : a; }
template <class T> inline T kmax(T a, T b) { return a < b ? b : a; }
inline void kcopy(float* d, const float* s, int n) { for (int i = 0; i < n; ++i) d[i] = s[i]; }
inline void kzero(float* d, int n) { for (int i = 0; i < n; ++i) d[i] = 0.0f; }
#define STRATA_KN_RAW 1
constexpr int VW = 4;
typedef __m128 VR;
inline VR vr_load(const float* p) { return _mm_loadu_ps(p); }
inline void vr_store(float* p, VR v) { _mm_storeu_ps(p, v); }
inline VR vr_set1(float f) { return _mm_set1_ps(f); }
inline VR vr_zero() { return _mm_setzero_ps(); }
inline VR vr_add(VR a, VR b) { return _mm_add_ps(a, b); }
inline VR vr_sub(VR a, VR b) { return _mm_sub_ps(a, b); }
inline VR vr_mul(VR a, VR b) { return _mm_mul_ps(a, b); }
inline VR vr_div(VR a, VR b) { return _mm_div_ps(a, b); }
inline VR vr_fma(VR a, VR b, VR c) { return _mm_add_ps(_mm_mul_ps(a, b), c); } // a*b + c with two roundings (no FMA on these CPUs)
inline VR vr_max(VR a, VR b) { return _mm_max_ps(a, b); }
inline float vr_sqrt1(float x) { return _mm_cvtss_f32(_mm_sqrt_ss(_mm_set_ss(x))); }
// (x0 + x2) + (x1 + x3): the scalar tier's order
inline float vr_hadd(VR a) {
const __m128 s = _mm_add_ps(a, _mm_movehl_ps(a, a));
return _mm_cvtss_f32(_mm_add_ss(s, _mm_shuffle_ps(s, s, 1)));
}
inline float vr_hmax(VR a) {
const __m128 s = _mm_max_ps(a, _mm_movehl_ps(a, a));
return _mm_cvtss_f32(_mm_max_ss(s, _mm_shuffle_ps(s, s, 1)));
}
// The product's micro-kernel: the generic micro of cpu_kernels_impl.h on raw __m128, multiply then add.
#define STRATA_KN_HAS_MICRO 1
template <int MR>
inline void micro(const float* A, size_t lda, const float* B, size_t ldb, int K, float* C, size_t ldc, int init, const float* bias) {
__m128 acc[MR][2];
for (int r = 0; r < MR; ++r)
for (int c = 0; c < 2; ++c) {
__m128 v = _mm_setzero_ps();
if (init != 0) v = _mm_loadu_ps(C + (size_t) r * ldc + c * 4);
if (init != 1 && bias) v = _mm_add_ps(v, _mm_loadu_ps(bias + c * 4));
acc[r][c] = v;
}
for (int k = 0; k < K; ++k) {
const __m128 b0 = _mm_loadu_ps(B + (size_t) k * ldb), b1 = _mm_loadu_ps(B + (size_t) k * ldb + 4);
for (int r = 0; r < MR; ++r) {
const __m128 a = _mm_set1_ps(A[(size_t) r * lda + k]);
acc[r][0] = _mm_add_ps(_mm_mul_ps(a, b0), acc[r][0]);
acc[r][1] = _mm_add_ps(_mm_mul_ps(a, b1), acc[r][1]);
}
}
for (int r = 0; r < MR; ++r)
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
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));
}
// 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
// even to 11 significant bits (|x| >= 65520, inf and NaN stay as they are, as in the scalar tier), F16 subnormals to multiples of 2^-24
inline __m128 round4(__m128 x, int kind) {
const __m128i u = _mm_castps_si128(x);
const __m128i absu = _mm_and_si128(u, _mm_set1_epi32(0x7fffffff));
if (kind == 1) {
const __m128i lsb = _mm_and_si128(_mm_srli_epi32(u, 16), _mm_set1_epi32(1));
const __m128i r = _mm_and_si128(_mm_add_epi32(u, _mm_add_epi32(_mm_set1_epi32(0x7fff), lsb)), _mm_set1_epi32((int) 0xffff0000u));
const __m128i nan = _mm_cmpgt_epi32(absu, _mm_set1_epi32(0x7f800000));
return _mm_castsi128_ps(_mm_blendv_epi8(r, u, nan));
}
const __m128 a = _mm_castsi128_ps(absu);
const __m128i sign = _mm_and_si128(u, _mm_set1_epi32((int) 0x80000000u));
// normal: drop 13 mantissa bits, round to nearest even
const __m128i lsb = _mm_and_si128(_mm_srli_epi32(absu, 13), _mm_set1_epi32(1));
const __m128i nrm = _mm_and_si128(_mm_add_epi32(absu, _mm_add_epi32(_mm_set1_epi32(0xfff), lsb)), _mm_set1_epi32((int) 0xffffe000u));
// subnormal: (a + 0.5) - 0.5 rounds a to a multiple of 2^-24 (the ulp of 0.5), ties to even, exactly
const __m128 sub = _mm_sub_ps(_mm_add_ps(a, _mm_set1_ps(0.5f)), _mm_set1_ps(0.5f));
const __m128 is_sub = _mm_cmplt_ps(a, _mm_set1_ps(6.103515625e-05f));
__m128 q = _mm_blendv_ps(_mm_castsi128_ps(nrm), sub, is_sub);
q = _mm_or_ps(q, _mm_castsi128_ps(sign));
const __m128 keep = _mm_cmplt_ps(a, _mm_set1_ps(65520.0f)); // false for NaN and |x| >= 65520: those stay
return _mm_blendv_ps(x, q, keep);
}
inline void round_buf(float* p, size_t n, int kind) {
size_t i = 0;
for (; i + 4 <= n; i += 4) _mm_storeu_ps(p + i, round4(_mm_loadu_ps(p + i), kind));
if (i < n) {
float t[4] = {0};
for (size_t j = i; j < n; ++j) t[j - i] = p[j];
_mm_storeu_ps(t, round4(_mm_loadu_ps(t), kind));
for (size_t j = i; j < n; ++j) p[j] = t[j - i];
}
}
// 4 halves (zero-extended to 32 bits) to float, bit exact without F16C: (exponent and mantissa << 13) as a float times 2^112 for normal and subnormal
// values (an exact scaling), inf / NaN by bits; the sign put back
inline __m128 h2f4(__m128i h) {
const __m128i em = _mm_and_si128(h, _mm_set1_epi32(0x7fff));
const __m128i sign = _mm_slli_epi32(_mm_and_si128(h, _mm_set1_epi32(0x8000)), 16);
const __m128i sh = _mm_slli_epi32(em, 13);
const __m128 fin = _mm_mul_ps(_mm_castsi128_ps(sh), _mm_castsi128_ps(_mm_set1_epi32(0x77800000))); // * 2^112
const __m128i inf = _mm_or_si128(sh, _mm_set1_epi32(0x7f800000));
const __m128i big = _mm_cmpgt_epi32(em, _mm_set1_epi32(0x7bff));
const __m128i r = _mm_blendv_epi8(_mm_castps_si128(fin), inf, big);
return _mm_castsi128_ps(_mm_or_si128(r, sign));
}
inline float f16_one(uint16_t h) { return _mm_cvtss_f32(h2f4(_mm_cvtsi32_si128((int) h))); }
inline float bf16_one(uint16_t h) { return _mm_cvtss_f32(_mm_castsi128_ps(_mm_cvtsi32_si128((int) ((uint32_t) h << 16)))); }
inline void conv16(const void* Wm, int kind, size_t off, int n, float* out) {
const uint16_t* p = (const uint16_t*) Wm + off;
int i = 0;
if (kind == 2) {
for (; i + 8 <= n; i += 8) {
const __m128i w = _mm_loadu_si128((const __m128i*) (p + i));
_mm_storeu_ps(out + i, h2f4(_mm_cvtepu16_epi32(w)));
_mm_storeu_ps(out + i + 4, h2f4(_mm_cvtepu16_epi32(_mm_srli_si128(w, 8))));
}
for (; i < n; ++i) out[i] = f16_one(p[i]);
} else {
const __m128i z = _mm_setzero_si128();
for (; i + 8 <= n; i += 8) {
const __m128i w = _mm_loadu_si128((const __m128i*) (p + i));
_mm_storeu_ps(out + i, _mm_castsi128_ps(_mm_unpacklo_epi16(z, w)));
_mm_storeu_ps(out + i + 4, _mm_castsi128_ps(_mm_unpackhi_epi16(z, w)));
}
for (; i < n; ++i) out[i] = bf16_one(p[i]);
}
}
} // namespace kn
} // namespace
} // namespace strata_vit
#define STRATA_KN_MR 6
#define KN_MIN(a, b) kmin(a, b)
#define KN_MAX(a, b) kmax(a, b)
#define KN_COPY(d, s, n) kcopy((d), (s), (n))
#define KN_ZERO(d, n) kzero((d), (n))
#define STRATA_KN_NAME "sse41"
#define STRATA_KN_GETTER cpu_ops_sse41
#include "cpu_kernels_impl.h"
#else // not x86-64: the tier does not exist, the dispatcher never asks for it
namespace strata_vit {
const CpuOps& cpu_ops_sse41() { return cpu_ops_scalar(); }
}
#endif
+3 -1
View File
@@ -9,7 +9,9 @@ namespace strata_vit {
const CpuOps& cpu_ops(quant::Isa isa) {
if (isa == quant::Isa::Neon) return quant::isa_ok(quant::Isa::Neon) ? cpu_ops_neon() : cpu_ops_scalar();
if (isa == quant::Isa::Avx512 && quant::isa_ok(quant::Isa::Avx512)) return cpu_ops_avx512();
if (isa >= quant::Isa::Avx2 && quant::isa_ok(quant::Isa::Avx2)) return cpu_ops_avx2();
if (isa >= quant::Isa::Avx2 && quant::isa_ok(quant::Isa::Avx2)) return cpu_ops_avx2(); // (Neon was handled above: it ranks outside x86)
if (isa >= quant::Isa::Avx && quant::isa_ok(quant::Isa::Avx)) return cpu_ops_avx();
if (isa >= quant::Isa::Sse41 && quant::isa_ok(quant::Isa::Sse41)) return cpu_ops_sse41();
return cpu_ops_scalar();
}
+2
View File
@@ -47,6 +47,8 @@ struct CpuOps {
};
const CpuOps& cpu_ops_scalar();
const CpuOps& cpu_ops_sse41(); // x86-64 only (both return the scalar table elsewhere): the tiers for CPUs without AVX2, multiply then add (no FMA)
const CpuOps& cpu_ops_avx();
const CpuOps& cpu_ops_avx2(); // defined only on x86-64 (cpu_ops_avx2 returns the scalar table elsewhere)
const CpuOps& cpu_ops_avx512();
const CpuOps& cpu_ops_neon(); // AArch64 only (cpu_ops_neon returns the scalar table elsewhere)
+1 -1
View File
@@ -63,7 +63,7 @@ bool own_reader_supported(const std::string& mmproj, bool gpu, int max_tokens, s
#endif
}
#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
else if (quant::best_isa() == quant::Isa::Scalar) {
else if ((int) quant::best_isa() < (int) quant::Isa::Avx2) { // every x86 tier below AVX2 (scalar, sse41, avx): unchanged by those tiers until gated
// an x86 CPU with only the portable kernel tier (no AVX2): measured 2.1 to 3.7 times mtmd-CPU's time (RESULTS-OWN-3V.md 12.7), so `auto` keeps mtmd
// here; `--tower own` still runs ours. Not on Arm: mtmd's portable Arm build has no fast BF16 path and our NEON tier is faster than mtmd-CPU on BF16
// and F16 weights alike (DGX Spark, 12.12)
+65 -3
View File
@@ -195,6 +195,59 @@ static void test_attention(const CpuOps& o) {
check(worst <= 2e-5, "online softmax + P*V", o.name, worst, 2e-5);
}
// the rotary of one token row and the position add against double
static void test_rope_pos(const CpuOps& o) {
const int H = 3, HD = 72, D = H * HD, half = HD / 2, quarter = HD / 4;
std::vector<float> row((size_t) 2 * D + 16), c1(quarter), s1(quarter), c2(quarter), s2(quarter);
for (auto& v : row) v = rnd(-2, 2);
for (int j = 0; j < quarter; ++j) { c1[j] = std::cos(0.3f * j); s1[j] = std::sin(0.3f * j); c2[j] = std::cos(0.7f * j + 1); s2[j] = std::sin(0.7f * j + 1); }
std::vector<float> got = row;
o.rope_row(got.data(), (size_t) D, H, HD, c1.data(), s1.data(), c2.data(), s2.data());
double worst = 0;
for (int part = 0; part < 2; ++part)
for (int hh = 0; hh < H; ++hh)
for (int j = 0; j < half; ++j) {
const size_t i0 = (size_t) part * D + (size_t) hh * HD + j, i1 = i0 + half;
const double co = j < quarter ? c1[j] : c2[j - quarter], si = j < quarter ? s1[j] : s2[j - quarter];
worst = std::max(worst, std::fabs((double) row[i0] * co - (double) row[i1] * si - got[i0]));
worst = std::max(worst, std::fabs((double) row[i0] * si + (double) row[i1] * co - got[i1]));
}
for (size_t i = (size_t) 2 * D; i < row.size(); ++i) if (got[i] != row[i]) worst = 1e9;
check(worst <= 2e-6, "rope_row", o.name, worst, 2e-6);
const int n = 1152;
std::vector<float> x(n), a(n), b(n), c(n), d(n);
for (int i = 0; i < n; ++i) { x[i] = rnd(-1, 1); a[i] = rnd(-1, 1); b[i] = rnd(-1, 1); c[i] = rnd(-1, 1); d[i] = rnd(-1, 1); }
std::vector<float> y = x;
const float dx = 0.37f, dy = 0.81f;
o.pos_add(y.data(), a.data(), b.data(), c.data(), d.data(), dx, dy, n);
worst = 0;
for (int i = 0; i < n; ++i) {
const double want = x[i] + a[i] * (1.0 - dx) * (1.0 - dy) + b[i] * (double) dx * (1.0 - dy) + c[i] * (1.0 - dx) * dy + d[i] * (double) dx * dy;
worst = std::max(worst, std::fabs(want - y[i]));
}
check(worst <= 2e-6, "pos_add", o.name, worst, 2e-6);
}
// The SSE4.1 tier multiplies then adds in the scalar tier's k order, so on x86 its products equal the scalar tier's bit for bit (cpu_kernels_sse41.cpp)
static void test_gemm_equals_scalar(const CpuOps& o) {
const CpuOps& ref = cpu_ops_scalar();
size_t bad = 0;
for (int M : {1, 7, 13, 50})
for (int K : {1, 72, 300})
for (int N : {3, 8, 21, 72})
for (int init = 0; init < 3; ++init) {
const int lda = K + 1, ldb = (N + 63) / 64 * 64 + 64, ldc = N + 3;
std::vector<float> A((size_t) M * lda), B((size_t) K * ldb + 128), C((size_t) M * ldc), bias((size_t) N);
for (auto& v : A) v = rnd(-1, 1);
for (auto& v : B) v = rnd(-1, 1);
for (auto& v : C) v = rnd(-1, 1);
for (auto& v : bias) v = rnd(-1, 1);
std::vector<float> C2 = C;
o.gemm(A.data(), (size_t) lda, M, B.data(), (size_t) ldb, K, N, C.data(), (size_t) ldc, init, bias.data());
ref.gemm(A.data(), (size_t) lda, M, B.data(), (size_t) ldb, K, N, C2.data(), (size_t) ldc, init, bias.data());
for (size_t i = 0; i < C.size(); ++i) bad += std::memcmp(&C[i], &C2[i], 4) != 0;
}
check(bad == 0, "gemm equals the scalar tier bit for bit", o.name, (double) bad, 0.0);
}
static void test_team() {
for (int nth : {1, 2, 7, 16}) {
Team t(nth);
@@ -234,10 +287,15 @@ int main(int argc, char** argv) {
return g_fail ? 1 : 0;
}
const quant::Caps& c = quant::caps();
std::printf("cpu: avx2 %d fma %d f16c %d avx512 %d; best tier %s\n", c.avx2, c.fma, c.f16c, c.avx512, quant::isa_name(quant::best_isa()));
std::printf("cpu: sse41 %d avx %d avx2 %d fma %d f16c %d avx512 %d; best tier %s\n", c.sse41, c.avx, c.avx2, c.fma, c.f16c, c.avx512,
quant::isa_name(quant::best_isa()));
// every tier this CPU runs, capped by STRATA_FORCE_ISA as in the tower (an emulated older CPU never reaches a wider tier); NEON on Arm
const int cap = (int) quant::best_isa();
std::vector<const CpuOps*> tiers = {&cpu_ops_scalar()};
if (quant::isa_ok(quant::Isa::Avx2)) tiers.push_back(&cpu_ops_avx2());
if (quant::isa_ok(quant::Isa::Avx512)) tiers.push_back(&cpu_ops_avx512());
if (cap >= (int) quant::Isa::Sse41 && cap != (int) quant::Isa::Neon && quant::isa_ok(quant::Isa::Sse41)) tiers.push_back(&cpu_ops_sse41());
if (cap >= (int) quant::Isa::Avx && cap != (int) quant::Isa::Neon && quant::isa_ok(quant::Isa::Avx)) tiers.push_back(&cpu_ops_avx());
if (cap >= (int) quant::Isa::Avx2 && cap != (int) quant::Isa::Neon && quant::isa_ok(quant::Isa::Avx2)) tiers.push_back(&cpu_ops_avx2());
if (cap == (int) quant::Isa::Avx512 && quant::isa_ok(quant::Isa::Avx512)) tiers.push_back(&cpu_ops_avx512());
if (quant::isa_ok(quant::Isa::Neon)) tiers.push_back(&cpu_ops_neon());
for (const CpuOps* o : tiers) {
std::printf("tier %s (tile %d x %d)\n", o->name, o->MR, o->NR);
@@ -249,6 +307,10 @@ int main(int argc, char** argv) {
test_layernorm(*o);
test_add(*o);
test_attention(*o);
test_rope_pos(*o);
#if defined(__x86_64__) || defined(_M_X64)
if (std::strcmp(o->name, "sse41") == 0) test_gemm_equals_scalar(*o);
#endif
}
test_team();
test_round_choice();