mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-05 07:48:45 +00:00
tools/vision/own: vectorised 16-bit rounding for STRATA_VIT_REF_ROUND on the CPU tower (round16 per ISA tier, BF16 and F16, plus a bit-exact test against the scalar tier)
NOT MEASURED YET: the build, the unit test and the ROUND speed pairs on the vectorised code are still queued on p2006 and l620 at the time of this commit.
This commit is contained in:
1 parent
423225d879
commit
aec86299d2
7 files changed
+87
-33
No files matched your search
@@ -33,6 +33,8 @@ struct CpuOps {
|
||||
void (*layernorm)(const float* x, float* y, int D, const float* w, const float* b, float eps);
|
||||
// x[i] += y[i], i < n
|
||||
void (*add)(float* x, const float* y, size_t n);
|
||||
// p[i] rounded to the 16-bit type and back (kind 1 BF16, 2 F16; round to nearest even): STRATA_VIT_REF_ROUND, the inputs of the products as ggml's CPU path hands them
|
||||
void (*round16)(float* p, size_t n, int kind);
|
||||
// One key tile of the online softmax for qrows query rows: S[qrows][nkeys] (row stride lds, already scaled) becomes P = exp(S - m_new) in place, the running
|
||||
// max m[r] and sum l[r] are updated and the context rows O[r][0..hd) (row stride ldo) are rescaled by exp(m_old - m_new) (O's P*V is added afterwards).
|
||||
void (*softmax_update)(float* S, size_t lds, int qrows, int nkeys, float* m, float* l, float* O, size_t ldo, int hd);
|
||||
|
||||
@@ -56,6 +56,25 @@ inline V vexp(V xv) {
|
||||
return _mm256_mul_ps(y, _mm256_castsi256_ps(e));
|
||||
}
|
||||
|
||||
// 8 floats rounded to the 16-bit type and back (BF16: integer round to nearest even; F16: the hardware conversion pair, also round to nearest even)
|
||||
inline __m256 round8(__m256 x, int kind) {
|
||||
if (kind == 2) return _mm256_cvtph_ps(_mm256_cvtps_ph(x, _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC));
|
||||
__m256i u = _mm256_castps_si256(x);
|
||||
const __m256i lsb = _mm256_and_si256(_mm256_srli_epi32(u, 16), _mm256_set1_epi32(1));
|
||||
u = _mm256_add_epi32(u, _mm256_add_epi32(_mm256_set1_epi32(0x7fff), lsb));
|
||||
return _mm256_castsi256_ps(_mm256_and_si256(u, _mm256_set1_epi32((int) 0xffff0000u)));
|
||||
}
|
||||
inline void round_buf(float* p, size_t n, int kind) {
|
||||
size_t i = 0;
|
||||
for (; i + 8 <= n; i += 8) _mm256_storeu_ps(p + i, round8(_mm256_loadu_ps(p + i), kind));
|
||||
if (i < n) {
|
||||
float t[8] = {0};
|
||||
for (size_t j = i; j < n; ++j) t[j - i] = p[j];
|
||||
_mm256_storeu_ps(t, round8(_mm256_loadu_ps(t), kind));
|
||||
for (size_t j = i; j < n; ++j) p[j] = t[j - i];
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
@@ -45,6 +45,24 @@ inline V vexp(V xv) {
|
||||
return _mm512_scalef_ps(y, fx);
|
||||
}
|
||||
|
||||
inline __m512 round16v(__m512 x, int kind) {
|
||||
if (kind == 2) return _mm512_cvtph_ps(_mm512_cvtps_ph(x, _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC));
|
||||
__m512i u = _mm512_castps_si512(x);
|
||||
const __m512i lsb = _mm512_and_si512(_mm512_srli_epi32(u, 16), _mm512_set1_epi32(1));
|
||||
u = _mm512_add_epi32(u, _mm512_add_epi32(_mm512_set1_epi32(0x7fff), lsb));
|
||||
return _mm512_castsi512_ps(_mm512_and_si512(u, _mm512_set1_epi32((int) 0xffff0000u)));
|
||||
}
|
||||
inline void round_buf(float* p, size_t n, int kind) {
|
||||
size_t i = 0;
|
||||
for (; i + 16 <= n; i += 16) _mm512_storeu_ps(p + i, round16v(_mm512_loadu_ps(p + i), kind));
|
||||
if (i < n) {
|
||||
float t[16] = {0};
|
||||
for (size_t j = i; j < n; ++j) t[j - i] = p[j];
|
||||
_mm512_storeu_ps(t, round16v(_mm512_loadu_ps(t), kind));
|
||||
for (size_t j = i; j < n; ++j) p[j] = t[j - i];
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
@@ -149,6 +149,8 @@ void add(float* x, const float* y, size_t n) {
|
||||
for (; i < n; ++i) x[i] += y[i];
|
||||
}
|
||||
|
||||
void round16(float* p, size_t n, int kind) { round_buf(p, n, kind); }
|
||||
|
||||
void softmax_update(float* S, size_t lds, int qrows, int nkeys, float* m, float* l, float* O, size_t ldo, int hd) {
|
||||
for (int r = 0; r < qrows; ++r) {
|
||||
float* s = S + (size_t) r * lds;
|
||||
@@ -184,7 +186,7 @@ void softmax_update(float* S, size_t lds, int qrows, int nkeys, float* m, float*
|
||||
} // namespace
|
||||
|
||||
const CpuOps& STRATA_KN_GETTER() {
|
||||
static const CpuOps ops = {STRATA_KN_NAME, kn::MRF, kn::NR, kn::pack_w, kn::conv, kn::gemm, kn::gelu, kn::layernorm, kn::add, kn::softmax_update};
|
||||
static const CpuOps ops = {STRATA_KN_NAME, kn::MRF, kn::NR, kn::pack_w, kn::conv, kn::gemm, kn::gelu, kn::layernorm, kn::add, kn::round16, kn::softmax_update};
|
||||
return ops;
|
||||
}
|
||||
|
||||
|
||||
@@ -30,6 +30,31 @@ inline V vexp(V a) {
|
||||
for (int i = 0; i < 4; ++i) v.x[i] = std::exp(a.x[i] < -87.0f ? -87.0f : a.x[i] > 88.0f ? 88.0f : a.x[i]);
|
||||
return v;
|
||||
}
|
||||
inline float round_one(float f, int kind) {
|
||||
uint32_t u;
|
||||
std::memcpy(&u, &f, 4);
|
||||
if (kind == 1) {
|
||||
if ((u & 0x7fffffffu) > 0x7f800000u) return f;
|
||||
u += 0x7fffu + ((u >> 16) & 1u);
|
||||
u &= 0xffff0000u;
|
||||
std::memcpy(&f, &u, 4);
|
||||
return f;
|
||||
}
|
||||
const float a = std::fabs(f);
|
||||
if (!(a < 65520.0f)) return f;
|
||||
if (a < 6.103515625e-05f) {
|
||||
const float q = std::nearbyint(a * 16777216.0f) / 16777216.0f;
|
||||
return (u & 0x80000000u) ? -q : q;
|
||||
}
|
||||
int e;
|
||||
std::frexp(a, &e);
|
||||
const float ulp = std::ldexp(1.0f, e - 11);
|
||||
const float q = std::nearbyint(a / ulp) * ulp;
|
||||
return (u & 0x80000000u) ? -q : q;
|
||||
}
|
||||
inline void round_buf(float* p, size_t n, int kind) {
|
||||
for (size_t i = 0; i < n; ++i) p[i] = round_one(p[i], kind);
|
||||
}
|
||||
inline void conv16(const void* Wm, int kind, size_t off, int n, float* out) {
|
||||
const uint16_t* p = (const uint16_t*) Wm + off;
|
||||
for (int i = 0; i < n; ++i) out[i] = kind == 1 ? quant::bf16_to_f32(p[i]) : quant::f16_to_f32(p[i]);
|
||||
|
||||
@@ -103,6 +103,22 @@ static void test_conv(const CpuOps& o) {
|
||||
}
|
||||
}
|
||||
|
||||
static void test_round(const CpuOps& o) {
|
||||
const CpuOps& ref = cpu_ops_scalar();
|
||||
for (int kind = 1; kind <= 2; ++kind)
|
||||
for (size_t n : {1u, 7u, 8u, 17u, 100u, 4099u}) {
|
||||
std::vector<float> x(n), a, b;
|
||||
for (size_t i = 0; i < n; ++i) x[i] = (i % 5 == 0) ? rnd(-1e-4f, 1e-4f) : (i % 7 == 0) ? rnd(-3000, 3000) : rnd(-4, 4);
|
||||
a = x;
|
||||
b = x;
|
||||
o.round16(a.data(), n, kind);
|
||||
ref.round16(b.data(), n, kind);
|
||||
size_t bad = 0;
|
||||
for (size_t i = 0; i < n; ++i) bad += std::memcmp(&a[i], &b[i], 4) != 0;
|
||||
check(bad == 0, kind == 1 ? "round16 BF16 equals the scalar tier bit for bit" : "round16 F16 equals the scalar tier bit for bit", o.name, (double) bad, 0.0);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_gelu(const CpuOps& o) {
|
||||
const int rows = 3, n = 203, ld = 211;
|
||||
std::vector<float> x((size_t) rows * ld), y;
|
||||
@@ -217,6 +233,7 @@ int main(int argc, char** argv) {
|
||||
test_gemm(*o);
|
||||
test_pack_values(*o);
|
||||
test_conv(*o);
|
||||
test_round(*o);
|
||||
test_gelu(*o);
|
||||
test_layernorm(*o);
|
||||
test_add(*o);
|
||||
|
||||
@@ -29,33 +29,6 @@ struct Layer {
|
||||
Mat qkv, o, up, down;
|
||||
};
|
||||
|
||||
inline float round_bf16(float f) {
|
||||
uint32_t u;
|
||||
std::memcpy(&u, &f, 4);
|
||||
if ((u & 0x7fffffffu) > 0x7f800000u) return f;
|
||||
u += 0x7fffu + ((u >> 16) & 1u);
|
||||
u &= 0xffff0000u;
|
||||
std::memcpy(&f, &u, 4);
|
||||
return f;
|
||||
}
|
||||
|
||||
inline float round_f16(float f) {
|
||||
uint32_t u;
|
||||
std::memcpy(&u, &f, 4);
|
||||
const uint32_t sign = u & 0x80000000u;
|
||||
const float a = std::fabs(f);
|
||||
if (!(a < 65520.0f)) return f;
|
||||
if (a < 6.103515625e-05f) {
|
||||
const float q = std::nearbyint(a * 16777216.0f) / 16777216.0f;
|
||||
return sign ? -q : q;
|
||||
}
|
||||
int e;
|
||||
std::frexp(a, &e);
|
||||
const float ulp = std::ldexp(1.0f, e - 11);
|
||||
const float q = std::nearbyint(a / ulp) * ulp;
|
||||
return sign ? -q : q;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
struct VitCpu::Impl {
|
||||
@@ -118,8 +91,8 @@ struct VitCpu::Impl {
|
||||
const size_t blk = 16384;
|
||||
const int tasks = (int) ((n + blk - 1) / blk);
|
||||
team->run(tasks, [&](int task, int) {
|
||||
const size_t i1 = std::min(n, ((size_t) task + 1) * blk);
|
||||
for (size_t i = (size_t) task * blk; i < i1; ++i) p[i] = kind == 1 ? round_bf16(p[i]) : round_f16(p[i]);
|
||||
const size_t i0 = (size_t) task * blk, i1 = std::min(n, i0 + blk);
|
||||
ops->round16(p + i0, i1 - i0, kind);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -320,9 +293,7 @@ bool VitCpu::encode(const PreparedImage& img, int threads, std::vector<float>& r
|
||||
v[j + half] = a * si + b * co;
|
||||
}
|
||||
}
|
||||
if (m.act_round) {
|
||||
for (size_t i = 0; i < D3; ++i) row[i] = round_f16(row[i]);
|
||||
}
|
||||
if (m.act_round) ops.round16(row, D3, 2);
|
||||
for (int i = 0; i < D; ++i) row[i] *= kq_scale;
|
||||
});
|
||||
// K transposed per head: KT[h][d][j]
|
||||
|
||||
Reference in new issue
Block a user