/** * Full UMMA FMHA — HD=16, SK=128, T=1 (decode) * * Pipeline: * Q×K^T (UMMA SS) → softmax (TMEM read/write) → PV (register math) * * For decode (T=1), P is (1, SK) — only row 0 is non-zero. * PV = P[0,:] × V is just a weighted sum: O[d] = Σ P[0,j] × V[d,j]. * No UMMA needed for PV — compute directly in registers. * * TMEM: 128 columns for S/P. O computed in registers. */ #include #include #include #include #include #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 = 16, SK = 128, BLOCK_MN = 128; __global__ void __launch_bounds__(128) test_fmha_hd16(const bf16_t* q, const bf16_t* k, const bf16_t* v, bf16_t* o_out, float* o_scalar, float scale) { const int tid = threadIdx.x, wid = tid / 32, lane = tid % 32; extern __shared__ char sbuf[]; uint32_t* sTmemBase = (uint32_t*)sbuf; bf16_t* sQ = (bf16_t*)(((uintptr_t)(sbuf + 4) + 15) & ~(uintptr_t)15); bf16_t* sK = sQ + 128 * 16 + 4096; float* sQ_row = (float*)(sK + 128 * 16); for (int d = tid; d < HD; d += 128) sQ_row[d] = bf16_to_f32(q[d]); if (wid == 1) tmem_alloc(__cvta_generic_to_shared(sTmemBase), 128); __syncthreads(); uint32_t tb = *sTmemBase; write_q_to_smem(sQ, q); write_k_to_smem(sK, k); bf16_t* sQ_pad = sQ + 128 * 16; for (int i = tid; i < 4096; i += 128) sQ_pad[i] = 0; __syncthreads(); // STEP 1: QK GEMM uint64_t desc_q = make_umma_desc_kmajor_none(__cvta_generic_to_shared(sQ), BLOCK_MN); uint64_t desc_k = make_umma_desc_kmajor_none(__cvta_generic_to_shared(sK), BLOCK_MN); uint32_t idesc = make_idesc(BLOCK_MN, BLOCK_MN); if (lane == 0) umma_ss_f16(tb, desc_q, desc_k, idesc, false); asm volatile("tcgen05.fence::after_thread_sync;" ::: "memory"); __syncthreads(); // STEP 2: Softmax — read S, compute P, write P to TMEM if (wid == 0) { float s_vals[SK], row_max = -INFINITY; for (int n = 0; n < SK / 8; n++) { float tmp[8]; asm volatile("tcgen05.ld.sync.aligned.32x32b.x8.b32 {%0,%1,%2,%3,%4,%5,%6,%7},[%8];" : "=f"(tmp[0]),"=f"(tmp[1]),"=f"(tmp[2]),"=f"(tmp[3]),"=f"(tmp[4]),"=f"(tmp[5]),"=f"(tmp[6]),"=f"(tmp[7]) : "r"(tb + n*8)); asm volatile("tcgen05.wait::ld.sync.aligned;"); if (lane == 0) for (int c=0;c<8;c++) { s_vals[n*8+c] = tmp[c]*scale; row_max = fmaxf(row_max, tmp[c]*scale); } } row_max = wmax(row_max); float row_sum = 0.0f; if (lane == 0) for (int j=0;j>>(d_q, d_k, d_v, d_o, d_o_scalar, SCALE); 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); cudaMemcpy(h_o_scalar, d_o_scalar, HD*sizeof(float), cudaMemcpyDeviceToHost); printf("O[0..15] MMA: "); for(int d=0;d0 ? max_diff/max_val : max_diff; float cos_sim = 0, norm_a=0, norm_b=0; for (int d=0;d 0.999f ? "PASSED" : "FAILED"); cudaFree(d_q); cudaFree(d_k); cudaFree(d_v); cudaFree(d_o); cudaFree(d_o_scalar); free(h_q); free(h_k); free(h_v); free(h_o); free(h_o_scalar); return cos_sim > 0.999f ? 0 : 1; }