Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 19 additions & 27 deletions python/tvm/tir/tensor_intrin/cuda.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
# under the License.
# pylint: disable=invalid-name,missing-function-docstring
"""Intrinsics for tensorization on NVIDIA GPU."""
import re
from typing import Dict, Tuple

from typing_extensions import Literal
Expand Down Expand Up @@ -44,16 +43,6 @@ def shared_32x16_to_ldmatrix_32x16_layout(i, j):
return thread_id, 8 * (j // 8) + (i // 16) * 4 + i % 4


def get_tensor_core_load_offset_factor(dtype):
"""get offset factor for tensor core load intrin"""
bits = re.search(r"(\d+)", dtype).group(0)
bits = int(bits)
if bits <= 4:
# sub-byte oeprations have different offset factor
return 128 // bits
return 256 // bits


@register_func("tir.index_map.shared_16x16_to_ldmatrix_32x8_layout")
def index_map_shared_16x16_to_ldmatrix_32x8_layout(ind):
i, j = ind[0], ind[1]
Expand Down Expand Up @@ -127,7 +116,7 @@ def get_ldmatrix_intrin(k_dim, dtype, is_b, transposed, shared_scope="shared"):
col_dim = k_dim

shmem_shape = (row_dim, col_dim)
offset_factor = get_tensor_core_load_offset_factor(dtype)
offset_factor = col_dim

@T.prim_func
def ldmatrix_desc(warp_handle: T.handle, shared_handle: T.handle) -> None:
Expand Down Expand Up @@ -244,8 +233,9 @@ def maybe_swap(i, j):
return j, i
return i, j

in_offset_factor = get_tensor_core_load_offset_factor(in_dtype)
out_offset_factor = get_tensor_core_load_offset_factor(out_dtype)
A_offset_factor = k_dim
B_offset_factor = maybe_swap(k_dim, N_DIM)[-1]
out_offset_factor = N_DIM

@T.prim_func
def mma_sync_desc(a: T.handle, b: T.handle, c: T.handle) -> None:
Expand All @@ -254,15 +244,15 @@ def mma_sync_desc(a: T.handle, b: T.handle, c: T.handle) -> None:
(WARP_SIZE, local_size),
in_dtype,
align=64,
offset_factor=in_offset_factor,
offset_factor=A_offset_factor,
scope="warp",
)
B = T.match_buffer(
b,
(WARP_SIZE, local_size),
in_dtype,
align=64,
offset_factor=in_offset_factor,
offset_factor=B_offset_factor,
scope="warp",
)
C = T.match_buffer(
Expand Down Expand Up @@ -309,15 +299,15 @@ def mma_sync_impl(a: T.handle, b: T.handle, c: T.handle) -> None:
(WARP_SIZE, local_size),
in_dtype,
align=64,
offset_factor=in_offset_factor,
offset_factor=A_offset_factor,
scope="warp",
)
B = T.match_buffer(
b,
(WARP_SIZE, local_size),
in_dtype,
align=64,
offset_factor=in_offset_factor,
offset_factor=B_offset_factor,
scope="warp",
)
C = T.match_buffer(
Expand Down Expand Up @@ -568,11 +558,11 @@ def get_wmma_load_intrin(
"""Generator of wmma_load intrins"""
wmma_fragment_scope = f"wmma.matrix_{'b' if is_b else 'a'}"
layout = "col_major" if is_col_major else "row_major"
offset_factor = get_tensor_core_load_offset_factor(dtype)

frag_m, frag_n = (k_dim, n_dim) if is_b else (m_dim, k_dim)
if is_col_major:
frag_m, frag_n = frag_n, frag_m
offset_factor = frag_n

@T.prim_func
def wmma_load_desc(a: T.handle, c: T.handle) -> None:
Expand Down Expand Up @@ -644,7 +634,7 @@ def get_wmma_fill_intrin(
) -> Tuple[PrimFunc, PrimFunc]:
"""Generator of wmma_fill intrins"""
zero = IntImm("int32", 0).astype(dtype)
offset_factor = get_tensor_core_load_offset_factor(dtype)
offset_factor = n_dim

@T.prim_func
def wmma_fill_desc(c: T.handle) -> None:
Expand Down Expand Up @@ -699,7 +689,7 @@ def get_wmma_store_intrin(
m_dim: int, n_dim: int, k_dim: int, dtype: str, scope: str
) -> Tuple[PrimFunc, PrimFunc]:
"""Generator of wmma_store intrins"""
offset_factor = get_tensor_core_load_offset_factor(dtype)
offset_factor = n_dim

@T.prim_func
def wmma_store_desc(a: T.handle, c: T.handle) -> None:
Expand Down Expand Up @@ -770,8 +760,6 @@ def get_wmma_sync_intrin(
m_dim: int, n_dim: int, k_dim: int, in_dtype: str, out_dtype: str, b_transposed: bool
) -> Tuple[PrimFunc, PrimFunc]:
"""Generator of wmma_sync intrins"""
in_offset_factor = get_tensor_core_load_offset_factor(in_dtype)
out_offset_factor = get_tensor_core_load_offset_factor(out_dtype)

def maybe_cast(v):
if in_dtype != out_dtype:
Expand All @@ -785,22 +773,26 @@ def maybe_swap(i, j):

b_shape_0, b_shape_1 = maybe_swap(k_dim, n_dim)

A_offset_factor = k_dim
B_offset_factor = b_shape_1
out_offset_factor = n_dim

@T.prim_func
def wmma_sync_desc(a: T.handle, b: T.handle, c: T.handle) -> None:
A = T.match_buffer(
a,
(m_dim, k_dim),
in_dtype,
align=64,
offset_factor=in_offset_factor,
offset_factor=A_offset_factor,
scope="wmma.matrix_a",
)
B = T.match_buffer(
b,
maybe_swap(k_dim, n_dim),
in_dtype,
align=64,
offset_factor=in_offset_factor,
offset_factor=B_offset_factor,
scope="wmma.matrix_b",
)
C = T.match_buffer(
Expand Down Expand Up @@ -837,7 +829,7 @@ def wmma_sync_impl(a: T.handle, b: T.handle, c: T.handle) -> None:
(m_dim, k_dim),
in_dtype,
align=64,
offset_factor=in_offset_factor,
offset_factor=A_offset_factor,
scope="wmma.matrix_a",
strides=[a1, a0],
)
Expand All @@ -846,7 +838,7 @@ def wmma_sync_impl(a: T.handle, b: T.handle, c: T.handle) -> None:
maybe_swap(k_dim, n_dim),
in_dtype,
align=64,
offset_factor=in_offset_factor,
offset_factor=B_offset_factor,
scope="wmma.matrix_b",
strides=[b1, b0],
)
Expand Down