|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +# pylint: disable=invalid-name,missing-function-docstring |
| 18 | +"""Intrinsics for tensorization on NVIDIA GPU.""" |
| 19 | +from ..._ffi import register_func |
| 20 | +from ...runtime import convert |
| 21 | +from .. import TensorIntrin |
| 22 | +from tvm.script import tir as T |
| 23 | + |
| 24 | + |
| 25 | +def shared_16x16_to_ldmatrix_32x8_layout(i, j): |
| 26 | + thread_id = 4 * (i % 8) + (j % 8) // 2 |
| 27 | + return thread_id, 4 * (j // 8) + (i // 8) * 2 + (j % 2) |
| 28 | + |
| 29 | + |
| 30 | +def shared_16x32_to_ldmatrix_32x16_layout(i, j): |
| 31 | + thread_id = 4 * (i % 8) + (j % 16) // 4 |
| 32 | + return thread_id, 8 * (j // 16) + (i // 8) * 4 + j % 4 |
| 33 | + |
| 34 | + |
| 35 | +def shared_32x16_to_ldmatrix_32x16_layout(i, j): |
| 36 | + thread_id = (i % 4) + 4 * (j % 8) |
| 37 | + return thread_id, 8 * (j // 8) + (i // 16) * 4 + i % 4 |
| 38 | + |
| 39 | + |
| 40 | +@register_func("tir.index_map.shared_16x16_to_ldmatrix_32x8_layout") |
| 41 | +def index_map_shared_16x16_to_ldmatrix_32x8_layout(i, j): |
| 42 | + thread_id, local_id = shared_16x16_to_ldmatrix_32x8_layout(i, j) |
| 43 | + return convert([thread_id, local_id]) |
| 44 | + |
| 45 | + |
| 46 | +lift = convert |
| 47 | + |
| 48 | +M_DIM = 16 |
| 49 | +WARP_SIZE = 32 |
| 50 | +HALF_WARP = WARP_SIZE // 2 |
| 51 | +HALF_WARP_expr = lift(HALF_WARP) |
| 52 | + |
| 53 | + |
| 54 | +def get_ldmatrix_intrin(k_dim, dtype, is_b, transposed): |
| 55 | + local_size = (M_DIM * k_dim) // WARP_SIZE |
| 56 | + shared_offset = None |
| 57 | + index_map = None |
| 58 | + |
| 59 | + if transposed: |
| 60 | + assert is_b, "Transposed A matrix not supported" |
| 61 | + |
| 62 | + ldmatrix_col_major = is_b and not transposed |
| 63 | + |
| 64 | + if k_dim == 16: |
| 65 | + assert dtype == "float16" |
| 66 | + |
| 67 | + index_map = shared_16x16_to_ldmatrix_32x8_layout |
| 68 | + |
| 69 | + if transposed: |
| 70 | + shared_offset = ( |
| 71 | + lambda tx, stride: stride * 8 * (tx // HALF_WARP_expr) |
| 72 | + + stride * (tx % 8) |
| 73 | + + 8 * ((tx % HALF_WARP_expr) // 8) |
| 74 | + ) |
| 75 | + else: |
| 76 | + shared_offset = lambda tx, stride: stride * (tx % HALF_WARP_expr) + 8 * ( |
| 77 | + tx // HALF_WARP_expr |
| 78 | + ) |
| 79 | + |
| 80 | + elif k_dim == 32: |
| 81 | + assert dtype == "int8" |
| 82 | + |
| 83 | + if ldmatrix_col_major: |
| 84 | + print("foo") |
| 85 | + index_map = shared_32x16_to_ldmatrix_32x16_layout |
| 86 | + shared_offset = ( |
| 87 | + lambda _, stride: stride |
| 88 | + ) # dummy offset, ldmatrix cannot be used for int8 + trans case |
| 89 | + elif is_b and transposed: |
| 90 | + index_map = shared_16x32_to_ldmatrix_32x16_layout |
| 91 | + shared_offset = ( |
| 92 | + lambda tx, stride: stride * 8 * (tx // HALF_WARP_expr) |
| 93 | + + (tx % 8) * stride |
| 94 | + + 16 * ((tx % HALF_WARP_expr) // 8) |
| 95 | + ) |
| 96 | + else: |
| 97 | + index_map = shared_16x32_to_ldmatrix_32x16_layout |
| 98 | + shared_offset = lambda tx, stride: stride * (tx % 16) + 16 * (tx // 16) |
| 99 | + |
| 100 | + else: |
| 101 | + assert False, "Unsupported k dim" |
| 102 | + |
| 103 | + assert index_map and shared_offset |
| 104 | + |
| 105 | + if is_b and not transposed: |
| 106 | + row_dim = k_dim |
| 107 | + col_dim = M_DIM |
| 108 | + else: |
| 109 | + row_dim = M_DIM |
| 110 | + col_dim = k_dim |
| 111 | + |
| 112 | + shmem_shape = (row_dim, col_dim) |
| 113 | + |
| 114 | + @T.prim_func |
| 115 | + def ldmatrix_desc(warp_handle: T.handle, shared_handle: T.handle) -> None: |
| 116 | + shared = T.match_buffer( |
| 117 | + shared_handle, shmem_shape, dtype, align=128, offset_factor=16, scope="shared" |
| 118 | + ) |
| 119 | + warp = T.match_buffer( |
| 120 | + warp_handle, (WARP_SIZE, local_size), dtype, align=128, offset_factor=16, scope="warp" |
| 121 | + ) |
| 122 | + |
| 123 | + with T.block("root"): |
| 124 | + T.reads(shared[0:row_dim, 0:col_dim]) |
| 125 | + T.writes(warp[0:WARP_SIZE, 0:local_size]) |
| 126 | + |
| 127 | + for ax0, ax1 in T.grid(row_dim, col_dim): |
| 128 | + with T.block("shared_warp"): |
| 129 | + v0, v1 = T.axis.remap("SS", [ax0, ax1]) |
| 130 | + T.reads(shared[v0, v1]) |
| 131 | + |
| 132 | + thread_id, local_id = index_map(v0, v1) |
| 133 | + T.writes(warp[thread_id, local_id]) |
| 134 | + warp[thread_id, local_id] = shared[v0, v1] |
| 135 | + |
| 136 | + @T.prim_func |
| 137 | + def ldmatrix_impl(warp_handle: T.handle, shared_handle: T.handle) -> None: |
| 138 | + s0 = T.var("int32") |
| 139 | + s1 = T.var("int32") |
| 140 | + shared = T.match_buffer( |
| 141 | + shared_handle, |
| 142 | + shmem_shape, |
| 143 | + dtype, |
| 144 | + align=128, |
| 145 | + offset_factor=16, |
| 146 | + scope="shared", |
| 147 | + strides=[s0, s1], |
| 148 | + ) |
| 149 | + warp = T.match_buffer( |
| 150 | + warp_handle, (WARP_SIZE, local_size), dtype, align=128, offset_factor=16, scope="warp" |
| 151 | + ) |
| 152 | + |
| 153 | + with T.block("root"): |
| 154 | + T.reads(shared[0:row_dim, 0:col_dim]) |
| 155 | + T.writes(warp[0:WARP_SIZE, 0:local_size]) |
| 156 | + tx = T.env_thread("threadIdx.x") |
| 157 | + T.launch_thread(tx, WARP_SIZE) |
| 158 | + |
| 159 | + T.evaluate( |
| 160 | + T.ptx_ldmatrix( |
| 161 | + ldmatrix_col_major, |
| 162 | + 4, # Always load 4 matrices |
| 163 | + ".b16", |
| 164 | + warp.data, |
| 165 | + warp.elem_offset + lift(local_size) * tx, |
| 166 | + shared.access_ptr("r"), |
| 167 | + shared_offset(tx, s0), |
| 168 | + dtype=dtype, |
| 169 | + ) |
| 170 | + ) |
| 171 | + |
| 172 | + return ldmatrix_desc, ldmatrix_impl |
| 173 | + |
| 174 | + |
| 175 | +LDMATRIX_16x16_A_INTRIN = "mma.ldmatrix_16x16_a" |
| 176 | +TensorIntrin.register(LDMATRIX_16x16_A_INTRIN, *get_ldmatrix_intrin(16, "float16", False, False)) |
| 177 | + |
| 178 | +LDMATRIX_16x16_B_INTRIN = "mma.ldmatrix_16x16_b" |
| 179 | +TensorIntrin.register(LDMATRIX_16x16_B_INTRIN, *get_ldmatrix_intrin(16, "float16", True, False)) |
| 180 | + |
| 181 | +LDMATRIX_16x16_B_TRANS_INTRIN = "mma.ldmatrix_16x16_b_trans" |
| 182 | +TensorIntrin.register( |
| 183 | + LDMATRIX_16x16_B_TRANS_INTRIN, *get_ldmatrix_intrin(16, "float16", True, True) |
| 184 | +) |
| 185 | + |
| 186 | +LDMATRIX_16x32_A_INTRIN = "mma.ldmatrix_16x32_a" |
| 187 | +TensorIntrin.register(LDMATRIX_16x32_A_INTRIN, *get_ldmatrix_intrin(32, "int8", False, False)) |
| 188 | + |
| 189 | +LDMATRIX_32x16_B_INTRIN = "mma.ldmatrix_32x16_b" |
| 190 | +TensorIntrin.register(LDMATRIX_32x16_B_INTRIN, *get_ldmatrix_intrin(32, "int8", True, False)) |
| 191 | + |
| 192 | +LDMATRIX_16x32_B_TRANS_INTRIN = "mma.ldmatrix_16x32_b_trans" |
| 193 | +TensorIntrin.register(LDMATRIX_16x32_B_TRANS_INTRIN, *get_ldmatrix_intrin(32, "int8", True, True)) |
0 commit comments