1#ifndef __MATH_MFMA_KERNEL_H__
2#define __MATH_MFMA_KERNEL_H__
102#include <hip/hip_runtime.h>
129#if defined(__gfx90a__) || defined(__gfx942__)
145 static int cached = -1;
148 hipDeviceProp_t prop;
150 if (hipGetDevice(&dev) == hipSuccess &&
151 hipGetDeviceProperties(&prop, dev) == hipSuccess &&
152 (strstr(prop.gcnArchName,
"gfx90a") != NULL ||
153 strstr(prop.gcnArchName,
"gfx942") != NULL)) {
156 if (hipMalloc(&d_flag,
sizeof(
int)) == hipSuccess) {
157 if (hipMemcpy(d_flag, &flag,
sizeof(
int),
158 hipMemcpyHostToDevice) == hipSuccess) {
161 if (hipGetLastError() == hipSuccess &&
162 hipMemcpy(&flag, d_flag,
sizeof(
int),
163 hipMemcpyDeviceToHost) == hipSuccess) {
198template < const
int LX >
200 if (
sizeof(
real) == 4) {
201 return (LX >= 2) && (LX <= 12);
203 return (
sizeof(
real) == 8) && (LX >= 4) && (LX <= 12);
276#define NEKO_MFMA_NWF_CANDIDATES 5
277#define NEKO_MFMA_TILE_CANDIDATES 2
278#define NEKO_MFMA_CANDIDATES \
279 (NEKO_MFMA_NWF_CANDIDATES * NEKO_MFMA_TILE_CANDIDATES)
280#define NEKO_MFMA_NWF(C) (1 << ((C) % NEKO_MFMA_NWF_CANDIDATES))
281#define NEKO_MFMA_TILE(C) ((C) / NEKO_MFMA_NWF_CANDIDATES)
282#define NEKO_MFMA_NTHRDS(C) dim3(64, NEKO_MFMA_NWF(C), 1)
339#define NEKO_MFMA_PAD_N(LX, SZ) (((LX) % 2 == 0) ? (NEKO_MFMA_PAD) : 0)
341#define NEKO_MFMA_PAD_N(LX, SZ) \
342 ((((LX) % 4 == 0) && !((LX) == 4 && (SZ) == 4)) ? 1 : 0)
345#define NEKO_MFMA_SJ_N(LX, SZ) ((LX) + NEKO_MFMA_PAD_N(LX, SZ))
346#define NEKO_MFMA_SK_N(LX, SZ) (NEKO_MFMA_SJ_N(LX, SZ) * (LX))
347#define NEKO_MFMA_CUBE_N(LX, SZ) (NEKO_MFMA_SK_N(LX, SZ) * (LX))
367#define NEKO_MFMA_SD_N(LX, SZ) NEKO_MFMA_SJ_N(LX, SZ)
368#define NEKO_MFMA_DMAT_N(LX, SZ) (NEKO_MFMA_SD_N(LX, SZ) * (LX))
383 if (
sizeof(
real) == 8) {
384#ifdef MFMA_F64_USE_16X16
390#ifdef MFMA_F32_USE_16X16
393 return sizeof(
real) == 4;
402 if (
sizeof(
real) == 8) {
403#ifdef MFMA_F64_USE_16X16
409#ifdef MFMA_F32_USE_16X16
448#define NEKO_MFMA_NGROUPS(LX) (((LX) * (LX) + 15) / 16)
456#define NEKO_MFMA_LDS_N(EB, LX, SZ) \
457 ((3 * NEKO_MFMA_DMAT_N(LX, SZ) + 4 * (EB) * NEKO_MFMA_CUBE_N(LX, SZ)) * (SZ))
458#define NEKO_MFMA_LDS_FITS(EB, LX, SZ) \
459 (NEKO_MFMA_LDS_N(EB, LX, SZ) <= NEKO_EB_MAX_LDS)
467#define NEKO_MFMA_EB_N(NWF, LX, SZ) \
468 ((NWF) / NEKO_MFMA_NGROUPS(LX) >= 16 && NEKO_MFMA_LDS_FITS(16, LX, SZ) ? 16 :\
469 (NWF) / NEKO_MFMA_NGROUPS(LX) >= 8 && NEKO_MFMA_LDS_FITS(8, LX, SZ) ? 8 : \
470 (NWF) / NEKO_MFMA_NGROUPS(LX) >= 4 && NEKO_MFMA_LDS_FITS(4, LX, SZ) ? 4 : \
471 (NWF) / NEKO_MFMA_NGROUPS(LX) >= 2 && NEKO_MFMA_LDS_FITS(2, LX, SZ) ? 2 : 1)
472#define NEKO_MFMA_EB(LX, C) \
473 NEKO_MFMA_EB_N(NEKO_MFMA_NWF(C), LX, sizeof(real))
475#define NEKO_MFMA_WPE(LX, C) (NEKO_MFMA_NWF(C) / NEKO_MFMA_EB(LX, C))
493#define NEKO_MFMA_POW2_FLOOR(X) \
494 ((X) >= 16 ? 16 : (X) >= 8 ? 8 : (X) >= 4 ? 4 : (X) >= 2 ? 2 : 1)
495#ifdef MFMA_F32_USE_16X16
496#define NEKO_MFMA_NEL_N(EB, LX, SZ, TILE) 1
498#define NEKO_MFMA_NEL_N(EB, LX, SZ, TILE) \
499 (((SZ) == 4 && (TILE) == 0) ? \
500 ((EB) < NEKO_MFMA_POW2_FLOOR(64 / ((LX) * (LX))) ? \
501 (EB) : NEKO_MFMA_POW2_FLOOR(64 / ((LX) * (LX)))) : 1)
503#define NEKO_MFMA_NEL(LX, C) \
504 NEKO_MFMA_NEL_N(NEKO_MFMA_EB(LX, C), LX, sizeof(real), NEKO_MFMA_TILE(C))
518#define NEKO_MFMA_SPT_N(WPE, LX, SZ) \
519 ((NEKO_MFMA_CUBE_N(LX, SZ) + (WPE) * 64 - 1) / ((WPE) * 64))
520#define NEKO_MFMA_SPT(LX, C) \
521 NEKO_MFMA_SPT_N(NEKO_MFMA_WPE(LX, C), LX, sizeof(real))
546#ifndef NEKO_MFMA_VECTOR_GREG_VGPRS
547#define NEKO_MFMA_VECTOR_GREG_VGPRS 64
549#define NEKO_MFMA_VECTOR_GREG_N(SPT, SZ) \
550 ((7 * (SPT) * ((SZ) / 4)) <= NEKO_MFMA_VECTOR_GREG_VGPRS)
551#define NEKO_MFMA_VECTOR_GREG(LX, C) \
552 NEKO_MFMA_VECTOR_GREG_N(NEKO_MFMA_SPT(LX, C), sizeof(real))
554#define NEKO_MFMA_NBLCKS(NELV, LX, C) \
555 dim3(((NELV) + NEKO_MFMA_EB(LX, C) - 1) / NEKO_MFMA_EB(LX, C), 1, 1)
572 const char *
v = getenv(
"NEKO_MFMA_TUNE");
575 return (atoi(
v) != 0);
594 const char *
v = getenv(
"NEKO_MFMA_NWF");
595 const char *t = getenv(
"NEKO_MFMA_TILE");
598 if (
v == NULL && t == NULL) {
602 nwf = (
v != NULL) ? atoi(
v) : 0;
603 tile = (t != NULL) ? atoi(t) : 0;
633#define NEKO_TUNE_LOG_MFMA(LX, T3) \
635 for (int c = 0; c < NEKO_MFMA_CANDIDATES; c++) { \
636 if ((T3)[c] >= NEKO_TUNE_INIT) { continue; } \
637 sprintf(neko_log_buf, "MFMA %s %2dwf %-2de x%-2d %-5s: %9.2f us/call", \
638 mfma_tile_name(c), NEKO_MFMA_NWF(c), NEKO_MFMA_EB(LX, c), \
639 (int) NEKO_MFMA_NEL(LX, c), \
640 NEKO_MFMA_PAD_N(LX, sizeof(real)) ? "pad" : "plain", \
641 NEKO_TUNE_US((T3)[c], iters)); \
642 log_message(neko_log_buf); \
655#define NEKO_TUNE_LOG_MFMA_VEC(LX, T3) \
657 for (int c = 0; c < NEKO_MFMA_CANDIDATES; c++) { \
658 if ((T3)[c] >= NEKO_TUNE_INIT) { continue; } \
659 sprintf(neko_log_buf, \
660 "MFMA %s %2dwf %-2de x%-2d %-5s %-4s: %9.2f us/call", \
661 mfma_tile_name(c), NEKO_MFMA_NWF(c), NEKO_MFMA_EB(LX, c), \
662 (int) NEKO_MFMA_NEL(LX, c), \
663 NEKO_MFMA_PAD_N(LX, sizeof(real)) ? "pad" : "plain", \
664 NEKO_MFMA_VECTOR_GREG(LX, c) ? "reg" : "glob", \
665 NEKO_TUNE_US((T3)[c], iters)); \
666 log_message(neko_log_buf); \
670#if defined(__gfx90a__) || defined(__gfx942__)
673typedef double mfma_f64x4 __attribute__((ext_vector_type(4)));
674typedef float mfma_f32x4 __attribute__((ext_vector_type(4)));
681template<
typename T >
685struct mfma_traits< double > {
686 typedef mfma_f64x4 acc_t;
687 __device__ __forceinline__
static acc_t mma(
double a,
double b, acc_t c) {
688 return __builtin_amdgcn_mfma_f64_16x16x4f64(a, b, c, 0, 0, 0);
690 __device__ __forceinline__
static int out_row(
const int g,
const int r) {
696struct mfma_traits< float > {
697 typedef mfma_f32x4 acc_t;
698 __device__ __forceinline__
static acc_t mma(
float a,
float b, acc_t c) {
699 return __builtin_amdgcn_mfma_f32_16x16x4f32(a, b, c, 0, 0, 0);
701 __device__ __forceinline__
static int out_row(
const int g,
const int r) {
716template< const
int LX, const
int PAD >
718 enum { SJ = LX + PAD,
719 SK = (LX + PAD) * LX,
720 SIZE = (LX + PAD) * LX * LX,
724 DSIZE = (LX + PAD) * LX };
731template< const
int LX, const
int PAD >
732__device__ __forceinline__
int mfma_dmat_idx(
const int row,
const int col) {
733 return row + mfma_cube<LX, PAD>::SD * col;
741template< const
int LX, const
int AXIS, const
int PAD = 0 >
742__device__ __forceinline__
int mfma_cube_idx(
const int p,
const int n) {
743 typedef mfma_cube<LX, PAD> C;
744 const int a = n % LX;
745 const int b = n / LX;
746 if (AXIS == 0)
return p + C::SJ * a + C::SK * b;
747 if (AXIS == 1)
return a + C::SJ * p + C::SK * b;
748 return a + C::SJ * b + C::SK * p;
769template< const
int LX, const
int PAD >
770__device__ __forceinline__
int mfma_slot_point(
const int s) {
771 typedef mfma_cube<LX, PAD> C;
775 const int k = s / C::SK;
776 const int rem = s - k * C::SK;
777 const int j = rem / C::SJ;
778 const int i = rem -
j * C::SJ;
780 return (
i < LX && k < LX) ? (
i + LX *
j + LX * LX * k) : -1;
799template<
typename T,
const int LX,
const int AXIS,
800 const bool TRANSPOSE,
const bool ACCUM,
const int NWF = 1,
802__device__ __forceinline__
803void mfma_contract(T * __restrict__ out,
804 const T * __restrict__ dmat,
805 const T * __restrict__ in,
806 const int lane,
const int wf = 0) {
807 typedef mfma_traits<T> mma_t;
808 const int g = lane >> 4;
809 const int c = lane & 15;
810 const int NTILES = (LX * LX + 15) / 16;
811 const int KSTEPS = (LX + 3) / 4;
812 const int NPASS = (NTILES + NWF - 1) / NWF;
819 for (
int ks = 0; ks < KSTEPS; ++ks) {
820 const int l = ks * 4 + g;
822 if (c < LX && l < LX)
823 areg[ks] = TRANSPOSE ? dmat[mfma_dmat_idx<LX, PAD>(l, c)]
824 : dmat[mfma_dmat_idx<LX, PAD>(c, l)];
828 for (
int p = 0; p < NPASS; ++p) {
829 const int nt = wf + p * NWF;
831 const int n = nt * 16 + c;
832 typename mma_t::acc_t acc = {0, 0, 0, 0};
834 for (
int ks = 0; ks < KSTEPS; ++ks) {
835 const int l = ks * 4 + g;
837 if (l < LX && n < LX * LX)
838 b = in[mfma_cube_idx<LX, AXIS, PAD>(l, n)];
839 acc = mma_t::mma(areg[ks], b, acc);
841 const T dvals[4] = { acc[0], acc[1], acc[2], acc[3] };
843 for (
int r = 0; r < 4; ++r) {
844 const int m = mma_t::out_row(g, r);
845 if (m < LX && n < LX * LX) {
846 const int idx = mfma_cube_idx<LX, AXIS, PAD>(m, n);
847 if (ACCUM) out[idx] += dvals[r];
848 else out[idx] = dvals[r];
896template<
const int LX,
const int AXIS,
897 const bool TRANSPOSE,
const bool ACCUM,
const int NWF = 1,
899__device__ __forceinline__
900void mfma_contract_4x4(
double * __restrict__ out,
901 const double * __restrict__ dmat,
902 const double * __restrict__ in,
903 const int lane,
const int wf = 0) {
904 const int lo = lane & 3;
905 const int gemm = (lane >> 2) & 3;
906 const int kq = lane >> 4;
907 const int MT = (LX + 3) / 4;
908 const int KSTEPS = (LX + 3) / 4;
909 const int NGROUPS = (LX * LX + 15) / 16;
910 const int NPASS = (NGROUPS + NWF - 1) / NWF;
933 for (
int gp = 0; gp < NPASS; ++gp) {
934 const int ng = wf + gp * NWF;
937 const int n = ng * 16 + gemm * 4 + lo;
940 for (
int mt = 0; mt < MT; ++mt)
943 for (
int ks = 0; ks < KSTEPS; ++ks) {
944 const int l = ks * 4 + kq;
946 if (l < LX && n < LX * LX)
947 b = in[mfma_cube_idx<LX, AXIS, PAD>(l, n)];
949 for (
int mt = 0; mt < MT; ++mt) {
950 const int m_a = mt * 4 + lo;
952 if (m_a < LX && l < LX)
953 a = TRANSPOSE ? dmat[mfma_dmat_idx<LX, PAD>(l, m_a)]
954 : dmat[mfma_dmat_idx<LX, PAD>(m_a, l)];
955 acc[mt] = __builtin_amdgcn_mfma_f64_4x4x4f64(a, b, acc[mt], 0, 0, 0);
959 for (
int mt = 0; mt < MT; ++mt) {
960 const int m_d = mt * 4 + kq;
961 if (m_d < LX && n < LX * LX) {
962 const int idx = mfma_cube_idx<LX, AXIS, PAD>(m_d, n);
963 if (ACCUM) out[idx] += acc[mt];
964 else out[idx] = acc[mt];
1026template<
const int LX,
const int AXIS,
1027 const bool TRANSPOSE,
const bool ACCUM,
const int NWF = 1,
1028 const int PAD = 0,
const int NEL = 1 >
1029__device__ __forceinline__
1030void mfma_contract_4x4x1(
float * __restrict__ out,
1031 const float * __restrict__ dmat,
1032 const float * __restrict__ in,
1033 const int lane,
const int wf = 0) {
1034 typedef mfma_cube<LX, PAD> C;
1035 const int blk = lane >> 2;
1036 const int lo = lane & 3;
1037 const int MT = (LX + 3) / 4;
1038 const int LX2 = LX * LX;
1039 const int NCOL = NEL * LX2;
1040 const int NG = (NCOL + 63) / 64;
1041 const int NPASS = (NG + NWF - 1) / NWF;
1042 const mfma_f32x4 zero = {0.0f, 0.0f, 0.0f, 0.0f};
1048 for (
int mt = 0; mt < MT; ++mt) {
1049 const int m_a = mt * 4 + lo;
1051 for (
int l = 0; l < LX; ++l) {
1054 areg[mt][l] = TRANSPOSE ? dmat[mfma_dmat_idx<LX, PAD>(l, m_a)]
1055 : dmat[mfma_dmat_idx<LX, PAD>(m_a, l)];
1060 for (
int gp = 0; gp < NPASS; ++gp) {
1061 const int ng = wf + gp * NWF;
1063 const int ncol = ng * 64 + 4 * blk + lo;
1064 const bool live = (ncol < NCOL);
1065 const int q = live ? (ncol / LX2) : 0;
1066 const int n = ncol - q * LX2;
1067 const float * __restrict__ inq = in + q * C::SIZE;
1068 float * __restrict__ outq = out + q * C::SIZE;
1071 for (
int mt = 0; mt < MT; ++mt)
1074 for (
int l = 0; l < LX; ++l) {
1077 b = inq[mfma_cube_idx<LX, AXIS, PAD>(l, n)];
1079 for (
int mt = 0; mt < MT; ++mt)
1080 acc[mt] = __builtin_amdgcn_mfma_f32_4x4x1f32(areg[mt][l], b,
1084 for (
int mt = 0; mt < MT; ++mt) {
1086 for (
int r = 0; r < 4; ++r) {
1087 const int m_d = mt * 4 + r;
1088 if (live && m_d < LX) {
1089 const int idx = mfma_cube_idx<LX, AXIS, PAD>(m_d, n);
1090 if (ACCUM) outq[idx] += acc[mt][r];
1091 else outq[idx] = acc[mt][r];
1127template<
typename T,
const int LX,
const int AXIS,
1128 const bool TRANSPOSE,
const bool ACCUM,
const int NWF = 1,
1129 const int TILE = 0,
const int PAD = 0,
const int NEL = 1 >
1130struct mfma_contract_sel {
1131 static_assert(NEL == 1,
1132 "only the single precision 4x4x1 tile batches elements");
1133 __device__ __forceinline__
1134 static void run(T * __restrict__ out,
const T * __restrict__ dmat,
1135 const T * __restrict__ in,
const int lane,
1137 mfma_contract<T, LX, AXIS, TRANSPOSE, ACCUM, NWF, PAD>(out, dmat, in,
1144template<
const int LX,
const int AXIS,
const bool TRANSPOSE,
const bool ACCUM,
1145 const int NWF,
const int PAD,
const int NEL >
1146struct mfma_contract_sel<double, LX, AXIS, TRANSPOSE, ACCUM, NWF, 0, PAD, NEL> {
1147 static_assert(NEL == 1,
1148 "only the single precision 4x4x1 tile batches elements");
1149 __device__ __forceinline__
1150 static void run(
double * __restrict__ out,
const double * __restrict__ dmat,
1151 const double * __restrict__ in,
const int lane,
1153#ifdef MFMA_F64_USE_16X16
1154 mfma_contract<double, LX, AXIS, TRANSPOSE, ACCUM, NWF, PAD>(out, dmat, in,
1157 mfma_contract_4x4<LX, AXIS, TRANSPOSE, ACCUM, NWF, PAD>(out, dmat, in,
1166template<
const int LX,
const int AXIS,
const bool TRANSPOSE,
const bool ACCUM,
1167 const int NWF,
const int PAD,
const int NEL >
1168struct mfma_contract_sel<double, LX, AXIS, TRANSPOSE, ACCUM, NWF, 1, PAD, NEL> {
1169 static_assert(NEL == 1,
1170 "only the single precision 4x4x1 tile batches elements");
1171 __device__ __forceinline__
1172 static void run(
double * __restrict__ out,
const double * __restrict__ dmat,
1173 const double * __restrict__ in,
const int lane,
1175 mfma_contract<double, LX, AXIS, TRANSPOSE, ACCUM, NWF, PAD>(out, dmat, in,
1183template<
const int LX,
const int AXIS,
const bool TRANSPOSE,
const bool ACCUM,
1184 const int NWF,
const int PAD,
const int NEL >
1185struct mfma_contract_sel<float, LX, AXIS, TRANSPOSE, ACCUM, NWF, 0, PAD, NEL> {
1186 __device__ __forceinline__
1187 static void run(
float * __restrict__ out,
const float * __restrict__ dmat,
1188 const float * __restrict__ in,
const int lane,
1190#ifdef MFMA_F32_USE_16X16
1191 static_assert(NEL == 1,
1192 "NEKO_MFMA_NEL_N is 1 under MFMA_F32_USE_16X16");
1193 mfma_contract<float, LX, AXIS, TRANSPOSE, ACCUM, NWF, PAD>(out, dmat, in,
1196 mfma_contract_4x4x1<LX, AXIS, TRANSPOSE, ACCUM, NWF, PAD, NEL>(out, dmat,
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ v
static __global__ void hip_mfma_arch_probe(int *flag)
static bool mfma_lx_supported()
static const char * mfma_tile_name(const int c)
#define NEKO_MFMA_NWF_CANDIDATES
#define NEKO_MFMA_CANDIDATES
static int neko_mfma_pin()
static int neko_mfma_candidates()
#define NEKO_MFMA_TILE_CANDIDATES
static bool hip_have_mfma()
#define NEKO_MFMA_TILE(C)
static int neko_mfma_sweep()
static bool mfma_tile_offered(const int c)