#include "iqk_config.h"
#include "iqk_mul_mat.h"
#include "iqk_flash_impl.h"
#include "ggml.h"
#if defined IQK_IMPLEMENT && defined GGML_IQK_FLASH_ATTENTION
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cstdint>
#include <cstring>
#include <cmath>
#include <unordered_set>
namespace {
inline uint32_t simple_gcd(uint32_t a, uint32_t b) {
while (a != b) {
if (a > b) a -= b;
else b -= a;
}
return a;
}
inline void accumulate_qkv(int Dv, float& M, float& S, float Mj, float Sj, float * Racc, const float * R) {
if (Mj == -INFINITY) return;
if (Mj > M) {
if (M == -INFINITY) {
std::memcpy(Racc, R, Dv*sizeof(float));
S = Sj;
} else {
float c = exp(M - Mj);
S = c*S + Sj;
for (int i = 0; i < Dv; ++i) Racc[i] = c*Racc[i] + R[i];
}
M = Mj;
} else {
float c = exp(Mj - M);
S += c*Sj;
for (int i = 0; i < Dv; ++i) Racc[i] += c*R[i];
}
}
}
size_t iqk_fa_work_buffer_size(const struct ggml_tensor * dst, int nth) {
auto Q = dst->src[0];
auto K = dst->src[1];
auto V = dst->src[2];
auto indexer = dst->src[5];
if (indexer && indexer->type == GGML_TYPE_I32 && indexer->ne[0] < K->ne[1] && Q->ne[1] >= nth &&
Q->ne[3] == 1 && K->ne[3] == 1 && V->ne[3] == 1 && K->ne[2] == 1) {
auto row_size_k = ggml_row_size(K->type, K->ne[0]);
auto row_size_v = ggml_row_size(V->type, V->ne[0]);
auto work_size = (row_size_k + row_size_v + 64) * indexer->ne[0];
return work_size * nth;
}
int rk2 = Q->ne[2]/K->ne[2];
size_t size = 0;
if (Q->ne[1] >= 8 && K->type == GGML_TYPE_Q8_0) {
size = ggml_row_size(GGML_TYPE_Q8_0, K->ne[0]) * K->ne[1]*K->ne[2]*K->ne[3];
}
if (Q->ne[1] == 1 && Q->ne[3] == 1 && Q->ne[2]/K->ne[2] > 1 && nth >= 1 && K->ne[1]/32 > 1) {
if (K->ne[2] > 1) {
int gcd = simple_gcd(K->ne[2], nth);
int nth_k = nth/gcd;
int nek2_k = K->ne[2]/gcd;
int nchunk = nek2_k*K->ne[1]/32;
int npt = (nchunk + nth_k - 1)/nth_k;
int nk;
if (npt*nth_k == nchunk) {
nk = 32 * (K->ne[1]*K->ne[2]/(32*nth));
} else {
int nm = 1;
while (true) {
if (nm*4 >= npt) break;
nm *= 2;
}
nk = 32*nm;
}
int nkk = (K->ne[1] + nk - 1)/nk;
int nstep_k = K->ne[2]*nkk;
size_t result_size = (V->ne[0] + 16)*Q->ne[2]/K->ne[2]*sizeof(float);
size += nstep_k*result_size;
return size;
}
int nstep_k = K->ne[1]/32;
if (nstep_k >= 4*nth) {
auto size_thread = (V->ne[0] + 16)*rk2*sizeof(float);
size += size_thread*nth;
return size;
}
int gcd_k = simple_gcd(nstep_k, nth);
if (gcd_k >= 1) {
int nth_k = nth/gcd_k;
int nq_per_thread = (rk2 + nth_k - 1)/nth_k;
if (nq_per_thread > 1) {
auto size_thread = (V->ne[0] + 16)*nq_per_thread*sizeof(float);
size += size_thread*nth;
return size;
}
}
int rv2 = Q->ne[2] / V->ne[2];
if (Q->ne[1] == 1 && Q->ne[3] == 1 && rk2 > 1 && rk2 == rv2 && K->ne[1]*K->ne[2] >= 32*nth) {
auto result_size = (V->ne[0] + 16)*rk2*sizeof(float);
size += result_size*nth;
}
return size;
}
return size;
}
static inline const std::unordered_set<ggml_type> & supported_kv_types() {
#ifdef GGML_IQK_FA_ALL_QUANTS
static std::unordered_set<ggml_type> k_supported = {
GGML_TYPE_F16, GGML_TYPE_Q8_0, GGML_TYPE_Q8_KV, GGML_TYPE_Q6_0, GGML_TYPE_Q4_0, GGML_TYPE_Q4_1, GGML_TYPE_IQ4_NL
};
#else
static std::unordered_set<ggml_type> k_supported = {
GGML_TYPE_F16, GGML_TYPE_Q8_0, GGML_TYPE_Q8_KV, GGML_TYPE_Q6_0,
};
#endif
return k_supported;
}
static inline bool are_kv_types_supported(ggml_type type_k, ggml_type type_v) {
if (type_k == GGML_TYPE_BF16) {
if (type_v != type_k) {
return false;
}
#ifdef __AVX512BF16__
return true;
#else
return false;
#endif
}
auto & supported = supported_kv_types();
auto it_k = supported.find(type_k);
auto it_v = supported.find(type_v);
return it_k != supported.end() && it_v != supported.end();
}
extern "C" IQK_API bool iqk_flash_attn_noalibi(int type_q, int type_mask, float max_bias,
int neq3, int neq2, long nbq3, long nbq2,
int nek3, int nek2, long nbk3, long nbk2,
int nev3, int nev2, long nbv3, long nbv2,
int ne2, int ne1, long nb1,
int int_type_k_in, int int_type_v, int Dk, int Dv, int neq1, int nek1, int stride_q, int stride_k, int stride_v, int stride_m, const void * q, const void * k, const void * v, const void * mask, const void * sinks, float scale, float softcap, float * qkv, [[maybe_unused]] void * work_buffer_in, [[maybe_unused]] barrier_t barrier, [[maybe_unused]] void * barrier_data,
int ith, int nth, int n_swa, [[maybe_unused]] ggml_tensor * indexer) {
if (type_q != 0 || type_mask != 1 || max_bias > 0) return false;
if (indexer && indexer->type == GGML_TYPE_I32) {
if (indexer->ne[0] < nek1 && neq1 >= nth && neq3 == 1 && nek3 == 1 && nev3 == 1 && nek2 == 1) {
int npt = (neq1 + nth - 1)/nth;
int ith_mid = nth;
int neq1_this_thread = npt;
int first = ith*npt;
if (npt*nth > neq1) {
ith_mid = neq1 - nth*(npt - 1);
if (ith >= ith_mid) {
--neq1_this_thread;
first = ith_mid*npt + (ith - ith_mid)*neq1_this_thread;
}
}
auto row_size_k = ggml_row_size(ggml_type(int_type_k_in), Dk);
auto row_size_v = ggml_row_size(ggml_type(int_type_v ), Dv);
auto work_size = (row_size_k + row_size_v + 64) * indexer->ne[0];
auto work_k = (char *)work_buffer_in + ith*work_size;
auto work_v = work_k + row_size_k*indexer->ne[0];
auto work_m = (uint16_t *)(work_v + row_size_v*indexer->ne[0]);
int nkv = indexer->ne[0];
for (int iq = first; iq < first + neq1_this_thread; ++iq) {
auto idx = (const int *)((const char *)indexer->data + iq*indexer->nb[1]);
auto M = (const uint16_t *)((const char *)mask + iq*stride_m);
for (int j = 0; j < nkv; ++j) {
std::memcpy(work_k + row_size_k*j, ((const char *)k + idx[j]*stride_k), row_size_k);
std::memcpy(work_v + row_size_v*j, ((const char *)v + idx[j]*stride_v), row_size_v);
work_m[j] = M[idx[j]];
}
auto this_q = (const char *)q + iq*stride_q;
auto this_qkv = qkv + iq*ne1*nb1/sizeof(float);
if (!iqk_flash_attn_impl(int_type_k_in, int_type_v,
Dk, Dv, neq2, nkv, nbq2, row_size_k, row_size_v, 0, Dv,
(const float *)this_q, work_k, work_v, work_m, nullptr, 0,
scale, softcap,
this_qkv, nullptr, nullptr)) return false;
}
return true;
}
}
if (auto type_k = ggml_type(int_type_k_in), type_v = ggml_type(int_type_v); !are_kv_types_supported(type_k, type_v)) {
if (ith == 0) {
fprintf(stderr, "\n==================== K cache %s coupled with V cache %s is not a supported combination on the CPU backend.\n",
ggml_type_name(type_k), ggml_type_name(type_v));
auto & supported = supported_kv_types();
fprintf(stderr, "Supported types are:\n");
for (auto type : supported) {
fprintf(stderr, " %s\n", ggml_type_name(type));
}
fprintf(stderr, " Warning: ik_llama.cpp does not support Q5_0 or Q5_1 KV cache on the CPU.\n");
#ifdef __AVX512BF16__
fprintf(stderr, " %s, but only if K and V are both %s\n", ggml_type_name(GGML_TYPE_BF16), ggml_type_name(GGML_TYPE_BF16));
#endif
#ifndef GGML_IQK_FA_ALL_QUANTS
fprintf(stderr, " To enable q4_0, q4_1, and iq4_nl KV cache types, recompile with -DGGML_IQK_FA_ALL_QUANTS=ON\n");
#endif
}
barrier(barrier_data);
GGML_ABORT("Fatal error");
}
if (n_swa > 0 && mask) {
constexpr int kMinBatch = 256;
int ntokens = std::max(kMinBatch, neq1);
int nblock = (ntokens + n_swa + kMinBatch - 1)/kMinBatch;
int first = nek1 - nblock*kMinBatch;
if (first > 0) {
k = (const char *)k + int64_t(first)*stride_k;
v = (const char *)v + int64_t(first)*stride_v;
mask = (const uint16_t *)mask + first;
nek1 -= first;
}
}
int rk2 = neq2/nek2;
int rv2 = neq2/nev2;
int rk3 = neq3/nek3;
int rv3 = neq3/nev3;
int first_k = 0, last_k = nek1;
if (neq3 == 1 && rk2 > 1 && neq1 == 1 && nek1 > 256 && mask) {
auto umask = (const uint16_t *)mask;
for (; first_k < last_k; ++first_k) {
if (umask[first_k] == 0) break;
}
if (first_k == last_k) {
fprintf(stderr, "============================== %s: found empty attention mask: nek1 = %d, first_k = %d\n", __func__, nek1, first_k);
GGML_ABORT("Fatal error");
}
for (; last_k > first_k; --last_k) {
if (umask[last_k-1] == 0) break;
}
int non = 32*((last_k - first_k + 31)/32);
first_k = std::max(0, last_k - non);
last_k = std::min(first_k + non, nek1);
if (last_k - first_k <= 3*nek1/4 && (last_k - first_k)%32 == 0) {
k = (const void *)((const char *)k + first_k*stride_k);
v = (const void *)((const char *)v + first_k*stride_v);
mask = (const void *)((const uint16_t *)mask + first_k);
nek1 = last_k - first_k;
}
}
int int_type_k = int_type_k_in;
auto work_buffer = work_buffer_in;
if (neq1 >= 8) {
uint64_t row_size = 0;
work_buffer = iqk_repack_k(int_type_k, Dk, nek1, nek2, nek3, stride_k, nbk2, nbk3, k, work_buffer_in, ith, nth, int_type_k, row_size);
if (int_type_k != int_type_k_in) {
stride_k = row_size;
nbk2 = stride_k*nek1;
nbk3 = nbk2*nek2;
k = work_buffer_in;
barrier(barrier_data);
}
}
if (neq3 == 1 && rk2 > 1 && neq1 == 1 && nth >= 1 && nek1/32 > 1 && nek2 == 1) {
int nstep_k = nek1/32;
if (nstep_k >= 4*nth) {
int nstep_k_per_thread = (nstep_k + nth - 1)/nth;
int ith_mid = nth;
int nstep_k_this_thread = nstep_k_per_thread;
if (nstep_k_per_thread*nth > nstep_k) {
ith_mid = nstep_k - nth*(nstep_k_per_thread - 1);
if (ith >= ith_mid) --nstep_k_this_thread;
}
nstep_k_per_thread *= 32;
nstep_k_this_thread *= 32;
auto kv_offset = ith <= ith_mid ? ith*nstep_k_per_thread
: ith_mid*nstep_k_per_thread + (ith - ith_mid)*nstep_k_this_thread;
auto kth = (const char *)k + kv_offset*stride_k;
auto vth = (const char *)v + kv_offset*stride_v;
auto qth = (const char *)q;
auto mth = mask ? (const char *)mask + kv_offset*sizeof(uint16_t) : nullptr;
auto work = (char *)work_buffer;
auto size_thread = (Dv + 16)*rk2*sizeof(float);
auto result_buffer = work;
auto work_this_thread = (float *)(result_buffer + ith*size_thread);
if (!iqk_flash_attn_impl(int_type_k, int_type_v,
Dk, Dv, rk2, nstep_k_this_thread, nbq2, stride_k, stride_v, 0, Dv, (const float *)qth, (const void *)kth, (const void *)vth, (const void *)mth, nullptr, 0,
scale, softcap,
work_this_thread, work_this_thread + (Dv+0)*rk2, work_this_thread + (Dv+1)*rk2)) return false;
barrier(barrier_data);
for (int j = ith; j < rk2; j += nth) {
auto Racc = qkv + j*nb1/sizeof(float);
float M = -INFINITY, S = 0;
for (int jth = 0; jth < nth; ++jth) {
auto R = (const float *)(result_buffer + jth*size_thread);
auto Mj = R + Dv*rk2;
auto Sj = Mj + rk2;
R += j*Dv;
accumulate_qkv(Dv, M, S, Mj[j], Sj[j], Racc, R);
}
float norm = S > 0 ? 1/S : 1;
for (int i = 0; i < Dv; ++i) Racc[i] *= norm;
}
return true;
}
int gcd_k = simple_gcd(nstep_k, nth);
if (gcd_k >= 1) {
int nth_k = nth/gcd_k;
int ith_k = ith%gcd_k;
int ith_q = ith/gcd_k;
int nq_per_thread = (rk2 + nth_k - 1)/nth_k;
if (nq_per_thread > 1) {
int ith_mid = nth_k;
int nq_this_thread = nq_per_thread;
if (nq_per_thread*nth_k > rk2) {
ith_mid = rk2 - nth_k*(nq_per_thread - 1);
if (ith_q >= ith_mid) --nq_this_thread;
}
int j_mid = ith_mid*nq_per_thread;
auto work = (char *)work_buffer;
auto size_thread = (Dv + 16)*nq_per_thread*sizeof(float);
auto result_buffer = work;
auto kth = (const char *)k + ith_k*(nek1/gcd_k)*stride_k;
auto vth = (const char *)v + ith_k*(nek1/gcd_k)*stride_v;
auto q_offset = ith_q < ith_mid ? ith_q*nq_per_thread*nbq2 : (ith_mid*nq_per_thread + (ith_q - ith_mid)*nq_this_thread)*nbq2;
auto qth = (const char *)q + q_offset;
auto mth = mask ? (const char *)mask + ith_k*(nek1/gcd_k)*sizeof(uint16_t) : nullptr;
auto work_this_thread = (float *)(result_buffer + ith*size_thread);
if (!iqk_flash_attn_impl(int_type_k, int_type_v,
Dk, Dv, nq_this_thread, nek1/gcd_k, nbq2, stride_k, stride_v, 0, Dv, (const float *)qth, (const void *)kth, (const void *)vth, (const void *)mth, nullptr, 0,
scale, softcap,
work_this_thread, work_this_thread + (Dv+0)*nq_this_thread, work_this_thread + (Dv+1)*nq_this_thread)) return false;
barrier(barrier_data);
for (int j = ith; j < rk2; j += nth) {
auto Racc = qkv + j*nb1/sizeof(float);
float M = -INFINITY, S = 0;
int jth_first, jj, nq_this_j;
if (j < j_mid) {
jth_first = j/nq_per_thread;
jj = j%nq_per_thread;
nq_this_j = nq_per_thread;
} else {
jth_first = ith_mid + (j - j_mid)/(nq_per_thread-1);
jj = (j - j_mid)%(nq_per_thread-1);
nq_this_j = nq_per_thread - 1;
}
jth_first *= gcd_k;
for (int jth = jth_first; jth < jth_first + gcd_k; ++jth) {
auto R = (const float *)(result_buffer + jth*size_thread);
auto Mj = R + Dv*nq_this_j;
auto Sj = Mj + nq_this_j;
R += jj*Dv;
accumulate_qkv(Dv, M, S, Mj[jj], Sj[jj], Racc, R);
}
float norm = S > 0 ? 1/S : 1;
for (int i = 0; i < Dv; ++i) Racc[i] *= norm;
}
return true;
}
}
}
if (neq3 == 1 && rk2 > 1 && rk2 == rv2 && neq1 == 1 && nth >= 1 && nek2*nek1 >= 32*nth) {
auto result_size = (Dv + 16)*rk2*sizeof(float);
int gcd = simple_gcd(nek2, nth);
int nth_k = nth/gcd;
int nek2_k = nek2/gcd;
int nchunk = nek2_k*nek1/32;
int npt = (nchunk + nth_k - 1)/nth_k;
int nk;
if (npt*nth_k == nchunk) {
nk = 32 * (nek2*nek1/(32*nth));
} else {
int nm = 1;
while (true) {
if (nm*4 >= npt) break;
nm *= 2;
}
nk = 32*nm;
}
int nkk = (nek1 + nk - 1)/nk;
int nstep_k = nek2*nkk;
for (int istep_k = ith; istep_k < nstep_k; istep_k += nth) {
int ik02 = istep_k/nkk;
int ik01 = nk*(istep_k - ik02*nkk);
int this_nk = ik01 + nk <= nek1 ? nk : nek1 - ik01;
if (this_nk <= 0) break;
auto this_result = (float *)((char *)work_buffer + istep_k*result_size);
auto this_q = (const float *)((const char *)q + ik02*rk2*nbq2);
auto this_k = (const char *)k + ik01*stride_k + ik02*nbk2;
auto this_v = (const char *)v + ik01*stride_v + ik02*nbv2;
auto this_m = mask ? (const char *)mask + ik01*sizeof(uint16_t) : nullptr; if (!iqk_flash_attn_impl(int_type_k, int_type_v,
Dk, Dv, rk2, this_nk, nbq2, stride_k, stride_v, 0, Dv,
this_q, (const void *)this_k, (const void *)this_v, (const void *)this_m, nullptr, 0,
scale, softcap, this_result, this_result + (Dv+0)*rk2, this_result + (Dv+1)*rk2)) return false;
}
barrier(barrier_data);
for (int iq2 = ith; iq2 < neq2; iq2 += nth) {
int ik02 = iq2/rk2;
int il = iq2 - ik02*rk2;
auto Racc = qkv + iq2*nb1/sizeof(float);
float M = -INFINITY, S = 0;
for (int ikk = 0; ikk < nkk; ++ikk) {
int istep_k = ik02*nkk + ikk;
auto this_result = (float *)((char *)work_buffer + istep_k*result_size);
const float * R = this_result + il*Dv;
const float * Mj = this_result + Dv*rk2;
const float * Sj = Mj + rk2;
accumulate_qkv(Dv, M, S, Mj[il], Sj[il], Racc, R);
}
if (sinks) {
float s = ((const float *)sinks)[iq2];
if (s > M) {
float m = expf(M - s);
for (int i = 0; i < Dv; ++i) Racc[i] *= m;
S = S*m + 1;
} else {
S += expf(s - M);
}
}
float norm = S > 0 ? 1/S : 1;
for (int i = 0; i < Dv; ++i) Racc[i] *= norm;
}
return true;
}
int ntg = nth/simple_gcd(neq2*neq3, nth);
int neq1g = (neq1 + ntg - 1)/ntg;
int counter = 0;
for (int64_t iq3 = 0; iq3 < neq3; iq3++) {
for (int64_t iq2 = 0; iq2 < neq2; iq2++) {
auto sinksf = sinks ? (const float *)sinks + iq2 : nullptr;
if (counter++ % (nth/ntg) == ith/ntg) {
int iq1 = (ith%ntg)*neq1g;
int this_neq1 = std::min(neq1g, neq1-iq1);
if (this_neq1 > 0) {
if (!iqk_flash_attn_impl(int_type_k, int_type_v,
Dk, Dv, this_neq1, nek1, stride_q, stride_k, stride_v, stride_m, ne1*nb1/sizeof(float),
(const float *)((const char *)q + iq2*nbq2 + iq3*nbq3 + iq1*stride_q),
(const void *)((const char *)k + iq2/rk2*nbk2 + iq3/rk3*nbk3),
(const void *)((const char *)v + iq2/rv2*nbv2 + iq3/rv3*nbv3),
mask ? (const void *)((const char *)mask + iq1*stride_m) : nullptr, sinksf, 1,
scale, softcap,
(float *)((char *)qkv + (iq3*ne2*ne1 + iq2 + iq1*ne1)*nb1), nullptr, nullptr)) return false;
}
}
}
}
return true;
}
#else
bool iqk_flash_attn_noalibi([[maybe_unused]] int type_q, [[maybe_unused]] int type_mask, [[maybe_unused]] float max_bias,
[[maybe_unused]] int neq3, [[maybe_unused]] int neq2, [[maybe_unused]] long nbq3, [[maybe_unused]] long nbq2,
[[maybe_unused]] int nek3, [[maybe_unused]] int nek2, [[maybe_unused]] long nbk3, [[maybe_unused]] long nbk2,
[[maybe_unused]] int nev3, [[maybe_unused]] int nev2, [[maybe_unused]] long nbv3, [[maybe_unused]] long nbv2,
[[maybe_unused]] int ne2, [[maybe_unused]] int ne1, [[maybe_unused]] long nb1,
[[maybe_unused]] int type_k, [[maybe_unused]] int type_v, [[maybe_unused]] int Dk, [[maybe_unused]] int Dv, [[maybe_unused]] int nq, [[maybe_unused]] int nk, [[maybe_unused]] int stride_q, [[maybe_unused]] int stride_k, [[maybe_unused]] int stride_v, [[maybe_unused]] int stride_m, [[maybe_unused]] const void * q, [[maybe_unused]] const void * k, [[maybe_unused]] const void * v, [[maybe_unused]] const void * mask, [[maybe_unused]] float scale, [[maybe_unused]] float softcap, [[maybe_unused]] float * qkv, [[maybe_unused]] void * work_buffer, [[maybe_unused]] barrier_t barrier, [[maybe_unused]] void * barrier_data,
[[maybe_unused]] int ith, [[maybe_unused]] int nth, [[maybe_unused]] int n_swa, [[maybe_unused]] ggml_tensor * indexer) {
return false;
}
#endif