GigaAM-v4-c / src /ga_kernels.c
eschmidbauer's picture
streaming runtime and conversion pipeline for GigaAM-v4
5b65ff7
Raw History Blame Contribute Delete
12 kB
#include "ga_kernels.h"
#include "ga_half.h"
#include <math.h>
#define EXP_LIMIT 88.3762626647949f
#define EXP_LOG2E 1.44269504088896341f
#define EXP_C1 0.693359375f
#define EXP_C2 (-2.12194440e-4f)
#define EXP_P0 1.9875691500e-4f
#define EXP_P1 1.3981999507e-3f
#define EXP_P2 8.3334519073e-3f
#define EXP_P3 4.1665795894e-2f
#define EXP_P4 1.6666665459e-1f
#define EXP_P5 5.0000001201e-1f
#if defined(__aarch64__)
#include <arm_neon.h>
#define GA_SIMD "neon"
#define VW 4
#define NR 4
#define GA_DOT_6XN
typedef float32x4_t vf;
#define vf_zero() vdupq_n_f32(0.0f)
#define vf_load(p) vld1q_f32(p)
#define vf_fma(acc, x, y) vfmaq_f32(acc, x, y)
#define vf_add(x, y) vaddq_f32(x, y)
#define vf_sum(x) vaddvq_f32(x)
#define vf_load_f16(p) vcvt_f32_f16(vreinterpret_f16_u16(vld1_u16(p)))
static void exp_neg_simd(const float *x, float *e, int n);
/* Fewer than one vector: padded through the same polynomial so position never matters. */
static void exp_neg_tail(const float *x, float *e, int n)
{
float in[4] = {0}, out[4];
for (int i = 0; i < n; i++)
in[i] = x[i];
exp_neg_simd(in, out, 4);
for (int i = 0; i < n; i++)
e[i] = out[i];
}
static void exp_neg_simd(const float *x, float *e, int n)
{
int i = 0;
for (; i + 4 <= n; i += 4) {
float32x4_t v = vnegq_f32(vld1q_f32(x + i));
float32x4_t fx, p, v2;
int32x4_t power;
v = vminq_f32(vmaxq_f32(v, vdupq_n_f32(-EXP_LIMIT)), vdupq_n_f32(EXP_LIMIT));
fx = vrndmq_f32(vfmaq_n_f32(vdupq_n_f32(0.5f), v, EXP_LOG2E));
v = vfmsq_n_f32(v, fx, EXP_C1);
v = vfmsq_n_f32(v, fx, EXP_C2);
v2 = vmulq_f32(v, v);
p = vdupq_n_f32(EXP_P0);
p = vfmaq_f32(vdupq_n_f32(EXP_P1), p, v);
p = vfmaq_f32(vdupq_n_f32(EXP_P2), p, v);
p = vfmaq_f32(vdupq_n_f32(EXP_P3), p, v);
p = vfmaq_f32(vdupq_n_f32(EXP_P4), p, v);
p = vfmaq_f32(vdupq_n_f32(EXP_P5), p, v);
p = vaddq_f32(vfmaq_f32(v, p, v2), vdupq_n_f32(1.0f));
power = vshlq_n_s32(vaddq_s32(vcvtq_s32_f32(fx), vdupq_n_s32(127)), 23);
vst1q_f32(e + i, vmulq_f32(p, vreinterpretq_f32_s32(power)));
}
if (i < n)
exp_neg_tail(x + i, e + i, n - i);
}
#elif defined(__AVX2__) && defined(__FMA__) && defined(__F16C__)
#include <immintrin.h>
#define GA_SIMD "avx2"
#define VW 8
#define NR 2
typedef __m256 vf;
#define vf_zero() _mm256_setzero_ps()
#define vf_load(p) _mm256_loadu_ps(p)
#define vf_fma(acc, x, y) _mm256_fmadd_ps(x, y, acc)
#define vf_add(x, y) _mm256_add_ps(x, y)
#define vf_load_f16(p) _mm256_cvtph_ps(_mm_loadu_si128((const __m128i *)(p)))
static inline float vf_sum(__m256 x)
{
__m128 lo = _mm_add_ps(_mm256_castps256_ps128(x), _mm256_extractf128_ps(x, 1));
lo = _mm_add_ps(lo, _mm_movehl_ps(lo, lo));
lo = _mm_add_ss(lo, _mm_shuffle_ps(lo, lo, 1));
return _mm_cvtss_f32(lo);
}
static void exp_neg_simd(const float *x, float *e, int n);
/* Fewer than one vector: padded through the same polynomial so position never matters. */
static void exp_neg_tail(const float *x, float *e, int n)
{
float in[8] = {0}, out[8];
for (int i = 0; i < n; i++)
in[i] = x[i];
exp_neg_simd(in, out, 8);
for (int i = 0; i < n; i++)
e[i] = out[i];
}
static void exp_neg_simd(const float *x, float *e, int n)
{
int i = 0;
for (; i + 8 <= n; i += 8) {
__m256 v = _mm256_sub_ps(_mm256_setzero_ps(), _mm256_loadu_ps(x + i));
__m256 fx, p, v2;
__m256i power;
v = _mm256_min_ps(_mm256_max_ps(v, _mm256_set1_ps(-EXP_LIMIT)),
_mm256_set1_ps(EXP_LIMIT));
fx = _mm256_floor_ps(_mm256_fmadd_ps(v, _mm256_set1_ps(EXP_LOG2E), _mm256_set1_ps(0.5f)));
v = _mm256_fnmadd_ps(fx, _mm256_set1_ps(EXP_C1), v);
v = _mm256_fnmadd_ps(fx, _mm256_set1_ps(EXP_C2), v);
v2 = _mm256_mul_ps(v, v);
p = _mm256_set1_ps(EXP_P0);
p = _mm256_fmadd_ps(p, v, _mm256_set1_ps(EXP_P1));
p = _mm256_fmadd_ps(p, v, _mm256_set1_ps(EXP_P2));
p = _mm256_fmadd_ps(p, v, _mm256_set1_ps(EXP_P3));
p = _mm256_fmadd_ps(p, v, _mm256_set1_ps(EXP_P4));
p = _mm256_fmadd_ps(p, v, _mm256_set1_ps(EXP_P5));
p = _mm256_add_ps(_mm256_fmadd_ps(p, v2, v), _mm256_set1_ps(1.0f));
power = _mm256_slli_epi32(
_mm256_add_epi32(_mm256_cvtps_epi32(fx), _mm256_set1_epi32(127)), 23);
_mm256_storeu_ps(e + i, _mm256_mul_ps(p, _mm256_castsi256_ps(power)));
}
if (i < n)
exp_neg_tail(x + i, e + i, n - i);
}
#elif defined(__wasm_simd128__)
#include <wasm_simd128.h>
#define GA_SIMD "wasm"
#define VW 4
#define NR 4
typedef v128_t vf;
/* simd128 has no fused multiply-add: results differ from native in the last bits. */
#define vf_zero() wasm_f32x4_splat(0.0f)
#define vf_load(p) wasm_v128_load(p)
#define vf_fma(acc, x, y) wasm_f32x4_add(acc, wasm_f32x4_mul(x, y))
#define vf_add(x, y) wasm_f32x4_add(x, y)
#define vf_load_f16(p) load_f16(p)
static inline float vf_sum(v128_t x)
{
return (wasm_f32x4_extract_lane(x, 0) + wasm_f32x4_extract_lane(x, 1)) +
(wasm_f32x4_extract_lane(x, 2) + wasm_f32x4_extract_lane(x, 3));
}
/* Exact for finite halves (incl. subnormals): bits << 13 scaled by 2^112. */
static inline v128_t load_f16(const uint16_t *p)
{
v128_t h = wasm_u32x4_load16x4(p);
v128_t sign = wasm_i32x4_shl(wasm_v128_and(h, wasm_i32x4_splat(0x8000)), 16);
v128_t mag = wasm_i32x4_shl(wasm_v128_and(h, wasm_i32x4_splat(0x7fff)), 13);
return wasm_v128_or(wasm_f32x4_mul(mag, wasm_f32x4_splat(0x1p112f)), sign);
}
static void exp_neg_simd(const float *x, float *e, int n);
/* Fewer than one vector: padded through the same polynomial so position never matters. */
static void exp_neg_tail(const float *x, float *e, int n)
{
float in[4] = {0}, out[4];
for (int i = 0; i < n; i++)
in[i] = x[i];
exp_neg_simd(in, out, 4);
for (int i = 0; i < n; i++)
e[i] = out[i];
}
#define MADD(a, b, c) wasm_f32x4_add(a, wasm_f32x4_mul(b, c))
#define MSUB(a, b, c) wasm_f32x4_sub(a, wasm_f32x4_mul(b, c))
#define SPLAT(v) wasm_f32x4_splat(v)
static void exp_neg_simd(const float *x, float *e, int n)
{
int i = 0;
for (; i + 4 <= n; i += 4) {
v128_t v = wasm_f32x4_neg(wasm_v128_load(x + i));
v128_t fx, p, v2, power;
v = wasm_f32x4_min(wasm_f32x4_max(v, SPLAT(-EXP_LIMIT)), SPLAT(EXP_LIMIT));
fx = wasm_f32x4_floor(MADD(SPLAT(0.5f), v, SPLAT(EXP_LOG2E)));
v = MSUB(v, fx, SPLAT(EXP_C1));
v = MSUB(v, fx, SPLAT(EXP_C2));
v2 = wasm_f32x4_mul(v, v);
p = SPLAT(EXP_P0);
p = MADD(SPLAT(EXP_P1), p, v);
p = MADD(SPLAT(EXP_P2), p, v);
p = MADD(SPLAT(EXP_P3), p, v);
p = MADD(SPLAT(EXP_P4), p, v);
p = MADD(SPLAT(EXP_P5), p, v);
p = wasm_f32x4_add(MADD(v, p, v2), SPLAT(1.0f));
power = wasm_i32x4_shl(
wasm_i32x4_add(wasm_i32x4_trunc_sat_f32x4(fx), wasm_i32x4_splat(127)), 23);
wasm_v128_store(e + i, wasm_f32x4_mul(p, power));
}
if (i < n)
exp_neg_tail(x + i, e + i, n - i);
}
#else
#define GA_SIMD "scalar"
#endif
const char *ga_kernel_name(void)
{
return GA_SIMD;
}
static inline void store(float *c, float value, float alpha, float beta)
{
*c = beta == 0.0f ? alpha * value : beta * *c + alpha * value;
}
#if defined(VW)
/* Accumulators are named variables: arrays of vectors get spilled by GCC. */
#if NR == 4
#define COLS(X) X(0) X(1) X(2) X(3)
#else
#define COLS(X) X(0) X(1)
#endif
#define DECLARE_4(j) \
const B_T *b_##j = b + (long)(j) * ldb; \
vf s0_##j = vf_zero(), s1_##j = vf_zero(), s2_##j = vf_zero(), s3_##j = vf_zero();
#define STEP_4(j) \
{ \
vf y = LOAD_B(b_##j + p); \
s0_##j = vf_fma(s0_##j, x0, y); \
s1_##j = vf_fma(s1_##j, x1, y); \
s2_##j = vf_fma(s2_##j, x2, y); \
s3_##j = vf_fma(s3_##j, x3, y); \
}
#define FINISH_4(j) \
out[0 * NR + (j)] = vf_sum(s0_##j); \
out[1 * NR + (j)] = vf_sum(s1_##j); \
out[2 * NR + (j)] = vf_sum(s2_##j); \
out[3 * NR + (j)] = vf_sum(s3_##j);
/* 6-row block, NEON only (32 registers): 6 x NR accumulators, NR columns, one A row. */
#define DECLARE_6(j) \
const B_T *b_##j = b + (long)(j) * ldb; \
vf u0_##j = vf_zero(), u1_##j = vf_zero(), u2_##j = vf_zero(); \
vf u3_##j = vf_zero(), u4_##j = vf_zero(), u5_##j = vf_zero();
#define LOAD_COL(j) vf y##j = LOAD_B(b_##j + p);
#define FMA_ROW_0(j) u0_##j = vf_fma(u0_##j, x, y##j);
#define FMA_ROW_1(j) u1_##j = vf_fma(u1_##j, x, y##j);
#define FMA_ROW_2(j) u2_##j = vf_fma(u2_##j, x, y##j);
#define FMA_ROW_3(j) u3_##j = vf_fma(u3_##j, x, y##j);
#define FMA_ROW_4(j) u4_##j = vf_fma(u4_##j, x, y##j);
#define FMA_ROW_5(j) u5_##j = vf_fma(u5_##j, x, y##j);
/* The empty asm keeps GCC from hoisting all six A loads (34 live registers, spills). */
#define ROW_STEP(i, ai) \
{ \
vf x = vf_load(ai + p); \
COLS(FMA_ROW_##i) \
__asm__ __volatile__("" ::: "memory"); \
}
#define FINISH_6(j) \
out[0 * NR + (j)] = vf_sum(u0_##j); \
out[1 * NR + (j)] = vf_sum(u1_##j); \
out[2 * NR + (j)] = vf_sum(u2_##j); \
out[3 * NR + (j)] = vf_sum(u3_##j); \
out[4 * NR + (j)] = vf_sum(u4_##j); \
out[5 * NR + (j)] = vf_sum(u5_##j);
#define B_T float
#define LOAD_B vf_load
#if defined(GA_DOT_6XN)
#define DOT_6XN
#endif
#define B_AT(p) (*(p))
#define SUFFIX f32
#include "ga_gemm_nt.inc"
#define B_T uint16_t
#define LOAD_B vf_load_f16
#define B_AT(p) ga_half_to_float(*(p))
#define SUFFIX f16
#if defined(GA_DOT_6XN)
#define DOT_6XN
#endif
#include "ga_gemm_nt.inc"
void ga_kernel_exp_neg(const float *x, float *e, int n)
{
exp_neg_simd(x, e, n);
}
#else
#define B_T float
#define B_AT(p) (*(p))
#define SUFFIX f32
#include "ga_gemm_nt.inc"
#define B_T uint16_t
#define B_AT(p) ga_half_to_float(*(p))
#define SUFFIX f16
#include "ga_gemm_nt.inc"
void ga_kernel_exp_neg(const float *x, float *e, int n)
{
for (int i = 0; i < n; i++)
e[i] = expf(-x[i]);
}
#endif
void ga_kernel_gemm_nt(int m, int n, int k, float alpha, const float *a, int lda,
const float *b, int ldb, float beta, float *c, int ldc)
{
gemm_nt_f32(m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
}
void ga_kernel_gemm_nt_f16(int m, int n, int k, float alpha, const float *a, int lda,
const uint16_t *b, int ldb, float beta, float *c, int ldc)
{
gemm_nt_f16(m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
}