|
| 1 | +# RUN: env SUPPORT_LIB=%mlir_cuda_runtime \ |
| 2 | +# RUN: %PYTHON %s | FileCheck %s |
| 3 | +# CHECK: PASS |
| 4 | + |
| 5 | +# ===--- GEMM Hopper Tensor Core Integration Test ---=== |
| 6 | +# |
| 7 | +# This test aims to validate the correctness of the supported GEMM kernels in |
| 8 | +# NVGPU dialects, with current support for Multistage and Warp Specialization |
| 9 | +# kernels. |
| 10 | +# The test constructs and metaprograms IR using Python bindings, allowing |
| 11 | +# generic IR building. This flexibility enables changes to the shape, |
| 12 | +# tile size, or data type of the GEMM for testing purposes. |
| 13 | +# The entry function is `matmul`, where one can specify GEMM shape, tile size, |
| 14 | +# data type, GEMM algorithm (Multistage or Warp Specialization), and the maximum |
| 15 | +# number of stages. |
| 16 | +# Verification is done via numpy's matmul operation. |
| 17 | +# |
| 18 | +# Example: |
| 19 | +# matmul(input_type=np.float16, # input types |
| 20 | +# output_type=np.float32, # output type |
| 21 | +# M=4096, N=4096, K=4096, # Shape |
| 22 | +# BLOCK_M=128, BLOCK_N=128, BLOCK_K=64, # Tile Size |
| 23 | +# use_warp_specialization=True, # Enable Warp Specialization |
| 24 | +# max_num_stages=3) # Number of stages in shared memory |
| 25 | +# |
| 26 | +# ===--- Parallelism Across CTAs ---=== |
| 27 | +# |
| 28 | +# GEMM includes three loops defining the shape of the GEMM, specified in the |
| 29 | +# `matmul` function. |
| 30 | +# The program builds IR using the following loop structure, tiling the loops |
| 31 | +# with the given tile size and parallelizing the two outermost loops into the |
| 32 | +# first and second dimensions of CTAs. |
| 33 | +# |
| 34 | +# for(bi = 0; i < M; i += BLOCK_M) # parallelize across blockIdx.x |
| 35 | +# for(bj = 0; j < N; j += BLOCK_N) # parallelize across blockIdx.y |
| 36 | +# for(bk = 0; k < K; K += BLOCK_K) |
| 37 | +# for(i = bi; i < (bi + BLOCK_M); ++i) |
| 38 | +# for(j = bj; j < (bj + BLOCK_N); ++j) |
| 39 | +# for(k = bk; k < (bk + BLOCK_K); ++k) |
| 40 | +# |
| 41 | +# ===--- Multistage Kernel ---=== |
| 42 | +# |
| 43 | +# This kernel launches a single warp group (128 threads). The primary thread |
| 44 | +# (pthread) requests load from TMA. Threads collectively wait for the data and |
| 45 | +# perform mma operations. After completing the shape, threads together store |
| 46 | +# first fragmented registers to shared memory, then from shared memory to global |
| 47 | +# memory; this part is called the epilogue. |
| 48 | +# |
| 49 | +# Execution Timeline of Multistage Kernel with 3 stages: |
| 50 | +# +-------+----------------+--------------------+--------------------+--------------------+-----+-----------------------+ |
| 51 | +# | |Prologue ----> |MainLoop ----> |Epilogue | |
| 52 | +# +-------+----------------+--------------------+--------------------+--------------------+-----+-----------------------+ |
| 53 | +# |pthread|[tma-0,1,2] |[wait-0][mma][tma-2]|[wait-1][mma][tma-0]|[wait-2][mma][tma-1]| ... | [mma-wait] |[epilogue]| |
| 54 | +# |wgroup | ........ |[wait-0][mma] |[wait-1][mma] |[wait-2][mma] | ... | [mma-wait] |[epilogue]| |
| 55 | +# +-------+----------------+--------------------+--------------------+--------------------+-----+-----------------------+ |
| 56 | +# |
| 57 | +# ===--- Warp Specialization Kernel ---=== |
| 58 | +# |
| 59 | +# This kernel launches 2 warp groups (2x128 threads) per CTA, specializing one |
| 60 | +# as `producer warp group` and another as `consumer warp group`. The |
| 61 | +# `producer warp group` is responsible for requesting TMA load, while the |
| 62 | +# `consumer warp group` performs the mma operation. The epilogue section is |
| 63 | +# handled by the `consumer warp group` as its threads own the fragmented registers. |
| 64 | +# |
| 65 | +# Execution Timeline of Warp Specialization Kernel with 2 stages: |
| 66 | +# +--------+--------+---------+---------+---------+-----------------------+---+--------------+-----------------+ |
| 67 | +# | |MainLoop ----> | 1st Epilogue | 2nd Epilogue | |
| 68 | +# +--------+--------+---------+---------+---------+-----------------------+---+--------------+-----------------+ |
| 69 | +# |pthread1|[tma-0] | [tma-1] | [tma-0] | [tma-1] | ..........................| ........... | [shmem->global] | |
| 70 | +# |wgroup1 | .......| | | | | | [shmem->global] | |
| 71 | +# +--------+--------+---------+---------+---------+-----------------------+---+--------------+-----------------+ |
| 72 | +# |wgroup2 |[wait-0][mma], [wait-1][mma], [wait-0][mma], [wait-1][mma], ......| [reg->shmem] | [shmem->global]| |
| 73 | +# +--------+--------+---------+---------+---------+-----------------------+---+--------------+-----------------+ |
| 74 | + |
| 75 | +import errno |
| 76 | +import numpy as np |
| 77 | +import subprocess |
| 78 | +import ctypes |
| 79 | +from tools import nvgpucompiler |
| 80 | +from tools import matmulBuilder |
| 81 | +import contextlib |
| 82 | +import os |
| 83 | +import sys |
| 84 | +import pathlib |
| 85 | +import ctypes |
| 86 | +from mlir import runtime as rt |
| 87 | + |
| 88 | +def generate_matmul(input_type=np.float16, |
| 89 | + output_type=np.float32, |
| 90 | + M=4096, |
| 91 | + N=4096, |
| 92 | + K=4096, |
| 93 | + BLOCK_M=128, |
| 94 | + BLOCK_N=128, |
| 95 | + BLOCK_K=64, |
| 96 | + use_warp_specilization=True, |
| 97 | + saveIR=False, |
| 98 | + max_num_stages=3): |
| 99 | + with matmulBuilder.ir.Context() as ctx, matmulBuilder.ir.Location.unknown(): |
| 100 | + if use_warp_specilization: |
| 101 | + mlir_nvgpu_module = matmulBuilder.generate_matmul_ws(input_type, output_type, M, N, K, BLOCK_M, BLOCK_N, |
| 102 | + BLOCK_K, max_num_stages) |
| 103 | + else: |
| 104 | + mlir_nvgpu_module = matmulBuilder.generate_matmul_multistage(input_type, output_type, M, N, K, BLOCK_M, |
| 105 | + BLOCK_N, BLOCK_K, max_num_stages) |
| 106 | + |
| 107 | + mlir_nvgpu_module.operation.verify() |
| 108 | + |
| 109 | + # Save generated IR |
| 110 | + if saveIR: |
| 111 | + # print(mlir_nvgpu_module) |
| 112 | + original_stdout = sys.stdout |
| 113 | + with open('gemm.mlir', 'w') as f: |
| 114 | + sys.stdout = f |
| 115 | + print(mlir_nvgpu_module) |
| 116 | + sys.stdout = original_stdout |
| 117 | + |
| 118 | + # Get compiler |
| 119 | + options = f"cubin-chip=sm_90a cubin-features=+ptx80 opt-level=3" |
| 120 | + support_lib = os.getenv("SUPPORT_LIB") |
| 121 | + if not os.path.exists(support_lib): |
| 122 | + raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), support_lib) |
| 123 | + compiler = nvgpucompiler.NvgpuCompiler(options, opt_level=3, shared_libs=[support_lib]) |
| 124 | + |
| 125 | + # Compile |
| 126 | + engine = compiler.compile_and_jit(mlir_nvgpu_module) |
| 127 | + return engine |
| 128 | + |
| 129 | + |
| 130 | +def matmul(input_type=np.float16, |
| 131 | + output_type=np.float32, |
| 132 | + M=128, |
| 133 | + N=128, |
| 134 | + K=128, |
| 135 | + BLOCK_M=128, |
| 136 | + BLOCK_N=128, |
| 137 | + BLOCK_K=64, |
| 138 | + use_warp_specilization=True, |
| 139 | + saveIR=False, |
| 140 | + max_num_stages=3, |
| 141 | + print_results=False, |
| 142 | + no_verify=False): |
| 143 | + # Print the configuration |
| 144 | + ity = "f16" if input_type == np.float16 else "f32" |
| 145 | + oty = "f16" if output_type == np.float16 else "f32" |
| 146 | + gemmty = "Warp Specilization" if use_warp_specilization else "Multistage" |
| 147 | + print("===-- Running GEMM " + gemmty + " " + oty + " += " + ity + " * " + ity + ", Size " + str(M) + "x" + str(N) + |
| 148 | + "x" + str(K) + ", Tile " + str(BLOCK_M) + "x" + str(BLOCK_N) + "x" + str(BLOCK_K) + ", stages " + |
| 149 | + str(max_num_stages) + " --===") |
| 150 | + |
| 151 | + # Build IR and compile |
| 152 | + engine = generate_matmul(input_type, output_type, M, N, K, BLOCK_M, BLOCK_N, BLOCK_K, use_warp_specilization, |
| 153 | + saveIR, max_num_stages) |
| 154 | + |
| 155 | + # Allocate matrices and invoke the matmul |
| 156 | + c = np.zeros((M, N), output_type) |
| 157 | + a = np.random.randn(M, K).astype(input_type) |
| 158 | + b = np.random.randn(K, N).astype(input_type) |
| 159 | + mem_a = ctypes.pointer(ctypes.pointer(rt.get_ranked_memref_descriptor(a))) |
| 160 | + mem_b = ctypes.pointer(ctypes.pointer(rt.get_ranked_memref_descriptor(b))) |
| 161 | + mem_c = ctypes.pointer(ctypes.pointer(rt.get_ranked_memref_descriptor(c))) |
| 162 | + kernelName = "mlir_matmul_warpspecialized" if use_warp_specilization else "mlir_matmul_multistage" |
| 163 | + |
| 164 | + # Launch the MLIR generated kernel |
| 165 | + engine.invoke(kernelName, mem_a, mem_b, mem_c) |
| 166 | + |
| 167 | + float_formatter = "{:.2f}".format |
| 168 | + np.set_printoptions(formatter={'float_kind': float_formatter}) |
| 169 | + |
| 170 | + if print_results: |
| 171 | + print(c) |
| 172 | + |
| 173 | + # Verify the results |
| 174 | + if not no_verify: |
| 175 | + ref = a.astype(input_type) @ b.astype(input_type) |
| 176 | + if print_results: |
| 177 | + print(ref) |
| 178 | + np.testing.assert_allclose(c, ref, rtol=5e-03, atol=1e-01) |
| 179 | + |
| 180 | + print("PASS ") |
| 181 | + |
| 182 | + |
| 183 | +# GEMM Multistage f32 += f16 * f16 |
| 184 | +matmul(np.float16, np.float32, 128, 128, 4096, max_num_stages=3, use_warp_specilization=False) |
| 185 | +# GEMM Warp Specilized f32 += f16 * f16 |
| 186 | +matmul(np.float16, np.float32, 256, 1024, 512, max_num_stages=3, use_warp_specilization=True) |
0 commit comments