-
Notifications
You must be signed in to change notification settings - Fork 16
[Feature] wgmma instructions for Hopper #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
d578631
upd
soodoshll 5ae6d16
add instructions
soodoshll 6ab05f8
add inference rules
soodoshll f2cfb81
matrix d layout
soodoshll 0d2d84b
upd
soodoshll 9b28995
runnable
soodoshll b81c9c2
upd
soodoshll 0119b66
upd
soodoshll 01237e5
upd
soodoshll 088a2b0
format
soodoshll 1356e89
remove print
soodoshll 7ea0aba
add test
soodoshll 097a6c3
fix
soodoshll 2c4e787
format
soodoshll e5b11ec
upd
soodoshll 5c01c3e
fix
soodoshll File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import math | ||
|
|
||
| import pandas | ||
| import tilus | ||
| import torch | ||
| from tilus import float16, float32, int32, uint32 | ||
| from tilus.utils import benchmark_func, cdiv | ||
|
|
||
|
|
||
| @tilus.autotune( | ||
| "block_m, block_n", [(64, 128), (128, 128), (128, 256), (256, 128), (256, 256)] | ||
| ) | ||
| @tilus.autotune("block_k", [16, 32, 64]) | ||
| class MatmulTMA(tilus.Script): | ||
| def __init__( | ||
| self, | ||
| block_m, | ||
| block_n, | ||
| block_k, | ||
| ): | ||
| super().__init__() | ||
| self.block_m = block_m | ||
| self.block_n = block_n | ||
| self.block_k = block_k | ||
|
|
||
| def __call__( | ||
| self, | ||
| m_size: int32, | ||
| n_size: int, | ||
| k_size: int, | ||
| a_ptr: ~float16, | ||
| b_ptr: ~float16, | ||
| c_ptr: ~float16, | ||
| ): | ||
| self.attrs.blocks = [ | ||
| cdiv(m_size, self.block_m), | ||
| cdiv(n_size, self.block_n), | ||
| ] | ||
| self.attrs.warps = 4 | ||
|
|
||
| block_m, block_n, block_k = self.block_m, self.block_n, self.block_k | ||
| offset_m: int32 = block_m * self.blockIdx.x | ||
| offset_n: int32 = block_n * self.blockIdx.y | ||
|
|
||
| ga = self.global_view(a_ptr, dtype=float16, shape=[m_size, k_size]) | ||
| gb = self.global_view(b_ptr, dtype=float16, shape=[n_size, k_size]) | ||
| sa = self.shared_tensor(dtype=float16, shape=[block_m, block_k]) | ||
| sb = self.shared_tensor(dtype=float16, shape=[block_n, block_k]) | ||
| acc = self.register_tensor(dtype=float32, shape=[block_m, block_n], init=0.0) | ||
|
|
||
| tma_barrier = self.mbarrier.alloc(count=[2]) | ||
| phase: uint32 = 0 | ||
|
|
||
| for offset_k in range(0, k_size, block_k): | ||
| # issue asynchronous copy instructions to load tiles of A and B | ||
| with self.single_thread(): | ||
| self.tma.global_to_shared( | ||
| src=ga, dst=sa, offsets=[offset_m, offset_k], mbarrier=tma_barrier | ||
| ) | ||
| self.tma.global_to_shared( | ||
| src=gb, dst=sb, offsets=[offset_n, offset_k], mbarrier=tma_barrier | ||
| ) | ||
| self.mbarrier.wait(tma_barrier, phase=phase) | ||
|
|
||
| # synchronize threads in the block to ensure data is available in shared memory | ||
| self.sync() | ||
|
|
||
| a = self.load_shared(sa) | ||
| b = self.load_shared(sb) | ||
| self.dot(a, b.transpose(), acc, out=acc) | ||
| self.sync() | ||
| phase ^= 1 | ||
|
|
||
| self.free_shared(sa) | ||
| self.free_shared(sb) | ||
|
|
||
| casted_acc = self.cast(acc, dtype=float16) | ||
| gc = self.global_view(c_ptr, dtype=float16, shape=[m_size, n_size]) | ||
| self.store_global(gc, casted_acc, offsets=[offset_m, offset_n]) | ||
|
|
||
|
|
||
| def main(): | ||
| headers = ["m", "n", "k", "name", "latency (ms)", "tflops"] | ||
| workloads = [ | ||
| [4096, 4096, 4096], | ||
| ] | ||
|
|
||
| rows = [] | ||
| for m, n, k in workloads: | ||
| matmul = MatmulTMA() | ||
|
|
||
| a = (torch.rand(m, k, dtype=torch.float16).cuda() - 0.5) / math.sqrt(k) | ||
| b = (torch.rand(n, k, dtype=torch.float16).cuda() - 0.5) / math.sqrt(k) | ||
| c_actual = torch.empty(m, n, dtype=torch.float16).cuda() | ||
| c_expect = a @ b.T | ||
| matmul(m, n, k, a, b, c_actual) | ||
| torch.cuda.synchronize() | ||
|
|
||
| # check correctness | ||
| torch.testing.assert_close(c_expect, c_actual) | ||
|
|
||
| # benchmark | ||
| for name, func in [ | ||
| ("torch", lambda: torch.matmul(a, b.T, out=c_expect)), | ||
| ("tilus", lambda: matmul(m, n, k, a, b, c_actual)), | ||
| ]: | ||
| latency = benchmark_func(func, warmup=5, repeat=20) | ||
| tflops = 2 * m * n * k / latency * 1e-9 | ||
| rows.append([m, n, k, name, latency, tflops]) | ||
|
|
||
| df = pandas.DataFrame(rows, columns=headers) | ||
| print(df) | ||
|
|
||
|
|
||
| # %% | ||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import math | ||
|
|
||
| import pandas | ||
| import tilus | ||
| import torch | ||
| from tilus import float16, float32, int32, uint32 | ||
| from tilus.utils import benchmark_func, cdiv | ||
|
|
||
| tilus.option.cache_dir("./cache") | ||
| tilus.option.debug.dump_ir(True) | ||
| torch.set_printoptions(precision=3, sci_mode=False, linewidth=160) | ||
|
|
||
|
|
||
| @tilus.autotune( | ||
| "block_m, block_n", [(64, 128), (128, 128), (128, 256), (256, 128), (256, 256)] | ||
| ) | ||
| @tilus.autotune("block_k", [16, 32, 64]) | ||
| class MatmulWGMMA(tilus.Script): | ||
| def __init__( | ||
| self, | ||
| block_m, | ||
| block_n, | ||
| block_k, | ||
| ): | ||
| super().__init__() | ||
| self.block_m = block_m | ||
| self.block_n = block_n | ||
| self.block_k = block_k | ||
|
|
||
| def __call__( | ||
| self, | ||
| m_size: int32, | ||
| n_size: int, | ||
| k_size: int, | ||
| a_ptr: ~float16, | ||
| b_ptr: ~float16, | ||
| c_ptr: ~float16, | ||
| ): | ||
| self.attrs.blocks = [ | ||
| cdiv(m_size, self.block_m), | ||
| cdiv(n_size, self.block_n), | ||
| ] | ||
| self.attrs.warps = 4 | ||
|
|
||
| block_m, block_n, block_k = self.block_m, self.block_n, self.block_k | ||
| offset_m: int32 = block_m * self.blockIdx.x | ||
| offset_n: int32 = block_n * self.blockIdx.y | ||
|
|
||
| ga = self.global_view(a_ptr, dtype=float16, shape=[m_size, k_size]) | ||
| gb = self.global_view(b_ptr, dtype=float16, shape=[n_size, k_size]) | ||
| sa = self.shared_tensor(dtype=float16, shape=[block_m, block_k]) | ||
| sb = self.shared_tensor(dtype=float16, shape=[block_n, block_k]) | ||
| acc = self.register_tensor(dtype=float32, shape=[block_m, block_n], init=0.0) | ||
|
|
||
| tma_barrier = self.mbarrier.alloc(count=[2]) | ||
| phase: uint32 = 0 | ||
|
|
||
| for offset_k in range(0, k_size, block_k): | ||
| # issue asynchronous copy instructions to load tiles of A and B | ||
| with self.single_thread(): | ||
| self.tma.global_to_shared( | ||
| src=ga, dst=sa, offsets=[offset_m, offset_k], mbarrier=tma_barrier | ||
| ) | ||
| self.tma.global_to_shared( | ||
| src=gb, dst=sb, offsets=[offset_n, offset_k], mbarrier=tma_barrier | ||
| ) | ||
| self.mbarrier.wait(tma_barrier, phase=phase) | ||
|
|
||
| # synchronize threads in the block to ensure data is available in shared memory | ||
| self.sync() | ||
|
|
||
| self.wgmma.fence() | ||
| self.wgmma.mma(sa, sb.transpose(), acc) | ||
| self.wgmma.commit_group() | ||
| self.wgmma.wait_group(0) | ||
| self.sync() | ||
| phase ^= 1 | ||
|
|
||
| self.free_shared(sa) | ||
| self.free_shared(sb) | ||
|
|
||
| casted_acc = self.cast(acc, dtype=float16) | ||
| gc = self.global_view(c_ptr, dtype=float16, shape=[m_size, n_size]) | ||
| self.store_global(gc, casted_acc, offsets=[offset_m, offset_n]) | ||
|
|
||
|
|
||
| def main(): | ||
| headers = ["m", "n", "k", "name", "latency (ms)", "tflops"] | ||
| workloads = [ | ||
| [4096, 4096, 4096], | ||
| # [128, 48, 16], | ||
| ] | ||
|
|
||
| rows = [] | ||
| for m, n, k in workloads: | ||
| matmul = MatmulWGMMA() | ||
|
|
||
| a = (torch.rand(m, k, dtype=torch.float16).cuda() - 0.5) / math.sqrt(k) | ||
| b = (torch.rand(n, k, dtype=torch.float16).cuda() - 0.5) / math.sqrt(k) | ||
| c_actual = torch.empty(m, n, dtype=torch.float16).cuda() | ||
| c_expect = a @ b.T | ||
| matmul(m, n, k, a, b, c_actual) | ||
| torch.cuda.synchronize() | ||
|
|
||
| # check correctness | ||
| torch.testing.assert_close(c_expect, c_actual) | ||
|
|
||
| # benchmark | ||
| for name, func in [ | ||
| ("torch", lambda: torch.matmul(a, b.T, out=c_expect)), | ||
| ("tilus", lambda: matmul(m, n, k, a, b, c_actual)), | ||
| ]: | ||
| latency = benchmark_func(func, warmup=5, repeat=20) | ||
| tflops = 2 * m * n * k / latency * 1e-9 | ||
| rows.append([m, n, k, name, latency, tflops]) | ||
|
|
||
| df = pandas.DataFrame(rows, columns=headers) | ||
| print(df) | ||
|
|
||
|
|
||
| # %% | ||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,4 +24,5 @@ | |
| semaphore, | ||
| simt_dot, | ||
| tcgen05, | ||
| wgmma, | ||
| ) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.