|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import mlir.extras.types as T |
| 4 | +import numpy as np |
| 5 | +from hip import hip |
| 6 | +from mlir.ir import InsertionPoint |
| 7 | + |
| 8 | +from mlir.extras.ast.canonicalize import canonicalize |
| 9 | +from mlir.extras.context import RAIIMLIRContextModule |
| 10 | +from mlir.extras.dialects.ext import memref, scf, arith, rocdl |
| 11 | + |
| 12 | +# noinspection PyUnresolvedReferences |
| 13 | +from mlir.extras.dialects.ext.gpu import ( |
| 14 | + all_reduce, |
| 15 | + wait, |
| 16 | + thread_attr as thread, |
| 17 | + block_idx, |
| 18 | + thread_idx, |
| 19 | + block_dim, |
| 20 | + GPUModuleMeta, |
| 21 | + func as gpu_func, |
| 22 | + set_container_module, |
| 23 | + launch, |
| 24 | + all_reduce_, |
| 25 | + module, |
| 26 | + get_compile_object_bytes, |
| 27 | + lds_space, |
| 28 | +) |
| 29 | +from mlir.extras.runtime.passes import run_pipeline, Pipeline |
| 30 | + |
| 31 | +# noinspection PyUnresolvedReferences |
| 32 | +from util import hip_check, launch_kernel, hip_synchronize |
| 33 | + |
| 34 | + |
| 35 | +def time_to_gflops(time_ms, N): |
| 36 | + return 1e-6 * (N * N * N * 2 + 3 * N * N) // time_ms |
| 37 | + |
| 38 | + |
| 39 | +# just so it doesn't get DCE'd by black/reformat |
| 40 | +# TypeError: 'mlir._mlir_libs._mlir.ir.BlockArgument' object is not subscriptable |
| 41 | +_ = memref |
| 42 | + |
| 43 | +ctx = RAIIMLIRContextModule() |
| 44 | +set_container_module(ctx.module) |
| 45 | + |
| 46 | +props = hip.hipDeviceProp_t() |
| 47 | +hip_check(hip.hipGetDeviceProperties(props, 0)) |
| 48 | +arch = props.gcnArchName.decode() |
| 49 | + |
| 50 | + |
| 51 | +# just a default attr - actual target is set blow |
| 52 | +@module("kernels", [f'#rocdl.target<abi = "500">']) |
| 53 | +def gpu_module(): |
| 54 | + pass |
| 55 | + |
| 56 | + |
| 57 | +ip = InsertionPoint.at_block_begin(gpu_module.regions[0].blocks[0]) |
| 58 | +ip.__enter__() |
| 59 | + |
| 60 | +set_container_module(ctx.module) |
| 61 | + |
| 62 | +v_len = 16 |
| 63 | +M, K, N = 1024, 1024, 1024 |
| 64 | +v16f16 = T.vector(v_len, T.f16()) |
| 65 | + |
| 66 | + |
| 67 | +@gpu_func |
| 68 | +@canonicalize(using=scf.canonicalizer) |
| 69 | +def smol_matmul( |
| 70 | + a: T.memref(M, K, T.f16()), |
| 71 | + b: T.memref(K, N, T.f16()), |
| 72 | + c: T.memref(M, N, T.f16()), |
| 73 | +): |
| 74 | + lIdx = thread_idx.x |
| 75 | + # a and b fragments are stored in 8 VGPRs each, in packed format, so 16 elements each for a and b |
| 76 | + # a_frag will store one column of the 16x16 matrix A tile |
| 77 | + # b_frag will store one row of the 16x16 matrix B tile |
| 78 | + a_frag = arith.constant(np.full([v_len], 0.0, np.float16), v16f16) |
| 79 | + b_frag = arith.constant(np.full([v_len], 0.0, np.float16), v16f16) |
| 80 | + c_frag = arith.constant(np.full([v_len], 0.0, np.float16), v16f16) |
| 81 | + |
| 82 | + # lane is (0-31) mod 16 instead of 0-31 due to matrix replication in RDNA 3 |
| 83 | + lane = lIdx % v_len |
| 84 | + for ele in range(v_len): |
| 85 | + b_frag[ele] = b[ele, lane] |
| 86 | + a_frag[ele] = a[lane, ele] |
| 87 | + # a_frag, b_frag = yield a_frag, b_frag |
| 88 | + |
| 89 | + # call the WMMA intrinsic |
| 90 | + false = arith.constant(False, T.bool()) |
| 91 | + c_frag = rocdl.wmma_f16_16x16x16_f16(v16f16, [a_frag, b_frag, c_frag, false]) |
| 92 | + |
| 93 | + for ele in range(v_len // 2): |
| 94 | + r = ele * 2 + (lIdx // v_len) |
| 95 | + # store results from unpacked c_frag output |
| 96 | + c[r, lane] = c_frag[ele * 2] |
| 97 | + |
| 98 | + |
| 99 | +props = hip.hipDeviceProp_t() |
| 100 | +hip_check(hip.hipGetDeviceProperties(props, 0)) |
| 101 | +arch = props.gcnArchName.decode().split(":")[0] |
| 102 | + |
| 103 | + |
| 104 | +@module("naive", [f'#rocdl.target<chip = "{arch}", abi = "500">']) |
| 105 | +def gpu_module(): |
| 106 | + smol_matmul.emit() |
| 107 | + |
| 108 | + |
| 109 | +ip.__exit__(None, None, None) |
| 110 | + |
| 111 | +lowered_module = run_pipeline( |
| 112 | + gpu_module, |
| 113 | + Pipeline() |
| 114 | + .Gpu(Pipeline().convert_gpu_to_rocdl(use_bare_ptr_memref_call_conv=True)) |
| 115 | + .rocdl_attach_target(chip=arch, abi="500", O=0) |
| 116 | + .gpu_to_llvm() |
| 117 | + .lower_to_llvm() |
| 118 | + .ensure_debug_info_scope_on_llvm_func(emission_kind="Full") |
| 119 | + .gpu_module_to_binary(), |
| 120 | +) |
| 121 | + |
| 122 | +hsaco = get_compile_object_bytes(lowered_module) |
| 123 | +hip_module = hip_check(hip.hipModuleLoadData(hsaco)) |
| 124 | +function = hip_check( |
| 125 | + hip.hipModuleGetFunction(hip_module, smol_matmul.__name__.encode()) |
| 126 | +) |
| 127 | + |
| 128 | +a_h = np.random.randint(0, 10, (M, K)).astype(dtype=np.float16) |
| 129 | +b_h = np.random.randint(0, 10, (K, N)).astype(dtype=np.float16) |
| 130 | +c_h = -3 * np.ones((M, N), dtype=np.float16) |
| 131 | + |
| 132 | +a_num_bytes = a_h.size * a_h.itemsize |
| 133 | +b_num_bytes = b_h.size * b_h.itemsize |
| 134 | +c_num_bytes = c_h.size * c_h.itemsize |
| 135 | + |
| 136 | +a_d = hip_check(hip.hipMalloc(a_num_bytes)) |
| 137 | +b_d = hip_check(hip.hipMalloc(b_num_bytes)) |
| 138 | +c_d = hip_check(hip.hipMalloc(c_num_bytes)) |
| 139 | + |
| 140 | +hip_check(hip.hipMemcpy(a_d, a_h, a_num_bytes, hip.hipMemcpyKind.hipMemcpyHostToDevice)) |
| 141 | +hip_check(hip.hipMemcpy(b_d, b_h, b_num_bytes, hip.hipMemcpyKind.hipMemcpyHostToDevice)) |
| 142 | +hip_check(hip.hipMemcpy(c_d, c_h, c_num_bytes, hip.hipMemcpyKind.hipMemcpyHostToDevice)) |
| 143 | + |
| 144 | +gridX = 32 |
| 145 | +gridY = 32 |
| 146 | +gridZ = 1 |
| 147 | +warp_size = 32 |
| 148 | +num_warps = 1 |
| 149 | +stream = 0 |
| 150 | +shared_memory = 0 |
| 151 | + |
| 152 | +launch_kernel( |
| 153 | + function.as_c_void_p(), |
| 154 | + gridX, |
| 155 | + gridY, |
| 156 | + gridZ, |
| 157 | + warp_size, |
| 158 | + num_warps, |
| 159 | + 1, |
| 160 | + stream, |
| 161 | + shared_memory, |
| 162 | + a_d, |
| 163 | + b_d, |
| 164 | + c_d, |
| 165 | +) |
| 166 | + |
| 167 | +correct = a_h @ b_h |
| 168 | +assert np.allclose(c_h, -3.0) |
| 169 | +assert not np.allclose(correct, c_h) |
| 170 | +hip_check(hip.hipMemcpy(c_h, c_d, c_num_bytes, hip.hipMemcpyKind.hipMemcpyDeviceToHost)) |
| 171 | + |
| 172 | +# if not np.allclose(c_h, correct): |
| 173 | +# with np.printoptions(threshold=np.inf, linewidth=200): |
| 174 | +# print(correct) |
| 175 | +# print(c_h) |
| 176 | +# assert False |
| 177 | + |
| 178 | +hip_check(hip.hipFree(a_d)) |
| 179 | +hip_check(hip.hipFree(b_d)) |
| 180 | +hip_check(hip.hipFree(c_d)) |
| 181 | + |
| 182 | +hip_check(hip.hipModuleUnload(hip_module)) |
0 commit comments