/**
 * Test 6-warp specialized FMHA kernel for HD=16/64/128/256.
 * Compile with -DHD_VAL=64 etc.
 */

#include <cuda_runtime.h>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>

#ifndef HD_VAL
#define HD_VAL 64
#endif

#include "dsv4/kernels/attention/fmha_common.cuh"
#include "dsv4/kernels/attention/fmha_umma_desc.cuh"

using namespace dsv4::kernels::attention;

static bf16_t f32_to_bf16_host(float f) { uint32_t u; memcpy(&u,&f,4); return (uint16_t)(u>>16); }
static float bf16_to_f32_host(bf16_t h) { uint32_t u=(uint32_t)h<<16; float f; memcpy(&f,&u,4); return f; }

constexpr int HD = HD_VAL;
constexpr int SK = 128;
constexpr int NKT_QK = HD / MMA_K_BF16;
constexpr int NKT_PV = SK / MMA_K_BF16;
constexpr int TILE_SZ = 128 * MMA_K_BF16;
constexpr int N_NSUB = HD / 16;
constexpr int V_SUB_SZ = 256;
constexpr int TMEM_N = (HD <= 128) ? 128 : 256;

// Include the kernel
#include "dsv4/kernels/attention/fmha_6warp.cuh"

int main() {
    printf("=== 6-warp FMHA HD=%d ===\n", HD);
    const float SCALE = 1.0f / sqrtf((float)HD);

    bf16_t* h_q = (bf16_t*)malloc(HD*sizeof(bf16_t));
    bf16_t* h_k = (bf16_t*)malloc(SK*HD*sizeof(bf16_t));
    bf16_t* h_v = (bf16_t*)malloc(HD*SK*sizeof(bf16_t));
    bf16_t* h_o = (bf16_t*)calloc(HD, sizeof(bf16_t));

    srand(42);
    for (int d=0;d<HD;d++) h_q[d] = f32_to_bf16_host((float)(rand()%100)/100.0f-0.5f);
    for (int i=0;i<SK*HD;i++) h_k[i] = f32_to_bf16_host((float)(rand()%100)/100.0f-0.5f);
    for (int i=0;i<HD*SK;i++) h_v[i] = f32_to_bf16_host((float)(rand()%100)/100.0f-0.5f);

    bf16_t *d_q,*d_k,*d_v,*d_o;
    cudaMalloc(&d_q, HD*sizeof(bf16_t));
    cudaMalloc(&d_k, SK*HD*sizeof(bf16_t));
    cudaMalloc(&d_v, HD*SK*sizeof(bf16_t));
    cudaMalloc(&d_o, HD*sizeof(bf16_t));
    cudaMemcpy(d_q, h_q, HD*sizeof(bf16_t), cudaMemcpyHostToDevice);
    cudaMemcpy(d_k, h_k, SK*HD*sizeof(bf16_t), cudaMemcpyHostToDevice);
    cudaMemcpy(d_v, h_v, HD*SK*sizeof(bf16_t), cudaMemcpyHostToDevice);

    // Compute reference
    float o_ref[HD];
    {
        float s[SK];
        for (int j=0;j<SK;j++) {
            float dot = 0.0f;
            for (int d=0;d<HD;d++) dot += bf16_to_f32_host(h_q[d]) * bf16_to_f32_host(h_k[j*HD+d]);
            s[j] = dot * SCALE;
        }
        float mx = -INFINITY;
        for (int j=0;j<SK;j++) mx = fmaxf(mx, s[j]);
        float sm = 0.0f;
        for (int j=0;j<SK;j++) { s[j] = expf(s[j]-mx); sm += s[j]; }
        for (int j=0;j<SK;j++) s[j] /= sm;
        for (int d=0;d<HD;d++) {
            float ov = 0.0f;
            for (int j=0;j<SK;j++) ov += s[j] * bf16_to_f32_host(h_v[d*SK+j]);
            o_ref[d] = ov;
        }
    }

    int smem = (4 + 8 + 16 + TILE_SZ*2 + TILE_SZ*2 + TILE_SZ*2 + V_SUB_SZ*2 + SK*4 + 256 + 127) & ~127;
    printf("SMEM: %d bytes (%.1f KB), 6 warps (192 threads), TMEM: %d cols\n", smem, smem/1024.0f, TMEM_N);

    if (smem > 48 * 1024) {
        cudaFuncSetAttribute(fmha_6warp_kernel<HD>, cudaFuncAttributeMaxDynamicSharedMemorySize, smem);
    }
    fmha_6warp_kernel<HD><<<1, 192, smem>>>(d_q, d_k, d_v, d_o, SK, SCALE);

    cudaError_t launch_err = cudaGetLastError();
    if (launch_err != cudaSuccess) { printf("LAUNCH ERROR: %s\n", cudaGetErrorString(launch_err)); return 1; }

    cudaError_t err = cudaDeviceSynchronize();
    if (err != cudaSuccess) { printf("CUDA ERROR: %s\n", cudaGetErrorString(err)); return 1; }

    cudaMemcpy(h_o, d_o, HD*sizeof(bf16_t), cudaMemcpyDeviceToHost);

    printf("O[0..7] MMA:  "); for(int d=0;d<min(8,HD);d++) printf("%.6f ",bf16_to_f32_host(h_o[d])); printf("\n");
    printf("O[0..7] ref:  "); for(int d=0;d<min(8,HD);d++) printf("%.6f ",o_ref[d]); printf("\n");

    float cs=0,na=0,nb=0;
    for (int d=0;d<HD;d++) {
        float a=bf16_to_f32_host(h_o[d]),b=o_ref[d];
        if(fabsf(b)>1e-4f) { cs+=a*b; na+=a*a; nb+=b*b; }
    }
    cs /= (sqrtf(na)*sqrtf(nb)+1e-10f);
    printf("Filtered cosine: %.8f\n", cs);
    printf("Test %s\n", cs > 0.999f ? "PASSED" : "FAILED");

    cudaFree(d_q); cudaFree(d_k); cudaFree(d_v); cudaFree(d_o);
    free(h_q); free(h_k); free(h_v); free(h_o);
    return cs > 0.999f ? 0 : 1;
}
