Repository: ggerganov/llama.cpp
File: ggml/src/ggml-cpu/simd-gemm.h (C/C++)

Code before:
    for (int64_t kk = 0; kk < K; kk++) {
        GGML_F32_VEC Bv[RN];
        for (int r = 0; r < RN; r++) {
            Bv[r] = GGML_F32_VEC_LOAD(B + kk * N + jj + r * KN);
        }
        for (int i = 0; i < RM; i++) {
            GGML_F32_VEC p = GGML_F32_VEC_SET1(A[(ii + i) * K + kk]);
            for (int r = 0; r < RN; r++) {
                acc[i][r] = GGML_F32_VEC_FMA(acc[i][r], Bv[r], p);
            }
        }
    }

    for (int i = 0; i < RM; i++) {
        for (int r = 0; r < RN; r++) {
            GGML_F32_VEC_STORE(C + (ii + i) * N + jj + r * KN, acc[i][r]);
        }
    }
}

// C[M x N] += A[M x K] * B[K x N]
static void simd_gemm(
    float       * GGML_RESTRICT C,
    const float * GGML_RESTRICT A,
    const float * GGML_RESTRICT B,
    int M, int64_t K, int64_t N)
{
    static constexpr int KN = GGML_F32_EPR;

    int ii = 0;
    for (; ii + GEMM_RM <= M; ii += GEMM_RM) {
        int64_t jj = 0;
        for (; jj + GEMM_RN * KN <= N; jj += GEMM_RN * KN) {
            simd_gemm_ukernel<GEMM_RM, GEMM_RN>(C, A, B, K, N, ii, jj);
        }
        for (; jj + KN <= N; jj += KN) {
            simd_gemm_ukernel<GEMM_RM, 1>(C, A, B, K, N, ii, jj);
        }
        for (; jj < N; jj++) {
            for (int i = 0; i < GEMM_RM; i++) {
                float a = C[(ii + i) * N + jj];
                for (int64_t kk = 0; kk < K; kk++) {
                    a += A[(ii + i) * K + kk] * B[kk * N + jj];
                }
                C[(ii + i) * N + jj] = a;
            }
        }
    }

    // Tail rows: one at a time
    for (; ii < M; ii++) {

Review comment (suggestion):
The int types are a bit inconsistent. Either use `int` or `int64_t` everywhere:

```suggestion
    int64_t M, int64_t K, int64_t N)
```

Code after:
            acc[i][r] = GGML_F32_VEC_LOAD(C + (ii + i) * N + jj + r * KN);
        }
    }

    for (int64_t kk = 0; kk < K; kk++) {
        GGML_F32_VEC Bv[RN];
        for (int r = 0; r < RN; r++) {
            Bv[r] = GGML_F32_VEC_LOAD(B + kk * N + jj + r * KN);
        }
        for (int i = 0; i < RM; i++) {
            GGML_F32_VEC p = GGML_F32_VEC_SET1(A[(ii + i) * K + kk]);
            for (int r = 0; r < RN; r++) {
                acc[i][r] = GGML_F32_VEC_FMA(acc[i][r], Bv[r], p);
            }
        }
    }

    for (int i = 0; i < RM; i++) {
        for (int r = 0; r < RN; r++) {
            GGML_F32_VEC_STORE(C + (ii + i) * N + jj + r * KN, acc[i][r]);
        }
    }
}

// C[M x N] += A[M x K] * B[K x N]
static void simd_gemm(
    float       * GGML_RESTRICT C,
    const float * GGML_RESTRICT A,
    const float * GGML_RESTRICT B,
    int64_t M, int64_t K, int64_t N)
{
    static constexpr int KN = GGML_F32_EPR;

    int64_t ii = 0;
    for (; ii + GEMM_RM <= M; ii += GEMM_RM) {
        int64_t jj = 0;
        for (; jj + GEMM_RN * KN <= N; jj += GEMM_RN * KN) {
            simd_gemm_ukernel<GEMM_RM, GEMM_RN>(C, A, B, K, N, ii, jj);
        }
        for (; jj + KN <= N; jj += KN) {
            simd_gemm_ukernel<GEMM_RM, 1>(C, A, B, K, N, ii, jj);
        }
        for (; jj < N; jj++) {
            for (int i = 0; i < GEMM_RM; i++) {
                float a = C[(ii + i) * N + jj];
                for (int64_t kk = 0; kk < K; kk++) {
                    a += A[(ii + i) * K + kk] * B[kk * N + jj];
                }
                C[(ii + i) * N + jj] = a;
            }
        }