Publish PyC CUDA kernel lab and performance summary
Browse files19 documented kernel-lab entries, prototype sources, manifests, and a concise H100 performance summary.
- PERFORMANCE_SUMMARY.md +17 -0
- README.md +38 -0
- kernels/prototypes/ada/gemm/kernel.cu +336 -0
- kernels/prototypes/ada/gemm_k128_warp32/kernel.cu +388 -0
- kernels/prototypes/ada/gemm_k128_warp32_dyn/kernel.cu +428 -0
- kernels/prototypes/ada/gemm_k32/kernel.cu +394 -0
- kernels/prototypes/ada/gemm_k32_warp32/kernel.cu +388 -0
- kernels/prototypes/ada/gemm_k32_wide/kernel.cu +388 -0
- kernels/prototypes/ada/gemm_k64_t44/kernel.cu +400 -0
- kernels/prototypes/ada/gemm_k64_warp32/kernel.cu +388 -0
- kernels/prototypes/ada/gemm_k64_warp32_acc2/kernel.cu +404 -0
- kernels/prototypes/ada/gemm_k64_warp32_async/kernel.cu +505 -0
- kernels/prototypes/ada/gemm_k64_warp32_ilp2/kernel.cu +397 -0
- kernels/prototypes/ada/gemm_k64_warp32_lb1/kernel.cu +388 -0
- kernels/prototypes/ada/gemm_k64_warp32_store2/kernel.cu +398 -0
- kernels/prototypes/ada/gemm_vec/kernel.cu +374 -0
- kernels/prototypes/ada/tensor_core/kernel.cu +327 -0
- kernels/prototypes/baseline/matmul/kernel.cu +30 -0
- kernels/prototypes/experimental/tokenizer_matmul/kernel.cu +248 -0
- kernels/prototypes/hopper/cublaslt_bf16/kernel.cu +331 -0
- kernels/prototypes/hopper/tensor_core/kernel.cu +450 -0
- kernels/prototypes/hopper/tensor_core_async/kernel.cu +612 -0
- kernels/prototypes/hopper/tensor_core_wgmma/README.md +28 -0
- manifests/lab_kernels.json +156 -0
- manifests/registry_kernels.json +157 -0
PERFORMANCE_SUMMARY.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Performance summary
|
| 2 |
+
|
| 3 |
+
These figures are selected from the PyC H100 kernel-lab campaign and are
|
| 4 |
+
included as orientation, not as universal benchmark claims.
|
| 5 |
+
|
| 6 |
+
| Lane | Shape | Result |
|
| 7 |
+
| --- | --- | --- |
|
| 8 |
+
| Hopper WMMA FP16 | 1024^3 | 0.105 ms, 20.473 TFLOPS |
|
| 9 |
+
| Hopper WMMA BF16 | 1024^3 | 0.102 ms, 21.024 TFLOPS |
|
| 10 |
+
| Hopper cuBLASLt BF16 control | 4096^3 | 0.162 ms, 846.466 TFLOPS |
|
| 11 |
+
| Hopper async square K64 | 4096^3 | 0.9252 ms in the captured profile |
|
| 12 |
+
|
| 13 |
+
The engineering progression is: shared-memory tiling and reuse, WMMA Tensor
|
| 14 |
+
Core execution, BF16/FP16 comparison, `cp.async` double buffering, CTA shape
|
| 15 |
+
and warp-work assignment, K-stage depth, and finally a cuBLASLt control lane.
|
| 16 |
+
Always interpret a result together with GPU model, architecture, CUDA version,
|
| 17 |
+
matrix shape, warmup/repeat policy, and correctness mode.
|
README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
tags:
|
| 4 |
+
- cuda
|
| 5 |
+
- gpu-optimization
|
| 6 |
+
- kernels
|
| 7 |
+
- gemm
|
| 8 |
+
- hpc
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
# PyC CUDA kernel lab
|
| 12 |
+
|
| 13 |
+
This repository documents 19 CUDA kernel-lab entries from PyC. It is a source
|
| 14 |
+
and evidence release, not a compiled binary distribution and not a claim that
|
| 15 |
+
all entries are wired into PyC runtime dispatch.
|
| 16 |
+
|
| 17 |
+
## Contents
|
| 18 |
+
|
| 19 |
+
- `kernels/prototypes/`: standalone CUDA prototype sources.
|
| 20 |
+
- `manifests/kernels.json`: the 19-entry lab catalog, including build/run commands.
|
| 21 |
+
- `manifests/kernels.json`: the catalog mirrored into the registry release.
|
| 22 |
+
- `PERFORMANCE_SUMMARY.md`: selected H100 campaign measurements and the optimization progression.
|
| 23 |
+
|
| 24 |
+
## Optimization themes
|
| 25 |
+
|
| 26 |
+
The progression covers shared-memory tiling, WMMA Tensor Core execution, BF16
|
| 27 |
+
versus FP16, `cp.async` double buffering, CTA shape, K-stage depth, warp work
|
| 28 |
+
assignment, and cuBLASLt as a hardware-library ceiling/control.
|
| 29 |
+
|
| 30 |
+
Performance numbers are campaign-specific measurements. They should be read
|
| 31 |
+
with the GPU, CUDA toolchain, matrix shape, correctness mode, and timing method
|
| 32 |
+
from the accompanying evidence; they are not universal benchmarks.
|
| 33 |
+
|
| 34 |
+
## Reproduce
|
| 35 |
+
|
| 36 |
+
The commands in `manifests/kernels.json` use `{nvcc}`, `{source}`, and
|
| 37 |
+
`{build_dir}` placeholders. Replace them with a CUDA 12.x toolchain, a suitable
|
| 38 |
+
Hopper or Ada GPU, and a local build directory before running.
|
kernels/prototypes/ada/gemm/kernel.cu
ADDED
|
@@ -0,0 +1,336 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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ADA_THREAD_TILE_M 4
|
| 14 |
+
#define PYC_ADA_THREAD_TILE_N 4
|
| 15 |
+
|
| 16 |
+
typedef struct {
|
| 17 |
+
int m;
|
| 18 |
+
int n;
|
| 19 |
+
int k;
|
| 20 |
+
int warmup;
|
| 21 |
+
int iters;
|
| 22 |
+
} ada_gemm_config;
|
| 23 |
+
|
| 24 |
+
static int check_cuda(cudaError_t status, const char* what) {
|
| 25 |
+
if (status != cudaSuccess) {
|
| 26 |
+
fprintf(stderr, "%s failed: %s\n", what, cudaGetErrorString(status));
|
| 27 |
+
return -1;
|
| 28 |
+
}
|
| 29 |
+
return 0;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
static void fill_matrix(float* data, int rows, int cols, float scale) {
|
| 33 |
+
int i;
|
| 34 |
+
for (i = 0; i < rows * cols; ++i) {
|
| 35 |
+
int pattern = (i * 17 + rows * 13 + cols * 7) % 31;
|
| 36 |
+
data[i] = ((float)pattern - 15.0f) * scale;
|
| 37 |
+
}
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
static void reference_gemm(
|
| 41 |
+
const float* a,
|
| 42 |
+
const float* b,
|
| 43 |
+
float* c,
|
| 44 |
+
int m,
|
| 45 |
+
int n,
|
| 46 |
+
int k) {
|
| 47 |
+
int row;
|
| 48 |
+
for (row = 0; row < m; ++row) {
|
| 49 |
+
int col;
|
| 50 |
+
for (col = 0; col < n; ++col) {
|
| 51 |
+
float acc = 0.0f;
|
| 52 |
+
int kk;
|
| 53 |
+
for (kk = 0; kk < k; ++kk) {
|
| 54 |
+
acc += a[row * k + kk] * b[kk * n + col];
|
| 55 |
+
}
|
| 56 |
+
c[row * n + col] = acc;
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
__launch_bounds__(PYC_ADA_THREADS_X * PYC_ADA_THREADS_Y, 2)
|
| 62 |
+
__global__ void ada_fp32_tiled_gemm(
|
| 63 |
+
const float* __restrict__ a,
|
| 64 |
+
const float* __restrict__ b,
|
| 65 |
+
float* __restrict__ c,
|
| 66 |
+
int m,
|
| 67 |
+
int n,
|
| 68 |
+
int k) {
|
| 69 |
+
__shared__ float a_tile[PYC_ADA_BLOCK_M][PYC_ADA_BLOCK_K];
|
| 70 |
+
__shared__ float b_tile[PYC_ADA_BLOCK_K][PYC_ADA_BLOCK_N];
|
| 71 |
+
|
| 72 |
+
const int thread_row = threadIdx.y;
|
| 73 |
+
const int thread_col = threadIdx.x;
|
| 74 |
+
const int block_row = blockIdx.y * PYC_ADA_BLOCK_M;
|
| 75 |
+
const int block_col = blockIdx.x * PYC_ADA_BLOCK_N;
|
| 76 |
+
const int lane_linear = thread_row * blockDim.x + thread_col;
|
| 77 |
+
const int a_loads_per_thread = (PYC_ADA_BLOCK_M * PYC_ADA_BLOCK_K) / (PYC_ADA_THREADS_X * PYC_ADA_THREADS_Y);
|
| 78 |
+
const int b_loads_per_thread = (PYC_ADA_BLOCK_K * PYC_ADA_BLOCK_N) / (PYC_ADA_THREADS_X * PYC_ADA_THREADS_Y);
|
| 79 |
+
float accum[PYC_ADA_THREAD_TILE_M][PYC_ADA_THREAD_TILE_N];
|
| 80 |
+
int row_fragment = thread_row * PYC_ADA_THREAD_TILE_M;
|
| 81 |
+
int col_fragment = thread_col * PYC_ADA_THREAD_TILE_N;
|
| 82 |
+
int kk_base;
|
| 83 |
+
int i;
|
| 84 |
+
int j;
|
| 85 |
+
|
| 86 |
+
for (i = 0; i < PYC_ADA_THREAD_TILE_M; ++i) {
|
| 87 |
+
for (j = 0; j < PYC_ADA_THREAD_TILE_N; ++j) {
|
| 88 |
+
accum[i][j] = 0.0f;
|
| 89 |
+
}
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
for (kk_base = 0; kk_base < k; kk_base += PYC_ADA_BLOCK_K) {
|
| 93 |
+
for (i = 0; i < a_loads_per_thread; ++i) {
|
| 94 |
+
int linear = lane_linear * a_loads_per_thread + i;
|
| 95 |
+
int tile_row = linear / PYC_ADA_BLOCK_K;
|
| 96 |
+
int tile_col = linear % PYC_ADA_BLOCK_K;
|
| 97 |
+
int global_row = block_row + tile_row;
|
| 98 |
+
int global_col = kk_base + tile_col;
|
| 99 |
+
float value = 0.0f;
|
| 100 |
+
if (global_row < m && global_col < k) {
|
| 101 |
+
value = a[global_row * k + global_col];
|
| 102 |
+
}
|
| 103 |
+
a_tile[tile_row][tile_col] = value;
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
for (i = 0; i < b_loads_per_thread; ++i) {
|
| 107 |
+
int linear = lane_linear * b_loads_per_thread + i;
|
| 108 |
+
int tile_row = linear / PYC_ADA_BLOCK_N;
|
| 109 |
+
int tile_col = linear % PYC_ADA_BLOCK_N;
|
| 110 |
+
int global_row = kk_base + tile_row;
|
| 111 |
+
int global_col = block_col + tile_col;
|
| 112 |
+
float value = 0.0f;
|
| 113 |
+
if (global_row < k && global_col < n) {
|
| 114 |
+
value = b[global_row * n + global_col];
|
| 115 |
+
}
|
| 116 |
+
b_tile[tile_row][tile_col] = value;
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
__syncthreads();
|
| 120 |
+
|
| 121 |
+
#pragma unroll
|
| 122 |
+
for (i = 0; i < PYC_ADA_BLOCK_K; ++i) {
|
| 123 |
+
float a_frag[PYC_ADA_THREAD_TILE_M];
|
| 124 |
+
float b_frag[PYC_ADA_THREAD_TILE_N];
|
| 125 |
+
|
| 126 |
+
#pragma unroll
|
| 127 |
+
for (j = 0; j < PYC_ADA_THREAD_TILE_M; ++j) {
|
| 128 |
+
a_frag[j] = a_tile[row_fragment + j][i];
|
| 129 |
+
}
|
| 130 |
+
#pragma unroll
|
| 131 |
+
for (j = 0; j < PYC_ADA_THREAD_TILE_N; ++j) {
|
| 132 |
+
b_frag[j] = b_tile[i][col_fragment + j];
|
| 133 |
+
}
|
| 134 |
+
#pragma unroll
|
| 135 |
+
for (j = 0; j < PYC_ADA_THREAD_TILE_M; ++j) {
|
| 136 |
+
int jj;
|
| 137 |
+
#pragma unroll
|
| 138 |
+
for (jj = 0; jj < PYC_ADA_THREAD_TILE_N; ++jj) {
|
| 139 |
+
accum[j][jj] += a_frag[j] * b_frag[jj];
|
| 140 |
+
}
|
| 141 |
+
}
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
__syncthreads();
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
for (i = 0; i < PYC_ADA_THREAD_TILE_M; ++i) {
|
| 148 |
+
int out_row = block_row + row_fragment + i;
|
| 149 |
+
if (out_row >= m) {
|
| 150 |
+
continue;
|
| 151 |
+
}
|
| 152 |
+
for (j = 0; j < PYC_ADA_THREAD_TILE_N; ++j) {
|
| 153 |
+
int out_col = block_col + col_fragment + j;
|
| 154 |
+
if (out_col < n) {
|
| 155 |
+
c[out_row * n + out_col] = accum[i][j];
|
| 156 |
+
}
|
| 157 |
+
}
|
| 158 |
+
}
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
static int configure_ada_kernel(void) {
|
| 162 |
+
cudaError_t status;
|
| 163 |
+
|
| 164 |
+
status = cudaFuncSetAttribute(
|
| 165 |
+
ada_fp32_tiled_gemm,
|
| 166 |
+
cudaFuncAttributePreferredSharedMemoryCarveout,
|
| 167 |
+
100);
|
| 168 |
+
if (status != cudaSuccess && status != cudaErrorNotSupported) {
|
| 169 |
+
fprintf(stderr, "cudaFuncSetAttribute failed: %s\n", cudaGetErrorString(status));
|
| 170 |
+
return -1;
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
return 0;
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
static int parse_int_arg(const char* text, int* out) {
|
| 177 |
+
char* end = NULL;
|
| 178 |
+
long value;
|
| 179 |
+
|
| 180 |
+
if (!text || !out) {
|
| 181 |
+
return -1;
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
value = strtol(text, &end, 10);
|
| 185 |
+
if (*text == '\0' || !end || *end != '\0' || value <= 0 || value > 1 << 20) {
|
| 186 |
+
return -1;
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
*out = (int)value;
|
| 190 |
+
return 0;
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
static int parse_config(int argc, char** argv, ada_gemm_config* cfg) {
|
| 194 |
+
if (!cfg) {
|
| 195 |
+
return -1;
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
cfg->m = 1024;
|
| 199 |
+
cfg->n = 1024;
|
| 200 |
+
cfg->k = 1024;
|
| 201 |
+
cfg->warmup = 10;
|
| 202 |
+
cfg->iters = 50;
|
| 203 |
+
|
| 204 |
+
if (argc > 1 && parse_int_arg(argv[1], &cfg->m) != 0) return -1;
|
| 205 |
+
if (argc > 2 && parse_int_arg(argv[2], &cfg->n) != 0) return -1;
|
| 206 |
+
if (argc > 3 && parse_int_arg(argv[3], &cfg->k) != 0) return -1;
|
| 207 |
+
if (argc > 4 && parse_int_arg(argv[4], &cfg->warmup) != 0) return -1;
|
| 208 |
+
if (argc > 5 && parse_int_arg(argv[5], &cfg->iters) != 0) return -1;
|
| 209 |
+
|
| 210 |
+
return 0;
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
int main(int argc, char** argv) {
|
| 214 |
+
ada_gemm_config cfg;
|
| 215 |
+
cudaDeviceProp props;
|
| 216 |
+
float* host_a = NULL;
|
| 217 |
+
float* host_b = NULL;
|
| 218 |
+
float* host_c = NULL;
|
| 219 |
+
float* ref_c = NULL;
|
| 220 |
+
float* dev_a = NULL;
|
| 221 |
+
float* dev_b = NULL;
|
| 222 |
+
float* dev_c = NULL;
|
| 223 |
+
cudaEvent_t start = NULL;
|
| 224 |
+
cudaEvent_t stop = NULL;
|
| 225 |
+
size_t a_bytes;
|
| 226 |
+
size_t b_bytes;
|
| 227 |
+
size_t c_bytes;
|
| 228 |
+
dim3 block;
|
| 229 |
+
dim3 grid;
|
| 230 |
+
float elapsed_ms = 0.0f;
|
| 231 |
+
double best_ms = 0.0;
|
| 232 |
+
int iter;
|
| 233 |
+
double max_abs_diff = 0.0;
|
| 234 |
+
int device = 0;
|
| 235 |
+
|
| 236 |
+
if (parse_config(argc, argv, &cfg) != 0) {
|
| 237 |
+
fprintf(stderr, "usage: %s [m] [n] [k] [warmup] [iters]\n", argv[0]);
|
| 238 |
+
return 2;
|
| 239 |
+
}
|
| 240 |
+
|
| 241 |
+
if (check_cuda(cudaGetDevice(&device), "cudaGetDevice") != 0) return 1;
|
| 242 |
+
if (check_cuda(cudaGetDeviceProperties(&props, device), "cudaGetDeviceProperties") != 0) return 1;
|
| 243 |
+
|
| 244 |
+
printf("device=%s cc=%d.%d\n", props.name, props.major, props.minor);
|
| 245 |
+
if (!(props.major == 8 && props.minor == 9)) {
|
| 246 |
+
printf("note=prototype tuned for Ada (sm_89); running on a different architecture\n");
|
| 247 |
+
}
|
| 248 |
+
|
| 249 |
+
a_bytes = (size_t)cfg.m * (size_t)cfg.k * sizeof(float);
|
| 250 |
+
b_bytes = (size_t)cfg.k * (size_t)cfg.n * sizeof(float);
|
| 251 |
+
c_bytes = (size_t)cfg.m * (size_t)cfg.n * sizeof(float);
|
| 252 |
+
|
| 253 |
+
host_a = (float*)malloc(a_bytes);
|
| 254 |
+
host_b = (float*)malloc(b_bytes);
|
| 255 |
+
host_c = (float*)malloc(c_bytes);
|
| 256 |
+
ref_c = (float*)malloc(c_bytes);
|
| 257 |
+
if (!host_a || !host_b || !host_c || !ref_c) {
|
| 258 |
+
fprintf(stderr, "host allocation failed\n");
|
| 259 |
+
return 1;
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
fill_matrix(host_a, cfg.m, cfg.k, 0.03125f);
|
| 263 |
+
fill_matrix(host_b, cfg.k, cfg.n, 0.0625f);
|
| 264 |
+
reference_gemm(host_a, host_b, ref_c, cfg.m, cfg.n, cfg.k);
|
| 265 |
+
|
| 266 |
+
if (check_cuda(cudaMalloc((void**)&dev_a, a_bytes), "cudaMalloc(a)") != 0) return 1;
|
| 267 |
+
if (check_cuda(cudaMalloc((void**)&dev_b, b_bytes), "cudaMalloc(b)") != 0) return 1;
|
| 268 |
+
if (check_cuda(cudaMalloc((void**)&dev_c, c_bytes), "cudaMalloc(c)") != 0) return 1;
|
| 269 |
+
|
| 270 |
+
if (check_cuda(cudaMemcpy(dev_a, host_a, a_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(a)") != 0) return 1;
|
| 271 |
+
if (check_cuda(cudaMemcpy(dev_b, host_b, b_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(b)") != 0) return 1;
|
| 272 |
+
|
| 273 |
+
if (configure_ada_kernel() != 0) return 1;
|
| 274 |
+
|
| 275 |
+
block = dim3(PYC_ADA_THREADS_X, PYC_ADA_THREADS_Y, 1);
|
| 276 |
+
grid = dim3(
|
| 277 |
+
(unsigned int)((cfg.n + PYC_ADA_BLOCK_N - 1) / PYC_ADA_BLOCK_N),
|
| 278 |
+
(unsigned int)((cfg.m + PYC_ADA_BLOCK_M - 1) / PYC_ADA_BLOCK_M),
|
| 279 |
+
1);
|
| 280 |
+
|
| 281 |
+
if (check_cuda(cudaEventCreate(&start), "cudaEventCreate(start)") != 0) return 1;
|
| 282 |
+
if (check_cuda(cudaEventCreate(&stop), "cudaEventCreate(stop)") != 0) return 1;
|
| 283 |
+
|
| 284 |
+
for (iter = 0; iter < cfg.warmup; ++iter) {
|
| 285 |
+
ada_fp32_tiled_gemm<<<grid, block>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 286 |
+
}
|
| 287 |
+
if (check_cuda(cudaGetLastError(), "kernel launch warmup") != 0) return 1;
|
| 288 |
+
if (check_cuda(cudaDeviceSynchronize(), "cudaDeviceSynchronize warmup") != 0) return 1;
|
| 289 |
+
|
| 290 |
+
best_ms = 0.0;
|
| 291 |
+
for (iter = 0; iter < cfg.iters; ++iter) {
|
| 292 |
+
if (check_cuda(cudaEventRecord(start), "cudaEventRecord(start)") != 0) return 1;
|
| 293 |
+
ada_fp32_tiled_gemm<<<grid, block>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 294 |
+
if (check_cuda(cudaEventRecord(stop), "cudaEventRecord(stop)") != 0) return 1;
|
| 295 |
+
if (check_cuda(cudaEventSynchronize(stop), "cudaEventSynchronize(stop)") != 0) return 1;
|
| 296 |
+
if (check_cuda(cudaEventElapsedTime(&elapsed_ms, start, stop), "cudaEventElapsedTime") != 0) return 1;
|
| 297 |
+
if (iter == 0 || elapsed_ms < (float)best_ms) {
|
| 298 |
+
best_ms = elapsed_ms;
|
| 299 |
+
}
|
| 300 |
+
}
|
| 301 |
+
|
| 302 |
+
if (check_cuda(cudaMemcpy(host_c, dev_c, c_bytes, cudaMemcpyDeviceToHost), "cudaMemcpy(c)") != 0) return 1;
|
| 303 |
+
|
| 304 |
+
for (iter = 0; iter < cfg.m * cfg.n; ++iter) {
|
| 305 |
+
double diff = fabs((double)host_c[iter] - (double)ref_c[iter]);
|
| 306 |
+
if (diff > max_abs_diff) {
|
| 307 |
+
max_abs_diff = diff;
|
| 308 |
+
}
|
| 309 |
+
}
|
| 310 |
+
|
| 311 |
+
printf("shape=%dx%dx%d\n", cfg.m, cfg.n, cfg.k);
|
| 312 |
+
printf("tile=%dx%dx%d threads=%dx%d\n",
|
| 313 |
+
PYC_ADA_BLOCK_M,
|
| 314 |
+
PYC_ADA_BLOCK_N,
|
| 315 |
+
PYC_ADA_BLOCK_K,
|
| 316 |
+
PYC_ADA_THREADS_X,
|
| 317 |
+
PYC_ADA_THREADS_Y);
|
| 318 |
+
printf("best_ms=%.3f\n", best_ms);
|
| 319 |
+
printf("max_abs_diff=%.6f\n", max_abs_diff);
|
| 320 |
+
if (best_ms > 0.0) {
|
| 321 |
+
double flops = 2.0 * (double)cfg.m * (double)cfg.n * (double)cfg.k;
|
| 322 |
+
double gflops = flops / (best_ms * 1.0e6);
|
| 323 |
+
printf("gflops=%.3f\n", gflops);
|
| 324 |
+
}
|
| 325 |
+
|
| 326 |
+
cudaEventDestroy(start);
|
| 327 |
+
cudaEventD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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <cuda_runtime.h>
|
| 2 |
+
|
| 3 |
+
#include <math.h>
|
| 4 |
+
#include <stdint.h>
|
| 5 |
+
#include <stdio.h>
|
| 6 |
+
#include <stdlib.h>
|
| 7 |
+
|
| 8 |
+
#define PYC_ADA_K128WARP_BLOCK_M 64
|
| 9 |
+
#define PYC_ADA_K128WARP_BLOCK_N 64
|
| 10 |
+
#define PYC_ADA_K128WARP_BLOCK_K 128
|
| 11 |
+
#define PYC_ADA_K128WARP_THREADS_X 32
|
| 12 |
+
#define PYC_ADA_K128WARP_THREADS_Y 8
|
| 13 |
+
#define PYC_ADA_K128WARP_THREAD_TILE_M 8
|
| 14 |
+
#define PYC_ADA_K128WARP_THREAD_TILE_N 2
|
| 15 |
+
#define PYC_ADA_K128WARP_VEC 4
|
| 16 |
+
|
| 17 |
+
typedef struct {
|
| 18 |
+
int m;
|
| 19 |
+
int n;
|
| 20 |
+
int k;
|
| 21 |
+
int warmup;
|
| 22 |
+
int iters;
|
| 23 |
+
} ada_gemm_k128warp_config;
|
| 24 |
+
|
| 25 |
+
static int check_cuda(cudaError_t status, const char* what) {
|
| 26 |
+
if (status != cudaSuccess) {
|
| 27 |
+
fprintf(stderr, "%s failed: %s\n", what, cudaGetErrorString(status));
|
| 28 |
+
return -1;
|
| 29 |
+
}
|
| 30 |
+
return 0;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
static void fill_matrix(float* data, int rows, int cols, float scale) {
|
| 34 |
+
int i;
|
| 35 |
+
for (i = 0; i < rows * cols; ++i) {
|
| 36 |
+
int pattern = (i * 17 + rows * 13 + cols * 7) % 31;
|
| 37 |
+
data[i] = ((float)pattern - 15.0f) * scale;
|
| 38 |
+
}
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
static void reference_gemm(const float* a, const float* b, float* c, int m, int n, int k) {
|
| 42 |
+
int row;
|
| 43 |
+
for (row = 0; row < m; ++row) {
|
| 44 |
+
int col;
|
| 45 |
+
for (col = 0; col < n; ++col) {
|
| 46 |
+
float acc = 0.0f;
|
| 47 |
+
int kk;
|
| 48 |
+
for (kk = 0; kk < k; ++kk) {
|
| 49 |
+
acc += a[row * k + kk] * b[kk * n + col];
|
| 50 |
+
}
|
| 51 |
+
c[row * n + col] = acc;
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
__device__ static void load_a_vec(
|
| 57 |
+
const float* __restrict__ a,
|
| 58 |
+
float shared_a[PYC_ADA_K128WARP_BLOCK_M][PYC_ADA_K128WARP_BLOCK_K + 1],
|
| 59 |
+
int lane_linear,
|
| 60 |
+
int block_row,
|
| 61 |
+
int kk_base,
|
| 62 |
+
int m,
|
| 63 |
+
int k) {
|
| 64 |
+
const int vecs_per_row = PYC_ADA_K128WARP_BLOCK_K / PYC_ADA_K128WARP_VEC;
|
| 65 |
+
const int total_vecs = (PYC_ADA_K128WARP_BLOCK_M * PYC_ADA_K128WARP_BLOCK_K) / PYC_ADA_K128WARP_VEC;
|
| 66 |
+
int phase;
|
| 67 |
+
|
| 68 |
+
for (phase = 0; phase < total_vecs / (PYC_ADA_K128WARP_THREADS_X * PYC_ADA_K128WARP_THREADS_Y); ++phase) {
|
| 69 |
+
const int linear = lane_linear + phase * (PYC_ADA_K128WARP_THREADS_X * PYC_ADA_K128WARP_THREADS_Y);
|
| 70 |
+
const int tile_row = linear / vecs_per_row;
|
| 71 |
+
const int tile_col = (linear % vecs_per_row) * PYC_ADA_K128WARP_VEC;
|
| 72 |
+
const int global_row = block_row + tile_row;
|
| 73 |
+
const int global_col = kk_base + tile_col;
|
| 74 |
+
int i;
|
| 75 |
+
|
| 76 |
+
if (global_row < m && global_col + (PYC_ADA_K128WARP_VEC - 1) < k) {
|
| 77 |
+
const float4 value = *reinterpret_cast<const float4*>(&a[global_row * k + global_col]);
|
| 78 |
+
shared_a[tile_row][tile_col + 0] = value.x;
|
| 79 |
+
shared_a[tile_row][tile_col + 1] = value.y;
|
| 80 |
+
shared_a[tile_row][tile_col + 2] = value.z;
|
| 81 |
+
shared_a[tile_row][tile_col + 3] = value.w;
|
| 82 |
+
continue;
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
for (i = 0; i < PYC_ADA_K128WARP_VEC; ++i) {
|
| 86 |
+
float value = 0.0f;
|
| 87 |
+
if (global_row < m && global_col + i < k) {
|
| 88 |
+
value = a[global_row * k + global_col + i];
|
| 89 |
+
}
|
| 90 |
+
shared_a[tile_row][tile_col + i] = value;
|
| 91 |
+
}
|
| 92 |
+
}
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
__device__ static void load_b_vec(
|
| 96 |
+
const float* __restrict__ b,
|
| 97 |
+
float shared_b[PYC_ADA_K128WARP_BLOCK_K][PYC_ADA_K128WARP_BLOCK_N + 1],
|
| 98 |
+
int lane_linear,
|
| 99 |
+
int block_col,
|
| 100 |
+
int kk_base,
|
| 101 |
+
int k,
|
| 102 |
+
int n) {
|
| 103 |
+
const int vecs_per_row = PYC_ADA_K128WARP_BLOCK_N / PYC_ADA_K128WARP_VEC;
|
| 104 |
+
const int total_vecs = (PYC_ADA_K128WARP_BLOCK_K * PYC_ADA_K128WARP_BLOCK_N) / PYC_ADA_K128WARP_VEC;
|
| 105 |
+
int phase;
|
| 106 |
+
|
| 107 |
+
for (phase = 0; phase < total_vecs / (PYC_ADA_K128WARP_THREADS_X * PYC_ADA_K128WARP_THREADS_Y); ++phase) {
|
| 108 |
+
const int linear = lane_linear + phase * (PYC_ADA_K128WARP_THREADS_X * PYC_ADA_K128WARP_THREADS_Y);
|
| 109 |
+
const int tile_row = linear / vecs_per_row;
|
| 110 |
+
const int tile_col = (linear % vecs_per_row) * PYC_ADA_K128WARP_VEC;
|
| 111 |
+
const int global_row = kk_base + tile_row;
|
| 112 |
+
const int global_col = block_col + tile_col;
|
| 113 |
+
int i;
|
| 114 |
+
|
| 115 |
+
if (global_row < k && global_col + (PYC_ADA_K128WARP_VEC - 1) < n) {
|
| 116 |
+
const float4 value = *reinterpret_cast<const float4*>(&b[global_row * n + global_col]);
|
| 117 |
+
shared_b[tile_row][tile_col + 0] = value.x;
|
| 118 |
+
shared_b[tile_row][tile_col + 1] = value.y;
|
| 119 |
+
shared_b[tile_row][tile_col + 2] = value.z;
|
| 120 |
+
shared_b[tile_row][tile_col + 3] = value.w;
|
| 121 |
+
continue;
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
for (i = 0; i < PYC_ADA_K128WARP_VEC; ++i) {
|
| 125 |
+
float value = 0.0f;
|
| 126 |
+
if (global_row < k && global_col + i < n) {
|
| 127 |
+
value = b[global_row * n + global_col + i];
|
| 128 |
+
}
|
| 129 |
+
shared_b[tile_row][tile_col + i] = value;
|
| 130 |
+
}
|
| 131 |
+
}
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
__launch_bounds__(PYC_ADA_K128WARP_THREADS_X * PYC_ADA_K128WARP_THREADS_Y, 1)
|
| 135 |
+
__global__ void ada_fp32_k128warp_gemm(
|
| 136 |
+
const float* __restrict__ a,
|
| 137 |
+
const float* __restrict__ b,
|
| 138 |
+
float* __restrict__ c,
|
| 139 |
+
int m,
|
| 140 |
+
int n,
|
| 141 |
+
int k) {
|
| 142 |
+
__shared__ float shared_a[PYC_ADA_K128WARP_BLOCK_M][PYC_ADA_K128WARP_BLOCK_K + 1];
|
| 143 |
+
__shared__ float shared_b[PYC_ADA_K128WARP_BLOCK_K][PYC_ADA_K128WARP_BLOCK_N + 1];
|
| 144 |
+
|
| 145 |
+
const int lane_linear = threadIdx.y * blockDim.x + threadIdx.x;
|
| 146 |
+
const int block_row = blockIdx.y * PYC_ADA_K128WARP_BLOCK_M;
|
| 147 |
+
const int block_col = blockIdx.x * PYC_ADA_K128WARP_BLOCK_N;
|
| 148 |
+
const int row_fragment = threadIdx.y * PYC_ADA_K128WARP_THREAD_TILE_M;
|
| 149 |
+
const int col_fragment = threadIdx.x * PYC_ADA_K128WARP_THREAD_TILE_N;
|
| 150 |
+
float accum[PYC_ADA_K128WARP_THREAD_TILE_M][PYC_ADA_K128WARP_THREAD_TILE_N];
|
| 151 |
+
int kk_base;
|
| 152 |
+
int i;
|
| 153 |
+
int j;
|
| 154 |
+
|
| 155 |
+
for (i = 0; i < PYC_ADA_K128WARP_THREAD_TILE_M; ++i) {
|
| 156 |
+
for (j = 0; j < PYC_ADA_K128WARP_THREAD_TILE_N; ++j) {
|
| 157 |
+
accum[i][j] = 0.0f;
|
| 158 |
+
}
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
for (kk_base = 0; kk_base < k; kk_base += PYC_ADA_K128WARP_BLOCK_K) {
|
| 162 |
+
load_a_vec(a, shared_a, lane_linear, block_row, kk_base, m, k);
|
| 163 |
+
load_b_vec(b, shared_b, lane_linear, block_col, kk_base, k, n);
|
| 164 |
+
|
| 165 |
+
__syncthreads();
|
| 166 |
+
|
| 167 |
+
#pragma unroll
|
| 168 |
+
for (i = 0; i < PYC_ADA_K128WARP_BLOCK_K; ++i) {
|
| 169 |
+
float a_frag[PYC_ADA_K128WARP_THREAD_TILE_M];
|
| 170 |
+
float b_frag[PYC_ADA_K128WARP_THREAD_TILE_N];
|
| 171 |
+
int ii;
|
| 172 |
+
|
| 173 |
+
#pragma unroll
|
| 174 |
+
for (ii = 0; ii < PYC_ADA_K128WARP_THREAD_TILE_M; ++ii) {
|
| 175 |
+
a_frag[ii] = shared_a[row_fragment + ii][i];
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
#pragma unroll
|
| 179 |
+
for (ii = 0; ii < PYC_ADA_K128WARP_THREAD_TILE_N; ++ii) {
|
| 180 |
+
b_frag[ii] = shared_b[i][col_fragment + ii];
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
#pragma unroll
|
| 184 |
+
for (ii = 0; ii < PYC_ADA_K128WARP_THREAD_TILE_M; ++ii) {
|
| 185 |
+
int jj;
|
| 186 |
+
#pragma unroll
|
| 187 |
+
for (jj = 0; jj < PYC_ADA_K128WARP_THREAD_TILE_N; ++jj) {
|
| 188 |
+
accum[ii][jj] += a_frag[ii] * b_frag[jj];
|
| 189 |
+
}
|
| 190 |
+
}
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
__syncthreads();
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
for (i = 0; i < PYC_ADA_K128WARP_THREAD_TILE_M; ++i) {
|
| 197 |
+
int out_row = block_row + row_fragment + i;
|
| 198 |
+
if (out_row >= m) {
|
| 199 |
+
continue;
|
| 200 |
+
}
|
| 201 |
+
for (j = 0; j < PYC_ADA_K128WARP_THREAD_TILE_N; ++j) {
|
| 202 |
+
int out_col = block_col + col_fragment + j;
|
| 203 |
+
if (out_col < n) {
|
| 204 |
+
c[out_row * n + out_col] = accum[i][j];
|
| 205 |
+
}
|
| 206 |
+
}
|
| 207 |
+
}
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
static int configure_ada_kernel(void) {
|
| 211 |
+
cudaError_t status;
|
| 212 |
+
|
| 213 |
+
status = cudaFuncSetAttribute(
|
| 214 |
+
ada_fp32_k128warp_gemm,
|
| 215 |
+
cudaFuncAttributePreferredSharedMemoryCarveout,
|
| 216 |
+
100);
|
| 217 |
+
if (status != cudaSuccess && status != cudaErrorNotSupported) {
|
| 218 |
+
fprintf(stderr, "cudaFuncSetAttribute failed: %s\n", cudaGetErrorString(status));
|
| 219 |
+
return -1;
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
return 0;
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
+
static int parse_int_arg(const char* text, int* out) {
|
| 226 |
+
char* end = NULL;
|
| 227 |
+
long value;
|
| 228 |
+
|
| 229 |
+
if (!text || !out) {
|
| 230 |
+
return -1;
|
| 231 |
+
}
|
| 232 |
+
|
| 233 |
+
value = strtol(text, &end, 10);
|
| 234 |
+
if (*text == '\0' || !end || *end != '\0' || value <= 0 || value > 1 << 20) {
|
| 235 |
+
return -1;
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
*out = (int)value;
|
| 239 |
+
return 0;
|
| 240 |
+
}
|
| 241 |
+
|
| 242 |
+
static int parse_config(int argc, char** argv, ada_gemm_k128warp_config* cfg) {
|
| 243 |
+
if (!cfg) {
|
| 244 |
+
return -1;
|
| 245 |
+
}
|
| 246 |
+
|
| 247 |
+
cfg->m = 1024;
|
| 248 |
+
cfg->n = 1024;
|
| 249 |
+
cfg->k = 1024;
|
| 250 |
+
cfg->warmup = 10;
|
| 251 |
+
cfg->iters = 50;
|
| 252 |
+
|
| 253 |
+
if (argc > 1 && parse_int_arg(argv[1], &cfg->m) != 0) return -1;
|
| 254 |
+
if (argc > 2 && parse_int_arg(argv[2], &cfg->n) != 0) return -1;
|
| 255 |
+
if (argc > 3 && parse_int_arg(argv[3], &cfg->k) != 0) return -1;
|
| 256 |
+
if (argc > 4 && parse_int_arg(argv[4], &cfg->warmup) != 0) return -1;
|
| 257 |
+
if (argc > 5 && parse_int_arg(argv[5], &cfg->iters) != 0) return -1;
|
| 258 |
+
|
| 259 |
+
return 0;
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
int main(int argc, char** argv) {
|
| 263 |
+
ada_gemm_k128warp_config cfg;
|
| 264 |
+
cudaDeviceProp props;
|
| 265 |
+
float* host_a = NULL;
|
| 266 |
+
float* host_b = NULL;
|
| 267 |
+
float* host_c = NULL;
|
| 268 |
+
float* ref_c = NULL;
|
| 269 |
+
float* dev_a = NULL;
|
| 270 |
+
float* dev_b = NULL;
|
| 271 |
+
float* dev_c = NULL;
|
| 272 |
+
cudaEvent_t start = NULL;
|
| 273 |
+
cudaEvent_t stop = NULL;
|
| 274 |
+
size_t a_bytes;
|
| 275 |
+
size_t b_bytes;
|
| 276 |
+
size_t c_bytes;
|
| 277 |
+
dim3 block;
|
| 278 |
+
dim3 grid;
|
| 279 |
+
float elapsed_ms = 0.0f;
|
| 280 |
+
double best_ms = 0.0;
|
| 281 |
+
int iter;
|
| 282 |
+
double max_abs_diff = 0.0;
|
| 283 |
+
int device = 0;
|
| 284 |
+
|
| 285 |
+
if (parse_config(argc, argv, &cfg) != 0) {
|
| 286 |
+
fprintf(stderr, "usage: %s [m] [n] [k] [warmup] [iters]\n", argv[0]);
|
| 287 |
+
return 2;
|
| 288 |
+
}
|
| 289 |
+
|
| 290 |
+
if (check_cuda(cudaGetDevice(&device), "cudaGetDevice") != 0) return 1;
|
| 291 |
+
if (check_cuda(cudaGetDeviceProperties(&props, device), "cudaGetDeviceProperties") != 0) return 1;
|
| 292 |
+
|
| 293 |
+
printf("device=%s cc=%d.%d\n", props.name, props.major, props.minor);
|
| 294 |
+
if (!(props.major == 8 && props.minor == 9)) {
|
| 295 |
+
printf("note=prototype tuned for Ada (sm_89); running on a different architecture\n");
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
+
a_bytes = (size_t)cfg.m * (size_t)cfg.k * sizeof(float);
|
| 299 |
+
b_bytes = (size_t)cfg.k * (size_t)cfg.n * sizeof(float);
|
| 300 |
+
c_bytes = (size_t)cfg.m * (size_t)cfg.n * sizeof(float);
|
| 301 |
+
|
| 302 |
+
host_a = (float*)malloc(a_bytes);
|
| 303 |
+
host_b = (float*)malloc(b_bytes);
|
| 304 |
+
host_c = (float*)malloc(c_bytes);
|
| 305 |
+
ref_c = (float*)malloc(c_bytes);
|
| 306 |
+
if (!host_a || !host_b || !host_c || !ref_c) {
|
| 307 |
+
fprintf(stderr, "host allocation failed\n");
|
| 308 |
+
return 1;
|
| 309 |
+
}
|
| 310 |
+
|
| 311 |
+
fill_matrix(host_a, cfg.m, cfg.k, 0.03125f);
|
| 312 |
+
fill_matrix(host_b, cfg.k, cfg.n, 0.0625f);
|
| 313 |
+
reference_gemm(host_a, host_b, ref_c, cfg.m, cfg.n, cfg.k);
|
| 314 |
+
|
| 315 |
+
if (check_cuda(cudaMalloc((void**)&dev_a, a_bytes), "cudaMalloc(a)") != 0) return 1;
|
| 316 |
+
if (check_cuda(cudaMalloc((void**)&dev_b, b_bytes), "cudaMalloc(b)") != 0) return 1;
|
| 317 |
+
if (check_cuda(cudaMalloc((void**)&dev_c, c_bytes), "cudaMalloc(c)") != 0) return 1;
|
| 318 |
+
|
| 319 |
+
if (check_cuda(cudaMemcpy(dev_a, host_a, a_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(a)") != 0) return 1;
|
| 320 |
+
if (check_cuda(cudaMemcpy(dev_b, host_b, b_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(b)") != 0) return 1;
|
| 321 |
+
|
| 322 |
+
if (configure_ada_kernel() != 0) return 1;
|
| 323 |
+
|
| 324 |
+
block = dim3(PYC_ADA_K128WARP_THREADS_X, PYC_ADA_K128WARP_THREADS_Y, 1);
|
| 325 |
+
grid = dim3(
|
| 326 |
+
(unsigned int)((cfg.n + PYC_ADA_K128WARP_BLOCK_N - 1) / PYC_ADA_K128WARP_BLOCK_N),
|
| 327 |
+
(unsigned int)((cfg.m + PYC_ADA_K128WARP_BLOCK_M - 1) / PYC_ADA_K128WARP_BLOCK_M),
|
| 328 |
+
1);
|
| 329 |
+
|
| 330 |
+
if (check_cuda(cudaEventCreate(&start), "cudaEventCreate(start)") != 0) return 1;
|
| 331 |
+
if (check_cuda(cudaEventCreate(&stop), "cudaEventCreate(stop)") != 0) return 1;
|
| 332 |
+
|
| 333 |
+
for (iter = 0; iter < cfg.warmup; ++iter) {
|
| 334 |
+
ada_fp32_k128warp_gemm<<<grid, block>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 335 |
+
}
|
| 336 |
+
if (check_cuda(cudaGetLastError(), "kernel launch warmup") != 0) return 1;
|
| 337 |
+
if (check_cuda(cudaDeviceSynchronize(), "cudaDeviceSynchronize warmup") != 0) return 1;
|
| 338 |
+
|
| 339 |
+
best_ms = 0.0;
|
| 340 |
+
for (iter = 0; iter < cfg.iters; ++iter) {
|
| 341 |
+
if (check_cuda(cudaEventRecord(start), "cudaEventRecord(start)") != 0) return 1;
|
| 342 |
+
ada_fp32_k128warp_gemm<<<grid, block>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 343 |
+
if (check_cuda(cudaEventRecord(stop), "cudaEventRecord(stop)") != 0) return 1;
|
| 344 |
+
if (check_cuda(cudaEventSynchronize(stop), "cudaEventSynchronize(stop)") != 0) return 1;
|
| 345 |
+
if (check_cuda(cudaEventElapsedTime(&elapsed_ms, start, stop), "cudaEventElapsedTime") != 0) return 1;
|
| 346 |
+
if (iter == 0 || elapsed_ms < (float)best_ms) {
|
| 347 |
+
best_ms = elapsed_ms;
|
| 348 |
+
}
|
| 349 |
+
}
|
| 350 |
+
|
| 351 |
+
if (check_cuda(cudaMemcpy(host_c, dev_c, c_bytes, cudaMemcpyDeviceToHost), "cudaMemcpy(c)") != 0) return 1;
|
| 352 |
+
|
| 353 |
+
for (iter = 0; iter < cfg.m * cfg.n; ++iter) {
|
| 354 |
+
double diff = fabs((double)host_c[iter] - (double)ref_c[iter]);
|
| 355 |
+
if (diff > max_abs_diff) {
|
| 356 |
+
max_abs_diff = diff;
|
| 357 |
+
}
|
| 358 |
+
}
|
| 359 |
+
|
| 360 |
+
printf("shape=%dx%dx%d\n", cfg.m, cfg.n, cfg.k);
|
| 361 |
+
printf("tile=%dx%dx%d threads=%dx%d thread_tile=%dx%d vector_width=%d\n",
|
| 362 |
+
PYC_ADA_K128WARP_BLOCK_M,
|
| 363 |
+
PYC_ADA_K128WARP_BLOCK_N,
|
| 364 |
+
PYC_ADA_K128WARP_BLOCK_K,
|
| 365 |
+
PYC_ADA_K128WARP_THREADS_X,
|
| 366 |
+
PYC_ADA_K128WARP_THREADS_Y,
|
| 367 |
+
PYC_ADA_K128WARP_THREAD_TILE_M,
|
| 368 |
+
PYC_ADA_K128WARP_THREAD_TILE_N,
|
| 369 |
+
PYC_ADA_K128WARP_VEC);
|
| 370 |
+
printf("best_ms=%.3f\n", best_ms);
|
| 371 |
+
printf("max_abs_diff=%.6f\n", max_abs_diff);
|
| 372 |
+
if (best_ms > 0.0) {
|
| 373 |
+
double flops = 2.0 * (double)cfg.m * (double)cfg.n * (double)cfg.k;
|
| 374 |
+
double gflops = flops / (best_ms * 1.0e6);
|
| 375 |
+
printf("gflops=%.3f\n", gflops);
|
| 376 |
+
}
|
| 377 |
+
|
| 378 |
+
cudaEventDestroy(start);
|
| 379 |
+
cudaEventDestroy(stop);
|
| 380 |
+
cudaFree(dev_a);
|
| 381 |
+
cudaFree(dev_b);
|
| 382 |
+
cudaFree(dev_c);
|
| 383 |
+
free(host_a);
|
| 384 |
+
free(host_b);
|
| 385 |
+
free(host_c);
|
| 386 |
+
free(ref_c);
|
| 387 |
+
return max_abs_diff <= 1e-2 ? 0 : 1;
|
| 388 |
+
}
|
kernels/prototypes/ada/gemm_k128_warp32_dyn/kernel.cu
ADDED
|
@@ -0,0 +1,428 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <cuda_runtime.h>
|
| 2 |
+
|
| 3 |
+
#include <math.h>
|
| 4 |
+
#include <stdint.h>
|
| 5 |
+
#include <stdio.h>
|
| 6 |
+
#include <stdlib.h>
|
| 7 |
+
|
| 8 |
+
#define PYC_ADA_K128D_BLOCK_M 64
|
| 9 |
+
#define PYC_ADA_K128D_BLOCK_N 64
|
| 10 |
+
#define PYC_ADA_K128D_BLOCK_K 128
|
| 11 |
+
#define PYC_ADA_K128D_THREADS_X 32
|
| 12 |
+
#define PYC_ADA_K128D_THREADS_Y 8
|
| 13 |
+
#define PYC_ADA_K128D_THREAD_TILE_M 8
|
| 14 |
+
#define PYC_ADA_K128D_THREAD_TILE_N 2
|
| 15 |
+
#define PYC_ADA_K128D_VEC 4
|
| 16 |
+
#define PYC_ADA_K128D_SHARED_A_STRIDE (PYC_ADA_K128D_BLOCK_K + 1)
|
| 17 |
+
#define PYC_ADA_K128D_SHARED_B_STRIDE (PYC_ADA_K128D_BLOCK_N + 1)
|
| 18 |
+
#define PYC_ADA_K128D_SHARED_A_ELEMS (PYC_ADA_K128D_BLOCK_M * PYC_ADA_K128D_SHARED_A_STRIDE)
|
| 19 |
+
#define PYC_ADA_K128D_SHARED_B_ELEMS (PYC_ADA_K128D_BLOCK_K * PYC_ADA_K128D_SHARED_B_STRIDE)
|
| 20 |
+
#define PYC_ADA_K128D_SHARED_BYTES ((PYC_ADA_K128D_SHARED_A_ELEMS + PYC_ADA_K128D_SHARED_B_ELEMS) * (int)sizeof(float))
|
| 21 |
+
|
| 22 |
+
typedef struct {
|
| 23 |
+
int m;
|
| 24 |
+
int n;
|
| 25 |
+
int k;
|
| 26 |
+
int warmup;
|
| 27 |
+
int iters;
|
| 28 |
+
} ada_gemm_k128d_config;
|
| 29 |
+
|
| 30 |
+
static int check_cuda(cudaError_t status, const char* what) {
|
| 31 |
+
if (status != cudaSuccess) {
|
| 32 |
+
fprintf(stderr, "%s failed: %s\n", what, cudaGetErrorString(status));
|
| 33 |
+
return -1;
|
| 34 |
+
}
|
| 35 |
+
return 0;
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
static void fill_matrix(float* data, int rows, int cols, float scale) {
|
| 39 |
+
int i;
|
| 40 |
+
for (i = 0; i < rows * cols; ++i) {
|
| 41 |
+
int pattern = (i * 17 + rows * 13 + cols * 7) % 31;
|
| 42 |
+
data[i] = ((float)pattern - 15.0f) * scale;
|
| 43 |
+
}
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
static void reference_gemm(const float* a, const float* b, float* c, int m, int n, int k) {
|
| 47 |
+
int row;
|
| 48 |
+
for (row = 0; row < m; ++row) {
|
| 49 |
+
int col;
|
| 50 |
+
for (col = 0; col < n; ++col) {
|
| 51 |
+
float acc = 0.0f;
|
| 52 |
+
int kk;
|
| 53 |
+
for (kk = 0; kk < k; ++kk) {
|
| 54 |
+
acc += a[row * k + kk] * b[kk * n + col];
|
| 55 |
+
}
|
| 56 |
+
c[row * n + col] = acc;
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
__device__ __forceinline__ float* shared_a_ptr(float* shared_mem) {
|
| 62 |
+
return shared_mem;
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
__device__ __forceinline__ float* shared_b_ptr(float* shared_mem) {
|
| 66 |
+
return shared_mem + PYC_ADA_K128D_SHARED_A_ELEMS;
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
__device__ __forceinline__ float shared_a_load(const float* shared_a, int row, int col) {
|
| 70 |
+
return shared_a[row * PYC_ADA_K128D_SHARED_A_STRIDE + col];
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
__device__ __forceinline__ float shared_b_load(const float* shared_b, int row, int col) {
|
| 74 |
+
return shared_b[row * PYC_ADA_K128D_SHARED_B_STRIDE + col];
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
__device__ __forceinline__ void shared_a_store(float* shared_a, int row, int col, float value) {
|
| 78 |
+
shared_a[row * PYC_ADA_K128D_SHARED_A_STRIDE + col] = value;
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
__device__ __forceinline__ void shared_b_store(float* shared_b, int row, int col, float value) {
|
| 82 |
+
shared_b[row * PYC_ADA_K128D_SHARED_B_STRIDE + col] = value;
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
__device__ static void load_a_vec(
|
| 86 |
+
const float* __restrict__ a,
|
| 87 |
+
float* shared_a,
|
| 88 |
+
int lane_linear,
|
| 89 |
+
int block_row,
|
| 90 |
+
int kk_base,
|
| 91 |
+
int m,
|
| 92 |
+
int k) {
|
| 93 |
+
const int vecs_per_row = PYC_ADA_K128D_BLOCK_K / PYC_ADA_K128D_VEC;
|
| 94 |
+
const int total_vecs = (PYC_ADA_K128D_BLOCK_M * PYC_ADA_K128D_BLOCK_K) / PYC_ADA_K128D_VEC;
|
| 95 |
+
int phase;
|
| 96 |
+
|
| 97 |
+
for (phase = 0; phase < total_vecs / (PYC_ADA_K128D_THREADS_X * PYC_ADA_K128D_THREADS_Y); ++phase) {
|
| 98 |
+
const int linear = lane_linear + phase * (PYC_ADA_K128D_THREADS_X * PYC_ADA_K128D_THREADS_Y);
|
| 99 |
+
const int tile_row = linear / vecs_per_row;
|
| 100 |
+
const int tile_col = (linear % vecs_per_row) * PYC_ADA_K128D_VEC;
|
| 101 |
+
const int global_row = block_row + tile_row;
|
| 102 |
+
const int global_col = kk_base + tile_col;
|
| 103 |
+
int i;
|
| 104 |
+
|
| 105 |
+
if (global_row < m && global_col + (PYC_ADA_K128D_VEC - 1) < k) {
|
| 106 |
+
const float4 value = *reinterpret_cast<const float4*>(&a[global_row * k + global_col]);
|
| 107 |
+
shared_a_store(shared_a, tile_row, tile_col + 0, value.x);
|
| 108 |
+
shared_a_store(shared_a, tile_row, tile_col + 1, value.y);
|
| 109 |
+
shared_a_store(shared_a, tile_row, tile_col + 2, value.z);
|
| 110 |
+
shared_a_store(shared_a, tile_row, tile_col + 3, value.w);
|
| 111 |
+
continue;
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
for (i = 0; i < PYC_ADA_K128D_VEC; ++i) {
|
| 115 |
+
float value = 0.0f;
|
| 116 |
+
if (global_row < m && global_col + i < k) {
|
| 117 |
+
value = a[global_row * k + global_col + i];
|
| 118 |
+
}
|
| 119 |
+
shared_a_store(shared_a, tile_row, tile_col + i, value);
|
| 120 |
+
}
|
| 121 |
+
}
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
__device__ static void load_b_vec(
|
| 125 |
+
const float* __restrict__ b,
|
| 126 |
+
float* shared_b,
|
| 127 |
+
int lane_linear,
|
| 128 |
+
int block_col,
|
| 129 |
+
int kk_base,
|
| 130 |
+
int k,
|
| 131 |
+
int n) {
|
| 132 |
+
const int vecs_per_row = PYC_ADA_K128D_BLOCK_N / PYC_ADA_K128D_VEC;
|
| 133 |
+
const int total_vecs = (PYC_ADA_K128D_BLOCK_K * PYC_ADA_K128D_BLOCK_N) / PYC_ADA_K128D_VEC;
|
| 134 |
+
int phase;
|
| 135 |
+
|
| 136 |
+
for (phase = 0; phase < total_vecs / (PYC_ADA_K128D_THREADS_X * PYC_ADA_K128D_THREADS_Y); ++phase) {
|
| 137 |
+
const int linear = lane_linear + phase * (PYC_ADA_K128D_THREADS_X * PYC_ADA_K128D_THREADS_Y);
|
| 138 |
+
const int tile_row = linear / vecs_per_row;
|
| 139 |
+
const int tile_col = (linear % vecs_per_row) * PYC_ADA_K128D_VEC;
|
| 140 |
+
const int global_row = kk_base + tile_row;
|
| 141 |
+
const int global_col = block_col + tile_col;
|
| 142 |
+
int i;
|
| 143 |
+
|
| 144 |
+
if (global_row < k && global_col + (PYC_ADA_K128D_VEC - 1) < n) {
|
| 145 |
+
const float4 value = *reinterpret_cast<const float4*>(&b[global_row * n + global_col]);
|
| 146 |
+
shared_b_store(shared_b, tile_row, tile_col + 0, value.x);
|
| 147 |
+
shared_b_store(shared_b, tile_row, tile_col + 1, value.y);
|
| 148 |
+
shared_b_store(shared_b, tile_row, tile_col + 2, value.z);
|
| 149 |
+
shared_b_store(shared_b, tile_row, tile_col + 3, value.w);
|
| 150 |
+
continue;
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
for (i = 0; i < PYC_ADA_K128D_VEC; ++i) {
|
| 154 |
+
float value = 0.0f;
|
| 155 |
+
if (global_row < k && global_col + i < n) {
|
| 156 |
+
value = b[global_row * n + global_col + i];
|
| 157 |
+
}
|
| 158 |
+
shared_b_store(shared_b, tile_row, tile_col + i, value);
|
| 159 |
+
}
|
| 160 |
+
}
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
__launch_bounds__(PYC_ADA_K128D_THREADS_X * PYC_ADA_K128D_THREADS_Y, 1)
|
| 164 |
+
__global__ void ada_fp32_k128dyn_gemm(
|
| 165 |
+
const float* __restrict__ a,
|
| 166 |
+
const float* __restrict__ b,
|
| 167 |
+
float* __restrict__ c,
|
| 168 |
+
int m,
|
| 169 |
+
int n,
|
| 170 |
+
int k) {
|
| 171 |
+
extern __shared__ float shared_mem[];
|
| 172 |
+
float* shared_a = shared_a_ptr(shared_mem);
|
| 173 |
+
float* shared_b = shared_b_ptr(shared_mem);
|
| 174 |
+
|
| 175 |
+
const int lane_linear = threadIdx.y * blockDim.x + threadIdx.x;
|
| 176 |
+
const int block_row = blockIdx.y * PYC_ADA_K128D_BLOCK_M;
|
| 177 |
+
const int block_col = blockIdx.x * PYC_ADA_K128D_BLOCK_N;
|
| 178 |
+
const int row_fragment = threadIdx.y * PYC_ADA_K128D_THREAD_TILE_M;
|
| 179 |
+
const int col_fragment = threadIdx.x * PYC_ADA_K128D_THREAD_TILE_N;
|
| 180 |
+
float accum[PYC_ADA_K128D_THREAD_TILE_M][PYC_ADA_K128D_THREAD_TILE_N];
|
| 181 |
+
int kk_base;
|
| 182 |
+
int i;
|
| 183 |
+
int j;
|
| 184 |
+
|
| 185 |
+
for (i = 0; i < PYC_ADA_K128D_THREAD_TILE_M; ++i) {
|
| 186 |
+
for (j = 0; j < PYC_ADA_K128D_THREAD_TILE_N; ++j) {
|
| 187 |
+
accum[i][j] = 0.0f;
|
| 188 |
+
}
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
for (kk_base = 0; kk_base < k; kk_base += PYC_ADA_K128D_BLOCK_K) {
|
| 192 |
+
load_a_vec(a, shared_a, lane_linear, block_row, kk_base, m, k);
|
| 193 |
+
load_b_vec(b, shared_b, lane_linear, block_col, kk_base, k, n);
|
| 194 |
+
|
| 195 |
+
__syncthreads();
|
| 196 |
+
|
| 197 |
+
#pragma unroll
|
| 198 |
+
for (i = 0; i < PYC_ADA_K128D_BLOCK_K; ++i) {
|
| 199 |
+
float a_frag[PYC_ADA_K128D_THREAD_TILE_M];
|
| 200 |
+
float b_frag[PYC_ADA_K128D_THREAD_TILE_N];
|
| 201 |
+
int ii;
|
| 202 |
+
|
| 203 |
+
#pragma unroll
|
| 204 |
+
for (ii = 0; ii < PYC_ADA_K128D_THREAD_TILE_M; ++ii) {
|
| 205 |
+
a_frag[ii] = shared_a_load(shared_a, row_fragment + ii, i);
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
#pragma unroll
|
| 209 |
+
for (ii = 0; ii < PYC_ADA_K128D_THREAD_TILE_N; ++ii) {
|
| 210 |
+
b_frag[ii] = shared_b_load(shared_b, i, col_fragment + ii);
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
#pragma unroll
|
| 214 |
+
for (ii = 0; ii < PYC_ADA_K128D_THREAD_TILE_M; ++ii) {
|
| 215 |
+
int jj;
|
| 216 |
+
#pragma unroll
|
| 217 |
+
for (jj = 0; jj < PYC_ADA_K128D_THREAD_TILE_N; ++jj) {
|
| 218 |
+
accum[ii][jj] += a_frag[ii] * b_frag[jj];
|
| 219 |
+
}
|
| 220 |
+
}
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
__syncthreads();
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
for (i = 0; i < PYC_ADA_K128D_THREAD_TILE_M; ++i) {
|
| 227 |
+
int out_row = block_row + row_fragment + i;
|
| 228 |
+
if (out_row >= m) {
|
| 229 |
+
continue;
|
| 230 |
+
}
|
| 231 |
+
for (j = 0; j < PYC_ADA_K128D_THREAD_TILE_N; ++j) {
|
| 232 |
+
int out_col = block_col + col_fragment + j;
|
| 233 |
+
if (out_col < n) {
|
| 234 |
+
c[out_row * n + out_col] = accum[i][j];
|
| 235 |
+
}
|
| 236 |
+
}
|
| 237 |
+
}
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
static int configure_ada_kernel(void) {
|
| 241 |
+
cudaError_t status;
|
| 242 |
+
|
| 243 |
+
status = cudaFuncSetAttribute(
|
| 244 |
+
ada_fp32_k128dyn_gemm,
|
| 245 |
+
cudaFuncAttributePreferredSharedMemoryCarveout,
|
| 246 |
+
100);
|
| 247 |
+
if (status != cudaSuccess && status != cudaErrorNotSupported) {
|
| 248 |
+
fprintf(stderr, "cudaFuncSetAttribute carveout failed: %s\n", cudaGetErrorString(status));
|
| 249 |
+
return -1;
|
| 250 |
+
}
|
| 251 |
+
|
| 252 |
+
status = cudaFuncSetAttribute(
|
| 253 |
+
ada_fp32_k128dyn_gemm,
|
| 254 |
+
cudaFuncAttributeMaxDynamicSharedMemorySize,
|
| 255 |
+
PYC_ADA_K128D_SHARED_BYTES);
|
| 256 |
+
if (status != cudaSuccess && status != cudaErrorNotSupported) {
|
| 257 |
+
fprintf(stderr, "cudaFuncSetAttribute dynamic shared failed: %s\n", cudaGetErrorString(status));
|
| 258 |
+
return -1;
|
| 259 |
+
}
|
| 260 |
+
|
| 261 |
+
return 0;
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
static int parse_int_arg(const char* text, int* out) {
|
| 265 |
+
char* end = NULL;
|
| 266 |
+
long value;
|
| 267 |
+
|
| 268 |
+
if (!text || !out) {
|
| 269 |
+
return -1;
|
| 270 |
+
}
|
| 271 |
+
|
| 272 |
+
value = strtol(text, &end, 10);
|
| 273 |
+
if (*text == '\0' || !end || *end != '\0' || value <= 0 || value > 1 << 20) {
|
| 274 |
+
return -1;
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
*out = (int)value;
|
| 278 |
+
return 0;
|
| 279 |
+
}
|
| 280 |
+
|
| 281 |
+
static int parse_config(int argc, char** argv, ada_gemm_k128d_config* cfg) {
|
| 282 |
+
if (!cfg) {
|
| 283 |
+
return -1;
|
| 284 |
+
}
|
| 285 |
+
|
| 286 |
+
cfg->m = 1024;
|
| 287 |
+
cfg->n = 1024;
|
| 288 |
+
cfg->k = 1024;
|
| 289 |
+
cfg->warmup = 10;
|
| 290 |
+
cfg->iters = 50;
|
| 291 |
+
|
| 292 |
+
if (argc > 1 && parse_int_arg(argv[1], &cfg->m) != 0) return -1;
|
| 293 |
+
if (argc > 2 && parse_int_arg(argv[2], &cfg->n) != 0) return -1;
|
| 294 |
+
if (argc > 3 && parse_int_arg(argv[3], &cfg->k) != 0) return -1;
|
| 295 |
+
if (argc > 4 && parse_int_arg(argv[4], &cfg->warmup) != 0) return -1;
|
| 296 |
+
if (argc > 5 && parse_int_arg(argv[5], &cfg->iters) != 0) return -1;
|
| 297 |
+
|
| 298 |
+
return 0;
|
| 299 |
+
}
|
| 300 |
+
|
| 301 |
+
int main(int argc, char** argv) {
|
| 302 |
+
ada_gemm_k128d_config cfg;
|
| 303 |
+
cudaDeviceProp props;
|
| 304 |
+
float* host_a = NULL;
|
| 305 |
+
float* host_b = NULL;
|
| 306 |
+
float* host_c = NULL;
|
| 307 |
+
float* ref_c = NULL;
|
| 308 |
+
float* dev_a = NULL;
|
| 309 |
+
float* dev_b = NULL;
|
| 310 |
+
float* dev_c = NULL;
|
| 311 |
+
cudaEvent_t start = NULL;
|
| 312 |
+
cudaEvent_t stop = NULL;
|
| 313 |
+
size_t a_bytes;
|
| 314 |
+
size_t b_bytes;
|
| 315 |
+
size_t c_bytes;
|
| 316 |
+
dim3 block;
|
| 317 |
+
dim3 grid;
|
| 318 |
+
float elapsed_ms = 0.0f;
|
| 319 |
+
double best_ms = 0.0;
|
| 320 |
+
int iter;
|
| 321 |
+
double max_abs_diff = 0.0;
|
| 322 |
+
int device = 0;
|
| 323 |
+
|
| 324 |
+
if (parse_config(argc, argv, &cfg) != 0) {
|
| 325 |
+
fprintf(stderr, "usage: %s [m] [n] [k] [warmup] [iters]\n", argv[0]);
|
| 326 |
+
return 2;
|
| 327 |
+
}
|
| 328 |
+
|
| 329 |
+
if (check_cuda(cudaGetDevice(&device), "cudaGetDevice") != 0) return 1;
|
| 330 |
+
if (check_cuda(cudaGetDeviceProperties(&props, device), "cudaGetDeviceProperties") != 0) return 1;
|
| 331 |
+
|
| 332 |
+
printf("device=%s cc=%d.%d\n", props.name, props.major, props.minor);
|
| 333 |
+
printf("dynamic_shared_bytes=%d\n", PYC_ADA_K128D_SHARED_BYTES);
|
| 334 |
+
if (!(props.major == 8 && props.minor == 9)) {
|
| 335 |
+
printf("note=prototype tuned for Ada (sm_89); running on a different architecture\n");
|
| 336 |
+
}
|
| 337 |
+
|
| 338 |
+
a_bytes = (size_t)cfg.m * (size_t)cfg.k * sizeof(float);
|
| 339 |
+
b_bytes = (size_t)cfg.k * (size_t)cfg.n * sizeof(float);
|
| 340 |
+
c_bytes = (size_t)cfg.m * (size_t)cfg.n * sizeof(float);
|
| 341 |
+
|
| 342 |
+
host_a = (float*)malloc(a_bytes);
|
| 343 |
+
host_b = (float*)malloc(b_bytes);
|
| 344 |
+
host_c = (float*)malloc(c_bytes);
|
| 345 |
+
ref_c = (float*)malloc(c_bytes);
|
| 346 |
+
if (!host_a || !host_b || !host_c || !ref_c) {
|
| 347 |
+
fprintf(stderr, "host allocation failed\n");
|
| 348 |
+
return 1;
|
| 349 |
+
}
|
| 350 |
+
|
| 351 |
+
fill_matrix(host_a, cfg.m, cfg.k, 0.03125f);
|
| 352 |
+
fill_matrix(host_b, cfg.k, cfg.n, 0.0625f);
|
| 353 |
+
reference_gemm(host_a, host_b, ref_c, cfg.m, cfg.n, cfg.k);
|
| 354 |
+
|
| 355 |
+
if (check_cuda(cudaMalloc((void**)&dev_a, a_bytes), "cudaMalloc(a)") != 0) return 1;
|
| 356 |
+
if (check_cuda(cudaMalloc((void**)&dev_b, b_bytes), "cudaMalloc(b)") != 0) return 1;
|
| 357 |
+
if (check_cuda(cudaMalloc((void**)&dev_c, c_bytes), "cudaMalloc(c)") != 0) return 1;
|
| 358 |
+
|
| 359 |
+
if (check_cuda(cudaMemcpy(dev_a, host_a, a_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(a)") != 0) return 1;
|
| 360 |
+
if (check_cuda(cudaMemcpy(dev_b, host_b, b_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(b)") != 0) return 1;
|
| 361 |
+
|
| 362 |
+
if (configure_ada_kernel() != 0) return 1;
|
| 363 |
+
|
| 364 |
+
block = dim3(PYC_ADA_K128D_THREADS_X, PYC_ADA_K128D_THREADS_Y, 1);
|
| 365 |
+
grid = dim3(
|
| 366 |
+
(unsigned int)((cfg.n + PYC_ADA_K128D_BLOCK_N - 1) / PYC_ADA_K128D_BLOCK_N),
|
| 367 |
+
(unsigned int)((cfg.m + PYC_ADA_K128D_BLOCK_M - 1) / PYC_ADA_K128D_BLOCK_M),
|
| 368 |
+
1);
|
| 369 |
+
|
| 370 |
+
if (check_cuda(cudaEventCreate(&start), "cudaEventCreate(start)") != 0) return 1;
|
| 371 |
+
if (check_cuda(cudaEventCreate(&stop), "cudaEventCreate(stop)") != 0) return 1;
|
| 372 |
+
|
| 373 |
+
for (iter = 0; iter < cfg.warmup; ++iter) {
|
| 374 |
+
ada_fp32_k128dyn_gemm<<<grid, block, PYC_ADA_K128D_SHARED_BYTES>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 375 |
+
}
|
| 376 |
+
if (check_cuda(cudaGetLastError(), "kernel launch warmup") != 0) return 1;
|
| 377 |
+
if (check_cuda(cudaDeviceSynchronize(), "cudaDeviceSynchronize warmup") != 0) return 1;
|
| 378 |
+
|
| 379 |
+
best_ms = 0.0;
|
| 380 |
+
for (iter = 0; iter < cfg.iters; ++iter) {
|
| 381 |
+
if (check_cuda(cudaEventRecord(start), "cudaEventRecord(start)") != 0) return 1;
|
| 382 |
+
ada_fp32_k128dyn_gemm<<<grid, block, PYC_ADA_K128D_SHARED_BYTES>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 383 |
+
if (check_cuda(cudaEventRecord(stop), "cudaEventRecord(stop)") != 0) return 1;
|
| 384 |
+
if (check_cuda(cudaEventSynchronize(stop), "cudaEventSynchronize(stop)") != 0) return 1;
|
| 385 |
+
if (check_cuda(cudaEventElapsedTime(&elapsed_ms, start, stop), "cudaEventElapsedTime") != 0) return 1;
|
| 386 |
+
if (iter == 0 || elapsed_ms < (float)best_ms) {
|
| 387 |
+
best_ms = elapsed_ms;
|
| 388 |
+
}
|
| 389 |
+
}
|
| 390 |
+
|
| 391 |
+
if (check_cuda(cudaMemcpy(host_c, dev_c, c_bytes, cudaMemcpyDeviceToHost), "cudaMemcpy(c)") != 0) return 1;
|
| 392 |
+
|
| 393 |
+
for (iter = 0; iter < cfg.m * cfg.n; ++iter) {
|
| 394 |
+
double diff = fabs((double)host_c[iter] - (double)ref_c[iter]);
|
| 395 |
+
if (diff > max_abs_diff) {
|
| 396 |
+
max_abs_diff = diff;
|
| 397 |
+
}
|
| 398 |
+
}
|
| 399 |
+
|
| 400 |
+
printf("shape=%dx%dx%d\n", cfg.m, cfg.n, cfg.k);
|
| 401 |
+
printf("tile=%dx%dx%d threads=%dx%d thread_tile=%dx%d vector_width=%d\n",
|
| 402 |
+
PYC_ADA_K128D_BLOCK_M,
|
| 403 |
+
PYC_ADA_K128D_BLOCK_N,
|
| 404 |
+
PYC_ADA_K128D_BLOCK_K,
|
| 405 |
+
PYC_ADA_K128D_THREADS_X,
|
| 406 |
+
PYC_ADA_K128D_THREADS_Y,
|
| 407 |
+
PYC_ADA_K128D_THREAD_TILE_M,
|
| 408 |
+
PYC_ADA_K128D_THREAD_TILE_N,
|
| 409 |
+
PYC_ADA_K128D_VEC);
|
| 410 |
+
printf("best_ms=%.3f\n", best_ms);
|
| 411 |
+
printf("max_abs_diff=%.6f\n", max_abs_diff);
|
| 412 |
+
if (best_ms > 0.0) {
|
| 413 |
+
double flops = 2.0 * (double)cfg.m * (double)cfg.n * (double)cfg.k;
|
| 414 |
+
double gflops = flops / (best_ms * 1.0e6);
|
| 415 |
+
printf("gflops=%.3f\n", gflops);
|
| 416 |
+
}
|
| 417 |
+
|
| 418 |
+
cudaEventDestroy(start);
|
| 419 |
+
cudaEventDestroy(stop);
|
| 420 |
+
cudaFree(dev_a);
|
| 421 |
+
cudaFree(dev_b);
|
| 422 |
+
cudaFree(dev_c);
|
| 423 |
+
free(host_a);
|
| 424 |
+
free(host_b);
|
| 425 |
+
free(host_c);
|
| 426 |
+
free(ref_c);
|
| 427 |
+
return max_abs_diff <= 1e-2 ? 0 : 1;
|
| 428 |
+
}
|
kernels/prototypes/ada/gemm_k32/kernel.cu
ADDED
|
@@ -0,0 +1,394 @@
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <cuda_runtime.h>
|
| 2 |
+
|
| 3 |
+
#include <math.h>
|
| 4 |
+
#include <stdint.h>
|
| 5 |
+
#include <stdio.h>
|
| 6 |
+
#include <stdlib.h>
|
| 7 |
+
|
| 8 |
+
#define PYC_ADA_K32_BLOCK_M 64
|
| 9 |
+
#define PYC_ADA_K32_BLOCK_N 64
|
| 10 |
+
#define PYC_ADA_K32_BLOCK_K 32
|
| 11 |
+
#define PYC_ADA_K32_THREADS_X 16
|
| 12 |
+
#define PYC_ADA_K32_THREADS_Y 16
|
| 13 |
+
#define PYC_ADA_K32_THREAD_TILE_M 4
|
| 14 |
+
#define PYC_ADA_K32_THREAD_TILE_N 4
|
| 15 |
+
#define PYC_ADA_K32_VEC 4
|
| 16 |
+
|
| 17 |
+
typedef struct {
|
| 18 |
+
int m;
|
| 19 |
+
int n;
|
| 20 |
+
int k;
|
| 21 |
+
int warmup;
|
| 22 |
+
int iters;
|
| 23 |
+
} ada_gemm_k32_config;
|
| 24 |
+
|
| 25 |
+
static int check_cuda(cudaError_t status, const char* what) {
|
| 26 |
+
if (status != cudaSuccess) {
|
| 27 |
+
fprintf(stderr, "%s failed: %s\n", what, cudaGetErrorString(status));
|
| 28 |
+
return -1;
|
| 29 |
+
}
|
| 30 |
+
return 0;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
static void fill_matrix(float* data, int rows, int cols, float scale) {
|
| 34 |
+
int i;
|
| 35 |
+
for (i = 0; i < rows * cols; ++i) {
|
| 36 |
+
int pattern = (i * 17 + rows * 13 + cols * 7) % 31;
|
| 37 |
+
data[i] = ((float)pattern - 15.0f) * scale;
|
| 38 |
+
}
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
static void reference_gemm(
|
| 42 |
+
const float* a,
|
| 43 |
+
const float* b,
|
| 44 |
+
float* c,
|
| 45 |
+
int m,
|
| 46 |
+
int n,
|
| 47 |
+
int k) {
|
| 48 |
+
int row;
|
| 49 |
+
for (row = 0; row < m; ++row) {
|
| 50 |
+
int col;
|
| 51 |
+
for (col = 0; col < n; ++col) {
|
| 52 |
+
float acc = 0.0f;
|
| 53 |
+
int kk;
|
| 54 |
+
for (kk = 0; kk < k; ++kk) {
|
| 55 |
+
acc += a[row * k + kk] * b[kk * n + col];
|
| 56 |
+
}
|
| 57 |
+
c[row * n + col] = acc;
|
| 58 |
+
}
|
| 59 |
+
}
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
__device__ static void load_a_vec(
|
| 63 |
+
const float* __restrict__ a,
|
| 64 |
+
float shared_a[PYC_ADA_K32_BLOCK_M][PYC_ADA_K32_BLOCK_K + 1],
|
| 65 |
+
int lane_linear,
|
| 66 |
+
int block_row,
|
| 67 |
+
int kk_base,
|
| 68 |
+
int m,
|
| 69 |
+
int k) {
|
| 70 |
+
const int vecs_per_row = PYC_ADA_K32_BLOCK_K / PYC_ADA_K32_VEC;
|
| 71 |
+
const int total_vecs = (PYC_ADA_K32_BLOCK_M * PYC_ADA_K32_BLOCK_K) / PYC_ADA_K32_VEC;
|
| 72 |
+
int phase;
|
| 73 |
+
|
| 74 |
+
for (phase = 0; phase < total_vecs / (PYC_ADA_K32_THREADS_X * PYC_ADA_K32_THREADS_Y); ++phase) {
|
| 75 |
+
const int linear = lane_linear + phase * (PYC_ADA_K32_THREADS_X * PYC_ADA_K32_THREADS_Y);
|
| 76 |
+
const int tile_row = linear / vecs_per_row;
|
| 77 |
+
const int tile_col = (linear % vecs_per_row) * PYC_ADA_K32_VEC;
|
| 78 |
+
const int global_row = block_row + tile_row;
|
| 79 |
+
const int global_col = kk_base + tile_col;
|
| 80 |
+
int i;
|
| 81 |
+
|
| 82 |
+
if (global_row < m && global_col + (PYC_ADA_K32_VEC - 1) < k) {
|
| 83 |
+
const float4 value = *reinterpret_cast<const float4*>(&a[global_row * k + global_col]);
|
| 84 |
+
shared_a[tile_row][tile_col + 0] = value.x;
|
| 85 |
+
shared_a[tile_row][tile_col + 1] = value.y;
|
| 86 |
+
shared_a[tile_row][tile_col + 2] = value.z;
|
| 87 |
+
shared_a[tile_row][tile_col + 3] = value.w;
|
| 88 |
+
continue;
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
for (i = 0; i < PYC_ADA_K32_VEC; ++i) {
|
| 92 |
+
float value = 0.0f;
|
| 93 |
+
if (global_row < m && global_col + i < k) {
|
| 94 |
+
value = a[global_row * k + global_col + i];
|
| 95 |
+
}
|
| 96 |
+
shared_a[tile_row][tile_col + i] = value;
|
| 97 |
+
}
|
| 98 |
+
}
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
__device__ static void load_b_vec(
|
| 102 |
+
const float* __restrict__ b,
|
| 103 |
+
float shared_b[PYC_ADA_K32_BLOCK_K][PYC_ADA_K32_BLOCK_N + 1],
|
| 104 |
+
int lane_linear,
|
| 105 |
+
int block_col,
|
| 106 |
+
int kk_base,
|
| 107 |
+
int k,
|
| 108 |
+
int n) {
|
| 109 |
+
const int vecs_per_row = PYC_ADA_K32_BLOCK_N / PYC_ADA_K32_VEC;
|
| 110 |
+
const int total_vecs = (PYC_ADA_K32_BLOCK_K * PYC_ADA_K32_BLOCK_N) / PYC_ADA_K32_VEC;
|
| 111 |
+
int phase;
|
| 112 |
+
|
| 113 |
+
for (phase = 0; phase < total_vecs / (PYC_ADA_K32_THREADS_X * PYC_ADA_K32_THREADS_Y); ++phase) {
|
| 114 |
+
const int linear = lane_linear + phase * (PYC_ADA_K32_THREADS_X * PYC_ADA_K32_THREADS_Y);
|
| 115 |
+
const int tile_row = linear / vecs_per_row;
|
| 116 |
+
const int tile_col = (linear % vecs_per_row) * PYC_ADA_K32_VEC;
|
| 117 |
+
const int global_row = kk_base + tile_row;
|
| 118 |
+
const int global_col = block_col + tile_col;
|
| 119 |
+
int i;
|
| 120 |
+
|
| 121 |
+
if (global_row < k && global_col + (PYC_ADA_K32_VEC - 1) < n) {
|
| 122 |
+
const float4 value = *reinterpret_cast<const float4*>(&b[global_row * n + global_col]);
|
| 123 |
+
shared_b[tile_row][tile_col + 0] = value.x;
|
| 124 |
+
shared_b[tile_row][tile_col + 1] = value.y;
|
| 125 |
+
shared_b[tile_row][tile_col + 2] = value.z;
|
| 126 |
+
shared_b[tile_row][tile_col + 3] = value.w;
|
| 127 |
+
continue;
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
for (i = 0; i < PYC_ADA_K32_VEC; ++i) {
|
| 131 |
+
float value = 0.0f;
|
| 132 |
+
if (global_row < k && global_col + i < n) {
|
| 133 |
+
value = b[global_row * n + global_col + i];
|
| 134 |
+
}
|
| 135 |
+
shared_b[tile_row][tile_col + i] = value;
|
| 136 |
+
}
|
| 137 |
+
}
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
__launch_bounds__(PYC_ADA_K32_THREADS_X * PYC_ADA_K32_THREADS_Y, 2)
|
| 141 |
+
__global__ void ada_fp32_k32_gemm(
|
| 142 |
+
const float* __restrict__ a,
|
| 143 |
+
const float* __restrict__ b,
|
| 144 |
+
float* __restrict__ c,
|
| 145 |
+
int m,
|
| 146 |
+
int n,
|
| 147 |
+
int k) {
|
| 148 |
+
__shared__ float shared_a[PYC_ADA_K32_BLOCK_M][PYC_ADA_K32_BLOCK_K + 1];
|
| 149 |
+
__shared__ float shared_b[PYC_ADA_K32_BLOCK_K][PYC_ADA_K32_BLOCK_N + 1];
|
| 150 |
+
|
| 151 |
+
const int lane_linear = threadIdx.y * blockDim.x + threadIdx.x;
|
| 152 |
+
const int block_row = blockIdx.y * PYC_ADA_K32_BLOCK_M;
|
| 153 |
+
const int block_col = blockIdx.x * PYC_ADA_K32_BLOCK_N;
|
| 154 |
+
const int row_fragment = threadIdx.y * PYC_ADA_K32_THREAD_TILE_M;
|
| 155 |
+
const int col_fragment = threadIdx.x * PYC_ADA_K32_THREAD_TILE_N;
|
| 156 |
+
float accum[PYC_ADA_K32_THREAD_TILE_M][PYC_ADA_K32_THREAD_TILE_N];
|
| 157 |
+
int kk_base;
|
| 158 |
+
int i;
|
| 159 |
+
int j;
|
| 160 |
+
|
| 161 |
+
for (i = 0; i < PYC_ADA_K32_THREAD_TILE_M; ++i) {
|
| 162 |
+
for (j = 0; j < PYC_ADA_K32_THREAD_TILE_N; ++j) {
|
| 163 |
+
accum[i][j] = 0.0f;
|
| 164 |
+
}
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
for (kk_base = 0; kk_base < k; kk_base += PYC_ADA_K32_BLOCK_K) {
|
| 168 |
+
load_a_vec(a, shared_a, lane_linear, block_row, kk_base, m, k);
|
| 169 |
+
load_b_vec(b, shared_b, lane_linear, block_col, kk_base, k, n);
|
| 170 |
+
|
| 171 |
+
__syncthreads();
|
| 172 |
+
|
| 173 |
+
#pragma unroll
|
| 174 |
+
for (i = 0; i < PYC_ADA_K32_BLOCK_K; ++i) {
|
| 175 |
+
float a_frag[PYC_ADA_K32_THREAD_TILE_M];
|
| 176 |
+
float b_frag[PYC_ADA_K32_THREAD_TILE_N];
|
| 177 |
+
int ii;
|
| 178 |
+
|
| 179 |
+
#pragma unroll
|
| 180 |
+
for (ii = 0; ii < PYC_ADA_K32_THREAD_TILE_M; ++ii) {
|
| 181 |
+
a_frag[ii] = shared_a[row_fragment + ii][i];
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
#pragma unroll
|
| 185 |
+
for (ii = 0; ii < PYC_ADA_K32_THREAD_TILE_N; ++ii) {
|
| 186 |
+
b_frag[ii] = shared_b[i][col_fragment + ii];
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
#pragma unroll
|
| 190 |
+
for (ii = 0; ii < PYC_ADA_K32_THREAD_TILE_M; ++ii) {
|
| 191 |
+
int jj;
|
| 192 |
+
#pragma unroll
|
| 193 |
+
for (jj = 0; jj < PYC_ADA_K32_THREAD_TILE_N; ++jj) {
|
| 194 |
+
accum[ii][jj] += a_frag[ii] * b_frag[jj];
|
| 195 |
+
}
|
| 196 |
+
}
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
__syncthreads();
|
| 200 |
+
}
|
| 201 |
+
|
| 202 |
+
for (i = 0; i < PYC_ADA_K32_THREAD_TILE_M; ++i) {
|
| 203 |
+
int out_row = block_row + row_fragment + i;
|
| 204 |
+
if (out_row >= m) {
|
| 205 |
+
continue;
|
| 206 |
+
}
|
| 207 |
+
for (j = 0; j < PYC_ADA_K32_THREAD_TILE_N; ++j) {
|
| 208 |
+
int out_col = block_col + col_fragment + j;
|
| 209 |
+
if (out_col < n) {
|
| 210 |
+
c[out_row * n + out_col] = accum[i][j];
|
| 211 |
+
}
|
| 212 |
+
}
|
| 213 |
+
}
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
static int configure_ada_kernel(void) {
|
| 217 |
+
cudaError_t status;
|
| 218 |
+
|
| 219 |
+
status = cudaFuncSetAttribute(
|
| 220 |
+
ada_fp32_k32_gemm,
|
| 221 |
+
cudaFuncAttributePreferredSharedMemoryCarveout,
|
| 222 |
+
100);
|
| 223 |
+
if (status != cudaSuccess && status != cudaErrorNotSupported) {
|
| 224 |
+
fprintf(stderr, "cudaFuncSetAttribute failed: %s\n", cudaGetErrorString(status));
|
| 225 |
+
return -1;
|
| 226 |
+
}
|
| 227 |
+
|
| 228 |
+
return 0;
|
| 229 |
+
}
|
| 230 |
+
|
| 231 |
+
static int parse_int_arg(const char* text, int* out) {
|
| 232 |
+
char* end = NULL;
|
| 233 |
+
long value;
|
| 234 |
+
|
| 235 |
+
if (!text || !out) {
|
| 236 |
+
return -1;
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
value = strtol(text, &end, 10);
|
| 240 |
+
if (*text == '\0' || !end || *end != '\0' || value <= 0 || value > 1 << 20) {
|
| 241 |
+
return -1;
|
| 242 |
+
}
|
| 243 |
+
|
| 244 |
+
*out = (int)value;
|
| 245 |
+
return 0;
|
| 246 |
+
}
|
| 247 |
+
|
| 248 |
+
static int parse_config(int argc, char** argv, ada_gemm_k32_config* cfg) {
|
| 249 |
+
if (!cfg) {
|
| 250 |
+
return -1;
|
| 251 |
+
}
|
| 252 |
+
|
| 253 |
+
cfg->m = 1024;
|
| 254 |
+
cfg->n = 1024;
|
| 255 |
+
cfg->k = 1024;
|
| 256 |
+
cfg->warmup = 10;
|
| 257 |
+
cfg->iters = 50;
|
| 258 |
+
|
| 259 |
+
if (argc > 1 && parse_int_arg(argv[1], &cfg->m) != 0) return -1;
|
| 260 |
+
if (argc > 2 && parse_int_arg(argv[2], &cfg->n) != 0) return -1;
|
| 261 |
+
if (argc > 3 && parse_int_arg(argv[3], &cfg->k) != 0) return -1;
|
| 262 |
+
if (argc > 4 && parse_int_arg(argv[4], &cfg->warmup) != 0) return -1;
|
| 263 |
+
if (argc > 5 && parse_int_arg(argv[5], &cfg->iters) != 0) return -1;
|
| 264 |
+
|
| 265 |
+
return 0;
|
| 266 |
+
}
|
| 267 |
+
|
| 268 |
+
int main(int argc, char** argv) {
|
| 269 |
+
ada_gemm_k32_config cfg;
|
| 270 |
+
cudaDeviceProp props;
|
| 271 |
+
float* host_a = NULL;
|
| 272 |
+
float* host_b = NULL;
|
| 273 |
+
float* host_c = NULL;
|
| 274 |
+
float* ref_c = NULL;
|
| 275 |
+
float* dev_a = NULL;
|
| 276 |
+
float* dev_b = NULL;
|
| 277 |
+
float* dev_c = NULL;
|
| 278 |
+
cudaEvent_t start = NULL;
|
| 279 |
+
cudaEvent_t stop = NULL;
|
| 280 |
+
size_t a_bytes;
|
| 281 |
+
size_t b_bytes;
|
| 282 |
+
size_t c_bytes;
|
| 283 |
+
dim3 block;
|
| 284 |
+
dim3 grid;
|
| 285 |
+
float elapsed_ms = 0.0f;
|
| 286 |
+
double best_ms = 0.0;
|
| 287 |
+
int iter;
|
| 288 |
+
double max_abs_diff = 0.0;
|
| 289 |
+
int device = 0;
|
| 290 |
+
|
| 291 |
+
if (parse_config(argc, argv, &cfg) != 0) {
|
| 292 |
+
fprintf(stderr, "usage: %s [m] [n] [k] [warmup] [iters]\n", argv[0]);
|
| 293 |
+
return 2;
|
| 294 |
+
}
|
| 295 |
+
|
| 296 |
+
if (check_cuda(cudaGetDevice(&device), "cudaGetDevice") != 0) return 1;
|
| 297 |
+
if (check_cuda(cudaGetDeviceProperties(&props, device), "cudaGetDeviceProperties") != 0) return 1;
|
| 298 |
+
|
| 299 |
+
printf("device=%s cc=%d.%d\n", props.name, props.major, props.minor);
|
| 300 |
+
if (!(props.major == 8 && props.minor == 9)) {
|
| 301 |
+
printf("note=prototype tuned for Ada (sm_89); running on a different architecture\n");
|
| 302 |
+
}
|
| 303 |
+
|
| 304 |
+
a_bytes = (size_t)cfg.m * (size_t)cfg.k * sizeof(float);
|
| 305 |
+
b_bytes = (size_t)cfg.k * (size_t)cfg.n * sizeof(float);
|
| 306 |
+
c_bytes = (size_t)cfg.m * (size_t)cfg.n * sizeof(float);
|
| 307 |
+
|
| 308 |
+
host_a = (float*)malloc(a_bytes);
|
| 309 |
+
host_b = (float*)malloc(b_bytes);
|
| 310 |
+
host_c = (float*)malloc(c_bytes);
|
| 311 |
+
ref_c = (float*)malloc(c_bytes);
|
| 312 |
+
if (!host_a || !host_b || !host_c || !ref_c) {
|
| 313 |
+
fprintf(stderr, "host allocation failed\n");
|
| 314 |
+
return 1;
|
| 315 |
+
}
|
| 316 |
+
|
| 317 |
+
fill_matrix(host_a, cfg.m, cfg.k, 0.03125f);
|
| 318 |
+
fill_matrix(host_b, cfg.k, cfg.n, 0.0625f);
|
| 319 |
+
reference_gemm(host_a, host_b, ref_c, cfg.m, cfg.n, cfg.k);
|
| 320 |
+
|
| 321 |
+
if (check_cuda(cudaMalloc((void**)&dev_a, a_bytes), "cudaMalloc(a)") != 0) return 1;
|
| 322 |
+
if (check_cuda(cudaMalloc((void**)&dev_b, b_bytes), "cudaMalloc(b)") != 0) return 1;
|
| 323 |
+
if (check_cuda(cudaMalloc((void**)&dev_c, c_bytes), "cudaMalloc(c)") != 0) return 1;
|
| 324 |
+
|
| 325 |
+
if (check_cuda(cudaMemcpy(dev_a, host_a, a_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(a)") != 0) return 1;
|
| 326 |
+
if (check_cuda(cudaMemcpy(dev_b, host_b, b_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(b)") != 0) return 1;
|
| 327 |
+
|
| 328 |
+
if (configure_ada_kernel() != 0) return 1;
|
| 329 |
+
|
| 330 |
+
block = dim3(PYC_ADA_K32_THREADS_X, PYC_ADA_K32_THREADS_Y, 1);
|
| 331 |
+
grid = dim3(
|
| 332 |
+
(unsigned int)((cfg.n + PYC_ADA_K32_BLOCK_N - 1) / PYC_ADA_K32_BLOCK_N),
|
| 333 |
+
(unsigned int)((cfg.m + PYC_ADA_K32_BLOCK_M - 1) / PYC_ADA_K32_BLOCK_M),
|
| 334 |
+
1);
|
| 335 |
+
|
| 336 |
+
if (check_cuda(cudaEventCreate(&start), "cudaEventCreate(start)") != 0) return 1;
|
| 337 |
+
if (check_cuda(cudaEventCreate(&stop), "cudaEventCreate(stop)") != 0) return 1;
|
| 338 |
+
|
| 339 |
+
for (iter = 0; iter < cfg.warmup; ++iter) {
|
| 340 |
+
ada_fp32_k32_gemm<<<grid, block>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 341 |
+
}
|
| 342 |
+
if (check_cuda(cudaGetLastError(), "kernel launch warmup") != 0) return 1;
|
| 343 |
+
if (check_cuda(cudaDeviceSynchronize(), "cudaDeviceSynchronize warmup") != 0) return 1;
|
| 344 |
+
|
| 345 |
+
best_ms = 0.0;
|
| 346 |
+
for (iter = 0; iter < cfg.iters; ++iter) {
|
| 347 |
+
if (check_cuda(cudaEventRecord(start), "cudaEventRecord(start)") != 0) return 1;
|
| 348 |
+
ada_fp32_k32_gemm<<<grid, block>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 349 |
+
if (check_cuda(cudaEventRecord(stop), "cudaEventRecord(stop)") != 0) return 1;
|
| 350 |
+
if (check_cuda(cudaEventSynchronize(stop), "cudaEventSynchronize(stop)") != 0) return 1;
|
| 351 |
+
if (check_cuda(cudaEventElapsedTime(&elapsed_ms, start, stop), "cudaEventElapsedTime") != 0) return 1;
|
| 352 |
+
if (iter == 0 || elapsed_ms < (float)best_ms) {
|
| 353 |
+
best_ms = elapsed_ms;
|
| 354 |
+
}
|
| 355 |
+
}
|
| 356 |
+
|
| 357 |
+
if (check_cuda(cudaMemcpy(host_c, dev_c, c_bytes, cudaMemcpyDeviceToHost), "cudaMemcpy(c)") != 0) return 1;
|
| 358 |
+
|
| 359 |
+
for (iter = 0; iter < cfg.m * cfg.n; ++iter) {
|
| 360 |
+
double diff = fabs((double)host_c[iter] - (double)ref_c[iter]);
|
| 361 |
+
if (diff > max_abs_diff) {
|
| 362 |
+
max_abs_diff = diff;
|
| 363 |
+
}
|
| 364 |
+
}
|
| 365 |
+
|
| 366 |
+
printf("shape=%dx%dx%d\n", cfg.m, cfg.n, cfg.k);
|
| 367 |
+
printf("tile=%dx%dx%d threads=%dx%d thread_tile=%dx%d vector_width=%d\n",
|
| 368 |
+
PYC_ADA_K32_BLOCK_M,
|
| 369 |
+
PYC_ADA_K32_BLOCK_N,
|
| 370 |
+
PYC_ADA_K32_BLOCK_K,
|
| 371 |
+
PYC_ADA_K32_THREADS_X,
|
| 372 |
+
PYC_ADA_K32_THREADS_Y,
|
| 373 |
+
PYC_ADA_K32_THREAD_TILE_M,
|
| 374 |
+
PYC_ADA_K32_THREAD_TILE_N,
|
| 375 |
+
PYC_ADA_K32_VEC);
|
| 376 |
+
printf("best_ms=%.3f\n", best_ms);
|
| 377 |
+
printf("max_abs_diff=%.6f\n", max_abs_diff);
|
| 378 |
+
if (best_ms > 0.0) {
|
| 379 |
+
double flops = 2.0 * (double)cfg.m * (double)cfg.n * (double)cfg.k;
|
| 380 |
+
double gflops = flops / (best_ms * 1.0e6);
|
| 381 |
+
printf("gflops=%.3f\n", gflops);
|
| 382 |
+
}
|
| 383 |
+
|
| 384 |
+
cudaEventDestroy(start);
|
| 385 |
+
cudaEventDestroy(stop);
|
| 386 |
+
cudaFree(dev_a);
|
| 387 |
+
cudaFree(dev_b);
|
| 388 |
+
cudaFree(dev_c);
|
| 389 |
+
free(host_a);
|
| 390 |
+
free(host_b);
|
| 391 |
+
free(host_c);
|
| 392 |
+
free(ref_c);
|
| 393 |
+
return max_abs_diff <= 1e-2 ? 0 : 1;
|
| 394 |
+
}
|
kernels/prototypes/ada/gemm_k32_warp32/kernel.cu
ADDED
|
@@ -0,0 +1,388 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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| 10 |
+
#define PYC_ADA_K32WARP_BLOCK_K 32
|
| 11 |
+
#define PYC_ADA_K32WARP_THREADS_X 32
|
| 12 |
+
#define PYC_ADA_K32WARP_THREADS_Y 8
|
| 13 |
+
#define PYC_ADA_K32WARP_THREAD_TILE_M 8
|
| 14 |
+
#define PYC_ADA_K32WARP_THREAD_TILE_N 2
|
| 15 |
+
#define PYC_ADA_K32WARP_VEC 4
|
| 16 |
+
|
| 17 |
+
typedef struct {
|
| 18 |
+
int m;
|
| 19 |
+
int n;
|
| 20 |
+
int k;
|
| 21 |
+
int warmup;
|
| 22 |
+
int iters;
|
| 23 |
+
} ada_gemm_k32warp_config;
|
| 24 |
+
|
| 25 |
+
static int check_cuda(cudaError_t status, const char* what) {
|
| 26 |
+
if (status != cudaSuccess) {
|
| 27 |
+
fprintf(stderr, "%s failed: %s\n", what, cudaGetErrorString(status));
|
| 28 |
+
return -1;
|
| 29 |
+
}
|
| 30 |
+
return 0;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
static void fill_matrix(float* data, int rows, int cols, float scale) {
|
| 34 |
+
int i;
|
| 35 |
+
for (i = 0; i < rows * cols; ++i) {
|
| 36 |
+
int pattern = (i * 17 + rows * 13 + cols * 7) % 31;
|
| 37 |
+
data[i] = ((float)pattern - 15.0f) * scale;
|
| 38 |
+
}
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
static void reference_gemm(const float* a, const float* b, float* c, int m, int n, int k) {
|
| 42 |
+
int row;
|
| 43 |
+
for (row = 0; row < m; ++row) {
|
| 44 |
+
int col;
|
| 45 |
+
for (col = 0; col < n; ++col) {
|
| 46 |
+
float acc = 0.0f;
|
| 47 |
+
int kk;
|
| 48 |
+
for (kk = 0; kk < k; ++kk) {
|
| 49 |
+
acc += a[row * k + kk] * b[kk * n + col];
|
| 50 |
+
}
|
| 51 |
+
c[row * n + col] = acc;
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
__device__ static void load_a_vec(
|
| 57 |
+
const float* __restrict__ a,
|
| 58 |
+
float shared_a[PYC_ADA_K32WARP_BLOCK_M][PYC_ADA_K32WARP_BLOCK_K + 1],
|
| 59 |
+
int lane_linear,
|
| 60 |
+
int block_row,
|
| 61 |
+
int kk_base,
|
| 62 |
+
int m,
|
| 63 |
+
int k) {
|
| 64 |
+
const int vecs_per_row = PYC_ADA_K32WARP_BLOCK_K / PYC_ADA_K32WARP_VEC;
|
| 65 |
+
const int total_vecs = (PYC_ADA_K32WARP_BLOCK_M * PYC_ADA_K32WARP_BLOCK_K) / PYC_ADA_K32WARP_VEC;
|
| 66 |
+
int phase;
|
| 67 |
+
|
| 68 |
+
for (phase = 0; phase < total_vecs / (PYC_ADA_K32WARP_THREADS_X * PYC_ADA_K32WARP_THREADS_Y); ++phase) {
|
| 69 |
+
const int linear = lane_linear + phase * (PYC_ADA_K32WARP_THREADS_X * PYC_ADA_K32WARP_THREADS_Y);
|
| 70 |
+
const int tile_row = linear / vecs_per_row;
|
| 71 |
+
const int tile_col = (linear % vecs_per_row) * PYC_ADA_K32WARP_VEC;
|
| 72 |
+
const int global_row = block_row + tile_row;
|
| 73 |
+
const int global_col = kk_base + tile_col;
|
| 74 |
+
int i;
|
| 75 |
+
|
| 76 |
+
if (global_row < m && global_col + (PYC_ADA_K32WARP_VEC - 1) < k) {
|
| 77 |
+
const float4 value = *reinterpret_cast<const float4*>(&a[global_row * k + global_col]);
|
| 78 |
+
shared_a[tile_row][tile_col + 0] = value.x;
|
| 79 |
+
shared_a[tile_row][tile_col + 1] = value.y;
|
| 80 |
+
shared_a[tile_row][tile_col + 2] = value.z;
|
| 81 |
+
shared_a[tile_row][tile_col + 3] = value.w;
|
| 82 |
+
continue;
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
for (i = 0; i < PYC_ADA_K32WARP_VEC; ++i) {
|
| 86 |
+
float value = 0.0f;
|
| 87 |
+
if (global_row < m && global_col + i < k) {
|
| 88 |
+
value = a[global_row * k + global_col + i];
|
| 89 |
+
}
|
| 90 |
+
shared_a[tile_row][tile_col + i] = value;
|
| 91 |
+
}
|
| 92 |
+
}
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
__device__ static void load_b_vec(
|
| 96 |
+
const float* __restrict__ b,
|
| 97 |
+
float shared_b[PYC_ADA_K32WARP_BLOCK_K][PYC_ADA_K32WARP_BLOCK_N + 1],
|
| 98 |
+
int lane_linear,
|
| 99 |
+
int block_col,
|
| 100 |
+
int kk_base,
|
| 101 |
+
int k,
|
| 102 |
+
int n) {
|
| 103 |
+
const int vecs_per_row = PYC_ADA_K32WARP_BLOCK_N / PYC_ADA_K32WARP_VEC;
|
| 104 |
+
const int total_vecs = (PYC_ADA_K32WARP_BLOCK_K * PYC_ADA_K32WARP_BLOCK_N) / PYC_ADA_K32WARP_VEC;
|
| 105 |
+
int phase;
|
| 106 |
+
|
| 107 |
+
for (phase = 0; phase < total_vecs / (PYC_ADA_K32WARP_THREADS_X * PYC_ADA_K32WARP_THREADS_Y); ++phase) {
|
| 108 |
+
const int linear = lane_linear + phase * (PYC_ADA_K32WARP_THREADS_X * PYC_ADA_K32WARP_THREADS_Y);
|
| 109 |
+
const int tile_row = linear / vecs_per_row;
|
| 110 |
+
const int tile_col = (linear % vecs_per_row) * PYC_ADA_K32WARP_VEC;
|
| 111 |
+
const int global_row = kk_base + tile_row;
|
| 112 |
+
const int global_col = block_col + tile_col;
|
| 113 |
+
int i;
|
| 114 |
+
|
| 115 |
+
if (global_row < k && global_col + (PYC_ADA_K32WARP_VEC - 1) < n) {
|
| 116 |
+
const float4 value = *reinterpret_cast<const float4*>(&b[global_row * n + global_col]);
|
| 117 |
+
shared_b[tile_row][tile_col + 0] = value.x;
|
| 118 |
+
shared_b[tile_row][tile_col + 1] = value.y;
|
| 119 |
+
shared_b[tile_row][tile_col + 2] = value.z;
|
| 120 |
+
shared_b[tile_row][tile_col + 3] = value.w;
|
| 121 |
+
continue;
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
for (i = 0; i < PYC_ADA_K32WARP_VEC; ++i) {
|
| 125 |
+
float value = 0.0f;
|
| 126 |
+
if (global_row < k && global_col + i < n) {
|
| 127 |
+
value = b[global_row * n + global_col + i];
|
| 128 |
+
}
|
| 129 |
+
shared_b[tile_row][tile_col + i] = value;
|
| 130 |
+
}
|
| 131 |
+
}
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
__launch_bounds__(PYC_ADA_K32WARP_THREADS_X * PYC_ADA_K32WARP_THREADS_Y, 2)
|
| 135 |
+
__global__ void ada_fp32_k32warp_gemm(
|
| 136 |
+
const float* __restrict__ a,
|
| 137 |
+
const float* __restrict__ b,
|
| 138 |
+
float* __restrict__ c,
|
| 139 |
+
int m,
|
| 140 |
+
int n,
|
| 141 |
+
int k) {
|
| 142 |
+
__shared__ float shared_a[PYC_ADA_K32WARP_BLOCK_M][PYC_ADA_K32WARP_BLOCK_K + 1];
|
| 143 |
+
__shared__ float shared_b[PYC_ADA_K32WARP_BLOCK_K][PYC_ADA_K32WARP_BLOCK_N + 1];
|
| 144 |
+
|
| 145 |
+
const int lane_linear = threadIdx.y * blockDim.x + threadIdx.x;
|
| 146 |
+
const int block_row = blockIdx.y * PYC_ADA_K32WARP_BLOCK_M;
|
| 147 |
+
const int block_col = blockIdx.x * PYC_ADA_K32WARP_BLOCK_N;
|
| 148 |
+
const int row_fragment = threadIdx.y * PYC_ADA_K32WARP_THREAD_TILE_M;
|
| 149 |
+
const int col_fragment = threadIdx.x * PYC_ADA_K32WARP_THREAD_TILE_N;
|
| 150 |
+
float accum[PYC_ADA_K32WARP_THREAD_TILE_M][PYC_ADA_K32WARP_THREAD_TILE_N];
|
| 151 |
+
int kk_base;
|
| 152 |
+
int i;
|
| 153 |
+
int j;
|
| 154 |
+
|
| 155 |
+
for (i = 0; i < PYC_ADA_K32WARP_THREAD_TILE_M; ++i) {
|
| 156 |
+
for (j = 0; j < PYC_ADA_K32WARP_THREAD_TILE_N; ++j) {
|
| 157 |
+
accum[i][j] = 0.0f;
|
| 158 |
+
}
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
for (kk_base = 0; kk_base < k; kk_base += PYC_ADA_K32WARP_BLOCK_K) {
|
| 162 |
+
load_a_vec(a, shared_a, lane_linear, block_row, kk_base, m, k);
|
| 163 |
+
load_b_vec(b, shared_b, lane_linear, block_col, kk_base, k, n);
|
| 164 |
+
|
| 165 |
+
__syncthreads();
|
| 166 |
+
|
| 167 |
+
#pragma unroll
|
| 168 |
+
for (i = 0; i < PYC_ADA_K32WARP_BLOCK_K; ++i) {
|
| 169 |
+
float a_frag[PYC_ADA_K32WARP_THREAD_TILE_M];
|
| 170 |
+
float b_frag[PYC_ADA_K32WARP_THREAD_TILE_N];
|
| 171 |
+
int ii;
|
| 172 |
+
|
| 173 |
+
#pragma unroll
|
| 174 |
+
for (ii = 0; ii < PYC_ADA_K32WARP_THREAD_TILE_M; ++ii) {
|
| 175 |
+
a_frag[ii] = shared_a[row_fragment + ii][i];
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
#pragma unroll
|
| 179 |
+
for (ii = 0; ii < PYC_ADA_K32WARP_THREAD_TILE_N; ++ii) {
|
| 180 |
+
b_frag[ii] = shared_b[i][col_fragment + ii];
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
#pragma unroll
|
| 184 |
+
for (ii = 0; ii < PYC_ADA_K32WARP_THREAD_TILE_M; ++ii) {
|
| 185 |
+
int jj;
|
| 186 |
+
#pragma unroll
|
| 187 |
+
for (jj = 0; jj < PYC_ADA_K32WARP_THREAD_TILE_N; ++jj) {
|
| 188 |
+
accum[ii][jj] += a_frag[ii] * b_frag[jj];
|
| 189 |
+
}
|
| 190 |
+
}
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
__syncthreads();
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
for (i = 0; i < PYC_ADA_K32WARP_THREAD_TILE_M; ++i) {
|
| 197 |
+
int out_row = block_row + row_fragment + i;
|
| 198 |
+
if (out_row >= m) {
|
| 199 |
+
continue;
|
| 200 |
+
}
|
| 201 |
+
for (j = 0; j < PYC_ADA_K32WARP_THREAD_TILE_N; ++j) {
|
| 202 |
+
int out_col = block_col + col_fragment + j;
|
| 203 |
+
if (out_col < n) {
|
| 204 |
+
c[out_row * n + out_col] = accum[i][j];
|
| 205 |
+
}
|
| 206 |
+
}
|
| 207 |
+
}
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
static int configure_ada_kernel(void) {
|
| 211 |
+
cudaError_t status;
|
| 212 |
+
|
| 213 |
+
status = cudaFuncSetAttribute(
|
| 214 |
+
ada_fp32_k32warp_gemm,
|
| 215 |
+
cudaFuncAttributePreferredSharedMemoryCarveout,
|
| 216 |
+
100);
|
| 217 |
+
if (status != cudaSuccess && status != cudaErrorNotSupported) {
|
| 218 |
+
fprintf(stderr, "cudaFuncSetAttribute failed: %s\n", cudaGetErrorString(status));
|
| 219 |
+
return -1;
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
return 0;
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
+
static int parse_int_arg(const char* text, int* out) {
|
| 226 |
+
char* end = NULL;
|
| 227 |
+
long value;
|
| 228 |
+
|
| 229 |
+
if (!text || !out) {
|
| 230 |
+
return -1;
|
| 231 |
+
}
|
| 232 |
+
|
| 233 |
+
value = strtol(text, &end, 10);
|
| 234 |
+
if (*text == '\0' || !end || *end != '\0' || value <= 0 || value > 1 << 20) {
|
| 235 |
+
return -1;
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
*out = (int)value;
|
| 239 |
+
return 0;
|
| 240 |
+
}
|
| 241 |
+
|
| 242 |
+
static int parse_config(int argc, char** argv, ada_gemm_k32warp_config* cfg) {
|
| 243 |
+
if (!cfg) {
|
| 244 |
+
return -1;
|
| 245 |
+
}
|
| 246 |
+
|
| 247 |
+
cfg->m = 1024;
|
| 248 |
+
cfg->n = 1024;
|
| 249 |
+
cfg->k = 1024;
|
| 250 |
+
cfg->warmup = 10;
|
| 251 |
+
cfg->iters = 50;
|
| 252 |
+
|
| 253 |
+
if (argc > 1 && parse_int_arg(argv[1], &cfg->m) != 0) return -1;
|
| 254 |
+
if (argc > 2 && parse_int_arg(argv[2], &cfg->n) != 0) return -1;
|
| 255 |
+
if (argc > 3 && parse_int_arg(argv[3], &cfg->k) != 0) return -1;
|
| 256 |
+
if (argc > 4 && parse_int_arg(argv[4], &cfg->warmup) != 0) return -1;
|
| 257 |
+
if (argc > 5 && parse_int_arg(argv[5], &cfg->iters) != 0) return -1;
|
| 258 |
+
|
| 259 |
+
return 0;
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
int main(int argc, char** argv) {
|
| 263 |
+
ada_gemm_k32warp_config cfg;
|
| 264 |
+
cudaDeviceProp props;
|
| 265 |
+
float* host_a = NULL;
|
| 266 |
+
float* host_b = NULL;
|
| 267 |
+
float* host_c = NULL;
|
| 268 |
+
float* ref_c = NULL;
|
| 269 |
+
float* dev_a = NULL;
|
| 270 |
+
float* dev_b = NULL;
|
| 271 |
+
float* dev_c = NULL;
|
| 272 |
+
cudaEvent_t start = NULL;
|
| 273 |
+
cudaEvent_t stop = NULL;
|
| 274 |
+
size_t a_bytes;
|
| 275 |
+
size_t b_bytes;
|
| 276 |
+
size_t c_bytes;
|
| 277 |
+
dim3 block;
|
| 278 |
+
dim3 grid;
|
| 279 |
+
float elapsed_ms = 0.0f;
|
| 280 |
+
double best_ms = 0.0;
|
| 281 |
+
int iter;
|
| 282 |
+
double max_abs_diff = 0.0;
|
| 283 |
+
int device = 0;
|
| 284 |
+
|
| 285 |
+
if (parse_config(argc, argv, &cfg) != 0) {
|
| 286 |
+
fprintf(stderr, "usage: %s [m] [n] [k] [warmup] [iters]\n", argv[0]);
|
| 287 |
+
return 2;
|
| 288 |
+
}
|
| 289 |
+
|
| 290 |
+
if (check_cuda(cudaGetDevice(&device), "cudaGetDevice") != 0) return 1;
|
| 291 |
+
if (check_cuda(cudaGetDeviceProperties(&props, device), "cudaGetDeviceProperties") != 0) return 1;
|
| 292 |
+
|
| 293 |
+
printf("device=%s cc=%d.%d\n", props.name, props.major, props.minor);
|
| 294 |
+
if (!(props.major == 8 && props.minor == 9)) {
|
| 295 |
+
printf("note=prototype tuned for Ada (sm_89); running on a different architecture\n");
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
+
a_bytes = (size_t)cfg.m * (size_t)cfg.k * sizeof(float);
|
| 299 |
+
b_bytes = (size_t)cfg.k * (size_t)cfg.n * sizeof(float);
|
| 300 |
+
c_bytes = (size_t)cfg.m * (size_t)cfg.n * sizeof(float);
|
| 301 |
+
|
| 302 |
+
host_a = (float*)malloc(a_bytes);
|
| 303 |
+
host_b = (float*)malloc(b_bytes);
|
| 304 |
+
host_c = (float*)malloc(c_bytes);
|
| 305 |
+
ref_c = (float*)malloc(c_bytes);
|
| 306 |
+
if (!host_a || !host_b || !host_c || !ref_c) {
|
| 307 |
+
fprintf(stderr, "host allocation failed\n");
|
| 308 |
+
return 1;
|
| 309 |
+
}
|
| 310 |
+
|
| 311 |
+
fill_matrix(host_a, cfg.m, cfg.k, 0.03125f);
|
| 312 |
+
fill_matrix(host_b, cfg.k, cfg.n, 0.0625f);
|
| 313 |
+
reference_gemm(host_a, host_b, ref_c, cfg.m, cfg.n, cfg.k);
|
| 314 |
+
|
| 315 |
+
if (check_cuda(cudaMalloc((void**)&dev_a, a_bytes), "cudaMalloc(a)") != 0) return 1;
|
| 316 |
+
if (check_cuda(cudaMalloc((void**)&dev_b, b_bytes), "cudaMalloc(b)") != 0) return 1;
|
| 317 |
+
if (check_cuda(cudaMalloc((void**)&dev_c, c_bytes), "cudaMalloc(c)") != 0) return 1;
|
| 318 |
+
|
| 319 |
+
if (check_cuda(cudaMemcpy(dev_a, host_a, a_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(a)") != 0) return 1;
|
| 320 |
+
if (check_cuda(cudaMemcpy(dev_b, host_b, b_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(b)") != 0) return 1;
|
| 321 |
+
|
| 322 |
+
if (configure_ada_kernel() != 0) return 1;
|
| 323 |
+
|
| 324 |
+
block = dim3(PYC_ADA_K32WARP_THREADS_X, PYC_ADA_K32WARP_THREADS_Y, 1);
|
| 325 |
+
grid = dim3(
|
| 326 |
+
(unsigned int)((cfg.n + PYC_ADA_K32WARP_BLOCK_N - 1) / PYC_ADA_K32WARP_BLOCK_N),
|
| 327 |
+
(unsigned int)((cfg.m + PYC_ADA_K32WARP_BLOCK_M - 1) / PYC_ADA_K32WARP_BLOCK_M),
|
| 328 |
+
1);
|
| 329 |
+
|
| 330 |
+
if (check_cuda(cudaEventCreate(&start), "cudaEventCreate(start)") != 0) return 1;
|
| 331 |
+
if (check_cuda(cudaEventCreate(&stop), "cudaEventCreate(stop)") != 0) return 1;
|
| 332 |
+
|
| 333 |
+
for (iter = 0; iter < cfg.warmup; ++iter) {
|
| 334 |
+
ada_fp32_k32warp_gemm<<<grid, block>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 335 |
+
}
|
| 336 |
+
if (check_cuda(cudaGetLastError(), "kernel launch warmup") != 0) return 1;
|
| 337 |
+
if (check_cuda(cudaDeviceSynchronize(), "cudaDeviceSynchronize warmup") != 0) return 1;
|
| 338 |
+
|
| 339 |
+
best_ms = 0.0;
|
| 340 |
+
for (iter = 0; iter < cfg.iters; ++iter) {
|
| 341 |
+
if (check_cuda(cudaEventRecord(start), "cudaEventRecord(start)") != 0) return 1;
|
| 342 |
+
ada_fp32_k32warp_gemm<<<grid, block>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 343 |
+
if (check_cuda(cudaEventRecord(stop), "cudaEventRecord(stop)") != 0) return 1;
|
| 344 |
+
if (check_cuda(cudaEventSynchronize(stop), "cudaEventSynchronize(stop)") != 0) return 1;
|
| 345 |
+
if (check_cuda(cudaEventElapsedTime(&elapsed_ms, start, stop), "cudaEventElapsedTime") != 0) return 1;
|
| 346 |
+
if (iter == 0 || elapsed_ms < (float)best_ms) {
|
| 347 |
+
best_ms = elapsed_ms;
|
| 348 |
+
}
|
| 349 |
+
}
|
| 350 |
+
|
| 351 |
+
if (check_cuda(cudaMemcpy(host_c, dev_c, c_bytes, cudaMemcpyDeviceToHost), "cudaMemcpy(c)") != 0) return 1;
|
| 352 |
+
|
| 353 |
+
for (iter = 0; iter < cfg.m * cfg.n; ++iter) {
|
| 354 |
+
double diff = fabs((double)host_c[iter] - (double)ref_c[iter]);
|
| 355 |
+
if (diff > max_abs_diff) {
|
| 356 |
+
max_abs_diff = diff;
|
| 357 |
+
}
|
| 358 |
+
}
|
| 359 |
+
|
| 360 |
+
printf("shape=%dx%dx%d\n", cfg.m, cfg.n, cfg.k);
|
| 361 |
+
printf("tile=%dx%dx%d threads=%dx%d thread_tile=%dx%d vector_width=%d\n",
|
| 362 |
+
PYC_ADA_K32WARP_BLOCK_M,
|
| 363 |
+
PYC_ADA_K32WARP_BLOCK_N,
|
| 364 |
+
PYC_ADA_K32WARP_BLOCK_K,
|
| 365 |
+
PYC_ADA_K32WARP_THREADS_X,
|
| 366 |
+
PYC_ADA_K32WARP_THREADS_Y,
|
| 367 |
+
PYC_ADA_K32WARP_THREAD_TILE_M,
|
| 368 |
+
PYC_ADA_K32WARP_THREAD_TILE_N,
|
| 369 |
+
PYC_ADA_K32WARP_VEC);
|
| 370 |
+
printf("best_ms=%.3f\n", best_ms);
|
| 371 |
+
printf("max_abs_diff=%.6f\n", max_abs_diff);
|
| 372 |
+
if (best_ms > 0.0) {
|
| 373 |
+
double flops = 2.0 * (double)cfg.m * (double)cfg.n * (double)cfg.k;
|
| 374 |
+
double gflops = flops / (best_ms * 1.0e6);
|
| 375 |
+
printf("gflops=%.3f\n", gflops);
|
| 376 |
+
}
|
| 377 |
+
|
| 378 |
+
cudaEventDestroy(start);
|
| 379 |
+
cudaEventDestroy(stop);
|
| 380 |
+
cudaFree(dev_a);
|
| 381 |
+
c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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <cuda_runtime.h>
|
| 2 |
+
|
| 3 |
+
#include <math.h>
|
| 4 |
+
#include <stdint.h>
|
| 5 |
+
#include <stdio.h>
|
| 6 |
+
#include <stdlib.h>
|
| 7 |
+
|
| 8 |
+
#define PYC_ADA_K32W_BLOCK_M 64
|
| 9 |
+
#define PYC_ADA_K32W_BLOCK_N 128
|
| 10 |
+
#define PYC_ADA_K32W_BLOCK_K 32
|
| 11 |
+
#define PYC_ADA_K32W_THREADS_X 16
|
| 12 |
+
#define PYC_ADA_K32W_THREADS_Y 16
|
| 13 |
+
#define PYC_ADA_K32W_THREAD_TILE_M 4
|
| 14 |
+
#define PYC_ADA_K32W_THREAD_TILE_N 8
|
| 15 |
+
#define PYC_ADA_K32W_VEC 4
|
| 16 |
+
|
| 17 |
+
typedef struct {
|
| 18 |
+
int m;
|
| 19 |
+
int n;
|
| 20 |
+
int k;
|
| 21 |
+
int warmup;
|
| 22 |
+
int iters;
|
| 23 |
+
} ada_gemm_k32w_config;
|
| 24 |
+
|
| 25 |
+
static int check_cuda(cudaError_t status, const char* what) {
|
| 26 |
+
if (status != cudaSuccess) {
|
| 27 |
+
fprintf(stderr, "%s failed: %s\n", what, cudaGetErrorString(status));
|
| 28 |
+
return -1;
|
| 29 |
+
}
|
| 30 |
+
return 0;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
static void fill_matrix(float* data, int rows, int cols, float scale) {
|
| 34 |
+
int i;
|
| 35 |
+
for (i = 0; i < rows * cols; ++i) {
|
| 36 |
+
int pattern = (i * 17 + rows * 13 + cols * 7) % 31;
|
| 37 |
+
data[i] = ((float)pattern - 15.0f) * scale;
|
| 38 |
+
}
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
static void reference_gemm(const float* a, const float* b, float* c, int m, int n, int k) {
|
| 42 |
+
int row;
|
| 43 |
+
for (row = 0; row < m; ++row) {
|
| 44 |
+
int col;
|
| 45 |
+
for (col = 0; col < n; ++col) {
|
| 46 |
+
float acc = 0.0f;
|
| 47 |
+
int kk;
|
| 48 |
+
for (kk = 0; kk < k; ++kk) {
|
| 49 |
+
acc += a[row * k + kk] * b[kk * n + col];
|
| 50 |
+
}
|
| 51 |
+
c[row * n + col] = acc;
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
__device__ static void load_a_vec(
|
| 57 |
+
const float* __restrict__ a,
|
| 58 |
+
float shared_a[PYC_ADA_K32W_BLOCK_M][PYC_ADA_K32W_BLOCK_K + 1],
|
| 59 |
+
int lane_linear,
|
| 60 |
+
int block_row,
|
| 61 |
+
int kk_base,
|
| 62 |
+
int m,
|
| 63 |
+
int k) {
|
| 64 |
+
const int vecs_per_row = PYC_ADA_K32W_BLOCK_K / PYC_ADA_K32W_VEC;
|
| 65 |
+
const int total_vecs = (PYC_ADA_K32W_BLOCK_M * PYC_ADA_K32W_BLOCK_K) / PYC_ADA_K32W_VEC;
|
| 66 |
+
int phase;
|
| 67 |
+
|
| 68 |
+
for (phase = 0; phase < total_vecs / (PYC_ADA_K32W_THREADS_X * PYC_ADA_K32W_THREADS_Y); ++phase) {
|
| 69 |
+
const int linear = lane_linear + phase * (PYC_ADA_K32W_THREADS_X * PYC_ADA_K32W_THREADS_Y);
|
| 70 |
+
const int tile_row = linear / vecs_per_row;
|
| 71 |
+
const int tile_col = (linear % vecs_per_row) * PYC_ADA_K32W_VEC;
|
| 72 |
+
const int global_row = block_row + tile_row;
|
| 73 |
+
const int global_col = kk_base + tile_col;
|
| 74 |
+
int i;
|
| 75 |
+
|
| 76 |
+
if (global_row < m && global_col + (PYC_ADA_K32W_VEC - 1) < k) {
|
| 77 |
+
const float4 value = *reinterpret_cast<const float4*>(&a[global_row * k + global_col]);
|
| 78 |
+
shared_a[tile_row][tile_col + 0] = value.x;
|
| 79 |
+
shared_a[tile_row][tile_col + 1] = value.y;
|
| 80 |
+
shared_a[tile_row][tile_col + 2] = value.z;
|
| 81 |
+
shared_a[tile_row][tile_col + 3] = value.w;
|
| 82 |
+
continue;
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
for (i = 0; i < PYC_ADA_K32W_VEC; ++i) {
|
| 86 |
+
float value = 0.0f;
|
| 87 |
+
if (global_row < m && global_col + i < k) {
|
| 88 |
+
value = a[global_row * k + global_col + i];
|
| 89 |
+
}
|
| 90 |
+
shared_a[tile_row][tile_col + i] = value;
|
| 91 |
+
}
|
| 92 |
+
}
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
__device__ static void load_b_vec(
|
| 96 |
+
const float* __restrict__ b,
|
| 97 |
+
float shared_b[PYC_ADA_K32W_BLOCK_K][PYC_ADA_K32W_BLOCK_N + 1],
|
| 98 |
+
int lane_linear,
|
| 99 |
+
int block_col,
|
| 100 |
+
int kk_base,
|
| 101 |
+
int k,
|
| 102 |
+
int n) {
|
| 103 |
+
const int vecs_per_row = PYC_ADA_K32W_BLOCK_N / PYC_ADA_K32W_VEC;
|
| 104 |
+
const int total_vecs = (PYC_ADA_K32W_BLOCK_K * PYC_ADA_K32W_BLOCK_N) / PYC_ADA_K32W_VEC;
|
| 105 |
+
int phase;
|
| 106 |
+
|
| 107 |
+
for (phase = 0; phase < total_vecs / (PYC_ADA_K32W_THREADS_X * PYC_ADA_K32W_THREADS_Y); ++phase) {
|
| 108 |
+
const int linear = lane_linear + phase * (PYC_ADA_K32W_THREADS_X * PYC_ADA_K32W_THREADS_Y);
|
| 109 |
+
const int tile_row = linear / vecs_per_row;
|
| 110 |
+
const int tile_col = (linear % vecs_per_row) * PYC_ADA_K32W_VEC;
|
| 111 |
+
const int global_row = kk_base + tile_row;
|
| 112 |
+
const int global_col = block_col + tile_col;
|
| 113 |
+
int i;
|
| 114 |
+
|
| 115 |
+
if (global_row < k && global_col + (PYC_ADA_K32W_VEC - 1) < n) {
|
| 116 |
+
const float4 value = *reinterpret_cast<const float4*>(&b[global_row * n + global_col]);
|
| 117 |
+
shared_b[tile_row][tile_col + 0] = value.x;
|
| 118 |
+
shared_b[tile_row][tile_col + 1] = value.y;
|
| 119 |
+
shared_b[tile_row][tile_col + 2] = value.z;
|
| 120 |
+
shared_b[tile_row][tile_col + 3] = value.w;
|
| 121 |
+
continue;
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
for (i = 0; i < PYC_ADA_K32W_VEC; ++i) {
|
| 125 |
+
float value = 0.0f;
|
| 126 |
+
if (global_row < k && global_col + i < n) {
|
| 127 |
+
value = b[global_row * n + global_col + i];
|
| 128 |
+
}
|
| 129 |
+
shared_b[tile_row][tile_col + i] = value;
|
| 130 |
+
}
|
| 131 |
+
}
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
__launch_bounds__(PYC_ADA_K32W_THREADS_X * PYC_ADA_K32W_THREADS_Y, 2)
|
| 135 |
+
__global__ void ada_fp32_k32w_gemm(
|
| 136 |
+
const float* __restrict__ a,
|
| 137 |
+
const float* __restrict__ b,
|
| 138 |
+
float* __restrict__ c,
|
| 139 |
+
int m,
|
| 140 |
+
int n,
|
| 141 |
+
int k) {
|
| 142 |
+
__shared__ float shared_a[PYC_ADA_K32W_BLOCK_M][PYC_ADA_K32W_BLOCK_K + 1];
|
| 143 |
+
__shared__ float shared_b[PYC_ADA_K32W_BLOCK_K][PYC_ADA_K32W_BLOCK_N + 1];
|
| 144 |
+
|
| 145 |
+
const int lane_linear = threadIdx.y * blockDim.x + threadIdx.x;
|
| 146 |
+
const int block_row = blockIdx.y * PYC_ADA_K32W_BLOCK_M;
|
| 147 |
+
const int block_col = blockIdx.x * PYC_ADA_K32W_BLOCK_N;
|
| 148 |
+
const int row_fragment = threadIdx.y * PYC_ADA_K32W_THREAD_TILE_M;
|
| 149 |
+
const int col_fragment = threadIdx.x * PYC_ADA_K32W_THREAD_TILE_N;
|
| 150 |
+
float accum[PYC_ADA_K32W_THREAD_TILE_M][PYC_ADA_K32W_THREAD_TILE_N];
|
| 151 |
+
int kk_base;
|
| 152 |
+
int i;
|
| 153 |
+
int j;
|
| 154 |
+
|
| 155 |
+
for (i = 0; i < PYC_ADA_K32W_THREAD_TILE_M; ++i) {
|
| 156 |
+
for (j = 0; j < PYC_ADA_K32W_THREAD_TILE_N; ++j) {
|
| 157 |
+
accum[i][j] = 0.0f;
|
| 158 |
+
}
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
for (kk_base = 0; kk_base < k; kk_base += PYC_ADA_K32W_BLOCK_K) {
|
| 162 |
+
load_a_vec(a, shared_a, lane_linear, block_row, kk_base, m, k);
|
| 163 |
+
load_b_vec(b, shared_b, lane_linear, block_col, kk_base, k, n);
|
| 164 |
+
|
| 165 |
+
__syncthreads();
|
| 166 |
+
|
| 167 |
+
#pragma unroll
|
| 168 |
+
for (i = 0; i < PYC_ADA_K32W_BLOCK_K; ++i) {
|
| 169 |
+
float a_frag[PYC_ADA_K32W_THREAD_TILE_M];
|
| 170 |
+
float b_frag[PYC_ADA_K32W_THREAD_TILE_N];
|
| 171 |
+
int ii;
|
| 172 |
+
|
| 173 |
+
#pragma unroll
|
| 174 |
+
for (ii = 0; ii < PYC_ADA_K32W_THREAD_TILE_M; ++ii) {
|
| 175 |
+
a_frag[ii] = shared_a[row_fragment + ii][i];
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
#pragma unroll
|
| 179 |
+
for (ii = 0; ii < PYC_ADA_K32W_THREAD_TILE_N; ++ii) {
|
| 180 |
+
b_frag[ii] = shared_b[i][col_fragment + ii];
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
#pragma unroll
|
| 184 |
+
for (ii = 0; ii < PYC_ADA_K32W_THREAD_TILE_M; ++ii) {
|
| 185 |
+
int jj;
|
| 186 |
+
#pragma unroll
|
| 187 |
+
for (jj = 0; jj < PYC_ADA_K32W_THREAD_TILE_N; ++jj) {
|
| 188 |
+
accum[ii][jj] += a_frag[ii] * b_frag[jj];
|
| 189 |
+
}
|
| 190 |
+
}
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
__syncthreads();
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
for (i = 0; i < PYC_ADA_K32W_THREAD_TILE_M; ++i) {
|
| 197 |
+
int out_row = block_row + row_fragment + i;
|
| 198 |
+
if (out_row >= m) {
|
| 199 |
+
continue;
|
| 200 |
+
}
|
| 201 |
+
for (j = 0; j < PYC_ADA_K32W_THREAD_TILE_N; ++j) {
|
| 202 |
+
int out_col = block_col + col_fragment + j;
|
| 203 |
+
if (out_col < n) {
|
| 204 |
+
c[out_row * n + out_col] = accum[i][j];
|
| 205 |
+
}
|
| 206 |
+
}
|
| 207 |
+
}
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
static int configure_ada_kernel(void) {
|
| 211 |
+
cudaError_t status;
|
| 212 |
+
|
| 213 |
+
status = cudaFuncSetAttribute(
|
| 214 |
+
ada_fp32_k32w_gemm,
|
| 215 |
+
cudaFuncAttributePreferredSharedMemoryCarveout,
|
| 216 |
+
100);
|
| 217 |
+
if (status != cudaSuccess && status != cudaErrorNotSupported) {
|
| 218 |
+
fprintf(stderr, "cudaFuncSetAttribute failed: %s\n", cudaGetErrorString(status));
|
| 219 |
+
return -1;
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
return 0;
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
+
static int parse_int_arg(const char* text, int* out) {
|
| 226 |
+
char* end = NULL;
|
| 227 |
+
long value;
|
| 228 |
+
|
| 229 |
+
if (!text || !out) {
|
| 230 |
+
return -1;
|
| 231 |
+
}
|
| 232 |
+
|
| 233 |
+
value = strtol(text, &end, 10);
|
| 234 |
+
if (*text == '\0' || !end || *end != '\0' || value <= 0 || value > 1 << 20) {
|
| 235 |
+
return -1;
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
*out = (int)value;
|
| 239 |
+
return 0;
|
| 240 |
+
}
|
| 241 |
+
|
| 242 |
+
static int parse_config(int argc, char** argv, ada_gemm_k32w_config* cfg) {
|
| 243 |
+
if (!cfg) {
|
| 244 |
+
return -1;
|
| 245 |
+
}
|
| 246 |
+
|
| 247 |
+
cfg->m = 1024;
|
| 248 |
+
cfg->n = 1024;
|
| 249 |
+
cfg->k = 1024;
|
| 250 |
+
cfg->warmup = 10;
|
| 251 |
+
cfg->iters = 50;
|
| 252 |
+
|
| 253 |
+
if (argc > 1 && parse_int_arg(argv[1], &cfg->m) != 0) return -1;
|
| 254 |
+
if (argc > 2 && parse_int_arg(argv[2], &cfg->n) != 0) return -1;
|
| 255 |
+
if (argc > 3 && parse_int_arg(argv[3], &cfg->k) != 0) return -1;
|
| 256 |
+
if (argc > 4 && parse_int_arg(argv[4], &cfg->warmup) != 0) return -1;
|
| 257 |
+
if (argc > 5 && parse_int_arg(argv[5], &cfg->iters) != 0) return -1;
|
| 258 |
+
|
| 259 |
+
return 0;
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
int main(int argc, char** argv) {
|
| 263 |
+
ada_gemm_k32w_config cfg;
|
| 264 |
+
cudaDeviceProp props;
|
| 265 |
+
float* host_a = NULL;
|
| 266 |
+
float* host_b = NULL;
|
| 267 |
+
float* host_c = NULL;
|
| 268 |
+
float* ref_c = NULL;
|
| 269 |
+
float* dev_a = NULL;
|
| 270 |
+
float* dev_b = NULL;
|
| 271 |
+
float* dev_c = NULL;
|
| 272 |
+
cudaEvent_t start = NULL;
|
| 273 |
+
cudaEvent_t stop = NULL;
|
| 274 |
+
size_t a_bytes;
|
| 275 |
+
size_t b_bytes;
|
| 276 |
+
size_t c_bytes;
|
| 277 |
+
dim3 block;
|
| 278 |
+
dim3 grid;
|
| 279 |
+
float elapsed_ms = 0.0f;
|
| 280 |
+
double best_ms = 0.0;
|
| 281 |
+
int iter;
|
| 282 |
+
double max_abs_diff = 0.0;
|
| 283 |
+
int device = 0;
|
| 284 |
+
|
| 285 |
+
if (parse_config(argc, argv, &cfg) != 0) {
|
| 286 |
+
fprintf(stderr, "usage: %s [m] [n] [k] [warmup] [iters]\n", argv[0]);
|
| 287 |
+
return 2;
|
| 288 |
+
}
|
| 289 |
+
|
| 290 |
+
if (check_cuda(cudaGetDevice(&device), "cudaGetDevice") != 0) return 1;
|
| 291 |
+
if (check_cuda(cudaGetDeviceProperties(&props, device), "cudaGetDeviceProperties") != 0) return 1;
|
| 292 |
+
|
| 293 |
+
printf("device=%s cc=%d.%d\n", props.name, props.major, props.minor);
|
| 294 |
+
if (!(props.major == 8 && props.minor == 9)) {
|
| 295 |
+
printf("note=prototype tuned for Ada (sm_89); running on a different architecture\n");
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
+
a_bytes = (size_t)cfg.m * (size_t)cfg.k * sizeof(float);
|
| 299 |
+
b_bytes = (size_t)cfg.k * (size_t)cfg.n * sizeof(float);
|
| 300 |
+
c_bytes = (size_t)cfg.m * (size_t)cfg.n * sizeof(float);
|
| 301 |
+
|
| 302 |
+
host_a = (float*)malloc(a_bytes);
|
| 303 |
+
host_b = (float*)malloc(b_bytes);
|
| 304 |
+
host_c = (float*)malloc(c_bytes);
|
| 305 |
+
ref_c = (float*)malloc(c_bytes);
|
| 306 |
+
if (!host_a || !host_b || !host_c || !ref_c) {
|
| 307 |
+
fprintf(stderr, "host allocation failed\n");
|
| 308 |
+
return 1;
|
| 309 |
+
}
|
| 310 |
+
|
| 311 |
+
fill_matrix(host_a, cfg.m, cfg.k, 0.03125f);
|
| 312 |
+
fill_matrix(host_b, cfg.k, cfg.n, 0.0625f);
|
| 313 |
+
reference_gemm(host_a, host_b, ref_c, cfg.m, cfg.n, cfg.k);
|
| 314 |
+
|
| 315 |
+
if (check_cuda(cudaMalloc((void**)&dev_a, a_bytes), "cudaMalloc(a)") != 0) return 1;
|
| 316 |
+
if (check_cuda(cudaMalloc((void**)&dev_b, b_bytes), "cudaMalloc(b)") != 0) return 1;
|
| 317 |
+
if (check_cuda(cudaMalloc((void**)&dev_c, c_bytes), "cudaMalloc(c)") != 0) return 1;
|
| 318 |
+
|
| 319 |
+
if (check_cuda(cudaMemcpy(dev_a, host_a, a_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(a)") != 0) return 1;
|
| 320 |
+
if (check_cuda(cudaMemcpy(dev_b, host_b, b_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(b)") != 0) return 1;
|
| 321 |
+
|
| 322 |
+
if (configure_ada_kernel() != 0) return 1;
|
| 323 |
+
|
| 324 |
+
block = dim3(PYC_ADA_K32W_THREADS_X, PYC_ADA_K32W_THREADS_Y, 1);
|
| 325 |
+
grid = dim3(
|
| 326 |
+
(unsigned int)((cfg.n + PYC_ADA_K32W_BLOCK_N - 1) / PYC_ADA_K32W_BLOCK_N),
|
| 327 |
+
(unsigned int)((cfg.m + PYC_ADA_K32W_BLOCK_M - 1) / PYC_ADA_K32W_BLOCK_M),
|
| 328 |
+
1);
|
| 329 |
+
|
| 330 |
+
if (check_cuda(cudaEventCreate(&start), "cudaEventCreate(start)") != 0) return 1;
|
| 331 |
+
if (check_cuda(cudaEventCreate(&stop), "cudaEventCreate(stop)") != 0) return 1;
|
| 332 |
+
|
| 333 |
+
for (iter = 0; iter < cfg.warmup; ++iter) {
|
| 334 |
+
ada_fp32_k32w_gemm<<<grid, block>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 335 |
+
}
|
| 336 |
+
if (check_cuda(cudaGetLastError(), "kernel launch warmup") != 0) return 1;
|
| 337 |
+
if (check_cuda(cudaDeviceSynchronize(), "cudaDeviceSynchronize warmup") != 0) return 1;
|
| 338 |
+
|
| 339 |
+
best_ms = 0.0;
|
| 340 |
+
for (iter = 0; iter < cfg.iters; ++iter) {
|
| 341 |
+
if (check_cuda(cudaEventRecord(start), "cudaEventRecord(start)") != 0) return 1;
|
| 342 |
+
ada_fp32_k32w_gemm<<<grid, block>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 343 |
+
if (check_cuda(cudaEventRecord(stop), "cudaEventRecord(stop)") != 0) return 1;
|
| 344 |
+
if (check_cuda(cudaEventSynchronize(stop), "cudaEventSynchronize(stop)") != 0) return 1;
|
| 345 |
+
if (check_cuda(cudaEventElapsedTime(&elapsed_ms, start, stop), "cudaEventElapsedTime") != 0) return 1;
|
| 346 |
+
if (iter == 0 || elapsed_ms < (float)best_ms) {
|
| 347 |
+
best_ms = elapsed_ms;
|
| 348 |
+
}
|
| 349 |
+
}
|
| 350 |
+
|
| 351 |
+
if (check_cuda(cudaMemcpy(host_c, dev_c, c_bytes, cudaMemcpyDeviceToHost), "cudaMemcpy(c)") != 0) return 1;
|
| 352 |
+
|
| 353 |
+
for (iter = 0; iter < cfg.m * cfg.n; ++iter) {
|
| 354 |
+
double diff = fabs((double)host_c[iter] - (double)ref_c[iter]);
|
| 355 |
+
if (diff > max_abs_diff) {
|
| 356 |
+
max_abs_diff = diff;
|
| 357 |
+
}
|
| 358 |
+
}
|
| 359 |
+
|
| 360 |
+
printf("shape=%dx%dx%d\n", cfg.m, cfg.n, cfg.k);
|
| 361 |
+
printf("tile=%dx%dx%d threads=%dx%d thread_tile=%dx%d vector_width=%d\n",
|
| 362 |
+
PYC_ADA_K32W_BLOCK_M,
|
| 363 |
+
PYC_ADA_K32W_BLOCK_N,
|
| 364 |
+
PYC_ADA_K32W_BLOCK_K,
|
| 365 |
+
PYC_ADA_K32W_THREADS_X,
|
| 366 |
+
PYC_ADA_K32W_THREADS_Y,
|
| 367 |
+
PYC_ADA_K32W_THREAD_TILE_M,
|
| 368 |
+
PYC_ADA_K32W_THREAD_TILE_N,
|
| 369 |
+
PYC_ADA_K32W_VEC);
|
| 370 |
+
printf("best_ms=%.3f\n", best_ms);
|
| 371 |
+
printf("max_abs_diff=%.6f\n", max_abs_diff);
|
| 372 |
+
if (best_ms > 0.0) {
|
| 373 |
+
double flops = 2.0 * (double)cfg.m * (double)cfg.n * (double)cfg.k;
|
| 374 |
+
double gflops = flops / (best_ms * 1.0e6);
|
| 375 |
+
printf("gflops=%.3f\n", gflops);
|
| 376 |
+
}
|
| 377 |
+
|
| 378 |
+
cudaEventDestroy(start);
|
| 379 |
+
cudaEventDestroy(stop);
|
| 380 |
+
cudaFree(dev_a);
|
| 381 |
+
cudaFree(dev_b);
|
| 382 |
+
cudaFree(dev_c);
|
| 383 |
+
free(host_a);
|
| 384 |
+
free(host_b);
|
| 385 |
+
free(host_c);
|
| 386 |
+
free(ref_c);
|
| 387 |
+
return max_abs_diff <= 1e-2 ? 0 : 1;
|
| 388 |
+
}
|
kernels/prototypes/ada/gemm_k64_t44/kernel.cu
ADDED
|
@@ -0,0 +1,400 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <cuda_runtime.h>
|
| 2 |
+
|
| 3 |
+
#include <math.h>
|
| 4 |
+
#include <stdint.h>
|
| 5 |
+
#include <stdio.h>
|
| 6 |
+
#include <stdlib.h>
|
| 7 |
+
|
| 8 |
+
#define PYC_ADA_K64T44_BLOCK_M 64
|
| 9 |
+
#define PYC_ADA_K64T44_BLOCK_N 64
|
| 10 |
+
#define PYC_ADA_K64T44_BLOCK_K 64
|
| 11 |
+
#define PYC_ADA_K64T44_THREADS_X 16
|
| 12 |
+
#define PYC_ADA_K64T44_THREADS_Y 16
|
| 13 |
+
#define PYC_ADA_K64T44_THREAD_TILE_M 4
|
| 14 |
+
#define PYC_ADA_K64T44_THREAD_TILE_N 4
|
| 15 |
+
#define PYC_ADA_K64T44_VEC 4
|
| 16 |
+
|
| 17 |
+
typedef struct {
|
| 18 |
+
int m;
|
| 19 |
+
int n;
|
| 20 |
+
int k;
|
| 21 |
+
int warmup;
|
| 22 |
+
int iters;
|
| 23 |
+
} ada_gemm_k64t44_config;
|
| 24 |
+
|
| 25 |
+
static int check_cuda(cudaError_t status, const char* what) {
|
| 26 |
+
if (status != cudaSuccess) {
|
| 27 |
+
fprintf(stderr, "%s failed: %s\n", what, cudaGetErrorString(status));
|
| 28 |
+
return -1;
|
| 29 |
+
}
|
| 30 |
+
return 0;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
static void fill_matrix(float* data, int rows, int cols, float scale) {
|
| 34 |
+
int i;
|
| 35 |
+
for (i = 0; i < rows * cols; ++i) {
|
| 36 |
+
int pattern = (i * 17 + rows * 13 + cols * 7) % 31;
|
| 37 |
+
data[i] = ((float)pattern - 15.0f) * scale;
|
| 38 |
+
}
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
static void reference_gemm(const float* a, const float* b, float* c, int m, int n, int k) {
|
| 42 |
+
int row;
|
| 43 |
+
for (row = 0; row < m; ++row) {
|
| 44 |
+
int col;
|
| 45 |
+
for (col = 0; col < n; ++col) {
|
| 46 |
+
float acc = 0.0f;
|
| 47 |
+
int kk;
|
| 48 |
+
for (kk = 0; kk < k; ++kk) {
|
| 49 |
+
acc += a[row * k + kk] * b[kk * n + col];
|
| 50 |
+
}
|
| 51 |
+
c[row * n + col] = acc;
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
__device__ static void load_a_vec(
|
| 57 |
+
const float* __restrict__ a,
|
| 58 |
+
float shared_a[PYC_ADA_K64T44_BLOCK_M][PYC_ADA_K64T44_BLOCK_K + 1],
|
| 59 |
+
int lane_linear,
|
| 60 |
+
int block_row,
|
| 61 |
+
int kk_base,
|
| 62 |
+
int m,
|
| 63 |
+
int k) {
|
| 64 |
+
const int vecs_per_row = PYC_ADA_K64T44_BLOCK_K / PYC_ADA_K64T44_VEC;
|
| 65 |
+
const int total_vecs = (PYC_ADA_K64T44_BLOCK_M * PYC_ADA_K64T44_BLOCK_K) / PYC_ADA_K64T44_VEC;
|
| 66 |
+
int phase;
|
| 67 |
+
|
| 68 |
+
for (phase = 0; phase < total_vecs / (PYC_ADA_K64T44_THREADS_X * PYC_ADA_K64T44_THREADS_Y); ++phase) {
|
| 69 |
+
const int linear = lane_linear + phase * (PYC_ADA_K64T44_THREADS_X * PYC_ADA_K64T44_THREADS_Y);
|
| 70 |
+
const int tile_row = linear / vecs_per_row;
|
| 71 |
+
const int tile_col = (linear % vecs_per_row) * PYC_ADA_K64T44_VEC;
|
| 72 |
+
const int global_row = block_row + tile_row;
|
| 73 |
+
const int global_col = kk_base + tile_col;
|
| 74 |
+
int i;
|
| 75 |
+
|
| 76 |
+
if (global_row < m && global_col + (PYC_ADA_K64T44_VEC - 1) < k) {
|
| 77 |
+
const float4 value = *reinterpret_cast<const float4*>(&a[global_row * k + global_col]);
|
| 78 |
+
shared_a[tile_row][tile_col + 0] = value.x;
|
| 79 |
+
shared_a[tile_row][tile_col + 1] = value.y;
|
| 80 |
+
shared_a[tile_row][tile_col + 2] = value.z;
|
| 81 |
+
shared_a[tile_row][tile_col + 3] = value.w;
|
| 82 |
+
continue;
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
for (i = 0; i < PYC_ADA_K64T44_VEC; ++i) {
|
| 86 |
+
float value = 0.0f;
|
| 87 |
+
if (global_row < m && global_col + i < k) {
|
| 88 |
+
value = a[global_row * k + global_col + i];
|
| 89 |
+
}
|
| 90 |
+
shared_a[tile_row][tile_col + i] = value;
|
| 91 |
+
}
|
| 92 |
+
}
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
__device__ static void load_b_vec(
|
| 96 |
+
const float* __restrict__ b,
|
| 97 |
+
float shared_b[PYC_ADA_K64T44_BLOCK_K][PYC_ADA_K64T44_BLOCK_N + 1],
|
| 98 |
+
int lane_linear,
|
| 99 |
+
int block_col,
|
| 100 |
+
int kk_base,
|
| 101 |
+
int k,
|
| 102 |
+
int n) {
|
| 103 |
+
const int vecs_per_row = PYC_ADA_K64T44_BLOCK_N / PYC_ADA_K64T44_VEC;
|
| 104 |
+
const int total_vecs = (PYC_ADA_K64T44_BLOCK_K * PYC_ADA_K64T44_BLOCK_N) / PYC_ADA_K64T44_VEC;
|
| 105 |
+
int phase;
|
| 106 |
+
|
| 107 |
+
for (phase = 0; phase < total_vecs / (PYC_ADA_K64T44_THREADS_X * PYC_ADA_K64T44_THREADS_Y); ++phase) {
|
| 108 |
+
const int linear = lane_linear + phase * (PYC_ADA_K64T44_THREADS_X * PYC_ADA_K64T44_THREADS_Y);
|
| 109 |
+
const int tile_row = linear / vecs_per_row;
|
| 110 |
+
const int tile_col = (linear % vecs_per_row) * PYC_ADA_K64T44_VEC;
|
| 111 |
+
const int global_row = kk_base + tile_row;
|
| 112 |
+
const int global_col = block_col + tile_col;
|
| 113 |
+
int i;
|
| 114 |
+
|
| 115 |
+
if (global_row < k && global_col + (PYC_ADA_K64T44_VEC - 1) < n) {
|
| 116 |
+
const float4 value = *reinterpret_cast<const float4*>(&b[global_row * n + global_col]);
|
| 117 |
+
shared_b[tile_row][tile_col + 0] = value.x;
|
| 118 |
+
shared_b[tile_row][tile_col + 1] = value.y;
|
| 119 |
+
shared_b[tile_row][tile_col + 2] = value.z;
|
| 120 |
+
shared_b[tile_row][tile_col + 3] = value.w;
|
| 121 |
+
continue;
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
for (i = 0; i < PYC_ADA_K64T44_VEC; ++i) {
|
| 125 |
+
float value = 0.0f;
|
| 126 |
+
if (global_row < k && global_col + i < n) {
|
| 127 |
+
value = b[global_row * n + global_col + i];
|
| 128 |
+
}
|
| 129 |
+
shared_b[tile_row][tile_col + i] = value;
|
| 130 |
+
}
|
| 131 |
+
}
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
__launch_bounds__(PYC_ADA_K64T44_THREADS_X * PYC_ADA_K64T44_THREADS_Y, 2)
|
| 135 |
+
__global__ void ada_fp32_k64t44_gemm(
|
| 136 |
+
const float* __restrict__ a,
|
| 137 |
+
const float* __restrict__ b,
|
| 138 |
+
float* __restrict__ c,
|
| 139 |
+
int m,
|
| 140 |
+
int n,
|
| 141 |
+
int k) {
|
| 142 |
+
__shared__ float shared_a[PYC_ADA_K64T44_BLOCK_M][PYC_ADA_K64T44_BLOCK_K + 1];
|
| 143 |
+
__shared__ float shared_b[PYC_ADA_K64T44_BLOCK_K][PYC_ADA_K64T44_BLOCK_N + 1];
|
| 144 |
+
|
| 145 |
+
const int lane_linear = threadIdx.y * blockDim.x + threadIdx.x;
|
| 146 |
+
const int block_row = blockIdx.y * PYC_ADA_K64T44_BLOCK_M;
|
| 147 |
+
const int block_col = blockIdx.x * PYC_ADA_K64T44_BLOCK_N;
|
| 148 |
+
const int row_fragment = threadIdx.y * PYC_ADA_K64T44_THREAD_TILE_M;
|
| 149 |
+
const int col_fragment = threadIdx.x * PYC_ADA_K64T44_THREAD_TILE_N;
|
| 150 |
+
float accum[PYC_ADA_K64T44_THREAD_TILE_M][PYC_ADA_K64T44_THREAD_TILE_N];
|
| 151 |
+
int kk_base;
|
| 152 |
+
int i;
|
| 153 |
+
int j;
|
| 154 |
+
|
| 155 |
+
for (i = 0; i < PYC_ADA_K64T44_THREAD_TILE_M; ++i) {
|
| 156 |
+
for (j = 0; j < PYC_ADA_K64T44_THREAD_TILE_N; ++j) {
|
| 157 |
+
accum[i][j] = 0.0f;
|
| 158 |
+
}
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
for (kk_base = 0; kk_base < k; kk_base += PYC_ADA_K64T44_BLOCK_K) {
|
| 162 |
+
load_a_vec(a, shared_a, lane_linear, block_row, kk_base, m, k);
|
| 163 |
+
load_b_vec(b, shared_b, lane_linear, block_col, kk_base, k, n);
|
| 164 |
+
|
| 165 |
+
__syncthreads();
|
| 166 |
+
|
| 167 |
+
#pragma unroll
|
| 168 |
+
for (i = 0; i < PYC_ADA_K64T44_BLOCK_K; ++i) {
|
| 169 |
+
float a_frag[PYC_ADA_K64T44_THREAD_TILE_M];
|
| 170 |
+
float b_frag[PYC_ADA_K64T44_THREAD_TILE_N];
|
| 171 |
+
int ii;
|
| 172 |
+
|
| 173 |
+
#pragma unroll
|
| 174 |
+
for (ii = 0; ii < PYC_ADA_K64T44_THREAD_TILE_M; ++ii) {
|
| 175 |
+
a_frag[ii] = shared_a[row_fragment + ii][i];
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
#pragma unroll
|
| 179 |
+
for (ii = 0; ii < PYC_ADA_K64T44_THREAD_TILE_N; ++ii) {
|
| 180 |
+
b_frag[ii] = shared_b[i][col_fragment + ii];
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
#pragma unroll
|
| 184 |
+
for (ii = 0; ii < PYC_ADA_K64T44_THREAD_TILE_M; ++ii) {
|
| 185 |
+
int jj;
|
| 186 |
+
#pragma unroll
|
| 187 |
+
for (jj = 0; jj < PYC_ADA_K64T44_THREAD_TILE_N; ++jj) {
|
| 188 |
+
accum[ii][jj] = fmaf(a_frag[ii], b_frag[jj], accum[ii][jj]);
|
| 189 |
+
}
|
| 190 |
+
}
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
__syncthreads();
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
for (i = 0; i < PYC_ADA_K64T44_THREAD_TILE_M; ++i) {
|
| 197 |
+
int out_row = block_row + row_fragment + i;
|
| 198 |
+
if (out_row >= m) {
|
| 199 |
+
continue;
|
| 200 |
+
}
|
| 201 |
+
if (col_fragment + 3 < PYC_ADA_K64T44_BLOCK_N) {
|
| 202 |
+
int out_col = block_col + col_fragment;
|
| 203 |
+
if (out_col + 3 < n) {
|
| 204 |
+
float4 value;
|
| 205 |
+
value.x = accum[i][0];
|
| 206 |
+
value.y = accum[i][1];
|
| 207 |
+
value.z = accum[i][2];
|
| 208 |
+
value.w = accum[i][3];
|
| 209 |
+
*reinterpret_cast<float4*>(&c[out_row * n + out_col]) = value;
|
| 210 |
+
continue;
|
| 211 |
+
}
|
| 212 |
+
}
|
| 213 |
+
for (j = 0; j < PYC_ADA_K64T44_THREAD_TILE_N; ++j) {
|
| 214 |
+
int out_col = block_col + col_fragment + j;
|
| 215 |
+
if (out_col < n) {
|
| 216 |
+
c[out_row * n + out_col] = accum[i][j];
|
| 217 |
+
}
|
| 218 |
+
}
|
| 219 |
+
}
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
static int configure_ada_kernel(void) {
|
| 223 |
+
cudaError_t status;
|
| 224 |
+
|
| 225 |
+
status = cudaFuncSetAttribute(
|
| 226 |
+
ada_fp32_k64t44_gemm,
|
| 227 |
+
cudaFuncAttributePreferredSharedMemoryCarveout,
|
| 228 |
+
100);
|
| 229 |
+
if (status != cudaSuccess && status != cudaErrorNotSupported) {
|
| 230 |
+
fprintf(stderr, "cudaFuncSetAttribute failed: %s\n", cudaGetErrorString(status));
|
| 231 |
+
return -1;
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
return 0;
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
static int parse_int_arg(const char* text, int* out) {
|
| 238 |
+
char* end = NULL;
|
| 239 |
+
long value;
|
| 240 |
+
|
| 241 |
+
if (!text || !out) {
|
| 242 |
+
return -1;
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
value = strtol(text, &end, 10);
|
| 246 |
+
if (*text == '\0' || !end || *end != '\0' || value <= 0 || value > 1 << 20) {
|
| 247 |
+
return -1;
|
| 248 |
+
}
|
| 249 |
+
|
| 250 |
+
*out = (int)value;
|
| 251 |
+
return 0;
|
| 252 |
+
}
|
| 253 |
+
|
| 254 |
+
static int parse_config(int argc, char** argv, ada_gemm_k64t44_config* cfg) {
|
| 255 |
+
if (!cfg) {
|
| 256 |
+
return -1;
|
| 257 |
+
}
|
| 258 |
+
|
| 259 |
+
cfg->m = 1024;
|
| 260 |
+
cfg->n = 1024;
|
| 261 |
+
cfg->k = 1024;
|
| 262 |
+
cfg->warmup = 10;
|
| 263 |
+
cfg->iters = 50;
|
| 264 |
+
|
| 265 |
+
if (argc > 1 && parse_int_arg(argv[1], &cfg->m) != 0) return -1;
|
| 266 |
+
if (argc > 2 && parse_int_arg(argv[2], &cfg->n) != 0) return -1;
|
| 267 |
+
if (argc > 3 && parse_int_arg(argv[3], &cfg->k) != 0) return -1;
|
| 268 |
+
if (argc > 4 && parse_int_arg(argv[4], &cfg->warmup) != 0) return -1;
|
| 269 |
+
if (argc > 5 && parse_int_arg(argv[5], &cfg->iters) != 0) return -1;
|
| 270 |
+
|
| 271 |
+
return 0;
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
int main(int argc, char** argv) {
|
| 275 |
+
ada_gemm_k64t44_config cfg;
|
| 276 |
+
cudaDeviceProp props;
|
| 277 |
+
float* host_a = NULL;
|
| 278 |
+
float* host_b = NULL;
|
| 279 |
+
float* host_c = NULL;
|
| 280 |
+
float* ref_c = NULL;
|
| 281 |
+
float* dev_a = NULL;
|
| 282 |
+
float* dev_b = NULL;
|
| 283 |
+
float* dev_c = NULL;
|
| 284 |
+
cudaEvent_t start = NULL;
|
| 285 |
+
cudaEvent_t stop = NULL;
|
| 286 |
+
size_t a_bytes;
|
| 287 |
+
size_t b_bytes;
|
| 288 |
+
size_t c_bytes;
|
| 289 |
+
dim3 block;
|
| 290 |
+
dim3 grid;
|
| 291 |
+
float elapsed_ms = 0.0f;
|
| 292 |
+
double best_ms = 0.0;
|
| 293 |
+
int iter;
|
| 294 |
+
double max_abs_diff = 0.0;
|
| 295 |
+
int device = 0;
|
| 296 |
+
|
| 297 |
+
if (parse_config(argc, argv, &cfg) != 0) {
|
| 298 |
+
fprintf(stderr, "usage: %s [m] [n] [k] [warmup] [iters]\n", argv[0]);
|
| 299 |
+
return 2;
|
| 300 |
+
}
|
| 301 |
+
|
| 302 |
+
if (check_cuda(cudaGetDevice(&device), "cudaGetDevice") != 0) return 1;
|
| 303 |
+
if (check_cuda(cudaGetDeviceProperties(&props, device), "cudaGetDeviceProperties") != 0) return 1;
|
| 304 |
+
|
| 305 |
+
printf("device=%s cc=%d.%d\n", props.name, props.major, props.minor);
|
| 306 |
+
if (!(props.major == 8 && props.minor == 9)) {
|
| 307 |
+
printf("note=prototype tuned for Ada (sm_89); running on a different architecture\n");
|
| 308 |
+
}
|
| 309 |
+
|
| 310 |
+
a_bytes = (size_t)cfg.m * (size_t)cfg.k * sizeof(float);
|
| 311 |
+
b_bytes = (size_t)cfg.k * (size_t)cfg.n * sizeof(float);
|
| 312 |
+
c_bytes = (size_t)cfg.m * (size_t)cfg.n * sizeof(float);
|
| 313 |
+
|
| 314 |
+
host_a = (float*)malloc(a_bytes);
|
| 315 |
+
host_b = (float*)malloc(b_bytes);
|
| 316 |
+
host_c = (float*)malloc(c_bytes);
|
| 317 |
+
ref_c = (float*)malloc(c_bytes);
|
| 318 |
+
if (!host_a || !host_b || !host_c || !ref_c) {
|
| 319 |
+
fprintf(stderr, "host allocation failed\n");
|
| 320 |
+
return 1;
|
| 321 |
+
}
|
| 322 |
+
|
| 323 |
+
fill_matrix(host_a, cfg.m, cfg.k, 0.03125f);
|
| 324 |
+
fill_matrix(host_b, cfg.k, cfg.n, 0.0625f);
|
| 325 |
+
reference_gemm(host_a, host_b, ref_c, cfg.m, cfg.n, cfg.k);
|
| 326 |
+
|
| 327 |
+
if (check_cuda(cudaMalloc((void**)&dev_a, a_bytes), "cudaMalloc(a)") != 0) return 1;
|
| 328 |
+
if (check_cuda(cudaMalloc((void**)&dev_b, b_bytes), "cudaMalloc(b)") != 0) return 1;
|
| 329 |
+
if (check_cuda(cudaMalloc((void**)&dev_c, c_bytes), "cudaMalloc(c)") != 0) return 1;
|
| 330 |
+
|
| 331 |
+
if (check_cuda(cudaMemcpy(dev_a, host_a, a_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(a)") != 0) return 1;
|
| 332 |
+
if (check_cuda(cudaMemcpy(dev_b, host_b, b_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(b)") != 0) return 1;
|
| 333 |
+
|
| 334 |
+
if (configure_ada_kernel() != 0) return 1;
|
| 335 |
+
|
| 336 |
+
block = dim3(PYC_ADA_K64T44_THREADS_X, PYC_ADA_K64T44_THREADS_Y, 1);
|
| 337 |
+
grid = dim3(
|
| 338 |
+
(unsigned int)((cfg.n + PYC_ADA_K64T44_BLOCK_N - 1) / PYC_ADA_K64T44_BLOCK_N),
|
| 339 |
+
(unsigned int)((cfg.m + PYC_ADA_K64T44_BLOCK_M - 1) / PYC_ADA_K64T44_BLOCK_M),
|
| 340 |
+
1);
|
| 341 |
+
|
| 342 |
+
if (check_cuda(cudaEventCreate(&start), "cudaEventCreate(start)") != 0) return 1;
|
| 343 |
+
if (check_cuda(cudaEventCreate(&stop), "cudaEventCreate(stop)") != 0) return 1;
|
| 344 |
+
|
| 345 |
+
for (iter = 0; iter < cfg.warmup; ++iter) {
|
| 346 |
+
ada_fp32_k64t44_gemm<<<grid, block>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 347 |
+
}
|
| 348 |
+
if (check_cuda(cudaGetLastError(), "kernel launch warmup") != 0) return 1;
|
| 349 |
+
if (check_cuda(cudaDeviceSynchronize(), "cudaDeviceSynchronize warmup") != 0) return 1;
|
| 350 |
+
|
| 351 |
+
best_ms = 0.0;
|
| 352 |
+
for (iter = 0; iter < cfg.iters; ++iter) {
|
| 353 |
+
if (check_cuda(cudaEventRecord(start), "cudaEventRecord(start)") != 0) return 1;
|
| 354 |
+
ada_fp32_k64t44_gemm<<<grid, block>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 355 |
+
if (check_cuda(cudaEventRecord(stop), "cudaEventRecord(stop)") != 0) return 1;
|
| 356 |
+
if (check_cuda(cudaEventSynchronize(stop), "cudaEventSynchronize(stop)") != 0) return 1;
|
| 357 |
+
if (check_cuda(cudaEventElapsedTime(&elapsed_ms, start, stop), "cudaEventElapsedTime") != 0) return 1;
|
| 358 |
+
if (iter == 0 || elapsed_ms < (float)best_ms) {
|
| 359 |
+
best_ms = elapsed_ms;
|
| 360 |
+
}
|
| 361 |
+
}
|
| 362 |
+
|
| 363 |
+
if (check_cuda(cudaMemcpy(host_c, dev_c, c_bytes, cudaMemcpyDeviceToHost), "cudaMemcpy(c)") != 0) return 1;
|
| 364 |
+
|
| 365 |
+
for (iter = 0; iter < cfg.m * cfg.n; ++iter) {
|
| 366 |
+
double diff = fabs((double)host_c[iter] - (double)ref_c[iter]);
|
| 367 |
+
if (diff > max_abs_diff) {
|
| 368 |
+
max_abs_diff = diff;
|
| 369 |
+
}
|
| 370 |
+
}
|
| 371 |
+
|
| 372 |
+
printf("shape=%dx%dx%d\n", cfg.m, cfg.n, cfg.k);
|
| 373 |
+
printf("tile=%dx%dx%d threads=%dx%d thread_tile=%dx%d vector_width=%d\n",
|
| 374 |
+
PYC_ADA_K64T44_BLOCK_M,
|
| 375 |
+
PYC_ADA_K64T44_BLOCK_N,
|
| 376 |
+
PYC_ADA_K64T44_BLOCK_K,
|
| 377 |
+
PYC_ADA_K64T44_THREADS_X,
|
| 378 |
+
PYC_ADA_K64T44_THREADS_Y,
|
| 379 |
+
PYC_ADA_K64T44_THREAD_TILE_M,
|
| 380 |
+
PYC_ADA_K64T44_THREAD_TILE_N,
|
| 381 |
+
PYC_ADA_K64T44_VEC);
|
| 382 |
+
printf("best_ms=%.3f\n", best_ms);
|
| 383 |
+
printf("max_abs_diff=%.6f\n", max_abs_diff);
|
| 384 |
+
if (best_ms > 0.0) {
|
| 385 |
+
double flops = 2.0 * (double)cfg.m * (double)cfg.n * (double)cfg.k;
|
| 386 |
+
double gflops = flops / (best_ms * 1.0e6);
|
| 387 |
+
printf("gflops=%.3f\n", gflops);
|
| 388 |
+
}
|
| 389 |
+
|
| 390 |
+
cudaEventDestroy(start);
|
| 391 |
+
cudaEventDestroy(stop);
|
| 392 |
+
cudaFree(dev_a);
|
| 393 |
+
cudaFree(dev_b);
|
| 394 |
+
cudaFree(dev_c);
|
| 395 |
+
free(host_a);
|
| 396 |
+
free(host_b);
|
| 397 |
+
free(host_c);
|
| 398 |
+
free(ref_c);
|
| 399 |
+
return max_abs_diff <= 1e-2 ? 0 : 1;
|
| 400 |
+
}
|
kernels/prototypes/ada/gemm_k64_warp32/kernel.cu
ADDED
|
@@ -0,0 +1,388 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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<math.h>
|
| 4 |
+
#include <stdint.h>
|
| 5 |
+
#include <stdio.h>
|
| 6 |
+
#include <stdlib.h>
|
| 7 |
+
|
| 8 |
+
#define PYC_ADA_K64WARP_BLOCK_M 64
|
| 9 |
+
#define PYC_ADA_K64WARP_BLOCK_N 64
|
| 10 |
+
#define PYC_ADA_K64WARP_BLOCK_K 64
|
| 11 |
+
#define PYC_ADA_K64WARP_THREADS_X 32
|
| 12 |
+
#define PYC_ADA_K64WARP_THREADS_Y 8
|
| 13 |
+
#define PYC_ADA_K64WARP_THREAD_TILE_M 8
|
| 14 |
+
#define PYC_ADA_K64WARP_THREAD_TILE_N 2
|
| 15 |
+
#define PYC_ADA_K64WARP_VEC 4
|
| 16 |
+
|
| 17 |
+
typedef struct {
|
| 18 |
+
int m;
|
| 19 |
+
int n;
|
| 20 |
+
int k;
|
| 21 |
+
int warmup;
|
| 22 |
+
int iters;
|
| 23 |
+
} ada_gemm_k64warp_config;
|
| 24 |
+
|
| 25 |
+
static int check_cuda(cudaError_t status, const char* what) {
|
| 26 |
+
if (status != cudaSuccess) {
|
| 27 |
+
fprintf(stderr, "%s failed: %s\n", what, cudaGetErrorString(status));
|
| 28 |
+
return -1;
|
| 29 |
+
}
|
| 30 |
+
return 0;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
static void fill_matrix(float* data, int rows, int cols, float scale) {
|
| 34 |
+
int i;
|
| 35 |
+
for (i = 0; i < rows * cols; ++i) {
|
| 36 |
+
int pattern = (i * 17 + rows * 13 + cols * 7) % 31;
|
| 37 |
+
data[i] = ((float)pattern - 15.0f) * scale;
|
| 38 |
+
}
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
static void reference_gemm(const float* a, const float* b, float* c, int m, int n, int k) {
|
| 42 |
+
int row;
|
| 43 |
+
for (row = 0; row < m; ++row) {
|
| 44 |
+
int col;
|
| 45 |
+
for (col = 0; col < n; ++col) {
|
| 46 |
+
float acc = 0.0f;
|
| 47 |
+
int kk;
|
| 48 |
+
for (kk = 0; kk < k; ++kk) {
|
| 49 |
+
acc += a[row * k + kk] * b[kk * n + col];
|
| 50 |
+
}
|
| 51 |
+
c[row * n + col] = acc;
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
__device__ static void load_a_vec(
|
| 57 |
+
const float* __restrict__ a,
|
| 58 |
+
float shared_a[PYC_ADA_K64WARP_BLOCK_M][PYC_ADA_K64WARP_BLOCK_K + 1],
|
| 59 |
+
int lane_linear,
|
| 60 |
+
int block_row,
|
| 61 |
+
int kk_base,
|
| 62 |
+
int m,
|
| 63 |
+
int k) {
|
| 64 |
+
const int vecs_per_row = PYC_ADA_K64WARP_BLOCK_K / PYC_ADA_K64WARP_VEC;
|
| 65 |
+
const int total_vecs = (PYC_ADA_K64WARP_BLOCK_M * PYC_ADA_K64WARP_BLOCK_K) / PYC_ADA_K64WARP_VEC;
|
| 66 |
+
int phase;
|
| 67 |
+
|
| 68 |
+
for (phase = 0; phase < total_vecs / (PYC_ADA_K64WARP_THREADS_X * PYC_ADA_K64WARP_THREADS_Y); ++phase) {
|
| 69 |
+
const int linear = lane_linear + phase * (PYC_ADA_K64WARP_THREADS_X * PYC_ADA_K64WARP_THREADS_Y);
|
| 70 |
+
const int tile_row = linear / vecs_per_row;
|
| 71 |
+
const int tile_col = (linear % vecs_per_row) * PYC_ADA_K64WARP_VEC;
|
| 72 |
+
const int global_row = block_row + tile_row;
|
| 73 |
+
const int global_col = kk_base + tile_col;
|
| 74 |
+
int i;
|
| 75 |
+
|
| 76 |
+
if (global_row < m && global_col + (PYC_ADA_K64WARP_VEC - 1) < k) {
|
| 77 |
+
const float4 value = *reinterpret_cast<const float4*>(&a[global_row * k + global_col]);
|
| 78 |
+
shared_a[tile_row][tile_col + 0] = value.x;
|
| 79 |
+
shared_a[tile_row][tile_col + 1] = value.y;
|
| 80 |
+
shared_a[tile_row][tile_col + 2] = value.z;
|
| 81 |
+
shared_a[tile_row][tile_col + 3] = value.w;
|
| 82 |
+
continue;
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
for (i = 0; i < PYC_ADA_K64WARP_VEC; ++i) {
|
| 86 |
+
float value = 0.0f;
|
| 87 |
+
if (global_row < m && global_col + i < k) {
|
| 88 |
+
value = a[global_row * k + global_col + i];
|
| 89 |
+
}
|
| 90 |
+
shared_a[tile_row][tile_col + i] = value;
|
| 91 |
+
}
|
| 92 |
+
}
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
__device__ static void load_b_vec(
|
| 96 |
+
const float* __restrict__ b,
|
| 97 |
+
float shared_b[PYC_ADA_K64WARP_BLOCK_K][PYC_ADA_K64WARP_BLOCK_N + 1],
|
| 98 |
+
int lane_linear,
|
| 99 |
+
int block_col,
|
| 100 |
+
int kk_base,
|
| 101 |
+
int k,
|
| 102 |
+
int n) {
|
| 103 |
+
const int vecs_per_row = PYC_ADA_K64WARP_BLOCK_N / PYC_ADA_K64WARP_VEC;
|
| 104 |
+
const int total_vecs = (PYC_ADA_K64WARP_BLOCK_K * PYC_ADA_K64WARP_BLOCK_N) / PYC_ADA_K64WARP_VEC;
|
| 105 |
+
int phase;
|
| 106 |
+
|
| 107 |
+
for (phase = 0; phase < total_vecs / (PYC_ADA_K64WARP_THREADS_X * PYC_ADA_K64WARP_THREADS_Y); ++phase) {
|
| 108 |
+
const int linear = lane_linear + phase * (PYC_ADA_K64WARP_THREADS_X * PYC_ADA_K64WARP_THREADS_Y);
|
| 109 |
+
const int tile_row = linear / vecs_per_row;
|
| 110 |
+
const int tile_col = (linear % vecs_per_row) * PYC_ADA_K64WARP_VEC;
|
| 111 |
+
const int global_row = kk_base + tile_row;
|
| 112 |
+
const int global_col = block_col + tile_col;
|
| 113 |
+
int i;
|
| 114 |
+
|
| 115 |
+
if (global_row < k && global_col + (PYC_ADA_K64WARP_VEC - 1) < n) {
|
| 116 |
+
const float4 value = *reinterpret_cast<const float4*>(&b[global_row * n + global_col]);
|
| 117 |
+
shared_b[tile_row][tile_col + 0] = value.x;
|
| 118 |
+
shared_b[tile_row][tile_col + 1] = value.y;
|
| 119 |
+
shared_b[tile_row][tile_col + 2] = value.z;
|
| 120 |
+
shared_b[tile_row][tile_col + 3] = value.w;
|
| 121 |
+
continue;
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
for (i = 0; i < PYC_ADA_K64WARP_VEC; ++i) {
|
| 125 |
+
float value = 0.0f;
|
| 126 |
+
if (global_row < k && global_col + i < n) {
|
| 127 |
+
value = b[global_row * n + global_col + i];
|
| 128 |
+
}
|
| 129 |
+
shared_b[tile_row][tile_col + i] = value;
|
| 130 |
+
}
|
| 131 |
+
}
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
__launch_bounds__(PYC_ADA_K64WARP_THREADS_X * PYC_ADA_K64WARP_THREADS_Y, 2)
|
| 135 |
+
__global__ void ada_fp32_k64warp_gemm(
|
| 136 |
+
const float* __restrict__ a,
|
| 137 |
+
const float* __restrict__ b,
|
| 138 |
+
float* __restrict__ c,
|
| 139 |
+
int m,
|
| 140 |
+
int n,
|
| 141 |
+
int k) {
|
| 142 |
+
__shared__ float shared_a[PYC_ADA_K64WARP_BLOCK_M][PYC_ADA_K64WARP_BLOCK_K + 1];
|
| 143 |
+
__shared__ float shared_b[PYC_ADA_K64WARP_BLOCK_K][PYC_ADA_K64WARP_BLOCK_N + 1];
|
| 144 |
+
|
| 145 |
+
const int lane_linear = threadIdx.y * blockDim.x + threadIdx.x;
|
| 146 |
+
const int block_row = blockIdx.y * PYC_ADA_K64WARP_BLOCK_M;
|
| 147 |
+
const int block_col = blockIdx.x * PYC_ADA_K64WARP_BLOCK_N;
|
| 148 |
+
const int row_fragment = threadIdx.y * PYC_ADA_K64WARP_THREAD_TILE_M;
|
| 149 |
+
const int col_fragment = threadIdx.x * PYC_ADA_K64WARP_THREAD_TILE_N;
|
| 150 |
+
float accum[PYC_ADA_K64WARP_THREAD_TILE_M][PYC_ADA_K64WARP_THREAD_TILE_N];
|
| 151 |
+
int kk_base;
|
| 152 |
+
int i;
|
| 153 |
+
int j;
|
| 154 |
+
|
| 155 |
+
for (i = 0; i < PYC_ADA_K64WARP_THREAD_TILE_M; ++i) {
|
| 156 |
+
for (j = 0; j < PYC_ADA_K64WARP_THREAD_TILE_N; ++j) {
|
| 157 |
+
accum[i][j] = 0.0f;
|
| 158 |
+
}
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
for (kk_base = 0; kk_base < k; kk_base += PYC_ADA_K64WARP_BLOCK_K) {
|
| 162 |
+
load_a_vec(a, shared_a, lane_linear, block_row, kk_base, m, k);
|
| 163 |
+
load_b_vec(b, shared_b, lane_linear, block_col, kk_base, k, n);
|
| 164 |
+
|
| 165 |
+
__syncthreads();
|
| 166 |
+
|
| 167 |
+
#pragma unroll
|
| 168 |
+
for (i = 0; i < PYC_ADA_K64WARP_BLOCK_K; ++i) {
|
| 169 |
+
float a_frag[PYC_ADA_K64WARP_THREAD_TILE_M];
|
| 170 |
+
float b_frag[PYC_ADA_K64WARP_THREAD_TILE_N];
|
| 171 |
+
int ii;
|
| 172 |
+
|
| 173 |
+
#pragma unroll
|
| 174 |
+
for (ii = 0; ii < PYC_ADA_K64WARP_THREAD_TILE_M; ++ii) {
|
| 175 |
+
a_frag[ii] = shared_a[row_fragment + ii][i];
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
#pragma unroll
|
| 179 |
+
for (ii = 0; ii < PYC_ADA_K64WARP_THREAD_TILE_N; ++ii) {
|
| 180 |
+
b_frag[ii] = shared_b[i][col_fragment + ii];
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
#pragma unroll
|
| 184 |
+
for (ii = 0; ii < PYC_ADA_K64WARP_THREAD_TILE_M; ++ii) {
|
| 185 |
+
int jj;
|
| 186 |
+
#pragma unroll
|
| 187 |
+
for (jj = 0; jj < PYC_ADA_K64WARP_THREAD_TILE_N; ++jj) {
|
| 188 |
+
accum[ii][jj] += a_frag[ii] * b_frag[jj];
|
| 189 |
+
}
|
| 190 |
+
}
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
__syncthreads();
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
for (i = 0; i < PYC_ADA_K64WARP_THREAD_TILE_M; ++i) {
|
| 197 |
+
int out_row = block_row + row_fragment + i;
|
| 198 |
+
if (out_row >= m) {
|
| 199 |
+
continue;
|
| 200 |
+
}
|
| 201 |
+
for (j = 0; j < PYC_ADA_K64WARP_THREAD_TILE_N; ++j) {
|
| 202 |
+
int out_col = block_col + col_fragment + j;
|
| 203 |
+
if (out_col < n) {
|
| 204 |
+
c[out_row * n + out_col] = accum[i][j];
|
| 205 |
+
}
|
| 206 |
+
}
|
| 207 |
+
}
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
static int configure_ada_kernel(void) {
|
| 211 |
+
cudaError_t status;
|
| 212 |
+
|
| 213 |
+
status = cudaFuncSetAttribute(
|
| 214 |
+
ada_fp32_k64warp_gemm,
|
| 215 |
+
cudaFuncAttributePreferredSharedMemoryCarveout,
|
| 216 |
+
100);
|
| 217 |
+
if (status != cudaSuccess && status != cudaErrorNotSupported) {
|
| 218 |
+
fprintf(stderr, "cudaFuncSetAttribute failed: %s\n", cudaGetErrorString(status));
|
| 219 |
+
return -1;
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
return 0;
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
+
static int parse_int_arg(const char* text, int* out) {
|
| 226 |
+
char* end = NULL;
|
| 227 |
+
long value;
|
| 228 |
+
|
| 229 |
+
if (!text || !out) {
|
| 230 |
+
return -1;
|
| 231 |
+
}
|
| 232 |
+
|
| 233 |
+
value = strtol(text, &end, 10);
|
| 234 |
+
if (*text == '\0' || !end || *end != '\0' || value <= 0 || value > 1 << 20) {
|
| 235 |
+
return -1;
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
*out = (int)value;
|
| 239 |
+
return 0;
|
| 240 |
+
}
|
| 241 |
+
|
| 242 |
+
static int parse_config(int argc, char** argv, ada_gemm_k64warp_config* cfg) {
|
| 243 |
+
if (!cfg) {
|
| 244 |
+
return -1;
|
| 245 |
+
}
|
| 246 |
+
|
| 247 |
+
cfg->m = 1024;
|
| 248 |
+
cfg->n = 1024;
|
| 249 |
+
cfg->k = 1024;
|
| 250 |
+
cfg->warmup = 10;
|
| 251 |
+
cfg->iters = 50;
|
| 252 |
+
|
| 253 |
+
if (argc > 1 && parse_int_arg(argv[1], &cfg->m) != 0) return -1;
|
| 254 |
+
if (argc > 2 && parse_int_arg(argv[2], &cfg->n) != 0) return -1;
|
| 255 |
+
if (argc > 3 && parse_int_arg(argv[3], &cfg->k) != 0) return -1;
|
| 256 |
+
if (argc > 4 && parse_int_arg(argv[4], &cfg->warmup) != 0) return -1;
|
| 257 |
+
if (argc > 5 && parse_int_arg(argv[5], &cfg->iters) != 0) return -1;
|
| 258 |
+
|
| 259 |
+
return 0;
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
int main(int argc, char** argv) {
|
| 263 |
+
ada_gemm_k64warp_config cfg;
|
| 264 |
+
cudaDeviceProp props;
|
| 265 |
+
float* host_a = NULL;
|
| 266 |
+
float* host_b = NULL;
|
| 267 |
+
float* host_c = NULL;
|
| 268 |
+
float* ref_c = NULL;
|
| 269 |
+
float* dev_a = NULL;
|
| 270 |
+
float* dev_b = NULL;
|
| 271 |
+
float* dev_c = NULL;
|
| 272 |
+
cudaEvent_t start = NULL;
|
| 273 |
+
cudaEvent_t stop = NULL;
|
| 274 |
+
size_t a_bytes;
|
| 275 |
+
size_t b_bytes;
|
| 276 |
+
size_t c_bytes;
|
| 277 |
+
dim3 block;
|
| 278 |
+
dim3 grid;
|
| 279 |
+
float elapsed_ms = 0.0f;
|
| 280 |
+
double best_ms = 0.0;
|
| 281 |
+
int iter;
|
| 282 |
+
double max_abs_diff = 0.0;
|
| 283 |
+
int device = 0;
|
| 284 |
+
|
| 285 |
+
if (parse_config(argc, argv, &cfg) != 0) {
|
| 286 |
+
fprintf(stderr, "usage: %s [m] [n] [k] [warmup] [iters]\n", argv[0]);
|
| 287 |
+
return 2;
|
| 288 |
+
}
|
| 289 |
+
|
| 290 |
+
if (check_cuda(cudaGetDevice(&device), "cudaGetDevice") != 0) return 1;
|
| 291 |
+
if (check_cuda(cudaGetDeviceProperties(&props, device), "cudaGetDeviceProperties") != 0) return 1;
|
| 292 |
+
|
| 293 |
+
printf("device=%s cc=%d.%d\n", props.name, props.major, props.minor);
|
| 294 |
+
if (!(props.major == 8 && props.minor == 9)) {
|
| 295 |
+
printf("note=prototype tuned for Ada (sm_89); running on a different architecture\n");
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
+
a_bytes = (size_t)cfg.m * (size_t)cfg.k * sizeof(float);
|
| 299 |
+
b_bytes = (size_t)cfg.k * (size_t)cfg.n * sizeof(float);
|
| 300 |
+
c_bytes = (size_t)cfg.m * (size_t)cfg.n * sizeof(float);
|
| 301 |
+
|
| 302 |
+
host_a = (float*)malloc(a_bytes);
|
| 303 |
+
host_b = (float*)malloc(b_bytes);
|
| 304 |
+
host_c = (float*)malloc(c_bytes);
|
| 305 |
+
ref_c = (float*)malloc(c_bytes);
|
| 306 |
+
if (!host_a || !host_b || !host_c || !ref_c) {
|
| 307 |
+
fprintf(stderr, "host allocation failed\n");
|
| 308 |
+
return 1;
|
| 309 |
+
}
|
| 310 |
+
|
| 311 |
+
fill_matrix(host_a, cfg.m, cfg.k, 0.03125f);
|
| 312 |
+
fill_matrix(host_b, cfg.k, cfg.n, 0.0625f);
|
| 313 |
+
reference_gemm(host_a, host_b, ref_c, cfg.m, cfg.n, cfg.k);
|
| 314 |
+
|
| 315 |
+
if (check_cuda(cudaMalloc((void**)&dev_a, a_bytes), "cudaMalloc(a)") != 0) return 1;
|
| 316 |
+
if (check_cuda(cudaMalloc((void**)&dev_b, b_bytes), "cudaMalloc(b)") != 0) return 1;
|
| 317 |
+
if (check_cuda(cudaMalloc((void**)&dev_c, c_bytes), "cudaMalloc(c)") != 0) return 1;
|
| 318 |
+
|
| 319 |
+
if (check_cuda(cudaMemcpy(dev_a, host_a, a_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(a)") != 0) return 1;
|
| 320 |
+
if (check_cuda(cudaMemcpy(dev_b, host_b, b_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(b)") != 0) return 1;
|
| 321 |
+
|
| 322 |
+
if (configure_ada_kernel() != 0) return 1;
|
| 323 |
+
|
| 324 |
+
block = dim3(PYC_ADA_K64WARP_THREADS_X, PYC_ADA_K64WARP_THREADS_Y, 1);
|
| 325 |
+
grid = dim3(
|
| 326 |
+
(unsigned int)((cfg.n + PYC_ADA_K64WARP_BLOCK_N - 1) / PYC_ADA_K64WARP_BLOCK_N),
|
| 327 |
+
(unsigned int)((cfg.m + PYC_ADA_K64WARP_BLOCK_M - 1) / PYC_ADA_K64WARP_BLOCK_M),
|
| 328 |
+
1);
|
| 329 |
+
|
| 330 |
+
if (check_cuda(cudaEventCreate(&start), "cudaEventCreate(start)") != 0) return 1;
|
| 331 |
+
if (check_cuda(cudaEventCreate(&stop), "cudaEventCreate(stop)") != 0) return 1;
|
| 332 |
+
|
| 333 |
+
for (iter = 0; iter < cfg.warmup; ++iter) {
|
| 334 |
+
ada_fp32_k64warp_gemm<<<grid, block>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 335 |
+
}
|
| 336 |
+
if (check_cuda(cudaGetLastError(), "kernel launch warmup") != 0) return 1;
|
| 337 |
+
if (check_cuda(cudaDeviceSynchronize(), "cudaDeviceSynchronize warmup") != 0) return 1;
|
| 338 |
+
|
| 339 |
+
best_ms = 0.0;
|
| 340 |
+
for (iter = 0; iter < cfg.iters; ++iter) {
|
| 341 |
+
if (check_cuda(cudaEventRecord(start), "cudaEventRecord(start)") != 0) return 1;
|
| 342 |
+
ada_fp32_k64warp_gemm<<<grid, block>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 343 |
+
if (check_cuda(cudaEventRecord(stop), "cudaEventRecord(stop)") != 0) return 1;
|
| 344 |
+
if (check_cuda(cudaEventSynchronize(stop), "cudaEventSynchronize(stop)") != 0) return 1;
|
| 345 |
+
if (check_cuda(cudaEventElapsedTime(&elapsed_ms, start, stop), "cudaEventElapsedTime") != 0) return 1;
|
| 346 |
+
if (iter == 0 || elapsed_ms < (float)best_ms) {
|
| 347 |
+
best_ms = elapsed_ms;
|
| 348 |
+
}
|
| 349 |
+
}
|
| 350 |
+
|
| 351 |
+
if (check_cuda(cudaMemcpy(host_c, dev_c, c_bytes, cudaMemcpyDeviceToHost), "cudaMemcpy(c)") != 0) return 1;
|
| 352 |
+
|
| 353 |
+
for (iter = 0; iter < cfg.m * cfg.n; ++iter) {
|
| 354 |
+
double diff = fabs((double)host_c[iter] - (double)ref_c[iter]);
|
| 355 |
+
if (diff > max_abs_diff) {
|
| 356 |
+
max_abs_diff = diff;
|
| 357 |
+
}
|
| 358 |
+
}
|
| 359 |
+
|
| 360 |
+
printf("shape=%dx%dx%d\n", cfg.m, cfg.n, cfg.k);
|
| 361 |
+
printf("tile=%dx%dx%d threads=%dx%d thread_tile=%dx%d vector_width=%d\n",
|
| 362 |
+
PYC_ADA_K64WARP_BLOCK_M,
|
| 363 |
+
PYC_ADA_K64WARP_BLOCK_N,
|
| 364 |
+
PYC_ADA_K64WARP_BLOCK_K,
|
| 365 |
+
PYC_ADA_K64WARP_THREADS_X,
|
| 366 |
+
PYC_ADA_K64WARP_THREADS_Y,
|
| 367 |
+
PYC_ADA_K64WARP_THREAD_TILE_M,
|
| 368 |
+
PYC_ADA_K64WARP_THREAD_TILE_N,
|
| 369 |
+
PYC_ADA_K64WARP_VEC);
|
| 370 |
+
printf("best_ms=%.3f\n", best_ms);
|
| 371 |
+
printf("max_abs_diff=%.6f\n", max_abs_diff);
|
| 372 |
+
if (best_ms > 0.0) {
|
| 373 |
+
double flops = 2.0 * (double)cfg.m * (double)cfg.n * (double)cfg.k;
|
| 374 |
+
double gflops = flops / (best_ms * 1.0e6);
|
| 375 |
+
printf("gflops=%.3f\n", gflops);
|
| 376 |
+
}
|
| 377 |
+
|
| 378 |
+
cudaEventDestroy(start);
|
| 379 |
+
cudaEventDestroy(stop);
|
| 380 |
+
cudaFree(dev_a);
|
| 381 |
+
cudaFree(dev_b);
|
| 382 |
+
cudaFree(dev_c);
|
| 383 |
+
free(host_a);
|
| 384 |
+
free(host_b);
|
| 385 |
+
free(host_c);
|
| 386 |
+
free(ref_c);
|
| 387 |
+
return max_abs_diff <= 1e-2 ? 0 : 1;
|
| 388 |
+
}
|
kernels/prototypes/ada/gemm_k64_warp32_acc2/kernel.cu
ADDED
|
@@ -0,0 +1,404 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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K64A2_THREADS_Y 8
|
| 13 |
+
#define PYC_ADA_K64A2_THREAD_TILE_M 8
|
| 14 |
+
#define PYC_ADA_K64A2_THREAD_TILE_N 2
|
| 15 |
+
#define PYC_ADA_K64A2_VEC 4
|
| 16 |
+
|
| 17 |
+
typedef struct {
|
| 18 |
+
int m;
|
| 19 |
+
int n;
|
| 20 |
+
int k;
|
| 21 |
+
int warmup;
|
| 22 |
+
int iters;
|
| 23 |
+
} ada_gemm_k64a2_config;
|
| 24 |
+
|
| 25 |
+
static int check_cuda(cudaError_t status, const char* what) {
|
| 26 |
+
if (status != cudaSuccess) {
|
| 27 |
+
fprintf(stderr, "%s failed: %s\n", what, cudaGetErrorString(status));
|
| 28 |
+
return -1;
|
| 29 |
+
}
|
| 30 |
+
return 0;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
static void fill_matrix(float* data, int rows, int cols, float scale) {
|
| 34 |
+
int i;
|
| 35 |
+
for (i = 0; i < rows * cols; ++i) {
|
| 36 |
+
int pattern = (i * 17 + rows * 13 + cols * 7) % 31;
|
| 37 |
+
data[i] = ((float)pattern - 15.0f) * scale;
|
| 38 |
+
}
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
static void reference_gemm(const float* a, const float* b, float* c, int m, int n, int k) {
|
| 42 |
+
int row;
|
| 43 |
+
for (row = 0; row < m; ++row) {
|
| 44 |
+
int col;
|
| 45 |
+
for (col = 0; col < n; ++col) {
|
| 46 |
+
float acc = 0.0f;
|
| 47 |
+
int kk;
|
| 48 |
+
for (kk = 0; kk < k; ++kk) {
|
| 49 |
+
acc += a[row * k + kk] * b[kk * n + col];
|
| 50 |
+
}
|
| 51 |
+
c[row * n + col] = acc;
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
__device__ static void load_a_vec(
|
| 57 |
+
const float* __restrict__ a,
|
| 58 |
+
float shared_a[PYC_ADA_K64A2_BLOCK_M][PYC_ADA_K64A2_BLOCK_K + 1],
|
| 59 |
+
int lane_linear,
|
| 60 |
+
int block_row,
|
| 61 |
+
int kk_base,
|
| 62 |
+
int m,
|
| 63 |
+
int k) {
|
| 64 |
+
const int vecs_per_row = PYC_ADA_K64A2_BLOCK_K / PYC_ADA_K64A2_VEC;
|
| 65 |
+
const int total_vecs = (PYC_ADA_K64A2_BLOCK_M * PYC_ADA_K64A2_BLOCK_K) / PYC_ADA_K64A2_VEC;
|
| 66 |
+
int phase;
|
| 67 |
+
|
| 68 |
+
for (phase = 0; phase < total_vecs / (PYC_ADA_K64A2_THREADS_X * PYC_ADA_K64A2_THREADS_Y); ++phase) {
|
| 69 |
+
const int linear = lane_linear + phase * (PYC_ADA_K64A2_THREADS_X * PYC_ADA_K64A2_THREADS_Y);
|
| 70 |
+
const int tile_row = linear / vecs_per_row;
|
| 71 |
+
const int tile_col = (linear % vecs_per_row) * PYC_ADA_K64A2_VEC;
|
| 72 |
+
const int global_row = block_row + tile_row;
|
| 73 |
+
const int global_col = kk_base + tile_col;
|
| 74 |
+
int i;
|
| 75 |
+
|
| 76 |
+
if (global_row < m && global_col + (PYC_ADA_K64A2_VEC - 1) < k) {
|
| 77 |
+
const float4 value = *reinterpret_cast<const float4*>(&a[global_row * k + global_col]);
|
| 78 |
+
shared_a[tile_row][tile_col + 0] = value.x;
|
| 79 |
+
shared_a[tile_row][tile_col + 1] = value.y;
|
| 80 |
+
shared_a[tile_row][tile_col + 2] = value.z;
|
| 81 |
+
shared_a[tile_row][tile_col + 3] = value.w;
|
| 82 |
+
continue;
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
for (i = 0; i < PYC_ADA_K64A2_VEC; ++i) {
|
| 86 |
+
float value = 0.0f;
|
| 87 |
+
if (global_row < m && global_col + i < k) {
|
| 88 |
+
value = a[global_row * k + global_col + i];
|
| 89 |
+
}
|
| 90 |
+
shared_a[tile_row][tile_col + i] = value;
|
| 91 |
+
}
|
| 92 |
+
}
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
__device__ static void load_b_vec(
|
| 96 |
+
const float* __restrict__ b,
|
| 97 |
+
float shared_b[PYC_ADA_K64A2_BLOCK_K][PYC_ADA_K64A2_BLOCK_N + 1],
|
| 98 |
+
int lane_linear,
|
| 99 |
+
int block_col,
|
| 100 |
+
int kk_base,
|
| 101 |
+
int k,
|
| 102 |
+
int n) {
|
| 103 |
+
const int vecs_per_row = PYC_ADA_K64A2_BLOCK_N / PYC_ADA_K64A2_VEC;
|
| 104 |
+
const int total_vecs = (PYC_ADA_K64A2_BLOCK_K * PYC_ADA_K64A2_BLOCK_N) / PYC_ADA_K64A2_VEC;
|
| 105 |
+
int phase;
|
| 106 |
+
|
| 107 |
+
for (phase = 0; phase < total_vecs / (PYC_ADA_K64A2_THREADS_X * PYC_ADA_K64A2_THREADS_Y); ++phase) {
|
| 108 |
+
const int linear = lane_linear + phase * (PYC_ADA_K64A2_THREADS_X * PYC_ADA_K64A2_THREADS_Y);
|
| 109 |
+
const int tile_row = linear / vecs_per_row;
|
| 110 |
+
const int tile_col = (linear % vecs_per_row) * PYC_ADA_K64A2_VEC;
|
| 111 |
+
const int global_row = kk_base + tile_row;
|
| 112 |
+
const int global_col = block_col + tile_col;
|
| 113 |
+
int i;
|
| 114 |
+
|
| 115 |
+
if (global_row < k && global_col + (PYC_ADA_K64A2_VEC - 1) < n) {
|
| 116 |
+
const float4 value = *reinterpret_cast<const float4*>(&b[global_row * n + global_col]);
|
| 117 |
+
shared_b[tile_row][tile_col + 0] = value.x;
|
| 118 |
+
shared_b[tile_row][tile_col + 1] = value.y;
|
| 119 |
+
shared_b[tile_row][tile_col + 2] = value.z;
|
| 120 |
+
shared_b[tile_row][tile_col + 3] = value.w;
|
| 121 |
+
continue;
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
for (i = 0; i < PYC_ADA_K64A2_VEC; ++i) {
|
| 125 |
+
float value = 0.0f;
|
| 126 |
+
if (global_row < k && global_col + i < n) {
|
| 127 |
+
value = b[global_row * n + global_col + i];
|
| 128 |
+
}
|
| 129 |
+
shared_b[tile_row][tile_col + i] = value;
|
| 130 |
+
}
|
| 131 |
+
}
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
__launch_bounds__(PYC_ADA_K64A2_THREADS_X * PYC_ADA_K64A2_THREADS_Y, 2)
|
| 135 |
+
__global__ void ada_fp32_k64a2_gemm(
|
| 136 |
+
const float* __restrict__ a,
|
| 137 |
+
const float* __restrict__ b,
|
| 138 |
+
float* __restrict__ c,
|
| 139 |
+
int m,
|
| 140 |
+
int n,
|
| 141 |
+
int k) {
|
| 142 |
+
__shared__ float shared_a[PYC_ADA_K64A2_BLOCK_M][PYC_ADA_K64A2_BLOCK_K + 1];
|
| 143 |
+
__shared__ float shared_b[PYC_ADA_K64A2_BLOCK_K][PYC_ADA_K64A2_BLOCK_N + 1];
|
| 144 |
+
|
| 145 |
+
const int lane_linear = threadIdx.y * blockDim.x + threadIdx.x;
|
| 146 |
+
const int block_row = blockIdx.y * PYC_ADA_K64A2_BLOCK_M;
|
| 147 |
+
const int block_col = blockIdx.x * PYC_ADA_K64A2_BLOCK_N;
|
| 148 |
+
const int row_fragment = threadIdx.y * PYC_ADA_K64A2_THREAD_TILE_M;
|
| 149 |
+
const int col_fragment = threadIdx.x * PYC_ADA_K64A2_THREAD_TILE_N;
|
| 150 |
+
float accum0[PYC_ADA_K64A2_THREAD_TILE_M];
|
| 151 |
+
float accum1[PYC_ADA_K64A2_THREAD_TILE_M];
|
| 152 |
+
int kk_base;
|
| 153 |
+
int i;
|
| 154 |
+
|
| 155 |
+
#pragma unroll
|
| 156 |
+
for (i = 0; i < PYC_ADA_K64A2_THREAD_TILE_M; ++i) {
|
| 157 |
+
accum0[i] = 0.0f;
|
| 158 |
+
accum1[i] = 0.0f;
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
for (kk_base = 0; kk_base < k; kk_base += PYC_ADA_K64A2_BLOCK_K) {
|
| 162 |
+
load_a_vec(a, shared_a, lane_linear, block_row, kk_base, m, k);
|
| 163 |
+
load_b_vec(b, shared_b, lane_linear, block_col, kk_base, k, n);
|
| 164 |
+
|
| 165 |
+
__syncthreads();
|
| 166 |
+
|
| 167 |
+
#pragma unroll
|
| 168 |
+
for (i = 0; i < PYC_ADA_K64A2_BLOCK_K; ++i) {
|
| 169 |
+
const float b0 = shared_b[i][col_fragment + 0];
|
| 170 |
+
const float b1 = shared_b[i][col_fragment + 1];
|
| 171 |
+
const float a0 = shared_a[row_fragment + 0][i];
|
| 172 |
+
const float a1 = shared_a[row_fragment + 1][i];
|
| 173 |
+
const float a2 = shared_a[row_fragment + 2][i];
|
| 174 |
+
const float a3 = shared_a[row_fragment + 3][i];
|
| 175 |
+
const float a4 = shared_a[row_fragment + 4][i];
|
| 176 |
+
const float a5 = shared_a[row_fragment + 5][i];
|
| 177 |
+
const float a6 = shared_a[row_fragment + 6][i];
|
| 178 |
+
const float a7 = shared_a[row_fragment + 7][i];
|
| 179 |
+
|
| 180 |
+
accum0[0] = fmaf(a0, b0, accum0[0]);
|
| 181 |
+
accum1[0] = fmaf(a0, b1, accum1[0]);
|
| 182 |
+
accum0[1] = fmaf(a1, b0, accum0[1]);
|
| 183 |
+
accum1[1] = fmaf(a1, b1, accum1[1]);
|
| 184 |
+
accum0[2] = fmaf(a2, b0, accum0[2]);
|
| 185 |
+
accum1[2] = fmaf(a2, b1, accum1[2]);
|
| 186 |
+
accum0[3] = fmaf(a3, b0, accum0[3]);
|
| 187 |
+
accum1[3] = fmaf(a3, b1, accum1[3]);
|
| 188 |
+
accum0[4] = fmaf(a4, b0, accum0[4]);
|
| 189 |
+
accum1[4] = fmaf(a4, b1, accum1[4]);
|
| 190 |
+
accum0[5] = fmaf(a5, b0, accum0[5]);
|
| 191 |
+
accum1[5] = fmaf(a5, b1, accum1[5]);
|
| 192 |
+
accum0[6] = fmaf(a6, b0, accum0[6]);
|
| 193 |
+
accum1[6] = fmaf(a6, b1, accum1[6]);
|
| 194 |
+
accum0[7] = fmaf(a7, b0, accum0[7]);
|
| 195 |
+
accum1[7] = fmaf(a7, b1, accum1[7]);
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
__syncthreads();
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
#pragma unroll
|
| 202 |
+
for (i = 0; i < PYC_ADA_K64A2_THREAD_TILE_M; ++i) {
|
| 203 |
+
const int out_row = block_row + row_fragment + i;
|
| 204 |
+
if (out_row >= m) {
|
| 205 |
+
continue;
|
| 206 |
+
}
|
| 207 |
+
if (col_fragment + 1 < PYC_ADA_K64A2_BLOCK_N) {
|
| 208 |
+
const int out_col = block_col + col_fragment;
|
| 209 |
+
if (out_col + 1 < n) {
|
| 210 |
+
float2 value;
|
| 211 |
+
value.x = accum0[i];
|
| 212 |
+
value.y = accum1[i];
|
| 213 |
+
*reinterpret_cast<float2*>(&c[out_row * n + out_col]) = value;
|
| 214 |
+
continue;
|
| 215 |
+
}
|
| 216 |
+
}
|
| 217 |
+
if (block_col + col_fragment + 0 < n) {
|
| 218 |
+
c[out_row * n + block_col + col_fragment + 0] = accum0[i];
|
| 219 |
+
}
|
| 220 |
+
if (block_col + col_fragment + 1 < n) {
|
| 221 |
+
c[out_row * n + block_col + col_fragment + 1] = accum1[i];
|
| 222 |
+
}
|
| 223 |
+
}
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
static int configure_ada_kernel(void) {
|
| 227 |
+
cudaError_t status;
|
| 228 |
+
|
| 229 |
+
status = cudaFuncSetAttribute(
|
| 230 |
+
ada_fp32_k64a2_gemm,
|
| 231 |
+
cudaFuncAttributePreferredSharedMemoryCarveout,
|
| 232 |
+
100);
|
| 233 |
+
if (status != cudaSuccess && status != cudaErrorNotSupported) {
|
| 234 |
+
fprintf(stderr, "cudaFuncSetAttribute failed: %s\n", cudaGetErrorString(status));
|
| 235 |
+
return -1;
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
return 0;
|
| 239 |
+
}
|
| 240 |
+
|
| 241 |
+
static int parse_int_arg(const char* text, int* out) {
|
| 242 |
+
char* end = NULL;
|
| 243 |
+
long value;
|
| 244 |
+
|
| 245 |
+
if (!text || !out) {
|
| 246 |
+
return -1;
|
| 247 |
+
}
|
| 248 |
+
|
| 249 |
+
value = strtol(text, &end, 10);
|
| 250 |
+
if (*text == '\0' || !end || *end != '\0' || value <= 0 || value > 1 << 20) {
|
| 251 |
+
return -1;
|
| 252 |
+
}
|
| 253 |
+
|
| 254 |
+
*out = (int)value;
|
| 255 |
+
return 0;
|
| 256 |
+
}
|
| 257 |
+
|
| 258 |
+
static int parse_config(int argc, char** argv, ada_gemm_k64a2_config* cfg) {
|
| 259 |
+
if (!cfg) {
|
| 260 |
+
return -1;
|
| 261 |
+
}
|
| 262 |
+
|
| 263 |
+
cfg->m = 1024;
|
| 264 |
+
cfg->n = 1024;
|
| 265 |
+
cfg->k = 1024;
|
| 266 |
+
cfg->warmup = 10;
|
| 267 |
+
cfg->iters = 50;
|
| 268 |
+
|
| 269 |
+
if (argc > 1 && parse_int_arg(argv[1], &cfg->m) != 0) return -1;
|
| 270 |
+
if (argc > 2 && parse_int_arg(argv[2], &cfg->n) != 0) return -1;
|
| 271 |
+
if (argc > 3 && parse_int_arg(argv[3], &cfg->k) != 0) return -1;
|
| 272 |
+
if (argc > 4 && parse_int_arg(argv[4], &cfg->warmup) != 0) return -1;
|
| 273 |
+
if (argc > 5 && parse_int_arg(argv[5], &cfg->iters) != 0) return -1;
|
| 274 |
+
|
| 275 |
+
return 0;
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
int main(int argc, char** argv) {
|
| 279 |
+
ada_gemm_k64a2_config cfg;
|
| 280 |
+
cudaDeviceProp props;
|
| 281 |
+
float* host_a = NULL;
|
| 282 |
+
float* host_b = NULL;
|
| 283 |
+
float* host_c = NULL;
|
| 284 |
+
float* ref_c = NULL;
|
| 285 |
+
float* dev_a = NULL;
|
| 286 |
+
float* dev_b = NULL;
|
| 287 |
+
float* dev_c = NULL;
|
| 288 |
+
cudaEvent_t start = NULL;
|
| 289 |
+
cudaEvent_t stop = NULL;
|
| 290 |
+
size_t a_bytes;
|
| 291 |
+
size_t b_bytes;
|
| 292 |
+
size_t c_bytes;
|
| 293 |
+
dim3 block;
|
| 294 |
+
dim3 grid;
|
| 295 |
+
float elapsed_ms = 0.0f;
|
| 296 |
+
double best_ms = 0.0;
|
| 297 |
+
int iter;
|
| 298 |
+
double max_abs_diff = 0.0;
|
| 299 |
+
int device = 0;
|
| 300 |
+
|
| 301 |
+
if (parse_config(argc, argv, &cfg) != 0) {
|
| 302 |
+
fprintf(stderr, "usage: %s [m] [n] [k] [warmup] [iters]\n", argv[0]);
|
| 303 |
+
return 2;
|
| 304 |
+
}
|
| 305 |
+
|
| 306 |
+
if (check_cuda(cudaGetDevice(&device), "cudaGetDevice") != 0) return 1;
|
| 307 |
+
if (check_cuda(cudaGetDeviceProperties(&props, device), "cudaGetDeviceProperties") != 0) return 1;
|
| 308 |
+
|
| 309 |
+
printf("device=%s cc=%d.%d\n", props.name, props.major, props.minor);
|
| 310 |
+
if (!(props.major == 8 && props.minor == 9)) {
|
| 311 |
+
printf("note=prototype tuned for Ada (sm_89); running on a different architecture\n");
|
| 312 |
+
}
|
| 313 |
+
|
| 314 |
+
a_bytes = (size_t)cfg.m * (size_t)cfg.k * sizeof(float);
|
| 315 |
+
b_bytes = (size_t)cfg.k * (size_t)cfg.n * sizeof(float);
|
| 316 |
+
c_bytes = (size_t)cfg.m * (size_t)cfg.n * sizeof(float);
|
| 317 |
+
|
| 318 |
+
host_a = (float*)malloc(a_bytes);
|
| 319 |
+
host_b = (float*)malloc(b_bytes);
|
| 320 |
+
host_c = (float*)malloc(c_bytes);
|
| 321 |
+
ref_c = (float*)malloc(c_bytes);
|
| 322 |
+
if (!host_a || !host_b || !host_c || !ref_c) {
|
| 323 |
+
fprintf(stderr, "host allocation failed\n");
|
| 324 |
+
return 1;
|
| 325 |
+
}
|
| 326 |
+
|
| 327 |
+
fill_matrix(host_a, cfg.m, cfg.k, 0.03125f);
|
| 328 |
+
fill_matrix(host_b, cfg.k, cfg.n, 0.0625f);
|
| 329 |
+
reference_gemm(host_a, host_b, ref_c, cfg.m, cfg.n, cfg.k);
|
| 330 |
+
|
| 331 |
+
if (check_cuda(cudaMalloc((void**)&dev_a, a_bytes), "cudaMalloc(a)") != 0) return 1;
|
| 332 |
+
if (check_cuda(cudaMalloc((void**)&dev_b, b_bytes), "cudaMalloc(b)") != 0) return 1;
|
| 333 |
+
if (check_cuda(cudaMalloc((void**)&dev_c, c_bytes), "cudaMalloc(c)") != 0) return 1;
|
| 334 |
+
|
| 335 |
+
if (check_cuda(cudaMemcpy(dev_a, host_a, a_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(a)") != 0) return 1;
|
| 336 |
+
if (check_cuda(cudaMemcpy(dev_b, host_b, b_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(b)") != 0) return 1;
|
| 337 |
+
|
| 338 |
+
if (configure_ada_kernel() != 0) return 1;
|
| 339 |
+
|
| 340 |
+
block = dim3(PYC_ADA_K64A2_THREADS_X, PYC_ADA_K64A2_THREADS_Y, 1);
|
| 341 |
+
grid = dim3(
|
| 342 |
+
(unsigned int)((cfg.n + PYC_ADA_K64A2_BLOCK_N - 1) / PYC_ADA_K64A2_BLOCK_N),
|
| 343 |
+
(unsigned int)((cfg.m + PYC_ADA_K64A2_BLOCK_M - 1) / PYC_ADA_K64A2_BLOCK_M),
|
| 344 |
+
1);
|
| 345 |
+
|
| 346 |
+
if (check_cuda(cudaEventCreate(&start), "cudaEventCreate(start)") != 0) return 1;
|
| 347 |
+
if (check_cuda(cudaEventCreate(&stop), "cudaEventCreate(stop)") != 0) return 1;
|
| 348 |
+
|
| 349 |
+
for (iter = 0; iter < cfg.warmup; ++iter) {
|
| 350 |
+
ada_fp32_k64a2_gemm<<<grid, block>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 351 |
+
}
|
| 352 |
+
if (check_cuda(cudaGetLastError(), "kernel launch warmup") != 0) return 1;
|
| 353 |
+
if (check_cuda(cudaDeviceSynchronize(), "cudaDeviceSynchronize warmup") != 0) return 1;
|
| 354 |
+
|
| 355 |
+
best_ms = 0.0;
|
| 356 |
+
for (iter = 0; iter < cfg.iters; ++iter) {
|
| 357 |
+
if (check_cuda(cudaEventRecord(start), "cudaEventRecord(start)") != 0) return 1;
|
| 358 |
+
ada_fp32_k64a2_gemm<<<grid, block>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 359 |
+
if (check_cuda(cudaEventRecord(stop), "cudaEventRecord(stop)") != 0) return 1;
|
| 360 |
+
if (check_cuda(cudaEventSynchronize(stop), "cudaEventSynchronize(stop)") != 0) return 1;
|
| 361 |
+
if (check_cuda(cudaEventElapsedTime(&elapsed_ms, start, stop), "cudaEventElapsedTime") != 0) return 1;
|
| 362 |
+
if (iter == 0 || elapsed_ms < (float)best_ms) {
|
| 363 |
+
best_ms = elapsed_ms;
|
| 364 |
+
}
|
| 365 |
+
}
|
| 366 |
+
|
| 367 |
+
if (check_cuda(cudaMemcpy(host_c, dev_c, c_bytes, cudaMemcpyDeviceToHost), "cudaMemcpy(c)") != 0) return 1;
|
| 368 |
+
|
| 369 |
+
for (iter = 0; iter < cfg.m * cfg.n; ++iter) {
|
| 370 |
+
double diff = fabs((double)host_c[iter] - (double)ref_c[iter]);
|
| 371 |
+
if (diff > max_abs_diff) {
|
| 372 |
+
max_abs_diff = diff;
|
| 373 |
+
}
|
| 374 |
+
}
|
| 375 |
+
|
| 376 |
+
printf("shape=%dx%dx%d\n", cfg.m, cfg.n, cfg.k);
|
| 377 |
+
printf("tile=%dx%dx%d threads=%dx%d thread_tile=%dx%d vector_width=%d\n",
|
| 378 |
+
PYC_ADA_K64A2_BLOCK_M,
|
| 379 |
+
PYC_ADA_K64A2_BLOCK_N,
|
| 380 |
+
PYC_ADA_K64A2_BLOCK_K,
|
| 381 |
+
PYC_ADA_K64A2_THREADS_X,
|
| 382 |
+
PYC_ADA_K64A2_THREADS_Y,
|
| 383 |
+
PYC_ADA_K64A2_THREAD_TILE_M,
|
| 384 |
+
PYC_ADA_K64A2_THREAD_TILE_N,
|
| 385 |
+
PYC_ADA_K64A2_VEC);
|
| 386 |
+
printf("best_ms=%.3f\n", best_ms);
|
| 387 |
+
printf("max_abs_diff=%.6f\n", max_abs_diff);
|
| 388 |
+
if (best_ms > 0.0) {
|
| 389 |
+
double flops = 2.0 * (double)cfg.m * (double)cfg.n * (double)cfg.k;
|
| 390 |
+
double gflops = flops / (best_ms * 1.0e6);
|
| 391 |
+
printf("gflops=%.3f\n", gflops);
|
| 392 |
+
}
|
| 393 |
+
|
| 394 |
+
cudaEventDestroy(start);
|
| 395 |
+
cudaEventDestroy(stop);
|
| 396 |
+
cudaFree(dev_a);
|
| 397 |
+
cudaFree(dev_b);
|
| 398 |
+
cudaFree(dev_c);
|
| 399 |
+
free(host_a);
|
| 400 |
+
free(host_b);
|
| 401 |
+
free(host_c);
|
| 402 |
+
free(ref_c);
|
| 403 |
+
return max_abs_diff <= 1e-2 ? 0 : 1;
|
| 404 |
+
}
|
kernels/prototypes/ada/gemm_k64_warp32_async/kernel.cu
ADDED
|
@@ -0,0 +1,505 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <cuda_runtime.h>
|
| 2 |
+
|
| 3 |
+
#include <math.h>
|
| 4 |
+
#include <stdint.h>
|
| 5 |
+
#include <stdio.h>
|
| 6 |
+
#include <stdlib.h>
|
| 7 |
+
#include <time.h>
|
| 8 |
+
|
| 9 |
+
#define PYC_ADA_K64ASYNC_BLOCK_M 64
|
| 10 |
+
#define PYC_ADA_K64ASYNC_BLOCK_N 64
|
| 11 |
+
#define PYC_ADA_K64ASYNC_BLOCK_K 64
|
| 12 |
+
#define PYC_ADA_K64ASYNC_THREADS_X 32
|
| 13 |
+
#define PYC_ADA_K64ASYNC_THREADS_Y 8
|
| 14 |
+
#define PYC_ADA_K64ASYNC_THREAD_TILE_M 8
|
| 15 |
+
#define PYC_ADA_K64ASYNC_THREAD_TILE_N 2
|
| 16 |
+
#define PYC_ADA_K64ASYNC_VEC 4
|
| 17 |
+
#define PYC_ADA_K64ASYNC_STAGES 2
|
| 18 |
+
#define PYC_ADA_K64ASYNC_SHARED_STRIDE_A (PYC_ADA_K64ASYNC_BLOCK_K + 4)
|
| 19 |
+
#define PYC_ADA_K64ASYNC_SHARED_STRIDE_B (PYC_ADA_K64ASYNC_BLOCK_N + 4)
|
| 20 |
+
#define PYC_ADA_K64ASYNC_STAGE_A_ELEMS (PYC_ADA_K64ASYNC_BLOCK_M * PYC_ADA_K64ASYNC_SHARED_STRIDE_A)
|
| 21 |
+
#define PYC_ADA_K64ASYNC_STAGE_B_ELEMS (PYC_ADA_K64ASYNC_BLOCK_K * PYC_ADA_K64ASYNC_SHARED_STRIDE_B)
|
| 22 |
+
#define PYC_ADA_K64ASYNC_SHARED_ELEMS (PYC_ADA_K64ASYNC_STAGES * (PYC_ADA_K64ASYNC_STAGE_A_ELEMS + PYC_ADA_K64ASYNC_STAGE_B_ELEMS))
|
| 23 |
+
#define PYC_ADA_K64ASYNC_SHARED_BYTES (PYC_ADA_K64ASYNC_SHARED_ELEMS * (int)sizeof(float))
|
| 24 |
+
|
| 25 |
+
typedef struct {
|
| 26 |
+
int m;
|
| 27 |
+
int n;
|
| 28 |
+
int k;
|
| 29 |
+
int warmup;
|
| 30 |
+
int iters;
|
| 31 |
+
} ada_gemm_k64async_config;
|
| 32 |
+
|
| 33 |
+
static int check_cuda(cudaError_t status, const char* what) {
|
| 34 |
+
if (status != cudaSuccess) {
|
| 35 |
+
fprintf(stderr, "%s failed: %s\n", what, cudaGetErrorString(status));
|
| 36 |
+
return -1;
|
| 37 |
+
}
|
| 38 |
+
return 0;
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
static double wall_ms_now(void) {
|
| 42 |
+
struct timespec ts;
|
| 43 |
+
timespec_get(&ts, TIME_UTC);
|
| 44 |
+
return ((double)ts.tv_sec * 1000.0) + ((double)ts.tv_nsec / 1000000.0);
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
static void fill_matrix(float* data, int rows, int cols, float scale) {
|
| 48 |
+
int i;
|
| 49 |
+
for (i = 0; i < rows * cols; ++i) {
|
| 50 |
+
int pattern = (i * 17 + rows * 13 + cols * 7) % 31;
|
| 51 |
+
data[i] = ((float)pattern - 15.0f) * scale;
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
static void reference_gemm(const float* a, const float* b, float* c, int m, int n, int k) {
|
| 56 |
+
int row;
|
| 57 |
+
for (row = 0; row < m; ++row) {
|
| 58 |
+
int col;
|
| 59 |
+
for (col = 0; col < n; ++col) {
|
| 60 |
+
float acc = 0.0f;
|
| 61 |
+
int kk;
|
| 62 |
+
for (kk = 0; kk < k; ++kk) {
|
| 63 |
+
acc += a[row * k + kk] * b[kk * n + col];
|
| 64 |
+
}
|
| 65 |
+
c[row * n + col] = acc;
|
| 66 |
+
}
|
| 67 |
+
}
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
__device__ static __forceinline__ void async_copy_16(void* dst, const void* src) {
|
| 71 |
+
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800
|
| 72 |
+
unsigned int smem_addr = (unsigned int)__cvta_generic_to_shared(dst);
|
| 73 |
+
asm volatile("cp.async.ca.shared.global [%0], [%1], 16;\n" :: "r"(smem_addr), "l"(src));
|
| 74 |
+
#else
|
| 75 |
+
*reinterpret_cast<float4*>(dst) = *reinterpret_cast<const float4*>(src);
|
| 76 |
+
#endif
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
__device__ static __forceinline__ void async_commit(void) {
|
| 80 |
+
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800
|
| 81 |
+
asm volatile("cp.async.commit_group;" ::: "memory");
|
| 82 |
+
#endif
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
__device__ static __forceinline__ void async_wait(void) {
|
| 86 |
+
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800
|
| 87 |
+
asm volatile("cp.async.wait_group 0;" ::: "memory");
|
| 88 |
+
#endif
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
__device__ static __forceinline__ float* shared_stage_a(float* shared_mem, int stage) {
|
| 92 |
+
return shared_mem + stage * (PYC_ADA_K64ASYNC_STAGE_A_ELEMS + PYC_ADA_K64ASYNC_STAGE_B_ELEMS);
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
__device__ static __forceinline__ float* shared_stage_b(float* shared_mem, int stage) {
|
| 96 |
+
return shared_stage_a(shared_mem, stage) + PYC_ADA_K64ASYNC_STAGE_A_ELEMS;
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
__device__ static __forceinline__ float shared_a_load(const float* shared_a, int row, int col) {
|
| 100 |
+
return shared_a[row * PYC_ADA_K64ASYNC_SHARED_STRIDE_A + col];
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
__device__ static __forceinline__ float shared_b_load(const float* shared_b, int row, int col) {
|
| 104 |
+
return shared_b[row * PYC_ADA_K64ASYNC_SHARED_STRIDE_B + col];
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
__device__ static __forceinline__ void shared_a_store(float* shared_a, int row, int col, float value) {
|
| 108 |
+
shared_a[row * PYC_ADA_K64ASYNC_SHARED_STRIDE_A + col] = value;
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
__device__ static __forceinline__ void shared_b_store(float* shared_b, int row, int col, float value) {
|
| 112 |
+
shared_b[row * PYC_ADA_K64ASYNC_SHARED_STRIDE_B + col] = value;
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
__device__ static void load_a_stage(
|
| 116 |
+
const float* __restrict__ a,
|
| 117 |
+
float* shared_a,
|
| 118 |
+
int stage,
|
| 119 |
+
int lane_linear,
|
| 120 |
+
int block_row,
|
| 121 |
+
int kk_base,
|
| 122 |
+
int m,
|
| 123 |
+
int k) {
|
| 124 |
+
const int block_threads = PYC_ADA_K64ASYNC_THREADS_X * PYC_ADA_K64ASYNC_THREADS_Y;
|
| 125 |
+
const int vecs_per_row = PYC_ADA_K64ASYNC_BLOCK_K / PYC_ADA_K64ASYNC_VEC;
|
| 126 |
+
const int total_vecs = (PYC_ADA_K64ASYNC_BLOCK_M * PYC_ADA_K64ASYNC_BLOCK_K) / PYC_ADA_K64ASYNC_VEC;
|
| 127 |
+
const int full_tile = (block_row + PYC_ADA_K64ASYNC_BLOCK_M <= m) &&
|
| 128 |
+
(kk_base + PYC_ADA_K64ASYNC_BLOCK_K <= k) &&
|
| 129 |
+
((k & (PYC_ADA_K64ASYNC_VEC - 1)) == 0);
|
| 130 |
+
int phase;
|
| 131 |
+
|
| 132 |
+
for (phase = 0; phase < total_vecs / block_threads; ++phase) {
|
| 133 |
+
const int linear = lane_linear + phase * block_threads;
|
| 134 |
+
const int tile_row = linear / vecs_per_row;
|
| 135 |
+
const int tile_col = (linear % vecs_per_row) * PYC_ADA_K64ASYNC_VEC;
|
| 136 |
+
const int global_row = block_row + tile_row;
|
| 137 |
+
const int global_col = kk_base + tile_col;
|
| 138 |
+
int i;
|
| 139 |
+
|
| 140 |
+
if (full_tile) {
|
| 141 |
+
async_copy_16(
|
| 142 |
+
&shared_a[tile_row * PYC_ADA_K64ASYNC_SHARED_STRIDE_A + tile_col],
|
| 143 |
+
&a[global_row * k + global_col]);
|
| 144 |
+
continue;
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
for (i = 0; i < PYC_ADA_K64ASYNC_VEC; ++i) {
|
| 148 |
+
float value = 0.0f;
|
| 149 |
+
if (global_row < m && global_col + i < k) {
|
| 150 |
+
value = a[global_row * k + global_col + i];
|
| 151 |
+
}
|
| 152 |
+
shared_a_store(shared_a, tile_row, tile_col + i, value);
|
| 153 |
+
}
|
| 154 |
+
}
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
__device__ static void load_b_stage(
|
| 158 |
+
const float* __restrict__ b,
|
| 159 |
+
float* shared_b,
|
| 160 |
+
int stage,
|
| 161 |
+
int lane_linear,
|
| 162 |
+
int block_col,
|
| 163 |
+
int kk_base,
|
| 164 |
+
int k,
|
| 165 |
+
int n) {
|
| 166 |
+
const int block_threads = PYC_ADA_K64ASYNC_THREADS_X * PYC_ADA_K64ASYNC_THREADS_Y;
|
| 167 |
+
const int vecs_per_row = PYC_ADA_K64ASYNC_BLOCK_N / PYC_ADA_K64ASYNC_VEC;
|
| 168 |
+
const int total_vecs = (PYC_ADA_K64ASYNC_BLOCK_K * PYC_ADA_K64ASYNC_BLOCK_N) / PYC_ADA_K64ASYNC_VEC;
|
| 169 |
+
const int full_tile = (block_col + PYC_ADA_K64ASYNC_BLOCK_N <= n) &&
|
| 170 |
+
(kk_base + PYC_ADA_K64ASYNC_BLOCK_K <= k) &&
|
| 171 |
+
((n & (PYC_ADA_K64ASYNC_VEC - 1)) == 0);
|
| 172 |
+
int phase;
|
| 173 |
+
|
| 174 |
+
for (phase = 0; phase < total_vecs / block_threads; ++phase) {
|
| 175 |
+
const int linear = lane_linear + phase * block_threads;
|
| 176 |
+
const int tile_row = linear / vecs_per_row;
|
| 177 |
+
const int tile_col = (linear % vecs_per_row) * PYC_ADA_K64ASYNC_VEC;
|
| 178 |
+
const int global_row = kk_base + tile_row;
|
| 179 |
+
const int global_col = block_col + tile_col;
|
| 180 |
+
int i;
|
| 181 |
+
|
| 182 |
+
if (full_tile) {
|
| 183 |
+
async_copy_16(
|
| 184 |
+
&shared_b[tile_row * PYC_ADA_K64ASYNC_SHARED_STRIDE_B + tile_col],
|
| 185 |
+
&b[global_row * n + global_col]);
|
| 186 |
+
continue;
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
for (i = 0; i < PYC_ADA_K64ASYNC_VEC; ++i) {
|
| 190 |
+
float value = 0.0f;
|
| 191 |
+
if (global_row < k && global_col + i < n) {
|
| 192 |
+
value = b[global_row * n + global_col + i];
|
| 193 |
+
}
|
| 194 |
+
shared_b_store(shared_b, tile_row, tile_col + i, value);
|
| 195 |
+
}
|
| 196 |
+
}
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
__launch_bounds__(PYC_ADA_K64ASYNC_THREADS_X * PYC_ADA_K64ASYNC_THREADS_Y, 2)
|
| 200 |
+
__global__ void ada_fp32_k64async_gemm(
|
| 201 |
+
const float* __restrict__ a,
|
| 202 |
+
const float* __restrict__ b,
|
| 203 |
+
float* __restrict__ c,
|
| 204 |
+
int m,
|
| 205 |
+
int n,
|
| 206 |
+
int k) {
|
| 207 |
+
extern __shared__ __align__(16) float shared_mem[];
|
| 208 |
+
|
| 209 |
+
const int lane_linear = threadIdx.y * blockDim.x + threadIdx.x;
|
| 210 |
+
const int block_row = blockIdx.y * PYC_ADA_K64ASYNC_BLOCK_M;
|
| 211 |
+
const int block_col = blockIdx.x * PYC_ADA_K64ASYNC_BLOCK_N;
|
| 212 |
+
const int row_fragment = threadIdx.y * PYC_ADA_K64ASYNC_THREAD_TILE_M;
|
| 213 |
+
const int col_fragment = threadIdx.x * PYC_ADA_K64ASYNC_THREAD_TILE_N;
|
| 214 |
+
float accum[PYC_ADA_K64ASYNC_THREAD_TILE_M][PYC_ADA_K64ASYNC_THREAD_TILE_N];
|
| 215 |
+
int kk_base;
|
| 216 |
+
int stage;
|
| 217 |
+
int next_stage;
|
| 218 |
+
int i;
|
| 219 |
+
int j;
|
| 220 |
+
|
| 221 |
+
for (i = 0; i < PYC_ADA_K64ASYNC_THREAD_TILE_M; ++i) {
|
| 222 |
+
for (j = 0; j < PYC_ADA_K64ASYNC_THREAD_TILE_N; ++j) {
|
| 223 |
+
accum[i][j] = 0.0f;
|
| 224 |
+
}
|
| 225 |
+
}
|
| 226 |
+
|
| 227 |
+
stage = 0;
|
| 228 |
+
load_a_stage(a, shared_stage_a(shared_mem, stage), stage, lane_linear, block_row, 0, m, k);
|
| 229 |
+
load_b_stage(b, shared_stage_b(shared_mem, stage), stage, lane_linear, block_col, 0, k, n);
|
| 230 |
+
async_commit();
|
| 231 |
+
async_wait();
|
| 232 |
+
__syncthreads();
|
| 233 |
+
|
| 234 |
+
for (kk_base = 0; kk_base < k; kk_base += PYC_ADA_K64ASYNC_BLOCK_K) {
|
| 235 |
+
const int next_kk = kk_base + PYC_ADA_K64ASYNC_BLOCK_K;
|
| 236 |
+
|
| 237 |
+
next_stage = stage ^ 1;
|
| 238 |
+
if (next_kk < k) {
|
| 239 |
+
load_a_stage(a, shared_stage_a(shared_mem, next_stage), next_stage, lane_linear, block_row, next_kk, m, k);
|
| 240 |
+
load_b_stage(b, shared_stage_b(shared_mem, next_stage), next_stage, lane_linear, block_col, next_kk, k, n);
|
| 241 |
+
async_commit();
|
| 242 |
+
}
|
| 243 |
+
|
| 244 |
+
#pragma unroll
|
| 245 |
+
for (i = 0; i < PYC_ADA_K64ASYNC_BLOCK_K; ++i) {
|
| 246 |
+
float a_frag[PYC_ADA_K64ASYNC_THREAD_TILE_M];
|
| 247 |
+
float b_frag[PYC_ADA_K64ASYNC_THREAD_TILE_N];
|
| 248 |
+
int ii;
|
| 249 |
+
|
| 250 |
+
#pragma unroll
|
| 251 |
+
for (ii = 0; ii < PYC_ADA_K64ASYNC_THREAD_TILE_M; ++ii) {
|
| 252 |
+
a_frag[ii] = shared_a_load(shared_stage_a(shared_mem, stage), row_fragment + ii, i);
|
| 253 |
+
}
|
| 254 |
+
|
| 255 |
+
#pragma unroll
|
| 256 |
+
for (ii = 0; ii < PYC_ADA_K64ASYNC_THREAD_TILE_N; ++ii) {
|
| 257 |
+
b_frag[ii] = shared_b_load(shared_stage_b(shared_mem, stage), i, col_fragment + ii);
|
| 258 |
+
}
|
| 259 |
+
|
| 260 |
+
#pragma unroll
|
| 261 |
+
for (ii = 0; ii < PYC_ADA_K64ASYNC_THREAD_TILE_M; ++ii) {
|
| 262 |
+
int jj;
|
| 263 |
+
#pragma unroll
|
| 264 |
+
for (jj = 0; jj < PYC_ADA_K64ASYNC_THREAD_TILE_N; ++jj) {
|
| 265 |
+
accum[ii][jj] = fmaf(a_frag[ii], b_frag[jj], accum[ii][jj]);
|
| 266 |
+
}
|
| 267 |
+
}
|
| 268 |
+
}
|
| 269 |
+
|
| 270 |
+
__syncthreads();
|
| 271 |
+
if (next_kk < k) {
|
| 272 |
+
async_wait();
|
| 273 |
+
__syncthreads();
|
| 274 |
+
stage = next_stage;
|
| 275 |
+
}
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
for (i = 0; i < PYC_ADA_K64ASYNC_THREAD_TILE_M; ++i) {
|
| 279 |
+
int out_row = block_row + row_fragment + i;
|
| 280 |
+
if (out_row >= m) {
|
| 281 |
+
continue;
|
| 282 |
+
}
|
| 283 |
+
if (col_fragment + 1 < PYC_ADA_K64ASYNC_BLOCK_N) {
|
| 284 |
+
int out_col = block_col + col_fragment;
|
| 285 |
+
if (out_col + 1 < n) {
|
| 286 |
+
float2 value;
|
| 287 |
+
value.x = accum[i][0];
|
| 288 |
+
value.y = accum[i][1];
|
| 289 |
+
*reinterpret_cast<float2*>(&c[out_row * n + out_col]) = value;
|
| 290 |
+
continue;
|
| 291 |
+
}
|
| 292 |
+
}
|
| 293 |
+
for (j = 0; j < PYC_ADA_K64ASYNC_THREAD_TILE_N; ++j) {
|
| 294 |
+
int out_col = block_col + col_fragment + j;
|
| 295 |
+
if (out_col < n) {
|
| 296 |
+
c[out_row * n + out_col] = accum[i][j];
|
| 297 |
+
}
|
| 298 |
+
}
|
| 299 |
+
}
|
| 300 |
+
}
|
| 301 |
+
|
| 302 |
+
static int configure_ada_kernel(void) {
|
| 303 |
+
cudaError_t status;
|
| 304 |
+
|
| 305 |
+
status = cudaFuncSetAttribute(
|
| 306 |
+
ada_fp32_k64async_gemm,
|
| 307 |
+
cudaFuncAttributeMaxDynamicSharedMemorySize,
|
| 308 |
+
PYC_ADA_K64ASYNC_SHARED_BYTES);
|
| 309 |
+
if (status != cudaSuccess && status != cudaErrorNotSupported) {
|
| 310 |
+
fprintf(stderr, "cudaFuncSetAttribute(max_dynamic_shared) failed: %s\n", cudaGetErrorString(status));
|
| 311 |
+
return -1;
|
| 312 |
+
}
|
| 313 |
+
|
| 314 |
+
status = cudaFuncSetAttribute(
|
| 315 |
+
ada_fp32_k64async_gemm,
|
| 316 |
+
cudaFuncAttributePreferredSharedMemoryCarveout,
|
| 317 |
+
100);
|
| 318 |
+
if (status != cudaSuccess && status != cudaErrorNotSupported) {
|
| 319 |
+
fprintf(stderr, "cudaFuncSetAttribute failed: %s\n", cudaGetErrorString(status));
|
| 320 |
+
return -1;
|
| 321 |
+
}
|
| 322 |
+
|
| 323 |
+
return 0;
|
| 324 |
+
}
|
| 325 |
+
|
| 326 |
+
static int parse_int_arg(const char* text, int* out) {
|
| 327 |
+
char* end = NULL;
|
| 328 |
+
long value;
|
| 329 |
+
|
| 330 |
+
if (!text || !out) {
|
| 331 |
+
return -1;
|
| 332 |
+
}
|
| 333 |
+
|
| 334 |
+
value = strtol(text, &end, 10);
|
| 335 |
+
if (*text == '\0' || !end || *end != '\0' || value <= 0 || value > 1 << 20) {
|
| 336 |
+
return -1;
|
| 337 |
+
}
|
| 338 |
+
|
| 339 |
+
*out = (int)value;
|
| 340 |
+
return 0;
|
| 341 |
+
}
|
| 342 |
+
|
| 343 |
+
static int parse_config(int argc, char** argv, ada_gemm_k64async_config* cfg) {
|
| 344 |
+
if (!cfg) {
|
| 345 |
+
return -1;
|
| 346 |
+
}
|
| 347 |
+
|
| 348 |
+
cfg->m = 1024;
|
| 349 |
+
cfg->n = 1024;
|
| 350 |
+
cfg->k = 1024;
|
| 351 |
+
cfg->warmup = 10;
|
| 352 |
+
cfg->iters = 50;
|
| 353 |
+
|
| 354 |
+
if (argc > 1 && parse_int_arg(argv[1], &cfg->m) != 0) return -1;
|
| 355 |
+
if (argc > 2 && parse_int_arg(argv[2], &cfg->n) != 0) return -1;
|
| 356 |
+
if (argc > 3 && parse_int_arg(argv[3], &cfg->k) != 0) return -1;
|
| 357 |
+
if (argc > 4 && parse_int_arg(argv[4], &cfg->warmup) != 0) return -1;
|
| 358 |
+
if (argc > 5 && parse_int_arg(argv[5], &cfg->iters) != 0) return -1;
|
| 359 |
+
|
| 360 |
+
return 0;
|
| 361 |
+
}
|
| 362 |
+
|
| 363 |
+
int main(int argc, char** argv) {
|
| 364 |
+
ada_gemm_k64async_config cfg;
|
| 365 |
+
cudaDeviceProp props;
|
| 366 |
+
float* host_a = NULL;
|
| 367 |
+
float* host_b = NULL;
|
| 368 |
+
float* host_c = NULL;
|
| 369 |
+
float* ref_c = NULL;
|
| 370 |
+
float* dev_a = NULL;
|
| 371 |
+
float* dev_b = NULL;
|
| 372 |
+
float* dev_c = NULL;
|
| 373 |
+
cudaEvent_t start = NULL;
|
| 374 |
+
cudaEvent_t stop = NULL;
|
| 375 |
+
size_t a_bytes;
|
| 376 |
+
size_t b_bytes;
|
| 377 |
+
size_t c_bytes;
|
| 378 |
+
dim3 block;
|
| 379 |
+
dim3 grid;
|
| 380 |
+
float elapsed_ms = 0.0f;
|
| 381 |
+
double best_ms = 0.0;
|
| 382 |
+
double best_wall_ms = 0.0;
|
| 383 |
+
double wall_sum_ms = 0.0;
|
| 384 |
+
int iter;
|
| 385 |
+
double max_abs_diff = 0.0;
|
| 386 |
+
int device = 0;
|
| 387 |
+
|
| 388 |
+
if (parse_config(argc, argv, &cfg) != 0) {
|
| 389 |
+
fprintf(stderr, "usage: %s [m] [n] [k] [warmup] [iters]\n", argv[0]);
|
| 390 |
+
return 2;
|
| 391 |
+
}
|
| 392 |
+
|
| 393 |
+
if (check_cuda(cudaGetDevice(&device), "cudaGetDevice") != 0) return 1;
|
| 394 |
+
if (check_cuda(cudaGetDeviceProperties(&props, device), "cudaGetDeviceProperties") != 0) return 1;
|
| 395 |
+
|
| 396 |
+
printf("device=%s cc=%d.%d\n", props.name, props.major, props.minor);
|
| 397 |
+
if (!(props.major == 8 && props.minor == 9)) {
|
| 398 |
+
printf("note=prototype tuned for Ada (sm_89); running on a different architecture\n");
|
| 399 |
+
}
|
| 400 |
+
|
| 401 |
+
a_bytes = (size_t)cfg.m * (size_t)cfg.k * sizeof(float);
|
| 402 |
+
b_bytes = (size_t)cfg.k * (size_t)cfg.n * sizeof(float);
|
| 403 |
+
c_bytes = (size_t)cfg.m * (size_t)cfg.n * sizeof(float);
|
| 404 |
+
|
| 405 |
+
host_a = (float*)malloc(a_bytes);
|
| 406 |
+
host_b = (float*)malloc(b_bytes);
|
| 407 |
+
host_c = (float*)malloc(c_bytes);
|
| 408 |
+
ref_c = (float*)malloc(c_bytes);
|
| 409 |
+
if (!host_a || !host_b || !host_c || !ref_c) {
|
| 410 |
+
fprintf(stderr, "host allocation failed\n");
|
| 411 |
+
return 1;
|
| 412 |
+
}
|
| 413 |
+
|
| 414 |
+
fill_matrix(host_a, cfg.m, cfg.k, 0.03125f);
|
| 415 |
+
fill_matrix(host_b, cfg.k, cfg.n, 0.0625f);
|
| 416 |
+
reference_gemm(host_a, host_b, ref_c, cfg.m, cfg.n, cfg.k);
|
| 417 |
+
|
| 418 |
+
if (check_cuda(cudaMalloc((void**)&dev_a, a_bytes), "cudaMalloc(a)") != 0) return 1;
|
| 419 |
+
if (check_cuda(cudaMalloc((void**)&dev_b, b_bytes), "cudaMalloc(b)") != 0) return 1;
|
| 420 |
+
if (check_cuda(cudaMalloc((void**)&dev_c, c_bytes), "cudaMalloc(c)") != 0) return 1;
|
| 421 |
+
|
| 422 |
+
if (check_cuda(cudaMemcpy(dev_a, host_a, a_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(a)") != 0) return 1;
|
| 423 |
+
if (check_cuda(cudaMemcpy(dev_b, host_b, b_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(b)") != 0) return 1;
|
| 424 |
+
|
| 425 |
+
if (configure_ada_kernel() != 0) return 1;
|
| 426 |
+
|
| 427 |
+
block = dim3(PYC_ADA_K64ASYNC_THREADS_X, PYC_ADA_K64ASYNC_THREADS_Y, 1);
|
| 428 |
+
grid = dim3(
|
| 429 |
+
(unsigned int)((cfg.n + PYC_ADA_K64ASYNC_BLOCK_N - 1) / PYC_ADA_K64ASYNC_BLOCK_N),
|
| 430 |
+
(unsigned int)((cfg.m + PYC_ADA_K64ASYNC_BLOCK_M - 1) / PYC_ADA_K64ASYNC_BLOCK_M),
|
| 431 |
+
1);
|
| 432 |
+
|
| 433 |
+
if (check_cuda(cudaEventCreate(&start), "cudaEventCreate(start)") != 0) return 1;
|
| 434 |
+
if (check_cuda(cudaEventCreate(&stop), "cudaEventCreate(stop)") != 0) return 1;
|
| 435 |
+
|
| 436 |
+
for (iter = 0; iter < cfg.warmup; ++iter) {
|
| 437 |
+
ada_fp32_k64async_gemm<<<grid, block, PYC_ADA_K64ASYNC_SHARED_BYTES>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 438 |
+
}
|
| 439 |
+
if (check_cuda(cudaGetLastError(), "kernel launch warmup") != 0) return 1;
|
| 440 |
+
if (check_cuda(cudaDeviceSynchronize(), "cudaDeviceSynchronize warmup") != 0) return 1;
|
| 441 |
+
|
| 442 |
+
best_ms = 0.0;
|
| 443 |
+
best_wall_ms = 0.0;
|
| 444 |
+
wall_sum_ms = 0.0;
|
| 445 |
+
for (iter = 0; iter < cfg.iters; ++iter) {
|
| 446 |
+
double wall_start_ms = wall_ms_now();
|
| 447 |
+
if (check_cuda(cudaEventRecord(start), "cudaEventRecord(start)") != 0) return 1;
|
| 448 |
+
ada_fp32_k64async_gemm<<<grid, block, PYC_ADA_K64ASYNC_SHARED_BYTES>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 449 |
+
if (check_cuda(cudaEventRecord(stop), "cudaEventRecord(stop)") != 0) return 1;
|
| 450 |
+
if (check_cuda(cudaEventSynchronize(stop), "cudaEventSynchronize(stop)") != 0) return 1;
|
| 451 |
+
if (check_cuda(cudaEventElapsedTime(&elapsed_ms, start, stop), "cudaEventElapsedTime") != 0) return 1;
|
| 452 |
+
{
|
| 453 |
+
double wall_elapsed_ms = wall_ms_now() - wall_start_ms;
|
| 454 |
+
wall_sum_ms += wall_elapsed_ms;
|
| 455 |
+
if (iter == 0 || wall_elapsed_ms < best_wall_ms) {
|
| 456 |
+
best_wall_ms = wall_elapsed_ms;
|
| 457 |
+
}
|
| 458 |
+
}
|
| 459 |
+
if (iter == 0 || elapsed_ms < (float)best_ms) {
|
| 460 |
+
best_ms = elapsed_ms;
|
| 461 |
+
}
|
| 462 |
+
}
|
| 463 |
+
|
| 464 |
+
if (check_cuda(cudaMemcpy(host_c, dev_c, c_bytes, cudaMemcpyDeviceToHost), "cudaMemcpy(c)") != 0) return 1;
|
| 465 |
+
|
| 466 |
+
for (iter = 0; iter < cfg.m * cfg.n; ++iter) {
|
| 467 |
+
double diff = fabs((double)host_c[iter] - (double)ref_c[iter]);
|
| 468 |
+
if (diff > max_abs_diff) {
|
| 469 |
+
max_abs_diff = diff;
|
| 470 |
+
}
|
| 471 |
+
}
|
| 472 |
+
|
| 473 |
+
printf("shape=%dx%dx%d\n", cfg.m, cfg.n, cfg.k);
|
| 474 |
+
printf("tile=%dx%dx%d threads=%dx%d thread_tile=%dx%d vector_width=%d\n",
|
| 475 |
+
PYC_ADA_K64ASYNC_BLOCK_M,
|
| 476 |
+
PYC_ADA_K64ASYNC_BLOCK_N,
|
| 477 |
+
PYC_ADA_K64ASYNC_BLOCK_K,
|
| 478 |
+
PYC_ADA_K64ASYNC_THREADS_X,
|
| 479 |
+
PYC_ADA_K64ASYNC_THREADS_Y,
|
| 480 |
+
PYC_ADA_K64ASYNC_THREAD_TILE_M,
|
| 481 |
+
PYC_ADA_K64ASYNC_THREAD_TILE_N,
|
| 482 |
+
PYC_ADA_K64ASYNC_VEC);
|
| 483 |
+
printf("best_ms=%.3f\n", best_ms);
|
| 484 |
+
if (cfg.iters > 0) {
|
| 485 |
+
printf("best_wall_ms=%.3f\n", best_wall_ms);
|
| 486 |
+
printf("mean_wall_ms=%.3f\n", wall_sum_ms / (double)cfg.iters);
|
| 487 |
+
}
|
| 488 |
+
printf("max_abs_diff=%.6f\n", max_abs_diff);
|
| 489 |
+
if (best_ms > 0.0) {
|
| 490 |
+
double flops = 2.0 * (double)cfg.m * (double)cfg.n * (double)cfg.k;
|
| 491 |
+
double gflops = flops / (best_ms * 1.0e6);
|
| 492 |
+
printf("gflops=%.3f\n", gflops);
|
| 493 |
+
}
|
| 494 |
+
|
| 495 |
+
cudaEventDestroy(start);
|
| 496 |
+
cudaEventDestroy(stop);
|
| 497 |
+
cudaFree(dev_a);
|
| 498 |
+
cudaFree(dev_b);
|
| 499 |
+
cudaFree(dev_c);
|
| 500 |
+
free(host_a);
|
| 501 |
+
free(host_b);
|
| 502 |
+
free(host_c);
|
| 503 |
+
free(ref_c);
|
| 504 |
+
return max_abs_diff <= 1e-2 ? 0 : 1;
|
| 505 |
+
}
|
kernels/prototypes/ada/gemm_k64_warp32_ilp2/kernel.cu
ADDED
|
@@ -0,0 +1,397 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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ADA_K64ILP2_THREADS_X 32
|
| 12 |
+
#define PYC_ADA_K64ILP2_THREADS_Y 8
|
| 13 |
+
#define PYC_ADA_K64ILP2_THREAD_TILE_M 8
|
| 14 |
+
#define PYC_ADA_K64ILP2_THREAD_TILE_N 2
|
| 15 |
+
#define PYC_ADA_K64ILP2_VEC 4
|
| 16 |
+
|
| 17 |
+
typedef struct {
|
| 18 |
+
int m;
|
| 19 |
+
int n;
|
| 20 |
+
int k;
|
| 21 |
+
int warmup;
|
| 22 |
+
int iters;
|
| 23 |
+
} ada_gemm_k64ilp2_config;
|
| 24 |
+
|
| 25 |
+
static int check_cuda(cudaError_t status, const char* what) {
|
| 26 |
+
if (status != cudaSuccess) {
|
| 27 |
+
fprintf(stderr, "%s failed: %s\n", what, cudaGetErrorString(status));
|
| 28 |
+
return -1;
|
| 29 |
+
}
|
| 30 |
+
return 0;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
static void fill_matrix(float* data, int rows, int cols, float scale) {
|
| 34 |
+
int i;
|
| 35 |
+
for (i = 0; i < rows * cols; ++i) {
|
| 36 |
+
int pattern = (i * 17 + rows * 13 + cols * 7) % 31;
|
| 37 |
+
data[i] = ((float)pattern - 15.0f) * scale;
|
| 38 |
+
}
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
static void reference_gemm(const float* a, const float* b, float* c, int m, int n, int k) {
|
| 42 |
+
int row;
|
| 43 |
+
for (row = 0; row < m; ++row) {
|
| 44 |
+
int col;
|
| 45 |
+
for (col = 0; col < n; ++col) {
|
| 46 |
+
float acc = 0.0f;
|
| 47 |
+
int kk;
|
| 48 |
+
for (kk = 0; kk < k; ++kk) {
|
| 49 |
+
acc += a[row * k + kk] * b[kk * n + col];
|
| 50 |
+
}
|
| 51 |
+
c[row * n + col] = acc;
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
__device__ static void load_a_vec(
|
| 57 |
+
const float* __restrict__ a,
|
| 58 |
+
float shared_a[PYC_ADA_K64ILP2_BLOCK_M][PYC_ADA_K64ILP2_BLOCK_K + 1],
|
| 59 |
+
int lane_linear,
|
| 60 |
+
int block_row,
|
| 61 |
+
int kk_base,
|
| 62 |
+
int m,
|
| 63 |
+
int k) {
|
| 64 |
+
const int vecs_per_row = PYC_ADA_K64ILP2_BLOCK_K / PYC_ADA_K64ILP2_VEC;
|
| 65 |
+
const int total_vecs = (PYC_ADA_K64ILP2_BLOCK_M * PYC_ADA_K64ILP2_BLOCK_K) / PYC_ADA_K64ILP2_VEC;
|
| 66 |
+
int phase;
|
| 67 |
+
|
| 68 |
+
for (phase = 0; phase < total_vecs / (PYC_ADA_K64ILP2_THREADS_X * PYC_ADA_K64ILP2_THREADS_Y); ++phase) {
|
| 69 |
+
const int linear = lane_linear + phase * (PYC_ADA_K64ILP2_THREADS_X * PYC_ADA_K64ILP2_THREADS_Y);
|
| 70 |
+
const int tile_row = linear / vecs_per_row;
|
| 71 |
+
const int tile_col = (linear % vecs_per_row) * PYC_ADA_K64ILP2_VEC;
|
| 72 |
+
const int global_row = block_row + tile_row;
|
| 73 |
+
const int global_col = kk_base + tile_col;
|
| 74 |
+
int i;
|
| 75 |
+
|
| 76 |
+
if (global_row < m && global_col + (PYC_ADA_K64ILP2_VEC - 1) < k) {
|
| 77 |
+
const float4 value = *reinterpret_cast<const float4*>(&a[global_row * k + global_col]);
|
| 78 |
+
shared_a[tile_row][tile_col + 0] = value.x;
|
| 79 |
+
shared_a[tile_row][tile_col + 1] = value.y;
|
| 80 |
+
shared_a[tile_row][tile_col + 2] = value.z;
|
| 81 |
+
shared_a[tile_row][tile_col + 3] = value.w;
|
| 82 |
+
continue;
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
for (i = 0; i < PYC_ADA_K64ILP2_VEC; ++i) {
|
| 86 |
+
float value = 0.0f;
|
| 87 |
+
if (global_row < m && global_col + i < k) {
|
| 88 |
+
value = a[global_row * k + global_col + i];
|
| 89 |
+
}
|
| 90 |
+
shared_a[tile_row][tile_col + i] = value;
|
| 91 |
+
}
|
| 92 |
+
}
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
__device__ static void load_b_vec(
|
| 96 |
+
const float* __restrict__ b,
|
| 97 |
+
float shared_b[PYC_ADA_K64ILP2_BLOCK_K][PYC_ADA_K64ILP2_BLOCK_N + 1],
|
| 98 |
+
int lane_linear,
|
| 99 |
+
int block_col,
|
| 100 |
+
int kk_base,
|
| 101 |
+
int k,
|
| 102 |
+
int n) {
|
| 103 |
+
const int vecs_per_row = PYC_ADA_K64ILP2_BLOCK_N / PYC_ADA_K64ILP2_VEC;
|
| 104 |
+
const int total_vecs = (PYC_ADA_K64ILP2_BLOCK_K * PYC_ADA_K64ILP2_BLOCK_N) / PYC_ADA_K64ILP2_VEC;
|
| 105 |
+
int phase;
|
| 106 |
+
|
| 107 |
+
for (phase = 0; phase < total_vecs / (PYC_ADA_K64ILP2_THREADS_X * PYC_ADA_K64ILP2_THREADS_Y); ++phase) {
|
| 108 |
+
const int linear = lane_linear + phase * (PYC_ADA_K64ILP2_THREADS_X * PYC_ADA_K64ILP2_THREADS_Y);
|
| 109 |
+
const int tile_row = linear / vecs_per_row;
|
| 110 |
+
const int tile_col = (linear % vecs_per_row) * PYC_ADA_K64ILP2_VEC;
|
| 111 |
+
const int global_row = kk_base + tile_row;
|
| 112 |
+
const int global_col = block_col + tile_col;
|
| 113 |
+
int i;
|
| 114 |
+
|
| 115 |
+
if (global_row < k && global_col + (PYC_ADA_K64ILP2_VEC - 1) < n) {
|
| 116 |
+
const float4 value = *reinterpret_cast<const float4*>(&b[global_row * n + global_col]);
|
| 117 |
+
shared_b[tile_row][tile_col + 0] = value.x;
|
| 118 |
+
shared_b[tile_row][tile_col + 1] = value.y;
|
| 119 |
+
shared_b[tile_row][tile_col + 2] = value.z;
|
| 120 |
+
shared_b[tile_row][tile_col + 3] = value.w;
|
| 121 |
+
continue;
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
for (i = 0; i < PYC_ADA_K64ILP2_VEC; ++i) {
|
| 125 |
+
float value = 0.0f;
|
| 126 |
+
if (global_row < k && global_col + i < n) {
|
| 127 |
+
value = b[global_row * n + global_col + i];
|
| 128 |
+
}
|
| 129 |
+
shared_b[tile_row][tile_col + i] = value;
|
| 130 |
+
}
|
| 131 |
+
}
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
__launch_bounds__(PYC_ADA_K64ILP2_THREADS_X * PYC_ADA_K64ILP2_THREADS_Y, 2)
|
| 135 |
+
__global__ void ada_fp32_k64ilp2_gemm(
|
| 136 |
+
const float* __restrict__ a,
|
| 137 |
+
const float* __restrict__ b,
|
| 138 |
+
float* __restrict__ c,
|
| 139 |
+
int m,
|
| 140 |
+
int n,
|
| 141 |
+
int k) {
|
| 142 |
+
__shared__ float shared_a[PYC_ADA_K64ILP2_BLOCK_M][PYC_ADA_K64ILP2_BLOCK_K + 1];
|
| 143 |
+
__shared__ float shared_b[PYC_ADA_K64ILP2_BLOCK_K][PYC_ADA_K64ILP2_BLOCK_N + 1];
|
| 144 |
+
|
| 145 |
+
const int lane_linear = threadIdx.y * blockDim.x + threadIdx.x;
|
| 146 |
+
const int block_row = blockIdx.y * PYC_ADA_K64ILP2_BLOCK_M;
|
| 147 |
+
const int block_col = blockIdx.x * PYC_ADA_K64ILP2_BLOCK_N;
|
| 148 |
+
const int row_fragment = threadIdx.y * PYC_ADA_K64ILP2_THREAD_TILE_M;
|
| 149 |
+
const int col_fragment = threadIdx.x * PYC_ADA_K64ILP2_THREAD_TILE_N;
|
| 150 |
+
float accum[PYC_ADA_K64ILP2_THREAD_TILE_M][PYC_ADA_K64ILP2_THREAD_TILE_N];
|
| 151 |
+
int kk_base;
|
| 152 |
+
int i;
|
| 153 |
+
int j;
|
| 154 |
+
|
| 155 |
+
for (i = 0; i < PYC_ADA_K64ILP2_THREAD_TILE_M; ++i) {
|
| 156 |
+
for (j = 0; j < PYC_ADA_K64ILP2_THREAD_TILE_N; ++j) {
|
| 157 |
+
accum[i][j] = 0.0f;
|
| 158 |
+
}
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
for (kk_base = 0; kk_base < k; kk_base += PYC_ADA_K64ILP2_BLOCK_K) {
|
| 162 |
+
load_a_vec(a, shared_a, lane_linear, block_row, kk_base, m, k);
|
| 163 |
+
load_b_vec(b, shared_b, lane_linear, block_col, kk_base, k, n);
|
| 164 |
+
|
| 165 |
+
__syncthreads();
|
| 166 |
+
|
| 167 |
+
#pragma unroll
|
| 168 |
+
for (i = 0; i < PYC_ADA_K64ILP2_BLOCK_K; i += 2) {
|
| 169 |
+
float a_frag0[PYC_ADA_K64ILP2_THREAD_TILE_M];
|
| 170 |
+
float a_frag1[PYC_ADA_K64ILP2_THREAD_TILE_M];
|
| 171 |
+
const float b0_0 = shared_b[i + 0][col_fragment + 0];
|
| 172 |
+
const float b0_1 = shared_b[i + 0][col_fragment + 1];
|
| 173 |
+
const float b1_0 = shared_b[i + 1][col_fragment + 0];
|
| 174 |
+
const float b1_1 = shared_b[i + 1][col_fragment + 1];
|
| 175 |
+
int ii;
|
| 176 |
+
|
| 177 |
+
#pragma unroll
|
| 178 |
+
for (ii = 0; ii < PYC_ADA_K64ILP2_THREAD_TILE_M; ++ii) {
|
| 179 |
+
a_frag0[ii] = shared_a[row_fragment + ii][i + 0];
|
| 180 |
+
a_frag1[ii] = shared_a[row_fragment + ii][i + 1];
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
#pragma unroll
|
| 184 |
+
for (ii = 0; ii < PYC_ADA_K64ILP2_THREAD_TILE_M; ++ii) {
|
| 185 |
+
accum[ii][0] = fmaf(a_frag0[ii], b0_0, accum[ii][0]);
|
| 186 |
+
accum[ii][1] = fmaf(a_frag0[ii], b0_1, accum[ii][1]);
|
| 187 |
+
accum[ii][0] = fmaf(a_frag1[ii], b1_0, accum[ii][0]);
|
| 188 |
+
accum[ii][1] = fmaf(a_frag1[ii], b1_1, accum[ii][1]);
|
| 189 |
+
}
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
__syncthreads();
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
for (i = 0; i < PYC_ADA_K64ILP2_THREAD_TILE_M; ++i) {
|
| 196 |
+
int out_row = block_row + row_fragment + i;
|
| 197 |
+
if (out_row >= m) {
|
| 198 |
+
continue;
|
| 199 |
+
}
|
| 200 |
+
if (col_fragment + 1 < PYC_ADA_K64ILP2_BLOCK_N) {
|
| 201 |
+
int out_col = block_col + col_fragment;
|
| 202 |
+
if (out_col + 1 < n) {
|
| 203 |
+
float2 value;
|
| 204 |
+
value.x = accum[i][0];
|
| 205 |
+
value.y = accum[i][1];
|
| 206 |
+
*reinterpret_cast<float2*>(&c[out_row * n + out_col]) = value;
|
| 207 |
+
continue;
|
| 208 |
+
}
|
| 209 |
+
}
|
| 210 |
+
for (j = 0; j < PYC_ADA_K64ILP2_THREAD_TILE_N; ++j) {
|
| 211 |
+
int out_col = block_col + col_fragment + j;
|
| 212 |
+
if (out_col < n) {
|
| 213 |
+
c[out_row * n + out_col] = accum[i][j];
|
| 214 |
+
}
|
| 215 |
+
}
|
| 216 |
+
}
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
static int configure_ada_kernel(void) {
|
| 220 |
+
cudaError_t status;
|
| 221 |
+
|
| 222 |
+
status = cudaFuncSetAttribute(
|
| 223 |
+
ada_fp32_k64ilp2_gemm,
|
| 224 |
+
cudaFuncAttributePreferredSharedMemoryCarveout,
|
| 225 |
+
100);
|
| 226 |
+
if (status != cudaSuccess && status != cudaErrorNotSupported) {
|
| 227 |
+
fprintf(stderr, "cudaFuncSetAttribute failed: %s\n", cudaGetErrorString(status));
|
| 228 |
+
return -1;
|
| 229 |
+
}
|
| 230 |
+
|
| 231 |
+
return 0;
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
static int parse_int_arg(const char* text, int* out) {
|
| 235 |
+
char* end = NULL;
|
| 236 |
+
long value;
|
| 237 |
+
|
| 238 |
+
if (!text || !out) {
|
| 239 |
+
return -1;
|
| 240 |
+
}
|
| 241 |
+
|
| 242 |
+
value = strtol(text, &end, 10);
|
| 243 |
+
if (*text == '\0' || !end || *end != '\0' || value <= 0 || value > 1 << 20) {
|
| 244 |
+
return -1;
|
| 245 |
+
}
|
| 246 |
+
|
| 247 |
+
*out = (int)value;
|
| 248 |
+
return 0;
|
| 249 |
+
}
|
| 250 |
+
|
| 251 |
+
static int parse_config(int argc, char** argv, ada_gemm_k64ilp2_config* cfg) {
|
| 252 |
+
if (!cfg) {
|
| 253 |
+
return -1;
|
| 254 |
+
}
|
| 255 |
+
|
| 256 |
+
cfg->m = 1024;
|
| 257 |
+
cfg->n = 1024;
|
| 258 |
+
cfg->k = 1024;
|
| 259 |
+
cfg->warmup = 10;
|
| 260 |
+
cfg->iters = 50;
|
| 261 |
+
|
| 262 |
+
if (argc > 1 && parse_int_arg(argv[1], &cfg->m) != 0) return -1;
|
| 263 |
+
if (argc > 2 && parse_int_arg(argv[2], &cfg->n) != 0) return -1;
|
| 264 |
+
if (argc > 3 && parse_int_arg(argv[3], &cfg->k) != 0) return -1;
|
| 265 |
+
if (argc > 4 && parse_int_arg(argv[4], &cfg->warmup) != 0) return -1;
|
| 266 |
+
if (argc > 5 && parse_int_arg(argv[5], &cfg->iters) != 0) return -1;
|
| 267 |
+
|
| 268 |
+
return 0;
|
| 269 |
+
}
|
| 270 |
+
|
| 271 |
+
int main(int argc, char** argv) {
|
| 272 |
+
ada_gemm_k64ilp2_config cfg;
|
| 273 |
+
cudaDeviceProp props;
|
| 274 |
+
float* host_a = NULL;
|
| 275 |
+
float* host_b = NULL;
|
| 276 |
+
float* host_c = NULL;
|
| 277 |
+
float* ref_c = NULL;
|
| 278 |
+
float* dev_a = NULL;
|
| 279 |
+
float* dev_b = NULL;
|
| 280 |
+
float* dev_c = NULL;
|
| 281 |
+
cudaEvent_t start = NULL;
|
| 282 |
+
cudaEvent_t stop = NULL;
|
| 283 |
+
size_t a_bytes;
|
| 284 |
+
size_t b_bytes;
|
| 285 |
+
size_t c_bytes;
|
| 286 |
+
dim3 block;
|
| 287 |
+
dim3 grid;
|
| 288 |
+
float elapsed_ms = 0.0f;
|
| 289 |
+
double best_ms = 0.0;
|
| 290 |
+
int iter;
|
| 291 |
+
double max_abs_diff = 0.0;
|
| 292 |
+
int device = 0;
|
| 293 |
+
|
| 294 |
+
if (parse_config(argc, argv, &cfg) != 0) {
|
| 295 |
+
fprintf(stderr, "usage: %s [m] [n] [k] [warmup] [iters]\n", argv[0]);
|
| 296 |
+
return 2;
|
| 297 |
+
}
|
| 298 |
+
|
| 299 |
+
if (check_cuda(cudaGetDevice(&device), "cudaGetDevice") != 0) return 1;
|
| 300 |
+
if (check_cuda(cudaGetDeviceProperties(&props, device), "cudaGetDeviceProperties") != 0) return 1;
|
| 301 |
+
|
| 302 |
+
printf("device=%s cc=%d.%d\n", props.name, props.major, props.minor);
|
| 303 |
+
if (!(props.major == 8 && props.minor == 9)) {
|
| 304 |
+
printf("note=prototype tuned for Ada (sm_89); running on a different architecture\n");
|
| 305 |
+
}
|
| 306 |
+
|
| 307 |
+
a_bytes = (size_t)cfg.m * (size_t)cfg.k * sizeof(float);
|
| 308 |
+
b_bytes = (size_t)cfg.k * (size_t)cfg.n * sizeof(float);
|
| 309 |
+
c_bytes = (size_t)cfg.m * (size_t)cfg.n * sizeof(float);
|
| 310 |
+
|
| 311 |
+
host_a = (float*)malloc(a_bytes);
|
| 312 |
+
host_b = (float*)malloc(b_bytes);
|
| 313 |
+
host_c = (float*)malloc(c_bytes);
|
| 314 |
+
ref_c = (float*)malloc(c_bytes);
|
| 315 |
+
if (!host_a || !host_b || !host_c || !ref_c) {
|
| 316 |
+
fprintf(stderr, "host allocation failed\n");
|
| 317 |
+
return 1;
|
| 318 |
+
}
|
| 319 |
+
|
| 320 |
+
fill_matrix(host_a, cfg.m, cfg.k, 0.03125f);
|
| 321 |
+
fill_matrix(host_b, cfg.k, cfg.n, 0.0625f);
|
| 322 |
+
reference_gemm(host_a, host_b, ref_c, cfg.m, cfg.n, cfg.k);
|
| 323 |
+
|
| 324 |
+
if (check_cuda(cudaMalloc((void**)&dev_a, a_bytes), "cudaMalloc(a)") != 0) return 1;
|
| 325 |
+
if (check_cuda(cudaMalloc((void**)&dev_b, b_bytes), "cudaMalloc(b)") != 0) return 1;
|
| 326 |
+
if (check_cuda(cudaMalloc((void**)&dev_c, c_bytes), "cudaMalloc(c)") != 0) return 1;
|
| 327 |
+
|
| 328 |
+
if (check_cuda(cudaMemcpy(dev_a, host_a, a_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(a)") != 0) return 1;
|
| 329 |
+
if (check_cuda(cudaMemcpy(dev_b, host_b, b_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(b)") != 0) return 1;
|
| 330 |
+
|
| 331 |
+
if (configure_ada_kernel() != 0) return 1;
|
| 332 |
+
|
| 333 |
+
block = dim3(PYC_ADA_K64ILP2_THREADS_X, PYC_ADA_K64ILP2_THREADS_Y, 1);
|
| 334 |
+
grid = dim3(
|
| 335 |
+
(unsigned int)((cfg.n + PYC_ADA_K64ILP2_BLOCK_N - 1) / PYC_ADA_K64ILP2_BLOCK_N),
|
| 336 |
+
(unsigned int)((cfg.m + PYC_ADA_K64ILP2_BLOCK_M - 1) / PYC_ADA_K64ILP2_BLOCK_M),
|
| 337 |
+
1);
|
| 338 |
+
|
| 339 |
+
if (check_cuda(cudaEventCreate(&start), "cudaEventCreate(start)") != 0) return 1;
|
| 340 |
+
if (check_cuda(cudaEventCreate(&stop), "cudaEventCreate(stop)") != 0) return 1;
|
| 341 |
+
|
| 342 |
+
for (iter = 0; iter < cfg.warmup; ++iter) {
|
| 343 |
+
ada_fp32_k64ilp2_gemm<<<grid, block>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 344 |
+
}
|
| 345 |
+
if (check_cuda(cudaGetLastError(), "kernel launch warmup") != 0) return 1;
|
| 346 |
+
if (check_cuda(cudaDeviceSynchronize(), "cudaDeviceSynchronize warmup") != 0) return 1;
|
| 347 |
+
|
| 348 |
+
best_ms = 0.0;
|
| 349 |
+
for (iter = 0; iter < cfg.iters; ++iter) {
|
| 350 |
+
if (check_cuda(cudaEventRecord(start), "cudaEventRecord(start)") != 0) return 1;
|
| 351 |
+
ada_fp32_k64ilp2_gemm<<<grid, block>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 352 |
+
if (check_cuda(cudaEventRecord(stop), "cudaEventRecord(stop)") != 0) return 1;
|
| 353 |
+
if (check_cuda(cudaEventSynchronize(stop), "cudaEventSynchronize(stop)") != 0) return 1;
|
| 354 |
+
if (check_cuda(cudaEventElapsedTime(&elapsed_ms, start, stop), "cudaEventElapsedTime") != 0) return 1;
|
| 355 |
+
if (iter == 0 || elapsed_ms < (float)best_ms) {
|
| 356 |
+
best_ms = elapsed_ms;
|
| 357 |
+
}
|
| 358 |
+
}
|
| 359 |
+
|
| 360 |
+
if (check_cuda(cudaMemcpy(host_c, dev_c, c_bytes, cudaMemcpyDeviceToHost), "cudaMemcpy(c)") != 0) return 1;
|
| 361 |
+
|
| 362 |
+
for (iter = 0; iter < cfg.m * cfg.n; ++iter) {
|
| 363 |
+
double diff = fabs((double)host_c[iter] - (double)ref_c[iter]);
|
| 364 |
+
if (diff > max_abs_diff) {
|
| 365 |
+
max_abs_diff = diff;
|
| 366 |
+
}
|
| 367 |
+
}
|
| 368 |
+
|
| 369 |
+
printf("shape=%dx%dx%d\n", cfg.m, cfg.n, cfg.k);
|
| 370 |
+
printf("tile=%dx%dx%d threads=%dx%d thread_tile=%dx%d vector_width=%d\n",
|
| 371 |
+
PYC_ADA_K64ILP2_BLOCK_M,
|
| 372 |
+
PYC_ADA_K64ILP2_BLOCK_N,
|
| 373 |
+
PYC_ADA_K64ILP2_BLOCK_K,
|
| 374 |
+
PYC_ADA_K64ILP2_THREADS_X,
|
| 375 |
+
PYC_ADA_K64ILP2_THREADS_Y,
|
| 376 |
+
PYC_ADA_K64ILP2_THREAD_TILE_M,
|
| 377 |
+
PYC_ADA_K64ILP2_THREAD_TILE_N,
|
| 378 |
+
PYC_ADA_K64ILP2_VEC);
|
| 379 |
+
printf("best_ms=%.3f\n", best_ms);
|
| 380 |
+
printf("max_abs_diff=%.6f\n", max_abs_diff);
|
| 381 |
+
if (best_ms > 0.0) {
|
| 382 |
+
double flops = 2.0 * (double)cfg.m * (double)cfg.n * (double)cfg.k;
|
| 383 |
+
double gflops = flops / (best_ms * 1.0e6);
|
| 384 |
+
printf("gflops=%.3f\n", gflops);
|
| 385 |
+
}
|
| 386 |
+
|
| 387 |
+
cudaEventDestroy(start);
|
| 388 |
+
cudaEventDestroy(stop);
|
| 389 |
+
cudaFree(dev_a);
|
| 390 |
+
cudaFree(dev_b);
|
| 391 |
+
cudaFree(dev_c);
|
| 392 |
+
free(host_a);
|
| 393 |
+
free(host_b);
|
| 394 |
+
free(host_c);
|
| 395 |
+
free(ref_c);
|
| 396 |
+
return max_abs_diff <= 1e-2 ? 0 : 1;
|
| 397 |
+
}
|
kernels/prototypes/ada/gemm_k64_warp32_lb1/kernel.cu
ADDED
|
@@ -0,0 +1,388 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
#define PYC_ADA_K64LB1_BLOCK_M 64
|
| 9 |
+
#define PYC_ADA_K64LB1_BLOCK_N 64
|
| 10 |
+
#define PYC_ADA_K64LB1_BLOCK_K 64
|
| 11 |
+
#define PYC_ADA_K64LB1_THREADS_X 32
|
| 12 |
+
#define PYC_ADA_K64LB1_THREADS_Y 8
|
| 13 |
+
#define PYC_ADA_K64LB1_THREAD_TILE_M 8
|
| 14 |
+
#define PYC_ADA_K64LB1_THREAD_TILE_N 2
|
| 15 |
+
#define PYC_ADA_K64LB1_VEC 4
|
| 16 |
+
|
| 17 |
+
typedef struct {
|
| 18 |
+
int m;
|
| 19 |
+
int n;
|
| 20 |
+
int k;
|
| 21 |
+
int warmup;
|
| 22 |
+
int iters;
|
| 23 |
+
} ada_gemm_k64lb1_config;
|
| 24 |
+
|
| 25 |
+
static int check_cuda(cudaError_t status, const char* what) {
|
| 26 |
+
if (status != cudaSuccess) {
|
| 27 |
+
fprintf(stderr, "%s failed: %s\n", what, cudaGetErrorString(status));
|
| 28 |
+
return -1;
|
| 29 |
+
}
|
| 30 |
+
return 0;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
static void fill_matrix(float* data, int rows, int cols, float scale) {
|
| 34 |
+
int i;
|
| 35 |
+
for (i = 0; i < rows * cols; ++i) {
|
| 36 |
+
int pattern = (i * 17 + rows * 13 + cols * 7) % 31;
|
| 37 |
+
data[i] = ((float)pattern - 15.0f) * scale;
|
| 38 |
+
}
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
static void reference_gemm(const float* a, const float* b, float* c, int m, int n, int k) {
|
| 42 |
+
int row;
|
| 43 |
+
for (row = 0; row < m; ++row) {
|
| 44 |
+
int col;
|
| 45 |
+
for (col = 0; col < n; ++col) {
|
| 46 |
+
float acc = 0.0f;
|
| 47 |
+
int kk;
|
| 48 |
+
for (kk = 0; kk < k; ++kk) {
|
| 49 |
+
acc += a[row * k + kk] * b[kk * n + col];
|
| 50 |
+
}
|
| 51 |
+
c[row * n + col] = acc;
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
__device__ static void load_a_vec(
|
| 57 |
+
const float* __restrict__ a,
|
| 58 |
+
float shared_a[PYC_ADA_K64LB1_BLOCK_M][PYC_ADA_K64LB1_BLOCK_K + 1],
|
| 59 |
+
int lane_linear,
|
| 60 |
+
int block_row,
|
| 61 |
+
int kk_base,
|
| 62 |
+
int m,
|
| 63 |
+
int k) {
|
| 64 |
+
const int vecs_per_row = PYC_ADA_K64LB1_BLOCK_K / PYC_ADA_K64LB1_VEC;
|
| 65 |
+
const int total_vecs = (PYC_ADA_K64LB1_BLOCK_M * PYC_ADA_K64LB1_BLOCK_K) / PYC_ADA_K64LB1_VEC;
|
| 66 |
+
int phase;
|
| 67 |
+
|
| 68 |
+
for (phase = 0; phase < total_vecs / (PYC_ADA_K64LB1_THREADS_X * PYC_ADA_K64LB1_THREADS_Y); ++phase) {
|
| 69 |
+
const int linear = lane_linear + phase * (PYC_ADA_K64LB1_THREADS_X * PYC_ADA_K64LB1_THREADS_Y);
|
| 70 |
+
const int tile_row = linear / vecs_per_row;
|
| 71 |
+
const int tile_col = (linear % vecs_per_row) * PYC_ADA_K64LB1_VEC;
|
| 72 |
+
const int global_row = block_row + tile_row;
|
| 73 |
+
const int global_col = kk_base + tile_col;
|
| 74 |
+
int i;
|
| 75 |
+
|
| 76 |
+
if (global_row < m && global_col + (PYC_ADA_K64LB1_VEC - 1) < k) {
|
| 77 |
+
const float4 value = *reinterpret_cast<const float4*>(&a[global_row * k + global_col]);
|
| 78 |
+
shared_a[tile_row][tile_col + 0] = value.x;
|
| 79 |
+
shared_a[tile_row][tile_col + 1] = value.y;
|
| 80 |
+
shared_a[tile_row][tile_col + 2] = value.z;
|
| 81 |
+
shared_a[tile_row][tile_col + 3] = value.w;
|
| 82 |
+
continue;
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
for (i = 0; i < PYC_ADA_K64LB1_VEC; ++i) {
|
| 86 |
+
float value = 0.0f;
|
| 87 |
+
if (global_row < m && global_col + i < k) {
|
| 88 |
+
value = a[global_row * k + global_col + i];
|
| 89 |
+
}
|
| 90 |
+
shared_a[tile_row][tile_col + i] = value;
|
| 91 |
+
}
|
| 92 |
+
}
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
__device__ static void load_b_vec(
|
| 96 |
+
const float* __restrict__ b,
|
| 97 |
+
float shared_b[PYC_ADA_K64LB1_BLOCK_K][PYC_ADA_K64LB1_BLOCK_N + 1],
|
| 98 |
+
int lane_linear,
|
| 99 |
+
int block_col,
|
| 100 |
+
int kk_base,
|
| 101 |
+
int k,
|
| 102 |
+
int n) {
|
| 103 |
+
const int vecs_per_row = PYC_ADA_K64LB1_BLOCK_N / PYC_ADA_K64LB1_VEC;
|
| 104 |
+
const int total_vecs = (PYC_ADA_K64LB1_BLOCK_K * PYC_ADA_K64LB1_BLOCK_N) / PYC_ADA_K64LB1_VEC;
|
| 105 |
+
int phase;
|
| 106 |
+
|
| 107 |
+
for (phase = 0; phase < total_vecs / (PYC_ADA_K64LB1_THREADS_X * PYC_ADA_K64LB1_THREADS_Y); ++phase) {
|
| 108 |
+
const int linear = lane_linear + phase * (PYC_ADA_K64LB1_THREADS_X * PYC_ADA_K64LB1_THREADS_Y);
|
| 109 |
+
const int tile_row = linear / vecs_per_row;
|
| 110 |
+
const int tile_col = (linear % vecs_per_row) * PYC_ADA_K64LB1_VEC;
|
| 111 |
+
const int global_row = kk_base + tile_row;
|
| 112 |
+
const int global_col = block_col + tile_col;
|
| 113 |
+
int i;
|
| 114 |
+
|
| 115 |
+
if (global_row < k && global_col + (PYC_ADA_K64LB1_VEC - 1) < n) {
|
| 116 |
+
const float4 value = *reinterpret_cast<const float4*>(&b[global_row * n + global_col]);
|
| 117 |
+
shared_b[tile_row][tile_col + 0] = value.x;
|
| 118 |
+
shared_b[tile_row][tile_col + 1] = value.y;
|
| 119 |
+
shared_b[tile_row][tile_col + 2] = value.z;
|
| 120 |
+
shared_b[tile_row][tile_col + 3] = value.w;
|
| 121 |
+
continue;
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
for (i = 0; i < PYC_ADA_K64LB1_VEC; ++i) {
|
| 125 |
+
float value = 0.0f;
|
| 126 |
+
if (global_row < k && global_col + i < n) {
|
| 127 |
+
value = b[global_row * n + global_col + i];
|
| 128 |
+
}
|
| 129 |
+
shared_b[tile_row][tile_col + i] = value;
|
| 130 |
+
}
|
| 131 |
+
}
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
__launch_bounds__(PYC_ADA_K64LB1_THREADS_X * PYC_ADA_K64LB1_THREADS_Y, 1)
|
| 135 |
+
__global__ void ada_fp32_k64lb1_gemm(
|
| 136 |
+
const float* __restrict__ a,
|
| 137 |
+
const float* __restrict__ b,
|
| 138 |
+
float* __restrict__ c,
|
| 139 |
+
int m,
|
| 140 |
+
int n,
|
| 141 |
+
int k) {
|
| 142 |
+
__shared__ float shared_a[PYC_ADA_K64LB1_BLOCK_M][PYC_ADA_K64LB1_BLOCK_K + 1];
|
| 143 |
+
__shared__ float shared_b[PYC_ADA_K64LB1_BLOCK_K][PYC_ADA_K64LB1_BLOCK_N + 1];
|
| 144 |
+
|
| 145 |
+
const int lane_linear = threadIdx.y * blockDim.x + threadIdx.x;
|
| 146 |
+
const int block_row = blockIdx.y * PYC_ADA_K64LB1_BLOCK_M;
|
| 147 |
+
const int block_col = blockIdx.x * PYC_ADA_K64LB1_BLOCK_N;
|
| 148 |
+
const int row_fragment = threadIdx.y * PYC_ADA_K64LB1_THREAD_TILE_M;
|
| 149 |
+
const int col_fragment = threadIdx.x * PYC_ADA_K64LB1_THREAD_TILE_N;
|
| 150 |
+
float accum[PYC_ADA_K64LB1_THREAD_TILE_M][PYC_ADA_K64LB1_THREAD_TILE_N];
|
| 151 |
+
int kk_base;
|
| 152 |
+
int i;
|
| 153 |
+
int j;
|
| 154 |
+
|
| 155 |
+
for (i = 0; i < PYC_ADA_K64LB1_THREAD_TILE_M; ++i) {
|
| 156 |
+
for (j = 0; j < PYC_ADA_K64LB1_THREAD_TILE_N; ++j) {
|
| 157 |
+
accum[i][j] = 0.0f;
|
| 158 |
+
}
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
for (kk_base = 0; kk_base < k; kk_base += PYC_ADA_K64LB1_BLOCK_K) {
|
| 162 |
+
load_a_vec(a, shared_a, lane_linear, block_row, kk_base, m, k);
|
| 163 |
+
load_b_vec(b, shared_b, lane_linear, block_col, kk_base, k, n);
|
| 164 |
+
|
| 165 |
+
__syncthreads();
|
| 166 |
+
|
| 167 |
+
#pragma unroll
|
| 168 |
+
for (i = 0; i < PYC_ADA_K64LB1_BLOCK_K; ++i) {
|
| 169 |
+
float a_frag[PYC_ADA_K64LB1_THREAD_TILE_M];
|
| 170 |
+
float b_frag[PYC_ADA_K64LB1_THREAD_TILE_N];
|
| 171 |
+
int ii;
|
| 172 |
+
|
| 173 |
+
#pragma unroll
|
| 174 |
+
for (ii = 0; ii < PYC_ADA_K64LB1_THREAD_TILE_M; ++ii) {
|
| 175 |
+
a_frag[ii] = shared_a[row_fragment + ii][i];
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
#pragma unroll
|
| 179 |
+
for (ii = 0; ii < PYC_ADA_K64LB1_THREAD_TILE_N; ++ii) {
|
| 180 |
+
b_frag[ii] = shared_b[i][col_fragment + ii];
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
#pragma unroll
|
| 184 |
+
for (ii = 0; ii < PYC_ADA_K64LB1_THREAD_TILE_M; ++ii) {
|
| 185 |
+
int jj;
|
| 186 |
+
#pragma unroll
|
| 187 |
+
for (jj = 0; jj < PYC_ADA_K64LB1_THREAD_TILE_N; ++jj) {
|
| 188 |
+
accum[ii][jj] += a_frag[ii] * b_frag[jj];
|
| 189 |
+
}
|
| 190 |
+
}
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
__syncthreads();
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
for (i = 0; i < PYC_ADA_K64LB1_THREAD_TILE_M; ++i) {
|
| 197 |
+
int out_row = block_row + row_fragment + i;
|
| 198 |
+
if (out_row >= m) {
|
| 199 |
+
continue;
|
| 200 |
+
}
|
| 201 |
+
for (j = 0; j < PYC_ADA_K64LB1_THREAD_TILE_N; ++j) {
|
| 202 |
+
int out_col = block_col + col_fragment + j;
|
| 203 |
+
if (out_col < n) {
|
| 204 |
+
c[out_row * n + out_col] = accum[i][j];
|
| 205 |
+
}
|
| 206 |
+
}
|
| 207 |
+
}
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
static int configure_ada_kernel(void) {
|
| 211 |
+
cudaError_t status;
|
| 212 |
+
|
| 213 |
+
status = cudaFuncSetAttribute(
|
| 214 |
+
ada_fp32_k64lb1_gemm,
|
| 215 |
+
cudaFuncAttributePreferredSharedMemoryCarveout,
|
| 216 |
+
100);
|
| 217 |
+
if (status != cudaSuccess && status != cudaErrorNotSupported) {
|
| 218 |
+
fprintf(stderr, "cudaFuncSetAttribute failed: %s\n", cudaGetErrorString(status));
|
| 219 |
+
return -1;
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
return 0;
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
+
static int parse_int_arg(const char* text, int* out) {
|
| 226 |
+
char* end = NULL;
|
| 227 |
+
long value;
|
| 228 |
+
|
| 229 |
+
if (!text || !out) {
|
| 230 |
+
return -1;
|
| 231 |
+
}
|
| 232 |
+
|
| 233 |
+
value = strtol(text, &end, 10);
|
| 234 |
+
if (*text == '\0' || !end || *end != '\0' || value <= 0 || value > 1 << 20) {
|
| 235 |
+
return -1;
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
*out = (int)value;
|
| 239 |
+
return 0;
|
| 240 |
+
}
|
| 241 |
+
|
| 242 |
+
static int parse_config(int argc, char** argv, ada_gemm_k64lb1_config* cfg) {
|
| 243 |
+
if (!cfg) {
|
| 244 |
+
return -1;
|
| 245 |
+
}
|
| 246 |
+
|
| 247 |
+
cfg->m = 1024;
|
| 248 |
+
cfg->n = 1024;
|
| 249 |
+
cfg->k = 1024;
|
| 250 |
+
cfg->warmup = 10;
|
| 251 |
+
cfg->iters = 50;
|
| 252 |
+
|
| 253 |
+
if (argc > 1 && parse_int_arg(argv[1], &cfg->m) != 0) return -1;
|
| 254 |
+
if (argc > 2 && parse_int_arg(argv[2], &cfg->n) != 0) return -1;
|
| 255 |
+
if (argc > 3 && parse_int_arg(argv[3], &cfg->k) != 0) return -1;
|
| 256 |
+
if (argc > 4 && parse_int_arg(argv[4], &cfg->warmup) != 0) return -1;
|
| 257 |
+
if (argc > 5 && parse_int_arg(argv[5], &cfg->iters) != 0) return -1;
|
| 258 |
+
|
| 259 |
+
return 0;
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
int main(int argc, char** argv) {
|
| 263 |
+
ada_gemm_k64lb1_config cfg;
|
| 264 |
+
cudaDeviceProp props;
|
| 265 |
+
float* host_a = NULL;
|
| 266 |
+
float* host_b = NULL;
|
| 267 |
+
float* host_c = NULL;
|
| 268 |
+
float* ref_c = NULL;
|
| 269 |
+
float* dev_a = NULL;
|
| 270 |
+
float* dev_b = NULL;
|
| 271 |
+
float* dev_c = NULL;
|
| 272 |
+
cudaEvent_t start = NULL;
|
| 273 |
+
cudaEvent_t stop = NULL;
|
| 274 |
+
size_t a_bytes;
|
| 275 |
+
size_t b_bytes;
|
| 276 |
+
size_t c_bytes;
|
| 277 |
+
dim3 block;
|
| 278 |
+
dim3 grid;
|
| 279 |
+
float elapsed_ms = 0.0f;
|
| 280 |
+
double best_ms = 0.0;
|
| 281 |
+
int iter;
|
| 282 |
+
double max_abs_diff = 0.0;
|
| 283 |
+
int device = 0;
|
| 284 |
+
|
| 285 |
+
if (parse_config(argc, argv, &cfg) != 0) {
|
| 286 |
+
fprintf(stderr, "usage: %s [m] [n] [k] [warmup] [iters]\n", argv[0]);
|
| 287 |
+
return 2;
|
| 288 |
+
}
|
| 289 |
+
|
| 290 |
+
if (check_cuda(cudaGetDevice(&device), "cudaGetDevice") != 0) return 1;
|
| 291 |
+
if (check_cuda(cudaGetDeviceProperties(&props, device), "cudaGetDeviceProperties") != 0) return 1;
|
| 292 |
+
|
| 293 |
+
printf("device=%s cc=%d.%d\n", props.name, props.major, props.minor);
|
| 294 |
+
if (!(props.major == 8 && props.minor == 9)) {
|
| 295 |
+
printf("note=prototype tuned for Ada (sm_89); running on a different architecture\n");
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
+
a_bytes = (size_t)cfg.m * (size_t)cfg.k * sizeof(float);
|
| 299 |
+
b_bytes = (size_t)cfg.k * (size_t)cfg.n * sizeof(float);
|
| 300 |
+
c_bytes = (size_t)cfg.m * (size_t)cfg.n * sizeof(float);
|
| 301 |
+
|
| 302 |
+
host_a = (float*)malloc(a_bytes);
|
| 303 |
+
host_b = (float*)malloc(b_bytes);
|
| 304 |
+
host_c = (float*)malloc(c_bytes);
|
| 305 |
+
ref_c = (float*)malloc(c_bytes);
|
| 306 |
+
if (!host_a || !host_b || !host_c || !ref_c) {
|
| 307 |
+
fprintf(stderr, "host allocation failed\n");
|
| 308 |
+
return 1;
|
| 309 |
+
}
|
| 310 |
+
|
| 311 |
+
fill_matrix(host_a, cfg.m, cfg.k, 0.03125f);
|
| 312 |
+
fill_matrix(host_b, cfg.k, cfg.n, 0.0625f);
|
| 313 |
+
reference_gemm(host_a, host_b, ref_c, cfg.m, cfg.n, cfg.k);
|
| 314 |
+
|
| 315 |
+
if (check_cuda(cudaMalloc((void**)&dev_a, a_bytes), "cudaMalloc(a)") != 0) return 1;
|
| 316 |
+
if (check_cuda(cudaMalloc((void**)&dev_b, b_bytes), "cudaMalloc(b)") != 0) return 1;
|
| 317 |
+
if (check_cuda(cudaMalloc((void**)&dev_c, c_bytes), "cudaMalloc(c)") != 0) return 1;
|
| 318 |
+
|
| 319 |
+
if (check_cuda(cudaMemcpy(dev_a, host_a, a_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(a)") != 0) return 1;
|
| 320 |
+
if (check_cuda(cudaMemcpy(dev_b, host_b, b_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(b)") != 0) return 1;
|
| 321 |
+
|
| 322 |
+
if (configure_ada_kernel() != 0) return 1;
|
| 323 |
+
|
| 324 |
+
block = dim3(PYC_ADA_K64LB1_THREADS_X, PYC_ADA_K64LB1_THREADS_Y, 1);
|
| 325 |
+
grid = dim3(
|
| 326 |
+
(unsigned int)((cfg.n + PYC_ADA_K64LB1_BLOCK_N - 1) / PYC_ADA_K64LB1_BLOCK_N),
|
| 327 |
+
(unsigned int)((cfg.m + PYC_ADA_K64LB1_BLOCK_M - 1) / PYC_ADA_K64LB1_BLOCK_M),
|
| 328 |
+
1);
|
| 329 |
+
|
| 330 |
+
if (check_cuda(cudaEventCreate(&start), "cudaEventCreate(start)") != 0) return 1;
|
| 331 |
+
if (check_cuda(cudaEventCreate(&stop), "cudaEventCreate(stop)") != 0) return 1;
|
| 332 |
+
|
| 333 |
+
for (iter = 0; iter < cfg.warmup; ++iter) {
|
| 334 |
+
ada_fp32_k64lb1_gemm<<<grid, block>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 335 |
+
}
|
| 336 |
+
if (check_cuda(cudaGetLastError(), "kernel launch warmup") != 0) return 1;
|
| 337 |
+
if (check_cuda(cudaDeviceSynchronize(), "cudaDeviceSynchronize warmup") != 0) return 1;
|
| 338 |
+
|
| 339 |
+
best_ms = 0.0;
|
| 340 |
+
for (iter = 0; iter < cfg.iters; ++iter) {
|
| 341 |
+
if (check_cuda(cudaEventRecord(start), "cudaEventRecord(start)") != 0) return 1;
|
| 342 |
+
ada_fp32_k64lb1_gemm<<<grid, block>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 343 |
+
if (check_cuda(cudaEventRecord(stop), "cudaEventRecord(stop)") != 0) return 1;
|
| 344 |
+
if (check_cuda(cudaEventSynchronize(stop), "cudaEventSynchronize(stop)") != 0) return 1;
|
| 345 |
+
if (check_cuda(cudaEventElapsedTime(&elapsed_ms, start, stop), "cudaEventElapsedTime") != 0) return 1;
|
| 346 |
+
if (iter == 0 || elapsed_ms < (float)best_ms) {
|
| 347 |
+
best_ms = elapsed_ms;
|
| 348 |
+
}
|
| 349 |
+
}
|
| 350 |
+
|
| 351 |
+
if (check_cuda(cudaMemcpy(host_c, dev_c, c_bytes, cudaMemcpyDeviceToHost), "cudaMemcpy(c)") != 0) return 1;
|
| 352 |
+
|
| 353 |
+
for (iter = 0; iter < cfg.m * cfg.n; ++iter) {
|
| 354 |
+
double diff = fabs((double)host_c[iter] - (double)ref_c[iter]);
|
| 355 |
+
if (diff > max_abs_diff) {
|
| 356 |
+
max_abs_diff = diff;
|
| 357 |
+
}
|
| 358 |
+
}
|
| 359 |
+
|
| 360 |
+
printf("shape=%dx%dx%d\n", cfg.m, cfg.n, cfg.k);
|
| 361 |
+
printf("tile=%dx%dx%d threads=%dx%d thread_tile=%dx%d vector_width=%d\n",
|
| 362 |
+
PYC_ADA_K64LB1_BLOCK_M,
|
| 363 |
+
PYC_ADA_K64LB1_BLOCK_N,
|
| 364 |
+
PYC_ADA_K64LB1_BLOCK_K,
|
| 365 |
+
PYC_ADA_K64LB1_THREADS_X,
|
| 366 |
+
PYC_ADA_K64LB1_THREADS_Y,
|
| 367 |
+
PYC_ADA_K64LB1_THREAD_TILE_M,
|
| 368 |
+
PYC_ADA_K64LB1_THREAD_TILE_N,
|
| 369 |
+
PYC_ADA_K64LB1_VEC);
|
| 370 |
+
printf("best_ms=%.3f\n", best_ms);
|
| 371 |
+
printf("max_abs_diff=%.6f\n", max_abs_diff);
|
| 372 |
+
if (best_ms > 0.0) {
|
| 373 |
+
double flops = 2.0 * (double)cfg.m * (double)cfg.n * (double)cfg.k;
|
| 374 |
+
double gflops = flops / (best_ms * 1.0e6);
|
| 375 |
+
printf("gflops=%.3f\n", gflops);
|
| 376 |
+
}
|
| 377 |
+
|
| 378 |
+
cudaEventDestroy(start);
|
| 379 |
+
cudaEventDestroy(stop);
|
| 380 |
+
cudaFree(dev_a);
|
| 381 |
+
cudaFree(dev_b);
|
| 382 |
+
cudaFree(dev_c);
|
| 383 |
+
free(host_a);
|
| 384 |
+
free(host_b);
|
| 385 |
+
free(host_c);
|
| 386 |
+
free(ref_c);
|
| 387 |
+
return max_abs_diff <= 1e-2 ? 0 : 1;
|
| 388 |
+
}
|
kernels/prototypes/ada/gemm_k64_warp32_store2/kernel.cu
ADDED
|
@@ -0,0 +1,398 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <cuda_runtime.h>
|
| 2 |
+
|
| 3 |
+
#include <math.h>
|
| 4 |
+
#include <stdint.h>
|
| 5 |
+
#include <stdio.h>
|
| 6 |
+
#include <stdlib.h>
|
| 7 |
+
|
| 8 |
+
#define PYC_ADA_K64S2_BLOCK_M 64
|
| 9 |
+
#define PYC_ADA_K64S2_BLOCK_N 64
|
| 10 |
+
#define PYC_ADA_K64S2_BLOCK_K 64
|
| 11 |
+
#define PYC_ADA_K64S2_THREADS_X 32
|
| 12 |
+
#define PYC_ADA_K64S2_THREADS_Y 8
|
| 13 |
+
#define PYC_ADA_K64S2_THREAD_TILE_M 8
|
| 14 |
+
#define PYC_ADA_K64S2_THREAD_TILE_N 2
|
| 15 |
+
#define PYC_ADA_K64S2_VEC 4
|
| 16 |
+
|
| 17 |
+
typedef struct {
|
| 18 |
+
int m;
|
| 19 |
+
int n;
|
| 20 |
+
int k;
|
| 21 |
+
int warmup;
|
| 22 |
+
int iters;
|
| 23 |
+
} ada_gemm_k64s2_config;
|
| 24 |
+
|
| 25 |
+
static int check_cuda(cudaError_t status, const char* what) {
|
| 26 |
+
if (status != cudaSuccess) {
|
| 27 |
+
fprintf(stderr, "%s failed: %s\n", what, cudaGetErrorString(status));
|
| 28 |
+
return -1;
|
| 29 |
+
}
|
| 30 |
+
return 0;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
static void fill_matrix(float* data, int rows, int cols, float scale) {
|
| 34 |
+
int i;
|
| 35 |
+
for (i = 0; i < rows * cols; ++i) {
|
| 36 |
+
int pattern = (i * 17 + rows * 13 + cols * 7) % 31;
|
| 37 |
+
data[i] = ((float)pattern - 15.0f) * scale;
|
| 38 |
+
}
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
static void reference_gemm(const float* a, const float* b, float* c, int m, int n, int k) {
|
| 42 |
+
int row;
|
| 43 |
+
for (row = 0; row < m; ++row) {
|
| 44 |
+
int col;
|
| 45 |
+
for (col = 0; col < n; ++col) {
|
| 46 |
+
float acc = 0.0f;
|
| 47 |
+
int kk;
|
| 48 |
+
for (kk = 0; kk < k; ++kk) {
|
| 49 |
+
acc += a[row * k + kk] * b[kk * n + col];
|
| 50 |
+
}
|
| 51 |
+
c[row * n + col] = acc;
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
__device__ static void load_a_vec(
|
| 57 |
+
const float* __restrict__ a,
|
| 58 |
+
float shared_a[PYC_ADA_K64S2_BLOCK_M][PYC_ADA_K64S2_BLOCK_K + 1],
|
| 59 |
+
int lane_linear,
|
| 60 |
+
int block_row,
|
| 61 |
+
int kk_base,
|
| 62 |
+
int m,
|
| 63 |
+
int k) {
|
| 64 |
+
const int vecs_per_row = PYC_ADA_K64S2_BLOCK_K / PYC_ADA_K64S2_VEC;
|
| 65 |
+
const int total_vecs = (PYC_ADA_K64S2_BLOCK_M * PYC_ADA_K64S2_BLOCK_K) / PYC_ADA_K64S2_VEC;
|
| 66 |
+
int phase;
|
| 67 |
+
|
| 68 |
+
for (phase = 0; phase < total_vecs / (PYC_ADA_K64S2_THREADS_X * PYC_ADA_K64S2_THREADS_Y); ++phase) {
|
| 69 |
+
const int linear = lane_linear + phase * (PYC_ADA_K64S2_THREADS_X * PYC_ADA_K64S2_THREADS_Y);
|
| 70 |
+
const int tile_row = linear / vecs_per_row;
|
| 71 |
+
const int tile_col = (linear % vecs_per_row) * PYC_ADA_K64S2_VEC;
|
| 72 |
+
const int global_row = block_row + tile_row;
|
| 73 |
+
const int global_col = kk_base + tile_col;
|
| 74 |
+
int i;
|
| 75 |
+
|
| 76 |
+
if (global_row < m && global_col + (PYC_ADA_K64S2_VEC - 1) < k) {
|
| 77 |
+
const float4 value = *reinterpret_cast<const float4*>(&a[global_row * k + global_col]);
|
| 78 |
+
shared_a[tile_row][tile_col + 0] = value.x;
|
| 79 |
+
shared_a[tile_row][tile_col + 1] = value.y;
|
| 80 |
+
shared_a[tile_row][tile_col + 2] = value.z;
|
| 81 |
+
shared_a[tile_row][tile_col + 3] = value.w;
|
| 82 |
+
continue;
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
for (i = 0; i < PYC_ADA_K64S2_VEC; ++i) {
|
| 86 |
+
float value = 0.0f;
|
| 87 |
+
if (global_row < m && global_col + i < k) {
|
| 88 |
+
value = a[global_row * k + global_col + i];
|
| 89 |
+
}
|
| 90 |
+
shared_a[tile_row][tile_col + i] = value;
|
| 91 |
+
}
|
| 92 |
+
}
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
__device__ static void load_b_vec(
|
| 96 |
+
const float* __restrict__ b,
|
| 97 |
+
float shared_b[PYC_ADA_K64S2_BLOCK_K][PYC_ADA_K64S2_BLOCK_N + 1],
|
| 98 |
+
int lane_linear,
|
| 99 |
+
int block_col,
|
| 100 |
+
int kk_base,
|
| 101 |
+
int k,
|
| 102 |
+
int n) {
|
| 103 |
+
const int vecs_per_row = PYC_ADA_K64S2_BLOCK_N / PYC_ADA_K64S2_VEC;
|
| 104 |
+
const int total_vecs = (PYC_ADA_K64S2_BLOCK_K * PYC_ADA_K64S2_BLOCK_N) / PYC_ADA_K64S2_VEC;
|
| 105 |
+
int phase;
|
| 106 |
+
|
| 107 |
+
for (phase = 0; phase < total_vecs / (PYC_ADA_K64S2_THREADS_X * PYC_ADA_K64S2_THREADS_Y); ++phase) {
|
| 108 |
+
const int linear = lane_linear + phase * (PYC_ADA_K64S2_THREADS_X * PYC_ADA_K64S2_THREADS_Y);
|
| 109 |
+
const int tile_row = linear / vecs_per_row;
|
| 110 |
+
const int tile_col = (linear % vecs_per_row) * PYC_ADA_K64S2_VEC;
|
| 111 |
+
const int global_row = kk_base + tile_row;
|
| 112 |
+
const int global_col = block_col + tile_col;
|
| 113 |
+
int i;
|
| 114 |
+
|
| 115 |
+
if (global_row < k && global_col + (PYC_ADA_K64S2_VEC - 1) < n) {
|
| 116 |
+
const float4 value = *reinterpret_cast<const float4*>(&b[global_row * n + global_col]);
|
| 117 |
+
shared_b[tile_row][tile_col + 0] = value.x;
|
| 118 |
+
shared_b[tile_row][tile_col + 1] = value.y;
|
| 119 |
+
shared_b[tile_row][tile_col + 2] = value.z;
|
| 120 |
+
shared_b[tile_row][tile_col + 3] = value.w;
|
| 121 |
+
continue;
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
for (i = 0; i < PYC_ADA_K64S2_VEC; ++i) {
|
| 125 |
+
float value = 0.0f;
|
| 126 |
+
if (global_row < k && global_col + i < n) {
|
| 127 |
+
value = b[global_row * n + global_col + i];
|
| 128 |
+
}
|
| 129 |
+
shared_b[tile_row][tile_col + i] = value;
|
| 130 |
+
}
|
| 131 |
+
}
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
__launch_bounds__(PYC_ADA_K64S2_THREADS_X * PYC_ADA_K64S2_THREADS_Y, 2)
|
| 135 |
+
__global__ void ada_fp32_k64s2_gemm(
|
| 136 |
+
const float* __restrict__ a,
|
| 137 |
+
const float* __restrict__ b,
|
| 138 |
+
float* __restrict__ c,
|
| 139 |
+
int m,
|
| 140 |
+
int n,
|
| 141 |
+
int k) {
|
| 142 |
+
__shared__ float shared_a[PYC_ADA_K64S2_BLOCK_M][PYC_ADA_K64S2_BLOCK_K + 1];
|
| 143 |
+
__shared__ float shared_b[PYC_ADA_K64S2_BLOCK_K][PYC_ADA_K64S2_BLOCK_N + 1];
|
| 144 |
+
|
| 145 |
+
const int lane_linear = threadIdx.y * blockDim.x + threadIdx.x;
|
| 146 |
+
const int block_row = blockIdx.y * PYC_ADA_K64S2_BLOCK_M;
|
| 147 |
+
const int block_col = blockIdx.x * PYC_ADA_K64S2_BLOCK_N;
|
| 148 |
+
const int row_fragment = threadIdx.y * PYC_ADA_K64S2_THREAD_TILE_M;
|
| 149 |
+
const int col_fragment = threadIdx.x * PYC_ADA_K64S2_THREAD_TILE_N;
|
| 150 |
+
float accum[PYC_ADA_K64S2_THREAD_TILE_M][PYC_ADA_K64S2_THREAD_TILE_N];
|
| 151 |
+
int kk_base;
|
| 152 |
+
int i;
|
| 153 |
+
int j;
|
| 154 |
+
|
| 155 |
+
for (i = 0; i < PYC_ADA_K64S2_THREAD_TILE_M; ++i) {
|
| 156 |
+
for (j = 0; j < PYC_ADA_K64S2_THREAD_TILE_N; ++j) {
|
| 157 |
+
accum[i][j] = 0.0f;
|
| 158 |
+
}
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
for (kk_base = 0; kk_base < k; kk_base += PYC_ADA_K64S2_BLOCK_K) {
|
| 162 |
+
load_a_vec(a, shared_a, lane_linear, block_row, kk_base, m, k);
|
| 163 |
+
load_b_vec(b, shared_b, lane_linear, block_col, kk_base, k, n);
|
| 164 |
+
|
| 165 |
+
__syncthreads();
|
| 166 |
+
|
| 167 |
+
#pragma unroll
|
| 168 |
+
for (i = 0; i < PYC_ADA_K64S2_BLOCK_K; ++i) {
|
| 169 |
+
float a_frag[PYC_ADA_K64S2_THREAD_TILE_M];
|
| 170 |
+
float b_frag[PYC_ADA_K64S2_THREAD_TILE_N];
|
| 171 |
+
int ii;
|
| 172 |
+
|
| 173 |
+
#pragma unroll
|
| 174 |
+
for (ii = 0; ii < PYC_ADA_K64S2_THREAD_TILE_M; ++ii) {
|
| 175 |
+
a_frag[ii] = shared_a[row_fragment + ii][i];
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
#pragma unroll
|
| 179 |
+
for (ii = 0; ii < PYC_ADA_K64S2_THREAD_TILE_N; ++ii) {
|
| 180 |
+
b_frag[ii] = shared_b[i][col_fragment + ii];
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
#pragma unroll
|
| 184 |
+
for (ii = 0; ii < PYC_ADA_K64S2_THREAD_TILE_M; ++ii) {
|
| 185 |
+
int jj;
|
| 186 |
+
#pragma unroll
|
| 187 |
+
for (jj = 0; jj < PYC_ADA_K64S2_THREAD_TILE_N; ++jj) {
|
| 188 |
+
accum[ii][jj] = fmaf(a_frag[ii], b_frag[jj], accum[ii][jj]);
|
| 189 |
+
}
|
| 190 |
+
}
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
__syncthreads();
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
for (i = 0; i < PYC_ADA_K64S2_THREAD_TILE_M; ++i) {
|
| 197 |
+
int out_row = block_row + row_fragment + i;
|
| 198 |
+
if (out_row >= m) {
|
| 199 |
+
continue;
|
| 200 |
+
}
|
| 201 |
+
if (col_fragment + 1 < PYC_ADA_K64S2_BLOCK_N) {
|
| 202 |
+
int out_col = block_col + col_fragment;
|
| 203 |
+
if (out_col + 1 < n) {
|
| 204 |
+
float2 value;
|
| 205 |
+
value.x = accum[i][0];
|
| 206 |
+
value.y = accum[i][1];
|
| 207 |
+
*reinterpret_cast<float2*>(&c[out_row * n + out_col]) = value;
|
| 208 |
+
continue;
|
| 209 |
+
}
|
| 210 |
+
}
|
| 211 |
+
for (j = 0; j < PYC_ADA_K64S2_THREAD_TILE_N; ++j) {
|
| 212 |
+
int out_col = block_col + col_fragment + j;
|
| 213 |
+
if (out_col < n) {
|
| 214 |
+
c[out_row * n + out_col] = accum[i][j];
|
| 215 |
+
}
|
| 216 |
+
}
|
| 217 |
+
}
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
static int configure_ada_kernel(void) {
|
| 221 |
+
cudaError_t status;
|
| 222 |
+
|
| 223 |
+
status = cudaFuncSetAttribute(
|
| 224 |
+
ada_fp32_k64s2_gemm,
|
| 225 |
+
cudaFuncAttributePreferredSharedMemoryCarveout,
|
| 226 |
+
100);
|
| 227 |
+
if (status != cudaSuccess && status != cudaErrorNotSupported) {
|
| 228 |
+
fprintf(stderr, "cudaFuncSetAttribute failed: %s\n", cudaGetErrorString(status));
|
| 229 |
+
return -1;
|
| 230 |
+
}
|
| 231 |
+
|
| 232 |
+
return 0;
|
| 233 |
+
}
|
| 234 |
+
|
| 235 |
+
static int parse_int_arg(const char* text, int* out) {
|
| 236 |
+
char* end = NULL;
|
| 237 |
+
long value;
|
| 238 |
+
|
| 239 |
+
if (!text || !out) {
|
| 240 |
+
return -1;
|
| 241 |
+
}
|
| 242 |
+
|
| 243 |
+
value = strtol(text, &end, 10);
|
| 244 |
+
if (*text == '\0' || !end || *end != '\0' || value <= 0 || value > 1 << 20) {
|
| 245 |
+
return -1;
|
| 246 |
+
}
|
| 247 |
+
|
| 248 |
+
*out = (int)value;
|
| 249 |
+
return 0;
|
| 250 |
+
}
|
| 251 |
+
|
| 252 |
+
static int parse_config(int argc, char** argv, ada_gemm_k64s2_config* cfg) {
|
| 253 |
+
if (!cfg) {
|
| 254 |
+
return -1;
|
| 255 |
+
}
|
| 256 |
+
|
| 257 |
+
cfg->m = 1024;
|
| 258 |
+
cfg->n = 1024;
|
| 259 |
+
cfg->k = 1024;
|
| 260 |
+
cfg->warmup = 10;
|
| 261 |
+
cfg->iters = 50;
|
| 262 |
+
|
| 263 |
+
if (argc > 1 && parse_int_arg(argv[1], &cfg->m) != 0) return -1;
|
| 264 |
+
if (argc > 2 && parse_int_arg(argv[2], &cfg->n) != 0) return -1;
|
| 265 |
+
if (argc > 3 && parse_int_arg(argv[3], &cfg->k) != 0) return -1;
|
| 266 |
+
if (argc > 4 && parse_int_arg(argv[4], &cfg->warmup) != 0) return -1;
|
| 267 |
+
if (argc > 5 && parse_int_arg(argv[5], &cfg->iters) != 0) return -1;
|
| 268 |
+
|
| 269 |
+
return 0;
|
| 270 |
+
}
|
| 271 |
+
|
| 272 |
+
int main(int argc, char** argv) {
|
| 273 |
+
ada_gemm_k64s2_config cfg;
|
| 274 |
+
cudaDeviceProp props;
|
| 275 |
+
float* host_a = NULL;
|
| 276 |
+
float* host_b = NULL;
|
| 277 |
+
float* host_c = NULL;
|
| 278 |
+
float* ref_c = NULL;
|
| 279 |
+
float* dev_a = NULL;
|
| 280 |
+
float* dev_b = NULL;
|
| 281 |
+
float* dev_c = NULL;
|
| 282 |
+
cudaEvent_t start = NULL;
|
| 283 |
+
cudaEvent_t stop = NULL;
|
| 284 |
+
size_t a_bytes;
|
| 285 |
+
size_t b_bytes;
|
| 286 |
+
size_t c_bytes;
|
| 287 |
+
dim3 block;
|
| 288 |
+
dim3 grid;
|
| 289 |
+
float elapsed_ms = 0.0f;
|
| 290 |
+
double best_ms = 0.0;
|
| 291 |
+
int iter;
|
| 292 |
+
double max_abs_diff = 0.0;
|
| 293 |
+
int device = 0;
|
| 294 |
+
|
| 295 |
+
if (parse_config(argc, argv, &cfg) != 0) {
|
| 296 |
+
fprintf(stderr, "usage: %s [m] [n] [k] [warmup] [iters]\n", argv[0]);
|
| 297 |
+
return 2;
|
| 298 |
+
}
|
| 299 |
+
|
| 300 |
+
if (check_cuda(cudaGetDevice(&device), "cudaGetDevice") != 0) return 1;
|
| 301 |
+
if (check_cuda(cudaGetDeviceProperties(&props, device), "cudaGetDeviceProperties") != 0) return 1;
|
| 302 |
+
|
| 303 |
+
printf("device=%s cc=%d.%d\n", props.name, props.major, props.minor);
|
| 304 |
+
if (!(props.major == 8 && props.minor == 9)) {
|
| 305 |
+
printf("note=prototype tuned for Ada (sm_89); running on a different architecture\n");
|
| 306 |
+
}
|
| 307 |
+
|
| 308 |
+
a_bytes = (size_t)cfg.m * (size_t)cfg.k * sizeof(float);
|
| 309 |
+
b_bytes = (size_t)cfg.k * (size_t)cfg.n * sizeof(float);
|
| 310 |
+
c_bytes = (size_t)cfg.m * (size_t)cfg.n * sizeof(float);
|
| 311 |
+
|
| 312 |
+
host_a = (float*)malloc(a_bytes);
|
| 313 |
+
host_b = (float*)malloc(b_bytes);
|
| 314 |
+
host_c = (float*)malloc(c_bytes);
|
| 315 |
+
ref_c = (float*)malloc(c_bytes);
|
| 316 |
+
if (!host_a || !host_b || !host_c || !ref_c) {
|
| 317 |
+
fprintf(stderr, "host allocation failed\n");
|
| 318 |
+
return 1;
|
| 319 |
+
}
|
| 320 |
+
|
| 321 |
+
fill_matrix(host_a, cfg.m, cfg.k, 0.03125f);
|
| 322 |
+
fill_matrix(host_b, cfg.k, cfg.n, 0.0625f);
|
| 323 |
+
reference_gemm(host_a, host_b, ref_c, cfg.m, cfg.n, cfg.k);
|
| 324 |
+
|
| 325 |
+
if (check_cuda(cudaMalloc((void**)&dev_a, a_bytes), "cudaMalloc(a)") != 0) return 1;
|
| 326 |
+
if (check_cuda(cudaMalloc((void**)&dev_b, b_bytes), "cudaMalloc(b)") != 0) return 1;
|
| 327 |
+
if (check_cuda(cudaMalloc((void**)&dev_c, c_bytes), "cudaMalloc(c)") != 0) return 1;
|
| 328 |
+
|
| 329 |
+
if (check_cuda(cudaMemcpy(dev_a, host_a, a_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(a)") != 0) return 1;
|
| 330 |
+
if (check_cuda(cudaMemcpy(dev_b, host_b, b_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(b)") != 0) return 1;
|
| 331 |
+
|
| 332 |
+
if (configure_ada_kernel() != 0) return 1;
|
| 333 |
+
|
| 334 |
+
block = dim3(PYC_ADA_K64S2_THREADS_X, PYC_ADA_K64S2_THREADS_Y, 1);
|
| 335 |
+
grid = dim3(
|
| 336 |
+
(unsigned int)((cfg.n + PYC_ADA_K64S2_BLOCK_N - 1) / PYC_ADA_K64S2_BLOCK_N),
|
| 337 |
+
(unsigned int)((cfg.m + PYC_ADA_K64S2_BLOCK_M - 1) / PYC_ADA_K64S2_BLOCK_M),
|
| 338 |
+
1);
|
| 339 |
+
|
| 340 |
+
if (check_cuda(cudaEventCreate(&start), "cudaEventCreate(start)") != 0) return 1;
|
| 341 |
+
if (check_cuda(cudaEventCreate(&stop), "cudaEventCreate(stop)") != 0) return 1;
|
| 342 |
+
|
| 343 |
+
for (iter = 0; iter < cfg.warmup; ++iter) {
|
| 344 |
+
ada_fp32_k64s2_gemm<<<grid, block>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 345 |
+
}
|
| 346 |
+
if (check_cuda(cudaGetLastError(), "kernel launch warmup") != 0) return 1;
|
| 347 |
+
if (check_cuda(cudaDeviceSynchronize(), "cudaDeviceSynchronize warmup") != 0) return 1;
|
| 348 |
+
|
| 349 |
+
best_ms = 0.0;
|
| 350 |
+
for (iter = 0; iter < cfg.iters; ++iter) {
|
| 351 |
+
if (check_cuda(cudaEventRecord(start), "cudaEventRecord(start)") != 0) return 1;
|
| 352 |
+
ada_fp32_k64s2_gemm<<<grid, block>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 353 |
+
if (check_cuda(cudaEventRecord(stop), "cudaEventRecord(stop)") != 0) return 1;
|
| 354 |
+
if (check_cuda(cudaEventSynchronize(stop), "cudaEventSynchronize(stop)") != 0) return 1;
|
| 355 |
+
if (check_cuda(cudaEventElapsedTime(&elapsed_ms, start, stop), "cudaEventElapsedTime") != 0) return 1;
|
| 356 |
+
if (iter == 0 || elapsed_ms < (float)best_ms) {
|
| 357 |
+
best_ms = elapsed_ms;
|
| 358 |
+
}
|
| 359 |
+
}
|
| 360 |
+
|
| 361 |
+
if (check_cuda(cudaMemcpy(host_c, dev_c, c_bytes, cudaMemcpyDeviceToHost), "cudaMemcpy(c)") != 0) return 1;
|
| 362 |
+
|
| 363 |
+
for (iter = 0; iter < cfg.m * cfg.n; ++iter) {
|
| 364 |
+
double diff = fabs((double)host_c[iter] - (double)ref_c[iter]);
|
| 365 |
+
if (diff > max_abs_diff) {
|
| 366 |
+
max_abs_diff = diff;
|
| 367 |
+
}
|
| 368 |
+
}
|
| 369 |
+
|
| 370 |
+
printf("shape=%dx%dx%d\n", cfg.m, cfg.n, cfg.k);
|
| 371 |
+
printf("tile=%dx%dx%d threads=%dx%d thread_tile=%dx%d vector_width=%d\n",
|
| 372 |
+
PYC_ADA_K64S2_BLOCK_M,
|
| 373 |
+
PYC_ADA_K64S2_BLOCK_N,
|
| 374 |
+
PYC_ADA_K64S2_BLOCK_K,
|
| 375 |
+
PYC_ADA_K64S2_THREADS_X,
|
| 376 |
+
PYC_ADA_K64S2_THREADS_Y,
|
| 377 |
+
PYC_ADA_K64S2_THREAD_TILE_M,
|
| 378 |
+
PYC_ADA_K64S2_THREAD_TILE_N,
|
| 379 |
+
PYC_ADA_K64S2_VEC);
|
| 380 |
+
printf("best_ms=%.3f\n", best_ms);
|
| 381 |
+
printf("max_abs_diff=%.6f\n", max_abs_diff);
|
| 382 |
+
if (best_ms > 0.0) {
|
| 383 |
+
double flops = 2.0 * (double)cfg.m * (double)cfg.n * (double)cfg.k;
|
| 384 |
+
double gflops = flops / (best_ms * 1.0e6);
|
| 385 |
+
printf("gflops=%.3f\n", gflops);
|
| 386 |
+
}
|
| 387 |
+
|
| 388 |
+
cudaEventDestroy(start);
|
| 389 |
+
cudaEventDestroy(stop);
|
| 390 |
+
cudaFree(dev_a);
|
| 391 |
+
cudaFree(dev_b);
|
| 392 |
+
cudaF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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <cuda_runtime.h>
|
| 2 |
+
|
| 3 |
+
#include <math.h>
|
| 4 |
+
#include <stdint.h>
|
| 5 |
+
#include <stdio.h>
|
| 6 |
+
#include <stdlib.h>
|
| 7 |
+
|
| 8 |
+
#define PYC_ADA_VEC_BLOCK_M 128
|
| 9 |
+
#define PYC_ADA_VEC_BLOCK_N 64
|
| 10 |
+
#define PYC_ADA_VEC_BLOCK_K 8
|
| 11 |
+
#define PYC_ADA_VEC_THREADS_X 16
|
| 12 |
+
#define PYC_ADA_VEC_THREADS_Y 16
|
| 13 |
+
#define PYC_ADA_VEC_THREAD_TILE_M 8
|
| 14 |
+
#define PYC_ADA_VEC_THREAD_TILE_N 4
|
| 15 |
+
#define PYC_ADA_VEC_WIDTH 4
|
| 16 |
+
|
| 17 |
+
typedef struct {
|
| 18 |
+
int m;
|
| 19 |
+
int n;
|
| 20 |
+
int k;
|
| 21 |
+
int warmup;
|
| 22 |
+
int iters;
|
| 23 |
+
} ada_gemm_vec_config;
|
| 24 |
+
|
| 25 |
+
static int check_cuda(cudaError_t status, const char* what) {
|
| 26 |
+
if (status != cudaSuccess) {
|
| 27 |
+
fprintf(stderr, "%s failed: %s\n", what, cudaGetErrorString(status));
|
| 28 |
+
return -1;
|
| 29 |
+
}
|
| 30 |
+
return 0;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
static void fill_matrix(float* data, int rows, int cols, float scale) {
|
| 34 |
+
int i;
|
| 35 |
+
for (i = 0; i < rows * cols; ++i) {
|
| 36 |
+
int pattern = (i * 17 + rows * 13 + cols * 7) % 31;
|
| 37 |
+
data[i] = ((float)pattern - 15.0f) * scale;
|
| 38 |
+
}
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
static void reference_gemm(const float* a, const float* b, float* c, int m, int n, int k) {
|
| 42 |
+
int row;
|
| 43 |
+
for (row = 0; row < m; ++row) {
|
| 44 |
+
int col;
|
| 45 |
+
for (col = 0; col < n; ++col) {
|
| 46 |
+
float acc = 0.0f;
|
| 47 |
+
int kk;
|
| 48 |
+
for (kk = 0; kk < k; ++kk) {
|
| 49 |
+
acc += a[row * k + kk] * b[kk * n + col];
|
| 50 |
+
}
|
| 51 |
+
c[row * n + col] = acc;
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
__device__ static void load_a_vec(
|
| 57 |
+
const float* __restrict__ a,
|
| 58 |
+
float shared_a[PYC_ADA_VEC_BLOCK_M][PYC_ADA_VEC_BLOCK_K + 1],
|
| 59 |
+
int lane_linear,
|
| 60 |
+
int block_row,
|
| 61 |
+
int kk_base,
|
| 62 |
+
int m,
|
| 63 |
+
int k) {
|
| 64 |
+
const int vecs_per_row = PYC_ADA_VEC_BLOCK_K / PYC_ADA_VEC_WIDTH;
|
| 65 |
+
const int tile_row = lane_linear / vecs_per_row;
|
| 66 |
+
const int tile_col = (lane_linear % vecs_per_row) * PYC_ADA_VEC_WIDTH;
|
| 67 |
+
const int global_row = block_row + tile_row;
|
| 68 |
+
const int global_col = kk_base + tile_col;
|
| 69 |
+
int i;
|
| 70 |
+
|
| 71 |
+
if (global_row < m && global_col + (PYC_ADA_VEC_WIDTH - 1) < k) {
|
| 72 |
+
const float4 value = *reinterpret_cast<const float4*>(&a[global_row * k + global_col]);
|
| 73 |
+
shared_a[tile_row][tile_col + 0] = value.x;
|
| 74 |
+
shared_a[tile_row][tile_col + 1] = value.y;
|
| 75 |
+
shared_a[tile_row][tile_col + 2] = value.z;
|
| 76 |
+
shared_a[tile_row][tile_col + 3] = value.w;
|
| 77 |
+
return;
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
for (i = 0; i < PYC_ADA_VEC_WIDTH; ++i) {
|
| 81 |
+
float value = 0.0f;
|
| 82 |
+
if (global_row < m && global_col + i < k) {
|
| 83 |
+
value = a[global_row * k + global_col + i];
|
| 84 |
+
}
|
| 85 |
+
shared_a[tile_row][tile_col + i] = value;
|
| 86 |
+
}
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
__device__ static void load_b_vec(
|
| 90 |
+
const float* __restrict__ b,
|
| 91 |
+
float shared_b[PYC_ADA_VEC_BLOCK_K][PYC_ADA_VEC_BLOCK_N + 1],
|
| 92 |
+
int lane_linear,
|
| 93 |
+
int block_col,
|
| 94 |
+
int kk_base,
|
| 95 |
+
int k,
|
| 96 |
+
int n) {
|
| 97 |
+
const int vecs_per_row = PYC_ADA_VEC_BLOCK_N / PYC_ADA_VEC_WIDTH;
|
| 98 |
+
const int tile_row = lane_linear / vecs_per_row;
|
| 99 |
+
const int tile_col = (lane_linear % vecs_per_row) * PYC_ADA_VEC_WIDTH;
|
| 100 |
+
const int global_row = kk_base + tile_row;
|
| 101 |
+
const int global_col = block_col + tile_col;
|
| 102 |
+
int i;
|
| 103 |
+
|
| 104 |
+
if (global_row < k && global_col + (PYC_ADA_VEC_WIDTH - 1) < n) {
|
| 105 |
+
const float4 value = *reinterpret_cast<const float4*>(&b[global_row * n + global_col]);
|
| 106 |
+
shared_b[tile_row][tile_col + 0] = value.x;
|
| 107 |
+
shared_b[tile_row][tile_col + 1] = value.y;
|
| 108 |
+
shared_b[tile_row][tile_col + 2] = value.z;
|
| 109 |
+
shared_b[tile_row][tile_col + 3] = value.w;
|
| 110 |
+
return;
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
for (i = 0; i < PYC_ADA_VEC_WIDTH; ++i) {
|
| 114 |
+
float value = 0.0f;
|
| 115 |
+
if (global_row < k && global_col + i < n) {
|
| 116 |
+
value = b[global_row * n + global_col + i];
|
| 117 |
+
}
|
| 118 |
+
shared_b[tile_row][tile_col + i] = value;
|
| 119 |
+
}
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
__launch_bounds__(PYC_ADA_VEC_THREADS_X * PYC_ADA_VEC_THREADS_Y, 2)
|
| 123 |
+
__global__ void ada_fp32_vec_gemm(
|
| 124 |
+
const float* __restrict__ a,
|
| 125 |
+
const float* __restrict__ b,
|
| 126 |
+
float* __restrict__ c,
|
| 127 |
+
int m,
|
| 128 |
+
int n,
|
| 129 |
+
int k) {
|
| 130 |
+
__shared__ float shared_a[PYC_ADA_VEC_BLOCK_M][PYC_ADA_VEC_BLOCK_K + 1];
|
| 131 |
+
__shared__ float shared_b[PYC_ADA_VEC_BLOCK_K][PYC_ADA_VEC_BLOCK_N + 1];
|
| 132 |
+
|
| 133 |
+
const int lane_linear = threadIdx.y * blockDim.x + threadIdx.x;
|
| 134 |
+
const int block_row = blockIdx.y * PYC_ADA_VEC_BLOCK_M;
|
| 135 |
+
const int block_col = blockIdx.x * PYC_ADA_VEC_BLOCK_N;
|
| 136 |
+
const int row_fragment = threadIdx.y * PYC_ADA_VEC_THREAD_TILE_M;
|
| 137 |
+
const int col_fragment = threadIdx.x * PYC_ADA_VEC_THREAD_TILE_N;
|
| 138 |
+
float accum[PYC_ADA_VEC_THREAD_TILE_M][PYC_ADA_VEC_THREAD_TILE_N];
|
| 139 |
+
int kk_base;
|
| 140 |
+
int i;
|
| 141 |
+
int j;
|
| 142 |
+
|
| 143 |
+
for (i = 0; i < PYC_ADA_VEC_THREAD_TILE_M; ++i) {
|
| 144 |
+
for (j = 0; j < PYC_ADA_VEC_THREAD_TILE_N; ++j) {
|
| 145 |
+
accum[i][j] = 0.0f;
|
| 146 |
+
}
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
for (kk_base = 0; kk_base < k; kk_base += PYC_ADA_VEC_BLOCK_K) {
|
| 150 |
+
load_a_vec(a, shared_a, lane_linear, block_row, kk_base, m, k);
|
| 151 |
+
if (lane_linear < (PYC_ADA_VEC_BLOCK_K * PYC_ADA_VEC_BLOCK_N) / PYC_ADA_VEC_WIDTH) {
|
| 152 |
+
load_b_vec(b, shared_b, lane_linear, block_col, kk_base, k, n);
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
__syncthreads();
|
| 156 |
+
|
| 157 |
+
#pragma unroll
|
| 158 |
+
for (i = 0; i < PYC_ADA_VEC_BLOCK_K; ++i) {
|
| 159 |
+
float a_frag[PYC_ADA_VEC_THREAD_TILE_M];
|
| 160 |
+
float b_frag[PYC_ADA_VEC_THREAD_TILE_N];
|
| 161 |
+
int ii;
|
| 162 |
+
|
| 163 |
+
#pragma unroll
|
| 164 |
+
for (ii = 0; ii < PYC_ADA_VEC_THREAD_TILE_M; ++ii) {
|
| 165 |
+
a_frag[ii] = shared_a[row_fragment + ii][i];
|
| 166 |
+
}
|
| 167 |
+
#pragma unroll
|
| 168 |
+
for (ii = 0; ii < PYC_ADA_VEC_THREAD_TILE_N; ++ii) {
|
| 169 |
+
b_frag[ii] = shared_b[i][col_fragment + ii];
|
| 170 |
+
}
|
| 171 |
+
#pragma unroll
|
| 172 |
+
for (ii = 0; ii < PYC_ADA_VEC_THREAD_TILE_M; ++ii) {
|
| 173 |
+
int jj;
|
| 174 |
+
#pragma unroll
|
| 175 |
+
for (jj = 0; jj < PYC_ADA_VEC_THREAD_TILE_N; ++jj) {
|
| 176 |
+
accum[ii][jj] += a_frag[ii] * b_frag[jj];
|
| 177 |
+
}
|
| 178 |
+
}
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
__syncthreads();
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
for (i = 0; i < PYC_ADA_VEC_THREAD_TILE_M; ++i) {
|
| 185 |
+
const int out_row = block_row + row_fragment + i;
|
| 186 |
+
if (out_row >= m) {
|
| 187 |
+
continue;
|
| 188 |
+
}
|
| 189 |
+
for (j = 0; j < PYC_ADA_VEC_THREAD_TILE_N; ++j) {
|
| 190 |
+
const int out_col = block_col + col_fragment + j;
|
| 191 |
+
if (out_col < n) {
|
| 192 |
+
c[out_row * n + out_col] = accum[i][j];
|
| 193 |
+
}
|
| 194 |
+
}
|
| 195 |
+
}
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
static int configure_ada_kernel(void) {
|
| 199 |
+
cudaError_t status = cudaFuncSetAttribute(
|
| 200 |
+
ada_fp32_vec_gemm,
|
| 201 |
+
cudaFuncAttributePreferredSharedMemoryCarveout,
|
| 202 |
+
100);
|
| 203 |
+
if (status != cudaSuccess && status != cudaErrorNotSupported) {
|
| 204 |
+
fprintf(stderr, "cudaFuncSetAttribute failed: %s\n", cudaGetErrorString(status));
|
| 205 |
+
return -1;
|
| 206 |
+
}
|
| 207 |
+
return 0;
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
static int parse_int_arg(const char* text, int* out) {
|
| 211 |
+
char* end = NULL;
|
| 212 |
+
long value;
|
| 213 |
+
|
| 214 |
+
if (!text || !out) {
|
| 215 |
+
return -1;
|
| 216 |
+
}
|
| 217 |
+
|
| 218 |
+
value = strtol(text, &end, 10);
|
| 219 |
+
if (*text == '\0' || !end || *end != '\0' || value <= 0 || value > 1 << 20) {
|
| 220 |
+
return -1;
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
*out = (int)value;
|
| 224 |
+
return 0;
|
| 225 |
+
}
|
| 226 |
+
|
| 227 |
+
static int parse_config(int argc, char** argv, ada_gemm_vec_config* cfg) {
|
| 228 |
+
if (!cfg) {
|
| 229 |
+
return -1;
|
| 230 |
+
}
|
| 231 |
+
|
| 232 |
+
cfg->m = 1024;
|
| 233 |
+
cfg->n = 1024;
|
| 234 |
+
cfg->k = 1024;
|
| 235 |
+
cfg->warmup = 10;
|
| 236 |
+
cfg->iters = 50;
|
| 237 |
+
|
| 238 |
+
if (argc > 1 && parse_int_arg(argv[1], &cfg->m) != 0) return -1;
|
| 239 |
+
if (argc > 2 && parse_int_arg(argv[2], &cfg->n) != 0) return -1;
|
| 240 |
+
if (argc > 3 && parse_int_arg(argv[3], &cfg->k) != 0) return -1;
|
| 241 |
+
if (argc > 4 && parse_int_arg(argv[4], &cfg->warmup) != 0) return -1;
|
| 242 |
+
if (argc > 5 && parse_int_arg(argv[5], &cfg->iters) != 0) return -1;
|
| 243 |
+
|
| 244 |
+
return 0;
|
| 245 |
+
}
|
| 246 |
+
|
| 247 |
+
int main(int argc, char** argv) {
|
| 248 |
+
ada_gemm_vec_config cfg;
|
| 249 |
+
cudaDeviceProp props;
|
| 250 |
+
float* host_a = NULL;
|
| 251 |
+
float* host_b = NULL;
|
| 252 |
+
float* host_c = NULL;
|
| 253 |
+
float* ref_c = NULL;
|
| 254 |
+
float* dev_a = NULL;
|
| 255 |
+
float* dev_b = NULL;
|
| 256 |
+
float* dev_c = NULL;
|
| 257 |
+
cudaEvent_t start = NULL;
|
| 258 |
+
cudaEvent_t stop = NULL;
|
| 259 |
+
size_t a_bytes;
|
| 260 |
+
size_t b_bytes;
|
| 261 |
+
size_t c_bytes;
|
| 262 |
+
dim3 block;
|
| 263 |
+
dim3 grid;
|
| 264 |
+
float elapsed_ms = 0.0f;
|
| 265 |
+
double best_ms = 0.0;
|
| 266 |
+
int iter;
|
| 267 |
+
double max_abs_diff = 0.0;
|
| 268 |
+
int device = 0;
|
| 269 |
+
|
| 270 |
+
if (parse_config(argc, argv, &cfg) != 0) {
|
| 271 |
+
fprintf(stderr, "usage: %s [m] [n] [k] [warmup] [iters]\n", argv[0]);
|
| 272 |
+
return 2;
|
| 273 |
+
}
|
| 274 |
+
|
| 275 |
+
if (check_cuda(cudaGetDevice(&device), "cudaGetDevice") != 0) return 1;
|
| 276 |
+
if (check_cuda(cudaGetDeviceProperties(&props, device), "cudaGetDeviceProperties") != 0) return 1;
|
| 277 |
+
|
| 278 |
+
printf("device=%s cc=%d.%d\n", props.name, props.major, props.minor);
|
| 279 |
+
if (!(props.major == 8 && props.minor == 9)) {
|
| 280 |
+
printf("note=prototype tuned for Ada (sm_89); running on a different architecture\n");
|
| 281 |
+
}
|
| 282 |
+
|
| 283 |
+
a_bytes = (size_t)cfg.m * (size_t)cfg.k * sizeof(float);
|
| 284 |
+
b_bytes = (size_t)cfg.k * (size_t)cfg.n * sizeof(float);
|
| 285 |
+
c_bytes = (size_t)cfg.m * (size_t)cfg.n * sizeof(float);
|
| 286 |
+
|
| 287 |
+
host_a = (float*)malloc(a_bytes);
|
| 288 |
+
host_b = (float*)malloc(b_bytes);
|
| 289 |
+
host_c = (float*)malloc(c_bytes);
|
| 290 |
+
ref_c = (float*)malloc(c_bytes);
|
| 291 |
+
if (!host_a || !host_b || !host_c || !ref_c) {
|
| 292 |
+
fprintf(stderr, "host allocation failed\n");
|
| 293 |
+
return 1;
|
| 294 |
+
}
|
| 295 |
+
|
| 296 |
+
fill_matrix(host_a, cfg.m, cfg.k, 0.03125f);
|
| 297 |
+
fill_matrix(host_b, cfg.k, cfg.n, 0.0625f);
|
| 298 |
+
reference_gemm(host_a, host_b, ref_c, cfg.m, cfg.n, cfg.k);
|
| 299 |
+
|
| 300 |
+
if (check_cuda(cudaMalloc((void**)&dev_a, a_bytes), "cudaMalloc(a)") != 0) return 1;
|
| 301 |
+
if (check_cuda(cudaMalloc((void**)&dev_b, b_bytes), "cudaMalloc(b)") != 0) return 1;
|
| 302 |
+
if (check_cuda(cudaMalloc((void**)&dev_c, c_bytes), "cudaMalloc(c)") != 0) return 1;
|
| 303 |
+
|
| 304 |
+
if (check_cuda(cudaMemcpy(dev_a, host_a, a_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(a)") != 0) return 1;
|
| 305 |
+
if (check_cuda(cudaMemcpy(dev_b, host_b, b_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(b)") != 0) return 1;
|
| 306 |
+
|
| 307 |
+
if (configure_ada_kernel() != 0) return 1;
|
| 308 |
+
|
| 309 |
+
block = dim3(PYC_ADA_VEC_THREADS_X, PYC_ADA_VEC_THREADS_Y, 1);
|
| 310 |
+
grid = dim3(
|
| 311 |
+
(unsigned int)((cfg.n + PYC_ADA_VEC_BLOCK_N - 1) / PYC_ADA_VEC_BLOCK_N),
|
| 312 |
+
(unsigned int)((cfg.m + PYC_ADA_VEC_BLOCK_M - 1) / PYC_ADA_VEC_BLOCK_M),
|
| 313 |
+
1);
|
| 314 |
+
|
| 315 |
+
if (check_cuda(cudaEventCreate(&start), "cudaEventCreate(start)") != 0) return 1;
|
| 316 |
+
if (check_cuda(cudaEventCreate(&stop), "cudaEventCreate(stop)") != 0) return 1;
|
| 317 |
+
|
| 318 |
+
for (iter = 0; iter < cfg.warmup; ++iter) {
|
| 319 |
+
ada_fp32_vec_gemm<<<grid, block>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 320 |
+
}
|
| 321 |
+
if (check_cuda(cudaGetLastError(), "kernel launch warmup") != 0) return 1;
|
| 322 |
+
if (check_cuda(cudaDeviceSynchronize(), "cudaDeviceSynchronize warmup") != 0) return 1;
|
| 323 |
+
|
| 324 |
+
best_ms = 0.0;
|
| 325 |
+
for (iter = 0; iter < cfg.iters; ++iter) {
|
| 326 |
+
if (check_cuda(cudaEventRecord(start), "cudaEventRecord(start)") != 0) return 1;
|
| 327 |
+
ada_fp32_vec_gemm<<<grid, block>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 328 |
+
if (check_cuda(cudaEventRecord(stop), "cudaEventRecord(stop)") != 0) return 1;
|
| 329 |
+
if (check_cuda(cudaEventSynchronize(stop), "cudaEventSynchronize(stop)") != 0) return 1;
|
| 330 |
+
if (check_cuda(cudaEventElapsedTime(&elapsed_ms, start, stop), "cudaEventElapsedTime") != 0) return 1;
|
| 331 |
+
if (iter == 0 || elapsed_ms < (float)best_ms) {
|
| 332 |
+
best_ms = elapsed_ms;
|
| 333 |
+
}
|
| 334 |
+
}
|
| 335 |
+
|
| 336 |
+
if (check_cuda(cudaMemcpy(host_c, dev_c, c_bytes, cudaMemcpyDeviceToHost), "cudaMemcpy(c)") != 0) return 1;
|
| 337 |
+
|
| 338 |
+
for (iter = 0; iter < cfg.m * cfg.n; ++iter) {
|
| 339 |
+
double diff = fabs((double)host_c[iter] - (double)ref_c[iter]);
|
| 340 |
+
if (diff > max_abs_diff) {
|
| 341 |
+
max_abs_diff = diff;
|
| 342 |
+
}
|
| 343 |
+
}
|
| 344 |
+
|
| 345 |
+
printf("variant=ada_gemm_vec128x64\n");
|
| 346 |
+
printf("shape=%dx%dx%d\n", cfg.m, cfg.n, cfg.k);
|
| 347 |
+
printf("tile=%dx%dx%d threads=%dx%d thread_tile=%dx%d vector_width=%d\n",
|
| 348 |
+
PYC_ADA_VEC_BLOCK_M,
|
| 349 |
+
PYC_ADA_VEC_BLOCK_N,
|
| 350 |
+
PYC_ADA_VEC_BLOCK_K,
|
| 351 |
+
PYC_ADA_VEC_THREADS_X,
|
| 352 |
+
PYC_ADA_VEC_THREADS_Y,
|
| 353 |
+
PYC_ADA_VEC_THREAD_TILE_M,
|
| 354 |
+
PYC_ADA_VEC_THREAD_TILE_N,
|
| 355 |
+
PYC_ADA_VEC_WIDTH);
|
| 356 |
+
printf("best_ms=%.3f\n", best_ms);
|
| 357 |
+
printf("max_abs_diff=%.6f\n", max_abs_diff);
|
| 358 |
+
if (best_ms > 0.0) {
|
| 359 |
+
double flops = 2.0 * (double)cfg.m * (double)cfg.n * (double)cfg.k;
|
| 360 |
+
double gflops = flops / (best_ms * 1.0e6);
|
| 361 |
+
printf("gflops=%.3f\n", gflops);
|
| 362 |
+
}
|
| 363 |
+
|
| 364 |
+
cudaEventDestroy(start);
|
| 365 |
+
cudaEventDestroy(stop);
|
| 366 |
+
cudaFree(dev_a);
|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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <cuda_runtime.h>
|
| 2 |
+
#include <mma.h>
|
| 3 |
+
|
| 4 |
+
#include <math.h>
|
| 5 |
+
#include <stdint.h>
|
| 6 |
+
#include <stdio.h>
|
| 7 |
+
#include <stdlib.h>
|
| 8 |
+
|
| 9 |
+
#if defined(PYC_ADA_TENSOR_CORE_USE_BF16) && PYC_ADA_TENSOR_CORE_USE_BF16
|
| 10 |
+
#include <cuda_bf16.h>
|
| 11 |
+
typedef __nv_bfloat16 pyc_tc_scalar_t;
|
| 12 |
+
#define PYC_TC_LANE_NAME "bf16"
|
| 13 |
+
static __host__ __device__ inline pyc_tc_scalar_t pyc_tc_make_scalar(float value) {
|
| 14 |
+
return __float2bfloat16(value);
|
| 15 |
+
}
|
| 16 |
+
static __host__ __device__ inline float pyc_tc_scalar_to_float(pyc_tc_scalar_t value) {
|
| 17 |
+
return __bfloat162float(value);
|
| 18 |
+
}
|
| 19 |
+
#else
|
| 20 |
+
#include <cuda_fp16.h>
|
| 21 |
+
typedef half pyc_tc_scalar_t;
|
| 22 |
+
#define PYC_TC_LANE_NAME "fp16"
|
| 23 |
+
static __host__ __device__ inline pyc_tc_scalar_t pyc_tc_make_scalar(float value) {
|
| 24 |
+
return __float2half(value);
|
| 25 |
+
}
|
| 26 |
+
static __host__ __device__ inline float pyc_tc_scalar_to_float(pyc_tc_scalar_t value) {
|
| 27 |
+
return __half2float(value);
|
| 28 |
+
}
|
| 29 |
+
#endif
|
| 30 |
+
|
| 31 |
+
namespace wmma = nvcuda::wmma;
|
| 32 |
+
|
| 33 |
+
#define PYC_TC_CTA_M 32
|
| 34 |
+
#define PYC_TC_CTA_N 64
|
| 35 |
+
#define PYC_TC_CTA_K 16
|
| 36 |
+
#define PYC_TC_WARPS_PER_BLOCK 8
|
| 37 |
+
#define PYC_TC_THREADS_PER_BLOCK 256
|
| 38 |
+
|
| 39 |
+
typedef struct {
|
| 40 |
+
int m;
|
| 41 |
+
int n;
|
| 42 |
+
int k;
|
| 43 |
+
int warmup;
|
| 44 |
+
int iters;
|
| 45 |
+
} pyc_tc_config;
|
| 46 |
+
|
| 47 |
+
static int check_cuda(cudaError_t status, const char* what) {
|
| 48 |
+
if (status != cudaSuccess) {
|
| 49 |
+
fprintf(stderr, "%s failed: %s\n", what, cudaGetErrorString(status));
|
| 50 |
+
return -1;
|
| 51 |
+
}
|
| 52 |
+
return 0;
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
static int parse_int_arg(const char* text, int* out_value) {
|
| 56 |
+
char* end = NULL;
|
| 57 |
+
long parsed;
|
| 58 |
+
if (!text || !out_value) {
|
| 59 |
+
return -1;
|
| 60 |
+
}
|
| 61 |
+
parsed = strtol(text, &end, 10);
|
| 62 |
+
if (end == text || *end != '\0' || parsed <= 0 || parsed > INT32_MAX) {
|
| 63 |
+
return -1;
|
| 64 |
+
}
|
| 65 |
+
*out_value = (int)parsed;
|
| 66 |
+
return 0;
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
static void fill_matrix(pyc_tc_scalar_t* data, int rows, int cols, float scale) {
|
| 70 |
+
int i;
|
| 71 |
+
for (i = 0; i < rows * cols; ++i) {
|
| 72 |
+
int pattern = (i * 19 + rows * 11 + cols * 7) % 29;
|
| 73 |
+
data[i] = pyc_tc_make_scalar(((float)pattern - 14.0f) * scale);
|
| 74 |
+
}
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
static void reference_gemm(
|
| 78 |
+
const pyc_tc_scalar_t* a,
|
| 79 |
+
const pyc_tc_scalar_t* b,
|
| 80 |
+
float* c,
|
| 81 |
+
int m,
|
| 82 |
+
int n,
|
| 83 |
+
int k) {
|
| 84 |
+
int row;
|
| 85 |
+
for (row = 0; row < m; ++row) {
|
| 86 |
+
int col;
|
| 87 |
+
for (col = 0; col < n; ++col) {
|
| 88 |
+
float acc = 0.0f;
|
| 89 |
+
int kk;
|
| 90 |
+
for (kk = 0; kk < k; ++kk) {
|
| 91 |
+
acc += pyc_tc_scalar_to_float(a[row * k + kk]) * pyc_tc_scalar_to_float(b[kk * n + col]);
|
| 92 |
+
}
|
| 93 |
+
c[row * n + col] = acc;
|
| 94 |
+
}
|
| 95 |
+
}
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
__launch_bounds__(PYC_TC_THREADS_PER_BLOCK, 2)
|
| 99 |
+
__global__ void pyc_tc_gemm_kernel(
|
| 100 |
+
const pyc_tc_scalar_t* __restrict__ a,
|
| 101 |
+
const pyc_tc_scalar_t* __restrict__ b,
|
| 102 |
+
float* __restrict__ c,
|
| 103 |
+
int m,
|
| 104 |
+
int n,
|
| 105 |
+
int k) {
|
| 106 |
+
__shared__ pyc_tc_scalar_t shared_a[PYC_TC_CTA_M][PYC_TC_CTA_K];
|
| 107 |
+
__shared__ pyc_tc_scalar_t shared_b[PYC_TC_CTA_N][PYC_TC_CTA_K];
|
| 108 |
+
|
| 109 |
+
const int warp_id = threadIdx.x / 32;
|
| 110 |
+
const int block_row = blockIdx.y * PYC_TC_CTA_M;
|
| 111 |
+
const int block_col = blockIdx.x * PYC_TC_CTA_N;
|
| 112 |
+
const int warp_row = (warp_id / 4) * 16;
|
| 113 |
+
const int warp_col = (warp_id % 4) * 16;
|
| 114 |
+
const int c_row = block_row + warp_row;
|
| 115 |
+
const int c_col = block_col + warp_col;
|
| 116 |
+
|
| 117 |
+
wmma::fragment<wmma::accumulator, 16, 16, 16, float> acc;
|
| 118 |
+
wmma::fill_fragment(acc, 0.0f);
|
| 119 |
+
|
| 120 |
+
if (warp_id >= PYC_TC_WARPS_PER_BLOCK) {
|
| 121 |
+
return;
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
for (int kk = 0; kk < k; kk += PYC_TC_CTA_K) {
|
| 125 |
+
int idx;
|
| 126 |
+
|
| 127 |
+
for (idx = threadIdx.x; idx < PYC_TC_CTA_M * PYC_TC_CTA_K; idx += blockDim.x) {
|
| 128 |
+
const int row = idx / PYC_TC_CTA_K;
|
| 129 |
+
const int col = idx % PYC_TC_CTA_K;
|
| 130 |
+
const int g_row = block_row + row;
|
| 131 |
+
const int g_col = kk + col;
|
| 132 |
+
if (g_row < m && g_col < k) {
|
| 133 |
+
shared_a[row][col] = a[g_row * k + g_col];
|
| 134 |
+
} else {
|
| 135 |
+
shared_a[row][col] = pyc_tc_make_scalar(0.0f);
|
| 136 |
+
}
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
for (idx = threadIdx.x; idx < PYC_TC_CTA_N * PYC_TC_CTA_K; idx += blockDim.x) {
|
| 140 |
+
const int row = idx / PYC_TC_CTA_K;
|
| 141 |
+
const int col = idx % PYC_TC_CTA_K;
|
| 142 |
+
const int g_row = kk + col;
|
| 143 |
+
const int g_col = block_col + row;
|
| 144 |
+
if (g_row < k && g_col < n) {
|
| 145 |
+
shared_b[row][col] = b[g_row * n + g_col];
|
| 146 |
+
} else {
|
| 147 |
+
shared_b[row][col] = pyc_tc_make_scalar(0.0f);
|
| 148 |
+
}
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
__syncthreads();
|
| 152 |
+
|
| 153 |
+
{
|
| 154 |
+
wmma::fragment<wmma::matrix_a, 16, 16, 16, pyc_tc_scalar_t, wmma::row_major> a_frag;
|
| 155 |
+
wmma::fragment<wmma::matrix_b, 16, 16, 16, pyc_tc_scalar_t, wmma::col_major> b_frag;
|
| 156 |
+
wmma::load_matrix_sync(a_frag, &shared_a[warp_row][0], PYC_TC_CTA_K);
|
| 157 |
+
wmma::load_matrix_sync(b_frag, &shared_b[warp_col][0], PYC_TC_CTA_K);
|
| 158 |
+
wmma::mma_sync(acc, a_frag, b_frag, acc);
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
__syncthreads();
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
if (c_row < m && c_col < n) {
|
| 165 |
+
wmma::store_matrix_sync(&c[c_row * n + c_col], acc, n, wmma::mem_row_major);
|
| 166 |
+
}
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
static int set_kernel_attributes(void) {
|
| 170 |
+
cudaError_t status;
|
| 171 |
+
|
| 172 |
+
status = cudaFuncSetAttribute(
|
| 173 |
+
pyc_tc_gemm_kernel,
|
| 174 |
+
cudaFuncAttributePreferredSharedMemoryCarveout,
|
| 175 |
+
100);
|
| 176 |
+
if (status != cudaSuccess && status != cudaErrorNotSupported) {
|
| 177 |
+
fprintf(stderr, "cudaFuncSetAttribute failed: %s\n", cudaGetErrorString(status));
|
| 178 |
+
return -1;
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
return 0;
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
static int parse_config(int argc, char** argv, pyc_tc_config* cfg) {
|
| 185 |
+
if (!cfg) {
|
| 186 |
+
return -1;
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
cfg->m = 1024;
|
| 190 |
+
cfg->n = 1024;
|
| 191 |
+
cfg->k = 1024;
|
| 192 |
+
cfg->warmup = 10;
|
| 193 |
+
cfg->iters = 50;
|
| 194 |
+
|
| 195 |
+
if (argc > 1 && parse_int_arg(argv[1], &cfg->m) != 0) return -1;
|
| 196 |
+
if (argc > 2 && parse_int_arg(argv[2], &cfg->n) != 0) return -1;
|
| 197 |
+
if (argc > 3 && parse_int_arg(argv[3], &cfg->k) != 0) return -1;
|
| 198 |
+
if (argc > 4 && parse_int_arg(argv[4], &cfg->warmup) != 0) return -1;
|
| 199 |
+
if (argc > 5 && parse_int_arg(argv[5], &cfg->iters) != 0) return -1;
|
| 200 |
+
|
| 201 |
+
return 0;
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
+
int main(int argc, char** argv) {
|
| 205 |
+
pyc_tc_config cfg;
|
| 206 |
+
cudaDeviceProp props;
|
| 207 |
+
pyc_tc_scalar_t* host_a = NULL;
|
| 208 |
+
pyc_tc_scalar_t* host_b = NULL;
|
| 209 |
+
float* host_c = NULL;
|
| 210 |
+
float* ref_c = NULL;
|
| 211 |
+
pyc_tc_scalar_t* dev_a = NULL;
|
| 212 |
+
pyc_tc_scalar_t* dev_b = NULL;
|
| 213 |
+
float* dev_c = NULL;
|
| 214 |
+
cudaEvent_t start = NULL;
|
| 215 |
+
cudaEvent_t stop = NULL;
|
| 216 |
+
size_t a_bytes;
|
| 217 |
+
size_t b_bytes;
|
| 218 |
+
size_t c_bytes;
|
| 219 |
+
dim3 block;
|
| 220 |
+
dim3 grid;
|
| 221 |
+
float elapsed_ms = 0.0f;
|
| 222 |
+
double best_ms = 0.0;
|
| 223 |
+
int iter;
|
| 224 |
+
double max_abs_diff = 0.0;
|
| 225 |
+
|
| 226 |
+
if (parse_config(argc, argv, &cfg) != 0) {
|
| 227 |
+
fprintf(stderr, "usage: %s [m] [n] [k] [warmup] [iters]\n", argv[0]);
|
| 228 |
+
return 2;
|
| 229 |
+
}
|
| 230 |
+
|
| 231 |
+
if ((cfg.m % PYC_TC_CTA_M) != 0 || (cfg.n % PYC_TC_CTA_N) != 0 || (cfg.k % PYC_TC_CTA_K) != 0) {
|
| 232 |
+
fprintf(stderr, "Tensor Core lane requires %dx%dx%d-aligned shapes\n", PYC_TC_CTA_M, PYC_TC_CTA_N, PYC_TC_CTA_K);
|
| 233 |
+
return 2;
|
| 234 |
+
}
|
| 235 |
+
|
| 236 |
+
if (check_cuda(cudaGetDeviceProperties(&props, 0), "cudaGetDeviceProperties") != 0) {
|
| 237 |
+
return 1;
|
| 238 |
+
}
|
| 239 |
+
if (props.major < 8 || (props.major == 8 && props.minor < 9)) {
|
| 240 |
+
fprintf(stderr, "Ada Tensor Core prototype requires sm_89-class hardware\n");
|
| 241 |
+
return 1;
|
| 242 |
+
}
|
| 243 |
+
|
| 244 |
+
a_bytes = (size_t)cfg.m * (size_t)cfg.k * sizeof(pyc_tc_scalar_t);
|
| 245 |
+
b_bytes = (size_t)cfg.k * (size_t)cfg.n * sizeof(pyc_tc_scalar_t);
|
| 246 |
+
c_bytes = (size_t)cfg.m * (size_t)cfg.n * sizeof(float);
|
| 247 |
+
|
| 248 |
+
host_a = (pyc_tc_scalar_t*)malloc(a_bytes);
|
| 249 |
+
host_b = (pyc_tc_scalar_t*)malloc(b_bytes);
|
| 250 |
+
host_c = (float*)malloc(c_bytes);
|
| 251 |
+
ref_c = (float*)malloc(c_bytes);
|
| 252 |
+
if (!host_a || !host_b || !host_c || !ref_c) {
|
| 253 |
+
fprintf(stderr, "host allocation failed\n");
|
| 254 |
+
return 1;
|
| 255 |
+
}
|
| 256 |
+
|
| 257 |
+
fill_matrix(host_a, cfg.m, cfg.k, 0.03125f);
|
| 258 |
+
fill_matrix(host_b, cfg.k, cfg.n, 0.0625f);
|
| 259 |
+
reference_gemm(host_a, host_b, ref_c, cfg.m, cfg.n, cfg.k);
|
| 260 |
+
|
| 261 |
+
if (check_cuda(cudaMalloc((void**)&dev_a, a_bytes), "cudaMalloc(a)") != 0) return 1;
|
| 262 |
+
if (check_cuda(cudaMalloc((void**)&dev_b, b_bytes), "cudaMalloc(b)") != 0) return 1;
|
| 263 |
+
if (check_cuda(cudaMalloc((void**)&dev_c, c_bytes), "cudaMalloc(c)") != 0) return 1;
|
| 264 |
+
|
| 265 |
+
if (check_cuda(cudaMemcpy(dev_a, host_a, a_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(a)") != 0) return 1;
|
| 266 |
+
if (check_cuda(cudaMemcpy(dev_b, host_b, b_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(b)") != 0) return 1;
|
| 267 |
+
|
| 268 |
+
if (set_kernel_attributes() != 0) return 1;
|
| 269 |
+
|
| 270 |
+
block = dim3(PYC_TC_THREADS_PER_BLOCK, 1, 1);
|
| 271 |
+
grid = dim3(
|
| 272 |
+
(unsigned int)((cfg.n + PYC_TC_CTA_N - 1) / PYC_TC_CTA_N),
|
| 273 |
+
(unsigned int)((cfg.m + PYC_TC_CTA_M - 1) / PYC_TC_CTA_M),
|
| 274 |
+
1);
|
| 275 |
+
|
| 276 |
+
if (check_cuda(cudaEventCreate(&start), "cudaEventCreate(start)") != 0) return 1;
|
| 277 |
+
if (check_cuda(cudaEventCreate(&stop), "cudaEventCreate(stop)") != 0) return 1;
|
| 278 |
+
|
| 279 |
+
for (iter = 0; iter < cfg.warmup; ++iter) {
|
| 280 |
+
pyc_tc_gemm_kernel<<<grid, block>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 281 |
+
}
|
| 282 |
+
if (check_cuda(cudaGetLastError(), "kernel launch warmup") != 0) return 1;
|
| 283 |
+
if (check_cuda(cudaDeviceSynchronize(), "cudaDeviceSynchronize warmup") != 0) return 1;
|
| 284 |
+
|
| 285 |
+
best_ms = 0.0;
|
| 286 |
+
for (iter = 0; iter < cfg.iters; ++iter) {
|
| 287 |
+
if (check_cuda(cudaEventRecord(start), "cudaEventRecord(start)") != 0) return 1;
|
| 288 |
+
pyc_tc_gemm_kernel<<<grid, block>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 289 |
+
if (check_cuda(cudaEventRecord(stop), "cudaEventRecord(stop)") != 0) return 1;
|
| 290 |
+
if (check_cuda(cudaEventSynchronize(stop), "cudaEventSynchronize(stop)") != 0) return 1;
|
| 291 |
+
if (check_cuda(cudaEventElapsedTime(&elapsed_ms, start, stop), "cudaEventElapsedTime") != 0) return 1;
|
| 292 |
+
if (iter == 0 || elapsed_ms < (float)best_ms) {
|
| 293 |
+
best_ms = elapsed_ms;
|
| 294 |
+
}
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
if (check_cuda(cudaMemcpy(host_c, dev_c, c_bytes, cudaMemcpyDeviceToHost), "cudaMemcpy(c)") != 0) return 1;
|
| 298 |
+
|
| 299 |
+
for (iter = 0; iter < cfg.m * cfg.n; ++iter) {
|
| 300 |
+
double diff = fabs((double)host_c[iter] - (double)ref_c[iter]);
|
| 301 |
+
if (diff > max_abs_diff) {
|
| 302 |
+
max_abs_diff = diff;
|
| 303 |
+
}
|
| 304 |
+
}
|
| 305 |
+
|
| 306 |
+
printf("lane=%s\n", PYC_TC_LANE_NAME);
|
| 307 |
+
printf("shape=%dx%dx%d\n", cfg.m, cfg.n, cfg.k);
|
| 308 |
+
printf("tile=%dx%dx%d threads=%d\n", PYC_TC_CTA_M, PYC_TC_CTA_N, PYC_TC_CTA_K, PYC_TC_THREADS_PER_BLOCK);
|
| 309 |
+
printf("best_ms=%.3f\n", best_ms);
|
| 310 |
+
printf("max_abs_diff=%.6f\n", max_abs_diff);
|
| 311 |
+
if (best_ms > 0.0) {
|
| 312 |
+
double flops = 2.0 * (double)cfg.m * (double)cfg.n * (double)cfg.k;
|
| 313 |
+
double gflops = flops / (best_ms * 1.0e6);
|
| 314 |
+
printf("gflops=%.3f\n", gflops);
|
| 315 |
+
}
|
| 316 |
+
|
| 317 |
+
cudaEventDestroy(start);
|
| 318 |
+
cudaEventDestroy(stop);
|
| 319 |
+
cudaFree(dev_a);
|
| 320 |
+
cudaFree(dev_b);
|
| 321 |
+
cudaFree(dev_c);
|
| 322 |
+
free(host_a);
|
| 323 |
+
free(host_b);
|
| 324 |
+
free(host_c);
|
| 325 |
+
free(ref_c);
|
| 326 |
+
return max_abs_diff <= 0.2 ? 0 : 1;
|
| 327 |
+
}
|
kernels/prototypes/baseline/matmul/kernel.cu
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// /kernel/matrix_mult.cu
|
| 2 |
+
#include <cuda_runtime.h>
|
| 3 |
+
|
| 4 |
+
__global__ void matrix_mult_kernel(float* a, float* b, float* c, int m, int n, int k) {
|
| 5 |
+
int row = blockIdx.y * blockDim.y + threadIdx.y;
|
| 6 |
+
int col = blockIdx.x * blockDim.x + threadIdx.x;
|
| 7 |
+
if (row < m && col < n) {
|
| 8 |
+
float sum = 0.0f;
|
| 9 |
+
for (int i = 0; i < k; i++) {
|
| 10 |
+
sum += a[row * k + i] * b[i * n + col];
|
| 11 |
+
}
|
| 12 |
+
c[row * n + col] = sum;
|
| 13 |
+
}
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
void matrix_multiply(float* a, float* b, float* c, int m, int n, int k) {
|
| 17 |
+
float *d_a, *d_b, *d_c;
|
| 18 |
+
cudaMalloc(&d_a, m * k * sizeof(float));
|
| 19 |
+
cudaMalloc(&d_b, k * n * sizeof(float));
|
| 20 |
+
cudaMalloc(&d_c, m * n * sizeof(float));
|
| 21 |
+
cudaMemcpy(d_a, a, m * k * sizeof(float), cudaMemcpyHostToDevice);
|
| 22 |
+
cudaMemcpy(d_b, b, k * n * sizeof(float), cudaMemcpyHostToDevice);
|
| 23 |
+
|
| 24 |
+
dim3 threads(16, 16);
|
| 25 |
+
dim3 blocks((n + threads.x - 1) / threads.x, (m + threads.y - 1) / threads.y);
|
| 26 |
+
matrix_mult_kernel<<<blocks, threads>>>(d_a, d_b, d_c, m, n, k);
|
| 27 |
+
|
| 28 |
+
cudaMemcpy(c, d_c, m * n * sizeof(float), cudaMemcpyDeviceToHost);
|
| 29 |
+
cudaFree(d_a); cudaFree(d_b); cudaFree(d_c);
|
| 30 |
+
}
|
kernels/prototypes/experimental/tokenizer_matmul/kernel.cu
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <cuda_runtime.h>
|
| 2 |
+
#include <stdio.h>
|
| 3 |
+
#include <ctype.h>
|
| 4 |
+
|
| 5 |
+
#define MAX_TOKENS 1024
|
| 6 |
+
|
| 7 |
+
// Enhanced token types
|
| 8 |
+
typedef enum {
|
| 9 |
+
TOKEN_IDENTIFIER = 0,
|
| 10 |
+
TOKEN_NUMBER = 1,
|
| 11 |
+
TOKEN_OPERATOR = 2,
|
| 12 |
+
TOKEN_KEYWORD = 3,
|
| 13 |
+
TOKEN_STRING = 4,
|
| 14 |
+
TOKEN_COMMENT = 5,
|
| 15 |
+
TOKEN_PREPROCESSOR = 6,
|
| 16 |
+
TOKEN_PUNCTUATION = 7
|
| 17 |
+
} TokenType;
|
| 18 |
+
|
| 19 |
+
// Add token metadata
|
| 20 |
+
typedef struct {
|
| 21 |
+
TokenType type;
|
| 22 |
+
int start_pos;
|
| 23 |
+
int end_pos;
|
| 24 |
+
int length;
|
| 25 |
+
int line;
|
| 26 |
+
int column;
|
| 27 |
+
char lexeme[256];
|
| 28 |
+
unsigned int hash;
|
| 29 |
+
} EnhancedTokenGPU;
|
| 30 |
+
|
| 31 |
+
typedef struct {
|
| 32 |
+
int type; // 0: identifier, 1: number, 2: operator
|
| 33 |
+
int start_pos;
|
| 34 |
+
int end_pos;
|
| 35 |
+
int length;
|
| 36 |
+
} TokenGPU;
|
| 37 |
+
|
| 38 |
+
// Add shared memory optimization
|
| 39 |
+
__shared__ char shared_input[1024];
|
| 40 |
+
__shared__ int shared_token_count;
|
| 41 |
+
|
| 42 |
+
// Enhanced tokenization kernel with better pattern matching
|
| 43 |
+
__global__ void enhanced_tokenize_kernel(const char* input, size_t input_length,
|
| 44 |
+
EnhancedTokenGPU* tokens, int* token_count,
|
| 45 |
+
bool enable_comments, bool enable_preprocessing) {
|
| 46 |
+
int idx = blockIdx.x * blockDim.x + threadIdx.x;
|
| 47 |
+
if (idx >= input_length) return;
|
| 48 |
+
|
| 49 |
+
// Load chunk into shared memory
|
| 50 |
+
int local_idx = threadIdx.x;
|
| 51 |
+
if (local_idx < 1024 && idx < input_length) {
|
| 52 |
+
shared_input[local_idx] = input[idx];
|
| 53 |
+
}
|
| 54 |
+
__syncthreads();
|
| 55 |
+
|
| 56 |
+
// Enhanced token detection with more patterns
|
| 57 |
+
if (idx > 0 && (isalnum(shared_input[local_idx-1]) && isalnum(shared_input[local_idx]))) return;
|
| 58 |
+
|
| 59 |
+
int tcount = atomicAdd(token_count, 0);
|
| 60 |
+
if (tcount >= MAX_TOKENS) return;
|
| 61 |
+
|
| 62 |
+
EnhancedTokenGPU token;
|
| 63 |
+
token.start_pos = idx;
|
| 64 |
+
token.hash = 0;
|
| 65 |
+
|
| 66 |
+
// Calculate line and column
|
| 67 |
+
int line = 1, column = 1;
|
| 68 |
+
for (int i = 0; i < idx; i++) {
|
| 69 |
+
if (input[i] == '\n') {
|
| 70 |
+
line++;
|
| 71 |
+
column = 1;
|
| 72 |
+
} else {
|
| 73 |
+
column++;
|
| 74 |
+
}
|
| 75 |
+
}
|
| 76 |
+
token.line = line;
|
| 77 |
+
token.column = column;
|
| 78 |
+
|
| 79 |
+
// Enhanced pattern matching
|
| 80 |
+
if (isalpha(shared_input[local_idx]) || shared_input[local_idx] == '_') {
|
| 81 |
+
// Handle identifiers and keywords
|
| 82 |
+
int end = local_idx;
|
| 83 |
+
while (end < 1024 && (isalnum(shared_input[end]) || shared_input[end] == '_')) {
|
| 84 |
+
token.hash = token.hash * 31 + shared_input[end];
|
| 85 |
+
end++;
|
| 86 |
+
}
|
| 87 |
+
token.type = TOKEN_IDENTIFIER;
|
| 88 |
+
token.end_pos = idx + (end - local_idx) - 1;
|
| 89 |
+
token.length = end - local_idx;
|
| 90 |
+
}
|
| 91 |
+
// ... Add more token pattern matching ...
|
| 92 |
+
|
| 93 |
+
// Store token if valid
|
| 94 |
+
if (token.length > 0) {
|
| 95 |
+
int new_count = atomicAdd(token_count, 1);
|
| 96 |
+
if (new_count < MAX_TOKENS) {
|
| 97 |
+
tokens[new_count] = token;
|
| 98 |
+
}
|
| 99 |
+
}
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
__global__ void tokenize_kernel(const char* input, size_t input_length, TokenGPU* tokens, int* token_count) {
|
| 103 |
+
int idx = blockIdx.x * blockDim.x + threadIdx.x;
|
| 104 |
+
if (idx >= input_length) return;
|
| 105 |
+
|
| 106 |
+
// Skip if not at token boundary
|
| 107 |
+
if (idx > 0 && (isalnum(input[idx-1]) && isalnum(input[idx]))) return;
|
| 108 |
+
|
| 109 |
+
int tcount = *token_count;
|
| 110 |
+
if (tcount >= MAX_TOKENS) return;
|
| 111 |
+
|
| 112 |
+
if (isalpha(input[idx])) {
|
| 113 |
+
int end = idx;
|
| 114 |
+
while (end < input_length && isalnum(input[end])) end++;
|
| 115 |
+
int new_count = atomicAdd(token_count, 1);
|
| 116 |
+
if (new_count < MAX_TOKENS) {
|
| 117 |
+
tokens[new_count].type = 0;
|
| 118 |
+
tokens[new_count].start_pos = idx;
|
| 119 |
+
tokens[new_count].end_pos = end - 1;
|
| 120 |
+
tokens[new_count].length = end - idx;
|
| 121 |
+
}
|
| 122 |
+
} else if (isdigit(input[idx])) {
|
| 123 |
+
int end = idx;
|
| 124 |
+
while (end < input_length && isdigit(input[end])) end++;
|
| 125 |
+
int new_count = atomicAdd(token_count, 1);
|
| 126 |
+
if (new_count < MAX_TOKENS) {
|
| 127 |
+
tokens[new_count].type = 1;
|
| 128 |
+
tokens[new_count].start_pos = idx;
|
| 129 |
+
tokens[new_count].end_pos = end - 1;
|
| 130 |
+
tokens[new_count].length = end - idx;
|
| 131 |
+
}
|
| 132 |
+
} else if (input[idx] == '+' || input[idx] == '-' || input[idx] == '*' || input[idx] == '/') {
|
| 133 |
+
int new_count = atomicAdd(token_count, 1);
|
| 134 |
+
if (new_count < MAX_TOKENS) {
|
| 135 |
+
tokens[new_count].type = 2;
|
| 136 |
+
tokens[new_count].start_pos = idx;
|
| 137 |
+
tokens[new_count].end_pos = idx;
|
| 138 |
+
tokens[new_count].length = 1;
|
| 139 |
+
}
|
| 140 |
+
}
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
__global__ void matrix_mult_kernel(float* a, float* b, float* c, int m, int n, int k) {
|
| 144 |
+
int row = blockIdx.y * blockDim.y + threadIdx.y;
|
| 145 |
+
int col = blockIdx.x * blockDim.x + threadIdx.x;
|
| 146 |
+
if (row < m && col < n) {
|
| 147 |
+
float sum = 0.0f;
|
| 148 |
+
for (int i = 0; i < k; i++) {
|
| 149 |
+
sum += a[row * k + i] * b[i * n + col];
|
| 150 |
+
}
|
| 151 |
+
c[row * n + col] = sum;
|
| 152 |
+
}
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
// Add parallel matrix operations
|
| 156 |
+
__global__ void enhanced_matrix_mult_kernel(float* a, float* b, float* c,
|
| 157 |
+
int m, int n, int k,
|
| 158 |
+
bool use_shared_memory) {
|
| 159 |
+
// ... existing matrix multiplication code ...
|
| 160 |
+
|
| 161 |
+
// Add shared memory optimization
|
| 162 |
+
__shared__ float shared_a[16][16];
|
| 163 |
+
__shared__ float shared_b[16][16];
|
| 164 |
+
|
| 165 |
+
// ... implement block matrix multiplication ...
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
// Add new CUDA utilities
|
| 169 |
+
void initialize_cuda_context(void) {
|
| 170 |
+
cudaFree(0); // Force context initialization
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
void optimize_kernel_launch(dim3* blocks, dim3* threads, size_t shared_memory_size) {
|
| 174 |
+
int device;
|
| 175 |
+
cudaGetDevice(&device);
|
| 176 |
+
cudaDeviceProp props;
|
| 177 |
+
cudaGetDeviceProperties(&props, device);
|
| 178 |
+
|
| 179 |
+
// Optimize launch configuration based on device properties
|
| 180 |
+
// ... implementation ...
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
void cuda_tokenize(const char* input, TokenGPU* tokens, int* token_count) {
|
| 184 |
+
size_t input_length = strlen(input);
|
| 185 |
+
char* d_input;
|
| 186 |
+
TokenGPU* d_tokens;
|
| 187 |
+
int* d_token_count;
|
| 188 |
+
|
| 189 |
+
cudaMalloc(&d_input, input_length + 1);
|
| 190 |
+
cudaMalloc(&d_tokens, MAX_TOKENS * sizeof(TokenGPU));
|
| 191 |
+
cudaMalloc(&d_token_count, sizeof(int));
|
| 192 |
+
cudaMemcpy(d_input, input, input_length + 1, cudaMemcpyHostToDevice);
|
| 193 |
+
cudaMemset(d_token_count, 0, sizeof(int));
|
| 194 |
+
|
| 195 |
+
int threads = 256;
|
| 196 |
+
int blocks = (input_length + threads - 1) / threads;
|
| 197 |
+
tokenize_kernel<<<blocks, threads>>>(d_input, input_length, d_tokens, d_token_count);
|
| 198 |
+
|
| 199 |
+
cudaMemcpy(token_count, d_token_count, sizeof(int), cudaMemcpyDeviceToHost);
|
| 200 |
+
cudaMemcpy(tokens, d_tokens, *token_count * sizeof(TokenGPU), cudaMemcpyDeviceToHost);
|
| 201 |
+
|
| 202 |
+
cudaFree(d_input);
|
| 203 |
+
cudaFree(d_tokens);
|
| 204 |
+
cudaFree(d_token_count);
|
| 205 |
+
}
|
| 206 |
+
|
| 207 |
+
void cuda_matrix_mult(float* a, float* b, float* c, int m, int n, int k) {
|
| 208 |
+
float *d_a, *d_b, *d_c;
|
| 209 |
+
cudaMalloc(&d_a, m * k * sizeof(float));
|
| 210 |
+
cudaMalloc(&d_b, k * n * sizeof(float));
|
| 211 |
+
cudaMalloc(&d_c, m * n * sizeof(float));
|
| 212 |
+
cudaMemcpy(d_a, a, m * k * sizeof(float), cudaMemcpyHostToDevice);
|
| 213 |
+
cudaMemcpy(d_b, b, k * n * sizeof(float), cudaMemcpyHostToDevice);
|
| 214 |
+
|
| 215 |
+
dim3 threads(16, 16);
|
| 216 |
+
dim3 blocks((n + threads.x - 1) / threads.x, (m + threads.y - 1) / threads.y);
|
| 217 |
+
matrix_mult_kernel<<<blocks, threads>>>(d_a, d_b, d_c, m, n, k);
|
| 218 |
+
|
| 219 |
+
cudaMemcpy(c, d_c, m * n * sizeof(float), cudaMemcpyDeviceToHost);
|
| 220 |
+
cudaFree(d_a); cudaFree(d_b); cudaFree(d_c);
|
| 221 |
+
}
|
| 222 |
+
void print_tokens(TokenGPU* tokens, int token_count) {
|
| 223 |
+
for (int i = 0; i < token_count; i++) {
|
| 224 |
+
printf("Token %d: Type %d, Start %d, End %d, Length %d\n",
|
| 225 |
+
i, tokens[i].type, tokens[i].start_pos, tokens[i].end_pos, tokens[i].length);
|
| 226 |
+
}
|
| 227 |
+
}
|
| 228 |
+
int main() {
|
| 229 |
+
const char* input = "int a = 5 + 3;";
|
| 230 |
+
TokenGPU tokens[MAX_TOKENS];
|
| 231 |
+
int token_count;
|
| 232 |
+
|
| 233 |
+
cuda_tokenize(input, tokens, &token_count);
|
| 234 |
+
print_tokens(tokens, token_count);
|
| 235 |
+
|
| 236 |
+
float a[6] = {1, 2, 3, 4, 5, 6};
|
| 237 |
+
float b[6] = {7, 8, 9, 10, 11, 12};
|
| 238 |
+
float c[4] = {0};
|
| 239 |
+
|
| 240 |
+
cuda_matrix_mult(a, b, c, 2, 3, 2);
|
| 241 |
+
for (int i = 0; i < 4; i++) {
|
| 242 |
+
printf("%f ", c[i]);
|
| 243 |
+
}
|
| 244 |
+
printf("\n");
|
| 245 |
+
|
| 246 |
+
return 0;
|
| 247 |
+
}
|
| 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|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <cublasLt.h>
|
| 2 |
+
#include <cuda_bf16.h>
|
| 3 |
+
#include <cuda_runtime.h>
|
| 4 |
+
|
| 5 |
+
#include <math.h>
|
| 6 |
+
#include <stdint.h>
|
| 7 |
+
#include <stdio.h>
|
| 8 |
+
#include <stdlib.h>
|
| 9 |
+
#include <string.h>
|
| 10 |
+
|
| 11 |
+
typedef struct {
|
| 12 |
+
int m;
|
| 13 |
+
int n;
|
| 14 |
+
int k;
|
| 15 |
+
int warmup;
|
| 16 |
+
int iters;
|
| 17 |
+
int skip_reference;
|
| 18 |
+
} pyc_hopper_cublaslt_bf16_config;
|
| 19 |
+
|
| 20 |
+
static int parse_int_arg(const char* text, int* out_value) {
|
| 21 |
+
char* end = NULL;
|
| 22 |
+
long parsed;
|
| 23 |
+
if (!text || !out_value) {
|
| 24 |
+
return -1;
|
| 25 |
+
}
|
| 26 |
+
parsed = strtol(text, &end, 10);
|
| 27 |
+
if (end == text || *end != '\0' || parsed < 0 || parsed > INT32_MAX) {
|
| 28 |
+
return -1;
|
| 29 |
+
}
|
| 30 |
+
*out_value = (int)parsed;
|
| 31 |
+
return 0;
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
static int env_flag(const char* name, int default_value) {
|
| 35 |
+
const char* raw = getenv(name);
|
| 36 |
+
if (!raw || raw[0] == '\0') {
|
| 37 |
+
return default_value;
|
| 38 |
+
}
|
| 39 |
+
if (
|
| 40 |
+
strcmp(raw, "1") == 0 || strcmp(raw, "true") == 0 || strcmp(raw, "TRUE") == 0
|
| 41 |
+
|| strcmp(raw, "yes") == 0 || strcmp(raw, "on") == 0) {
|
| 42 |
+
return 1;
|
| 43 |
+
}
|
| 44 |
+
if (
|
| 45 |
+
strcmp(raw, "0") == 0 || strcmp(raw, "false") == 0 || strcmp(raw, "FALSE") == 0
|
| 46 |
+
|| strcmp(raw, "no") == 0 || strcmp(raw, "off") == 0) {
|
| 47 |
+
return 0;
|
| 48 |
+
}
|
| 49 |
+
return default_value;
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
static int check_cuda(cudaError_t status, const char* what) {
|
| 53 |
+
if (status != cudaSuccess) {
|
| 54 |
+
fprintf(stderr, "%s failed: %s\n", what, cudaGetErrorString(status));
|
| 55 |
+
return -1;
|
| 56 |
+
}
|
| 57 |
+
return 0;
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
static int check_cublas(cublasStatus_t status, const char* what) {
|
| 61 |
+
if (status != CUBLAS_STATUS_SUCCESS) {
|
| 62 |
+
fprintf(stderr, "%s failed: cublas status %d\n", what, (int)status);
|
| 63 |
+
return -1;
|
| 64 |
+
}
|
| 65 |
+
return 0;
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
static void fill_matrix(__nv_bfloat16* data, int rows, int cols, float scale) {
|
| 69 |
+
int i;
|
| 70 |
+
for (i = 0; i < rows * cols; ++i) {
|
| 71 |
+
int pattern = (i * 23 + rows * 13 + cols * 5) % 31;
|
| 72 |
+
data[i] = __float2bfloat16(((float)pattern - 15.0f) * scale);
|
| 73 |
+
}
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
static void reference_gemm(
|
| 77 |
+
const __nv_bfloat16* a,
|
| 78 |
+
const __nv_bfloat16* b,
|
| 79 |
+
__nv_bfloat16* d,
|
| 80 |
+
int m,
|
| 81 |
+
int n,
|
| 82 |
+
int k) {
|
| 83 |
+
int row;
|
| 84 |
+
for (row = 0; row < m; ++row) {
|
| 85 |
+
int col;
|
| 86 |
+
for (col = 0; col < n; ++col) {
|
| 87 |
+
float acc = 0.0f;
|
| 88 |
+
int kk;
|
| 89 |
+
for (kk = 0; kk < k; ++kk) {
|
| 90 |
+
acc += __bfloat162float(a[row * k + kk]) * __bfloat162float(b[kk * n + col]);
|
| 91 |
+
}
|
| 92 |
+
d[row * n + col] = __float2bfloat16(acc);
|
| 93 |
+
}
|
| 94 |
+
}
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
static int parse_config(int argc, char** argv, pyc_hopper_cublaslt_bf16_config* cfg) {
|
| 98 |
+
if (!cfg) {
|
| 99 |
+
return -1;
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
cfg->m = 4096;
|
| 103 |
+
cfg->n = 4096;
|
| 104 |
+
cfg->k = 4096;
|
| 105 |
+
cfg->warmup = 3;
|
| 106 |
+
cfg->iters = 30;
|
| 107 |
+
cfg->skip_reference = env_flag("PYC_HOPPER_CUBLASLT_SKIP_REFERENCE", 1);
|
| 108 |
+
|
| 109 |
+
if (argc > 1 && parse_int_arg(argv[1], &cfg->m) != 0) return -1;
|
| 110 |
+
if (argc > 2 && parse_int_arg(argv[2], &cfg->n) != 0) return -1;
|
| 111 |
+
if (argc > 3 && parse_int_arg(argv[3], &cfg->k) != 0) return -1;
|
| 112 |
+
if (argc > 4 && parse_int_arg(argv[4], &cfg->warmup) != 0) return -1;
|
| 113 |
+
if (argc > 5 && parse_int_arg(argv[5], &cfg->iters) != 0) return -1;
|
| 114 |
+
if (argc > 6 && parse_int_arg(argv[6], &cfg->skip_reference) != 0) return -1;
|
| 115 |
+
|
| 116 |
+
return 0;
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
int main(int argc, char** argv) {
|
| 120 |
+
pyc_hopper_cublaslt_bf16_config cfg;
|
| 121 |
+
struct cudaDeviceProp props;
|
| 122 |
+
cublasLtHandle_t lt_handle = NULL;
|
| 123 |
+
cublasLtMatmulDesc_t op_desc = NULL;
|
| 124 |
+
cublasLtMatrixLayout_t a_layout = NULL;
|
| 125 |
+
cublasLtMatrixLayout_t b_layout = NULL;
|
| 126 |
+
cublasLtMatrixLayout_t c_layout = NULL;
|
| 127 |
+
cublasLtMatmulPreference_t pref = NULL;
|
| 128 |
+
cublasLtMatmulHeuristicResult_t heuristic;
|
| 129 |
+
int returned_results = 0;
|
| 130 |
+
cudaEvent_t start = NULL;
|
| 131 |
+
cudaEvent_t stop = NULL;
|
| 132 |
+
cudaStream_t stream = NULL;
|
| 133 |
+
__nv_bfloat16* host_a = NULL;
|
| 134 |
+
__nv_bfloat16* host_b = NULL;
|
| 135 |
+
__nv_bfloat16* host_d = NULL;
|
| 136 |
+
__nv_bfloat16* ref_d = NULL;
|
| 137 |
+
__nv_bfloat16* dev_a = NULL;
|
| 138 |
+
__nv_bfloat16* dev_b = NULL;
|
| 139 |
+
__nv_bfloat16* dev_d = NULL;
|
| 140 |
+
void* workspace = NULL;
|
| 141 |
+
size_t workspace_bytes = 64u * 1024u * 1024u;
|
| 142 |
+
size_t a_bytes;
|
| 143 |
+
size_t b_bytes;
|
| 144 |
+
size_t d_bytes;
|
| 145 |
+
float alpha = 1.0f;
|
| 146 |
+
float beta = 0.0f;
|
| 147 |
+
float elapsed_ms = 0.0f;
|
| 148 |
+
double best_ms = 0.0;
|
| 149 |
+
double max_abs_diff = 0.0;
|
| 150 |
+
int iter;
|
| 151 |
+
|
| 152 |
+
if (parse_config(argc, argv, &cfg) != 0) {
|
| 153 |
+
fprintf(stderr, "usage: %s [m] [n] [k] [warmup] [iters] [skip_reference]\n", argv[0]);
|
| 154 |
+
return 2;
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
if (check_cuda(cudaGetDeviceProperties(&props, 0), "cudaGetDeviceProperties") != 0) {
|
| 158 |
+
return 1;
|
| 159 |
+
}
|
| 160 |
+
if (props.major < 9) {
|
| 161 |
+
fprintf(stderr, "Hopper cuBLASLt BF16 prototype requires sm_90-class hardware\n");
|
| 162 |
+
return 1;
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
a_bytes = (size_t)cfg.m * (size_t)cfg.k * sizeof(__nv_bfloat16);
|
| 166 |
+
b_bytes = (size_t)cfg.k * (size_t)cfg.n * sizeof(__nv_bfloat16);
|
| 167 |
+
d_bytes = (size_t)cfg.m * (size_t)cfg.n * sizeof(__nv_bfloat16);
|
| 168 |
+
|
| 169 |
+
host_a = (__nv_bfloat16*)malloc(a_bytes);
|
| 170 |
+
host_b = (__nv_bfloat16*)malloc(b_bytes);
|
| 171 |
+
if (!host_a || !host_b) {
|
| 172 |
+
fprintf(stderr, "host allocation failed\n");
|
| 173 |
+
return 1;
|
| 174 |
+
}
|
| 175 |
+
if (!cfg.skip_reference) {
|
| 176 |
+
host_d = (__nv_bfloat16*)malloc(d_bytes);
|
| 177 |
+
ref_d = (__nv_bfloat16*)malloc(d_bytes);
|
| 178 |
+
if (!host_d || !ref_d) {
|
| 179 |
+
fprintf(stderr, "host validation allocation failed\n");
|
| 180 |
+
return 1;
|
| 181 |
+
}
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
fill_matrix(host_a, cfg.m, cfg.k, 0.03125f);
|
| 185 |
+
fill_matrix(host_b, cfg.k, cfg.n, 0.0625f);
|
| 186 |
+
if (!cfg.skip_reference) {
|
| 187 |
+
reference_gemm(host_a, host_b, ref_d, cfg.m, cfg.n, cfg.k);
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
if (check_cuda(cudaMalloc((void**)&dev_a, a_bytes), "cudaMalloc(a)") != 0) return 1;
|
| 191 |
+
if (check_cuda(cudaMalloc((void**)&dev_b, b_bytes), "cudaMalloc(b)") != 0) return 1;
|
| 192 |
+
if (check_cuda(cudaMalloc((void**)&dev_d, d_bytes), "cudaMalloc(d)") != 0) return 1;
|
| 193 |
+
if (check_cuda(cudaMalloc(&workspace, workspace_bytes), "cudaMalloc(workspace)") != 0) return 1;
|
| 194 |
+
|
| 195 |
+
if (check_cuda(cudaMemcpy(dev_a, host_a, a_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(a)") != 0) return 1;
|
| 196 |
+
if (check_cuda(cudaMemcpy(dev_b, host_b, b_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(b)") != 0) return 1;
|
| 197 |
+
if (check_cuda(cudaMemset(dev_d, 0, d_bytes), "cudaMemset(d)") != 0) return 1;
|
| 198 |
+
|
| 199 |
+
if (check_cuda(cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking), "cudaStreamCreateWithFlags") != 0) return 1;
|
| 200 |
+
if (check_cublas(cublasLtCreate(<_handle), "cublasLtCreate") != 0) return 1;
|
| 201 |
+
|
| 202 |
+
if (check_cublas(cublasLtMatmulDescCreate(&op_desc, CUBLAS_COMPUTE_32F, CUDA_R_32F), "cublasLtMatmulDescCreate") != 0) return 1;
|
| 203 |
+
{
|
| 204 |
+
cublasOperation_t transa = CUBLAS_OP_N;
|
| 205 |
+
cublasOperation_t transb = CUBLAS_OP_N;
|
| 206 |
+
if (check_cublas(cublasLtMatmulDescSetAttribute(op_desc, CUBLASLT_MATMUL_DESC_TRANSA, &transa, sizeof(transa)), "cublasLtMatmulDescSetAttribute(TRANSA)") != 0) return 1;
|
| 207 |
+
if (check_cublas(cublasLtMatmulDescSetAttribute(op_desc, CUBLASLT_MATMUL_DESC_TRANSB, &transb, sizeof(transb)), "cublasLtMatmulDescSetAttribute(TRANSB)") != 0) return 1;
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
if (check_cublas(cublasLtMatrixLayoutCreate(&a_layout, CUDA_R_16BF, cfg.m, cfg.k, cfg.k), "cublasLtMatrixLayoutCreate(A)") != 0) return 1;
|
| 211 |
+
if (check_cublas(cublasLtMatrixLayoutCreate(&b_layout, CUDA_R_16BF, cfg.k, cfg.n, cfg.n), "cublasLtMatrixLayoutCreate(B)") != 0) return 1;
|
| 212 |
+
if (check_cublas(cublasLtMatrixLayoutCreate(&c_layout, CUDA_R_16BF, cfg.m, cfg.n, cfg.n), "cublasLtMatrixLayoutCreate(D)") != 0) return 1;
|
| 213 |
+
{
|
| 214 |
+
cublasLtOrder_t order = CUBLASLT_ORDER_ROW;
|
| 215 |
+
if (check_cublas(cublasLtMatrixLayoutSetAttribute(a_layout, CUBLASLT_MATRIX_LAYOUT_ORDER, &order, sizeof(order)), "cublasLtMatrixLayoutSetAttribute(A order)") != 0) return 1;
|
| 216 |
+
if (check_cublas(cublasLtMatrixLayoutSetAttribute(b_layout, CUBLASLT_MATRIX_LAYOUT_ORDER, &order, sizeof(order)), "cublasLtMatrixLayoutSetAttribute(B order)") != 0) return 1;
|
| 217 |
+
if (check_cublas(cublasLtMatrixLayoutSetAttribute(c_layout, CUBLASLT_MATRIX_LAYOUT_ORDER, &order, sizeof(order)), "cublasLtMatrixLayoutSetAttribute(D order)") != 0) return 1;
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
if (check_cublas(cublasLtMatmulPreferenceCreate(&pref), "cublasLtMatmulPreferenceCreate") != 0) return 1;
|
| 221 |
+
if (check_cublas(cublasLtMatmulPreferenceSetAttribute(pref, CUBLASLT_MATMUL_PREF_MAX_WORKSPACE_BYTES, &workspace_bytes, sizeof(workspace_bytes)), "cublasLtMatmulPreferenceSetAttribute") != 0) return 1;
|
| 222 |
+
if (check_cublas(cublasLtMatmulAlgoGetHeuristic(lt_handle, op_desc, a_layout, b_layout, c_layout, c_layout, pref, 1, &heuristic, &returned_results), "cublasLtMatmulAlgoGetHeuristic") != 0) return 1;
|
| 223 |
+
if (returned_results <= 0) {
|
| 224 |
+
fprintf(stderr, "cublasLtMatmulAlgoGetHeuristic returned no algorithms\n");
|
| 225 |
+
return 1;
|
| 226 |
+
}
|
| 227 |
+
|
| 228 |
+
if (check_cuda(cudaEventCreate(&start), "cudaEventCreate(start)") != 0) return 1;
|
| 229 |
+
if (check_cuda(cudaEventCreate(&stop), "cudaEventCreate(stop)") != 0) return 1;
|
| 230 |
+
|
| 231 |
+
for (iter = 0; iter < cfg.warmup; ++iter) {
|
| 232 |
+
if (check_cublas(
|
| 233 |
+
cublasLtMatmul(
|
| 234 |
+
lt_handle,
|
| 235 |
+
op_desc,
|
| 236 |
+
&alpha,
|
| 237 |
+
dev_a,
|
| 238 |
+
a_layout,
|
| 239 |
+
dev_b,
|
| 240 |
+
b_layout,
|
| 241 |
+
&beta,
|
| 242 |
+
dev_d,
|
| 243 |
+
c_layout,
|
| 244 |
+
dev_d,
|
| 245 |
+
c_layout,
|
| 246 |
+
&heuristic.algo,
|
| 247 |
+
workspace,
|
| 248 |
+
workspace_bytes,
|
| 249 |
+
stream),
|
| 250 |
+
"cublasLtMatmul(warmup)")
|
| 251 |
+
!= 0) return 1;
|
| 252 |
+
}
|
| 253 |
+
if (check_cuda(cudaStreamSynchronize(stream), "cudaStreamSynchronize(warmup)") != 0) return 1;
|
| 254 |
+
|
| 255 |
+
for (iter = 0; iter < cfg.iters; ++iter) {
|
| 256 |
+
if (check_cuda(cudaEventRecord(start, stream), "cudaEventRecord(start)") != 0) return 1;
|
| 257 |
+
if (check_cublas(
|
| 258 |
+
cublasLtMatmul(
|
| 259 |
+
lt_handle,
|
| 260 |
+
op_desc,
|
| 261 |
+
&alpha,
|
| 262 |
+
dev_a,
|
| 263 |
+
a_layout,
|
| 264 |
+
dev_b,
|
| 265 |
+
b_layout,
|
| 266 |
+
&beta,
|
| 267 |
+
dev_d,
|
| 268 |
+
c_layout,
|
| 269 |
+
dev_d,
|
| 270 |
+
c_layout,
|
| 271 |
+
&heuristic.algo,
|
| 272 |
+
workspace,
|
| 273 |
+
workspace_bytes,
|
| 274 |
+
stream),
|
| 275 |
+
"cublasLtMatmul(bench)")
|
| 276 |
+
!= 0) return 1;
|
| 277 |
+
if (check_cuda(cudaEventRecord(stop, stream), "cudaEventRecord(stop)") != 0) return 1;
|
| 278 |
+
if (check_cuda(cudaEventSynchronize(stop), "cudaEventSynchronize(stop)") != 0) return 1;
|
| 279 |
+
if (check_cuda(cudaEventElapsedTime(&elapsed_ms, start, stop), "cudaEventElapsedTime") != 0) return 1;
|
| 280 |
+
if (iter == 0 || elapsed_ms < (float)best_ms) {
|
| 281 |
+
best_ms = elapsed_ms;
|
| 282 |
+
}
|
| 283 |
+
}
|
| 284 |
+
|
| 285 |
+
if (!cfg.skip_reference) {
|
| 286 |
+
int idx;
|
| 287 |
+
if (check_cuda(cudaMemcpy(host_d, dev_d, d_bytes, cudaMemcpyDeviceToHost), "cudaMemcpy(d)") != 0) return 1;
|
| 288 |
+
for (idx = 0; idx < cfg.m * cfg.n; ++idx) {
|
| 289 |
+
double diff = fabs((double)__bfloat162float(host_d[idx]) - (double)__bfloat162float(ref_d[idx]));
|
| 290 |
+
if (diff > max_abs_diff) {
|
| 291 |
+
max_abs_diff = diff;
|
| 292 |
+
}
|
| 293 |
+
}
|
| 294 |
+
}
|
| 295 |
+
|
| 296 |
+
printf("kernel=hopper_cublaslt_bf16\n");
|
| 297 |
+
printf("arch=sm%d%d\n", props.major, props.minor);
|
| 298 |
+
printf("shape=%dx%dx%d\n", cfg.m, cfg.n, cfg.k);
|
| 299 |
+
printf("workspace_bytes=%zu\n", workspace_bytes);
|
| 300 |
+
printf("heuristic_workspace_bytes=%zu\n", heuristic.workspaceSize);
|
| 301 |
+
printf("skip_reference=%d\n", cfg.skip_reference);
|
| 302 |
+
printf("best_ms=%.3f\n", best_ms);
|
| 303 |
+
if (!cfg.skip_reference) {
|
| 304 |
+
printf("max_abs_diff=%.6f\n", max_abs_diff);
|
| 305 |
+
}
|
| 306 |
+
if (best_ms > 0.0) {
|
| 307 |
+
double flops = 2.0 * (double)cfg.m * (double)cfg.n * (double)cfg.k;
|
| 308 |
+
double gflops = flops / (best_ms * 1.0e6);
|
| 309 |
+
printf("gflops=%.3f\n", gflops);
|
| 310 |
+
printf("tflops=%.3f\n", gflops / 1000.0);
|
| 311 |
+
}
|
| 312 |
+
|
| 313 |
+
if (start) cudaEventDestroy(start);
|
| 314 |
+
if (stop) cudaEventDestroy(stop);
|
| 315 |
+
if (stream) cudaStreamDestroy(stream);
|
| 316 |
+
if (pref) cublasLtMatmulPreferenceDestroy(pref);
|
| 317 |
+
if (c_layout) cublasLtMatrixLayoutDestroy(c_layout);
|
| 318 |
+
if (b_layout) cublasLtMatrixLayoutDestroy(b_layout);
|
| 319 |
+
if (a_layout) cublasLtMatrixLayoutDestroy(a_layout);
|
| 320 |
+
if (op_desc) cublasLtMatmulDescDestroy(op_desc);
|
| 321 |
+
if (lt_handle) cublasLtDestroy(lt_handle);
|
| 322 |
+
if (workspace) cudaFree(workspace);
|
| 323 |
+
if (dev_d) cudaFree(dev_d);
|
| 324 |
+
if (dev_b) cudaFree(dev_b);
|
| 325 |
+
if (dev_a) cudaFree(dev_a);
|
| 326 |
+
free(ref_d);
|
| 327 |
+
free(host_d);
|
| 328 |
+
free(host_b);
|
| 329 |
+
free(host_a);
|
| 330 |
+
return (cfg.skip_reference || max_abs_diff <= 0.25) ? 0 : 1;
|
| 331 |
+
}
|
kernels/prototypes/hopper/tensor_core/kernel.cu
ADDED
|
@@ -0,0 +1,450 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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|
| 5 |
+
#include <stdint.h>
|
| 6 |
+
#include <stdio.h>
|
| 7 |
+
#include <stdlib.h>
|
| 8 |
+
#include <string.h>
|
| 9 |
+
|
| 10 |
+
#if defined(PYC_HOPPER_TENSOR_CORE_USE_BF16) && PYC_HOPPER_TENSOR_CORE_USE_BF16
|
| 11 |
+
#include <cuda_bf16.h>
|
| 12 |
+
typedef __nv_bfloat16 pyc_hopper_tc_scalar_t;
|
| 13 |
+
#define PYC_HOPPER_TC_LANE_NAME "bf16"
|
| 14 |
+
static __host__ __device__ inline pyc_hopper_tc_scalar_t pyc_hopper_tc_make_scalar(float value) {
|
| 15 |
+
return __float2bfloat16(value);
|
| 16 |
+
}
|
| 17 |
+
static __host__ __device__ inline float pyc_hopper_tc_scalar_to_float(pyc_hopper_tc_scalar_t value) {
|
| 18 |
+
return __bfloat162float(value);
|
| 19 |
+
}
|
| 20 |
+
#else
|
| 21 |
+
#include <cuda_fp16.h>
|
| 22 |
+
typedef half pyc_hopper_tc_scalar_t;
|
| 23 |
+
#define PYC_HOPPER_TC_LANE_NAME "fp16"
|
| 24 |
+
static __host__ __device__ inline pyc_hopper_tc_scalar_t pyc_hopper_tc_make_scalar(float value) {
|
| 25 |
+
return __float2half(value);
|
| 26 |
+
}
|
| 27 |
+
static __host__ __device__ inline float pyc_hopper_tc_scalar_to_float(pyc_hopper_tc_scalar_t value) {
|
| 28 |
+
return __half2float(value);
|
| 29 |
+
}
|
| 30 |
+
#endif
|
| 31 |
+
|
| 32 |
+
namespace wmma = nvcuda::wmma;
|
| 33 |
+
|
| 34 |
+
#ifndef PYC_HOPPER_TC_MMA_TILE_M
|
| 35 |
+
#define PYC_HOPPER_TC_MMA_TILE_M 16
|
| 36 |
+
#endif
|
| 37 |
+
|
| 38 |
+
#ifndef PYC_HOPPER_TC_MMA_TILE_N
|
| 39 |
+
#define PYC_HOPPER_TC_MMA_TILE_N 16
|
| 40 |
+
#endif
|
| 41 |
+
|
| 42 |
+
#ifndef PYC_HOPPER_TC_MMA_TILE_K
|
| 43 |
+
#define PYC_HOPPER_TC_MMA_TILE_K 16
|
| 44 |
+
#endif
|
| 45 |
+
|
| 46 |
+
#ifndef PYC_HOPPER_TC_WARP_ROW_TILES
|
| 47 |
+
#define PYC_HOPPER_TC_WARP_ROW_TILES 1
|
| 48 |
+
#endif
|
| 49 |
+
|
| 50 |
+
#ifndef PYC_HOPPER_TC_WARP_COL_TILES
|
| 51 |
+
#define PYC_HOPPER_TC_WARP_COL_TILES 1
|
| 52 |
+
#endif
|
| 53 |
+
|
| 54 |
+
#ifndef PYC_HOPPER_TC_WARP_ROW_GROUPS
|
| 55 |
+
#define PYC_HOPPER_TC_WARP_ROW_GROUPS 4
|
| 56 |
+
#endif
|
| 57 |
+
|
| 58 |
+
#ifndef PYC_HOPPER_TC_WARP_COL_GROUPS
|
| 59 |
+
#define PYC_HOPPER_TC_WARP_COL_GROUPS 4
|
| 60 |
+
#endif
|
| 61 |
+
|
| 62 |
+
#ifndef PYC_HOPPER_TC_TILE_K
|
| 63 |
+
#define PYC_HOPPER_TC_TILE_K 16
|
| 64 |
+
#endif
|
| 65 |
+
|
| 66 |
+
#ifndef PYC_HOPPER_TC_SHARED_PAD_A
|
| 67 |
+
#define PYC_HOPPER_TC_SHARED_PAD_A 0
|
| 68 |
+
#endif
|
| 69 |
+
|
| 70 |
+
#ifndef PYC_HOPPER_TC_SHARED_PAD_B
|
| 71 |
+
#define PYC_HOPPER_TC_SHARED_PAD_B 0
|
| 72 |
+
#endif
|
| 73 |
+
|
| 74 |
+
#define PYC_HOPPER_TC_WARP_TILE_M (PYC_HOPPER_TC_MMA_TILE_M * PYC_HOPPER_TC_WARP_ROW_TILES)
|
| 75 |
+
#define PYC_HOPPER_TC_WARP_TILE_N (PYC_HOPPER_TC_MMA_TILE_N * PYC_HOPPER_TC_WARP_COL_TILES)
|
| 76 |
+
#define PYC_HOPPER_TC_TILE_M (PYC_HOPPER_TC_WARP_TILE_M * PYC_HOPPER_TC_WARP_ROW_GROUPS)
|
| 77 |
+
#define PYC_HOPPER_TC_TILE_N (PYC_HOPPER_TC_WARP_TILE_N * PYC_HOPPER_TC_WARP_COL_GROUPS)
|
| 78 |
+
#define PYC_HOPPER_TC_WARPS_PER_BLOCK (PYC_HOPPER_TC_WARP_ROW_GROUPS * PYC_HOPPER_TC_WARP_COL_GROUPS)
|
| 79 |
+
#define PYC_HOPPER_TC_THREADS_PER_BLOCK (PYC_HOPPER_TC_WARPS_PER_BLOCK * 32)
|
| 80 |
+
#define PYC_HOPPER_TC_SHARED_STRIDE_A (PYC_HOPPER_TC_TILE_K + PYC_HOPPER_TC_SHARED_PAD_A)
|
| 81 |
+
#define PYC_HOPPER_TC_SHARED_STRIDE_B (PYC_HOPPER_TC_TILE_K + PYC_HOPPER_TC_SHARED_PAD_B)
|
| 82 |
+
|
| 83 |
+
typedef struct {
|
| 84 |
+
int m;
|
| 85 |
+
int n;
|
| 86 |
+
int k;
|
| 87 |
+
int warmup;
|
| 88 |
+
int iters;
|
| 89 |
+
int skip_reference;
|
| 90 |
+
} pyc_hopper_tc_config;
|
| 91 |
+
|
| 92 |
+
static int check_cuda(cudaError_t status, const char* what) {
|
| 93 |
+
if (status != cudaSuccess) {
|
| 94 |
+
fprintf(stderr, "%s failed: %s\n", what, cudaGetErrorString(status));
|
| 95 |
+
return -1;
|
| 96 |
+
}
|
| 97 |
+
return 0;
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
static int parse_int_arg(const char* text, int* out_value) {
|
| 101 |
+
char* end = NULL;
|
| 102 |
+
long parsed;
|
| 103 |
+
if (!text || !out_value) {
|
| 104 |
+
return -1;
|
| 105 |
+
}
|
| 106 |
+
parsed = strtol(text, &end, 10);
|
| 107 |
+
if (end == text || *end != '\0' || parsed < 0 || parsed > INT32_MAX) {
|
| 108 |
+
return -1;
|
| 109 |
+
}
|
| 110 |
+
*out_value = (int)parsed;
|
| 111 |
+
return 0;
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
static int env_flag(const char* name, int default_value) {
|
| 115 |
+
const char* raw = getenv(name);
|
| 116 |
+
if (!raw || raw[0] == '\0') {
|
| 117 |
+
return default_value;
|
| 118 |
+
}
|
| 119 |
+
if (
|
| 120 |
+
strcmp(raw, "1") == 0 || strcmp(raw, "true") == 0 || strcmp(raw, "TRUE") == 0
|
| 121 |
+
|| strcmp(raw, "yes") == 0 || strcmp(raw, "on") == 0) {
|
| 122 |
+
return 1;
|
| 123 |
+
}
|
| 124 |
+
if (
|
| 125 |
+
strcmp(raw, "0") == 0 || strcmp(raw, "false") == 0 || strcmp(raw, "FALSE") == 0
|
| 126 |
+
|| strcmp(raw, "no") == 0 || strcmp(raw, "off") == 0) {
|
| 127 |
+
return 0;
|
| 128 |
+
}
|
| 129 |
+
return default_value;
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
static void fill_matrix(pyc_hopper_tc_scalar_t* data, int rows, int cols, float scale) {
|
| 133 |
+
int i;
|
| 134 |
+
for (i = 0; i < rows * cols; ++i) {
|
| 135 |
+
int pattern = (i * 23 + rows * 13 + cols * 5) % 31;
|
| 136 |
+
data[i] = pyc_hopper_tc_make_scalar(((float)pattern - 15.0f) * scale);
|
| 137 |
+
}
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
static void reference_gemm(
|
| 141 |
+
const pyc_hopper_tc_scalar_t* a,
|
| 142 |
+
const pyc_hopper_tc_scalar_t* b,
|
| 143 |
+
float* c,
|
| 144 |
+
int m,
|
| 145 |
+
int n,
|
| 146 |
+
int k) {
|
| 147 |
+
int row;
|
| 148 |
+
for (row = 0; row < m; ++row) {
|
| 149 |
+
int col;
|
| 150 |
+
for (col = 0; col < n; ++col) {
|
| 151 |
+
float acc = 0.0f;
|
| 152 |
+
int kk;
|
| 153 |
+
for (kk = 0; kk < k; ++kk) {
|
| 154 |
+
acc += pyc_hopper_tc_scalar_to_float(a[row * k + kk]) * pyc_hopper_tc_scalar_to_float(b[kk * n + col]);
|
| 155 |
+
}
|
| 156 |
+
c[row * n + col] = acc;
|
| 157 |
+
}
|
| 158 |
+
}
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
__launch_bounds__(PYC_HOPPER_TC_THREADS_PER_BLOCK, 1)
|
| 162 |
+
__global__ void pyc_hopper_tc_gemm_kernel(
|
| 163 |
+
const pyc_hopper_tc_scalar_t* __restrict__ a,
|
| 164 |
+
const pyc_hopper_tc_scalar_t* __restrict__ b,
|
| 165 |
+
float* __restrict__ c,
|
| 166 |
+
int m,
|
| 167 |
+
int n,
|
| 168 |
+
int k) {
|
| 169 |
+
__shared__ __align__(16) pyc_hopper_tc_scalar_t shared_a[PYC_HOPPER_TC_TILE_M][PYC_HOPPER_TC_SHARED_STRIDE_A];
|
| 170 |
+
__shared__ __align__(16) pyc_hopper_tc_scalar_t shared_b[PYC_HOPPER_TC_TILE_N][PYC_HOPPER_TC_SHARED_STRIDE_B];
|
| 171 |
+
|
| 172 |
+
const int warp_id = threadIdx.x / 32;
|
| 173 |
+
const int block_row = blockIdx.y * PYC_HOPPER_TC_TILE_M;
|
| 174 |
+
const int block_col = blockIdx.x * PYC_HOPPER_TC_TILE_N;
|
| 175 |
+
const int warp_row_group = warp_id / PYC_HOPPER_TC_WARP_COL_GROUPS;
|
| 176 |
+
const int warp_col_group = warp_id % PYC_HOPPER_TC_WARP_COL_GROUPS;
|
| 177 |
+
const int warp_row = warp_row_group * PYC_HOPPER_TC_WARP_TILE_M;
|
| 178 |
+
const int warp_col = warp_col_group * PYC_HOPPER_TC_WARP_TILE_N;
|
| 179 |
+
|
| 180 |
+
wmma::fragment<wmma::accumulator, 16, 16, 16, float> acc[PYC_HOPPER_TC_WARP_ROW_TILES][PYC_HOPPER_TC_WARP_COL_TILES];
|
| 181 |
+
|
| 182 |
+
if (warp_id >= PYC_HOPPER_TC_WARPS_PER_BLOCK) {
|
| 183 |
+
return;
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
for (int row_tile = 0; row_tile < PYC_HOPPER_TC_WARP_ROW_TILES; ++row_tile) {
|
| 187 |
+
for (int col_tile = 0; col_tile < PYC_HOPPER_TC_WARP_COL_TILES; ++col_tile) {
|
| 188 |
+
wmma::fill_fragment(acc[row_tile][col_tile], 0.0f);
|
| 189 |
+
}
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
for (int kk = 0; kk < k; kk += PYC_HOPPER_TC_TILE_K) {
|
| 193 |
+
int idx;
|
| 194 |
+
|
| 195 |
+
for (idx = threadIdx.x; idx < PYC_HOPPER_TC_TILE_M * PYC_HOPPER_TC_TILE_K; idx += blockDim.x) {
|
| 196 |
+
const int row = idx / PYC_HOPPER_TC_TILE_K;
|
| 197 |
+
const int col = idx % PYC_HOPPER_TC_TILE_K;
|
| 198 |
+
const int g_row = block_row + row;
|
| 199 |
+
const int g_col = kk + col;
|
| 200 |
+
if (g_row < m && g_col < k) {
|
| 201 |
+
shared_a[row][col] = a[g_row * k + g_col];
|
| 202 |
+
} else {
|
| 203 |
+
shared_a[row][col] = pyc_hopper_tc_make_scalar(0.0f);
|
| 204 |
+
}
|
| 205 |
+
}
|
| 206 |
+
|
| 207 |
+
for (idx = threadIdx.x; idx < PYC_HOPPER_TC_TILE_N * PYC_HOPPER_TC_TILE_K; idx += blockDim.x) {
|
| 208 |
+
const int row = idx / PYC_HOPPER_TC_TILE_K;
|
| 209 |
+
const int col = idx % PYC_HOPPER_TC_TILE_K;
|
| 210 |
+
const int g_row = kk + col;
|
| 211 |
+
const int g_col = block_col + row;
|
| 212 |
+
if (g_row < k && g_col < n) {
|
| 213 |
+
shared_b[row][col] = b[g_row * n + g_col];
|
| 214 |
+
} else {
|
| 215 |
+
shared_b[row][col] = pyc_hopper_tc_make_scalar(0.0f);
|
| 216 |
+
}
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
__syncthreads();
|
| 220 |
+
|
| 221 |
+
for (int k_frag = 0; k_frag < PYC_HOPPER_TC_TILE_K; k_frag += PYC_HOPPER_TC_MMA_TILE_K) {
|
| 222 |
+
wmma::fragment<wmma::matrix_a, 16, 16, 16, pyc_hopper_tc_scalar_t, wmma::row_major> a_frag[PYC_HOPPER_TC_WARP_ROW_TILES];
|
| 223 |
+
wmma::fragment<wmma::matrix_b, 16, 16, 16, pyc_hopper_tc_scalar_t, wmma::col_major> b_frag[PYC_HOPPER_TC_WARP_COL_TILES];
|
| 224 |
+
|
| 225 |
+
for (int row_tile = 0; row_tile < PYC_HOPPER_TC_WARP_ROW_TILES; ++row_tile) {
|
| 226 |
+
const int a_row = warp_row + row_tile * PYC_HOPPER_TC_MMA_TILE_M;
|
| 227 |
+
wmma::load_matrix_sync(a_frag[row_tile], &shared_a[a_row][k_frag], PYC_HOPPER_TC_SHARED_STRIDE_A);
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
+
for (int col_tile = 0; col_tile < PYC_HOPPER_TC_WARP_COL_TILES; ++col_tile) {
|
| 231 |
+
const int b_row = warp_col + col_tile * PYC_HOPPER_TC_MMA_TILE_N;
|
| 232 |
+
wmma::load_matrix_sync(b_frag[col_tile], &shared_b[b_row][k_frag], PYC_HOPPER_TC_SHARED_STRIDE_B);
|
| 233 |
+
}
|
| 234 |
+
|
| 235 |
+
for (int row_tile = 0; row_tile < PYC_HOPPER_TC_WARP_ROW_TILES; ++row_tile) {
|
| 236 |
+
for (int col_tile = 0; col_tile < PYC_HOPPER_TC_WARP_COL_TILES; ++col_tile) {
|
| 237 |
+
wmma::mma_sync(acc[row_tile][col_tile], a_frag[row_tile], b_frag[col_tile], acc[row_tile][col_tile]);
|
| 238 |
+
}
|
| 239 |
+
}
|
| 240 |
+
}
|
| 241 |
+
|
| 242 |
+
__syncthreads();
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
for (int row_tile = 0; row_tile < PYC_HOPPER_TC_WARP_ROW_TILES; ++row_tile) {
|
| 246 |
+
const int c_row = block_row + warp_row + row_tile * PYC_HOPPER_TC_MMA_TILE_M;
|
| 247 |
+
for (int col_tile = 0; col_tile < PYC_HOPPER_TC_WARP_COL_TILES; ++col_tile) {
|
| 248 |
+
const int c_col = block_col + warp_col + col_tile * PYC_HOPPER_TC_MMA_TILE_N;
|
| 249 |
+
if (c_row < m && c_col < n) {
|
| 250 |
+
wmma::store_matrix_sync(&c[c_row * n + c_col], acc[row_tile][col_tile], n, wmma::mem_row_major);
|
| 251 |
+
}
|
| 252 |
+
}
|
| 253 |
+
}
|
| 254 |
+
}
|
| 255 |
+
|
| 256 |
+
static int set_kernel_attributes(void) {
|
| 257 |
+
cudaError_t status;
|
| 258 |
+
|
| 259 |
+
status = cudaFuncSetAttribute(
|
| 260 |
+
pyc_hopper_tc_gemm_kernel,
|
| 261 |
+
cudaFuncAttributePreferredSharedMemoryCarveout,
|
| 262 |
+
100);
|
| 263 |
+
if (status != cudaSuccess && status != cudaErrorNotSupported) {
|
| 264 |
+
fprintf(stderr, "cudaFuncSetAttribute failed: %s\n", cudaGetErrorString(status));
|
| 265 |
+
return -1;
|
| 266 |
+
}
|
| 267 |
+
|
| 268 |
+
return 0;
|
| 269 |
+
}
|
| 270 |
+
|
| 271 |
+
static int parse_config(int argc, char** argv, pyc_hopper_tc_config* cfg) {
|
| 272 |
+
if (!cfg) {
|
| 273 |
+
return -1;
|
| 274 |
+
}
|
| 275 |
+
|
| 276 |
+
cfg->m = 1024;
|
| 277 |
+
cfg->n = 1024;
|
| 278 |
+
cfg->k = 1024;
|
| 279 |
+
cfg->warmup = 5;
|
| 280 |
+
cfg->iters = 20;
|
| 281 |
+
cfg->skip_reference = env_flag("PYC_HOPPER_TC_SKIP_REFERENCE", 0);
|
| 282 |
+
|
| 283 |
+
if (argc > 1 && parse_int_arg(argv[1], &cfg->m) != 0) return -1;
|
| 284 |
+
if (argc > 2 && parse_int_arg(argv[2], &cfg->n) != 0) return -1;
|
| 285 |
+
if (argc > 3 && parse_int_arg(argv[3], &cfg->k) != 0) return -1;
|
| 286 |
+
if (argc > 4 && parse_int_arg(argv[4], &cfg->warmup) != 0) return -1;
|
| 287 |
+
if (argc > 5 && parse_int_arg(argv[5], &cfg->iters) != 0) return -1;
|
| 288 |
+
if (argc > 6 && parse_int_arg(argv[6], &cfg->skip_reference) != 0) return -1;
|
| 289 |
+
|
| 290 |
+
return 0;
|
| 291 |
+
}
|
| 292 |
+
|
| 293 |
+
int main(int argc, char** argv) {
|
| 294 |
+
pyc_hopper_tc_config cfg;
|
| 295 |
+
struct cudaDeviceProp props;
|
| 296 |
+
pyc_hopper_tc_scalar_t* host_a = NULL;
|
| 297 |
+
pyc_hopper_tc_scalar_t* host_b = NULL;
|
| 298 |
+
float* host_c = NULL;
|
| 299 |
+
float* ref_c = NULL;
|
| 300 |
+
pyc_hopper_tc_scalar_t* dev_a = NULL;
|
| 301 |
+
pyc_hopper_tc_scalar_t* dev_b = NULL;
|
| 302 |
+
float* dev_c = NULL;
|
| 303 |
+
cudaEvent_t start = NULL;
|
| 304 |
+
cudaEvent_t stop = NULL;
|
| 305 |
+
size_t a_bytes;
|
| 306 |
+
size_t b_bytes;
|
| 307 |
+
size_t c_bytes;
|
| 308 |
+
dim3 block;
|
| 309 |
+
dim3 grid;
|
| 310 |
+
float elapsed_ms = 0.0f;
|
| 311 |
+
double best_ms = 0.0;
|
| 312 |
+
double max_abs_diff = 0.0;
|
| 313 |
+
int iter;
|
| 314 |
+
|
| 315 |
+
if (parse_config(argc, argv, &cfg) != 0) {
|
| 316 |
+
fprintf(stderr, "usage: %s [m] [n] [k] [warmup] [iters] [skip_reference]\n", argv[0]);
|
| 317 |
+
return 2;
|
| 318 |
+
}
|
| 319 |
+
|
| 320 |
+
if (
|
| 321 |
+
(cfg.m % PYC_HOPPER_TC_TILE_M) != 0 || (cfg.n % PYC_HOPPER_TC_TILE_N) != 0
|
| 322 |
+
|| (cfg.k % PYC_HOPPER_TC_TILE_K) != 0) {
|
| 323 |
+
fprintf(
|
| 324 |
+
stderr,
|
| 325 |
+
"Hopper Tensor Core lane requires %dx%dx%d-aligned shapes\n",
|
| 326 |
+
PYC_HOPPER_TC_TILE_M,
|
| 327 |
+
PYC_HOPPER_TC_TILE_N,
|
| 328 |
+
PYC_HOPPER_TC_TILE_K);
|
| 329 |
+
return 2;
|
| 330 |
+
}
|
| 331 |
+
|
| 332 |
+
if (check_cuda(cudaGetDeviceProperties(&props, 0), "cudaGetDeviceProperties") != 0) {
|
| 333 |
+
return 1;
|
| 334 |
+
}
|
| 335 |
+
if (props.major < 9) {
|
| 336 |
+
fprintf(stderr, "Hopper Tensor Core prototype requires sm_90-class hardware\n");
|
| 337 |
+
return 1;
|
| 338 |
+
}
|
| 339 |
+
|
| 340 |
+
a_bytes = (size_t)cfg.m * (size_t)cfg.k * sizeof(pyc_hopper_tc_scalar_t);
|
| 341 |
+
b_bytes = (size_t)cfg.k * (size_t)cfg.n * sizeof(pyc_hopper_tc_scalar_t);
|
| 342 |
+
c_bytes = (size_t)cfg.m * (size_t)cfg.n * sizeof(float);
|
| 343 |
+
|
| 344 |
+
host_a = (pyc_hopper_tc_scalar_t*)malloc(a_bytes);
|
| 345 |
+
host_b = (pyc_hopper_tc_scalar_t*)malloc(b_bytes);
|
| 346 |
+
if (!host_a || !host_b) {
|
| 347 |
+
fprintf(stderr, "host allocation failed\n");
|
| 348 |
+
return 1;
|
| 349 |
+
}
|
| 350 |
+
if (!cfg.skip_reference) {
|
| 351 |
+
host_c = (float*)malloc(c_bytes);
|
| 352 |
+
ref_c = (float*)malloc(c_bytes);
|
| 353 |
+
if (!host_c || !ref_c) {
|
| 354 |
+
fprintf(stderr, "host validation allocation failed\n");
|
| 355 |
+
return 1;
|
| 356 |
+
}
|
| 357 |
+
}
|
| 358 |
+
|
| 359 |
+
fill_matrix(host_a, cfg.m, cfg.k, 0.03125f);
|
| 360 |
+
fill_matrix(host_b, cfg.k, cfg.n, 0.0625f);
|
| 361 |
+
if (!cfg.skip_reference) {
|
| 362 |
+
reference_gemm(host_a, host_b, ref_c, cfg.m, cfg.n, cfg.k);
|
| 363 |
+
}
|
| 364 |
+
|
| 365 |
+
if (check_cuda(cudaMalloc((void**)&dev_a, a_bytes), "cudaMalloc(a)") != 0) return 1;
|
| 366 |
+
if (check_cuda(cudaMalloc((void**)&dev_b, b_bytes), "cudaMalloc(b)") != 0) return 1;
|
| 367 |
+
if (check_cuda(cudaMalloc((void**)&dev_c, c_bytes), "cudaMalloc(c)") != 0) return 1;
|
| 368 |
+
|
| 369 |
+
if (check_cuda(cudaMemcpy(dev_a, host_a, a_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(a)") != 0) return 1;
|
| 370 |
+
if (check_cuda(cudaMemcpy(dev_b, host_b, b_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(b)") != 0) return 1;
|
| 371 |
+
|
| 372 |
+
if (set_kernel_attributes() != 0) return 1;
|
| 373 |
+
|
| 374 |
+
block = dim3(PYC_HOPPER_TC_THREADS_PER_BLOCK, 1, 1);
|
| 375 |
+
grid = dim3(
|
| 376 |
+
(unsigned int)((cfg.n + PYC_HOPPER_TC_TILE_N - 1) / PYC_HOPPER_TC_TILE_N),
|
| 377 |
+
(unsigned int)((cfg.m + PYC_HOPPER_TC_TILE_M - 1) / PYC_HOPPER_TC_TILE_M),
|
| 378 |
+
1);
|
| 379 |
+
|
| 380 |
+
if (check_cuda(cudaEventCreate(&start), "cudaEventCreate(start)") != 0) return 1;
|
| 381 |
+
if (check_cuda(cudaEventCreate(&stop), "cudaEventCreate(stop)") != 0) return 1;
|
| 382 |
+
|
| 383 |
+
for (iter = 0; iter < cfg.warmup; ++iter) {
|
| 384 |
+
pyc_hopper_tc_gemm_kernel<<<grid, block>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 385 |
+
}
|
| 386 |
+
if (check_cuda(cudaGetLastError(), "kernel launch warmup") != 0) return 1;
|
| 387 |
+
if (check_cuda(cudaDeviceSynchronize(), "cudaDeviceSynchronize warmup") != 0) return 1;
|
| 388 |
+
|
| 389 |
+
best_ms = 0.0;
|
| 390 |
+
for (iter = 0; iter < cfg.iters; ++iter) {
|
| 391 |
+
if (check_cuda(cudaEventRecord(start), "cudaEventRecord(start)") != 0) return 1;
|
| 392 |
+
pyc_hopper_tc_gemm_kernel<<<grid, block>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 393 |
+
if (check_cuda(cudaEventRecord(stop), "cudaEventRecord(stop)") != 0) return 1;
|
| 394 |
+
if (check_cuda(cudaEventSynchronize(stop), "cudaEventSynchronize(stop)") != 0) return 1;
|
| 395 |
+
if (check_cuda(cudaEventElapsedTime(&elapsed_ms, start, stop), "cudaEventElapsedTime") != 0) return 1;
|
| 396 |
+
if (iter == 0 || elapsed_ms < (float)best_ms) {
|
| 397 |
+
best_ms = elapsed_ms;
|
| 398 |
+
}
|
| 399 |
+
}
|
| 400 |
+
|
| 401 |
+
if (!cfg.skip_reference) {
|
| 402 |
+
if (check_cuda(cudaMemcpy(host_c, dev_c, c_bytes, cudaMemcpyDeviceToHost), "cudaMemcpy(c)") != 0) return 1;
|
| 403 |
+
for (iter = 0; iter < cfg.m * cfg.n; ++iter) {
|
| 404 |
+
double diff = fabs((double)host_c[iter] - (double)ref_c[iter]);
|
| 405 |
+
if (diff > max_abs_diff) {
|
| 406 |
+
max_abs_diff = diff;
|
| 407 |
+
}
|
| 408 |
+
}
|
| 409 |
+
}
|
| 410 |
+
|
| 411 |
+
printf("kernel=hopper_tensor_core\n");
|
| 412 |
+
printf("lane=%s\n", PYC_HOPPER_TC_LANE_NAME);
|
| 413 |
+
printf("arch=sm%d%d\n", props.major, props.minor);
|
| 414 |
+
printf("shape=%dx%dx%d\n", cfg.m, cfg.n, cfg.k);
|
| 415 |
+
printf(
|
| 416 |
+
"tile=%dx%dx%d warp_tile=%dx%d warp_groups=%dx%d pads=%dx%d warps=%d threads=%d\n",
|
| 417 |
+
PYC_HOPPER_TC_TILE_M,
|
| 418 |
+
PYC_HOPPER_TC_TILE_N,
|
| 419 |
+
PYC_HOPPER_TC_TILE_K,
|
| 420 |
+
PYC_HOPPER_TC_WARP_TILE_M,
|
| 421 |
+
PYC_HOPPER_TC_WARP_TILE_N,
|
| 422 |
+
PYC_HOPPER_TC_WARP_ROW_GROUPS,
|
| 423 |
+
PYC_HOPPER_TC_WARP_COL_GROUPS,
|
| 424 |
+
PYC_HOPPER_TC_SHARED_PAD_A,
|
| 425 |
+
PYC_HOPPER_TC_SHARED_PAD_B,
|
| 426 |
+
PYC_HOPPER_TC_WARPS_PER_BLOCK,
|
| 427 |
+
PYC_HOPPER_TC_THREADS_PER_BLOCK);
|
| 428 |
+
printf("skip_reference=%d\n", cfg.skip_reference);
|
| 429 |
+
printf("best_ms=%.3f\n", best_ms);
|
| 430 |
+
if (!cfg.skip_reference) {
|
| 431 |
+
printf("max_abs_diff=%.6f\n", max_abs_diff);
|
| 432 |
+
}
|
| 433 |
+
if (best_ms > 0.0) {
|
| 434 |
+
double flops = 2.0 * (double)cfg.m * (double)cfg.n * (double)cfg.k;
|
| 435 |
+
double gflops = flops / (best_ms * 1.0e6);
|
| 436 |
+
printf("gflops=%.3f\n", gflops);
|
| 437 |
+
printf("tflops=%.3f\n", gflops / 1000.0);
|
| 438 |
+
}
|
| 439 |
+
|
| 440 |
+
cudaEventDestroy(start);
|
| 441 |
+
cudaEventDestroy(stop);
|
| 442 |
+
cudaFree(dev_a);
|
| 443 |
+
cudaFree(dev_b);
|
| 444 |
+
cudaFree(dev_c);
|
| 445 |
+
free(host_a);
|
| 446 |
+
free(host_b);
|
| 447 |
+
free(host_c);
|
| 448 |
+
free(ref_c);
|
| 449 |
+
return (cfg.skip_reference || max_abs_diff <= 0.2) ? 0 : 1;
|
| 450 |
+
}
|
kernels/prototypes/hopper/tensor_core_async/kernel.cu
ADDED
|
@@ -0,0 +1,612 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <cuda_runtime.h>
|
| 2 |
+
#include <mma.h>
|
| 3 |
+
|
| 4 |
+
#include <math.h>
|
| 5 |
+
#include <stdint.h>
|
| 6 |
+
#include <stdio.h>
|
| 7 |
+
#include <stdlib.h>
|
| 8 |
+
#include <string.h>
|
| 9 |
+
|
| 10 |
+
#if defined(PYC_HOPPER_TENSOR_CORE_USE_BF16) && PYC_HOPPER_TENSOR_CORE_USE_BF16
|
| 11 |
+
#include <cuda_bf16.h>
|
| 12 |
+
typedef __nv_bfloat16 pyc_hopper_tc_scalar_t;
|
| 13 |
+
#define PYC_HOPPER_TC_LANE_NAME "bf16"
|
| 14 |
+
static __host__ __device__ inline pyc_hopper_tc_scalar_t pyc_hopper_tc_make_scalar(float value) {
|
| 15 |
+
return __float2bfloat16(value);
|
| 16 |
+
}
|
| 17 |
+
static __host__ __device__ inline float pyc_hopper_tc_scalar_to_float(pyc_hopper_tc_scalar_t value) {
|
| 18 |
+
return __bfloat162float(value);
|
| 19 |
+
}
|
| 20 |
+
#else
|
| 21 |
+
#include <cuda_fp16.h>
|
| 22 |
+
typedef half pyc_hopper_tc_scalar_t;
|
| 23 |
+
#define PYC_HOPPER_TC_LANE_NAME "fp16"
|
| 24 |
+
static __host__ __device__ inline pyc_hopper_tc_scalar_t pyc_hopper_tc_make_scalar(float value) {
|
| 25 |
+
return __float2half(value);
|
| 26 |
+
}
|
| 27 |
+
static __host__ __device__ inline float pyc_hopper_tc_scalar_to_float(pyc_hopper_tc_scalar_t value) {
|
| 28 |
+
return __half2float(value);
|
| 29 |
+
}
|
| 30 |
+
#endif
|
| 31 |
+
|
| 32 |
+
namespace wmma = nvcuda::wmma;
|
| 33 |
+
|
| 34 |
+
#ifndef PYC_HOPPER_TC_MMA_TILE_M
|
| 35 |
+
#define PYC_HOPPER_TC_MMA_TILE_M 16
|
| 36 |
+
#endif
|
| 37 |
+
|
| 38 |
+
#ifndef PYC_HOPPER_TC_MMA_TILE_N
|
| 39 |
+
#define PYC_HOPPER_TC_MMA_TILE_N 16
|
| 40 |
+
#endif
|
| 41 |
+
|
| 42 |
+
#ifndef PYC_HOPPER_TC_MMA_TILE_K
|
| 43 |
+
#define PYC_HOPPER_TC_MMA_TILE_K 16
|
| 44 |
+
#endif
|
| 45 |
+
|
| 46 |
+
#ifndef PYC_HOPPER_TC_WARP_ROW_TILES
|
| 47 |
+
#define PYC_HOPPER_TC_WARP_ROW_TILES 2
|
| 48 |
+
#endif
|
| 49 |
+
|
| 50 |
+
#ifndef PYC_HOPPER_TC_WARP_COL_TILES
|
| 51 |
+
#define PYC_HOPPER_TC_WARP_COL_TILES 2
|
| 52 |
+
#endif
|
| 53 |
+
|
| 54 |
+
#ifndef PYC_HOPPER_TC_WARP_ROW_GROUPS
|
| 55 |
+
#define PYC_HOPPER_TC_WARP_ROW_GROUPS 2
|
| 56 |
+
#endif
|
| 57 |
+
|
| 58 |
+
#ifndef PYC_HOPPER_TC_WARP_COL_GROUPS
|
| 59 |
+
#define PYC_HOPPER_TC_WARP_COL_GROUPS 2
|
| 60 |
+
#endif
|
| 61 |
+
|
| 62 |
+
#ifndef PYC_HOPPER_TC_TILE_K
|
| 63 |
+
#define PYC_HOPPER_TC_TILE_K 32
|
| 64 |
+
#endif
|
| 65 |
+
|
| 66 |
+
#ifndef PYC_HOPPER_TC_SHARED_PAD_A
|
| 67 |
+
#define PYC_HOPPER_TC_SHARED_PAD_A 8
|
| 68 |
+
#endif
|
| 69 |
+
|
| 70 |
+
#ifndef PYC_HOPPER_TC_SHARED_PAD_B
|
| 71 |
+
#define PYC_HOPPER_TC_SHARED_PAD_B 8
|
| 72 |
+
#endif
|
| 73 |
+
|
| 74 |
+
#ifndef PYC_HOPPER_TC_STAGES
|
| 75 |
+
#define PYC_HOPPER_TC_STAGES 2
|
| 76 |
+
#endif
|
| 77 |
+
|
| 78 |
+
#define PYC_HOPPER_TC_WARP_TILE_M (PYC_HOPPER_TC_MMA_TILE_M * PYC_HOPPER_TC_WARP_ROW_TILES)
|
| 79 |
+
#define PYC_HOPPER_TC_WARP_TILE_N (PYC_HOPPER_TC_MMA_TILE_N * PYC_HOPPER_TC_WARP_COL_TILES)
|
| 80 |
+
#define PYC_HOPPER_TC_TILE_M (PYC_HOPPER_TC_WARP_TILE_M * PYC_HOPPER_TC_WARP_ROW_GROUPS)
|
| 81 |
+
#define PYC_HOPPER_TC_TILE_N (PYC_HOPPER_TC_WARP_TILE_N * PYC_HOPPER_TC_WARP_COL_GROUPS)
|
| 82 |
+
#define PYC_HOPPER_TC_WARPS_PER_BLOCK (PYC_HOPPER_TC_WARP_ROW_GROUPS * PYC_HOPPER_TC_WARP_COL_GROUPS)
|
| 83 |
+
#define PYC_HOPPER_TC_THREADS_PER_BLOCK (PYC_HOPPER_TC_WARPS_PER_BLOCK * 32)
|
| 84 |
+
#define PYC_HOPPER_TC_SHARED_STRIDE_A (PYC_HOPPER_TC_TILE_K + PYC_HOPPER_TC_SHARED_PAD_A)
|
| 85 |
+
#define PYC_HOPPER_TC_SHARED_STRIDE_B (PYC_HOPPER_TC_TILE_N + PYC_HOPPER_TC_SHARED_PAD_B)
|
| 86 |
+
#define PYC_HOPPER_TC_STAGE_A_ELEMS (PYC_HOPPER_TC_TILE_M * PYC_HOPPER_TC_SHARED_STRIDE_A)
|
| 87 |
+
#define PYC_HOPPER_TC_STAGE_B_ELEMS (PYC_HOPPER_TC_TILE_K * PYC_HOPPER_TC_SHARED_STRIDE_B)
|
| 88 |
+
#define PYC_HOPPER_TC_STAGE_ELEMS (PYC_HOPPER_TC_STAGE_A_ELEMS + PYC_HOPPER_TC_STAGE_B_ELEMS)
|
| 89 |
+
#define PYC_HOPPER_TC_SHARED_ELEMS (PYC_HOPPER_TC_STAGES * PYC_HOPPER_TC_STAGE_ELEMS)
|
| 90 |
+
#define PYC_HOPPER_TC_SHARED_BYTES (PYC_HOPPER_TC_SHARED_ELEMS * (int)sizeof(pyc_hopper_tc_scalar_t))
|
| 91 |
+
#define PYC_HOPPER_TC_COPY_BYTES 16
|
| 92 |
+
#define PYC_HOPPER_TC_COPY_ELEMS (PYC_HOPPER_TC_COPY_BYTES / (int)sizeof(pyc_hopper_tc_scalar_t))
|
| 93 |
+
|
| 94 |
+
typedef struct {
|
| 95 |
+
int m;
|
| 96 |
+
int n;
|
| 97 |
+
int k;
|
| 98 |
+
int warmup;
|
| 99 |
+
int iters;
|
| 100 |
+
int skip_reference;
|
| 101 |
+
} pyc_hopper_tc_config;
|
| 102 |
+
|
| 103 |
+
static int check_cuda(cudaError_t status, const char* what) {
|
| 104 |
+
if (status != cudaSuccess) {
|
| 105 |
+
fprintf(stderr, "%s failed: %s\n", what, cudaGetErrorString(status));
|
| 106 |
+
return -1;
|
| 107 |
+
}
|
| 108 |
+
return 0;
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
static int parse_int_arg(const char* text, int* out_value) {
|
| 112 |
+
char* end = NULL;
|
| 113 |
+
long parsed;
|
| 114 |
+
if (!text || !out_value) {
|
| 115 |
+
return -1;
|
| 116 |
+
}
|
| 117 |
+
parsed = strtol(text, &end, 10);
|
| 118 |
+
if (end == text || *end != '\0' || parsed < 0 || parsed > INT32_MAX) {
|
| 119 |
+
return -1;
|
| 120 |
+
}
|
| 121 |
+
*out_value = (int)parsed;
|
| 122 |
+
return 0;
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
static int env_flag(const char* name, int default_value) {
|
| 126 |
+
const char* raw = getenv(name);
|
| 127 |
+
if (!raw || raw[0] == '\0') {
|
| 128 |
+
return default_value;
|
| 129 |
+
}
|
| 130 |
+
if (
|
| 131 |
+
strcmp(raw, "1") == 0 || strcmp(raw, "true") == 0 || strcmp(raw, "TRUE") == 0
|
| 132 |
+
|| strcmp(raw, "yes") == 0 || strcmp(raw, "on") == 0) {
|
| 133 |
+
return 1;
|
| 134 |
+
}
|
| 135 |
+
if (
|
| 136 |
+
strcmp(raw, "0") == 0 || strcmp(raw, "false") == 0 || strcmp(raw, "FALSE") == 0
|
| 137 |
+
|| strcmp(raw, "no") == 0 || strcmp(raw, "off") == 0) {
|
| 138 |
+
return 0;
|
| 139 |
+
}
|
| 140 |
+
return default_value;
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
static void fill_matrix(pyc_hopper_tc_scalar_t* data, int rows, int cols, float scale) {
|
| 144 |
+
int i;
|
| 145 |
+
for (i = 0; i < rows * cols; ++i) {
|
| 146 |
+
int pattern = (i * 23 + rows * 13 + cols * 5) % 31;
|
| 147 |
+
data[i] = pyc_hopper_tc_make_scalar(((float)pattern - 15.0f) * scale);
|
| 148 |
+
}
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
static void reference_gemm(
|
| 152 |
+
const pyc_hopper_tc_scalar_t* a,
|
| 153 |
+
const pyc_hopper_tc_scalar_t* b,
|
| 154 |
+
float* c,
|
| 155 |
+
int m,
|
| 156 |
+
int n,
|
| 157 |
+
int k) {
|
| 158 |
+
int row;
|
| 159 |
+
for (row = 0; row < m; ++row) {
|
| 160 |
+
int col;
|
| 161 |
+
for (col = 0; col < n; ++col) {
|
| 162 |
+
float acc = 0.0f;
|
| 163 |
+
int kk;
|
| 164 |
+
for (kk = 0; kk < k; ++kk) {
|
| 165 |
+
acc += pyc_hopper_tc_scalar_to_float(a[row * k + kk]) * pyc_hopper_tc_scalar_to_float(b[kk * n + col]);
|
| 166 |
+
}
|
| 167 |
+
c[row * n + col] = acc;
|
| 168 |
+
}
|
| 169 |
+
}
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
__device__ static __forceinline__ void async_copy_16(void* dst, const void* src) {
|
| 173 |
+
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800
|
| 174 |
+
unsigned int smem_addr = (unsigned int)__cvta_generic_to_shared(dst);
|
| 175 |
+
asm volatile("cp.async.ca.shared.global [%0], [%1], 16;\n" :: "r"(smem_addr), "l"(src));
|
| 176 |
+
#else
|
| 177 |
+
*reinterpret_cast<int4*>(dst) = *reinterpret_cast<const int4*>(src);
|
| 178 |
+
#endif
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
__device__ static __forceinline__ void async_commit(void) {
|
| 182 |
+
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800
|
| 183 |
+
asm volatile("cp.async.commit_group;" ::: "memory");
|
| 184 |
+
#endif
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
__device__ static __forceinline__ void async_wait(void) {
|
| 188 |
+
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800
|
| 189 |
+
asm volatile("cp.async.wait_group 0;" ::: "memory");
|
| 190 |
+
#endif
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
__device__ static __forceinline__ pyc_hopper_tc_scalar_t* shared_stage_base(
|
| 194 |
+
pyc_hopper_tc_scalar_t* shared_mem,
|
| 195 |
+
int stage) {
|
| 196 |
+
return shared_mem + stage * PYC_HOPPER_TC_STAGE_ELEMS;
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
__device__ static __forceinline__ pyc_hopper_tc_scalar_t* shared_stage_a(
|
| 200 |
+
pyc_hopper_tc_scalar_t* shared_mem,
|
| 201 |
+
int stage) {
|
| 202 |
+
return shared_stage_base(shared_mem, stage);
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
__device__ static __forceinline__ pyc_hopper_tc_scalar_t* shared_stage_b(
|
| 206 |
+
pyc_hopper_tc_scalar_t* shared_mem,
|
| 207 |
+
int stage) {
|
| 208 |
+
return shared_stage_a(shared_mem, stage) + PYC_HOPPER_TC_STAGE_A_ELEMS;
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
__device__ static __forceinline__ pyc_hopper_tc_scalar_t shared_a_load(
|
| 212 |
+
const pyc_hopper_tc_scalar_t* shared_a,
|
| 213 |
+
int row,
|
| 214 |
+
int col) {
|
| 215 |
+
return shared_a[row * PYC_HOPPER_TC_SHARED_STRIDE_A + col];
|
| 216 |
+
}
|
| 217 |
+
|
| 218 |
+
__device__ static __forceinline__ void shared_a_store(
|
| 219 |
+
pyc_hopper_tc_scalar_t* shared_a,
|
| 220 |
+
int row,
|
| 221 |
+
int col,
|
| 222 |
+
pyc_hopper_tc_scalar_t value) {
|
| 223 |
+
shared_a[row * PYC_HOPPER_TC_SHARED_STRIDE_A + col] = value;
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
__device__ static __forceinline__ void shared_b_store(
|
| 227 |
+
pyc_hopper_tc_scalar_t* shared_b,
|
| 228 |
+
int row,
|
| 229 |
+
int col,
|
| 230 |
+
pyc_hopper_tc_scalar_t value) {
|
| 231 |
+
shared_b[row * PYC_HOPPER_TC_SHARED_STRIDE_B + col] = value;
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
__device__ static void load_a_stage(
|
| 235 |
+
const pyc_hopper_tc_scalar_t* __restrict__ a,
|
| 236 |
+
pyc_hopper_tc_scalar_t* shared_a,
|
| 237 |
+
int lane_linear,
|
| 238 |
+
int block_row,
|
| 239 |
+
int kk_base,
|
| 240 |
+
int m,
|
| 241 |
+
int k) {
|
| 242 |
+
const int block_threads = PYC_HOPPER_TC_THREADS_PER_BLOCK;
|
| 243 |
+
const int vecs_per_row = PYC_HOPPER_TC_TILE_K / PYC_HOPPER_TC_COPY_ELEMS;
|
| 244 |
+
const int total_vecs = (PYC_HOPPER_TC_TILE_M * PYC_HOPPER_TC_TILE_K) / PYC_HOPPER_TC_COPY_ELEMS;
|
| 245 |
+
const int full_tile = (block_row + PYC_HOPPER_TC_TILE_M <= m) &&
|
| 246 |
+
(kk_base + PYC_HOPPER_TC_TILE_K <= k) &&
|
| 247 |
+
((k & (PYC_HOPPER_TC_COPY_ELEMS - 1)) == 0);
|
| 248 |
+
int linear;
|
| 249 |
+
|
| 250 |
+
for (linear = lane_linear; linear < total_vecs; linear += block_threads) {
|
| 251 |
+
const int tile_row = linear / vecs_per_row;
|
| 252 |
+
const int tile_col = (linear % vecs_per_row) * PYC_HOPPER_TC_COPY_ELEMS;
|
| 253 |
+
const int global_row = block_row + tile_row;
|
| 254 |
+
const int global_col = kk_base + tile_col;
|
| 255 |
+
int i;
|
| 256 |
+
|
| 257 |
+
if (full_tile) {
|
| 258 |
+
async_copy_16(
|
| 259 |
+
&shared_a[tile_row * PYC_HOPPER_TC_SHARED_STRIDE_A + tile_col],
|
| 260 |
+
&a[global_row * k + global_col]);
|
| 261 |
+
continue;
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
for (i = 0; i < PYC_HOPPER_TC_COPY_ELEMS; ++i) {
|
| 265 |
+
pyc_hopper_tc_scalar_t value = pyc_hopper_tc_make_scalar(0.0f);
|
| 266 |
+
if (global_row < m && global_col + i < k) {
|
| 267 |
+
value = a[global_row * k + global_col + i];
|
| 268 |
+
}
|
| 269 |
+
shared_a_store(shared_a, tile_row, tile_col + i, value);
|
| 270 |
+
}
|
| 271 |
+
}
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
__device__ static void load_b_stage(
|
| 275 |
+
const pyc_hopper_tc_scalar_t* __restrict__ b,
|
| 276 |
+
pyc_hopper_tc_scalar_t* shared_b,
|
| 277 |
+
int lane_linear,
|
| 278 |
+
int block_col,
|
| 279 |
+
int kk_base,
|
| 280 |
+
int k,
|
| 281 |
+
int n) {
|
| 282 |
+
const int block_threads = PYC_HOPPER_TC_THREADS_PER_BLOCK;
|
| 283 |
+
const int vecs_per_row = PYC_HOPPER_TC_TILE_N / PYC_HOPPER_TC_COPY_ELEMS;
|
| 284 |
+
const int total_vecs = (PYC_HOPPER_TC_TILE_K * PYC_HOPPER_TC_TILE_N) / PYC_HOPPER_TC_COPY_ELEMS;
|
| 285 |
+
const int full_tile = (block_col + PYC_HOPPER_TC_TILE_N <= n) &&
|
| 286 |
+
(kk_base + PYC_HOPPER_TC_TILE_K <= k) &&
|
| 287 |
+
((n & (PYC_HOPPER_TC_COPY_ELEMS - 1)) == 0);
|
| 288 |
+
int linear;
|
| 289 |
+
|
| 290 |
+
for (linear = lane_linear; linear < total_vecs; linear += block_threads) {
|
| 291 |
+
const int tile_row = linear / vecs_per_row;
|
| 292 |
+
const int tile_col = (linear % vecs_per_row) * PYC_HOPPER_TC_COPY_ELEMS;
|
| 293 |
+
const int global_row = kk_base + tile_row;
|
| 294 |
+
const int global_col = block_col + tile_col;
|
| 295 |
+
int i;
|
| 296 |
+
|
| 297 |
+
if (full_tile) {
|
| 298 |
+
async_copy_16(
|
| 299 |
+
&shared_b[tile_row * PYC_HOPPER_TC_SHARED_STRIDE_B + tile_col],
|
| 300 |
+
&b[global_row * n + global_col]);
|
| 301 |
+
continue;
|
| 302 |
+
}
|
| 303 |
+
|
| 304 |
+
for (i = 0; i < PYC_HOPPER_TC_COPY_ELEMS; ++i) {
|
| 305 |
+
pyc_hopper_tc_scalar_t value = pyc_hopper_tc_make_scalar(0.0f);
|
| 306 |
+
if (global_row < k && global_col + i < n) {
|
| 307 |
+
value = b[global_row * n + global_col + i];
|
| 308 |
+
}
|
| 309 |
+
shared_b_store(shared_b, tile_row, tile_col + i, value);
|
| 310 |
+
}
|
| 311 |
+
}
|
| 312 |
+
}
|
| 313 |
+
|
| 314 |
+
__launch_bounds__(PYC_HOPPER_TC_THREADS_PER_BLOCK, 2)
|
| 315 |
+
__global__ void pyc_hopper_tc_async_gemm_kernel(
|
| 316 |
+
const pyc_hopper_tc_scalar_t* __restrict__ a,
|
| 317 |
+
const pyc_hopper_tc_scalar_t* __restrict__ b,
|
| 318 |
+
float* __restrict__ c,
|
| 319 |
+
int m,
|
| 320 |
+
int n,
|
| 321 |
+
int k) {
|
| 322 |
+
extern __shared__ __align__(16) pyc_hopper_tc_scalar_t shared_mem[];
|
| 323 |
+
|
| 324 |
+
const int lane_linear = threadIdx.x;
|
| 325 |
+
const int warp_id = threadIdx.x / 32;
|
| 326 |
+
const int block_row = blockIdx.y * PYC_HOPPER_TC_TILE_M;
|
| 327 |
+
const int block_col = blockIdx.x * PYC_HOPPER_TC_TILE_N;
|
| 328 |
+
const int warp_row_group = warp_id / PYC_HOPPER_TC_WARP_COL_GROUPS;
|
| 329 |
+
const int warp_col_group = warp_id % PYC_HOPPER_TC_WARP_COL_GROUPS;
|
| 330 |
+
const int warp_row = warp_row_group * PYC_HOPPER_TC_WARP_TILE_M;
|
| 331 |
+
const int warp_col = warp_col_group * PYC_HOPPER_TC_WARP_TILE_N;
|
| 332 |
+
wmma::fragment<wmma::accumulator, 16, 16, 16, float> acc[PYC_HOPPER_TC_WARP_ROW_TILES][PYC_HOPPER_TC_WARP_COL_TILES];
|
| 333 |
+
int stage = 0;
|
| 334 |
+
int kk_base;
|
| 335 |
+
|
| 336 |
+
if (warp_id >= PYC_HOPPER_TC_WARPS_PER_BLOCK) {
|
| 337 |
+
return;
|
| 338 |
+
}
|
| 339 |
+
|
| 340 |
+
for (int row_tile = 0; row_tile < PYC_HOPPER_TC_WARP_ROW_TILES; ++row_tile) {
|
| 341 |
+
for (int col_tile = 0; col_tile < PYC_HOPPER_TC_WARP_COL_TILES; ++col_tile) {
|
| 342 |
+
wmma::fill_fragment(acc[row_tile][col_tile], 0.0f);
|
| 343 |
+
}
|
| 344 |
+
}
|
| 345 |
+
|
| 346 |
+
load_a_stage(a, shared_stage_a(shared_mem, stage), lane_linear, block_row, 0, m, k);
|
| 347 |
+
load_b_stage(b, shared_stage_b(shared_mem, stage), lane_linear, block_col, 0, k, n);
|
| 348 |
+
async_commit();
|
| 349 |
+
async_wait();
|
| 350 |
+
__syncthreads();
|
| 351 |
+
|
| 352 |
+
for (kk_base = 0; kk_base < k; kk_base += PYC_HOPPER_TC_TILE_K) {
|
| 353 |
+
const int next_kk = kk_base + PYC_HOPPER_TC_TILE_K;
|
| 354 |
+
const int next_stage = stage ^ 1;
|
| 355 |
+
|
| 356 |
+
if (next_kk < k) {
|
| 357 |
+
load_a_stage(a, shared_stage_a(shared_mem, next_stage), lane_linear, block_row, next_kk, m, k);
|
| 358 |
+
load_b_stage(b, shared_stage_b(shared_mem, next_stage), lane_linear, block_col, next_kk, k, n);
|
| 359 |
+
async_commit();
|
| 360 |
+
}
|
| 361 |
+
|
| 362 |
+
for (int k_frag = 0; k_frag < PYC_HOPPER_TC_TILE_K; k_frag += PYC_HOPPER_TC_MMA_TILE_K) {
|
| 363 |
+
wmma::fragment<wmma::matrix_a, 16, 16, 16, pyc_hopper_tc_scalar_t, wmma::row_major> a_frag[PYC_HOPPER_TC_WARP_ROW_TILES];
|
| 364 |
+
wmma::fragment<wmma::matrix_b, 16, 16, 16, pyc_hopper_tc_scalar_t, wmma::row_major> b_frag[PYC_HOPPER_TC_WARP_COL_TILES];
|
| 365 |
+
|
| 366 |
+
for (int row_tile = 0; row_tile < PYC_HOPPER_TC_WARP_ROW_TILES; ++row_tile) {
|
| 367 |
+
const int a_row = warp_row + row_tile * PYC_HOPPER_TC_MMA_TILE_M;
|
| 368 |
+
wmma::load_matrix_sync(
|
| 369 |
+
a_frag[row_tile],
|
| 370 |
+
&shared_stage_a(shared_mem, stage)[a_row * PYC_HOPPER_TC_SHARED_STRIDE_A + k_frag],
|
| 371 |
+
PYC_HOPPER_TC_SHARED_STRIDE_A);
|
| 372 |
+
}
|
| 373 |
+
|
| 374 |
+
for (int col_tile = 0; col_tile < PYC_HOPPER_TC_WARP_COL_TILES; ++col_tile) {
|
| 375 |
+
const int b_col = warp_col + col_tile * PYC_HOPPER_TC_MMA_TILE_N;
|
| 376 |
+
wmma::load_matrix_sync(
|
| 377 |
+
b_frag[col_tile],
|
| 378 |
+
&shared_stage_b(shared_mem, stage)[k_frag * PYC_HOPPER_TC_SHARED_STRIDE_B + b_col],
|
| 379 |
+
PYC_HOPPER_TC_SHARED_STRIDE_B);
|
| 380 |
+
}
|
| 381 |
+
|
| 382 |
+
for (int row_tile = 0; row_tile < PYC_HOPPER_TC_WARP_ROW_TILES; ++row_tile) {
|
| 383 |
+
for (int col_tile = 0; col_tile < PYC_HOPPER_TC_WARP_COL_TILES; ++col_tile) {
|
| 384 |
+
wmma::mma_sync(acc[row_tile][col_tile], a_frag[row_tile], b_frag[col_tile], acc[row_tile][col_tile]);
|
| 385 |
+
}
|
| 386 |
+
}
|
| 387 |
+
}
|
| 388 |
+
|
| 389 |
+
if (next_kk < k) {
|
| 390 |
+
async_wait();
|
| 391 |
+
__syncthreads();
|
| 392 |
+
stage = next_stage;
|
| 393 |
+
}
|
| 394 |
+
}
|
| 395 |
+
|
| 396 |
+
for (int row_tile = 0; row_tile < PYC_HOPPER_TC_WARP_ROW_TILES; ++row_tile) {
|
| 397 |
+
const int c_row = block_row + warp_row + row_tile * PYC_HOPPER_TC_MMA_TILE_M;
|
| 398 |
+
for (int col_tile = 0; col_tile < PYC_HOPPER_TC_WARP_COL_TILES; ++col_tile) {
|
| 399 |
+
const int c_col = block_col + warp_col + col_tile * PYC_HOPPER_TC_MMA_TILE_N;
|
| 400 |
+
if (c_row < m && c_col < n) {
|
| 401 |
+
wmma::store_matrix_sync(&c[c_row * n + c_col], acc[row_tile][col_tile], n, wmma::mem_row_major);
|
| 402 |
+
}
|
| 403 |
+
}
|
| 404 |
+
}
|
| 405 |
+
}
|
| 406 |
+
|
| 407 |
+
static int set_kernel_attributes(void) {
|
| 408 |
+
cudaError_t status;
|
| 409 |
+
|
| 410 |
+
status = cudaFuncSetAttribute(
|
| 411 |
+
pyc_hopper_tc_async_gemm_kernel,
|
| 412 |
+
cudaFuncAttributeMaxDynamicSharedMemorySize,
|
| 413 |
+
PYC_HOPPER_TC_SHARED_BYTES);
|
| 414 |
+
if (status != cudaSuccess && status != cudaErrorNotSupported) {
|
| 415 |
+
fprintf(stderr, "cudaFuncSetAttribute(max_dynamic_shared) failed: %s\n", cudaGetErrorString(status));
|
| 416 |
+
return -1;
|
| 417 |
+
}
|
| 418 |
+
|
| 419 |
+
status = cudaFuncSetAttribute(
|
| 420 |
+
pyc_hopper_tc_async_gemm_kernel,
|
| 421 |
+
cudaFuncAttributePreferredSharedMemoryCarveout,
|
| 422 |
+
100);
|
| 423 |
+
if (status != cudaSuccess && status != cudaErrorNotSupported) {
|
| 424 |
+
fprintf(stderr, "cudaFuncSetAttribute failed: %s\n", cudaGetErrorString(status));
|
| 425 |
+
return -1;
|
| 426 |
+
}
|
| 427 |
+
|
| 428 |
+
return 0;
|
| 429 |
+
}
|
| 430 |
+
|
| 431 |
+
static int parse_config(int argc, char** argv, pyc_hopper_tc_config* cfg) {
|
| 432 |
+
if (!cfg) {
|
| 433 |
+
return -1;
|
| 434 |
+
}
|
| 435 |
+
|
| 436 |
+
cfg->m = 1024;
|
| 437 |
+
cfg->n = 1024;
|
| 438 |
+
cfg->k = 1024;
|
| 439 |
+
cfg->warmup = 5;
|
| 440 |
+
cfg->iters = 20;
|
| 441 |
+
cfg->skip_reference = env_flag("PYC_HOPPER_TC_SKIP_REFERENCE", 0);
|
| 442 |
+
|
| 443 |
+
if (argc > 1 && parse_int_arg(argv[1], &cfg->m) != 0) return -1;
|
| 444 |
+
if (argc > 2 && parse_int_arg(argv[2], &cfg->n) != 0) return -1;
|
| 445 |
+
if (argc > 3 && parse_int_arg(argv[3], &cfg->k) != 0) return -1;
|
| 446 |
+
if (argc > 4 && parse_int_arg(argv[4], &cfg->warmup) != 0) return -1;
|
| 447 |
+
if (argc > 5 && parse_int_arg(argv[5], &cfg->iters) != 0) return -1;
|
| 448 |
+
if (argc > 6 && parse_int_arg(argv[6], &cfg->skip_reference) != 0) return -1;
|
| 449 |
+
|
| 450 |
+
return 0;
|
| 451 |
+
}
|
| 452 |
+
|
| 453 |
+
int main(int argc, char** argv) {
|
| 454 |
+
pyc_hopper_tc_config cfg;
|
| 455 |
+
struct cudaDeviceProp props;
|
| 456 |
+
pyc_hopper_tc_scalar_t* host_a = NULL;
|
| 457 |
+
pyc_hopper_tc_scalar_t* host_b = NULL;
|
| 458 |
+
float* host_c = NULL;
|
| 459 |
+
float* ref_c = NULL;
|
| 460 |
+
pyc_hopper_tc_scalar_t* dev_a = NULL;
|
| 461 |
+
pyc_hopper_tc_scalar_t* dev_b = NULL;
|
| 462 |
+
float* dev_c = NULL;
|
| 463 |
+
cudaEvent_t start = NULL;
|
| 464 |
+
cudaEvent_t stop = NULL;
|
| 465 |
+
size_t a_bytes;
|
| 466 |
+
size_t b_bytes;
|
| 467 |
+
size_t c_bytes;
|
| 468 |
+
dim3 block;
|
| 469 |
+
dim3 grid;
|
| 470 |
+
float elapsed_ms = 0.0f;
|
| 471 |
+
double best_ms = 0.0;
|
| 472 |
+
double max_abs_diff = 0.0;
|
| 473 |
+
int iter;
|
| 474 |
+
|
| 475 |
+
if (parse_config(argc, argv, &cfg) != 0) {
|
| 476 |
+
fprintf(stderr, "usage: %s [m] [n] [k] [warmup] [iters] [skip_reference]\n", argv[0]);
|
| 477 |
+
return 2;
|
| 478 |
+
}
|
| 479 |
+
|
| 480 |
+
if (
|
| 481 |
+
(cfg.m % PYC_HOPPER_TC_TILE_M) != 0 || (cfg.n % PYC_HOPPER_TC_TILE_N) != 0
|
| 482 |
+
|| (cfg.k % PYC_HOPPER_TC_TILE_K) != 0) {
|
| 483 |
+
fprintf(
|
| 484 |
+
stderr,
|
| 485 |
+
"Hopper Tensor Core async lane requires %dx%dx%d-aligned shapes\n",
|
| 486 |
+
PYC_HOPPER_TC_TILE_M,
|
| 487 |
+
PYC_HOPPER_TC_TILE_N,
|
| 488 |
+
PYC_HOPPER_TC_TILE_K);
|
| 489 |
+
return 2;
|
| 490 |
+
}
|
| 491 |
+
|
| 492 |
+
if (check_cuda(cudaGetDeviceProperties(&props, 0), "cudaGetDeviceProperties") != 0) {
|
| 493 |
+
return 1;
|
| 494 |
+
}
|
| 495 |
+
if (props.major < 9) {
|
| 496 |
+
fprintf(stderr, "Hopper Tensor Core async prototype requires sm_90-class hardware\n");
|
| 497 |
+
return 1;
|
| 498 |
+
}
|
| 499 |
+
|
| 500 |
+
a_bytes = (size_t)cfg.m * (size_t)cfg.k * sizeof(pyc_hopper_tc_scalar_t);
|
| 501 |
+
b_bytes = (size_t)cfg.k * (size_t)cfg.n * sizeof(pyc_hopper_tc_scalar_t);
|
| 502 |
+
c_bytes = (size_t)cfg.m * (size_t)cfg.n * sizeof(float);
|
| 503 |
+
|
| 504 |
+
host_a = (pyc_hopper_tc_scalar_t*)malloc(a_bytes);
|
| 505 |
+
host_b = (pyc_hopper_tc_scalar_t*)malloc(b_bytes);
|
| 506 |
+
if (!host_a || !host_b) {
|
| 507 |
+
fprintf(stderr, "host allocation failed\n");
|
| 508 |
+
return 1;
|
| 509 |
+
}
|
| 510 |
+
if (!cfg.skip_reference) {
|
| 511 |
+
host_c = (float*)malloc(c_bytes);
|
| 512 |
+
ref_c = (float*)malloc(c_bytes);
|
| 513 |
+
if (!host_c || !ref_c) {
|
| 514 |
+
fprintf(stderr, "host validation allocation failed\n");
|
| 515 |
+
return 1;
|
| 516 |
+
}
|
| 517 |
+
}
|
| 518 |
+
|
| 519 |
+
fill_matrix(host_a, cfg.m, cfg.k, 0.03125f);
|
| 520 |
+
fill_matrix(host_b, cfg.k, cfg.n, 0.0625f);
|
| 521 |
+
if (!cfg.skip_reference) {
|
| 522 |
+
reference_gemm(host_a, host_b, ref_c, cfg.m, cfg.n, cfg.k);
|
| 523 |
+
}
|
| 524 |
+
|
| 525 |
+
if (check_cuda(cudaMalloc((void**)&dev_a, a_bytes), "cudaMalloc(a)") != 0) return 1;
|
| 526 |
+
if (check_cuda(cudaMalloc((void**)&dev_b, b_bytes), "cudaMalloc(b)") != 0) return 1;
|
| 527 |
+
if (check_cuda(cudaMalloc((void**)&dev_c, c_bytes), "cudaMalloc(c)") != 0) return 1;
|
| 528 |
+
|
| 529 |
+
if (check_cuda(cudaMemcpy(dev_a, host_a, a_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(a)") != 0) return 1;
|
| 530 |
+
if (check_cuda(cudaMemcpy(dev_b, host_b, b_bytes, cudaMemcpyHostToDevice), "cudaMemcpy(b)") != 0) return 1;
|
| 531 |
+
|
| 532 |
+
if (set_kernel_attributes() != 0) return 1;
|
| 533 |
+
|
| 534 |
+
block = dim3(PYC_HOPPER_TC_THREADS_PER_BLOCK, 1, 1);
|
| 535 |
+
grid = dim3(
|
| 536 |
+
(unsigned int)((cfg.n + PYC_HOPPER_TC_TILE_N - 1) / PYC_HOPPER_TC_TILE_N),
|
| 537 |
+
(unsigned int)((cfg.m + PYC_HOPPER_TC_TILE_M - 1) / PYC_HOPPER_TC_TILE_M),
|
| 538 |
+
1);
|
| 539 |
+
|
| 540 |
+
if (check_cuda(cudaEventCreate(&start), "cudaEventCreate(start)") != 0) return 1;
|
| 541 |
+
if (check_cuda(cudaEventCreate(&stop), "cudaEventCreate(stop)") != 0) return 1;
|
| 542 |
+
|
| 543 |
+
for (iter = 0; iter < cfg.warmup; ++iter) {
|
| 544 |
+
pyc_hopper_tc_async_gemm_kernel<<<grid, block, PYC_HOPPER_TC_SHARED_BYTES>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 545 |
+
}
|
| 546 |
+
if (check_cuda(cudaGetLastError(), "kernel launch warmup") != 0) return 1;
|
| 547 |
+
if (check_cuda(cudaDeviceSynchronize(), "cudaDeviceSynchronize warmup") != 0) return 1;
|
| 548 |
+
|
| 549 |
+
best_ms = 0.0;
|
| 550 |
+
for (iter = 0; iter < cfg.iters; ++iter) {
|
| 551 |
+
if (check_cuda(cudaEventRecord(start), "cudaEventRecord(start)") != 0) return 1;
|
| 552 |
+
pyc_hopper_tc_async_gemm_kernel<<<grid, block, PYC_HOPPER_TC_SHARED_BYTES>>>(dev_a, dev_b, dev_c, cfg.m, cfg.n, cfg.k);
|
| 553 |
+
if (check_cuda(cudaEventRecord(stop), "cudaEventRecord(stop)") != 0) return 1;
|
| 554 |
+
if (check_cuda(cudaEventSynchronize(stop), "cudaEventSynchronize(stop)") != 0) return 1;
|
| 555 |
+
if (check_cuda(cudaEventElapsedTime(&elapsed_ms, start, stop), "cudaEventElapsedTime") != 0) return 1;
|
| 556 |
+
if (iter == 0 || elapsed_ms < (float)best_ms) {
|
| 557 |
+
best_ms = elapsed_ms;
|
| 558 |
+
}
|
| 559 |
+
}
|
| 560 |
+
|
| 561 |
+
if (!cfg.skip_reference) {
|
| 562 |
+
if (check_cuda(cudaMemcpy(host_c, dev_c, c_bytes, cudaMemcpyDeviceToHost), "cudaMemcpy(c)") != 0) return 1;
|
| 563 |
+
for (iter = 0; iter < cfg.m * cfg.n; ++iter) {
|
| 564 |
+
double diff = fabs((double)host_c[iter] - (double)ref_c[iter]);
|
| 565 |
+
if (diff > max_abs_diff) {
|
| 566 |
+
max_abs_diff = diff;
|
| 567 |
+
}
|
| 568 |
+
}
|
| 569 |
+
}
|
| 570 |
+
|
| 571 |
+
printf("kernel=hopper_tensor_core_async\n");
|
| 572 |
+
printf("lane=%s\n", PYC_HOPPER_TC_LANE_NAME);
|
| 573 |
+
printf("arch=sm%d%d\n", props.major, props.minor);
|
| 574 |
+
printf("shape=%dx%dx%d\n", cfg.m, cfg.n, cfg.k);
|
| 575 |
+
printf(
|
| 576 |
+
"tile=%dx%dx%d warp_tile=%dx%d warp_groups=%dx%d pads=%dx%d warps=%d threads=%d stages=%d shared_bytes=%d\n",
|
| 577 |
+
PYC_HOPPER_TC_TILE_M,
|
| 578 |
+
PYC_HOPPER_TC_TILE_N,
|
| 579 |
+
PYC_HOPPER_TC_TILE_K,
|
| 580 |
+
PYC_HOPPER_TC_WARP_TILE_M,
|
| 581 |
+
PYC_HOPPER_TC_WARP_TILE_N,
|
| 582 |
+
PYC_HOPPER_TC_WARP_ROW_GROUPS,
|
| 583 |
+
PYC_HOPPER_TC_WARP_COL_GROUPS,
|
| 584 |
+
PYC_HOPPER_TC_SHARED_PAD_A,
|
| 585 |
+
PYC_HOPPER_TC_SHARED_PAD_B,
|
| 586 |
+
PYC_HOPPER_TC_WARPS_PER_BLOCK,
|
| 587 |
+
PYC_HOPPER_TC_THREADS_PER_BLOCK,
|
| 588 |
+
PYC_HOPPER_TC_STAGES,
|
| 589 |
+
PYC_HOPPER_TC_SHARED_BYTES);
|
| 590 |
+
printf("skip_reference=%d\n", cfg.skip_reference);
|
| 591 |
+
printf("best_ms=%.3f\n", best_ms);
|
| 592 |
+
if (!cfg.skip_reference) {
|
| 593 |
+
printf("max_abs_diff=%.6f\n", max_abs_diff);
|
| 594 |
+
}
|
| 595 |
+
if (best_ms > 0.0) {
|
| 596 |
+
double flops = 2.0 * (double)cfg.m * (double)cfg.n * (double)cfg.k;
|
| 597 |
+
double gflops = flops / (best_ms * 1.0e6);
|
| 598 |
+
printf("gflops=%.3f\n", gflops);
|
| 599 |
+
printf("tflops=%.3f\n", gflops / 1000.0);
|
| 600 |
+
}
|
| 601 |
+
|
| 602 |
+
cudaEventDestroy(start);
|
| 603 |
+
cudaEventDestroy(stop);
|
| 604 |
+
cudaFree(dev_a);
|
| 605 |
+
cudaFree(dev_b);
|
| 606 |
+
cudaFree(dev_c);
|
| 607 |
+
free(host_a);
|
| 608 |
+
free(host_b);
|
| 609 |
+
free(host_c);
|
| 610 |
+
free(ref_c);
|
| 611 |
+
return (cfg.skip_reference || max_abs_diff <= 0.2) ? 0 : 1;
|
| 612 |
+
}
|
kernels/prototypes/hopper/tensor_core_wgmma/README.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Hopper Tensor Core WGMMA Lane
|
| 2 |
+
|
| 3 |
+
This directory is reserved for the Hopper-native gap-closing kernel lane.
|
| 4 |
+
|
| 5 |
+
Current state:
|
| 6 |
+
|
| 7 |
+
- `tensor_core_async` is the owned WMMA guardrail lane.
|
| 8 |
+
- `cuBLASLt` is the control ceiling lane.
|
| 9 |
+
- `tensor_core_wgmma` is the next implementation lane intended to close the remaining device-side gap.
|
| 10 |
+
|
| 11 |
+
Minimum contract for the first implementation:
|
| 12 |
+
|
| 13 |
+
- target shape: `4096x4096x4096`
|
| 14 |
+
- dtype: BF16 input, FP32 accumulation
|
| 15 |
+
- architecture: `sm90`
|
| 16 |
+
- correctness lane: `512x512x512` with reference enabled
|
| 17 |
+
- performance lane: `4096x4096x4096` with reference disabled
|
| 18 |
+
|
| 19 |
+
Design goals:
|
| 20 |
+
|
| 21 |
+
- use warpgroup MMA rather than per-warp WMMA
|
| 22 |
+
- move toward TMA-backed staging for the long-term feed path
|
| 23 |
+
- preserve a standalone harness so the lane can be benchmarked by `kernel_lab`
|
| 24 |
+
- keep the first fast path simple: no generalized epilogue work unless it is proven necessary
|
| 25 |
+
|
| 26 |
+
Promotion rule:
|
| 27 |
+
|
| 28 |
+
- only promote a `tensor_core_wgmma` variant if it beats the current owned SM90 baseline without correctness regressions
|
manifests/lab_kernels.json
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"kernels": [
|
| 3 |
+
{
|
| 4 |
+
"name": "matrix_mult",
|
| 5 |
+
"source": "kernels/prototypes/baseline/matmul/kernel.cu",
|
| 6 |
+
"description": "CUDA matrix multiplication kernel (compile-only baseline).",
|
| 7 |
+
"tags": ["cuda", "matmul"],
|
| 8 |
+
"compile_cmd": "{nvcc} -O3 -c {source} -o {build_dir}/{name}.o",
|
| 9 |
+
"run_cmd": ""
|
| 10 |
+
},
|
| 11 |
+
{
|
| 12 |
+
"name": "tokenizer_kernel",
|
| 13 |
+
"source": "kernels/prototypes/experimental/tokenizer_matmul/kernel.cu",
|
| 14 |
+
"description": "Experimental tokenizer/matrix CUDA kernel file.",
|
| 15 |
+
"tags": ["cuda", "tokenizer", "experimental"],
|
| 16 |
+
"compile_cmd": "{nvcc} -O2 -c {source} -o {build_dir}/{name}.o",
|
| 17 |
+
"run_cmd": ""
|
| 18 |
+
},
|
| 19 |
+
{
|
| 20 |
+
"name": "ada_gemm",
|
| 21 |
+
"source": "kernels/prototypes/ada/gemm/kernel.cu",
|
| 22 |
+
"description": "Ada-focused FP32 shared-memory GEMM prototype with standalone correctness and timing harness.",
|
| 23 |
+
"tags": ["cuda", "matmul", "ada", "sm89", "prototype"],
|
| 24 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -gencode arch=compute_89,code=sm_89 -gencode arch=compute_89,code=compute_89 {source} -o {build_dir}/{name}",
|
| 25 |
+
"run_cmd": "{build_dir}/{name} 1024 1024 1024 10 50"
|
| 26 |
+
},
|
| 27 |
+
{
|
| 28 |
+
"name": "ada_gemm_k64_warp32_async",
|
| 29 |
+
"source": "kernels/prototypes/ada/gemm_k64_warp32_async/kernel.cu",
|
| 30 |
+
"description": "Ada FP32 GEMM winner with 64x64x64 tiles, 32x8 threads, and cp.async double-buffered shared-memory stages.",
|
| 31 |
+
"tags": ["cuda", "matmul", "ada", "sm89", "prototype", "fp32", "vectorized", "winner", "k64", "warp32", "async", "cpasync"],
|
| 32 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -gencode arch=compute_89,code=sm_89 -gencode arch=compute_89,code=compute_89 {source} -o {build_dir}/{name}",
|
| 33 |
+
"run_cmd": "{build_dir}/{name} 1024 1024 1024 10 50"
|
| 34 |
+
},
|
| 35 |
+
{
|
| 36 |
+
"name": "ada_tensor_core_fp16",
|
| 37 |
+
"source": "kernels/prototypes/ada/tensor_core/kernel.cu",
|
| 38 |
+
"description": "Ada Tensor Core FP16 GEMM prototype with WMMA-backed 32x64x16 correctness and timing harness.",
|
| 39 |
+
"tags": ["cuda", "matmul", "ada", "sm89", "prototype", "tensor-core", "fp16"],
|
| 40 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -DPYC_ADA_TENSOR_CORE_USE_BF16=0 -gencode arch=compute_89,code=sm_89 -gencode arch=compute_89,code=compute_89 {source} -o {build_dir}/{name}",
|
| 41 |
+
"run_cmd": "{build_dir}/{name} 1024 1024 1024 10 50"
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
"name": "ada_tensor_core_bf16",
|
| 45 |
+
"source": "kernels/prototypes/ada/tensor_core/kernel.cu",
|
| 46 |
+
"description": "Ada Tensor Core BF16 GEMM prototype with WMMA-backed 32x64x16 correctness and timing harness.",
|
| 47 |
+
"tags": ["cuda", "matmul", "ada", "sm89", "prototype", "tensor-core", "bf16"],
|
| 48 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -DPYC_ADA_TENSOR_CORE_USE_BF16=1 -gencode arch=compute_89,code=sm_89 -gencode arch=compute_89,code=compute_89 {source} -o {build_dir}/{name}",
|
| 49 |
+
"run_cmd": "{build_dir}/{name} 1024 1024 1024 10 50"
|
| 50 |
+
},
|
| 51 |
+
{
|
| 52 |
+
"name": "hopper_tensor_core_fp16",
|
| 53 |
+
"source": "kernels/prototypes/hopper/tensor_core/kernel.cu",
|
| 54 |
+
"description": "Hopper Tensor Core FP16 GEMM prototype with a 64x64x16 WMMA tile and larger per-block warpgroup-style work.",
|
| 55 |
+
"tags": ["cuda", "matmul", "hopper", "sm90", "prototype", "tensor-core", "fp16"],
|
| 56 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -DPYC_HOPPER_TENSOR_CORE_USE_BF16=0 -gencode arch=compute_90,code=sm_90 -gencode arch=compute_90,code=compute_90 {source} -o {build_dir}/{name}",
|
| 57 |
+
"run_cmd": "{build_dir}/{name} 1024 1024 1024 5 20"
|
| 58 |
+
},
|
| 59 |
+
{
|
| 60 |
+
"name": "hopper_tensor_core_bf16",
|
| 61 |
+
"source": "kernels/prototypes/hopper/tensor_core/kernel.cu",
|
| 62 |
+
"description": "Hopper Tensor Core BF16 GEMM prototype with a 64x64x16 WMMA tile and larger per-block warpgroup-style work.",
|
| 63 |
+
"tags": ["cuda", "matmul", "hopper", "sm90", "prototype", "tensor-core", "bf16"],
|
| 64 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -DPYC_HOPPER_TENSOR_CORE_USE_BF16=1 -gencode arch=compute_90,code=sm_90 -gencode arch=compute_90,code=compute_90 {source} -o {build_dir}/{name}",
|
| 65 |
+
"run_cmd": "{build_dir}/{name} 1024 1024 1024 5 20"
|
| 66 |
+
},
|
| 67 |
+
{
|
| 68 |
+
"name": "hopper_tensor_core_bf16_perf",
|
| 69 |
+
"source": "kernels/prototypes/hopper/tensor_core/kernel.cu",
|
| 70 |
+
"description": "Hopper BF16 baseline perf lane at 4096^3 with reference disabled to isolate steady-state Tensor Core throughput.",
|
| 71 |
+
"tags": ["cuda", "matmul", "hopper", "sm90", "prototype", "tensor-core", "bf16", "perf", "next-loop"],
|
| 72 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -DPYC_HOPPER_TENSOR_CORE_USE_BF16=1 -gencode arch=compute_90,code=sm_90 -gencode arch=compute_90,code=compute_90 {source} -o {build_dir}/{name}",
|
| 73 |
+
"run_cmd": "{build_dir}/{name} 4096 4096 4096 3 30 1"
|
| 74 |
+
},
|
| 75 |
+
{
|
| 76 |
+
"name": "hopper_tensor_core_bf16_warp2n",
|
| 77 |
+
"source": "kernels/prototypes/hopper/tensor_core/kernel.cu",
|
| 78 |
+
"description": "Hopper BF16 perf lane with 8 warps per CTA and each warp computing two N fragments to increase work per warp.",
|
| 79 |
+
"tags": ["cuda", "matmul", "hopper", "sm90", "prototype", "tensor-core", "bf16", "perf", "next-loop", "warp2n"],
|
| 80 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -DPYC_HOPPER_TENSOR_CORE_USE_BF16=1 -DPYC_HOPPER_TC_WARP_COL_TILES=2 -DPYC_HOPPER_TC_WARP_ROW_GROUPS=4 -DPYC_HOPPER_TC_WARP_COL_GROUPS=2 -DPYC_HOPPER_TC_SHARED_PAD_A=8 -DPYC_HOPPER_TC_SHARED_PAD_B=8 -gencode arch=compute_90,code=sm_90 -gencode arch=compute_90,code=compute_90 {source} -o {build_dir}/{name}",
|
| 81 |
+
"run_cmd": "{build_dir}/{name} 4096 4096 4096 3 30 1"
|
| 82 |
+
},
|
| 83 |
+
{
|
| 84 |
+
"name": "hopper_tensor_core_bf16_k32",
|
| 85 |
+
"source": "kernels/prototypes/hopper/tensor_core/kernel.cu",
|
| 86 |
+
"description": "Hopper BF16 perf lane with a 64x64x32 shared-memory stage to cut synchronization frequency across K.",
|
| 87 |
+
"tags": ["cuda", "matmul", "hopper", "sm90", "prototype", "tensor-core", "bf16", "perf", "next-loop", "k32"],
|
| 88 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -DPYC_HOPPER_TENSOR_CORE_USE_BF16=1 -DPYC_HOPPER_TC_TILE_K=32 -DPYC_HOPPER_TC_SHARED_PAD_A=8 -DPYC_HOPPER_TC_SHARED_PAD_B=8 -gencode arch=compute_90,code=sm_90 -gencode arch=compute_90,code=compute_90 {source} -o {build_dir}/{name}",
|
| 89 |
+
"run_cmd": "{build_dir}/{name} 4096 4096 4096 3 30 1"
|
| 90 |
+
},
|
| 91 |
+
{
|
| 92 |
+
"name": "hopper_tensor_core_bf16_warp2n_k32",
|
| 93 |
+
"source": "kernels/prototypes/hopper/tensor_core/kernel.cu",
|
| 94 |
+
"description": "Hopper BF16 perf lane combining 8-warp CTAs, two N fragments per warp, padded shared-memory strides, and 32-wide K staging.",
|
| 95 |
+
"tags": ["cuda", "matmul", "hopper", "sm90", "prototype", "tensor-core", "bf16", "perf", "next-loop", "warp2n", "k32"],
|
| 96 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -DPYC_HOPPER_TENSOR_CORE_USE_BF16=1 -DPYC_HOPPER_TC_WARP_COL_TILES=2 -DPYC_HOPPER_TC_WARP_ROW_GROUPS=4 -DPYC_HOPPER_TC_WARP_COL_GROUPS=2 -DPYC_HOPPER_TC_TILE_K=32 -DPYC_HOPPER_TC_SHARED_PAD_A=8 -DPYC_HOPPER_TC_SHARED_PAD_B=8 -gencode arch=compute_90,code=sm_90 -gencode arch=compute_90,code=compute_90 {source} -o {build_dir}/{name}",
|
| 97 |
+
"run_cmd": "{build_dir}/{name} 4096 4096 4096 3 30 1"
|
| 98 |
+
},
|
| 99 |
+
{
|
| 100 |
+
"name": "hopper_cublaslt_bf16",
|
| 101 |
+
"source": "kernels/prototypes/hopper/cublaslt_bf16/kernel.cu",
|
| 102 |
+
"description": "Hopper cuBLASLt BF16 GEMM control lane for measuring the hardware-native library ceiling before a custom TMA/WGMMA implementation lands.",
|
| 103 |
+
"tags": ["cuda", "matmul", "hopper", "sm90", "prototype", "cublaslt", "bf16", "control", "ceiling"],
|
| 104 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -gencode arch=compute_90,code=sm_90 -gencode arch=compute_90,code=compute_90 {source} -lcublasLt -lcublas -o {build_dir}/{name}",
|
| 105 |
+
"run_cmd": "{build_dir}/{name} 4096 4096 4096 3 30 1"
|
| 106 |
+
},
|
| 107 |
+
{
|
| 108 |
+
"name": "hopper_cublaslt_bf16_check",
|
| 109 |
+
"source": "kernels/prototypes/hopper/cublaslt_bf16/kernel.cu",
|
| 110 |
+
"description": "Smaller Hopper cuBLASLt BF16 correctness lane with reference enabled.",
|
| 111 |
+
"tags": ["cuda", "matmul", "hopper", "sm90", "prototype", "cublaslt", "bf16", "correctness"],
|
| 112 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -gencode arch=compute_90,code=sm_90 -gencode arch=compute_90,code=compute_90 {source} -lcublasLt -lcublas -o {build_dir}/{name}",
|
| 113 |
+
"run_cmd": "{build_dir}/{name} 512 512 512 2 10 0"
|
| 114 |
+
},
|
| 115 |
+
{
|
| 116 |
+
"name": "hopper_tensor_core_bf16_async",
|
| 117 |
+
"source": "kernels/prototypes/hopper/tensor_core_async/kernel.cu",
|
| 118 |
+
"description": "Hopper BF16 async WMMA lane with cp.async double-buffered staging, 64x64x32 tiles, and four fragments per warp to cut exposed feed bubbles.",
|
| 119 |
+
"tags": ["cuda", "matmul", "hopper", "sm90", "prototype", "tensor-core", "bf16", "async", "cpasync", "double-buffered"],
|
| 120 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -DPYC_HOPPER_TENSOR_CORE_USE_BF16=1 -gencode arch=compute_90,code=sm_90 -gencode arch=compute_90,code=compute_90 {source} -o {build_dir}/{name}",
|
| 121 |
+
"run_cmd": "{build_dir}/{name} 4096 4096 4096 3 30 1"
|
| 122 |
+
},
|
| 123 |
+
{
|
| 124 |
+
"name": "hopper_tensor_core_bf16_async_wide",
|
| 125 |
+
"source": "kernels/prototypes/hopper/tensor_core_async/kernel.cu",
|
| 126 |
+
"description": "Hopper BF16 async WMMA lane widened to a 64x128x32 CTA so each stage amortizes more scheduling overhead before the WGMMA/TMA path lands.",
|
| 127 |
+
"tags": ["cuda", "matmul", "hopper", "sm90", "prototype", "tensor-core", "bf16", "async", "cpasync", "double-buffered", "wide"],
|
| 128 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -DPYC_HOPPER_TENSOR_CORE_USE_BF16=1 -DPYC_HOPPER_TC_WARP_COL_GROUPS=4 -gencode arch=compute_90,code=sm_90 -gencode arch=compute_90,code=compute_90 {source} -o {build_dir}/{name}",
|
| 129 |
+
"run_cmd": "{build_dir}/{name} 4096 4096 4096 3 30 1"
|
| 130 |
+
},
|
| 131 |
+
{
|
| 132 |
+
"name": "hopper_tensor_core_bf16_async_wide_k64",
|
| 133 |
+
"source": "kernels/prototypes/hopper/tensor_core_async/kernel.cu",
|
| 134 |
+
"description": "Hopper BF16 async WMMA lane keeping the 64x128 CTA but doubling stage depth to 64 so each stage amortizes more feed and loop overhead.",
|
| 135 |
+
"tags": ["cuda", "matmul", "hopper", "sm90", "prototype", "tensor-core", "bf16", "async", "cpasync", "double-buffered", "wide", "k64", "gap-close"],
|
| 136 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -DPYC_HOPPER_TENSOR_CORE_USE_BF16=1 -DPYC_HOPPER_TC_WARP_COL_GROUPS=4 -DPYC_HOPPER_TC_TILE_K=64 -gencode arch=compute_90,code=sm_90 -gencode arch=compute_90,code=compute_90 {source} -o {build_dir}/{name}",
|
| 137 |
+
"run_cmd": "{build_dir}/{name} 4096 4096 4096 3 30 1"
|
| 138 |
+
},
|
| 139 |
+
{
|
| 140 |
+
"name": "hopper_tensor_core_bf16_async_square",
|
| 141 |
+
"source": "kernels/prototypes/hopper/tensor_core_async/kernel.cu",
|
| 142 |
+
"description": "Hopper BF16 async WMMA lane widened into a 128x128x32 CTA so the square 4096^3 regime gets more work per CTA before switching stages.",
|
| 143 |
+
"tags": ["cuda", "matmul", "hopper", "sm90", "prototype", "tensor-core", "bf16", "async", "cpasync", "double-buffered", "square", "gap-close"],
|
| 144 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -DPYC_HOPPER_TENSOR_CORE_USE_BF16=1 -DPYC_HOPPER_TC_WARP_ROW_GROUPS=4 -DPYC_HOPPER_TC_WARP_COL_GROUPS=4 -gencode arch=compute_90,code=sm_90 -gencode arch=compute_90,code=compute_90 {source} -o {build_dir}/{name}",
|
| 145 |
+
"run_cmd": "{build_dir}/{name} 4096 4096 4096 3 30 1"
|
| 146 |
+
},
|
| 147 |
+
{
|
| 148 |
+
"name": "hopper_tensor_core_bf16_async_square_k64",
|
| 149 |
+
"source": "kernels/prototypes/hopper/tensor_core_async/kernel.cu",
|
| 150 |
+
"description": "Hopper BF16 async WMMA square lane with a 128x128x64 CTA to test the last cheap work-per-stage lever before the WGMMA/TMA path takes over.",
|
| 151 |
+
"tags": ["cuda", "matmul", "hopper", "sm90", "prototype", "tensor-core", "bf16", "async", "cpasync", "double-buffered", "square", "k64", "gap-close"],
|
| 152 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -DPYC_HOPPER_TENSOR_CORE_USE_BF16=1 -DPYC_HOPPER_TC_WARP_ROW_GROUPS=4 -DPYC_HOPPER_TC_WARP_COL_GROUPS=4 -DPYC_HOPPER_TC_TILE_K=64 -gencode arch=compute_90,code=sm_90 -gencode arch=compute_90,code=compute_90 {source} -o {build_dir}/{name}",
|
| 153 |
+
"run_cmd": "{build_dir}/{name} 4096 4096 4096 3 30 1"
|
| 154 |
+
}
|
| 155 |
+
]
|
| 156 |
+
}
|
manifests/registry_kernels.json
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"schema_version": 1,
|
| 3 |
+
"kernels": [
|
| 4 |
+
{
|
| 5 |
+
"name": "matrix_mult",
|
| 6 |
+
"source": "kernels/prototypes/baseline/matmul/kernel.cu",
|
| 7 |
+
"description": "CUDA matrix multiplication kernel (compile-only baseline).",
|
| 8 |
+
"tags": ["cuda", "matmul"],
|
| 9 |
+
"compile_cmd": "{nvcc} -O3 -c {source} -o {build_dir}/{name}.o",
|
| 10 |
+
"run_cmd": ""
|
| 11 |
+
},
|
| 12 |
+
{
|
| 13 |
+
"name": "tokenizer_kernel",
|
| 14 |
+
"source": "kernels/prototypes/experimental/tokenizer_matmul/kernel.cu",
|
| 15 |
+
"description": "Experimental tokenizer/matrix CUDA kernel file.",
|
| 16 |
+
"tags": ["cuda", "tokenizer", "experimental"],
|
| 17 |
+
"compile_cmd": "{nvcc} -O2 -c {source} -o {build_dir}/{name}.o",
|
| 18 |
+
"run_cmd": ""
|
| 19 |
+
},
|
| 20 |
+
{
|
| 21 |
+
"name": "ada_gemm",
|
| 22 |
+
"source": "kernels/prototypes/ada/gemm/kernel.cu",
|
| 23 |
+
"description": "Ada-focused FP32 shared-memory GEMM prototype with standalone correctness and timing harness.",
|
| 24 |
+
"tags": ["cuda", "matmul", "ada", "sm89", "prototype"],
|
| 25 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -gencode arch=compute_89,code=sm_89 -gencode arch=compute_89,code=compute_89 {source} -o {build_dir}/{name}",
|
| 26 |
+
"run_cmd": "{build_dir}/{name} 1024 1024 1024 10 50"
|
| 27 |
+
},
|
| 28 |
+
{
|
| 29 |
+
"name": "ada_gemm_k64_warp32_async",
|
| 30 |
+
"source": "kernels/prototypes/ada/gemm_k64_warp32_async/kernel.cu",
|
| 31 |
+
"description": "Ada FP32 GEMM winner with 64x64x64 tiles, 32x8 threads, and cp.async double-buffered shared-memory stages.",
|
| 32 |
+
"tags": ["cuda", "matmul", "ada", "sm89", "prototype", "fp32", "vectorized", "winner", "k64", "warp32", "async", "cpasync"],
|
| 33 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -gencode arch=compute_89,code=sm_89 -gencode arch=compute_89,code=compute_89 {source} -o {build_dir}/{name}",
|
| 34 |
+
"run_cmd": "{build_dir}/{name} 1024 1024 1024 10 50"
|
| 35 |
+
},
|
| 36 |
+
{
|
| 37 |
+
"name": "ada_tensor_core_fp16",
|
| 38 |
+
"source": "kernels/prototypes/ada/tensor_core/kernel.cu",
|
| 39 |
+
"description": "Ada Tensor Core FP16 GEMM prototype.",
|
| 40 |
+
"tags": ["cuda", "matmul", "ada", "sm89", "prototype", "tensor-core", "fp16"],
|
| 41 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -DPYC_ADA_TENSOR_CORE_USE_BF16=0 -gencode arch=compute_89,code=sm_89 -gencode arch=compute_89,code=compute_89 {source} -o {build_dir}/{name}",
|
| 42 |
+
"run_cmd": "{build_dir}/{name} 1024 1024 1024 10 50"
|
| 43 |
+
},
|
| 44 |
+
{
|
| 45 |
+
"name": "ada_tensor_core_bf16",
|
| 46 |
+
"source": "kernels/prototypes/ada/tensor_core/kernel.cu",
|
| 47 |
+
"description": "Ada Tensor Core BF16 GEMM prototype.",
|
| 48 |
+
"tags": ["cuda", "matmul", "ada", "sm89", "prototype", "tensor-core", "bf16"],
|
| 49 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -DPYC_ADA_TENSOR_CORE_USE_BF16=1 -gencode arch=compute_89,code=sm_89 -gencode arch=compute_89,code=compute_89 {source} -o {build_dir}/{name}",
|
| 50 |
+
"run_cmd": "{build_dir}/{name} 1024 1024 1024 10 50"
|
| 51 |
+
},
|
| 52 |
+
{
|
| 53 |
+
"name": "hopper_tensor_core_fp16",
|
| 54 |
+
"source": "kernels/prototypes/hopper/tensor_core/kernel.cu",
|
| 55 |
+
"description": "Hopper Tensor Core FP16 GEMM prototype.",
|
| 56 |
+
"tags": ["cuda", "matmul", "hopper", "sm90", "prototype", "tensor-core", "fp16"],
|
| 57 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -DPYC_HOPPER_TENSOR_CORE_USE_BF16=0 -gencode arch=compute_90,code=sm_90 -gencode arch=compute_90,code=compute_90 {source} -o {build_dir}/{name}",
|
| 58 |
+
"run_cmd": "{build_dir}/{name} 1024 1024 1024 5 20"
|
| 59 |
+
},
|
| 60 |
+
{
|
| 61 |
+
"name": "hopper_tensor_core_bf16",
|
| 62 |
+
"source": "kernels/prototypes/hopper/tensor_core/kernel.cu",
|
| 63 |
+
"description": "Hopper Tensor Core BF16 GEMM prototype.",
|
| 64 |
+
"tags": ["cuda", "matmul", "hopper", "sm90", "prototype", "tensor-core", "bf16"],
|
| 65 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -DPYC_HOPPER_TENSOR_CORE_USE_BF16=1 -gencode arch=compute_90,code=sm_90 -gencode arch=compute_90,code=compute_90 {source} -o {build_dir}/{name}",
|
| 66 |
+
"run_cmd": "{build_dir}/{name} 1024 1024 1024 5 20"
|
| 67 |
+
},
|
| 68 |
+
{
|
| 69 |
+
"name": "hopper_tensor_core_bf16_perf",
|
| 70 |
+
"source": "kernels/prototypes/hopper/tensor_core/kernel.cu",
|
| 71 |
+
"description": "Hopper BF16 baseline performance lane.",
|
| 72 |
+
"tags": ["cuda", "matmul", "hopper", "sm90", "prototype", "tensor-core", "bf16", "perf"],
|
| 73 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -DPYC_HOPPER_TENSOR_CORE_USE_BF16=1 -gencode arch=compute_90,code=sm_90 -gencode arch=compute_90,code=compute_90 {source} -o {build_dir}/{name}",
|
| 74 |
+
"run_cmd": "{build_dir}/{name} 4096 4096 4096 3 30 1"
|
| 75 |
+
},
|
| 76 |
+
{
|
| 77 |
+
"name": "hopper_tensor_core_bf16_warp2n",
|
| 78 |
+
"source": "kernels/prototypes/hopper/tensor_core/kernel.cu",
|
| 79 |
+
"description": "Hopper BF16 performance lane with two N fragments per warp.",
|
| 80 |
+
"tags": ["cuda", "matmul", "hopper", "sm90", "prototype", "tensor-core", "bf16", "perf", "warp2n"],
|
| 81 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -DPYC_HOPPER_TENSOR_CORE_USE_BF16=1 -DPYC_HOPPER_TC_WARP_COL_TILES=2 -DPYC_HOPPER_TC_WARP_ROW_GROUPS=4 -DPYC_HOPPER_TC_WARP_COL_GROUPS=2 -DPYC_HOPPER_TC_SHARED_PAD_A=8 -DPYC_HOPPER_TC_SHARED_PAD_B=8 -gencode arch=compute_90,code=sm_90 -gencode arch=compute_90,code=compute_90 {source} -o {build_dir}/{name}",
|
| 82 |
+
"run_cmd": "{build_dir}/{name} 4096 4096 4096 3 30 1"
|
| 83 |
+
},
|
| 84 |
+
{
|
| 85 |
+
"name": "hopper_tensor_core_bf16_k32",
|
| 86 |
+
"source": "kernels/prototypes/hopper/tensor_core/kernel.cu",
|
| 87 |
+
"description": "Hopper BF16 performance lane with 32-wide K staging.",
|
| 88 |
+
"tags": ["cuda", "matmul", "hopper", "sm90", "prototype", "tensor-core", "bf16", "perf", "k32"],
|
| 89 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -DPYC_HOPPER_TENSOR_CORE_USE_BF16=1 -DPYC_HOPPER_TC_TILE_K=32 -DPYC_HOPPER_TC_SHARED_PAD_A=8 -DPYC_HOPPER_TC_SHARED_PAD_B=8 -gencode arch=compute_90,code=sm_90 -gencode arch=compute_90,code=compute_90 {source} -o {build_dir}/{name}",
|
| 90 |
+
"run_cmd": "{build_dir}/{name} 4096 4096 4096 3 30 1"
|
| 91 |
+
},
|
| 92 |
+
{
|
| 93 |
+
"name": "hopper_tensor_core_bf16_warp2n_k32",
|
| 94 |
+
"source": "kernels/prototypes/hopper/tensor_core/kernel.cu",
|
| 95 |
+
"description": "Hopper BF16 performance lane combining warp2n and 32-wide K staging.",
|
| 96 |
+
"tags": ["cuda", "matmul", "hopper", "sm90", "prototype", "tensor-core", "bf16", "perf", "warp2n", "k32"],
|
| 97 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -DPYC_HOPPER_TENSOR_CORE_USE_BF16=1 -DPYC_HOPPER_TC_WARP_COL_TILES=2 -DPYC_HOPPER_TC_WARP_ROW_GROUPS=4 -DPYC_HOPPER_TC_WARP_COL_GROUPS=2 -DPYC_HOPPER_TC_TILE_K=32 -DPYC_HOPPER_TC_SHARED_PAD_A=8 -DPYC_HOPPER_TC_SHARED_PAD_B=8 -gencode arch=compute_90,code=sm_90 -gencode arch=compute_90,code=compute_90 {source} -o {build_dir}/{name}",
|
| 98 |
+
"run_cmd": "{build_dir}/{name} 4096 4096 4096 3 30 1"
|
| 99 |
+
},
|
| 100 |
+
{
|
| 101 |
+
"name": "hopper_cublaslt_bf16",
|
| 102 |
+
"source": "kernels/prototypes/hopper/cublaslt_bf16/kernel.cu",
|
| 103 |
+
"description": "Hopper cuBLASLt BF16 GEMM control lane.",
|
| 104 |
+
"tags": ["cuda", "matmul", "hopper", "sm90", "prototype", "cublaslt", "bf16", "control"],
|
| 105 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -gencode arch=compute_90,code=sm_90 -gencode arch=compute_90,code=compute_90 {source} -lcublasLt -lcublas -o {build_dir}/{name}",
|
| 106 |
+
"run_cmd": "{build_dir}/{name} 4096 4096 4096 3 30 1"
|
| 107 |
+
},
|
| 108 |
+
{
|
| 109 |
+
"name": "hopper_cublaslt_bf16_check",
|
| 110 |
+
"source": "kernels/prototypes/hopper/cublaslt_bf16/kernel.cu",
|
| 111 |
+
"description": "Smaller Hopper cuBLASLt BF16 correctness lane.",
|
| 112 |
+
"tags": ["cuda", "matmul", "hopper", "sm90", "prototype", "cublaslt", "bf16", "correctness"],
|
| 113 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -gencode arch=compute_90,code=sm_90 -gencode arch=compute_90,code=compute_90 {source} -lcublasLt -lcublas -o {build_dir}/{name}",
|
| 114 |
+
"run_cmd": "{build_dir}/{name} 512 512 512 2 10 0"
|
| 115 |
+
},
|
| 116 |
+
{
|
| 117 |
+
"name": "hopper_tensor_core_bf16_async",
|
| 118 |
+
"source": "kernels/prototypes/hopper/tensor_core_async/kernel.cu",
|
| 119 |
+
"description": "Hopper BF16 async WMMA lane with double-buffered staging.",
|
| 120 |
+
"tags": ["cuda", "matmul", "hopper", "sm90", "prototype", "tensor-core", "bf16", "async", "cpasync"],
|
| 121 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -DPYC_HOPPER_TENSOR_CORE_USE_BF16=1 -gencode arch=compute_90,code=sm_90 -gencode arch=compute_90,code=compute_90 {source} -o {build_dir}/{name}",
|
| 122 |
+
"run_cmd": "{build_dir}/{name} 4096 4096 4096 3 30 1"
|
| 123 |
+
},
|
| 124 |
+
{
|
| 125 |
+
"name": "hopper_tensor_core_bf16_async_wide",
|
| 126 |
+
"source": "kernels/prototypes/hopper/tensor_core_async/kernel.cu",
|
| 127 |
+
"description": "Hopper BF16 async WMMA lane widened to a 64x128 CTA.",
|
| 128 |
+
"tags": ["cuda", "matmul", "hopper", "sm90", "prototype", "tensor-core", "bf16", "async", "wide"],
|
| 129 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -DPYC_HOPPER_TENSOR_CORE_USE_BF16=1 -DPYC_HOPPER_TC_WARP_COL_GROUPS=4 -gencode arch=compute_90,code=sm_90 -gencode arch=compute_90,code=compute_90 {source} -o {build_dir}/{name}",
|
| 130 |
+
"run_cmd": "{build_dir}/{name} 4096 4096 4096 3 30 1"
|
| 131 |
+
},
|
| 132 |
+
{
|
| 133 |
+
"name": "hopper_tensor_core_bf16_async_wide_k64",
|
| 134 |
+
"source": "kernels/prototypes/hopper/tensor_core_async/kernel.cu",
|
| 135 |
+
"description": "Hopper BF16 async WMMA wide lane with K=64 staging.",
|
| 136 |
+
"tags": ["cuda", "matmul", "hopper", "sm90", "prototype", "tensor-core", "bf16", "async", "wide", "k64"],
|
| 137 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -DPYC_HOPPER_TENSOR_CORE_USE_BF16=1 -DPYC_HOPPER_TC_WARP_COL_GROUPS=4 -DPYC_HOPPER_TC_TILE_K=64 -gencode arch=compute_90,code=sm_90 -gencode arch=compute_90,code=compute_90 {source} -o {build_dir}/{name}",
|
| 138 |
+
"run_cmd": "{build_dir}/{name} 4096 4096 4096 3 30 1"
|
| 139 |
+
},
|
| 140 |
+
{
|
| 141 |
+
"name": "hopper_tensor_core_bf16_async_square",
|
| 142 |
+
"source": "kernels/prototypes/hopper/tensor_core_async/kernel.cu",
|
| 143 |
+
"description": "Hopper BF16 async WMMA lane widened to a 128x128 CTA.",
|
| 144 |
+
"tags": ["cuda", "matmul", "hopper", "sm90", "prototype", "tensor-core", "bf16", "async", "square"],
|
| 145 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -DPYC_HOPPER_TENSOR_CORE_USE_BF16=1 -DPYC_HOPPER_TC_WARP_ROW_GROUPS=4 -DPYC_HOPPER_TC_WARP_COL_GROUPS=4 -gencode arch=compute_90,code=sm_90 -gencode arch=compute_90,code=compute_90 {source} -o {build_dir}/{name}",
|
| 146 |
+
"run_cmd": "{build_dir}/{name} 4096 4096 4096 3 30 1"
|
| 147 |
+
},
|
| 148 |
+
{
|
| 149 |
+
"name": "hopper_tensor_core_bf16_async_square_k64",
|
| 150 |
+
"source": "kernels/prototypes/hopper/tensor_core_async/kernel.cu",
|
| 151 |
+
"description": "Hopper BF16 async WMMA square lane with K=64 staging.",
|
| 152 |
+
"tags": ["cuda", "matmul", "hopper", "sm90", "prototype", "tensor-core", "bf16", "async", "square", "k64"],
|
| 153 |
+
"compile_cmd": "{nvcc} -O3 -std=c++17 -lineinfo -DPYC_HOPPER_TENSOR_CORE_USE_BF16=1 -DPYC_HOPPER_TC_WARP_ROW_GROUPS=4 -DPYC_HOPPER_TC_WARP_COL_GROUPS=4 -DPYC_HOPPER_TC_TILE_K=64 -gencode arch=compute_90,code=sm_90 -gencode arch=compute_90,code=compute_90 {source} -o {build_dir}/{name}",
|
| 154 |
+
"run_cmd": "{build_dir}/{name} 4096 4096 4096 3 30 1"
|
| 155 |
+
}
|
| 156 |
+
]
|
| 157 |
+
}
|