/** * Standalone test for tcgen05.mma QK GEMM verification. * Compares tensor-core Q@K^T output against CPU reference. */ #include "dsv4/kernels/attention/fmha_common.cuh" #include "dsv4/kernels/attention/fmha_umma_desc.cuh" #include "dsv4/kernels/attention/fmha_qk_verify.cuh" #include #include #include #include #include using namespace dsv4::kernels::attention; uint16_t f32_to_bf16_cpu(float f) { uint32_t u; memcpy(&u,&f,4); return (uint16_t)(u>>16); } float bf16_to_f32_cpu(uint16_t h) { uint32_t u = ((uint32_t)h)<<16; float f; memcpy(&f,&u,4); return f; } float cosine_sim(const float* a, const float* b, int n) { float dot=0, na=0, nb=0; for(int i=0;i 0 ? dot/d : 0; } int main() { printf("=== tcgen05.mma QK GEMM Verification ===\n\n"); constexpr int HD = 64; int s_k = 128; int B = 1, H = 1; float scale = 1.0f / sqrtf((float)HD); // CPU reference: Q(1, HD) @ K^T(HD, s_k) → S(1, s_k) float *hq=(float*)malloc(B*H*HD*4), *hk=(float*)malloc(B*s_k*HD*4); float *hs_ref=(float*)malloc(B*s_k*4); srand(42); for(int i=0;i<<>>( dq, dk, ds_out, H*HD, s_k*HD, s_k, scale ); cudaError_t err = cudaDeviceSynchronize(); if (err != cudaSuccess) { printf("❌ Kernel failed: %s\n", cudaGetErrorString(err)); return 1; } // Read back float *hs_gpu = (float*)malloc(s_k*4); cudaMemcpy(hs_gpu, ds_out, s_k*4, cudaMemcpyDeviceToHost); // Compare // Compare — but also print raw values for debugging printf("Scalar QK[0]: %.6f (ref: %.6f)\n", hs_gpu[0], hs_ref[0]); printf("MMA QK[0]: %.6f\n", hs_gpu[3]); printf("sQ[0]: %.6f sK[0]: %.6f\n", hs_gpu[1], hs_gpu[2]); float max_diff = 0; int nan_count = 0; for(int j=0;j 0.99f && nan_count == 0); printf("QK GEMM: cos %.6f max_diff %.6f nan=%d %s\n", cos, max_diff, nan_count, pass ? "✅" : "❌"); if (!pass || max_diff > 0.1f) { printf(" GPU[:4] = %.6f %.6f %.6f %.6f\n", hs_gpu[0], hs_gpu[1], hs_gpu[2], hs_gpu[3]); printf(" Ref[:4] = %.6f %.6f %.6f %.6f\n", hs_ref[0], hs_ref[1], hs_ref[2], hs_ref[3]); } cudaFree(dq); cudaFree(dk); cudaFree(ds_out); free(hq); free(hk); free(hs_ref); free(hs_gpu); free(hqb); free(hkb); printf("\n%s\n", pass ? "✅ QK GEMM PASSED!" : "❌ QK GEMM FAILED"); return pass ? 0 : 1; }