// Copyright 2023 Tencent
// SPDX-License-Identifier: BSD-3-Clause

#version 450

#define LOCAL_MEMORY_UNROLL_INCH 8

layout (constant_id = 0) const int attn_mask = 0;
layout (constant_id = 1) const int M = 0;
layout (constant_id = 2) const int N = 0;
layout (constant_id = 3) const int K = 0;
layout (constant_id = 4) const int B = 0;
layout (constant_id = 5) const int attn_mask_dims = 0;

layout (binding = 0) readonly buffer q_blob { sfpvec4 q_blob_data[]; };
layout (binding = 1) readonly buffer k_blob { sfpvec4 k_blob_data[]; };
layout (binding = 2) writeonly buffer qkcross_blob { sfpvec4 qkcross_blob_data[]; };
layout (binding = 3) readonly buffer attn_mask_blob { sfpvec4 attn_mask_blob_data[]; };

layout (push_constant) uniform parameter
{
    int M;
    int N;
    int K;
    int B;
    int attn_mask_dims;
} p;

#if NCNN_shader_local_memory
shared lfpvec4 tmp_q[8][LOCAL_MEMORY_UNROLL_INCH][4];
shared lfpvec4 tmp_k[8][LOCAL_MEMORY_UNROLL_INCH];
#endif

void main()
{
    int gx = int(gl_GlobalInvocationID.x);
    int gy = int(gl_GlobalInvocationID.y);
    int gz = int(gl_GlobalInvocationID.z);

#if !NCNN_shader_local_memory
    if (gx >= psc(N) || gy >= psc(M) || gz >= psc(B))
        return;
#endif

    afpvec4 sum = afpvec4(0.f);

    if (attn_mask == 1)
    {
        if (psc(attn_mask_dims) == 3)
        {
            const int mi = gz * psc(M) * psc(N) + gy * psc(N) + gx;
            sum = buffer_ld4(attn_mask_blob_data, mi);
        }
        else
        {
            const int mi = gy * psc(N) + gx;
            sum = buffer_ld4(attn_mask_blob_data, mi);
        }
    }

#if NCNN_shader_local_memory
    const int NN = psc(K);

    const int lx = int(gl_LocalInvocationID.x);
    const int ly = int(gl_LocalInvocationID.y);

    int ai = (gz * psc(M) * psc(K) + lx * psc(M) + gy) * 4;
    int bi = gz * psc(N) * psc(K) + ly * psc(N) + gx;

    int k = 0;
    for (; k + (LOCAL_MEMORY_UNROLL_INCH - 1) < NN; k += LOCAL_MEMORY_UNROLL_INCH)
    {
        {
            tmp_q[ly][lx][0] = sfp2lfpvec4(q_blob_data[ai]);
            tmp_q[ly][lx][1] = sfp2lfpvec4(q_blob_data[ai + 1]);
            tmp_q[ly][lx][2] = sfp2lfpvec4(q_blob_data[ai + 2]);
            tmp_q[ly][lx][3] = sfp2lfpvec4(q_blob_data[ai + 3]);
        }

        {
            tmp_k[lx][ly] = sfp2lfpvec4(k_blob_data[bi]);
        }

        barrier();

        for (int k4 = 0; k4 < LOCAL_MEMORY_UNROLL_INCH; k4++)
        {
            afpvec4 q0 = lfp2afpvec4(tmp_q[ly][k4][0]);
            afpvec4 q1 = lfp2afpvec4(tmp_q[ly][k4][1]);
            afpvec4 q2 = lfp2afpvec4(tmp_q[ly][k4][2]);
            afpvec4 q3 = lfp2afpvec4(tmp_q[ly][k4][3]);

            afpvec4 k0 = lfp2afpvec4(tmp_k[lx][k4]);

            sum.r += dot(q0, k0);
            sum.g += dot(q1, k0);
            sum.b += dot(q2, k0);
            sum.a += dot(q3, k0);
        }

        ai += LOCAL_MEMORY_UNROLL_INCH * psc(M) * 4;
        bi += LOCAL_MEMORY_UNROLL_INCH * psc(N);

        barrier();
    }

    if (k < NN)
    {
        const int remain = NN - k;

        if (lx < remain)
        {
            tmp_q[ly][lx][0] = sfp2lfpvec4(q_blob_data[ai]);
            tmp_q[ly][lx][1] = sfp2lfpvec4(q_blob_data[ai + 1]);
            tmp_q[ly][lx][2] = sfp2lfpvec4(q_blob_data[ai + 2]);
            tmp_q[ly][lx][3] = sfp2lfpvec4(q_blob_data[ai + 3]);
        }

        if (ly < remain)
        {
            tmp_k[lx][ly] = sfp2lfpvec4(k_blob_data[bi]);
        }

        barrier();

        for (int k4 = 0; k4 < remain; k4++)
        {
            afpvec4 q0 = lfp2afpvec4(tmp_q[ly][k4][0]);
            afpvec4 q1 = lfp2afpvec4(tmp_q[ly][k4][1]);
            afpvec4 q2 = lfp2afpvec4(tmp_q[ly][k4][2]);
            afpvec4 q3 = lfp2afpvec4(tmp_q[ly][k4][3]);

            afpvec4 k0 = lfp2afpvec4(tmp_k[lx][k4]);

            sum.r += dot(q0, k0);
            sum.g += dot(q1, k0);
            sum.b += dot(q2, k0);
            sum.a += dot(q3, k0);
        }
    }
#else
    for (int k = 0; k < psc(K); k++)
    {
        const int ai = (gz * psc(M) * psc(K) + k * psc(M) + gy) * 4;
        afpvec4 q0 = buffer_ld4(q_blob_data, ai);
        afpvec4 q1 = buffer_ld4(q_blob_data, ai + 1);
        afpvec4 q2 = buffer_ld4(q_blob_data, ai + 2);
        afpvec4 q3 = buffer_ld4(q_blob_data, ai + 3);

        const int bi = gz * psc(N) * psc(K) + k * psc(N) + gx;
        afpvec4 k0 = buffer_ld4(k_blob_data, bi);

        sum.r += dot(q0, k0);
        sum.g += dot(q1, k0);
        sum.b += dot(q2, k0);
        sum.a += dot(q3, k0);
    }
#endif

#if NCNN_shader_local_memory
    if (gx >= psc(N) || gy >= psc(M) || gz >= psc(B))
        return;
#endif

    const int gi = gz * psc(M) * psc(N) + gy * psc(N) + gx;
    buffer_st4(qkcross_blob_data, gi, sum);
}
