|
| 1 | +""" |
| 2 | +BF16 x INT16 GEMM with Helion |
| 3 | +============================================================ |
| 4 | +The kernel performs matrix multiplication where one matrix is in bfloat16 format and the other is in int16 format. |
| 5 | +The int16 values are converted to bfloat16 before performing the matrix multiplication. |
| 6 | +""" |
| 7 | + |
| 8 | +# %% |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +from typing import Callable |
| 12 | + |
| 13 | +import torch |
| 14 | +from torch import Tensor |
| 15 | + |
| 16 | +import helion |
| 17 | +import helion.language as hl |
| 18 | + |
| 19 | + |
| 20 | +# %% |
| 21 | +@helion.kernel(static_shapes=True) |
| 22 | +def _bf16xint16_gemm(x: Tensor, w: Tensor) -> Tensor: |
| 23 | + """ |
| 24 | + x is bf16, w is int16. |
| 25 | + """ |
| 26 | + M, K = x.shape |
| 27 | + K2, N = w.shape |
| 28 | + assert K == K2, f"size mismatch {K} != {K2}" |
| 29 | + |
| 30 | + out = torch.empty([M, N], dtype=torch.bfloat16, device=x.device) |
| 31 | + |
| 32 | + for tile_m, tile_n in hl.tile([M, N]): |
| 33 | + acc = hl.zeros([tile_m, tile_n], dtype=torch.float32) |
| 34 | + for tile_k in hl.tile(K): |
| 35 | + x_tile = x[tile_m, tile_k] |
| 36 | + w_tile = w[tile_k, tile_n].to(torch.bfloat16) |
| 37 | + acc = hl.dot(x_tile, w_tile, acc=acc) |
| 38 | + out[tile_m, tile_n] = acc.to(torch.bfloat16) |
| 39 | + |
| 40 | + return out |
| 41 | + |
| 42 | + |
| 43 | +# %% |
| 44 | +@helion.kernel(static_shapes=True) |
| 45 | +def _int16xbf16_gemm(x: Tensor, w: Tensor) -> Tensor: |
| 46 | + """ |
| 47 | + x is int16, w is bf16. |
| 48 | + """ |
| 49 | + M, K = x.shape |
| 50 | + K2, N = w.shape |
| 51 | + assert K == K2, f"size mismatch {K} != {K2}" |
| 52 | + |
| 53 | + out = torch.empty([M, N], dtype=torch.bfloat16, device=x.device) |
| 54 | + |
| 55 | + for tile_m, tile_n in hl.tile([M, N]): |
| 56 | + acc = hl.zeros([tile_m, tile_n], dtype=torch.float32) |
| 57 | + for tile_k in hl.tile(K): |
| 58 | + x_tile = x[tile_m, tile_k].to(torch.bfloat16) |
| 59 | + w_tile = w[tile_k, tile_n] |
| 60 | + acc = hl.dot(x_tile, w_tile, acc=acc) |
| 61 | + out[tile_m, tile_n] = acc.to(torch.bfloat16) |
| 62 | + |
| 63 | + return out |
| 64 | + |
| 65 | + |
| 66 | +# %% |
| 67 | +def bf16xint16_gemm(x: Tensor, w: Tensor, transpose: bool = False) -> Tensor: |
| 68 | + """ |
| 69 | + This function dispatches to the appropriate kernel based on the transpose flag. |
| 70 | +
|
| 71 | + Args: |
| 72 | + x (Tensor): Input tensor. |
| 73 | + w (Tensor): Weight tensor. |
| 74 | + transpose (bool): If True, assumes x is int16 and w is bf16. Default: False. |
| 75 | +
|
| 76 | + Returns: |
| 77 | + Tensor: Output tensor in bfloat16 format. |
| 78 | + """ |
| 79 | + if transpose: |
| 80 | + return _int16xbf16_gemm(x, w) |
| 81 | + return _bf16xint16_gemm(x, w) |
| 82 | + |
| 83 | + |
| 84 | +# %% |
| 85 | +def bf16xint16_gemm_tritonbench( |
| 86 | + tb_op: object, x: torch.Tensor, w: torch.Tensor |
| 87 | +) -> Callable[[], torch.Tensor]: |
| 88 | + """ |
| 89 | + Wrapper for TritonBench compatibility. |
| 90 | +
|
| 91 | + Args: |
| 92 | + tb_op: TritonBench operator instance |
| 93 | + x (torch.Tensor): Input tensor in bfloat16 format. |
| 94 | + w (torch.Tensor): Weight tensor in int16 format. |
| 95 | +
|
| 96 | + Returns: |
| 97 | + Callable that returns output tensor in bfloat16 format. |
| 98 | + """ |
| 99 | + # Check if transpose mode based on tritonbench operator |
| 100 | + transpose = getattr(tb_op, "transpose", False) |
| 101 | + |
| 102 | + def run_kernel() -> torch.Tensor: |
| 103 | + return bf16xint16_gemm(x, w, transpose=transpose) |
| 104 | + |
| 105 | + return run_kernel |
| 106 | + |
| 107 | + |
| 108 | +# %% |
| 109 | +def reference_bf16xint16_pytorch( |
| 110 | + x: torch.Tensor, w: torch.Tensor, transpose: bool = False |
| 111 | +) -> torch.Tensor: |
| 112 | + """ |
| 113 | + Reference implementation using PyTorch operations. |
| 114 | +
|
| 115 | + Args: |
| 116 | + x (torch.Tensor): Input tensor. |
| 117 | + w (torch.Tensor): Weight tensor. |
| 118 | + transpose (bool): Transpose mode flag. |
| 119 | +
|
| 120 | + Returns: |
| 121 | + torch.Tensor: Output tensor in bfloat16 format. |
| 122 | + """ |
| 123 | + if transpose: |
| 124 | + x_bf16 = x.to(torch.bfloat16) |
| 125 | + return torch.matmul(x_bf16, w) |
| 126 | + w_bf16 = w.to(torch.bfloat16) |
| 127 | + return torch.matmul(x, w_bf16) |
| 128 | + |
| 129 | + |
| 130 | +# %% |
| 131 | +def check(m: int, k: int, n: int) -> None: |
| 132 | + """ |
| 133 | + Test the bf16 x int16 GEMM implementation against the PyTorch reference. |
| 134 | +
|
| 135 | + Args: |
| 136 | + m (int): Number of rows. |
| 137 | + k (int): Shared dimension. |
| 138 | + n (int): Number of cols. |
| 139 | + """ |
| 140 | + x = torch.randn([m, k], device="cuda", dtype=torch.bfloat16) |
| 141 | + w = torch.randint(-(2**15), 2**15 - 1, (k, n), device="cuda", dtype=torch.int16) |
| 142 | + |
| 143 | + result = bf16xint16_gemm(x, w, transpose=False) |
| 144 | + expected = reference_bf16xint16_pytorch(x, w, transpose=False) |
| 145 | + torch.testing.assert_close(result, expected, rtol=1e-2, atol=1e-2) |
| 146 | + |
| 147 | + x_int16 = torch.randint( |
| 148 | + -(2**15), 2**15 - 1, (m, k), device="cuda", dtype=torch.int16 |
| 149 | + ) |
| 150 | + w_bf16 = torch.randn([k, n], device="cuda", dtype=torch.bfloat16) |
| 151 | + |
| 152 | + result = bf16xint16_gemm(x_int16, w_bf16, transpose=True) |
| 153 | + expected = reference_bf16xint16_pytorch(x_int16, w_bf16, transpose=True) |
| 154 | + torch.testing.assert_close(result, expected, rtol=1e-2, atol=1e-2) |
| 155 | + |
| 156 | + |
| 157 | +# %% |
| 158 | +def main() -> None: |
| 159 | + """ |
| 160 | + Main entry point that runs the bf16xint16 kernel verification with different tensor sizes. |
| 161 | + """ |
| 162 | + check(256, 256, 256) |
| 163 | + check(512, 512, 512) |
| 164 | + check(65536, 1024, 1280) |
| 165 | + |
| 166 | + |
| 167 | +# %% |
| 168 | +if __name__ == "__main__": |
| 169 | + main() |
0 commit comments