router lookahead: an AVX1 dot and a plain fallback for CPUs without AVX2 (Strata_Dirigo)
The routing-aware prefetch's router dot (bf16_rows_dot_multi) lives in kq_avx2.cpp, so an AVX-only CPU started fine and died on the first request (SIGILL in vpmovzxwd on a Xeon E5-2687W). It now takes the AVX2 kernel where the CPU has AVX2, kq_avx1.cpp's AVX dot (mul + add, 128-bit integer widening) where it has AVX, and the same sums in plain C++ below that. cpu_avx1_ok() probes AVX + OSXSAVE + the YMM state. An estimate only (which experts to prefetch). A CPU with AVX2 runs exactly what it ran before. From rwkeyes' Strata_Dirigo fork (MIT; 0d00a66, e0754d3), with MSVC's /arch:AVX added and __builtin_memcpy replaced by std::memcpy for MSVC.
This commit is contained in:
1 parent
3f32e4b66a
commit
51a60af92a
6 files changed
+151
-3
No files matched your search
+5
-1
@@ -888,17 +888,21 @@ if(STRATA_NATIVE_EXPERTS)
|
||||
# part of strata_kernels_cpu (the pool calls these and they call its CPU probe: one library, so GNU ld's
|
||||
# one-pass static linking has no cycle to resolve)
|
||||
target_sources(strata_kernels_cpu PRIVATE src/kernels/cpu/native_expert.cpp src/kernels/cpu/iq_avx512.cpp
|
||||
src/kernels/cpu/iq_avx2.cpp src/kernels/cpu/kq_avx2.cpp)
|
||||
src/kernels/cpu/iq_avx2.cpp src/kernels/cpu/kq_avx2.cpp
|
||||
src/kernels/cpu/kq_avx1.cpp)
|
||||
target_include_directories(strata_kernels_cpu PRIVATE third_party/ggml)
|
||||
if(MSVC)
|
||||
set_source_files_properties(src/kernels/cpu/iq_avx512.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX512")
|
||||
set_source_files_properties(src/kernels/cpu/iq_avx2.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX2")
|
||||
set_source_files_properties(src/kernels/cpu/kq_avx2.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX2")
|
||||
# the router lookahead's AVX1 dot (older CPUs; called behind cpu_avx1_ok): no FMA, no 256-bit integer ops
|
||||
set_source_files_properties(src/kernels/cpu/kq_avx1.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX")
|
||||
else()
|
||||
set_source_files_properties(src/kernels/cpu/iq_avx512.cpp PROPERTIES COMPILE_OPTIONS
|
||||
"-mavx512f;-mavx512bw;-mavx512vl;-mavx512dq;-mavx512vnni;-mavx512vbmi;-mf16c;-mfma")
|
||||
set_source_files_properties(src/kernels/cpu/iq_avx2.cpp PROPERTIES COMPILE_OPTIONS "-mavx2;-mfma;-mf16c")
|
||||
set_source_files_properties(src/kernels/cpu/kq_avx2.cpp PROPERTIES COMPILE_OPTIONS "-mavx2;-mfma;-mf16c")
|
||||
set_source_files_properties(src/kernels/cpu/kq_avx1.cpp PROPERTIES COMPILE_OPTIONS "-mavx")
|
||||
endif()
|
||||
target_link_libraries(strata_kernels_cpu PUBLIC ggml-cpu ggml-base)
|
||||
target_compile_definitions(strata_kernels_cpu PUBLIC STRATA_NATIVE_EXPERTS=1)
|
||||
|
||||
@@ -47,6 +47,8 @@ bool cpu_avx512_ok();
|
||||
/// Whether this CPU (and its OS) runs the AVX2 kernels (AVX, AVX2, FMA, F16C): the floor of every expert kernel
|
||||
/// (q2_avx2.cpp, iq_avx2.cpp, and ggml-cpu in the portable build). STRATA_FORCE_AVX2 does not change it.
|
||||
bool cpu_avx2_ok();
|
||||
/// Whether this CPU (and its OS) runs AVX (Sandy Bridge and newer): the AVX1 router dot (kq_avx1.cpp).
|
||||
bool cpu_avx1_ok();
|
||||
/// Whether a native pack's layer of this ggml type runs on Strata's own Q2_0 kernels (AVX2 / AVX-512). On a
|
||||
/// CPU without AVX2 (a native, non-portable build: ggml-cpu compiled for this CPU's SSE) a Q2_0 layer takes
|
||||
/// ggml-cpu's own Q2_0 vec_dot like every other native type: slower, but it runs.
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
// include/strata/kernels/cpu/kq_avx1.hpp - the AVX1 router dot for older CPUs (from the Strata_Dirigo fork).
|
||||
//
|
||||
// bf16_rows_dot_multi (kq_avx2.cpp) is compiled with -mavx2 and uses instructions an AVX-only CPU does
|
||||
// not have (`vpmovzxwd`, `vfmadd`), so an AVX-only host cannot call it at all. This is the same
|
||||
// arithmetic on AVX1 instructions: 8 float lanes per token, one weight stream per row shared by all
|
||||
// `nt` tokens, one horizontal sum per (row, token). AVX1 has no FMA, so the multiply and the add stay
|
||||
// separate; it has no 256-bit integer ops, so the bf16 -> f32 widening runs in 128-bit halves.
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace strata::kernels::cpu {
|
||||
|
||||
/// Mirrors bf16_rows_dot_multi's arithmetic (same accumulation shape, mul+add instead of FMA).
|
||||
/// Requires cols % 8 == 0 and nt <= 8, exactly like the AVX2 kernel it stands in for.
|
||||
void bf16_rows_dot_multi_avx1(const uint16_t* w, int rows, int cols, const float* x, int nt, float* out);
|
||||
|
||||
} // namespace strata::kernels::cpu
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "strata/kernels/elementwise.hpp"
|
||||
#include "strata/kernels/quantize_act.hpp"
|
||||
#include "strata/kernels/s2_expert_grouped.hpp"
|
||||
#include "strata/kernels/cpu/kq_avx1.hpp"
|
||||
#include "strata/kernels/cpu/kq_avx2.hpp"
|
||||
|
||||
#include <cuda_runtime.h>
|
||||
@@ -1219,8 +1220,35 @@ void RouterLookahead::run() {
|
||||
}
|
||||
const auto t0 = std::chrono::steady_clock::now();
|
||||
want.clear();
|
||||
strata::kernels::cpu::bf16_rows_dot_multi(routers_[(size_t) layer].data(), (int) n_expert_, (int) n_embd_,
|
||||
x_.data(), (int) nt, logits.data());
|
||||
// The router dot is AVX2 (kq_avx2.cpp); a CPU without AVX2 (the experimental older-CPU builds) takes the AVX1
|
||||
// one (kq_avx1.cpp) or, without AVX, the same sums in plain C++. From the Strata_Dirigo fork (rwkeyes): an
|
||||
// AVX-only Xeon E5-2687W died here (vpmovzxwd) on its first request. An estimate only (which experts to
|
||||
// prefetch); the order of the additions differs between the three, the output does not depend on it.
|
||||
if (strata::kernels::cpu::cpu_avx2_ok()) {
|
||||
strata::kernels::cpu::bf16_rows_dot_multi(routers_[(size_t) layer].data(), (int) n_expert_, (int) n_embd_,
|
||||
x_.data(), (int) nt, logits.data());
|
||||
} else if (strata::kernels::cpu::cpu_avx1_ok()) {
|
||||
strata::kernels::cpu::bf16_rows_dot_multi_avx1(routers_[(size_t) layer].data(), (int) n_expert_,
|
||||
(int) n_embd_, x_.data(), (int) nt, logits.data());
|
||||
} else {
|
||||
// a mul and an add, not std::fma: without an FMA instruction that is a libm call per element (74x
|
||||
// slower on a Xeon E5-2665, measured by the fork)
|
||||
const uint16_t* rw = routers_[(size_t) layer].data();
|
||||
for (int64_t r = 0; r < n_expert_; ++r) {
|
||||
const uint16_t* wr = rw + (size_t) r * (size_t) n_embd_;
|
||||
for (int64_t t = 0; t < nt; ++t) {
|
||||
const float* xr = x_.data() + (size_t) t * (size_t) n_embd_;
|
||||
float acc = 0.0f;
|
||||
for (int64_t c = 0; c < n_embd_; ++c) {
|
||||
const uint32_t bits = (uint32_t) wr[c] << 16;
|
||||
float wf;
|
||||
std::memcpy(&wf, &bits, sizeof wf);
|
||||
acc += wf * xr[c];
|
||||
}
|
||||
logits[(size_t) t * (size_t) n_expert_ + (size_t) r] = acc;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int64_t t = 0; t < nt; ++t) {
|
||||
const float* lt = logits.data() + (size_t) (t * n_expert_);
|
||||
for (int64_t e = 0; e < n_expert_; ++e) order[(size_t) e] = (int32_t) e;
|
||||
|
||||
@@ -85,6 +85,31 @@ bool cpu_avx2_ok() {
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool cpu_avx1_ok() {
|
||||
// AVX (Sandy Bridge, 2011): AVX + OSXSAVE with the OS saving the YMM state; FMA, F16C and AVX2 not needed.
|
||||
// From the Strata_Dirigo fork (rwkeyes).
|
||||
static const bool ok = [] {
|
||||
unsigned r[4] = {0, 0, 0, 0};
|
||||
#if defined(_MSC_VER)
|
||||
int x[4];
|
||||
__cpuidex(x, 1, 0);
|
||||
for (int i = 0; i < 4; ++i) r[i] = (unsigned) x[i];
|
||||
#else
|
||||
__cpuid_count(1, 0, r[0], r[1], r[2], r[3]);
|
||||
#endif
|
||||
if (!((r[2] >> 27) & 1u) || !((r[2] >> 28) & 1u)) return false; // OSXSAVE, AVX
|
||||
#if defined(_MSC_VER)
|
||||
const unsigned long long xcr0 = _xgetbv(0);
|
||||
#else
|
||||
unsigned lo = 0, hi = 0;
|
||||
__asm__ volatile("xgetbv" : "=a"(lo), "=d"(hi) : "c"(0));
|
||||
const unsigned long long xcr0 = ((unsigned long long) hi << 32) | lo;
|
||||
#endif
|
||||
return (xcr0 & 0x6) == 0x6;
|
||||
}();
|
||||
return ok;
|
||||
}
|
||||
|
||||
std::string cpu_name() {
|
||||
unsigned r[12] = {};
|
||||
#if defined(_MSC_VER)
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
// src/kernels/cpu/kq_avx1.cpp - the AVX1 router dot for older CPUs (see kq_avx1.hpp). From rwkeyes' Strata_Dirigo
|
||||
// fork (MIT), its AVX1 floor.
|
||||
//
|
||||
// Measured on the hardware this exists for (Xeon E5-2665, 2x8 cores, AVX only) for one layer's router
|
||||
// [512 experts x 5120 embeddings]: the scalar std::fma fallback it replaces took 362 ms/layer, a scalar
|
||||
// mul+add 4.9 ms, and this kernel 2.6 ms - 137x, with no extra memory (the weights stay bf16).
|
||||
//
|
||||
// Compiled for AVX (/arch:AVX, -mavx; CMakeLists.txt sets that per source); it is only ever called behind
|
||||
// cpu_avx1_ok(), so no CPU that lacks AVX can reach it.
|
||||
#include "strata/kernels/cpu/kq_avx1.hpp"
|
||||
|
||||
#include <immintrin.h>
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#if !defined(__AVX__)
|
||||
#error "kq_avx1.cpp must be compiled with AVX enabled (see the per-source flags in CMakeLists.txt)"
|
||||
#endif
|
||||
|
||||
namespace strata::kernels::cpu {
|
||||
namespace {
|
||||
|
||||
// 8 floats -> one float, the same reduction order the AVX2 kernel's hsum_float_8 uses.
|
||||
inline float hsum_float_8_avx1(__m256 v) {
|
||||
__m128 lo = _mm256_castps256_ps128(v);
|
||||
__m128 hi = _mm256_extractf128_ps(v, 1);
|
||||
lo = _mm_add_ps(lo, hi);
|
||||
lo = _mm_add_ps(lo, _mm_movehl_ps(lo, lo));
|
||||
lo = _mm_add_ss(lo, _mm_shuffle_ps(lo, lo, 0x55));
|
||||
return _mm_cvtss_f32(lo);
|
||||
}
|
||||
|
||||
// 8 bf16 -> 8 f32: a bf16 is the top half of its f32, so widen in 128-bit halves (AVX1 has no
|
||||
// 256-bit integer ops) and shift each 32-bit lane into the high half.
|
||||
inline __m256 bf16x8_to_f32x8_avx1(const uint16_t* p) {
|
||||
const __m128i v = _mm_loadu_si128((const __m128i*) p);
|
||||
const __m128i z = _mm_setzero_si128();
|
||||
const __m128 lo = _mm_castsi128_ps(_mm_slli_epi32(_mm_unpacklo_epi16(v, z), 16));
|
||||
const __m128 hi = _mm_castsi128_ps(_mm_slli_epi32(_mm_unpackhi_epi16(v, z), 16));
|
||||
return _mm256_insertf128_ps(_mm256_castps128_ps256(lo), hi, 1);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void bf16_rows_dot_multi_avx1(const uint16_t* w, int rows, int cols, const float* x, int nt, float* out) {
|
||||
// each row is read once for all `nt` (<= 8) tokens, exactly as the AVX2 kernel does
|
||||
for (int r = 0; r < rows; ++r) {
|
||||
const uint16_t* wr = w + (size_t) r * (size_t) cols;
|
||||
__m256 acc[8];
|
||||
for (int t = 0; t < nt; ++t) acc[t] = _mm256_setzero_ps();
|
||||
int c = 0;
|
||||
for (; c + 8 <= cols; c += 8) {
|
||||
const __m256 wf = bf16x8_to_f32x8_avx1(wr + c);
|
||||
for (int t = 0; t < nt; ++t) {
|
||||
const __m256 xv = _mm256_loadu_ps(x + (size_t) t * (size_t) cols + c);
|
||||
acc[t] = _mm256_add_ps(acc[t], _mm256_mul_ps(wf, xv)); // no FMA on AVX1: mul, then add
|
||||
}
|
||||
}
|
||||
for (int t = 0; t < nt; ++t) {
|
||||
float s = hsum_float_8_avx1(acc[t]);
|
||||
for (; c < cols; ++c) { // cols % 8 != 0 tail (cols % 8 == 0 today)
|
||||
const uint32_t bits = (uint32_t) wr[c] << 16;
|
||||
float wf; std::memcpy(&wf, &bits, sizeof wf);
|
||||
s += wf * x[(size_t) t * (size_t) cols + c];
|
||||
}
|
||||
out[(size_t) t * (size_t) rows + r] = s;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace strata::kernels::cpu
|
||||
Reference in new issue
Block a user