omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / gpu / amd / gfx942 / fp8gemv / time_fp8gemv.cpp
| 1 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | // Times fp8gemv_stream and hipblasLtMatmul on a machine that has /dev/kfd. |
| 3 | // bench.sh compiles this with hipcc and passes the hsaco path. Both arms run |
| 4 | // in this process. |
| 5 | // |
| 6 | // hot - 10 calls on the buffers being timed, then 21 samples. Nothing is |
| 7 | // read or written between those samples except the call itself. |
| 8 | // cold - before each of the 21 samples, a read of a 512 MiB device buffer. |
| 9 | // 512 MiB is larger than 256 MB, so the read does not fit in the L2 |
| 10 | // or in a 256 MB MALL. The GEMV weights and the GEMM A and B are not |
| 11 | // written between that read and the timed call. |
| 12 | // |
| 13 | // The library timer is hipblasLtMatmul. hipblaslt-bench --flush launches |
| 14 | // flush_icache, which is s_icache_inv and sixteen s_nop 0 |
| 15 | // (clients/include/testing_matmul.hpp, ROCm/hipBLASLt |
| 16 | // 3a609b06926c8227e753b62087555e1f435bf2d4). --rotating (size in MB, |
| 17 | // clients/benchmarks/README.md) indexes copy i % block_count, with |
| 18 | // block_count = max(1, min(max(cold_iters, iters), ceil(rotating / |
| 19 | // totalRotatingSizeNeeded))). Those copies are initialized before one |
| 20 | // hipEvent pair around the hot loop, and argument_model.hpp log_perf divides |
| 21 | // that interval by iters. The operands were just written. That is not this |
| 22 | // cold mode. |
| 23 | // |
| 24 | // flags bit 0 is the workgroup-to-row remap, not hipBLASLt's tensor swizzle. |
| 25 | // Both settings are checked against the column-order plain reference before |
| 26 | // any sample is timed. The tolerance is check.om's allow() plus the 8 ulps |
| 27 | // of |got|+|plain| from order(). A mismatch prints FAIL and exits non-zero. |
| 28 | // |
| 29 | // SH_MEM_CONFIG bit 8 is SH_MEM_CONFIG__F8_MODE in |
| 30 | // drivers/gpu/drm/amd/include/asic_reg/gc/gc_9_4_3_sh_mask.h (shift 0x8, |
| 31 | // mask 0x00000100). Alignment mode is bits 4:3. RETRY_DISABLE is bit 12. |
| 32 | // KFD sets F8_MODE for GC 9.4.3. This code object does not write the register. |
| 33 | // See the README for the file and field citations. |
| 34 | |
| 35 | #include "plain_ref.h" |
| 36 | |
| 37 | #include <hip/hip_runtime.h> |
| 38 | #include <hipblaslt/hipblaslt.h> |
| 39 | |
| 40 | #include <algorithm> |
| 41 | #include <cstdint> |
| 42 | #include <cstdio> |
| 43 | #include <cstring> |
| 44 | #include <vector> |
| 45 | |
| 46 | namespace { |
| 47 | |
| 48 | constexpr double kPeakBytesPerSec = 5.3e12; |
| 49 | constexpr int kWarmup = 10; |
| 50 | constexpr int kRuns = 21; |
| 51 | constexpr size_t kFlushBytes = 512ull << 20; |
| 52 | constexpr int kFlushBlocks = 2048; |
| 53 | |
| 54 | struct alignas(8) Args { |
| 55 | uint64_t w; |
| 56 | uint64_t x; |
| 57 | uint64_t scale; |
| 58 | uint64_t y; |
| 59 | uint64_t residual; |
| 60 | uint64_t gamma; |
| 61 | float inv_rms; |
| 62 | uint32_t m; |
| 63 | uint32_t k; |
| 64 | uint32_t flags; |
| 65 | }; |
| 66 | |
| 67 | static_assert(sizeof(Args) == 64, "kernarg is 64 bytes"); |
| 68 | static_assert(offsetof(Args, inv_rms) == 48, "inv_rms offset"); |
| 69 | static_assert(offsetof(Args, flags) == 60, "flags offset"); |
| 70 | |
| 71 | #define CHECK(cmd) \ |
| 72 | do { \ |
| 73 | hipError_t err_ = (cmd); \ |
| 74 | if (err_ != hipSuccess) { \ |
| 75 | std::fprintf(stderr, "%s: %s\n", #cmd, hipGetErrorString(err_)); \ |
| 76 | return 1; \ |
| 77 | } \ |
| 78 | } while (0) |
| 79 | |
| 80 | #define CHECK_LT(cmd) \ |
| 81 | do { \ |
| 82 | hipblasStatus_t st_ = (cmd); \ |
| 83 | if (st_ != HIPBLAS_STATUS_SUCCESS) { \ |
| 84 | std::fprintf(stderr, "%s: hipblaslt status %d\n", #cmd, \ |
| 85 | static_cast<int>(st_)); \ |
| 86 | return 1; \ |
| 87 | } \ |
| 88 | } while (0) |
| 89 | |
| 90 | } // namespace |
| 91 | |
| 92 | __global__ void flush_l2(const uint32_t* p, size_t n, uint32_t* sink) { |
| 93 | size_t stride = static_cast<size_t>(gridDim.x) * blockDim.x; |
| 94 | uint32_t acc = 0; |
| 95 | for (size_t i = static_cast<size_t>(blockIdx.x) * blockDim.x + threadIdx.x; |
| 96 | i < n; i += stride) { |
| 97 | acc += p[i]; |
| 98 | } |
| 99 | sink[blockIdx.x] = acc; |
| 100 | } |
| 101 | |
| 102 | namespace { |
| 103 | |
| 104 | int launch(hipFunction_t fn, Args args, uint32_t grid, hipEvent_t start, |
| 105 | hipEvent_t stop, float* ms) { |
| 106 | size_t bytes = sizeof(args); |
| 107 | void* extra[] = {HIP_LAUNCH_PARAM_BUFFER_POINTER, &args, |
| 108 | HIP_LAUNCH_PARAM_BUFFER_SIZE, &bytes, HIP_LAUNCH_PARAM_END}; |
| 109 | CHECK(hipEventRecord(start)); |
| 110 | CHECK(hipModuleLaunchKernel(fn, grid, 1, 1, 64, 1, 1, 0, nullptr, nullptr, |
| 111 | extra)); |
| 112 | CHECK(hipEventRecord(stop)); |
| 113 | CHECK(hipEventSynchronize(stop)); |
| 114 | CHECK(hipEventElapsedTime(ms, start, stop)); |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | int flush_cache(uint32_t* buf, uint32_t* sink) { |
| 119 | size_t n = kFlushBytes / sizeof(uint32_t); |
| 120 | flush_l2<<<kFlushBlocks, 256>>>(buf, n, sink); |
| 121 | CHECK(hipGetLastError()); |
| 122 | CHECK(hipDeviceSynchronize()); |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | void print_samples(const std::vector<double>& us) { |
| 127 | std::printf("samples_us"); |
| 128 | for (double sample : us) std::printf(" %.4f", sample); |
| 129 | std::printf("\n"); |
| 130 | } |
| 131 | |
| 132 | // One hipblasLtMatmul per sample, on A and B that were filled once. Hot does |
| 133 | // not touch those buffers between samples. Cold calls flush_cache and then |
| 134 | // the matmul, and does not fill A or B again. beta is 0, so the byte count |
| 135 | // is M*K + K + 4*M (A, B, D). C is a separate buffer and is not in that count, |
| 136 | // which is the exclusion testing_matmul.hpp makes when beta is 0 |
| 137 | // (sizeC is 0 in totalRotatingSizeNeeded). |
| 138 | int time_hipblaslt(uint32_t* flush_buf, uint32_t* flush_sink, hipEvent_t start, |
| 139 | hipEvent_t stop, const uint32_t* rows, int nrows, |
| 140 | const uint32_t* cols, int ncols) { |
| 141 | uint64_t biggest = 0; |
| 142 | uint32_t max_m = 0; |
| 143 | uint32_t max_k = 0; |
| 144 | for (int i = 0; i < nrows; ++i) { |
| 145 | max_m = std::max(max_m, rows[i]); |
| 146 | for (int j = 0; j < ncols; ++j) { |
| 147 | max_k = std::max(max_k, cols[j]); |
| 148 | biggest = std::max(biggest, static_cast<uint64_t>(rows[i]) * cols[j]); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | void* a = nullptr; |
| 153 | void* b = nullptr; |
| 154 | void* c = nullptr; |
| 155 | void* d = nullptr; |
| 156 | void* scale_a = nullptr; |
| 157 | void* scale_b = nullptr; |
| 158 | void* workspace = nullptr; |
| 159 | CHECK(hipMalloc(&a, biggest)); |
| 160 | CHECK(hipMalloc(&b, max_k)); |
| 161 | CHECK(hipMalloc(&c, static_cast<size_t>(max_m) * sizeof(float))); |
| 162 | CHECK(hipMalloc(&d, static_cast<size_t>(max_m) * sizeof(float))); |
| 163 | CHECK(hipMalloc(&scale_a, sizeof(float))); |
| 164 | CHECK(hipMalloc(&scale_b, sizeof(float))); |
| 165 | const uint64_t kWorkspace = 128ull << 20; |
| 166 | CHECK(hipMalloc(&workspace, kWorkspace)); |
| 167 | // Filled once. Neither hot nor cold writes A or B again. |
| 168 | CHECK(hipMemset(a, 0x40, biggest)); |
| 169 | CHECK(hipMemset(b, 0x40, max_k)); |
| 170 | const float one = 1.f; |
| 171 | CHECK(hipMemcpy(scale_a, &one, sizeof(one), hipMemcpyHostToDevice)); |
| 172 | CHECK(hipMemcpy(scale_b, &one, sizeof(one), hipMemcpyHostToDevice)); |
| 173 | |
| 174 | hipblasLtHandle_t handle = nullptr; |
| 175 | CHECK_LT(hipblasLtCreate(&handle)); |
| 176 | hipblasLtMatmulPreference_t pref = nullptr; |
| 177 | CHECK_LT(hipblasLtMatmulPreferenceCreate(&pref)); |
| 178 | CHECK_LT(hipblasLtMatmulPreferenceSetAttribute( |
| 179 | pref, HIPBLASLT_MATMUL_PREF_MAX_WORKSPACE_BYTES, &kWorkspace, |
| 180 | sizeof(kWorkspace))); |
| 181 | |
| 182 | struct Problem { |
| 183 | uint32_t m; |
| 184 | uint32_t k; |
| 185 | hipblasLtMatrixLayout_t layout_a; |
| 186 | hipblasLtMatrixLayout_t layout_b; |
| 187 | hipblasLtMatrixLayout_t layout_c; |
| 188 | hipblasLtMatrixLayout_t layout_d; |
| 189 | hipblasLtMatmulDesc_t desc; |
| 190 | hipblasLtMatmulAlgo_t algo; |
| 191 | size_t workspace_bytes; |
| 192 | }; |
| 193 | std::vector<Problem> problems; |
| 194 | const float alpha = 1.f; |
| 195 | const float beta = 0.f; |
| 196 | hipblasOperation_t trans_a = HIPBLAS_OP_T; |
| 197 | hipblasOperation_t trans_b = HIPBLAS_OP_N; |
| 198 | // Unset --compute_input_typeA/B becomes the compute type's real type. |
| 199 | // For f32 that is HIP_R_32F. The matrix types stay FNUZ. |
| 200 | hipDataType compute_input = HIP_R_32F; |
| 201 | |
| 202 | for (int i = 0; i < nrows; ++i) { |
| 203 | for (int j = 0; j < ncols; ++j) { |
| 204 | Problem p{}; |
| 205 | p.m = rows[i]; |
| 206 | p.k = cols[j]; |
| 207 | CHECK_LT(hipblasLtMatrixLayoutCreate(&p.layout_a, HIP_R_8F_E4M3_FNUZ, p.k, p.m, |
| 208 | static_cast<int64_t>(p.k))); |
| 209 | CHECK_LT(hipblasLtMatrixLayoutCreate(&p.layout_b, HIP_R_8F_E4M3_FNUZ, p.k, 1, |
| 210 | static_cast<int64_t>(p.k))); |
| 211 | CHECK_LT(hipblasLtMatrixLayoutCreate(&p.layout_c, HIP_R_32F, p.m, 1, |
| 212 | static_cast<int64_t>(p.m))); |
| 213 | CHECK_LT(hipblasLtMatrixLayoutCreate(&p.layout_d, HIP_R_32F, p.m, 1, |
| 214 | static_cast<int64_t>(p.m))); |
| 215 | CHECK_LT(hipblasLtMatmulDescCreate(&p.desc, HIPBLAS_COMPUTE_32F, HIP_R_32F)); |
| 216 | CHECK_LT(hipblasLtMatmulDescSetAttribute( |
| 217 | p.desc, HIPBLASLT_MATMUL_DESC_COMPUTE_INPUT_TYPE_A_EXT, &compute_input, |
| 218 | sizeof(void*))); |
| 219 | CHECK_LT(hipblasLtMatmulDescSetAttribute( |
| 220 | p.desc, HIPBLASLT_MATMUL_DESC_COMPUTE_INPUT_TYPE_B_EXT, &compute_input, |
| 221 | sizeof(void*))); |
| 222 | CHECK_LT(hipblasLtMatmulDescSetAttribute(p.desc, HIPBLASLT_MATMUL_DESC_TRANSA, |
| 223 | &trans_a, sizeof(int32_t))); |
| 224 | CHECK_LT(hipblasLtMatmulDescSetAttribute(p.desc, HIPBLASLT_MATMUL_DESC_TRANSB, |
| 225 | &trans_b, sizeof(int32_t))); |
| 226 | CHECK_LT(hipblasLtMatmulDescSetAttribute( |
| 227 | p.desc, HIPBLASLT_MATMUL_DESC_A_SCALE_POINTER, &scale_a, sizeof(void*))); |
| 228 | CHECK_LT(hipblasLtMatmulDescSetAttribute( |
| 229 | p.desc, HIPBLASLT_MATMUL_DESC_B_SCALE_POINTER, &scale_b, sizeof(void*))); |
| 230 | |
| 231 | hipblasLtMatmulHeuristicResult_t heur{}; |
| 232 | int returned = 0; |
| 233 | CHECK_LT(hipblasLtMatmulAlgoGetHeuristic(handle, p.desc, p.layout_a, p.layout_b, |
| 234 | p.layout_c, p.layout_d, pref, 1, &heur, |
| 235 | &returned)); |
| 236 | if (returned < 1 || heur.state != HIPBLAS_STATUS_SUCCESS) { |
| 237 | std::printf("FAIL hipBLASLt heuristic M %u K %u returned %d\n", p.m, p.k, |
| 238 | returned); |
| 239 | std::fprintf(stderr, "FAIL hipBLASLt heuristic M %u K %u returned %d\n", p.m, |
| 240 | p.k, returned); |
| 241 | return 1; |
| 242 | } |
| 243 | p.algo = heur.algo; |
| 244 | p.workspace_bytes = heur.workspaceSize; |
| 245 | if (p.workspace_bytes > kWorkspace) { |
| 246 | std::printf("FAIL hipBLASLt workspace M %u K %u wants %zu\n", p.m, p.k, |
| 247 | p.workspace_bytes); |
| 248 | std::fprintf(stderr, "FAIL hipBLASLt workspace M %u K %u wants %zu\n", p.m, p.k, |
| 249 | p.workspace_bytes); |
| 250 | return 1; |
| 251 | } |
| 252 | problems.push_back(p); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | std::printf( |
| 257 | "hipblaslt timer hipblasLtMatmul in this process. A and B filled once. " |
| 258 | "cold reads flush_bytes %zu then calls matmul on those same buffers.\n", |
| 259 | kFlushBytes); |
| 260 | |
| 261 | const char* modes[] = {"hot", "cold"}; |
| 262 | for (const char* mode : modes) { |
| 263 | bool cold = std::strcmp(mode, "cold") == 0; |
| 264 | int warmup = cold ? 0 : kWarmup; |
| 265 | for (const Problem& p : problems) { |
| 266 | float ms = 0.f; |
| 267 | void* ws = p.workspace_bytes == 0 ? nullptr : workspace; |
| 268 | for (int n = 0; n < warmup; ++n) { |
| 269 | CHECK(hipEventRecord(start)); |
| 270 | CHECK_LT(hipblasLtMatmul(handle, p.desc, &alpha, a, p.layout_a, b, p.layout_b, |
| 271 | &beta, c, p.layout_c, d, p.layout_d, &p.algo, ws, |
| 272 | p.workspace_bytes, nullptr)); |
| 273 | CHECK(hipEventRecord(stop)); |
| 274 | CHECK(hipEventSynchronize(stop)); |
| 275 | } |
| 276 | std::vector<double> us; |
| 277 | us.reserve(kRuns); |
| 278 | for (int n = 0; n < kRuns; ++n) { |
| 279 | if (cold && flush_cache(flush_buf, flush_sink)) return 1; |
| 280 | CHECK(hipEventRecord(start)); |
| 281 | CHECK_LT(hipblasLtMatmul(handle, p.desc, &alpha, a, p.layout_a, b, p.layout_b, |
| 282 | &beta, c, p.layout_c, d, p.layout_d, &p.algo, ws, |
| 283 | p.workspace_bytes, nullptr)); |
| 284 | CHECK(hipEventRecord(stop)); |
| 285 | CHECK(hipEventSynchronize(stop)); |
| 286 | CHECK(hipEventElapsedTime(&ms, start, stop)); |
| 287 | us.push_back(static_cast<double>(ms) * 1000.0); |
| 288 | } |
| 289 | std::vector<double> sorted = us; |
| 290 | std::sort(sorted.begin(), sorted.end()); |
| 291 | double med = sorted[sorted.size() / 2]; |
| 292 | double bytes = static_cast<double>(p.m) * p.k + static_cast<double>(p.k) + |
| 293 | 4.0 * p.m; |
| 294 | double gbps = bytes / (med * 1e-6) / 1e9; |
| 295 | double pct = (bytes / (med * 1e-6)) / kPeakBytesPerSec * 100.0; |
| 296 | std::printf( |
| 297 | "hipblaslt mode %s M %u K %u warmup %d runs %d min_us %.4f median_us %.4f " |
| 298 | "max_us %.4f spread_us %.4f bytes %.0f GB/s %.4f pct_of_5.3TBs %.4f\n", |
| 299 | mode, p.m, p.k, warmup, kRuns, sorted.front(), med, sorted.back(), |
| 300 | sorted.back() - sorted.front(), bytes, gbps, pct); |
| 301 | print_samples(us); |
| 302 | } |
| 303 | } |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | uint64_t xorshift(uint64_t x) { |
| 308 | x ^= x << 13; |
| 309 | x ^= x >> 7; |
| 310 | x ^= x << 17; |
| 311 | return x; |
| 312 | } |
| 313 | |
| 314 | } // namespace |
| 315 | |
| 316 | int main(int argc, char** argv) { |
| 317 | bool flush_only = argc == 2 && std::strcmp(argv[1], "--flush") == 0; |
| 318 | if (!flush_only && argc != 2) { |
| 319 | std::fprintf(stderr, "usage: time_fp8gemv fp8gemv_stream.hsaco\n"); |
| 320 | std::fprintf(stderr, " time_fp8gemv --flush\n"); |
| 321 | return 2; |
| 322 | } |
| 323 | |
| 324 | int device = 0; |
| 325 | CHECK(hipGetDeviceCount(&device)); |
| 326 | if (device < 1) { |
| 327 | std::fprintf(stderr, "BLOCKED_HARDWARE hipGetDeviceCount returned 0\n"); |
| 328 | return 1; |
| 329 | } |
| 330 | CHECK(hipSetDevice(0)); |
| 331 | hipDeviceProp_t prop{}; |
| 332 | CHECK(hipGetDeviceProperties(&prop, 0)); |
| 333 | int runtime = 0; |
| 334 | CHECK(hipRuntimeGetVersion(&runtime)); |
| 335 | std::printf("device %s gcn %s pci %04x:%02x:%02x\n", prop.name, prop.gcnArchName, |
| 336 | prop.pciDomainID, prop.pciBusID, prop.pciDeviceID); |
| 337 | std::printf("hip %d clock_kHz %d mem_clock_kHz %d\n", runtime, prop.clockRate, |
| 338 | prop.memoryClockRate); |
| 339 | std::printf( |
| 340 | "sh_mem F8_MODE is bit 8 of SH_MEM_CONFIG (gc_9_4_3_sh_mask.h " |
| 341 | "SH_MEM_CONFIG__F8_MODE). KFD sets it for GC 9.4.3. This code object " |
| 342 | "does not.\n"); |
| 343 | |
| 344 | uint32_t* flush_buf = nullptr; |
| 345 | uint32_t* flush_sink = nullptr; |
| 346 | CHECK(hipMalloc(&flush_buf, kFlushBytes)); |
| 347 | CHECK(hipMalloc(&flush_sink, static_cast<size_t>(kFlushBlocks) * sizeof(uint32_t))); |
| 348 | CHECK(hipMemset(flush_buf, 1, kFlushBytes)); |
| 349 | if (flush_cache(flush_buf, flush_sink)) return 1; |
| 350 | std::printf("flush_bytes %zu\n", kFlushBytes); |
| 351 | if (flush_only) return 0; |
| 352 | |
| 353 | hipModule_t mod{}; |
| 354 | CHECK(hipModuleLoad(&mod, argv[1])); |
| 355 | hipFunction_t fn{}; |
| 356 | hipError_t got_fn = hipModuleGetFunction(&fn, mod, "fp8gemv_stream"); |
| 357 | if (got_fn != hipSuccess) { |
| 358 | std::fprintf(stderr, "hipModuleGetFunction(fp8gemv_stream): %s\n", |
| 359 | hipGetErrorString(got_fn)); |
| 360 | return 1; |
| 361 | } |
| 362 | |
| 363 | const uint32_t rows[] = {4096, 8192, 14336, 28672}; |
| 364 | const uint32_t cols[] = {4096, 8192}; |
| 365 | uint64_t biggest = 0; |
| 366 | for (uint32_t m : rows) { |
| 367 | for (uint32_t k : cols) { |
| 368 | biggest = std::max(biggest, static_cast<uint64_t>(m) * k); |
| 369 | } |
| 370 | } |
| 371 | uint32_t max_m = rows[sizeof(rows) / sizeof(rows[0]) - 1]; |
| 372 | uint32_t max_k = cols[sizeof(cols) / sizeof(cols[0]) - 1]; |
| 373 | |
| 374 | void* w = nullptr; |
| 375 | void* x = nullptr; |
| 376 | void* scale = nullptr; |
| 377 | void* y = nullptr; |
| 378 | void* residual = nullptr; |
| 379 | void* gamma = nullptr; |
| 380 | CHECK(hipMalloc(&w, biggest)); |
| 381 | CHECK(hipMalloc(&x, static_cast<size_t>(max_k) * sizeof(float))); |
| 382 | CHECK(hipMalloc(&scale, static_cast<size_t>(max_m) * sizeof(float))); |
| 383 | CHECK(hipMalloc(&y, static_cast<size_t>(max_m) * sizeof(float))); |
| 384 | CHECK(hipMalloc(&residual, static_cast<size_t>(max_m) * sizeof(float))); |
| 385 | CHECK(hipMalloc(&gamma, static_cast<size_t>(max_m) * sizeof(float))); |
| 386 | |
| 387 | std::vector<uint8_t> host_w(biggest); |
| 388 | uint64_t xs = 1; |
| 389 | for (uint64_t i = 0; i < biggest; ++i) { |
| 390 | xs = xorshift(xs); |
| 391 | uint8_t b = static_cast<uint8_t>(xs & 0xffu); |
| 392 | if (b == 0x80) b = 0x7f; |
| 393 | host_w[i] = b; |
| 394 | } |
| 395 | std::vector<uint32_t> host_x(max_k, 0x3f800000u); |
| 396 | std::vector<uint32_t> host_scale(max_m, 0x3f800000u); |
| 397 | CHECK(hipMemcpy(w, host_w.data(), biggest, hipMemcpyHostToDevice)); |
| 398 | CHECK(hipMemcpy(x, host_x.data(), host_x.size() * sizeof(uint32_t), |
| 399 | hipMemcpyHostToDevice)); |
| 400 | CHECK(hipMemcpy(scale, host_scale.data(), host_scale.size() * sizeof(uint32_t), |
| 401 | hipMemcpyHostToDevice)); |
| 402 | |
| 403 | hipEvent_t start{}; |
| 404 | hipEvent_t stop{}; |
| 405 | CHECK(hipEventCreate(&start)); |
| 406 | CHECK(hipEventCreate(&stop)); |
| 407 | |
| 408 | std::printf("kernel_bytes M*K+4*K+8*M\n"); |
| 409 | std::printf("peak denominator %.1f TB/s published\n", kPeakBytesPerSec / 1e12); |
| 410 | |
| 411 | struct Shape { |
| 412 | uint32_t m; |
| 413 | uint32_t k; |
| 414 | uint32_t swizzle; |
| 415 | uint32_t grid; |
| 416 | Args args; |
| 417 | }; |
| 418 | std::vector<Shape> shapes; |
| 419 | for (uint32_t m : rows) { |
| 420 | for (uint32_t k : cols) { |
| 421 | for (uint32_t swizzle : {0u, 1u}) { |
| 422 | uint32_t span = (m + 7u) >> 3; |
| 423 | Shape s; |
| 424 | s.m = m; |
| 425 | s.k = k; |
| 426 | s.swizzle = swizzle; |
| 427 | s.grid = swizzle ? span * 8u : m; |
| 428 | s.args = Args{}; |
| 429 | s.args.w = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(w)); |
| 430 | s.args.x = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(x)); |
| 431 | s.args.scale = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(scale)); |
| 432 | s.args.y = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(y)); |
| 433 | s.args.residual = |
| 434 | static_cast<uint64_t>(reinterpret_cast<uintptr_t>(residual)); |
| 435 | s.args.gamma = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(gamma)); |
| 436 | s.args.inv_rms = 1.0f; |
| 437 | s.args.m = m; |
| 438 | s.args.k = k; |
| 439 | s.args.flags = swizzle; |
| 440 | shapes.push_back(s); |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | for (const Shape& s : shapes) { |
| 446 | CHECK(hipMemset(y, 0, static_cast<size_t>(s.m) * sizeof(float))); |
| 447 | float ms = 0.f; |
| 448 | if (launch(fn, s.args, s.grid, start, stop, &ms)) return 1; |
| 449 | std::vector<uint32_t> got(s.m); |
| 450 | CHECK(hipMemcpy(got.data(), y, got.size() * sizeof(uint32_t), |
| 451 | hipMemcpyDeviceToHost)); |
| 452 | for (uint32_t row = 0; row < s.m; ++row) { |
| 453 | PlainRow plain = plain_row(host_w.data() + static_cast<uint64_t>(row) * s.k, |
| 454 | host_x.data(), s.k, host_scale[row]); |
| 455 | uint32_t bound = |
| 456 | tolerance_bits(plain.sumabs, s.k, host_scale[row], got[row], plain.bits); |
| 457 | if (!within_plain(got[row], plain.bits, bound)) { |
| 458 | std::printf( |
| 459 | "FAIL M %u K %u swizzle %u row %u got %08x plain %08x bound %08x\n", |
| 460 | s.m, s.k, s.swizzle, row, got[row], plain.bits, bound); |
| 461 | std::fprintf(stderr, |
| 462 | "FAIL M %u K %u swizzle %u row %u got %08x plain %08x bound %08x\n", |
| 463 | s.m, s.k, s.swizzle, row, got[row], plain.bits, bound); |
| 464 | return 1; |
| 465 | } |
| 466 | } |
| 467 | std::printf("check M %u K %u swizzle %u PASS\n", s.m, s.k, s.swizzle); |
| 468 | } |
| 469 | std::printf("device check PASS\n"); |
| 470 | |
| 471 | const char* modes[] = {"hot", "cold"}; |
| 472 | for (const char* mode : modes) { |
| 473 | bool cold = std::strcmp(mode, "cold") == 0; |
| 474 | int warmup = cold ? 0 : kWarmup; |
| 475 | for (const Shape& s : shapes) { |
| 476 | float ms = 0.f; |
| 477 | for (int i = 0; i < warmup; ++i) { |
| 478 | if (launch(fn, s.args, s.grid, start, stop, &ms)) return 1; |
| 479 | } |
| 480 | std::vector<double> us; |
| 481 | us.reserve(kRuns); |
| 482 | for (int i = 0; i < kRuns; ++i) { |
| 483 | if (cold && flush_cache(flush_buf, flush_sink)) return 1; |
| 484 | if (launch(fn, s.args, s.grid, start, stop, &ms)) return 1; |
| 485 | us.push_back(static_cast<double>(ms) * 1000.0); |
| 486 | } |
| 487 | std::vector<double> sorted = us; |
| 488 | std::sort(sorted.begin(), sorted.end()); |
| 489 | double med = sorted[sorted.size() / 2]; |
| 490 | double lo = sorted.front(); |
| 491 | double hi = sorted.back(); |
| 492 | double bytes = static_cast<double>(s.m) * s.k + 4.0 * s.k + 8.0 * s.m; |
| 493 | double gbps = bytes / (med * 1e-6) / 1e9; |
| 494 | double pct = (bytes / (med * 1e-6)) / kPeakBytesPerSec * 100.0; |
| 495 | std::printf( |
| 496 | "kernel_swizzle %u mode %s M %u K %u grid %u warmup %d runs %d " |
| 497 | "min_us %.4f median_us %.4f max_us %.4f spread_us %.4f bytes %.0f " |
| 498 | "GB/s %.4f pct_of_5.3TBs %.4f\n", |
| 499 | s.swizzle, mode, s.m, s.k, s.grid, warmup, kRuns, lo, med, hi, hi - lo, |
| 500 | bytes, gbps, pct); |
| 501 | print_samples(us); |
| 502 | } |
| 503 | } |
| 504 | if (time_hipblaslt(flush_buf, flush_sink, start, stop, rows, |
| 505 | static_cast<int>(sizeof(rows) / sizeof(rows[0])), cols, |
| 506 | static_cast<int>(sizeof(cols) / sizeof(cols[0])))) { |
| 507 | return 1; |
| 508 | } |
| 509 | return 0; |
| 510 | } |