1#ifndef __MATH_DMMA_TMA_KERNEL_H__
2#define __MATH_DMMA_TMA_KERNEL_H__
95#include <cuda_runtime.h>
107#define DMMA_NG_OPGRAD 9
111#define DMMA_NG_CONV1 13
119#if defined(CUDART_VERSION) && (CUDART_VERSION >= 12000)
120#define NEKO_TMA_TOOLKIT 1
122#define NEKO_TMA_TOOLKIT 0
135#ifndef NEKO_TMA_ARCH_COMPILED
136#if defined(__CUDA_ARCH_LIST__)
139 const int arch[] = { __CUDA_ARCH_LIST__ };
141 for (
int i = 0;
i < (int) (
sizeof(arch)/
sizeof(arch[0]));
i++) {
142 if (arch[
i] >= 900 && arch[
i] < 1000) {
157 return (NEKO_TMA_ARCH_COMPILED != 0);
168 static int cached = -1;
176 }
else if (cudaGetDevice(&dev) == cudaSuccess &&
177 cudaGetDeviceProperties(&prop, dev) == cudaSuccess) {
178 cached = (prop.major == 9) ? 1 : 0;
198template< const
int LX >
213template< const
int LX >
228template< const
int LX >
241template< const
int LX >
247template< const
int LX >
258template< const
int LX >
295#define NEKO_DMMA_TMA_BATCH_SMEM ((int) sizeof(dmma_tma_batch_smem))
300 "dmma tma batch block exceeds the opt-in shared memory ceiling");
303 "dmma tma batch bulk copy targets must stay 128 byte aligned");
314 static int cached = -1;
322 }
else if (cudaGetDevice(&dev) == cudaSuccess &&
323 cudaDeviceGetAttribute(&optin,
324 cudaDevAttrMaxSharedMemoryPerBlockOptin,
325 dev) == cudaSuccess) {
372#define NEKO_OPGRAD_TMA_SMEM ((int) sizeof(opgrad_tma_smem))
401#define NEKO_CONV1_TMA_SMEM ((int) sizeof(conv1_tma_smem))
405 "tma block exceeds the opt-in shared memory ceiling");
410 "tma bulk copy targets must stay 128 byte aligned");
434template< const
int LX >
452 return ((((uintptr_t) p) & (uintptr_t) 15) == 0);
456 const void *
h1,
const void *
g11,
457 const void *
g22,
const void *
g33,
458 const void *
g12,
const void *
g13,
470 const void *
aw,
const void *
u,
471 const void *
v,
const void *
w,
472 const void *
h1,
const void *
g11,
473 const void *
g22,
const void *
g33,
474 const void *
g12,
const void *
g13,
484 const void *
dr,
const void *
ds,
528 const void *
dr,
const void *
ds,
538 const void *
vx,
const void *
vy,
557 const char *
v = getenv(
"NEKO_DMMA_TMA_NW");
572#define NEKO_TUNE_LOG_DMMA_TMA_BATCH(LX, T5) \
574 for (int c = 0; c < NEKO_DMMA_CANDIDATES; c++) { \
575 if ((T5)[c] >= NEKO_TUNE_INIT) { continue; } \
577 sprintf(lbl_, "TMAB %dw", NEKO_DMMA_NW(c)); \
578 sprintf(neko_log_buf, "%-13s: %9.2f us/call", lbl_, \
579 NEKO_TUNE_US((T5)[c], iters)); \
580 log_message(neko_log_buf); \
585#define NEKO_TUNE_LOG_DMMA_TMA(LX, T4) \
587 for (int c = 0; c < NEKO_DMMA_CANDIDATES; c++) { \
588 if ((T4)[c] >= NEKO_TUNE_INIT) { continue; } \
590 sprintf(lbl_, "TMA %dw", NEKO_DMMA_NW(c)); \
591 sprintf(neko_log_buf, "%-13s: %9.2f us/call", lbl_, \
592 NEKO_TUNE_US((T4)[c], iters)); \
593 log_message(neko_log_buf); \
597#if defined(__CUDA_ARCH__) && (__CUDA_ARCH__ >= 900) && \
598 (__CUDA_ARCH__ < 1000) && NEKO_TMA_TOOLKIT
613__device__ __forceinline__
static unsigned int tma_smem(
const void *p)
615 return (
unsigned int) __cvta_generic_to_shared(p);
622__device__ __forceinline__
static unsigned long long tma_gmem(
const void *p)
624 return (
unsigned long long) __cvta_generic_to_global(p);
630__device__ __forceinline__
static void tma_barrier_init(
unsigned long long *bar,
633 asm volatile(
"mbarrier.init.shared::cta.b64 [%0], %1;"
634 ::
"r"(tma_smem(bar)),
"r"(count) :
"memory");
641__device__ __forceinline__
static void tma_expect(
unsigned long long *bar,
644 asm volatile(
"mbarrier.arrive.expect_tx.shared::cta.b64 _, [%0], %1;"
645 ::
"r"(tma_smem(bar)),
"r"(bytes) :
"memory");
649__device__ __forceinline__
static void tma_load(
void *dst,
const void *src,
651 unsigned long long *bar)
653 asm volatile(
"cp.async.bulk.shared::cluster.global"
654 ".mbarrier::complete_tx::bytes [%0], [%1], %2, [%3];"
655 ::
"r"(tma_smem(dst)),
"l"(tma_gmem(src)),
"r"(bytes),
656 "r"(tma_smem(bar)) :
"memory");
667__device__ __forceinline__
static void tma_wait(
unsigned long long *bar,
675 "mbarrier.try_wait.parity.shared::cta.b64 p, [%1], %2;\n"
676 "selp.b32 %0, 1, 0, p;\n"
678 :
"=r"(done) :
"r"(tma_smem(bar)),
"r"(phase) :
"memory");
688__device__ __forceinline__
static void tma_fence_shared()
690 asm volatile(
"fence.proxy.async.shared::cta;" :::
"memory");
696__device__ __forceinline__
static void tma_store(
void *dst,
const void *src,
699 asm volatile(
"cp.async.bulk.global.shared::cta.bulk_group [%0], [%1], %2;"
700 ::
"l"(tma_gmem(dst)),
"r"(tma_smem(src)),
"r"(bytes)
708__device__ __forceinline__
static void tma_store_wait()
710 asm volatile(
"cp.async.bulk.commit_group;" :::
"memory");
711 asm volatile(
"cp.async.bulk.wait_group 0;" :::
"memory");
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ g23
__global__ void T *__restrict__ T *__restrict__ aw
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ g22
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ g13
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ w
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ g12
__global__ void const T *__restrict__ u
__global__ void T *__restrict__ av
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ g33
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ v
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ g11
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ h1
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dtdy
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dtdx
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dtdz
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ jacinv
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dsdz
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ drdz
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ drdx
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dsdx
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dsdy
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ drdy
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ ds
__global__ void const T *__restrict__ x
__global__ void const T *__restrict__ const T *__restrict__ dr
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dt
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ vz
__global__ void const T *__restrict__ const T *__restrict__ vx
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ vy
#define NEKO_DMMA_CANDIDATES
static bool dmma_tma_aligned(const void *w, const void *u, const void *h1, const void *g11, const void *g22, const void *g33, const void *g12, const void *g13, const void *g23)
static bool dmma_tma_vector_aligned(const void *au, const void *av, const void *aw, const void *u, const void *v, const void *w, const void *h1, const void *g11, const void *g22, const void *g33, const void *g12, const void *g13, const void *g23)
static bool cuda_have_tma_conv1()
static bool dmma_tma_cdtp_aligned(const void *dtx, const void *x, const void *dr, const void *ds, const void *dt)
static bool cuda_have_tma_batch()
static bool dmma_tma_batch_lx_supported()
#define NEKO_OPGRAD_TMA_SMEM
static bool dmma_tma_vector_lx_supported()
static bool tma_arch_compiled()
#define NEKO_CONV1_TMA_SMEM
static bool dmma_tma_cdtp_lx_supported()
static int cuda_tma_smem_optin()
static bool dmma_tma_dudxyz_lx_supported()
static bool cuda_have_tma_opgrad()
static bool dmma_tma_conv1_lx_supported()
static bool dmma_tma_lx_supported()
static int neko_dmma_tma_pin()
static bool dmma_tma_metrics_aligned(const void *drdx, const void *dsdx, const void *dtdx, const void *drdy, const void *dsdy, const void *dtdy, const void *drdz, const void *dsdz, const void *dtdz)
#define NEKO_DMMA_TMA_BATCH_SMEM
static bool dmma_tma_opgrad_aligned(const void *u, const void *drdx, const void *dsdx, const void *dtdx, const void *drdy, const void *dsdy, const void *dtdy, const void *drdz, const void *dsdz, const void *dtdz)
static bool dmma_tma_opgrad_lx_supported()
static bool dmma_tma_conv1_aligned(const void *du, const void *u, const void *vx, const void *vy, const void *vz, const void *jacinv, const void *drdx, const void *dsdx, const void *dtdx, const void *drdy, const void *dsdy, const void *dtdy, const void *drdz, const void *dsdz, const void *dtdz)
static bool dmma_tma_dudxyz_aligned(const void *du, const void *u, const void *dr, const void *ds, const void *dt, const void *jacinv)
static bool cuda_have_tma()
static bool dmma_tma_ptr_aligned(const void *p)