| 1 | #cxx . . $ `<file>/external/whispercpp/simd-gemm.h`/pragma once |
| 2 | |
| 3 | // Computes C[M x N] += A[M x K] * B[K x N] |
| 4 | |
| 5 | #include "simd-mappings.h"cxx . . $ `<file>/external/whispercpp/simd-mappings.h`/ |
| 6 | |
| 7 | // TODO: add support for sizeless vector types |
| 8 | #if defined(GGML_SIMD) && !defined(__ARM_FEATURE_SVE) && !defined(__riscv_v_intrinsic) |
| 9 | |
| 10 | // TODO: untested on avx512 |
| 11 | // These are in units of GGML_F32_EPR |
| 12 | #if defined(__AVX512F__) || defined (__ARM_NEON__) |
| 13 | static constexpr int GEMM_RM = 4; |
| 14 | static constexpr int GEMM_RN = 4; // 16+4+1 = 25/32 |
| 15 | #elif defined(__AVX2__) || defined(__AVX__) |
| 16 | static constexpr int GEMM_RM = 6; |
| 17 | static constexpr int GEMM_RN = 2; // 12+2+1 = 15/16 |
| 18 | #else |
| 19 | static constexpr int GEMM_RM = 2; |
| 20 | static constexpr int GEMM_RN = 2; |
| 21 | #endif |
| 22 | |
| 23 | template <int RM, int RN> |
| 24 | static inline void simd_gemm_ukernel( |
| 25 | float * GGML_RESTRICT C, |
| 26 | const float * GGML_RESTRICT A, |
| 27 | const float * GGML_RESTRICT B, |
| 28 | int K, int N) |
| 29 | { |
| 30 | static constexpr int KN = GGML_F32_EPR; |
| 31 | |
| 32 | GGML_F32_VEC acc[RM][RN]; |
| 33 | for (int64_t i = 0; i < RM; i++) { |
| 34 | for (int r = 0; r < RN; r++) { |
| 35 | acc[i][r] = GGML_F32_VEC_LOAD(C + i * N + r * KN); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | for (int64_t kk = 0; kk < K; kk++) { |
| 40 | GGML_F32_VEC Bv[RN]; |
| 41 | for (int r = 0; r < RN; r++) { |
| 42 | Bv[r] = GGML_F32_VEC_LOAD(B + kk * N + r * KN); |
| 43 | } |
| 44 | for (int64_t i = 0; i < RM; i++) { |
| 45 | GGML_F32_VEC p = GGML_F32_VEC_SET1(A[i * K + kk]); |
| 46 | for (int r = 0; r < RN; r++) { |
| 47 | acc[i][r] = GGML_F32_VEC_FMA(acc[i][r], Bv[r], p); |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | for (int64_t i = 0; i < RM; i++) { |
| 53 | for (int r = 0; r < RN; r++) { |
| 54 | GGML_F32_VEC_STORE(C + i * N + r * KN, acc[i][r]); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // C[M x N] += A[M x K] * B[K x N] |
| 60 | static void simd_gemm( |
| 61 | float * GGML_RESTRICT C, |
| 62 | const float * GGML_RESTRICT A, |
| 63 | const float * GGML_RESTRICT B, |
| 64 | int M, int K, int N) |
| 65 | { |
| 66 | static constexpr int KN = GGML_F32_EPR; |
| 67 | |
| 68 | int64_t ii = 0; |
| 69 | for (; ii + GEMM_RM <= M; ii += GEMM_RM) { |
| 70 | int64_t jj = 0; |
| 71 | for (; jj + GEMM_RN * KN <= N; jj += GEMM_RN * KN) { |
| 72 | simd_gemm_ukernel<GEMM_RM, GEMM_RN>(C + jj, A, B + jj, K, N); |
| 73 | } |
| 74 | for (; jj + KN <= N; jj += KN) { |
| 75 | simd_gemm_ukernel<GEMM_RM, 1>(C + jj, A, B + jj, K, N); |
| 76 | } |
| 77 | for (; jj < N; jj++) { |
| 78 | for (int64_t i = 0; i < GEMM_RM; i++) { |
| 79 | float a = C[i * N + jj]; |
| 80 | for (int64_t kk = 0; kk < K; kk++) { |
| 81 | a += A[i + kk] * B[kk * N + jj]; |
| 82 | } |
| 83 | C[i * N + jj] = a; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | A += GEMM_RM * K; |
| 88 | C += GEMM_RM * N; |
| 89 | } |
| 90 | |
| 91 | // Tail rows: one at a time |
| 92 | for (; ii < M; ii++) { |
| 93 | int64_t jj = 0; |
| 94 | for (; jj + GEMM_RN * KN <= N; jj += GEMM_RN * KN) { |
| 95 | simd_gemm_ukernel<1, GEMM_RN>(C + jj, A, B + jj, K, N); |
| 96 | } |
| 97 | for (; jj + KN <= N; jj += KN) { |
| 98 | simd_gemm_ukernel<1, 1>(C + jj, A, B + jj, K, N); |
| 99 | } |
| 100 | for (; jj < N; jj++) { |
| 101 | float a = C[jj]; |
| 102 | for (int64_t kk = 0; kk < K; kk++) { |
| 103 | a += A[kk] * B[kk * N + jj]; |
| 104 | } |
| 105 | C[jj] = a; |
| 106 | } |
| 107 | |
| 108 | A += K; |
| 109 | C += N; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | #if defined(__GNUC__) && !defined(__clang__) |
| 114 | #pragma GCC diagnostic pop |
| 115 | #endif |
| 116 | |
| 117 | #else // scalar path |
| 118 | |
| 119 | static void simd_gemmcxx . . $ simd_gemm(d9e0d5679513bf24).( |
| 120 | float * GGML_RESTRICTcxx . . $ `external/whispercpp/ggml.h:2710:17`! C, |
| 121 | const float * GGML_RESTRICTcxx . . $ `external/whispercpp/ggml.h:2710:17`! A, |
| 122 | const float * GGML_RESTRICTcxx . . $ `external/whispercpp/ggml.h:2710:17`! B, |
| 123 | int M, int K, int N) |
| 124 | { |
| 125 | for (int64_tcxx . . $ int64_t# i = 0; i < M; i++) { |
| 126 | for (int64_tcxx . . $ int64_t# j = 0; j < N; j++) { |
| 127 | float sum = C[i * N + j]; |
| 128 | for (int64_tcxx . . $ int64_t# kk = 0; kk < K; kk++) { |
| 129 | sum += A[i * K + kk] * B[kk * N + j]; |
| 130 | } |
| 131 | C[i * N + j] = sum; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | #endif // GGML_SIMD |