Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
d8884e6
Refactor BatchMatMulEmitter and BatchMatMulSelector for improved read…
LeiWang1999 Jul 5, 2024
fc84173
Refactor import statements for improved readability and maintainability
LeiWang1999 Jul 5, 2024
02f64de
Refactor import statements for improved readability and maintainability
LeiWang1999 Jul 5, 2024
397eee6
disable failure email for ci
LeiWang1999 Jul 5, 2024
20f6ad1
remove email notifications.
LeiWang1999 Jul 6, 2024
b93c394
move relax pass from testing to mlc_llm
LeiWang1999 Jul 6, 2024
ba6a6df
Merge branch 'main' of https://github.com/Microsoft/BitBLAS into main
LeiWang1999 Jul 6, 2024
257693a
Refactor scripts with se check_eual_ref_scripts_with_emitter function
LeiWang1999 Jul 6, 2024
9bb7f49
Lint Fix
LeiWang1999 Jul 6, 2024
39e7614
Merge branch 'main' of https://github.com/Microsoft/BitBLAS into main
LeiWang1999 Jul 6, 2024
93eb5a5
Refactor scripts with se check_eual_ref_scripts_with_emitter function
LeiWang1999 Jul 6, 2024
72b9740
Merge branch 'main' of https://github.com/Microsoft/BitBLAS into main
LeiWang1999 Aug 23, 2024
5b65979
Merge branch 'main' of https://github.com/Microsoft/BitBLAS into main
LeiWang1999 Aug 27, 2024
d9bd479
Merge branch 'main' of https://github.com/Microsoft/BitBLAS into main
LeiWang1999 Aug 29, 2024
99515cb
buf fix for matrix support
LeiWang1999 Aug 29, 2024
14406ef
lint fix
LeiWang1999 Aug 29, 2024
d30ec4f
dispatch tensor core based on shapes
LeiWang1999 Aug 29, 2024
fde4029
update install commands
LeiWang1999 Aug 30, 2024
6a04749
import scripts
LeiWang1999 Aug 31, 2024
9d90c40
Merge branch 'main' of https://github.com/Microsoft/BitBLAS into docs
LeiWang1999 Aug 31, 2024
9ef14e9
remove shared mem hack
LeiWang1999 Sep 1, 2024
63f363e
revert change for swizzling
LeiWang1999 Sep 1, 2024
b29c66c
bug fix
LeiWang1999 Sep 1, 2024
4643dd9
Merge branch 'main' of https://github.com/Microsoft/BitBLAS into docs
LeiWang1999 Sep 1, 2024
28beb13
tl examples
LeiWang1999 Sep 2, 2024
c0b476f
Enhance Swizzle
LeiWang1999 Sep 2, 2024
2bf14a8
lint fix
LeiWang1999 Sep 2, 2024
52accbf
Merge branch 'main' of https://github.com/Microsoft/BitBLAS into tl-l…
LeiWang1999 Sep 2, 2024
19aa985
test fix
LeiWang1999 Sep 3, 2024
ef8f93c
lint fix
LeiWang1999 Sep 3, 2024
4015cc4
optimize layout
LeiWang1999 Sep 3, 2024
5c5880c
update tl utils.
LeiWang1999 Sep 3, 2024
1042ffd
macro optimization
LeiWang1999 Sep 3, 2024
1ecd76e
Merge branch 'main' of https://github.com/Microsoft/BitBLAS into tl-l…
LeiWang1999 Sep 3, 2024
7bb21e7
test fix
LeiWang1999 Sep 4, 2024
6a22442
gemm_ss
LeiWang1999 Sep 4, 2024
b9ea093
Merge branch 'main' of https://github.com/Microsoft/BitBLAS into tl-l…
LeiWang1999 Sep 4, 2024
e9b56b4
doc fix
LeiWang1999 Sep 4, 2024
3eb6888
lint fix
LeiWang1999 Sep 6, 2024
5322785
Merge branch 'main' of https://github.com/Microsoft/BitBLAS into tl-l…
LeiWang1999 Sep 6, 2024
6f18d15
lint fix
LeiWang1999 Sep 6, 2024
187f448
remove debug print
LeiWang1999 Sep 6, 2024
e1fac68
remove debug print
LeiWang1999 Sep 6, 2024
4f25626
vectorization init
LeiWang1999 Sep 6, 2024
2686030
Merge branch 'main' of https://github.com/Microsoft/BitBLAS into tl-l…
LeiWang1999 Sep 6, 2024
23a8e8b
lint fix
LeiWang1999 Sep 6, 2024
069ad5e
prelude update
LeiWang1999 Sep 6, 2024
23fe3f8
Merge branch 'main' of https://github.com/Microsoft/BitBLAS into tl-l…
LeiWang1999 Sep 6, 2024
9119dd3
update tvm
LeiWang1999 Sep 16, 2024
15f4c1f
bug fix for reduce_k with shared memory
LeiWang1999 Sep 16, 2024
f8518ae
bug fix
LeiWang1999 Sep 16, 2024
ea50147
bug fix
LeiWang1999 Sep 16, 2024
f888af1
Enhance Macro Generation
LeiWang1999 Sep 16, 2024
a0bfabf
Lift Layout to reduce load time
LeiWang1999 Sep 16, 2024
b1fdbcf
lint fix
LeiWang1999 Sep 16, 2024
137b6fd
Merge branch 'main' of https://github.com/Microsoft/BitBLAS into tl-l…
LeiWang1999 Sep 16, 2024
0acc369
test fix
LeiWang1999 Sep 16, 2024
62de446
red fix
LeiWang1999 Sep 17, 2024
958f6f2
Merge branch 'main' of https://github.com/Microsoft/BitBLAS into tl-l…
LeiWang1999 Sep 26, 2024
f21b25c
tile lang macro example
LeiWang1999 Sep 26, 2024
0fb9535
tile lang macro example
LeiWang1999 Sep 26, 2024
2c93dad
optimize the marcro generator related items
LeiWang1999 Sep 26, 2024
e5bbf81
lint fix
LeiWang1999 Sep 26, 2024
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
2 changes: 1 addition & 1 deletion 3rdparty/tvm
5 changes: 4 additions & 1 deletion bitblas/tl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@
get_ldmatrix_offset, # noqa: F401
)

from .macro_generator import TensorCorePTXMacroGenerator # noqa: F401
from .macro_generator import (
TensorCoreIntrinEmitter, # noqa: F401
TensorCoreIntrinEmitterWithLadderTransform, # noqa: F401
)
208 changes: 39 additions & 169 deletions bitblas/tl/macro_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
lift = convert


class TensorCorePTXMacroGenerator(object):
class TensorCoreIntrinEmitter(object):
"""
To eliminate Python syntax within TIR Macro.
"""
Expand Down Expand Up @@ -45,8 +45,6 @@ def __init__(self,
warp_col_tiles=8,
chunk=16,
reduce_k=1,
transform_kind_a: Union[int, TransformKind] = 0,
transform_kind_b: Union[int, TransformKind] = 0,
num_elems_per_byte=1):
self.a_dtype = a_dtype
self.b_dtype = b_dtype
Expand All @@ -68,7 +66,6 @@ def __init__(self,
self.warp_cols = warp_col_tiles // self.micro_size_y
self.reduce_k = reduce_k
self.threads = self.WARP_SIZE * (block_row_warps * block_col_warps) * reduce_k
self._initialize_transform_kind(transform_kind_a, transform_kind_b)
self.num_elems_per_byte = num_elems_per_byte

def _initialize_k_dim(self, a_dtype="float16"):
Expand Down Expand Up @@ -99,26 +96,8 @@ def _initialize_micro_size(self, m_dim=16, n_dim=16, k_dim=16):
self.micro_size_y = n_dim
self.micro_size_k = k_dim

def _initialize_transform_kind(self, transform_kind_a, transform_kind_b):
if isinstance(transform_kind_a, int):
self.transform_kind_a = TransformKind(transform_kind_a)
elif isinstance(transform_kind_a, TransformKind):
self.transform_kind_a = transform_kind_a
else:
raise ValueError("Unsupported transform_kind_a")

if isinstance(transform_kind_b, int):
self.transform_kind_b = TransformKind(transform_kind_b)
elif isinstance(transform_kind_b, TransformKind):
self.transform_kind_b = transform_kind_b
else:
raise ValueError("Unsupported transform_kind_b")

assert transform_kind_b in [0, 3], "Currently only support 0 and 3"

@staticmethod
@T.macro
def LDMATRIX_A(
def _warp_ldmatrix_a(
inst,
A_local_buf,
A_shared_buf,
Expand All @@ -143,9 +122,8 @@ def LDMATRIX_A(
get_ldmatrix_offset("A", tx, 0, stride, inst.a_dtype, inst.a_transposed),
)

@staticmethod
@T.macro
def LDMATRIX_B(
def _warp_ldmatrix_b(
inst,
B_local_buf,
B_shared_buf,
Expand Down Expand Up @@ -173,9 +151,8 @@ def LDMATRIX_B(
get_ldmatrix_offset("B", tx, 0, stride, inst.b_dtype, inst.b_transposed),
)

@staticmethod
@T.macro
def MMA(inst, A_local_buf, B_local_buf, C_local_buf):
def _warp_mma(inst, A_local_buf, B_local_buf, C_local_buf):
for i, j in T.grid(inst.warp_rows, inst.warp_cols):
T.ptx_mma(
inst.accum_dtype,
Expand Down Expand Up @@ -216,9 +193,8 @@ def MMA(inst, A_local_buf, B_local_buf, C_local_buf):
# MMA Store must be in simulated instead of TVM Intrins
# As TVM Intrins is like a hack that the threadIdx.x should be always
# equal to the warp_size
@staticmethod
@T.macro
def STMATRIX(inst, C_local_buf, C_shared_buf, thread_bindings):
def _warp_stmatrix(inst, C_local_buf, C_shared_buf, thread_bindings):
tx = thread_bindings % inst.WARP_SIZE
ty = (thread_bindings // inst.WARP_SIZE) % inst.block_row_warps
tz = (thread_bindings // (inst.WARP_SIZE * inst.block_row_warps)) % inst.block_col_warps
Expand All @@ -231,55 +207,25 @@ def STMATRIX(inst, C_local_buf, C_shared_buf, thread_bindings):
col] = C_local_buf[i * (inst.warp_cols * inst.local_size_out) +
j * inst.local_size_out + local_id]

# Allow GEMM from shared memory to local memory
@staticmethod
@T.macro
def GEMM_SS(inst, A_shared_buf, B_shared_buf, C_local_buf, thread_bindings):
# TODO(lei): alloc_buffer within the macro is not supported yet.
A_local_buf = T.alloc_fragment((inst.warp_rows * inst.local_size_a),
inst.a_dtype,
scope="local")
B_local_buf = T.alloc_fragment((inst.warp_cols * inst.local_size_b),
inst.b_dtype,
scope="local")
for ki in T.serial(0, (inst.chunk // inst.micro_size_k)):
inst.LDMATRIX_A(
inst,
A_local_buf,
A_shared_buf,
ki,
thread_bindings=thread_bindings,
)
def ldmatrix_a(self, A_local_buf, A_shared_buf, ki, thread_bindings, rk=0):
return self._warp_ldmatrix_a(self, A_local_buf, A_shared_buf, ki, thread_bindings, rk)

inst.LDMATRIX_B(
inst,
B_local_buf,
B_shared_buf,
ki,
thread_bindings=thread_bindings,
)
def ldmatrix_b(self, B_local_buf, B_shared_buf, ki, thread_bindings, rk=0):
return self._warp_ldmatrix_b(self, B_local_buf, B_shared_buf, ki, thread_bindings, rk)

def mma(self, A_local_buf, B_local_buf, C_local_buf):
return self._warp_mma(self, A_local_buf, B_local_buf, C_local_buf)

inst.MMA(inst, A_local_buf, B_local_buf, C_local_buf)
def stmatrix(self, C_local_buf, C_shared_buf, thread_bindings):
return self._warp_stmatrix(self, C_local_buf, C_shared_buf, thread_bindings)


class TensorCorePTXMacroGeneratorWithLadderTransform(object):
class TensorCoreIntrinEmitterWithLadderTransform(TensorCoreIntrinEmitter):
"""
To eliminate Python syntax within TIR Macro.
With Ladder Transform Plugin.
"""

M_DIM = 16
N_DIM = 16
WARP_SIZE = 32
dtype_abbrv = {
"float16": "fp16",
"bfloat16": "bf16",
"float32": "fp32",
"int8": "int8",
"int32": "int32",
"e4m3_float8": "e4m3",
"e5m2_float8": "e5m2",
}

def __init__(
self,
a_dtype="float16",
Expand All @@ -297,28 +243,21 @@ def __init__(
transform_kind_b: Union[int, TransformKind] = 0,
num_elems_per_byte=1,
):
self.a_dtype = a_dtype
self.b_dtype = b_dtype
self.accum_dtype = accum_dtype
self.a_transposed = a_transposed
self.b_transposed = b_transposed
# Hint Information
self.block_row_warps = block_row_warps
self.block_col_warps = block_col_warps
self.warp_row_tiles = warp_row_tiles
self.warp_col_tiles = warp_col_tiles
self.chunk = chunk
self._initialize_k_dim(a_dtype)
self._initialize_abbrev(a_dtype, b_dtype, accum_dtype)
self._initialize_local_size(self.M_DIM, self.N_DIM, self.k_dim, self.WARP_SIZE)
self._initialize_mma_prefix(self.k_dim)
self._initialize_micro_size(self.M_DIM, self.N_DIM, self.k_dim)
self.warp_rows = warp_row_tiles // self.micro_size_x
self.warp_cols = warp_col_tiles // self.micro_size_y
self.reduce_k = reduce_k
self.threads = self.WARP_SIZE * (block_row_warps * block_col_warps) * reduce_k
super().__init__(
a_dtype=a_dtype,
b_dtype=b_dtype,
accum_dtype=accum_dtype,
a_transposed=a_transposed,
b_transposed=b_transposed,
block_row_warps=block_row_warps,
block_col_warps=block_col_warps,
warp_row_tiles=warp_row_tiles,
warp_col_tiles=warp_col_tiles,
chunk=chunk,
reduce_k=reduce_k,
num_elems_per_byte=num_elems_per_byte,
)
self._initialize_transform_kind(transform_kind_a, transform_kind_b)
self.num_elems_per_byte = num_elems_per_byte

def _initialize_k_dim(self, a_dtype="float16"):
self.k_dim = 256 // DataType(a_dtype).bits
Expand Down Expand Up @@ -361,38 +300,13 @@ def _initialize_transform_kind(self, transform_kind_a, transform_kind_b):
else:
raise ValueError("Unsupported transform_kind_b")

assert transform_kind_b in [0, 3], "Currently only support 0 and 3"

@staticmethod
@T.macro
def LDMATRIX_A(
inst,
A_local_buf,
A_shared_buf,
ki,
thread_bindings,
rk=0,
):
stride = A_shared_buf.shape[-1]
tx = thread_bindings % inst.WARP_SIZE
ty = (thread_bindings // inst.WARP_SIZE) % inst.block_row_warps
if self.transform_kind_a != TransformKind.NonTransform:
raise ValueError("TransformKind A is not supported yet")

for i in T.serial(inst.warp_rows):
T.ptx_ldmatrix(
inst.a_dtype,
T.bool(False),
4,
".b16",
A_local_buf.data,
i * inst.local_size_a,
T.address_of(A_shared_buf[ty * inst.warp_row_tiles + i * inst.micro_size_x,
rk * inst.chunk + ki * inst.micro_size_k,]),
get_ldmatrix_offset("A", tx, 0, stride, inst.a_dtype, inst.a_transposed),
)
assert transform_kind_b in [0, 3], "Currently only support 0 and 3"

@staticmethod
@T.macro
def LDMATRIX_B(
def _warp_ldmatrix_b(
inst,
B_local_buf,
B_shared_buf,
Expand Down Expand Up @@ -436,9 +350,8 @@ def LDMATRIX_B(
B_local_buf[j * local_size_dequantize + local_id] = B_shared_buf[ri, rj, rii,
rjj]

@staticmethod
@T.macro
def MMA(inst, A_local_buf, B_local_buf, C_local_buf):
def _warp_mma(inst, A_local_buf, B_local_buf, C_local_buf):
for i, j in T.grid(inst.warp_rows, inst.warp_cols):
T.ptx_mma(
inst.accum_dtype,
Expand Down Expand Up @@ -475,51 +388,8 @@ def MMA(inst, A_local_buf, B_local_buf, C_local_buf):
T.bool(False),
)

# STS
# MMA Store must be in simulated instead of TVM Intrins
# As TVM Intrins is like a hack that the threadIdx.x should be always
# equal to the warp_size
@staticmethod
@T.macro
def STMATRIX(inst, C_local_buf, C_shared_buf, thread_bindings):
tx = thread_bindings % inst.WARP_SIZE
ty = (thread_bindings // inst.WARP_SIZE) % inst.block_row_warps
tz = (thread_bindings // (inst.WARP_SIZE * inst.block_row_warps)) % inst.block_col_warps
for i, j in T.grid(inst.warp_rows, inst.warp_cols):
for local_id_o in T.serial(inst.local_size_out // 2):
for local_id_i in T.vectorized(2):
local_id = local_id_o * 2 + local_id_i
row, col = T.meta_var(mma_store_index_map(tx, local_id))
C_shared_buf[ty * inst.warp_rows + i, tz * inst.warp_cols + j, row,
col] = C_local_buf[i * (inst.warp_cols * inst.local_size_out) +
j * inst.local_size_out + local_id]

# Allow GEMM from shared memory to local memory
@staticmethod
@T.macro
def GEMM_SS(inst, A_shared_buf, B_shared_buf, C_local_buf, thread_bindings):
# TODO(lei): alloc_buffer within the macro is not supported yet.
A_local_buf = T.alloc_fragment((inst.warp_rows * inst.local_size_a),
inst.a_dtype,
scope="local")
B_local_buf = T.alloc_fragment((inst.warp_cols * inst.local_size_b),
inst.b_dtype,
scope="local")
for ki in T.serial(0, (inst.chunk // inst.micro_size_k)):
inst.LDMATRIX_A(
inst,
A_local_buf,
A_shared_buf,
ki,
thread_bindings=thread_bindings,
)

inst.LDMATRIX_B(
inst,
B_local_buf,
B_shared_buf,
ki,
thread_bindings=thread_bindings,
)
def ldmatrix_b(self, B_local_buf, B_shared_buf, ki, thread_bindings, rk=0):
return self._warp_ldmatrix_b(self, B_local_buf, B_shared_buf, ki, thread_bindings, rk)

inst.MMA(inst, A_local_buf, B_local_buf, C_local_buf)
def mma(self, A_local_buf, B_local_buf, C_local_buf):
return self._warp_mma(self, A_local_buf, B_local_buf, C_local_buf)
Loading